-
Notifications
You must be signed in to change notification settings - Fork 0
/
romi_ing.ino
527 lines (475 loc) · 14.5 KB
/
romi_ing.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
#include "encoders.h"
#include "lineSensors.h"
#include "common_sys.h"
#include "pid.h"
#include "SimpleKalmanFilter.h"
#include "kinematics.h"
//~~~~~~~~~~~~~~~~~~TODO's~~~~~~~~~~~~~~~~~~~~~//
// TODO: Look at the idea of confidence for line following for the robot.
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//float max_speed = 4.2; //Max reachable speed of the wheels -> output is 255
int max_power = 35; // ~5.06
int max_power_home = 60;
int min_power = 20;
float max_des_speed = 2.0*M_PI;
static float max_ang_vel = 3.0*M_PI;
static float max_linear_vel = 0.1;
/*20= 2.02, 50 = 5.7, 63.75= 6.8, 100=11.0 , 152 = 17.2, 191.25 = 20.5, 235=24.96 ,255=26.84
speed = 0.1056*(PWM) + 0.287 is an alright linear approximation to convert pwm signal to speed values
*/
float slope = 0.1056, y_int = 0.287;
PID left_wheel( 1.0, 0.02, 0.0000);
PID right_wheel(1.0, 0.02, 0.0000);
//PID heading(1.4,0.0,0.01);
//PID heading(1.0,0.0,0.035);
//PID heading(0.42, 0.005,0.00006);
//PID heading(0.45, 0.001,0.004);
PID heading(0.58, 0.01,0.0005);
PID rth_heading(1.0,0.0,0.0);
PID rth_position(0.05, 0.0, 0.0);
//PID rth_heading2(0.05,0.001,1.0);
/*Passed between states*/
volatile byte l_power;
volatile byte r_power;
volatile bool l_direction;
volatile bool r_direction;
unsigned long last_timestamp;
unsigned long beep_timestamp;
float distance_from_home = 0.0;
int count = 0;
//intialise the state
// NB: -1, -2, -3, -4 are a debuging state So use that accordingly
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
int state = 0;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
bool isClose = false;
bool shouldBeep = true;
bool direction_chosen = false;
float current_rotation = 0.0;
/*Sensors*/
LineSensor l_sensor(LINE_LEFT_PIN);
LineSensor c_sensor(LINE_CENTRE_PIN);
LineSensor r_sensor(LINE_RIGHT_PIN);
//Interrupt definition here
double hz = 250.0;
volatile double prev_theta_e1 = 0.0;
volatile double prev_theta_e0 = 0.0;
// Variables for speed that are globally acessible
volatile double left_wheel_vel;
volatile double left_wheel_est;
volatile double right_wheel_vel;
volatile double right_wheel_est;
SimpleKalmanFilter left_filter(0.3,0.1,0.01);
SimpleKalmanFilter right_filter(0.3,0.1,0.01);
volatile double confidence = -1.0;
//Control loop timers
Kinematics2D::Kinematics Romi(0.14, 0.035);
ISR( TIMER3_COMPA_vect ) {
// Do speed calcs and odom in here
left_wheel_vel = (theta_e0 - prev_theta_e0)*hz;
right_wheel_vel = (theta_e1 - prev_theta_e1)*hz;
left_wheel_est = left_filter.updateEstimate(left_wheel_vel);
right_wheel_est = right_filter.updateEstimate(right_wheel_vel);
//If the wheels are not moving clip the values at zero
if(left_wheel_vel == 0)left_wheel_est=0.0;
if(right_wheel_vel == 0)right_wheel_est=0.0;
Romi.update(left_wheel_vel, right_wheel_vel, millis());
prev_theta_e0 = theta_e0;
prev_theta_e1 = theta_e1;
if(state == 0 || state ==1){
if((l_sensor.readCalibrated()+ c_sensor.readCalibrated() + r_sensor.readCalibrated())/3 < 100)
{
confidence -= 0.004;
}
else
{
confidence += 0.004;
}
confidence = max(min(1, confidence), -1);
}
}
void stateCleanup(void)
{
l_power = 0.0;
r_power =0.0;
r_direction = FORWARD;
l_direction = FORWARD;
right_wheel.reset();
left_wheel.reset();
rth_heading.reset();
rth_position.reset();
direction_chosen = false;
current_rotation = 0.0;
last_timestamp = millis();
beep_timestamp = millis();
shouldBeep = true;
}
bool startCalibrate = false;
bool startSystem = false;
void setup() {
left_wheel.setMax(max_des_speed);
right_wheel.setMax(max_des_speed);
heading.setMax(1.0);
rth_heading.setMax(M_PI);
rth_position.setMax(0.01);
pinMode(13, OUTPUT);
Serial.begin( 9600 );
pinMode(17, INPUT);
int buttonState = 0; // variable for reading the pushbutton status
while(!startCalibrate)
{
buttonState = digitalRead(17);
if(buttonState == LOW)
{
startCalibrate = true;
}
}
delay(1000);
// run a calibrationfor the light sensor
l_sensor.calibrate();
c_sensor.calibrate();
r_sensor.calibrate();
pinMode(6, OUTPUT);
flash_leds(500);
// wait for a second
analogWrite(6,100);
delay(250);
analogWrite(6,0);
while(!startSystem)
{
buttonState = digitalRead(17);
if(buttonState == LOW)
{
startSystem = true;
}
}
delay(1000);
stateCleanup();
//setup timer for speed and odom timed calculations
setupMotorPins();
setupEncoder0();
setupEncoder1();
setupTimer3(hz);
last_timestamp = millis();
beep_timestamp = millis();
}
void loop()
{
unsigned long time_now = millis();
unsigned long elapsed_time = time_now - last_timestamp;
unsigned long beep_time = time_now - beep_timestamp;
//switch case logic for romi -> each state should only adjust power and direction of motors for keeping traceability
switch(state){
case 0:
{
if((l_sensor.readCalibrated()+ c_sensor.readCalibrated() + r_sensor.readCalibrated())/3 < 300)
{
if(elapsed_time >= 50)
{
last_timestamp = millis();
float right_output, left_output;
right_output = right_wheel.update(0.6*max_des_speed, right_wheel_est);
left_output = left_wheel.update(0.6*max_des_speed, left_wheel_est);
// right_output = 0.7*max_des_speed;
// left_output = 0.7* max_des_speed;
l_direction = FORWARD;
r_direction = FORWARD;
if(abs((left_output - y_int)/slope) > max_power)
{
l_power = (byte) max_power;
}
else
{
l_power = (byte)abs((left_output - y_int)/slope);
}
if(abs((right_output - y_int)/slope)> max_power)
{
r_power = (byte) max_power;
}
else
{
r_power = (byte)abs((right_output - y_int)/slope);
}
}
}
else
{
l_power = 0;
r_power = 0;
}
if(confidence >=0.0)
{
if(shouldBeep)
{
shouldBeep = false;
beep_timestamp = millis();
analogWrite(6,100);
digitalWrite(13, HIGH);
beep_time = 0;
}
if(beep_time >=500)
{
analogWrite(6,0);
digitalWrite(13, LOW);
stateCleanup();
state = 1;
break;
}
}
}
break;
case 1:
{
float m = weightedPower(l_sensor, c_sensor, r_sensor, min_power, max_power);
if( elapsed_time >= 50)
{
float heading_output = 0.0;
float right_output = 0.0;
float left_output = 0.0;
float forward_vel = 0.0;
float right_vel, left_vel;
float ang_vel = 0.0;
last_timestamp = millis();
heading_output = heading.update(0.0, m);
count++;
if(count%2==0)
{
forward_vel = float_map(confidence, -1.0, 1.0, 0.0, 0.9)*max_linear_vel;
ang_vel = heading_output*1.0*max_ang_vel;
Romi.robotVelToWheelVels(forward_vel, ang_vel, left_vel, right_vel);
// right_output = right_wheel.update(right_wheel_vel, right_wheel_est);
// left_output = left_wheel.update(left_wheel_vel, left_wheel_est);
//
// left_output= -heading_output*(max_des_speed);
// right_output = heading_output*(max_des_speed);
left_output = left_vel;
right_output = right_vel;
count = 0;
if(left_output < 0)
{
l_direction = REVERSE;
}
else
{
l_direction = FORWARD;
}
if(right_output < 0)
{
r_direction = REVERSE;
}
else
{
r_direction = FORWARD;
}
if(abs((left_output - y_int)/slope) > max_power)
{
l_power = (byte) max_power;
}
else
{
l_power = (byte)abs((left_output - y_int)/slope);
}
if(abs((right_output - y_int)/slope)> max_power)
{
r_power = (byte) max_power;
}
else
{
r_power = (byte)abs((right_output - y_int)/slope);
}
}
}
if(confidence <=-1.0)
{
l_power = 0;
r_power = 0;
if(shouldBeep)
{
shouldBeep = false;
beep_timestamp = millis();
analogWrite(6,100);
digitalWrite(13, HIGH);
beep_time = 0;
}
if(beep_time >=1000)
{
analogWrite(6,0);
digitalWrite(13, LOW);
stateCleanup();
state = 2;
break;
}
}
}
break;
case 2:
{
/*Return to home -
First lets look at home
*/
if( elapsed_time >= 20 )
{
float right_output = 0.0;
float left_output = 0.0;
float alpha = acos((-Romi.getPose().x*cos(Romi.getPose().theta) - Romi.getPose().y*sin(Romi.getPose().theta))/sqrt(square(Romi.getPose().x) + square(Romi.getPose().y)));
float home_heading = (-Romi.getPose().y*cos(Romi.getPose().theta) + Romi.getPose().x*sin(Romi.getPose().theta)) > 0.0 ? alpha: -alpha;
float ang_vel = (home_heading > 0)? 0.2*max_ang_vel : -0.2*max_ang_vel ;
float head_tol = M_PI/180.0;
// if(isClose)
// {
// ang_vel = (home_heading > 0)? 0.2*max_ang_vel : -0.2*max_ang_vel ;
// head_tol = M_PI/90.0;
// }
last_timestamp = millis();
count++;
if(count%2==0)
{
count = 0;
if(home_heading > head_tol || home_heading < -head_tol)
{
float right_vel, left_vel;
Romi.robotVelToWheelVels(0.0, ang_vel, left_vel, right_vel);
right_output = right_vel;
left_output = left_vel;
if(left_output < 0)
{
l_direction = REVERSE;
}
else
{
l_direction = FORWARD;
}
if(right_output < 0)
{
r_direction = REVERSE;
}
else
{
r_direction = FORWARD;
}
if(abs((left_output - y_int)/slope) > max_power)
{
l_power = (byte) max_power;
}
else
{
l_power = (byte)abs((left_output - y_int)/slope);
}
if(abs((right_output - y_int)/slope)> max_power)
{
r_power = (byte) max_power;
}
else
{
r_power = (byte)abs((right_output - y_int)/slope);
}
}
else
{
distance_from_home = sqrt(square(Romi.getPose().x) + square(Romi.getPose().y));
stateCleanup();
state = 3;
break;
}
}
}
}
break;
case 3:
{
/*TODO: Re=implement this entire logic once Iv'e tested the rotation state*/
if( elapsed_time >= 50)
{
float abs_distance = sqrt(square(Romi.getPose().x) + square(Romi.getPose().y));
float right_output = 0.0;
float left_output = 0.0;
float alpha = acos((-Romi.getPose().x*cos(Romi.getPose().theta) - Romi.getPose().y*sin(Romi.getPose().theta))/sqrt(square(Romi.getPose().x) + square(Romi.getPose().y)));
float home_heading = (-Romi.getPose().y*cos(Romi.getPose().theta) + Romi.getPose().x*sin(Romi.getPose().theta)) > 0.0 ? alpha: -alpha;
float ang_vel = (home_heading > 0)? float_map(abs(home_heading), 0.0, M_PI, 0.0, 3.0)*max_ang_vel : -float_map(abs(home_heading), 0.0, M_PI, 0.0, 3.0)*max_ang_vel ;
// float head_tol = M_PI/90.0;
// float lin_vel = float_map(abs_distance, 0.0, distance_from_home, 0.5,1.0)*max_linear_vel;
float lin_vel = max_linear_vel;
last_timestamp = millis();
// if( !isClose && abs_distance < 0.2)
// {
// // Re-orient when we are close
// isClose = true;
// stateCleanup();
// state = 2;
// break;
// }
count++;
if(count%2==0)
{
float right_vel, left_vel;
Romi.robotVelToWheelVels(lin_vel, ang_vel, left_vel, right_vel);
right_output = right_vel;
left_output = left_vel;
count = 0;
if(abs_distance > 0.01 && Romi.getPose().x >= 0)
{
if(left_output < 0)
{
l_direction = REVERSE;
}
else
{
l_direction = FORWARD;
}
if(right_output < 0)
{
r_direction = REVERSE;
}
else
{
r_direction = FORWARD;
}
if(abs((left_output - y_int)/slope) > max_power_home)
{
l_power = (byte) max_power_home;
}
else
{
l_power = (byte)abs((left_output - y_int)/slope);
}
if(abs((right_output - y_int)/slope)> max_power_home)
{
r_power = (byte) max_power_home;
}
else
{
r_power = (byte)abs((right_output - y_int)/slope);
}
}
else
{
stateCleanup();
state = 4;
break;
}
}
}
}
break;
case 4:
{
l_power = 0.0;
r_power = 0.0;
Serial.print(Romi.getPose().x,3);
Serial.print(",");
Serial.print(Romi.getPose().y, 3);
Serial.print(",");
Serial.println(Romi.getPose().theta,6);
// Finished
}
break;
}
// Send power PWM to pins, to motor drivers.
digitalWrite( R_DIR_PIN, r_direction);
digitalWrite( L_DIR_PIN, l_direction);
analogWrite( L_PWM_PIN, l_power );
analogWrite( R_PWM_PIN, r_power );
//Serial.print(confidence);
//Serial.print(",");
//Serial.println(state);
}