-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyStepperController.cpp
281 lines (240 loc) · 6.59 KB
/
MyStepperController.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
#include "MyStepperController.h"
MyStepperController::MyStepperController(UINT stepsPerRev, SHORT pinStep, SHORT pinDir)
: stepsPerRev(stepsPerRev),pinStep(pinStep), pinDir(pinDir)
{
pinMode(pinStep, OUTPUT);
pinMode(pinDir, OUTPUT);
digitalWrite(pinDir, LOW);
digitalWrite(pinStep, LOW);
}
void MyStepperController::run()
{
calcSpeed();
calcDirection();
runSpeed();
}
void MyStepperController::moveTo(LONG targetStep)
{
ctrlMode = POSITIONING;
targetPos = targetStep;
// Serial.print("\n at pos: ");
// Serial.print(stepCount);
// Serial.print("\n goto pos: ");
// Serial.print(targetStep);
}
void MyStepperController::setTargetSpeed(FLOAT ts)
{
if (ctrlMode != POSITIONING)
targetSpeed = ts;
}
void MyStepperController::setMaxSpeed(FLOAT speed)
{
maxSpeed = abs(speed);
}
void MyStepperController::setAcceleration(UINT accel)
{
acceleration = accel;
}
LONG MyStepperController::getStepCount(void)
{
return stepCount;
}
FLOAT MyStepperController::getCurrentSpeed(void)
{
return currSpeed;
}
BOOL MyStepperController::isRunning(void)
{
return (STOP != rotDir);
}
void MyStepperController::stop(void)
{
setTargetSpeed(0.0);
ctrlMode = SPEED_CTRL;
}
CTRL_MODE MyStepperController::getCtrlMode(void)
{
return ctrlMode;
}
DIRECTION MyStepperController::getRotDir(void)
{
return rotDir;
}
void MyStepperController::setLowerLimit(LONG pos)
{
lowerLimit = pos;
}
void MyStepperController::setUpperLimit(LONG pos)
{
upperLimit = pos;
}
void MyStepperController::setStepCount(LONG pos)
{
stepCount = pos;
}
void MyStepperController::setLimitProtectionEnabled(BOOL enbl)
{
limitProtectionEnabled = enbl;
}
void MyStepperController::doStep(DIRECTION direction)
{
switch (direction)
{
case STOP:
return;
case CLOCKWISE:
/* write DIR pin */
digitalWrite(pinDir, LOW);
stepCount++;
break;
case COUNTERCLOCKWISE:
/* write DIR pin */
digitalWrite(pinDir, HIGH);
stepCount--;
break;
}
/* create pulse on STEP */
digitalWrite(pinStep, HIGH);
delayMicroseconds(PULSE_DURATION);
digitalWrite(pinStep, LOW);
}
void MyStepperController::runSpeed(void)
{
static ULONG tPulseBegin; // time when the last step pulse has been done
ULONG time = micros();
if(rotDir == STOP)
n = 0; // used for speed calculation of the first step
if (time - tPulseBegin >= tPeriod)
{
doStep(rotDir);
tPeriod = getNewPeriod();
tPulseBegin = time;
}
}
ULONG MyStepperController::getNewPeriod()
{
static ULONG rest;
FLOAT fnew, f;
ULONG tPeriodNew;
INT i;
ULONG currfrequency = abs(currSpeed);
if (rotDir != STOP)
{
if(n==0)
{
/* calculate the first period of acceleration according to the approach introduced in
* http://ww1.microchip.com/downloads/en/AppNotes/doc8017.pdf because otherwise the
* first period will be very long if accerlation is slow in the first moment.
* However I will not follow the whole approach since I want to change speed more
* frequent and a the simple period calculation T= 1s/f works sufficient */
tPeriodNew = (FLOAT)SECOND_US*sqrt(2.0f/(FLOAT)acceleration);
}
else
{
/* use simple period calculation T= 1s/f after first period */
tPeriodNew = ((FLOAT)SECOND_US / (FLOAT)currfrequency);
}
n++;
}
else
{
tPeriodNew = 0;
}
return tPeriodNew;
}
/* calculates the current speed depending on the user given target speed
* considering the acceleration */
void MyStepperController::calcSpeed(void)
{
/* 1. if we want to run to a position we just modify the target speed
* to the max allowed speed */
if (POSITIONING == ctrlMode)
{
if (stepCount < targetPos)
targetSpeed = maxSpeed;
else if (stepCount > targetPos)
targetSpeed = -maxSpeed;
else /* target position reached, go back to SPEED_CTRL */
ctrlMode = SPEED_CTRL;
}
/* limit target speed to max speed */
targetSpeed = constrain(targetSpeed, -maxSpeed, maxSpeed);
/* 2. next step is to gradually ramp the current speed to targetspeed */
rampSpeed();
/* 3. after that we reduce the speed in case we get near the limits if
* limit pretection is enabled */
if (true == limitProtectionEnabled)
{
if(currSpeed > 0.0f)
currSpeed = limitSpeed(upperLimit - stepCount);
else
currSpeed = limitSpeed(lowerLimit - stepCount);
}
/* 4. and if we are running towards a position we do the same with the
* target position instead of the limit */
if (POSITIONING == ctrlMode)
currSpeed = limitSpeed(targetPos - stepCount);
}
void MyStepperController::rampSpeed(void)
{
/* generate a linar ramp up/down */
static ULONG tlastRun = 0;
ULONG time = millis();
ULONG dt = time - tlastRun;
if (dt != 0)
{
if (currSpeed < targetSpeed)
{
/* accelerate: v = v0 + a*dt */
currSpeed += (dt/SECOND_MS * acceleration) ;
/* limit to targetspeed */
currSpeed = min(currSpeed, targetSpeed);
}
else
{
/* decellerate: v = v0 - a*dt */
currSpeed -= (dt/SECOND_MS * acceleration);
/* limit to targetspeed */
currSpeed = max(currSpeed, targetSpeed);
}
tlastRun = time;
}
}
FLOAT MyStepperController::limitSpeed(LONG distToGo)
{
FLOAT retVal;
FLOAT limitedSpeed;
if (distToGo != 0)
{
limitedSpeed = sqrt(2 * abs(distToGo) * acceleration);
/* if speed and distance have same sign then speed needs to be limited */
if( currSpeed > 0.0 && distToGo > 0)
retVal = min(limitedSpeed, currSpeed);
else if ( currSpeed < 0.0 && distToGo < 0)
retVal = max(-limitedSpeed, currSpeed);
else
retVal = currSpeed;
}
else
{
retVal = 0.0f;
}
return retVal;
}
void MyStepperController::calcDirection(void)
{
/* since speed variables are FLOAT which is never exactly zero we need to have a threshold
* below the motor speed shall be considered as zero */
if (abs(currSpeed) < (FLOAT)STOP_THRESHOLD)
{
rotDir = STOP;
}
else if (currSpeed > 0.0f)
{
rotDir = CLOCKWISE;
}
else
{
rotDir = COUNTERCLOCKWISE;
}
}