-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3_motion.ino
454 lines (398 loc) · 13.2 KB
/
3_motion.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
void power_motor(){
// power up the motor
digitalWrite(motorl1_enable, HIGH);
digitalWrite(motorl2_enable, HIGH);
digitalWrite(motorr1_enable, HIGH);
digitalWrite(motorr2_enable, HIGH);
}
void set_motor_direction(int dir){
// set up the motor direction pin to the direction you want
// 1 = forward
// 2 = backward
// 3 = right
// 4 = left
switch(dir){
case 2:
digitalWrite(motorr1_dir, LOW);
digitalWrite(motorr2_dir, LOW);
digitalWrite(motorl1_dir, HIGH);
digitalWrite(motorl2_dir, HIGH);
break;
case 1:
digitalWrite(motorr1_dir, HIGH);
digitalWrite(motorr2_dir, HIGH);
digitalWrite(motorl1_dir, LOW);
digitalWrite(motorl2_dir, LOW);
break;
case 3:
digitalWrite(motorr1_dir, LOW);
digitalWrite(motorr2_dir, LOW);
digitalWrite(motorl1_dir, LOW);
digitalWrite(motorl2_dir, LOW);
break;
case 4:
digitalWrite(motorr1_dir, HIGH);
digitalWrite(motorr2_dir, HIGH);
digitalWrite(motorl1_dir, HIGH);
digitalWrite(motorl2_dir, HIGH);
break;
default:
break;
}
}
void stop_motor(){
// stop the motor, but not cut the power
// becareful when you use this function, don't left the motor stop too fast!
analogWrite(motorr1_pwm,0);
analogWrite(motorr2_pwm,0);
analogWrite(motorl1_pwm,0);
analogWrite(motorl2_pwm,0);
}
void cut_power(){
// cut out power for all motors, helps to save the motor when stalling
digitalWrite(motorl1_enable, LOW);
digitalWrite(motorl2_enable, LOW);
digitalWrite(motorr1_enable, LOW);
digitalWrite(motorr2_enable, LOW);
// digitalWrite(motorr1_dir, LOW);
// digitalWrite(motorr2_dir, LOW);
// digitalWrite(motorl1_dir, LOW);
// digitalWrite(motorl2_dir, LOW);
//stop_motor();
}
void report_error(){
// stop the motor and report the error by turning on red LED
// have to restart the Arduino to reset
cut_power();
blink_LED(1, 500);
while(1){}
}
void turn_to_direction(float goal){
// turn the robot to a certain degree
// Assumption: 0 <= goal <= 360
// return normally if success
// light up red LED and lock itself if there is failure
clear_compass_buffer();
static int individual_speed[4] = {32,32,32,32};
long last_Reading[4] = {0,0,0,0};
long current_Reading[4] = {0,0,0,0};
long target_speed = 2;
float kp[4] = {3.0,3.0,3.0,3.0};
int stall_counter[4] = {0,0,0,0};
int max_stall_counter = 20;
int max_stall_speed = 10;
double accuracy = 0.3;
int prev_direction = 1, current_direction = 1;
leftEncoder.setEncoderCount(0);
rightEncoder.setEncoderCount(0);
leftEncoder2.setEncoderCount(0);
rightEncoder2.setEncoderCount(0);
unsigned long last_time = millis();
long elapsed_time;
float error = goal - get_direction_filter10();
if (error > 180) error = error - 360;
else if (error < -180) error = error + 360;
power_motor();
while (abs(error) > accuracy){
// set up direction
if (error > 0){
set_motor_direction(3);
current_direction = 1;
}
else{
set_motor_direction(4);
current_direction = 0;
}
// wait for a short time to avoid changing direction rapidly
if (current_direction != prev_direction){
cut_power();
active_wait(500);
power_motor();
}
prev_direction = current_direction;
// get encoder reading
current_Reading[0] = leftEncoder.getEncoderCount();
current_Reading[1] = rightEncoder.getEncoderCount();
current_Reading[2] = leftEncoder2.getEncoderCount();
current_Reading[3] = rightEncoder2.getEncoderCount();
elapsed_time = millis() - last_time;
last_time = millis();
// calculate amount of motor rotation for this loop
for (int i = 0; i<4;i++){last_Reading[i] = abs(current_Reading[i] - last_Reading[i]);}
// check if any motor is not rotating, stop the alrorithm if any motor get stalled for too long
for (int i = 0; i<4;i++){
if (last_Reading[i] <= max_stall_speed){
stall_counter[i] ++;
if (stall_counter[i] >= max_stall_counter){
cut_power();
report_error();
}
}
else {stall_counter[i] = 0;}
}
// use proportional controller to controll the motor speed to follow the desired speed
for (int i = 0; i<4;i++){
individual_speed[i] += (target_speed*elapsed_time - last_Reading[i])*kp[i]/elapsed_time;
individual_speed[i] = max(0,individual_speed[i]);
individual_speed[i] = min(max_power,individual_speed[i]);
}
// output information to computer
Serial.print(target_speed*elapsed_time);
Serial.print(",");
for (int i = 0; i<4;i++){
Serial.print(last_Reading[i]);
Serial.print(",");
}
Serial.print(elapsed_time);
Serial.print(",");
for (int i = 0; i<4;i++){
Serial.print(individual_speed[i]);
Serial.print(",");
}
Serial.println(".");
// oupdate last reading
for (int i = 0; i<4;i++){last_Reading[i] = current_Reading[i];}
// drive motor
analogWrite(motorr1_pwm,individual_speed[2]);
analogWrite(motorr2_pwm,individual_speed[0]);
analogWrite(motorl1_pwm,individual_speed[3]);
analogWrite(motorl2_pwm,individual_speed[1]);
// update error
error = goal - get_direction_filter10();
if (error > 180) error = error - 360;
else if (error < -180) error = error + 360;
active_wait(10);
}
// stop motor
stop_motor();
cut_power();
return;
}
void drive_to_destination(long goal,float dir){
// drive the robot straight for a certain distance
// goal = distance you want to travel in the form of encoder reading
// dir = direction, not implemented, you can put in any number
// return normally if success
// if there is failure, light up red LED and lock itself
float error;
int stall_counter = 0;
int max_stall_counter = 20;
int max_stall_speed = 10;
int fast_speed,slow_speed,motor_speedd = motor_speed;
motor_speedd = max(0,motor_speedd);
motor_speedd = min(max_power,motor_speedd);
long currentLeftEncoderCount = 0;
long currentRightEncoderCount = 0;
long distance_traveled = 0;
long last_distance = 0;
leftEncoder.setEncoderCount(0);
rightEncoder.setEncoderCount(0);
if (goal >= 0){
set_motor_direction(1);
}
else{
set_motor_direction(2);
}
power_motor();
analogWrite(motorr1_pwm,motor_speedd);
analogWrite(motorr2_pwm,motor_speedd);
analogWrite(motorl1_pwm,motor_speedd);
analogWrite(motorl2_pwm,motor_speedd);
goal = abs(goal);
while(distance_traveled < goal){
avoid_obstacle();
// ----------- code for direction correction, not tested ------------
// error = dir - get_direction_filter10();
// if (error > 180) error = error - 360;
// else if (error < -180) error = error + 360;
//
// speed_scale = abs(error)/5.0*0.5;
// fast_speed = motor_speed * (1.0 + speed_scale);
// fast_speed = min(200,fast_speed);
// slow_speed = motor_speed * (1.0 - speed_scale);
// slow_speed = max(16,slow_speed);
//
//// Serial.print(dir,3);
//// Serial.print(" , ");
//// Serial.print(error,3);
//// Serial.print(" , ");
//// Serial.print(speed_scale);
//// Serial.print(" , ");
//// Serial.print(slow_speed);
//// Serial.print(" , ");
//// Serial.println(fast_speed);
//
// if (error < 0){
// analogWrite(motorr1_pwm,fast_speed);
// analogWrite(motorr2_pwm,fast_speed);
// analogWrite(motorl1_pwm,slow_speed);
// analogWrite(motorl2_pwm,slow_speed);
// }
// else{
// analogWrite(motorr1_pwm,slow_speed);
// analogWrite(motorr2_pwm,slow_speed);
// analogWrite(motorl1_pwm,fast_speed);
// analogWrite(motorl2_pwm,fast_speed);
// }
currentLeftEncoderCount = leftEncoder.getEncoderCount();
currentRightEncoderCount = rightEncoder.getEncoderCount();
distance_traveled = abs((currentLeftEncoderCount+currentRightEncoderCount)/2);
if ((distance_traveled - last_distance) <= max_stall_speed){
stall_counter ++;
if (stall_counter >= max_stall_counter){
cut_power();
report_error();
}
}
else {stall_counter = 0;}
last_distance = distance_traveled;
active_wait(10);
}
Serial.println(distance_traveled);
stop_motor();
cut_power();
return;
}
void turn_right(float degree){
// make the car turn right by certain amount of degree
// if you want the car to turn left, input a negative number
// return normally if success
// if there is failure, light up red LED and lock itself
if (degree > 180) degree = degree - 360;
else if (degree < -180) degree = degree + 360;
float current_degree = get_direction_filter10();
float goal = get_direction_filter10()+ degree;
if (goal > 180) goal = goal - 360;
else if (goal < -180) goal = goal + 360;
turn_to_direction(goal);
}
void turn_for_a_period(int dir, int time_in_ms){
// make the car turn right or left by certain amount of time
// if dir > 0, turn right
// if dir <=0, turn left
// Total driving duration = time_in_ms
// return normally if success
// if there is failure, light up red LED and lock itself
static int individual_speed[4] = {32,32,32,32};
long last_Reading[4] = {0,0,0,0};
long current_Reading[4] = {0,0,0,0};
long target_speed = 2;
float kp[4] = {3.0,3.0,3.0,3.0};
int stall_counter[4] = {0,0,0,0};
int max_stall_counter = 20;
int max_stall_speed = 10;
leftEncoder.setEncoderCount(0);
rightEncoder.setEncoderCount(0);
leftEncoder2.setEncoderCount(0);
rightEncoder2.setEncoderCount(0);
power_motor();
// set up direction
if (dir > 0){set_motor_direction(3);}
else{set_motor_direction(4);}
long elapsed_time;
unsigned long last_time = millis();
unsigned long start = millis();
while(millis()-start < time_in_ms){
// get encoder reading
current_Reading[0] = leftEncoder.getEncoderCount();
current_Reading[1] = rightEncoder.getEncoderCount();
current_Reading[2] = leftEncoder2.getEncoderCount();
current_Reading[3] = rightEncoder2.getEncoderCount();
elapsed_time = millis() - last_time;
last_time = millis();
// calculate amount of motor rotation for this loop
for (int i = 0; i<4;i++){last_Reading[i] = abs(current_Reading[i] - last_Reading[i]);}
// check if any motor is not rotating, stop the alrorithm if any motor get stalled for too long
for (int i = 0; i<4;i++){
if (last_Reading[i] <= max_stall_speed){
stall_counter[i] ++;
if (stall_counter[i] >= max_stall_counter){
cut_power();
report_error();
}
}
else {stall_counter[i] = 0;}
}
// use proportional controller to controll the motor speed to follow the desired speed
for (int i = 0; i<4;i++){
individual_speed[i] += (target_speed*elapsed_time - last_Reading[i])*kp[i]/elapsed_time;
individual_speed[i] = max(0,individual_speed[i]);
individual_speed[i] = min(max_power,individual_speed[i]);
}
// output information to computer
Serial.print(target_speed*elapsed_time);
Serial.print(",");
for (int i = 0; i<4;i++){
Serial.print(last_Reading[i]);
Serial.print(",");
}
Serial.print(elapsed_time);
Serial.print(",");
for (int i = 0; i<4;i++){
Serial.print(individual_speed[i]);
Serial.print(",");
}
Serial.println(".");
// oupdate last reading
for (int i = 0; i<4;i++){last_Reading[i] = current_Reading[i];}
// drive motor
analogWrite(motorr1_pwm,individual_speed[2]);
analogWrite(motorr2_pwm,individual_speed[0]);
analogWrite(motorl1_pwm,individual_speed[3]);
analogWrite(motorl2_pwm,individual_speed[1]);
active_wait(10);
}
// stop motor
cut_power();
return;
}
void drive_straight(int dir, int time_in_ms){
// drive the motor straight for a certain amount of time
// if dir > 0, drive forward
// if dir <=0, drive backward
// Total driving duration = time_in_ms
// return normally if success
// if there is failure, light up red LED and lock itself
unsigned long prev_time;
int stall_counter = 0;
int max_stall_counter = 20;
int max_stall_speed = 10;
int motor_speedd = motor_speed;
motor_speedd = max(0,motor_speedd);
motor_speedd = min(max_power,motor_speedd);
long currentLeftEncoderCount = 0;
long currentRightEncoderCount = 0;
long distance_traveled = 0;
long last_distance = 0;
leftEncoder2.setEncoderCount(0);
rightEncoder2.setEncoderCount(0);
if (dir > 0){
set_motor_direction(1);
}
else{
set_motor_direction(2);
}
power_motor();
analogWrite(motorr1_pwm,motor_speedd);
analogWrite(motorr2_pwm,motor_speedd);
analogWrite(motorl1_pwm,motor_speedd);
analogWrite(motorl2_pwm,motor_speedd);
prev_time = millis();
while(millis()-prev_time < time_in_ms){
currentLeftEncoderCount = leftEncoder2.getEncoderCount();
currentRightEncoderCount = rightEncoder2.getEncoderCount();
distance_traveled = abs((currentLeftEncoderCount+currentRightEncoderCount)/2);
if ((distance_traveled - last_distance) <= max_stall_speed){
stall_counter ++;
if (stall_counter >= max_stall_counter){
cut_power();
report_error();
}
}
else {stall_counter = 0;}
last_distance = distance_traveled;
active_wait(10);
}
cut_power();
return;
}