-
Notifications
You must be signed in to change notification settings - Fork 0
/
StepperMotor2.cpp
350 lines (297 loc) · 8.66 KB
/
StepperMotor2.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
/**
Class to control stepper motor movement
S.Slonevskiy
May 2014
**/
#include "Arduino.h"
#include "StepperMotor2.h"
/*
Constructor
*/
StepperMotor2::StepperMotor2(int pin1, int pin2, int pin3, int pin4, int calPin, int maxAngleDeg, int stepStyle)
{
// Assign pins
_pins[0] = pin1;
_pins[1] = pin2;
_pins[2] = pin3;
_pins[3] = pin4;
_calPin = calPin;
// Init pins to digital output
for (int count = 0; count < 4; count++) {
pinMode(_pins[count], OUTPUT);
}
// Init calibration pin
pinMode(_calPin, INPUT_PULLUP);
// Define stepping technique
if (stepStyle==SM2_HALFSTEP) {
_numStepStates = NELEMS(__steppingHalf);
for (int ii=0; ii<_numStepStates; ii++) {
_stepStates[ii] = __steppingHalf[ii];
}
}
else { // if (stepStyle==SM2_FULLSTEP) {
_numStepStates = NELEMS(__steppingFull);
for (int ii=0; ii<_numStepStates; ii++) {
_stepStates[ii] = __steppingFull[ii];
}
}
/* WARNING! DO NOT PUT ANY Serial.print STATEMENTS IN THIS SECTION! */
/* At the moment of construction of this function, serial interface is */
/* not initialized yet, so it hangs the entire Teensy */
// Init internal delay counter
_previousStepMicros = 0;
// Init go-to location as false
_gotoActive = false;
_gotoPosition = 0;
// Init motor characteristcs
// Note: 512 * _numStepStates = 360 degrees
_maxPosition = (unsigned long)512 * (unsigned long)_numStepStates * (unsigned long)maxAngleDeg / 360;
_udegreesStepSize = (unsigned long)360 * (unsigned long)1000000 / ( (unsigned long)512 * (unsigned long)_numStepStates );
// Setup calibration mode
setupCal();
}
/*
Use in debugging...
*/
void StepperMotor2::debugInternalParams()
{
Serial.print("SM2 Init: _maxPosition ");
Serial.println(_maxPosition);
Serial.print("SM2 Init: _udegreesStepSize ");
Serial.println(_udegreesStepSize);
Serial.print("SM2 Init: _stepDelay ");
Serial.println(_stepDelay);
}
/*
Start continuous motion
int directionSpeedDPS sets motion speed in degrees/second, sign signifies direction
*/
void StepperMotor2::run(int directionSpeedDPS)
{
if (!_isCalibrationMode) {
_gotoActive = false;
if (directionSpeedDPS==0) {
_stepDirection = 0;
}
else {
_stepDirection = (directionSpeedDPS>0) ? +1 : -1;
_stepDelay = _udegreesStepSize / abs(directionSpeedDPS); //SM2_MAX_DELAY-abs(speedDirection);
//Serial.print("Step delay at ");
//Serial.print(_stepDelay);
//Serial.println();
}
}
}
/*
Move stepper one semi-step, no delay, no repetitive motion
Stops any continuous runs currently on
int direction sets direction of the step, magnitude of the parameter is ignored
*/
void StepperMotor2::step(int direction)
{
if (!_isCalibrationMode) {
_gotoActive = false;
_stepDirection = 0;
//_currentStepState = _currentStepState + 1;
if (direction<0 || direction>0) {
_currentPosition += (direction>0) ? +1 : -1;
writeStepToPins();
//Serial.print("StepperMotor2: stepped to position ");
//Serial.println(_currentPosition);
}
}
}
/*
Stop any current motion, except calibration
*/
void StepperMotor2::stop()
{
if (!_isCalibrationMode) {
_stepDirection = 0;
}
}
/*
Goto a sepcified position in the specified amount of time
int newPosition step number to go to
unsigned int msecToComplete mseconds to complete this operation
*/
void StepperMotor2::gotoPosition(int newPosition, unsigned int msecToComplete)
{
if (!_isCalibrationMode) {
_gotoActive = true;
_gotoPosition = newPosition;
_gofromPosition = _currentPosition;
_gotoMsecToComplete = msecToComplete;
if (newPosition>_currentPosition) {
_stepDirection = +1;
}
else if (newPosition<_currentPosition) {
_stepDirection = -1;
}
else {
_stepDirection = 0;
_gotoActive = false;
}
//requestedSpeedDPS = (_gotoPosition-_currentPosition) * _udegreesStepSize / (timeToComplete*1e6);
//_stepDelay = _udegreesStepSize / requestedSpeedDPS;
// _stepDelay is in units of usec / step
_stepDelay = ((unsigned long)msecToComplete*1e3) / (unsigned long)abs(_gotoPosition-_currentPosition);
Serial.print("StepperMotor2: go to position ");
Serial.print(_gotoPosition);
Serial.print(" with step delay of ");
Serial.print(_stepDelay);
Serial.print(" usec, from current position of ");
Serial.print(_currentPosition);
Serial.print(", with step direction ");
Serial.println(_stepDirection);
}
}
/*
Invoke recalibration mode
*/
void StepperMotor2::recal()
{
setupCal();
}
/*
Move stepper motor one step (when elapsed delay time passed)
Must be called from the loop() function
*/
void StepperMotor2::move()
{
unsigned long currentMicros = micros();
if((currentMicros - _previousStepMicros > _stepDelay) && (_stepDirection!=0)) {
_previousStepMicros = currentMicros;
//_currentStepState = _currentStepState + _stepDirection;
_currentPosition += _stepDirection;
writeStepToPins();
}
if (_isCalibrationMode) {
// Check calibration pin
// If there, then define this as position 0, and go to position where we started
bool calState = digitalRead(_calPin);
if (!calState) {
// When cal pin goes low we are there
_gotoPosition = -_currentPosition;
_currentPosition = 0;
_isCalibrationMode = false;
gotoPosition(_gotoPosition,1500);
Serial.println("StepperMotor2: calibration finished.");
}
}
else {
// Goto location logic
if (_gotoActive && _gotoPosition==_currentPosition) {
_gotoActive = false;
_stepDirection = 0;
Serial.println("StepperMotor2: go to position reached");
}
else {
//_stepDelay = ((unsigned long)msecToComplete*1e3) / (unsigned long)abs(_gotoPosition-_currentPosition);
if (_gotoActive) {
_stepDelay = sineEaseInOut(abs(_gotoPosition - _currentPosition), abs(_gotoPosition - _gofromPosition), _gotoMsecToComplete);
if (_stepDelay<SM2_MIN_DELAY) { _stepDelay = SM2_MIN_DELAY; }
if (_stepDelay>SM2_MAX_DELAY) { _stepDelay = SM2_MAX_DELAY; }
}
}
}
}
/*
PRIVATE
*/
/*
Current step number is translated to the high/low state on four stepper wires
Rotation edges are enfored here
*/
void StepperMotor2::writeStepToPins()
{
/*
if (_currentStepState>_numStepStates-1) {
_currentStepState = 0;
_currentPosition++;
//Serial.print("StepperMotor2: position ");
//Serial.println(_currentPosition);
}
if (_currentStepState<0) {
_currentStepState = _numStepStates-1;
_currentPosition--;
//Serial.print("StepperMotor2: position ");
//Serial.println(_currentPosition);
}
*/
if (_currentPosition>=0) {
_currentStepState = _currentPosition % _numStepStates;
}
else {
_currentStepState = _numStepStates - ((-_currentPosition) % _numStepStates);
}
if (!_isCalibrationMode && ((_currentPosition<=0) || (_currentPosition>=_maxPosition))) {
// Cannot move anywhere we are at the either edge so stop
stop();
// If edge was overjumped, then reset
if (_currentPosition<=0) _currentPosition=0;
if (_currentPosition>=_maxPosition) _currentPosition=_maxPosition;
Serial.print("StepperMotor2: reached the edge, position ");
Serial.println(_currentPosition);
return;
}
unsigned int mask = 0001;
for (int count = 0; count < 4; count++) {
if (_stepStates[_currentStepState] & mask) {
digitalWrite(_pins[count], HIGH);
}
else {
digitalWrite(_pins[count], LOW);
}
mask <<= 1;
}
}
/*
Setup parameters required for calibration
*/
void StepperMotor2::setupCal()
{
_currentPosition = 0;
_stepDirection = -1; // counter-clockwise turn until calibration event is triggered
_stepDelay = _udegreesStepSize / SM2_CAL_SPEED_DPS; // calibration mode can be pretty fast
_isCalibrationMode = true; // make sure cal mode is on
}
/*
Sine in/out easing used in gotoPosition function
int p remaining steps
int c total number of steps to make
unsigned int d time to complete operation
return delay time in microseconds
*/
unsigned long StepperMotor2::sineEaseInOut(int p, int c, unsigned int d)
{
long aa = (1000-2*((long)p*1000)/(long)c);
unsigned long bb = 1000000-(unsigned long)(aa*aa);
unsigned long cc = isqrt(bb);
//unsigned long dt = 1000000*(unsigned long)d/cc*2/c/3;
unsigned long dt = 1000000/cc*(unsigned long)d*2/c/3;
return dt;
}
/*
Integer square root
unsigned long x integer to take a square root of
return square root of a number
*/
unsigned long StepperMotor2::isqrt(unsigned long x)
{
unsigned long op, res, one;
op = x;
res = 0;
/* "one" starts at the highest power of four <= than the argument. */
one = (unsigned long)1 << 30; /* second-to-top bit set */
while (one > op) one >>= 2;
while (one != 0) {
if (op >= res + one) {
op -= res + one;
res += one << 1; // <-- faster than 2 * one
}
res >>= 1;
one >>= 2;
}
return res;
}