-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathHexapod_Demo.cpp
427 lines (378 loc) · 12.4 KB
/
Hexapod_Demo.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
/**
* S T E W A R T P L A T F O R M O N E S P 3 2
*
* Copyright (C) 2019 Nicolas Jeanmonod, ouilogique.com
*
* 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 <https://www.gnu.org/licenses/>.
*
*/
#include <Hexapod_Demo.h>
#include <Hexapod_Servo.h>
#include <Hexapod_Kinematics.h>
#define COUNT_OF(x) ((sizeof(x) / sizeof(0 [x])) / ((size_t)(!(sizeof(x) % sizeof(0 [x])))))
extern angle_t servo_angles[NB_SERVOS];
extern Hexapod_Servo hx_servo;
/**
*
*/
Hexapod_Demo::Hexapod_Demo()
{
}
/**
*
*/
void Hexapod_Demo::demoMov_MinMaxAllAxis()
{
Serial.println("\n########## demoMov_MinMaxAllAxis START ##########");
const platform_t coords[] = {
// X
{HX_X_MAX, 0, 0, 0, 0, 0},
{HX_X_MIN, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
// Y
{0, HX_Y_MAX, 0, 0, 0, 0},
{0, HX_Y_MIN, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
// Z
{0, 0, HX_Z_MAX, 0, 0, 0},
{0, 0, HX_Z_MIN, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
// A
{0, 0, 0, HX_A_MAX, 0, 0},
{0, 0, 0, HX_A_MIN, 0, 0},
{0, 0, 0, 0, 0, 0},
// B
{0, 0, 0, 0, HX_B_MAX, 0},
{0, 0, 0, 0, HX_B_MIN, 0},
{0, 0, 0, 0, 0, 0},
// C
{0, 0, 0, 0, 0, HX_C_MAX},
{0, 0, 0, 0, 0, HX_C_MIN},
{0, 0, 0, 0, 0, 0}};
int8_t movOK = -1;
for (uint8_t cnt = 0; cnt < COUNT_OF(coords); cnt++)
{
Serial.print("cnt = ");
Serial.println(cnt);
movOK = hx_servo.calcServoAngles(coords[cnt], servo_angles);
hx_servo.updateServos(movOK);
delay(1000);
}
Serial.println("demoMov_MinMaxAllAxis DONE");
}
/**
*
*/
void Hexapod_Demo::demoMov_circles(uint8_t nb_turn = 1)
{
if (nb_turn == 0)
return;
// Move in circles in the horizontal plane.
Serial.println("\n########## demoMov_circles START ##########");
const uint8_t nb_points = 90;
const double radius = HX_X_MAX;
const double angleInc = TWO_PI / nb_points;
double angle = 0;
platform_t coords[nb_points];
for (uint8_t angleID = 0; angleID < nb_points; angleID++)
{
coords[angleID] = {(radius * sin(angle)),
(radius * cos(angle)),
0, 0, 0, 0};
angle += angleInc;
}
int8_t movOK = -1;
for (uint8_t turn = 0; turn < nb_turn; turn++)
{
for (uint8_t cnt = 0; cnt < nb_points; cnt++)
{
static unsigned long T1;
while ((millis() - T1) < 2UL)
{
}
T1 = millis();
movOK = hx_servo.calcServoAngles(coords[cnt], servo_angles);
hx_servo.updateServos(movOK, 0UL);
// hx_servo.updateServosIncremental(movOK, 0UL);
}
}
Serial.println("demoMov_circles DONE");
}
/**
*
*/
void Hexapod_Demo::demoMov_shakeHeave()
{
Serial.println("\n########## demoMov_shakeHeave START ##########");
double shakeVal = HX_Z_MIN;
int8_t movOK = -1;
const uint32_t wait = 200;
delay(wait);
movOK = hx_servo.calcServoAngles({0, 0, HX_Z_MAX, 0, 0, 0}, servo_angles);
hx_servo.updateServos(movOK);
delay(wait);
movOK = hx_servo.calcServoAngles({0, 0, HX_Z_MIN, 0, 0, 0}, servo_angles);
hx_servo.updateServos(movOK);
delay(wait);
for (uint8_t demoMov_shakeHeave = 0; demoMov_shakeHeave < 10; demoMov_shakeHeave++)
{
static unsigned long T1;
while ((millis() - T1) < 1500UL) // 60UL for fast servos.
{
}
T1 = millis();
movOK = hx_servo.calcServoAngles({0, 0, shakeVal, 0, 0, 0}, servo_angles);
hx_servo.updateServos(movOK);
shakeVal = -shakeVal;
}
delay(wait);
movOK = hx_servo.calcServoAngles({0, 0, HX_Z_MAX, 0, 0, 0}, servo_angles);
hx_servo.updateServos(movOK);
delay(wait);
movOK = hx_servo.calcServoAngles({0, 0, HX_Z_MIN, 0, 0, 0}, servo_angles);
hx_servo.updateServos(movOK);
delay(wait);
movOK = hx_servo.home(servo_angles);
hx_servo.updateServos(movOK);
Serial.println("demoMov_shakeHeave DONE");
}
/**
*
*/
void Hexapod_Demo::testNaN()
{
Serial.println("\n########## TEST NaN ##########");
int8_t movOK;
platform_t coords[] = {
{-33.0, 11.0, -15.0, 17.0, -5.7, 5.7},
{-33.0, 11.0, -15.0, 5.7, 17.0, 17.0}};
for (uint8_t coord_id = 0; coord_id < COUNT_OF(coords); coord_id++)
{
movOK = hx_servo.calcServoAngles(coords[coord_id], servo_angles);
hx_servo.printServoAngles();
Serial.print("movOK = ");
Serial.println(movOK);
}
}
/**
*
*/
void Hexapod_Demo::testCalculations()
{
hx_servo.calcServoAngles({0, 0, 0, 0, 0, 0}, servo_angles);
Serial.print("\n0, 0, 0, 0, 0, 0 ");
hx_servo.printServoAngles();
hx_servo.calcServoAngles({0, 0, HX_Z_MAX, 0, 0, 0}, servo_angles);
Serial.print("\n0, 0, HX_Z_MAX, 0, 0, 0");
hx_servo.printServoAngles();
hx_servo.calcServoAngles({0, 0, HX_Z_MIN, 0, 0, 0}, servo_angles);
Serial.print("\n0, 0, HX_Z_MIN, 0, 0, 0");
hx_servo.printServoAngles();
}
/**
*
*/
void Hexapod_Demo::testCalcSpeed()
{
Serial.println("\n########## TEST CALCULATION SPEED ##########");
unsigned long T1 = 0, T2 = 0, TTot = 0, dT = 0, Tmin = 10000, Tmax = 0;
const int nb_intervals = 2;
const double divide = 3;
int count = 0;
for (double hx_x = HX_X_MIN / divide; hx_x <= HX_X_MAX / divide; hx_x += (HX_X_MAX - HX_X_MIN) / nb_intervals / divide)
{
for (double hx_y = HX_Y_MIN / divide; hx_y <= HX_Y_MAX / divide; hx_y += (HX_Y_MAX - HX_Y_MIN) / nb_intervals / divide)
{
for (double hx_z = HX_Z_MIN / divide; hx_z <= HX_Z_MAX / divide; hx_z += (HX_Z_MAX - HX_Z_MIN) / nb_intervals / divide)
{
for (double hx_a = HX_A_MIN / divide; hx_a <= HX_A_MAX / divide; hx_a += (HX_A_MAX - HX_A_MIN) / nb_intervals / divide)
{
for (double hx_b = HX_B_MIN / divide; hx_b <= HX_B_MAX / divide; hx_b += (HX_B_MAX - HX_B_MIN) / nb_intervals / divide)
{
for (double hx_c = HX_C_MIN / divide; hx_c <= HX_C_MAX / divide; hx_c += (HX_C_MAX - HX_C_MIN) / nb_intervals / divide)
{
T1 = micros();
hx_servo.calcServoAngles({hx_x, hx_y, hx_z, hx_a, hx_b, hx_c}, servo_angles);
// hx_servo.calcServoAnglesAlgo1({hx_x, hx_y, hx_z, hx_a, hx_b, hx_c}, servo_angles);
// hx_servo.calcServoAnglesAlgo2({hx_x, hx_y, hx_z, hx_a, hx_b, hx_c}, servo_angles);
// hx_servo.calcServoAnglesAlgo3({hx_x, hx_y, hx_z, hx_a, hx_b, hx_c}, servo_angles);
T2 = micros();
dT = T2 - T1;
// Serial.println(dT);
if (dT > Tmax)
Tmax = dT;
else if (dT < Tmin)
Tmin = dT;
TTot += (dT);
count++;
}
}
}
}
}
}
Serial.print("Algorithm = ");
Serial.println(ALGO);
Serial.print("nb iterations = ");
Serial.println(count);
Serial.print("total time elapsed (us) = ");
Serial.println(TTot);
Serial.print("time per calculation (us) = ");
Serial.println(TTot / count);
Serial.print("Tmin (us) = ");
Serial.println(Tmin);
Serial.print("Tmax (us) = ");
Serial.println(Tmax);
}
/**
*
*/
void Hexapod_Demo::findMinMax()
{
double HX_X_MIN = 0;
double HX_X_MAX = 0;
// double HX_X_MID = 0;
double HX_Y_MIN = 0;
double HX_Y_MAX = 0;
// double HX_Y_MID = 0;
double HX_Z_MIN = 0;
double HX_Z_MAX = 0;
// double HX_Z_MID = 0;
double HX_A_MIN = 0;
double HX_A_MAX = 0;
// double HX_A_MID = 0;
double HX_B_MIN = 0;
double HX_B_MAX = 0;
// double HX_B_MID = 0;
double HX_C_MIN = 0;
double HX_C_MAX = 0;
// double HX_C_MID = 0;
unsigned long T1 = millis();
unsigned long timeToFindMinMax;
uint8_t movOK;
angle_t servo_angles[NB_SERVOS];
double COORD_MIN = -100.0;
double COORD_MAX = 200.0;
double COORD_INC = 1;
for (double coord = COORD_MIN; coord < COORD_MAX; coord += COORD_INC)
{
// Find X min/max.
movOK = hx_servo.calcServoAngles({coord, 0, 0, 0, 0, 0}, servo_angles);
if (movOK == 0)
{
if (coord < HX_X_MIN)
HX_X_MIN = coord;
else if (coord > HX_X_MAX)
HX_X_MAX = coord;
}
// Find Y min/max.
movOK = hx_servo.calcServoAngles({0, coord, 0, 0, 0, 0}, servo_angles);
if (movOK == 0)
{
if (coord < HX_Y_MIN)
HX_Y_MIN = coord;
else if (coord > HX_Y_MAX)
HX_Y_MAX = coord;
}
// Find Z min/max.
movOK = hx_servo.calcServoAngles({0, 0, coord, 0, 0, 0}, servo_angles);
if (movOK == 0)
{
if (coord < HX_Z_MIN)
HX_Z_MIN = coord;
else if (coord > HX_Z_MAX)
HX_Z_MAX = coord;
}
}
COORD_MIN = -HALF_PI;
COORD_MAX = HALF_PI;
COORD_INC = radians(1);
for (double coord = COORD_MIN; coord < COORD_MAX; coord += COORD_INC)
{
// Find A min/max.
movOK = hx_servo.calcServoAngles({0, 0, 0, coord, 0, 0}, servo_angles);
if (movOK == 0)
{
if (coord < HX_A_MIN)
HX_A_MIN = coord;
else if (coord > HX_A_MAX)
HX_A_MAX = coord;
}
// Find B min/max.
movOK = hx_servo.calcServoAngles({0, 0, 0, 0, coord, 0}, servo_angles);
if (movOK == 0)
{
if (coord < HX_B_MIN)
HX_B_MIN = coord;
else if (coord > HX_B_MAX)
HX_B_MAX = coord;
}
// Find C min/max.
movOK = hx_servo.calcServoAngles({0, 0, 0, 0, 0, coord}, servo_angles);
if (movOK == 0)
{
if (coord < HX_C_MIN)
HX_C_MIN = coord;
else if (coord > HX_C_MAX)
HX_C_MAX = coord;
}
}
// HX_X_MID = (HX_X_MAX + HX_X_MIN) / 2;
// HX_Y_MID = (HX_Y_MAX + HX_Y_MIN) / 2;
// HX_Z_MID = (HX_Z_MAX + HX_Z_MIN) / 2;
// HX_A_MID = (HX_A_MAX + HX_A_MIN) / 2;
// HX_B_MID = (HX_B_MAX + HX_B_MIN) / 2;
// HX_C_MID = (HX_C_MAX + HX_C_MIN) / 2;
timeToFindMinMax = millis() - T1;
Serial.println("\n########## MIN / MAX ##########");
Serial.print("\nHX_X_MIN = ");
Serial.print(HX_X_MIN);
Serial.println(" mm");
Serial.print("HX_X_MAX = ");
Serial.print(HX_X_MAX);
Serial.println(" mm");
Serial.print("\nHX_Y_MIN = ");
Serial.print(HX_Y_MIN);
Serial.println(" mm");
Serial.print("HX_Y_MAX = ");
Serial.print(HX_Y_MAX);
Serial.println(" mm");
Serial.print("\nHX_Z_MIN = ");
Serial.print(HX_Z_MIN);
Serial.println(" mm");
Serial.print("HX_Z_MAX = ");
Serial.print(HX_Z_MAX);
Serial.println(" mm");
Serial.print("\nHX_A_MIN = ");
Serial.print(degrees(HX_A_MIN));
Serial.println(" deg");
Serial.print("HX_A_MAX = ");
Serial.print(degrees(HX_A_MAX));
Serial.println(" deg");
Serial.print("\nHX_B_MIN = ");
Serial.print(degrees(HX_B_MIN));
Serial.println(" deg");
Serial.print("HX_B_MAX = ");
Serial.print(degrees(HX_B_MAX));
Serial.println(" deg");
Serial.print("\nHX_C_MIN = ");
Serial.print(degrees(HX_C_MIN));
Serial.println(" deg");
Serial.print("HX_C_MAX = ");
Serial.print(degrees(HX_C_MAX));
Serial.println(" deg");
Serial.print("\ntimeToFindMinMax = ");
Serial.print(timeToFindMinMax);
Serial.println(" ms");
}