/
Slave01.ino
596 lines (466 loc) · 24 KB
/
Slave01.ino
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
#include <EasyTransfer.h>
#include <SoftwareSerial.h>
#include <ODriveArduino.h>
//**************Slave Arduinos****************
EasyTransfer ET3; // slave 1 - back of robot
// Printing with stream operator
template<class T> inline Print& operator <<(Print &obj, T arg) { obj.print(arg); return obj; }
template<> inline Print& operator <<(Print &obj, float arg) { obj.print(arg, 4); return obj; }
// Serial to the ODrive
SoftwareSerial odrive_serial(10, 11); //RX (ODrive TX), TX (ODrive RX) // undercarriage
// ODrive objects
ODriveArduino odrive0(odrive_serial); // undercarriage
ODriveArduino odrive2(Serial3); // right leg
ODriveArduino odrive1(Serial2); // left leg
//**************Slave Arduinos****************
struct SLAVE1_DATA_STRUCTURE{
int16_t hipR;
int16_t hipL;
int16_t shoulderR;
int16_t shoulderL;
int16_t elbowR;
int16_t elbowL;
};
//**************Slave Arduinos****************
SLAVE1_DATA_STRUCTURE mydata_back;
// Slave 1 - Back End - bottom ctrl panel!
int but1; // left bottom
int but2; // right bottom
int but3; // left top
int but4; // right top
int home1 = 1;
int home1a = 1;
int home2 = 1;
int home2a = 1;
int home3 = 1;
int home3a = 1;
int home4 = 1;
int home4a = 1;
int home5 = 1;
int home5a = 1;
int home6 = 1;
int home6a = 1;
long home1Offset;
long home2Offset;
long home3Offset;
long home4Offset;
long home5Offset;
long home6Offset;
long home1Home;
long home2Home;
long home3Home;
long home4Home;
long home5Home;
long home6Home;
int flag = 0;
int motorSpeedFlag = 0;
int requested_state;
unsigned long previousFilterMillis; // digital pin filter
int filterTime = 200;
unsigned long previousStartupmillis;
int startupFlag = 0;
unsigned long previousMillis = 0;
const long interval = 10;
double hipRFiltered; // value in mm
double hipLFiltered;
double hipRFiltered2; // value in encoder counts
double hipLFiltered2;
double shoulderRFiltered; // value in mm
double shoulderLFiltered;
double shoulderRFiltered2; // value in encoder counts
double shoulderLFiltered2;
double elbowRFiltered; // value in mm
double elbowLFiltered;
double elbowRFiltered2; // value in encoder counts
double elbowLFiltered2;
double hipROutput; // actual output in encoder counts
double hipLOutput;
double shoulderROutput;
double shoulderLOutput;
double elbowROutput;
double elbowLOutput;
void setup() {
// LEDs
pinMode(33, OUTPUT); // white left bottom
pinMode(35, OUTPUT); // white right bottom
pinMode(37, OUTPUT); // yellow left top
pinMode(39, OUTPUT); // yellow right top
// control panel buttons
pinMode(25, INPUT_PULLUP); // left bottom
pinMode(27, INPUT_PULLUP); // right bottom
pinMode(29, INPUT_PULLUP); // left top
pinMode(31, INPUT_PULLUP); // right top
// home switches
pinMode(43, INPUT_PULLUP);
pinMode(45, INPUT_PULLUP);
pinMode(47, INPUT_PULLUP);
pinMode(49, INPUT_PULLUP);
pinMode(51, INPUT_PULLUP);
pinMode(53, INPUT_PULLUP);
Serial.begin(115200);
Serial2.begin(115200);
Serial3.begin(115200);
odrive_serial.begin(115200); // software serial
//**************Slave Arduinos****************
Serial1.begin(57600);
ET3.begin(details(mydata_back), &Serial1);
Serial.println("ODriveArduino");
Serial.println("Setting parameters...");
// ***set mtor parameters for initial setup***
// right leg
for (int axis = 0; axis < 2; ++axis) {
Serial3 << "w axis" << axis << ".controller.config.vel_limit " << 22000.0f << '\n';
Serial3 << "w axis" << axis << ".motor.config.current_lim " << 20.0f << '\n';
}
// left leg
for (int axis = 0; axis < 2; ++axis) {
Serial2 << "w axis" << axis << ".controller.config.vel_limit " << 22000.0f << '\n';
Serial2 << "w axis" << axis << ".motor.config.current_lim " << 20.0f << '\n';
}
// undercariage
for (int axis = 0; axis < 2; ++axis) {
odrive_serial << "w axis" << axis << ".controller.config.vel_limit " << 22000.0f << '\n';
odrive_serial << "w axis" << axis << ".motor.config.current_lim " << 20.0f << '\n';
}
delay (500); // wait for everything to finish
digitalWrite(39, HIGH); // set initial homing LED yellow right top
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) { //start timed event
previousMillis = currentMillis;
but1 = digitalRead(25);
but2 = digitalRead(27);
but3 = digitalRead(29);
but4 = digitalRead(31);
home1 = digitalRead(43);
home2 = digitalRead(45);
home3 = digitalRead(47);
home4 = digitalRead(49);
home5 = digitalRead(51);
home6 = digitalRead(53);
// *****************************right leg**********************************
if (but4 == 0 && flag == 0) {
digitalWrite(39, LOW);
Serial.println("Right Motor 0");
requested_state = ODriveArduino::AXIS_STATE_MOTOR_CALIBRATION;
Serial << "Axis" << 0 << ": Requesting state " << requested_state << '\n';
odrive2.run_state(0, requested_state, true);
requested_state = ODriveArduino::AXIS_STATE_ENCODER_OFFSET_CALIBRATION;
Serial << "Axis" << 0 << ": Requesting state " << requested_state << '\n';
odrive2.run_state(0, requested_state, true);
requested_state = ODriveArduino::AXIS_STATE_CLOSED_LOOP_CONTROL;
Serial << "Axis" << 0 << ": Requesting state " << requested_state << '\n';
odrive2.run_state(0, requested_state, false); // don't wait
while (home4a == 1) {
home4 = digitalRead(49);
if (home4 == 1) {
previousFilterMillis = millis();
}
else if (home4 == 0 && millis() - previousFilterMillis > filterTime) {
home4a = 0;
}
// move motor 0
odrive2.SetVelocity(0, 10000);
}
// stop motor 0
odrive2.SetVelocity(0, 0);
delay(300);
//save zero position and back off two revolutions
Serial3 << "r axis" << 0 << ".encoder.pos_estimate\n";
home4Offset = odrive2.readInt();
Serial.println(home4Offset);
odrive2.SetPosition(0, (home4Offset-(8192*2))); // back off two revolutions
delay (500); // wait for that to properly finish
Serial.println("Right Motor 1");
requested_state = ODriveArduino::AXIS_STATE_MOTOR_CALIBRATION;
Serial << "Axis" << 1 << ": Requesting state " << requested_state << '\n';
odrive2.run_state(1, requested_state, true);
requested_state = ODriveArduino::AXIS_STATE_ENCODER_OFFSET_CALIBRATION;
Serial << "Axis" << 1 << ": Requesting state " << requested_state << '\n';
odrive2.run_state(1, requested_state, true);
requested_state = ODriveArduino::AXIS_STATE_CLOSED_LOOP_CONTROL;
Serial << "Axis" << 1 << ": Requesting state " << requested_state << '\n';
odrive2.run_state(1, requested_state, false); // don't wait
while (home3a == 1) {
home3 = digitalRead(47);
if (home3 == 1) {
previousFilterMillis = millis();
}
else if (home3 == 0 && millis() - previousFilterMillis > filterTime) {
home3a = 0;
}
// move motor 1
odrive2.SetVelocity(1, 10000);
}
//stop motor 1
odrive2.SetVelocity(1, 0);
delay(300);
//save zero position and back off two revolutions
Serial3 << "r axis" << 1 << ".encoder.pos_estimate\n";
home3Offset = odrive2.readInt();
Serial.println(home3Offset);
odrive2.SetPosition(1, (home3Offset-(8192*2))); // back off one revolution
flag = 1;
digitalWrite(37, HIGH); // Yellow top left
}
// *********************************left leg************************************
else if (but3 == 0 && flag == 1) {
digitalWrite(37, LOW);
Serial.println("Left Motor 0");
requested_state = ODriveArduino::AXIS_STATE_MOTOR_CALIBRATION;
Serial << "Axis" << 0 << ": Requesting state " << requested_state << '\n';
odrive1.run_state(0, requested_state, true);
requested_state = ODriveArduino::AXIS_STATE_ENCODER_OFFSET_CALIBRATION;
Serial << "Axis" << 0 << ": Requesting state " << requested_state << '\n';
odrive1.run_state(0, requested_state, true);
requested_state = ODriveArduino::AXIS_STATE_CLOSED_LOOP_CONTROL;
Serial << "Axis" << 0 << ": Requesting state " << requested_state << '\n';
odrive1.run_state(0, requested_state, false); // don't wait
while (home2a == 1) {
home2 = digitalRead(45);
if (home2 == 1) {
previousFilterMillis = millis();
}
else if (home2 == 0 && millis() - previousFilterMillis > filterTime) {
home2a = 0;
}
// move motor 0
odrive1.SetVelocity(0, 10000);
}
// stop motor 0
odrive1.SetVelocity(0, 0);
delay(300);
//save zero position and back off two revolutions
Serial2 << "r axis" << 0 << ".encoder.pos_estimate\n";
home2Offset = odrive1.readInt();
Serial.println(home2Offset);
odrive1.SetPosition(0, (home2Offset-(8192*2))); // back off two revolutions
delay (500); // wait for that to properly finish
Serial.println("Left Motor 1");
requested_state = ODriveArduino::AXIS_STATE_MOTOR_CALIBRATION;
Serial << "Axis" << 1 << ": Requesting state " << requested_state << '\n';
odrive1.run_state(1, requested_state, true);
requested_state = ODriveArduino::AXIS_STATE_ENCODER_OFFSET_CALIBRATION;
Serial << "Axis" << 1 << ": Requesting state " << requested_state << '\n';
odrive1.run_state(1, requested_state, true);
requested_state = ODriveArduino::AXIS_STATE_CLOSED_LOOP_CONTROL;
Serial << "Axis" << 1 << ": Requesting state " << requested_state << '\n';
odrive1.run_state(1, requested_state, false); // don't wait
while (home1a == 1) {
home1 = digitalRead(43);
if (home1 == 1) {
previousFilterMillis = millis();
}
else if (home1 == 0 && millis() - previousFilterMillis > filterTime) {
home1a = 0;
}
// move motor 1
odrive1.SetVelocity(1, 10000);
}
//stop motor 1
odrive1.SetVelocity(1, 0);
delay(300);
//save zero position and back off two revolutions
Serial2 << "r axis" << 1 << ".encoder.pos_estimate\n";
home1Offset = odrive1.readInt();
Serial.println(home1Offset);
odrive1.SetPosition(1, (home1Offset-(8192*2))); // back off one revolution
/* ********** commenting out so calibration takes place on the stand with the legs bent
// time to boost the legs up
digitalWrite(37, HIGH); // yellow left
digitalWrite(39, HIGH); // yellow right
flag = 2;
}
else if (but4 == 0 || but3 == 0 && flag == 2) {
digitalWrite(37, LOW);
digitalWrite(39, LOW);
for (int axis = 0; axis < 2; ++axis) {
Serial2 << "w axis" << axis << ".controller.config.vel_limit " << 48000.0f << '\n'; // set motor speed to fast ODrive1
}
for (int axis = 0; axis < 2; ++axis) {
Serial3 << "w axis" << axis << ".controller.config.vel_limit " << 48000.0f << '\n'; // set motor speed to fast ODrive2
}
odrive2.SetPosition(0, (home4Offset - 163840)); // move legs straight right
odrive2.SetPosition(1, (home3Offset - 245760)); // move legs straight right
odrive1.SetPosition(0, (home2Offset - 163840)); // move legs straight left
odrive1.SetPosition(1, (home1Offset - 245760)); // move legs straight left
*/
digitalWrite(35, HIGH); // white LED right - ready for undercarriage calibration
flag = 3;
}
// *****************************leg motor undercarriage**************************************
else if (but2 == 0 && flag == 3) {
// bend right leg
digitalWrite(35, LOW);
/*
odrive2.SetPosition(0, (home4Offset - 16384)); // move leg bent right
odrive2.SetPosition(1, (home3Offset - 16384)); // move leg bent right
delay (2000); // wait for leg to bend
*/
// calibrate right leg undercarriage, ODrive axis 1
Serial.println("Right Leg undercarriage");
requested_state = ODriveArduino::AXIS_STATE_MOTOR_CALIBRATION;
Serial << "Axis" << 1 << ": Requesting state " << requested_state << '\n';
odrive0.run_state(1, requested_state, true);
requested_state = ODriveArduino::AXIS_STATE_ENCODER_OFFSET_CALIBRATION;
Serial << "Axis" << 1 << ": Requesting state " << requested_state << '\n';
odrive0.run_state(1, requested_state, true);
requested_state = ODriveArduino::AXIS_STATE_CLOSED_LOOP_CONTROL;
Serial << "Axis" << 1 << ": Requesting state " << requested_state << '\n';
odrive0.run_state(1, requested_state, false); // don't wait
delay (500); // wait for that to properly finish
/* COMMENTING OUT - CALIBRATION IS DONE WITH MANUAL ALIGNMENT
// move undercarriage ODrive axis 1 under it hits the home switch 6
while (home6a == 1) {
home6 = digitalRead(53);
if (home6 == 1) {
previousFilterMillis = millis();
}
else if (home6 == 0 && millis() - previousFilterMillis > filterTime) {
home6a = 0;
}
// move motor 1
odrive0.SetVelocity(1, -10000);
}
//stop motor 1
odrive0.SetVelocity(1, 0);
//save zero position
odrive_serial << "r axis" << 1 << ".encoder.pos_estimate\n";
home6Offset = odrive0.readInt();
Serial.print(home6Offset);
Serial.print(" , ");
while ((home6Offset < -122000) || (home6Offset > -52000)) { // filter because software serial is a bit rubbish
odrive_serial << "r axis" << 1 << ".encoder.pos_estimate\n";
home6Offset = odrive0.readInt();
}
Serial.println(home6Offset); // print filtered value
odrive_serial << "w axis" << 1 << ".controller.config.vel_limit " << 48000.0f << '\n'; // set motor speed to fast for Odrive 0 / axis 1
odrive0.SetPosition(1, (home6Offset+87654)); // back off 25mm
delay(3000); // wait for leg to move out again
// put leg straight again
odrive2.SetPosition(0, (home4Offset - 163840)); // move legs straight right
odrive2.SetPosition(1, (home3Offset - 245760)); // move legs straight right
*/
digitalWrite(33, HIGH); // white LED left bottom - ready for the other side
flag = 4;
}
//**************** do the other leg *****************
else if (but1 == 0 && flag == 4) {
digitalWrite(33, LOW);
/*
odrive1.SetPosition(0, (home2Offset - 16384)); // move leg bent left
odrive1.SetPosition(1, (home1Offset - 16384)); // move leg bent left
delay (2000); // wait for leg to bend
*/
// calibrate right leg undercarriage, ODrive axis 0
Serial.println("Right Leg undercarriage");
requested_state = ODriveArduino::AXIS_STATE_MOTOR_CALIBRATION;
Serial << "Axis" << 0 << ": Requesting state " << requested_state << '\n';
odrive0.run_state(0, requested_state, true);
requested_state = ODriveArduino::AXIS_STATE_ENCODER_OFFSET_CALIBRATION;
Serial << "Axis" << 0 << ": Requesting state " << requested_state << '\n';
odrive0.run_state(0, requested_state, true);
requested_state = ODriveArduino::AXIS_STATE_CLOSED_LOOP_CONTROL;
Serial << "Axis" << 0 << ": Requesting state " << requested_state << '\n';
odrive0.run_state(0, requested_state, false); // don't wait
delay (500); // wait for that to properly finish
/* COMMENTING OUT - CALIBRATION IS DONE WITH MANUAL ALIGNMENT
// move undercarriage ODrive axis 0 under it hits the home switch 5
while (home5a == 1) {
home5 = digitalRead(51);
if (home5 == 1) {
previousFilterMillis = millis();
}
else if (home5 == 0 && millis() - previousFilterMillis > filterTime) {
home5a = 0;
}
// move motor 0
odrive0.SetVelocity(0, -10000);
}
//stop motor 0
odrive0.SetVelocity(0, 0);
//save zero position
odrive_serial << "r axis" << 0 << ".encoder.pos_estimate\n";
home5Offset = odrive0.readInt();
Serial.print(home5Offset);
Serial.print(" , ");
while ((home5Offset < -122000) || (home5Offset > -52000)) { // filter because software serial is a bit rubbish
odrive_serial << "r axis" << 0 << ".encoder.pos_estimate\n";
home5Offset = odrive0.readInt();
}
Serial.println(home5Offset); // print filtered value
odrive_serial << "w axis" << 0 << ".controller.config.vel_limit " << 48000.0f << '\n'; // set motor speed to fast for Odrive 0 / axis 0
odrive0.SetPosition(0, (home5Offset+87654)); // back off 25mm
delay(3000); // wait for leg to move out again
// put leg straight again
odrive1.SetPosition(0, (home2Offset - 163840)); // move legs straight left
odrive1.SetPosition(1, (home1Offset - 245760)); // move legs straight left
delay(3000); // wait for the leg to be straight
*/
flag = 5; // proceed to next stage
}
if (flag == 5) { // main output to leg starts here
if (motorSpeedFlag == 0) { // only send the data once ever
// turn up motor speeds a bit
odrive_serial << "w axis" << 0 << ".controller.config.vel_limit " << 96000.0f << '\n';
odrive_serial << "w axis" << 1 << ".controller.config.vel_limit " << 96000.0f << '\n';
Serial2 << "w axis" << 0 << ".controller.config.vel_limit " << 220000.0f << '\n';
Serial2 << "w axis" << 1 << ".controller.config.vel_limit " << 200000.0f << '\n';
Serial3 << "w axis" << 0 << ".controller.config.vel_limit " << 220000.0f << '\n';
Serial3 << "w axis" << 1 << ".controller.config.vel_limit " << 200000.0f << '\n';
motorSpeedFlag = 1; // set the flag after this has run once so it never runs again
}
// ***********serial receive from master***************
if (ET3.receiveData()) {
if(startupFlag == 0) {
previousStartupmillis = currentMillis; // start filter settling timer
startupFlag = 1; // only do this once ever the first time you get data
}
mydata_back.shoulderR = constrain(mydata_back.shoulderR, 80,200);
mydata_back.shoulderL = constrain(mydata_back.shoulderL, 80,200);
mydata_back.elbowR = constrain(mydata_back.elbowR, 80,200);
mydata_back.elbowL = constrain(mydata_back.elbowL, 80,200);
mydata_back.hipR = constrain(mydata_back.hipR, -35,35);
mydata_back.hipL = constrain(mydata_back.hipL, -35,35);
shoulderRFiltered = filter(mydata_back.shoulderR, shoulderRFiltered);
shoulderLFiltered = filter(mydata_back.shoulderL, shoulderLFiltered);
elbowRFiltered = filter(mydata_back.elbowR, elbowRFiltered);
elbowLFiltered = filter(mydata_back.elbowL, elbowLFiltered);
// ********* LEGS OUTPUT *********
shoulderRFiltered2 = shoulderRFiltered*3490; // work out encoder counts per milimeter
shoulderLFiltered2 = shoulderLFiltered*3490; // work out encoder counts per milimeter
elbowRFiltered2 = elbowRFiltered*3490; // work out encoder counts per milimeter
elbowLFiltered2 = elbowLFiltered*3490; // work out encoder counts per milimeter
shoulderROutput = home4Offset - (715450 - shoulderRFiltered2); // use offset from homing minus the actual postion of the leg from the joint
elbowROutput = home3Offset - (715450 - elbowRFiltered2);
shoulderLOutput = home2Offset - (715450 - shoulderLFiltered2); // use offset from homing minus the actual postion of the leg from the joint
elbowLOutput = home1Offset - (715450 - elbowLFiltered2);
// ******** HIP - UNDERCARRIAGE OUTPUT *********
hipRFiltered = filter(mydata_back.hipR, hipRFiltered); // filtered value in mm
hipLFiltered = filter(mydata_back.hipL, hipLFiltered); // filtered value in mm
hipRFiltered2 = hipRFiltered * 3490; // work out encoder counts per mm
hipLFiltered2 = hipLFiltered * 3490; // work out encoder counts per mm
// calibration is done by manually aligning
//hipROutput = (home6Offset+87654)+hipRFiltered; // +/- mid actuator postion with new data
//hipLOutput = (home5Offset+87654)+hipLFiltered; // +/- mid actuator postion with new data
hipROutput = hipRFiltered2; // +/- mid actuator postion with new data
hipLOutput = hipLFiltered2; // +/- mid actuator postion with new data
if (currentMillis - previousStartupmillis > 3000) { // makes sure filter has settled the first time
odrive2.SetPosition(0, shoulderROutput); // output position to actuator RIGHT LEG
odrive2.SetPosition(1, elbowROutput); // output position to actuator RIGHT LEG
odrive1.SetPosition(0, shoulderLOutput); // output position to actuator LEFT LEG
odrive1.SetPosition(1, elbowLOutput); // output position to actuator LEFT LEG
odrive0.SetPosition(0, hipLOutput); // output data to ODrive
odrive0.SetPosition(1, hipROutput); // output data to ODrive
}
} // end of receive data
} // end of main output to leg
} // end of timed event
}
//***************filter joint motions*****************
double filter(double lengthOrig, double currentValue) {
double filter = 40;
double lengthFiltered = (lengthOrig + (currentValue * filter)) / (filter + 1);
return lengthFiltered;
}