-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRML_RobotArm_v3.ino
582 lines (496 loc) · 13.3 KB
/
RML_RobotArm_v3.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
#include <Servo.h>
#define NUMBER_OF_SERVOS (4)
#if 1
#define FWD_MIN (46)
#define FWD_MAX (166)
#define LIFT_MIN (80)
#define LIFT_MAX (138)
#define TURN_MIN (0)
#define TURN_MAX (180)
#define JAW_MIN (40)
#define JAW_MAX (140)
#else
//used for calibration - wide limits
#define FWD_MIN (0)
#define FWD_MAX (180)
#define LIFT_MIN (0)
#define LIFT_MAX (180)
#define TURN_MIN (0)
#define TURN_MAX (180)
#define JAW_MIN (0)
#define JAW_MAX (180)
#endif
#define FWD_MED ((FWD_MAX+FWD_MIN)/2)
#define LIFT_MED ((LIFT_MAX+LIFT_MIN)/2)
#define TURN_MED ((TURN_MAX+TURN_MIN)/2)
#define JAW_MED ((JAW_MAX+JAW_MIN)/2)
#define FWD (0)
#define LFT (1)
#define TRN (2)
#define JAW (3)
#define NUM_STATES (6)
#define STEP_DELAY (30)
#define DELAY_BETWEEN_STATES (500)
#define autoStartPin (3)
#define autoStopPin (14)
#define servo_fwd (11)
#define servo_lift (10)
#define servo_turn (9)
#define servo_jaw (5)
typedef struct {
Servo servo_id;
const char* servo_name;
char pin;
int value_current;
int value_init;
int value_min;
int value_max;
} ServoState;
int states[NUM_STATES][NUMBER_OF_SERVOS];
Servo servos[NUMBER_OF_SERVOS];
/*
ServoState ServoControl[NUMBER_OF_SERVOS] = {servos[FWD], "servo forward", servo_fwd, FWD_MED, 100, FWD_MIN, FWD_MAX,
servos[LFT], "servo lift", servo_lift, LIFT_MED, 130, LIFT_MIN, LIFT_MAX,
servos[TRN], "servo turn", servo_turn, TURN_MED, 164, TURN_MIN, TURN_MAX,
servos[JAW], "servo jaw", servo_jaw, JAW_MED, 46, JAW_MIN, JAW_MAX
};
*/
ServoState ServoControl[NUMBER_OF_SERVOS] = {servos[FWD], "servo forward", servo_fwd, FWD_MED, 120, FWD_MIN, FWD_MAX,
servos[LFT], "servo lift", servo_lift, LIFT_MED, 100, LIFT_MIN, LIFT_MAX,
servos[TRN], "servo turn", servo_turn, TURN_MED, 105, TURN_MIN, TURN_MAX,
servos[JAW], "servo jaw", servo_jaw, JAW_MED, 46, JAW_MIN, JAW_MAX
};
int autoStartPinValue = LOW;
int autoStopPinValue = LOW;
void setup()
{
/*
#define FWD_MIN (46)
#define FWD_MAX (166)
#define LIFT_MIN (80)
#define LIFT_MAX (138)
#define TURN_MIN (0)
#define TURN_MAX (180)
#define JAW_MIN (40)
#define JAW_MAX (140)
// positin ready to grab cube
states[0][0] = 100;
states[0][1] = 86;
states[0][2] = 90;
states[0][3] = 100;
// position grab cube
states[1][0] = 160;
states[1][1] = 86;
states[1][2] = 90;
states[1][3] = 46;
// position over cup
states[2][0] = 134;
states[2][1] = 134;
states[2][2] = 130;
states[2][3] = 46;
// position drop cube into cup
states[3][0] = 134;
states[3][1] = 134;
states[3][2] = 130;
states[3][3] = 100;
// position over STOP button
states[4][0] = 134;
states[4][1] = 134;
states[4][2] = 165;
states[4][3] = 46;
// position push STOP button
states[5][0] = 166;
states[5][1] = 80;
states[5][2] = 165;
states[5][3] = 46;
*/
// positin ready to grab cube
states[0][0] = 100;
states[0][1] = 86;
states[0][2] = 30;
states[0][3] = 100;
// position grab cube
states[1][0] = 160;
states[1][1] = 86;
states[1][2] = 30;
states[1][3] = 46;
// position over cup
states[2][0] = 134;
states[2][1] = 134;
states[2][2] = 70;
states[2][3] = 46;
// position drop cube into cup
states[3][0] = 134;
states[3][1] = 134;
states[3][2] = 70;
states[3][3] = 100;
// position over STOP button
states[4][0] = 134;
states[4][1] = 134;
states[4][2] = 105;
states[4][3] = 46;
// position push STOP button
states[5][0] = 166;
states[5][1] = 80;
states[5][2] = 105;
states[5][3] = 46;
for (int i = 0; NUMBER_OF_SERVOS > i; i++)
{
ServoControl[i].servo_id.attach(ServoControl[i].pin);
ServoControl[i].servo_id.write(ServoControl[i].value_init);
ServoControl[i].value_current = ServoControl[i].value_init;
}
Serial.begin(9600);
PrintHelp();
Serial.println("Attach done");
MoveToInitialState();
Serial.println("Setup done");
}
void loop()
{
int use_servo = 0;
String inData;
String inOperation;
String inValue;
int inValue_int = 0;
static int do_once = 1;
inOperation = "#"; // "#" means "No Operation"
inValue = "";
inData = "";
while (Serial.available())
{
inData = Serial.readString();
Serial.print("Arduino Received: ");
Serial.println(inData);
inOperation = inData.substring(0, 1);
if (1 < inData.length())
{
inValue = inData.substring(1);
inValue_int = inValue.toInt();
}
else
{
inValue = "";
inValue_int = 0;
}
}
if (1 == do_once)
{
do_once = 0;
inOperation = "a";
inValue = "";
inData = "a";
inValue_int = 0;
}
#if 0
autoStartPinValue = digitalRead(autoStartPin);
if ((HIGH == autoStartPinValue) && (0 == do_once_power))
{
do_once_powr = 0;
inOperation = "a";
inValue = "";
inData = "a";
inValue_int = 0;
}
#endif
#if 0
autoStopPinValue = digitalRead(autoStopPin);
if (HIGH == autoStopPinValue)
{
inOperation = "q";
inValue = "";
inData = "q";
inValue_int = 0;
}
#endif
{
switch (inOperation[0])
{
case '#' :
{
delay(10); // "No operation" sleep a bit to save power
}
break;
case 'h' :
case 'H' :
{
PrintHelp();
}
break;
case 'q' :
{
MoveToInitialState();
}
break;
case 'i' :
case 'I' :
{
MoveToInitialState();
}
break;
case 'a' :
case 'A' :
{
Serial.println("Auto sequence start");
MoveToInitialState();
for (int i = 0; NUM_STATES > i; i++)
{
Serial.println("=====================");
MoveToState(i);
delay (DELAY_BETWEEN_STATES);
}
Serial.println("=====================");
MoveToState(4);
inOperation = "";
}
break;
case 'z' :
case 'Z' :
{
StoreState(inValue_int);
}
break;
// Move Servo FWD (0)
// ====================================
case 'f' :
case 'F' :
{
use_servo = FWD;
MoveServoSmoothByValue(use_servo, inValue_int);
}
PrintCurrentServoValues();
break;
// ====================================
// Move Servo LFT (1)
// ====================================
case 'l' :
case 'L' :
{
use_servo = LFT;
MoveServoSmoothByValue(use_servo, inValue_int);
}
PrintCurrentServoValues();
break;
// ====================================
// Move Servo TRN (2)
// ====================================
case 't' :
case 'T' :
{
use_servo = TRN;
MoveServoSmoothByValue(use_servo, inValue_int);
}
PrintCurrentServoValues();
break;
// ====================================
// Move Servo JAW (3)
// ====================================
case 'j' :
case 'J' :
{
use_servo = JAW;
MoveServoSmoothByValue(use_servo, inValue_int);
}
PrintCurrentServoValues();
break;
// ====================================
case 'p' :
case 'P' :
PrintCurrentServoValues();
PrintAllServoValues();
break;
default :
{
Serial.println("Input command is not decognised");
break;
}
}
}
}
void MoveToInitialState()
{
Serial.println("MoveToInitialState entry");
for (int i = 0; NUMBER_OF_SERVOS > i; i++)
{
MoveServoSmoothToPosition(i, ServoControl[i].value_init);
Serial.print("ServoControl[");
Serial.print(i);
Serial.print("] set to ");
Serial.println(ServoControl[i].value_init);
}
Serial.println("MoveToInitialState exit");
}
void MoveJawOpen()
{
Serial.println("MoveJawOpen entry");
ServoControl[JAW].value_current = JAW_MAX;
ServoControl[JAW].servo_id.write(ServoControl[JAW].value_current);
Serial.println("MoveJawOpen exit");
}
void MoveJawClose()
{
Serial.println("MoveJawClose entry");
ServoControl[JAW].value_current = JAW_MIN;
ServoControl[JAW].servo_id.write(ServoControl[JAW].value_current);
Serial.println("MoveJawClose exit");
}
void MoveToState(int state_id)
{
Serial.print("MoveToState entry state=");
Serial.println(state_id);
for (int i = 0; NUMBER_OF_SERVOS > i; i++)
{
if ((ServoControl[i].value_min <= states[state_id][i]) && (states[state_id][i] <= ServoControl[i].value_max))
{
MoveServoSmoothToPosition(i, states[state_id][i]);
Serial.print("ServoControl[");
Serial.print(i);
Serial.print("] set to ");
Serial.println(ServoControl[i].value_current);
}
else
{
Serial.println("Error: value outside limits");
}
}
Serial.println("MoveToState exit");
}
void StoreState(int state_id)
{
Serial.print("StoreState entry. state=");
Serial.println(state_id);
if ((0 <= state_id) && (NUM_STATES > state_id))
{
for (int i = 0; NUMBER_OF_SERVOS > i; i++)
{
states[state_id][i] = ServoControl[i].value_current;
Serial.print("states[");
Serial.print(state_id);
Serial.print("][");
Serial.print(i);
Serial.print("] = ");
Serial.println(states[state_id][i]);
}
}
Serial.println("StoreState exit");
}
void PrintCurrentServoValues()
{
Serial.println("PrintCurrentServoValues entry");
Serial.println("=====================");
for (int i = 0; NUMBER_OF_SERVOS > i; i++)
{
Serial.print("[");
Serial.print(i);
Serial.print("] = ");
Serial.print(ServoControl[i].value_current);
Serial.println(";");
}
Serial.println("=====================");
Serial.println("=====================");
Serial.println("PrintCurrentServoValues exit");
}
void PrintAllServoValues()
{
Serial.println("PrintAllServoValues entry");
for (int i = 0; NUM_STATES > i; i++)
{
Serial.println("=====================");
for (int j = 0; NUMBER_OF_SERVOS > j; j++)
{
Serial.print("states[");
Serial.print(i);
Serial.print("][");
Serial.print(j);
Serial.print("] = ");
Serial.print(states[i][j]);
Serial.println(";");
}
}
Serial.println("=====================");
Serial.println("=====================");
Serial.println("PrintAllServoValues exit");
}
void MoveServoSmoothByValue(int use_servo, int by_value)
{
int move_to = 0;
int increment = 0;
Serial.println("MoveServoSmoothByValue entry");
move_to = ServoControl[use_servo].value_current + by_value;
if ((ServoControl[use_servo].value_min <= move_to) && (move_to <= ServoControl[use_servo].value_max))
{
Serial.print("Moving servo ");
Serial.print(use_servo);
Serial.print(" by ");
Serial.println(by_value);
if (move_to < ServoControl[use_servo].value_current)
{
increment = -1;
}
else
{
increment = 1;
}
while (ServoControl[use_servo].value_current != move_to)
{
ServoControl[use_servo].value_current += increment;
ServoControl[use_servo].servo_id.write(ServoControl[use_servo].value_current);
delay(STEP_DELAY);
}
}
else
{
Serial.println("Error: value outside limits");
}
Serial.println("MoveServoSmoothByValue exit");
}
void MoveServoSmoothToPosition(int use_servo, int position_value)
{
int move_to = 0;
int increment = 0;
Serial.println("MoveServoSmoothToPosition entry");
move_to = position_value;
if ((ServoControl[use_servo].value_min <= move_to) && (move_to <= ServoControl[use_servo].value_max))
{
Serial.print("Moving servo ");
Serial.print(use_servo);
Serial.print(" to position ");
Serial.println(position_value);
Serial.print(" from position ");
Serial.println(ServoControl[use_servo].value_current);
if (move_to < ServoControl[use_servo].value_current)
{
increment = -1;
}
else
{
increment = 1;
}
while (ServoControl[use_servo].value_current != move_to)
{
ServoControl[use_servo].value_current += increment;
ServoControl[use_servo].servo_id.write(ServoControl[use_servo].value_current);
delay(STEP_DELAY);
}
}
else
{
Serial.println("Error: value outside limits");
}
Serial.println("MoveServoSmoothToPosition exit");
}
void PrintHelp()
{
Serial.println("=====================================");
Serial.println("!!!!!!!!!!!!!!! Important:Set COM port to send Newline !!!!!!!!!!!!!!!");
Serial.println("h - display this Help");
Serial.println("i - move Arm to initial state");
Serial.println("p - print current state and stored state values");
Serial.println("z - store current state");
Serial.println("a - auto-sequence start");
Serial.println("f - Move Servo 0 by <value>; value can be positive or negative");
Serial.println("l - Move Servo 1 by <value>; value can be positive or negative");
Serial.println("t - Move Servo 2 by <value>; value can be positive or negative");
Serial.println("j - Move Servo 3 by <value>; value can be positive or negative");
Serial.println("=====================================");
}