-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeStepper.cpp
628 lines (598 loc) · 12.2 KB
/
MeStepper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/**
* \par Copyright (C), 2012-2016, MakeBlock
* \class MeStepper
* \brief Driver for Me Stepper device.
* @file MeStepper.cpp
* @author MakeBlock
* @version V1.0.3
* @date 2016/01/20
* @brief Driver for Stepper device.
*
* \par Copyright
* This software is Copyright (C), 2012-2016, MakeBlock. Use is subject to license \n
* conditions. The main licensing options available are GPL V2 or Commercial: \n
*
* \par Open Source Licensing GPL V2
* This is the appropriate option if you want to share the source code of your \n
* application with everyone you distribute it to, and you also want to give them \n
* the right to share who uses it. If you wish to use this software under Open \n
* Source Licensing, you must contribute all your source code to the open source \n
* community in accordance with the GPL Version 2 when your application is \n
* distributed. See http://www.gnu.org/copyleft/gpl.html
*
* \par Description
* This file is a drive for Me Stepper device.
*
* \par Method List:
*
* 1. void MeStepper::setpin(uint8_t dir_data, uint8_t step_data);
* 2. void MeStepper::moveTo(long absolute);
* 3. void MeStepper::move(long relative);
* 4. boolean MeStepper::run();
* 5. boolean MeStepper::runSpeed();
* 6. void MeStepper::setMaxSpeed(float speed);
* 7. void MeStepper::setAcceleration(float acceleration);
* 8. void MeStepper::setSpeed(float speed);
* 9. float MeStepper::speed();
* 10. long MeStepper::distanceToGo();
* 11. long MeStepper::targetPosition();
* 12. long MeStepper::currentPosition();
* 13. void MeStepper::setCurrentPosition(long position);
* 14. void MeStepper::runToPosition();
* 15. boolean MeStepper::runSpeedToPosition();
* 16. void MeStepper::runToNewPosition(long position);
* 17. void MeStepper::disableOutputs();
* 18. void MeStepper::enableOutputs();
*
* \par History:
* <pre>
* `<Author>` `<Time>` `<Version>` `<Descr>`
* Mark Yan 2015/10/09 1.0.0 Bulid the new
* Mark Yan 2015/10/29 1.0.1 Fix minor errors in format
* forfish 2015/11/11 1.0.2 Add description.
* Mark Yan 2016/01/20 1.0.3 Fix minor errors in comment.
* </pre>
*
* DERIVED FROM
* // AccelStepper.h
* //
* // Copyright (C) 2009-2013 Mike McCauley
* // $Id: AccelStepper.cpp,v 1.23 2016/08/09 00:39:10 mikem Exp $
*/
#include "MeStepper.h"
typedef enum
{
DIRECTION_CCW = 0, ///< Clockwise
DIRECTION_CW = 1 ///< Counter-Clockwise
} Direction;
/**
* Alternate Constructor which can call your own function to map the stepper to arduino port,
* no pins are used or initialized here.
* \param[in]
* None
*/
MeStepper::MeStepper(): MePort(0)
{
}
/**
* Alternate Constructor which can call your own function to map the stepper to arduino port,
* the slot2 pin will be used here since specify slot is not be set.
* \param[in]
* port - RJ25 port from PORT_1 to M2
*/
MeStepper::MeStepper(uint8_t port): MePort(port)
{
_currentPos = 0;
_targetPos = 0;
_acceleration = 0;
_lastStepTime = micros();
_speed = 0;
_dir = DIRECTION_CW;
setCurrentPosition(0);
pinMode(s1,OUTPUT);
pinMode(s2,OUTPUT);
}
/**
* \par Function
* setpin
* \par Description
* Set pin for Stepper.
* \param[in]
* dir_data - The direction data of Stepper's movement.
* \param[in]
* step_data - The command data for Stepper.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void MeStepper::setpin(uint8_t dir_data, uint8_t step_data)
{
_dir_data = dir_data;
_step_data = step_data;
pinMode(_dir_data, OUTPUT);
pinMode(_step_data, OUTPUT);
#ifdef ME_PORT_DEFINED
s1 = _dir_data;
s2 = _step_data;
#endif // ME_PORT_DEFINED
}
/**
* \par Function
* moveTo
* \par Description
* Stepper moves to the aim.
* \param[in]
* absolute - The absolute length to Stepper's movement.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void MeStepper::moveTo(long absolute)
{
if (_targetPos != absolute)
{
_targetPos = absolute;
computeNewSpeed();
}
}
/**
* \par Function
* move
* \par Description
* Stepper moves to the aim.
* \param[in]
* relative - The relative length to Stepper's movement.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void MeStepper::move(long relative)
{
moveTo(_currentPos + relative);
}
/**
* \par Function
* runSpeed
* \par Description
* The speed of Stepper's running.
* \param[in]
* None
* \par Output
* None
* \par Return
* Return true or false.
* \par Others
* None
*/
boolean MeStepper::runSpeed()
{
// Dont do anything unless we actually have a step interval
if (!_stepInterval)
{
return false;
}
if (micros() - _lastStepTime > _stepInterval)
{
if (_dir == DIRECTION_CW)
{
// Clockwise
_currentPos += 1;
}
else
{
// Anticlockwise
_currentPos -= 1;
}
step();
_lastStepTime = micros();
return true;
}
else
{
return false;
}
}
/**
* \par Function
* distanceToGo
* \par Description
* The distance that Stepper should go.
* \param[in]
* None
* \par Output
* None
* \par Return
* Return the length of Stepper's running.
* \par Others
* None
*/
long MeStepper::distanceToGo()
{
return _targetPos - _currentPos;
}
/**
* \par Function
* targetPosition
* \par Description
* Stepper goes to target position.
* \param[in]
* None
* \par Output
* None
* \par Return
* Return the position of Stepper.
* \par Others
* None
*/
long MeStepper::targetPosition()
{
return _targetPos;
}
/**
* \par Function
* currentPosition
* \par Description
* Stepper's current position.
* \param[in]
* None
* \par Output
* None
* \par Return
* Return the current position of Stepper.
* \par Others
* None
*/
long MeStepper::currentPosition()
{
return _currentPos;
}
/**
* \par Function
* setCurrentPosition
* \par Description
* Set Stepper's current position.
* \param[in]
* position - The current position for Stepper.
* \par Output
* None
* \par Return
* Return the current position of Stepper.
* \par Others
* None
*/
void MeStepper::setCurrentPosition(long position)
{
_targetPos = _currentPos = position;
_n = 0;
_stepInterval = 0;
}
/**
* \par Function
* computeNewSpeed
* \par Description
* Compute New Speed of Stepper.
* \param[in]
* None
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void MeStepper::computeNewSpeed()
{
long distanceTo = distanceToGo();
long stepsToStop = (long)((_speed * _speed) / (2.0 * _acceleration));
if (distanceTo == 0 && stepsToStop <= 1)
{
// We are at the target and its time to stop
_stepInterval = 0;
_speed = 0.0;
_n = 0;
return;
}
if (distanceTo > 0)
{
// We are anticlockwise from the target
// Need to go clockwise from here, maybe decelerate now
if (_n > 0)
{
// Currently accelerating, need to decel now? Or maybe going the wrong way?
if ((stepsToStop >= distanceTo) || _dir == DIRECTION_CCW)
{
_n = -stepsToStop; // Start deceleration
}
}
else if (_n < 0)
{
// Currently decelerating, need to accel again?
if ((stepsToStop < distanceTo) && _dir == DIRECTION_CW)
{
_n = -_n; // Start accceleration
}
}
}
else if (distanceTo < 0)
{
// We are clockwise from the target
// Need to go anticlockwise from here, maybe decelerate
if (_n > 0)
{
// Currently accelerating, need to decel now? Or maybe going the wrong way?
if ((stepsToStop >= -distanceTo) || _dir == DIRECTION_CW)
{
_n = -stepsToStop; // Start deceleration
}
}
else if (_n < 0)
{
// Currently decelerating, need to accel again?
if ((stepsToStop < -distanceTo) && _dir == DIRECTION_CCW)
{
_n = -_n; // Start accceleration
}
}
}
// Need to accelerate or decelerate
if (_n == 0)
{
// First step from stopped
_cn = _c0;
_dir = (distanceTo > 0) ? DIRECTION_CW : DIRECTION_CCW;
}
else
{
// Subsequent step. Works for accel (n is +_ve) and decel (n is -ve).
_cn = _cn - ((2.0 * _cn) / ((4.0 * _n) + 1)); // Equation 13
_cn = max(_cn, _cmin);
}
_n++;
_stepInterval = _cn;
_speed = 1000000.0 / _cn;
if (_dir == DIRECTION_CCW)
{
_speed = -_speed;
}
}
/**
* \par Function
* run
* \par Description
* Stepper's status----run or not.
* \param[in]
* None
* \par Output
* None
* \par Return
* Return the status.
* \par Others
* None
*/
boolean MeStepper::run()
{
if((_speed == 0.0) || (distanceToGo() == 0))
{
return false;
}
if (runSpeed())
{
computeNewSpeed();
return true;
}
}
/**
* \par Function
* setMaxSpeed
* \par Description
* Set Max Speed for Stepper.
* \param[in]
* None
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void MeStepper::setMaxSpeed(float speed)
{
if (_maxSpeed != speed)
{
_maxSpeed = speed;
_cmin = 1000000.0 / speed;
// Recompute _n from current speed and adjust speed if accelerating or cruising
if (_n > 0)
{
_n = (long)((_speed * _speed) / (2.0 * _acceleration)); // Equation 16
computeNewSpeed();
}
}
}
/**
* \par Function
* setAcceleration
* \par Description
* Set Acceleration for Stepper.
* \param[in]
* acceleration - The acceleration for Stepper.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void MeStepper::setAcceleration(float acceleration)
{
if(acceleration == 0.0)
{
return;
}
if(_acceleration != acceleration)
{
_n = _n * (_acceleration / acceleration);
// _c0 = sqrt(2.0 / acceleration) * 1000000.0;
// Accelerates at half the expected rate. Why?
_c0 = sqrt(1.0/acceleration) * 1000000.0;
_acceleration = acceleration;
computeNewSpeed();
}
}
/**
* \par Function
* setSpeed
* \par Description
* Set Speed for Stepper.
* \param[in]
* speed - The speed of Stepper.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void MeStepper::setSpeed(float speed)
{
if (speed == _speed)
{
return;
}
speed = constrain(speed, -_maxSpeed, _maxSpeed);
if (speed == 0.0)
{
_stepInterval = 0;
}
else
{
_stepInterval = fabs(1000000.0 /speed);
_dir = (speed > 0.0) ? DIRECTION_CW : DIRECTION_CCW;
}
_speed = speed;
}
/**
* \par Function
* speed
* \par Description
* The Speed of Stepper.
* \param[in]
* None
* \par Output
* None
* \par Return
* Return the Stepper's speed.
* \par Others
* None
*/
float MeStepper::speed()
{
return _speed;
}
/**
* \par Function
* step
* \par Description
* Stepper runs step by step.
* \param[in]
* None
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void MeStepper::step()
{
if(_dir == DIRECTION_CW)
{
digitalWrite(s1,LOW);
}
else
{
digitalWrite(s1,HIGH);
}
digitalWrite(s2, HIGH);
delayMicroseconds(1);
digitalWrite(s2, LOW);
}
/**
* \par Function
* runToPosition
* \par Description
* Stepper runs to position.
* \param[in]
* None
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void MeStepper::runToPosition()
{
while (run())
{
;
}
}
/**
* \par Function
* runSpeedToPosition
* \par Description
* The speed of Stepper on the way to position.
* \param[in]
* None
* \par Output
* None
* \par Return
* Return true or false.
* \par Others
* None
*/
boolean MeStepper::runSpeedToPosition()
{
if (_targetPos == _currentPos)
{
return false;
}
if (_targetPos >_currentPos)
{
_dir = DIRECTION_CW;
}
else
{
_dir = DIRECTION_CCW;
}
return runSpeed();
}
/**
* \par Function
* runToNewPosition
* \par Description
* The Stepper runs to new position.
* \param[in]
* None
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void MeStepper::runToNewPosition(long position)
{
moveTo(position);
runToPosition();
}