-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathStepperController.cpp
333 lines (290 loc) · 9.69 KB
/
StepperController.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
/*
StepperController.cpp
Copyright (C) 2017 Niryo
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "StepperController.h"
StepperController::StepperController()
{
current_step_number = 0;
goal_step_number = 0;
delay_between_two_updates = STEPPER_DELAY_MIN;
time_last_update = micros();
time_last_step = micros();
// default config below
// cmd will be reached after 0.1 sec (will make trajectory smoothier if send cmd > 10 Hz)
// if you want/need to send cmd at a lower rate, update this value
micros_to_reach_goal = 100000; // todo get from CAN
control_mode = STEPPER_CONTROL_MODE_RELAX;
micro_steps = STEPPER_DEFAULT_MICRO_STEPS;
umax = UMAX_DEFAULT;
steps_to_add_to_goal = 0;
}
void StepperController::reset()
{
// not yet implemented
}
/*
* If standard mode is activated, you need to call this function at the beginning and end of trajectory
* It will do 2 things :
* - start at current sensor position if the motor missed some steps at previous trajectory
* - start and finish with a little offset to compensate a small error between current steps and real motor steps
*/
void StepperController::synchronizePosition(bool begin_trajectory)
{
long diff = motor_position_steps - current_step_number;
if (abs(diff) < 50) {
steps_to_add_to_goal = diff;
}
else {
if (begin_trajectory) {
current_step_number = motor_position_steps;
steps_to_add_to_goal = 0;
}
}
}
void StepperController::attach()
{
steps_to_add_to_goal = 0;
fan_HIGH();
output(- 1800 * current_step_number / micro_steps, umax);
}
void StepperController::detach()
{
if (KEEP_RESISTANCE_WHEN_DETACHED) {
relaxed_mode_with_resistance();
}
else {
output(-1800 * current_step_number / micro_steps, 0);
}
fan_LOW();
}
void StepperController::start()
{
is_enabled = true;
time_last_update = micros();
time_last_step = micros();
}
void StepperController::stop()
{
is_enabled = false;
}
/*
* Allows micro steps between 1 and 32
*/
void StepperController::setMicroSteps(uint8_t new_micro_steps)
{
if (new_micro_steps < 1 || new_micro_steps > 32) {
return;
}
micro_steps = new_micro_steps;
}
/*
* Modify max effort -> amount of used current
*/
void StepperController::setMaxEffort(uint8_t effort)
{
umax = effort;
}
void StepperController::setControlMode(uint8_t control_mode)
{
this->control_mode = control_mode;
if (control_mode == STEPPER_CONTROL_MODE_RELAX) {
detach();
}
else if (control_mode == STEPPER_CONTROL_MODE_STANDARD) {
attach();
}
}
void StepperController::relativeMove(long steps, unsigned long delay)
{
current_step_number = motor_position_steps;
goal_step_number = current_step_number + steps;
delay_between_two_updates = (delay > STEPPER_DELAY_MIN) ? delay : STEPPER_DELAY_MIN;
}
/*
* This method will :
* - set new step goal
* - calculate according delay between steps
* - if standard_mode, a small amount can be added to compensate some small errors
*
*/
void StepperController::setNewGoal(long steps)
{
long step_diff = steps - current_step_number;
goal_step_number = steps - steps_to_add_to_goal;
if (step_diff != 0) {
long calc_delay = micros_to_reach_goal / abs(step_diff);
delay_between_two_updates = (calc_delay > STEPPER_DELAY_MIN) ? calc_delay : STEPPER_DELAY_MIN;
}
else {
delay_between_two_updates = 10000;
}
/*if (step_diff != 0) {
SerialUSB.print("steps received : ");
SerialUSB.print(steps);
SerialUSB.print(", current_step : ");
SerialUSB.print(current_step_number);
SerialUSB.print(", step diff : ");
SerialUSB.print(step_diff);
SerialUSB.print(", steps_to_add_to_goal : ");
SerialUSB.print(steps_to_add_to_goal);
//SerialUSB.print(", delay : ");
//SerialUSB.print(delay_between_two_updates);
SerialUSB.print(", current sensor steps : ");
SerialUSB.println(motor_position_steps);
}*/
}
/*
* Update controller depending on control mode
*
* RELAX mode :
* - disable motor, and copy sensor position to goal position
*
* STANDARD mode :
* - motor is powered with a constant current.
* - it will follow the command step by step with precision
* - no protection against missed steps (if many) during a trajectory, but
* - every trajectory will correct previous trajectory missed steps
* --> you need to call synchronizePosition(1) at the beginning of any given trajectory
* --> you need to call synchronizePosition(0) at the end of any given trajectory
*
*/
void StepperController::update()
{
if (micros() - time_last_update > delay_between_two_updates) {
time_last_update += delay_between_two_updates;
if (control_mode == STEPPER_CONTROL_MODE_RELAX) {
relaxModeUpdate();
}
else if (control_mode == STEPPER_CONTROL_MODE_STANDARD) {
standardModeUpdate();
}
else {
time_last_update = micros();
}
}
}
void StepperController::relaxModeUpdate()
{
time_last_update = micros();
current_step_number = motor_position_steps;
goal_step_number = motor_position_steps;
}
void StepperController::standardModeUpdate()
{
// minimum amount of time between 2 steps
// -> so the motor will never go too fast (and possibly miss steps due to code)
if (micros() - time_last_step < 180) {
return;
}
if (current_step_number < goal_step_number) {
++current_step_number;
output(-1800 * current_step_number / micro_steps, umax);
time_last_step = micros();
}
else if (current_step_number > goal_step_number) {
--current_step_number;
output(-1800 * current_step_number / micro_steps, umax);
time_last_step = micros();
}
else {
// nothing to do, goal has been reached
time_last_update = micros();
}
}
/*
* This method will rotate the motor until it reaches an obstacle and misses some steps
* The point where motor will miss step will define the offset
*
* --> You can use this method to calibrate the motor with a home offset and a physical obstacle
* If no step is missed (i.e. no obstacle), a timeout will be sent
* If an offset has been set, a successful answer will be sent
*
*/
uint8_t StepperController::calibrate(int direction, unsigned long delay_steps, long steps_offset, unsigned long calibration_timeout) // timeout in seconds
{
SerialUSB.println("Start calibration");
SerialUSB.print("Direction : ");
SerialUSB.println(direction);
int MISS_STEPS_TRESHOLD = 4;
long time_begin_calibration = micros();
long timeout = 1000000 * (calibration_timeout - 3); // take 3 sec off calibration timeout
if (timeout <= 0) { return STEPPER_CALIBRATION_BAD_PARAM; }
if (delay_steps < 500) { delay_steps = 500; }
if (delay_steps > 2000) { delay_steps = 2000; }
long position = motor_position_steps;
// attach motor and wait for stability
attach();
delay(500);
long last_motor_position_steps = motor_position_steps;
int miss_steps_counter = 0;
bool calibration_ok = false;
// accelerate a little bit, just for fun
for (int i = 8000; i > delay_steps; i -= 50) { // about 4 steps to accelerate
position = (direction) ? position + 1 : position - 1; // increment position
output(-1800 * position / 32, UMAX_40_PERCENT); // 32 microsteps -> more precision
delayMicroseconds(i);
}
// move at constant speed until reach an obstacle
while (micros() - time_begin_calibration < timeout) {
position = (direction) ? position + 1 : position - 1; // increment position
output(-1800 * position / 32, UMAX_50_PERCENT); // 32 microsteps -> more precision
delayMicroseconds(delay_steps);
update_current_position(micro_steps); // read pos from sensor
// Check if motor missed a step
if (direction && (motor_position_steps - last_motor_position_steps < 0)) {
miss_steps_counter++;
}
else if (!direction && (motor_position_steps - last_motor_position_steps > 0)) {
miss_steps_counter++;
}
else {
miss_steps_counter = 0;
}
if (miss_steps_counter > MISS_STEPS_TRESHOLD) {
calibration_ok = true;
break;
}
last_motor_position_steps = motor_position_steps;
}
// Now we are close
// Continue to rotate (slowly) to get home position and set offset
long home_position = motor_position_without_offset;
delay(100);
for (int i = 0; i < 32; ++i) { // move 1 step more
position = (direction) ? position + 1 : position - 1;
output(-1800 * position / 32, UMAX_40_PERCENT); // 32 microsteps -> more precision
delayMicroseconds(10000); // go slower
update_current_position(micro_steps); // read pos from sensor
}
delay(500);
// back to relax mode
detach();
if (calibration_ok) {
SerialUSB.println("---- END -->");
SerialUSB.print("Home position : ");
SerialUSB.print(home_position);
SerialUSB.print(", Motor position : ");
SerialUSB.println(motor_position_without_offset);
offset = motor_position_without_offset - steps_offset;
current_step_number = motor_position_steps;
goal_step_number = motor_position_steps;
SerialUSB.print("Calibration OK, set offset : ");
SerialUSB.println(offset);
return STEPPER_CALIBRATION_OK;
}
else {
SerialUSB.println("Calibration timeout");
return STEPPER_CALIBRATION_TIMEOUT;
}
}