-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRomi_CodeStub.ino
More file actions
362 lines (298 loc) · 8.14 KB
/
Copy pathRomi_CodeStub.ino
File metadata and controls
362 lines (298 loc) · 8.14 KB
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
#include "encoders.h"
#include "pid.h"
#include "kinematics.h"
#include "lineSensors.h"
#include "motor.h"
//Pin definitions for motor
#define L_PWM_PIN 10
#define L_DIR_PIN 16
#define R_PWM_PIN 9
#define R_DIR_PIN 15
#define LINE_LEFT_PIN A2 //Pin for the left line sensor
#define LINE_CENTRE_PIN A3 //Pin for the centre line sensor
#define LINE_RIGHT_PIN A4 //Pin for the right line sensor
#define kp_linefw 5
#define ki_linefw 0.05
#define kd_linefw 0.15
#define kp_return 3.50
#define ki_return 0.0003
#define kd_return 0.05
#define BUZZER 6
int i = 0; //For recording the direction of route.
//If i = 0 then it's a forward route, if i = 1. then it's a reverse route.
float start_time = 0; //It's used to keep track of how much time has passed since the Romi started.
unsigned long startTime;
unsigned long prev_update;
unsigned long prev_move;
unsigned long timeStamp;
long count_prev_left;
long count_prev_right;
float homeTheta;
int LeftSensorRead, CentreSensorRead, RightSensorRead; //define sensor readings
int state;
bool setup_dist;
bool setup_check;
motor_c M; //Control the condition of the left and right wheels.
PID_c pid_returnd( kp_return, ki_return, kd_return );
PID_c pid_linefw( kp_linefw, ki_linefw, kd_linefw );
lineSensor_c line_left(LINE_LEFT_PIN); //Create a line sensor object for the left sensor
lineSensor_c line_centre(LINE_CENTRE_PIN); //Create a line sensor object for the centre sensor
lineSensor_c line_right(LINE_RIGHT_PIN); //Create a line sensor object for the right sensor
Kinematics_c kinematics;
void setup()
{
// Initialise your other globals variables
// and devices.
setupEncoder0();
setupEncoder1();
line_left.calibrate();
line_centre.calibrate();
line_right.calibrate();
state = 0; // Remember the Romi's current state.
setup_dist = false;
setup_check = false;
startTime = millis();
prev_update = millis();
prev_move = millis();
timeStamp = micros();
count_prev_left = 0;
count_prev_right = 0;
LeftSensorRead = 0;
CentreSensorRead = 0;
RightSensorRead = 0;
// Initialise the Serial communication
Serial.begin(9600);
delay(1000);
Serial.println("***RESET***");
}
// over set
void loop(){
unsigned long current_time = millis();
unsigned long update_time = current_time - prev_update;
unsigned long move_time = current_time - prev_move;
if (update_time > 2) {
prev_update = current_time;
kinematics.update(count_left, count_right); // call an update to your kinematics at a time interval
}
if (move_time > 5) {
prev_move = current_time;
Serial.println(state);
if (state == 0){
find_line();
}
else if (state == 1){
BangBang();
}
else if (state == 2){
line_follow();
}
else if (state ==3){
find_home();
}
else if (state ==4){
return_home();
}
else if (state == 5){
M.leftWheel(0.0);
M.rightWheel(0.0);
}
}
}
// over loop
//Go straight until find the line.
void find_line() {
bool onLine = CheckForLine();
if (!onLine) {
float theta_error = kinematics.getTheta();
int turn_pwm = 0;
if (theta_error < 0){
turn_pwm = -2;
}
else if (theta_error > 0) {
turn_pwm = 2;
}
else turn_pwm = 0;
int left_demand = 50 - turn_pwm;
int right_demand = 50 + turn_pwm;
M.leftWheel(left_demand);
M.rightWheel(right_demand);
}
else {
M.leftWheel(0.0);
M.rightWheel(0.0);
//Set state to follow line.
state = 1;
}
}
//Determine if Romi is online.
bool CheckForLine() {
bool onLine = false;
LeftSensorRead = line_left.readCalibrated();
CentreSensorRead = line_centre.readCalibrated();
RightSensorRead = line_right.readCalibrated();
if ( LeftSensorRead > 300 || CentreSensorRead > 300 || RightSensorRead > 300 ) {
onLine = true;
}
return onLine;
}
void BangBang() {
LeftSensorRead = line_left.readCalibrated();
CentreSensorRead = line_centre.readCalibrated();
RightSensorRead = line_right.readCalibrated();
bool left_on_line = false;
bool centre_on_line = false;
bool right_on_line = false;
if (LeftSensorRead > 90) left_on_line = true;
if (CentreSensorRead > 110) centre_on_line = true;
if (RightSensorRead > 90) right_on_line = true;
if (centre_on_line) {
M.leftWheel(37.0);
M.rightWheel(36.0);
}
else if (left_on_line) {
M.rightWheel(36.0);
M.leftWheel(-37.0);
}
else if (right_on_line) {
M.leftWheel(37.0);
M.rightWheel(-36.0);
}
else {
M.leftWheel(0.0);
M.rightWheel(0.0);
//Try to rejoin line
state = 2;
}
}
//Try to find line if lost
bool line_follow() {
bool FoundLine = false;
unsigned long current_time = millis();
if (!setup_check) {
startTime = millis();
setup_check = true;
}
unsigned long elapsedTime = current_time - startTime;
if (elapsedTime < 1150) {
M.leftWheel(33.0);
M.rightWheel(-32.0);
FoundLine = CheckForLine();
}
else if (elapsedTime < 3680) {
M.leftWheel(-33.0);
M.rightWheel(32.0);
FoundLine = CheckForLine();
}
else if (elapsedTime < 5000){
M.leftWheel(33.0);
M.rightWheel(-32.0);
FoundLine = CheckForLine();
}
else if (elapsedTime < 5550){
M.leftWheel(33.0);
M.rightWheel(32.0);
FoundLine = CheckForLine();
}
else if((current_time - start_time) < 16000){
i ++;
if(elapsedTime < 8150){
M.leftWheel(33.0);
M.rightWheel(-32.0);
FoundLine = CheckForLine();
}else if (elapsedTime < 8550){
M.leftWheel(33.0);
M.rightWheel(32.0);
FoundLine = CheckForLine();
}}
else {
M.leftWheel(0.0);
M.rightWheel(0.0);
analogWrite(BUZZER, 10);
delay(500);
analogWrite(BUZZER, 0);
state = 3;
}
if (FoundLine) {
setup_check = false;
state = 1;
}
}
//Make sure the Romi is pointing to the starting position
void find_home() {
if(i == 0){
float angle = kinematics.homeAngle();
turn_angle_right(angle);
} else if (i > 0){
float angle = kinematics.homeAnglez();
turn_angle_left(angle);
}
}
void return_home() {
if (!setup_dist) {
homeTheta = kinematics.getTheta();
setup_dist = true;
}
float theta_error = homeTheta - kinematics.getTheta();
int turn_pwm = 0;
if (theta_error > 0){
turn_pwm = -2;
}
else if (theta_error < 0) {
turn_pwm = 2;
}
else turn_pwm = 0;
int left_demand = 50 - turn_pwm;
int right_demand = 50 + turn_pwm;
M.leftWheel(left_demand);
M.rightWheel(right_demand);
if (abs(kinematics.getx_pos()) < 10) {
setup_dist = false;
state = 5;
}
}
//
void turn_angle_right(float angle) {
float new_count_left, new_count_right;
if (!setup_dist) {
float count = (((angle / 360.0) * (140.0 * PI)) / (70.0 * PI)) * 1440.0;
new_count_left = count_left + count;
new_count_right = count_right - count;
setup_dist = true;
}
float power = 75.0;
if (count_left < new_count_left) {
M.leftWheel(power + 1.0);
}
else M.leftWheel(0.0);
if (count_right > new_count_right) {
M.rightWheel(-power);
}
else M.rightWheel(0.0);
if (count_left > new_count_left && count_right < new_count_right) {
setup_dist = false;
//Set state to drive towards home.
state = 4;
}
}
void turn_angle_left(float angle) {
float new_count_left, new_count_right;
if (!setup_dist) {
float count = (((angle / 360.0) * (140.0 * PI)) / (70.0 * PI)) * 1440.0;
new_count_left = count_left - count;
new_count_right = count_right + count;
setup_dist = true;
}
float power = 75.0;
if (count_left < new_count_left) {
M.leftWheel(power + 1.0);
}
else M.leftWheel(0.0);
if (count_right > new_count_right) {
M.rightWheel(-power);
}
else M.leftWheel(0.0);
if (count_left > new_count_left && count_right < new_count_right) {
setup_dist = false;
state = 4;
}
}