-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMotor.cpp
565 lines (457 loc) · 17.2 KB
/
Motor.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
// encapsulates a fischertechnik encoder-motor with gearbox
#include "Motor.h"
Motor::Motor() { }
void Motor::init(int motorId, int counterId, int switchId, float pulsesPerDegreeRotation, bool reversed) {
_motorId = motorId;
_counterId = counterId;
_switchId = switchId;
_running = 0;
_calibrated = false;
_pulsesPerDegreeRotation = pulsesPerDegreeRotation;
_reversed = reversed;
// set input to type "switch": either 1 or 0
ftduino.input_set_mode(_switchId, Ftduino::SWITCH);
// set counter input to react on rising edge
ftduino.counter_set_mode(_counterId, Ftduino::C_EDGE_RISING);
// in case limit switch is already engaged, move a little away
if(ftduino.input_get(_switchId)) {
Serial.println("init: Limit switch engaged: moving away...");
// drive away from switch and stop as soon as switch opens
ftduino.motor_set(_motorId, rev(Ftduino::LEFT), Ftduino::MAX/4);
unsigned long timeBudget = millis() + 5000;
while(ftduino.input_get(_switchId) && millis() < timeBudget);
// stop, switch opened or time up
ftduino.motor_set(_motorId, Ftduino::BRAKE, Ftduino::MAX);
if(!ftduino.input_get(_switchId)){
Serial.println("init: limit switch opened. Okay");
}else{
Serial.println("init: Error: disengaging limit switch failed!");
return false;
}
}
for (int i = 0; i < bufferSize; i++) {
positionBuffer[i] = 0;
timeBuffer[i] = 0;
}
}
int Motor::getId() {
return _motorId;
}
void Motor::setMinSpeed(int minSpeed) {
_minSpeed = minSpeed;
}
int Motor::getMinSpeed() {
return _minSpeed;
}
void Motor::setLimit(int maxAbsPulses) {
_maxAbsPulses = maxAbsPulses;
}
void Motor::setMotionProfile(int motionProfile) {
_motionProfile = motionProfile;
}
int Motor::getMotionProfile() {
return _motionProfile;
}
bool Motor::isCalibrated() {
return _calibrated;
}
bool Motor::isRunning() {
return _running;
}
// Turn motor into direction, at given speed, for specified number of pulses
void Motor::set(int direction, int speed, int pulsesToGo, int velocity) {
direction = rev(direction);
ftduino.counter_clear(_counterId);
ftduino.motor_set(_motorId, direction, speed);
_setDirection = direction;
_setSpeed = speed;
_actualSpeed = _setSpeed;
_toGo = pulsesToGo;
_setVelocity = velocity;
_running = millis();
_timeBudget = _setSpeed * _toGo * 1.2;
_cruising = false;
Serial.print("M");
Serial.print(_motorId+1);
Serial.print(" ");
Serial.print(_setDirection == 1 ? "LEFT" : "RIGHT");
Serial.print(" speed:");
Serial.print(_setSpeed);
Serial.print(" pulses to go:");
Serial.print(_toGo);
Serial.print(" timeBudget:");
Serial.print(_timeBudget);
Serial.print(" mProfile:");
Serial.println(_motionProfile);
}
void Motor::setDeg(int absDegrees, int speed, int velocity) {
if(!_calibrated){
Serial.println("Error: Motor has not been calibrated!");
return false;
}
float pulsesToGo = (absDegrees * _pulsesPerDegreeRotation) - _calibrationPulsesLost;
if(pulsesToGo < 0){
Serial.print("M"); Serial.print(_motorId + 1);
Serial.print(" Warning: ");
Serial.print(absDegrees);
Serial.print(" outside of reach due to _calibrationPulsesLost. Veto.");
return false;
}
int roundedToGo = (int)(pulsesToGo + 0.5);
if(roundedToGo + _absPulses > _maxAbsPulses){
Serial.print("M"); Serial.print(_motorId + 1);
Serial.print(" Warning: ");
Serial.print(absDegrees);
Serial.print(" outside of reach due to _maxAbsPulses. Veto.");
return false;
}
// Start motor into the right direction at min speed and hand motor control over to update()
int deltaPulses = 0;
if(roundedToGo > _absPulses){
deltaPulses = roundedToGo - _absPulses;
set(Ftduino::LEFT, _minSpeed, deltaPulses, velocity);
}else if(roundedToGo < _absPulses){
deltaPulses = _absPulses - roundedToGo;
set(Ftduino::RIGHT, _minSpeed, deltaPulses, velocity);
}
_setSpeed = speed;
_actualSpeed = _minSpeed;
if(deltaPulses){
Serial.print("M"); Serial.print(_motorId + 1);
Serial.print(" setDeg:");
Serial.print(absDegrees);
Serial.print(" go ");
Serial.print(deltaPulses);
Serial.print(" (rounded) pulses ");
Serial.println(rev(Ftduino::LEFT) == Ftduino::LEFT ? "left": "right");
}else{
Serial.print("M"); Serial.print(_motorId + 1);
Serial.println(" setDeg: 0 to go.");
}
}
// Returns a speed value based on the delta between how many pulses we need to go and how many we have already travelled (pulses counted)
// This provides a basic "trapezoidal motion profile, divided into three phases: acceleration, cruise and deceleration phase.
int Motor::speedFromDeltaRamped(int pulsesToGo, int pulsesCounted) {
int pulsesRemaining = pulsesToGo - pulsesCounted;
int rampedSpeed;
int minBandingArea = 5;
int maxBandingArea = 25;
int bandingArea = constrain((pulsesToGo * maxBandingArea) / 100, minBandingArea, maxBandingArea);
_cruising = false;
if(pulsesCounted < bandingArea){ // ramp up, acceleration phase
rampedSpeed = map(pulsesCounted, 0, bandingArea, _minSpeed, _setSpeed);
}else if (pulsesRemaining < bandingArea) { // ramp down, deceleration phase
rampedSpeed = map(pulsesRemaining, 0, bandingArea, _minSpeed, _actualSpeed); // note _actualSpeed here, as velocity adjustment during cruise may have lowered it and we don't want speed to shoot up only to ramp down
}else{
// cruise phase
rampedSpeed = _setSpeed;
_cruising = true;
}
if(rampedSpeed < _minSpeed){
return _minSpeed;
}
return rampedSpeed;
}
// Unused:
// Returns a velocity value based on the delta between how many pulses we need to go and how many we have already travelled (pulses counted)
// This provides the basis for a basic "trapezoidal motion profile, divided into three phases: acceleration, cruise and deceleration phase.
int Motor::velocityFromDeltaRamped(int pulsesToGo, int pulsesCounted) {
int pulsesRemaining = pulsesToGo - pulsesCounted;
int rampedVelocity;
int minBandingArea = 5;
int maxBandingArea = 25;
int bandingArea = constrain((pulsesToGo * maxBandingArea) / 100, minBandingArea, maxBandingArea);
_cruising = false;
if(pulsesCounted < bandingArea){ // ramp up, acceleration phase
rampedVelocity = map(pulsesCounted, 0, bandingArea, _minVelocity, _setVelocity);
}else if (pulsesRemaining < bandingArea) { // ramp down, deceleration phase
rampedVelocity = map(pulsesRemaining, 0, bandingArea, _minVelocity, _setVelocity);
}else{
// cruise phase
rampedVelocity = _setVelocity;
_cruising = true;
}
if(rampedVelocity < _minVelocity){
return _minVelocity;
}
return rampedVelocity;
}
// a PID controller provides a continuous adjustment of motor speed based on the error between the desired and current positions, which, when tuned well,
// provides more accurate positioning and smoother motion compared to a ramping approach as it is more aware of the distance that needs to be travelled
int Motor::speedFromPosPID(int targetPosition, int currentPosition) {
unsigned long currentTime = millis();
float deltaTime = currentTime - lastTime; // casting from two unsigned long to one float
lastTime = currentTime;
// Calculate error
int error = targetPosition - currentPosition;
// Integral term with anti-windup
integral += Ki * error * deltaTime;
float minIntegral = 0;
integral = constrain(integral, minIntegral, maxIntegral);
// Derivative term
float derivative = 0;
if (deltaTime > 0) { // Avoid division by zero
derivative = Kd * (error - lastError) / deltaTime;
}
lastError = error;
// Calculate PID output: the Proportional, Integral, and Derivative terms:
// P: proportional to the current error (P = Kp * error; proportional term)
// I (integral): sum of all past errors
// D (derivative): rate of change of error
float output = Kp * error + integral + derivative;
// Map PID output to motor speed range
int speed = map(abs(output), 0, 1000, _minSpeed, _maxSpeed);
// Ensure speed is within bounds
speed = constrain(speed, _minSpeed, _maxSpeed);
// Determine direction
if (output < 0) {
speed = -speed;
}
return speed;
}
// Unused:
// Velocity PID controller (outer loop)
float Motor::velocityFromPosPID(int targetPosition, int currentPosition) {
int error = targetPosition - currentPosition;
// limit integral (anti-windup)
float maxIntegral = 100.0;
float minIntegral = 0.0;
integral_pos += error;
if (integral_pos > maxIntegral){
integral_pos = maxIntegral;
} else if (integral_pos < minIntegral){
integral_pos = minIntegral;
}
int derivative = (error - previousError_pos) / deltaTime;
previousError_pos = error;
float output = (Kp_pos * error) + (Ki_pos * integral_pos) + (Kd_pos * derivative);
// Ensure velocity is within bounds
output = constrain(output, _minVelocity, _maxVelocity);
return output; // This is the desired velocity
}
// Unused:
// Motor Speed PID controller (inner loop)
int Motor::speedFromVelocityPID(float desiredVelocity, float currentVelocity) {
float error = desiredVelocity - currentVelocity; // P
integral_vel += error; // I
// D
float derivative = 0;
if (deltaTime > 0) { // Avoid division by zero
float derivative = Kd * (error - lastError) / deltaTime;
}
float output = Kp_vel * error + Ki_vel * integral_vel + Kd_vel * derivative;
previousError_vel = error;
// Clamp the motor speed to within maxMotorSpeed
output = constrain(output, _minSpeed, _maxSpeed);
return output; // This is the motor speed
}
// get a proportionally adjusted speed based on velocity error
int Motor::speedFromVelocityProp(int speed, float currentVelocity, float desiredVelocity) {
float error = desiredVelocity - currentVelocity;
float proportional = Kp_prop * error; // calc proportional adjustment value
// round and cast to int
if (proportional >= 0) {
speed += static_cast<int>(proportional + 0.5);
} else {
speed += static_cast<int>(proportional - 0.5);
}
// Ensure the new speed is within the allowed range
if (speed < 0) speed = _minSpeed;
if (speed > 64) speed = _maxSpeed;
return speed;
}
// Velocity
float Motor::getVelocity(unsigned long currentTime, int currentPosition) {
float currentVelocity = 0;
static int numReadings = 0; // Number of valid readings in the buffer
if (currentPosition != lastPosition) {
// New reading detected, update buffer
positionBuffer[bufferIndex] = currentPosition;
timeBuffer[bufferIndex] = currentTime;
numReadings = min(numReadings + 1, bufferSize); // Increase readings count until it reaches buffer size
// Calculate velocity
int oldestIndex = (bufferIndex + 1) % numReadings; // Ensure we use only valid readings initially
float positionDifference = positionBuffer[bufferIndex] - positionBuffer[oldestIndex];
unsigned long timeDiff = timeBuffer[bufferIndex] - timeBuffer[oldestIndex];
float timeDifference = (float)timeDiff / 1000.0f; // ms to s
if (numReadings > 1 && timeDifference > 0 && timeDifference < _velocityTimeout / 1000.0f) {
currentVelocity = positionDifference / timeDifference;
} else {
// Optionally handle the error here (e.g., logging)
Serial.println("Velocity calculation error: Invalid time difference or insufficient readings");
currentVelocity = 0.00;
}
bufferIndex = (bufferIndex + 1) % bufferSize; // Wrap around
lastVelocity = currentVelocity;
} else if (currentTime - lastTime > _velocityTimeout) {
Serial.println("No new readings detected for an extended period");
lastVelocity = 0;
currentVelocity = 0;
} else {
currentVelocity = lastVelocity;
}
lastPosition = currentPosition;
lastTime = currentTime; // Ensure this is updated with every new reading
return currentVelocity;
}
int Motor::rev(int direction) {
if(_reversed){
return direction == Ftduino::LEFT ? Ftduino::RIGHT : Ftduino::LEFT;
}
return direction;
}
void Motor::update() {
if(_running){
// Failsafe: Check if axis is triggering its limit switch
if(ftduino.input_get(_switchId)) {
// Serial.println("Input 1");
digitalWrite(LED_BUILTIN, HIGH);
ftduino.motor_set(_motorId, Ftduino::BRAKE, Ftduino::MAX);
_running = 0;
Serial.print("M"); Serial.print(_motorId + 1);
Serial.print(" limit switch! at steps: ");
Serial.println(ftduino.counter_get(_counterId));
// drive away from switch and stop as soon as switch opens
ftduino.motor_set(_motorId, rev(Ftduino::LEFT), Ftduino::MAX/4);
unsigned long timeBudget = millis() + 5000;
while(ftduino.input_get(_switchId) && millis() < timeBudget);
// stop, switch opened or time up
ftduino.motor_set(_motorId, Ftduino::BRAKE, Ftduino::MAX);
}
unsigned long currentMillis = millis();
deltaTime = currentMillis - lastTime; // casting from two unsigned long to one float
lastTime = currentMillis;
if(_toGo){
int remaining_pulses = _toGo - ftduino.counter_get(_counterId);
if (remaining_pulses <= 0) {
ftduino.motor_set(_motorId, Ftduino::BRAKE, Ftduino::MAX);
if(_calibrated){
if(_setDirection == rev(Ftduino::LEFT) ){
_absPulses += ftduino.counter_get(_counterId);
}else{
_absPulses -= ftduino.counter_get(_counterId);
}
}
unsigned long took = currentMillis - _running;
_running = 0;
_toGo = 0;
}else{
if(_motionProfile == 3){
float currentVelocity = getVelocity(currentMillis, ftduino.counter_get(_counterId) );
int rampedSpeed = speedFromDeltaRamped(_toGo, ftduino.counter_get(_counterId));
if(_cruising){
int adjustedSpeed = speedFromVelocityProp(rampedSpeed, currentVelocity, _setVelocity);
if(adjustedSpeed != _actualSpeed){
ftduino.motor_set(_motorId, _setDirection, adjustedSpeed);
_actualSpeed = adjustedSpeed;
}
}else{
if(rampedSpeed != _actualSpeed){
ftduino.motor_set(_motorId, _setDirection, rampedSpeed);
_actualSpeed = rampedSpeed;
}
}
}else if(_motionProfile == 2){
int pidSpeed = speedFromPosPID(_toGo, ftduino.counter_get(_counterId) );
if(pidSpeed != _actualSpeed){
ftduino.motor_set(_motorId, _setDirection, pidSpeed);
_actualSpeed = pidSpeed;
}
}else if(_motionProfile == 1){
// Ramped motion profile
int rampedSpeed = speedFromDeltaRamped(_toGo, ftduino.counter_get(_counterId));
if(rampedSpeed != _actualSpeed){
ftduino.motor_set(_motorId, _setDirection, rampedSpeed);
_actualSpeed = rampedSpeed;
}else{
// Raw operation: motor is left running as it was started
}
}
}
// Failsafe: Check if motor is running suspiciously long
if (currentMillis - _running >= _timeBudget) {
ftduino.motor_set(_motorId, Ftduino::BRAKE, Ftduino::MAX);
if(_running){
Serial.print("M"); Serial.print(_motorId + 1);
Serial.print(" failsave timeBudget "); Serial.print(_timeBudget);
Serial.print(" over, STOPPING at steps: ");
Serial.println( ftduino.counter_get(_counterId) );
}else{
}
_running = 0;
_timeBudget = 0;
}
}
}
void Motor::report() {
float absDegrees = (_absPulses + _calibrationPulsesLost) / _pulsesPerDegreeRotation;
Serial.print("M"); Serial.print(_motorId + 1);
Serial.print(" cal:");
Serial.print(_calibrated ? "yes" : "NO");
Serial.print(" absPu:");
Serial.print(_absPulses);
Serial.print(" absDeg:");
Serial.print(absDegrees, 2); // two decimal places
Serial.print(" run:");
Serial.print(_running);
Serial.print(" toGo:");
Serial.print(_toGo);
Serial.print(" timeBud:");
Serial.print(_timeBudget);
Serial.println(" ---");
}
bool Motor::moveHome() {
int calibrationSpeed = Ftduino::MAX/4;
if(calibrationSpeed < _minSpeed){ calibrationSpeed = _minSpeed; }
// in case limit switch is already engaged, move a little away
if(ftduino.input_get(_switchId)) {
Serial.print("M"); Serial.print(_motorId + 1);
Serial.println(" Limit switch engaged: moving away...");
ftduino.motor_set(_motorId, rev(Ftduino::LEFT), calibrationSpeed);
delay(1000);
ftduino.motor_set(_motorId, Ftduino::BRAKE, 0);
}
// check if limit switch open again
if(ftduino.input_get(_switchId)) {
Serial.print("M"); Serial.print(_motorId + 1);
Serial.println(" Error: end stop still engaged!");
return false;
}
// drive motor into end stop
Serial.print("M"); Serial.print(_motorId + 1);
Serial.println(" moving towards end stop...");
ftduino.motor_set(_motorId, rev(Ftduino::RIGHT), calibrationSpeed);
// warte auf Endschalter
unsigned long timeBudget = millis() + 12000;
while(!ftduino.input_get(_switchId) && millis() < timeBudget);
// stop, switch reached or time up
ftduino.motor_set(_motorId, Ftduino::BRAKE, Ftduino::MAX);
if(!ftduino.input_get(_switchId)){
Serial.print("M"); Serial.print(_motorId + 1);
Serial.println(" Error: end stop not reached after 12 secs");
return false;
}
delay(300);
// set this position as our reference zero
_absPulses = 0;
ftduino.counter_clear(_counterId);
// drive away from switch and stop as soon as switch opens
ftduino.motor_set(_motorId, rev(Ftduino::LEFT), calibrationSpeed);
timeBudget = millis() + 5000;
while(ftduino.input_get(_switchId) && millis() < timeBudget);
// stop, switch opened or time up
ftduino.motor_set(_motorId, Ftduino::BRAKE, Ftduino::MAX);
if(ftduino.input_get(_switchId)){
Serial.print("M"); Serial.print(_motorId + 1);
Serial.println(" Error: homing failed. Switch still engaged!");
return false;
}
_absPulses = ftduino.counter_get(_counterId);
_calibrationPulsesLost = _absPulses;
ftduino.counter_clear(_counterId);
_calibrated = true;
Serial.print("M"); Serial.print(_motorId + 1);
Serial.print(" homing routine successful. ");
Serial.print("Motor at "); Serial.print(_absPulses); Serial.println(" pulses.");
return true;
}