-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrobot.cc
314 lines (251 loc) · 6.68 KB
/
robot.cc
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
#include <WProgram.h>
#include "util.h"
#include "robot.h"
int i;
int speed;
int turn = 0;
int back = 0;
// Battery level
int bat;
// Current wheel speeds
int speed_l, speed_r;
// Sensor readings
int sharp_front, sharp_left, sharp_right;
// Last dominant reading
Side sharp_last = NONE;
// Robot state
State state = SEARCH;
// Timer keeping track of how long we've been in LOST state
unsigned long lost_time;
/**
* Set wheel speeds
*/
void setSpeed(int left, int right) {
digitalWrite(LEFT_DIR, (left < 0) ? BACKWARD : FORWARD);
digitalWrite(RIGHT_DIR, (right < 0) ? BACKWARD : FORWARD);
analogWrite(LEFT_PWM, (unsigned) abs(left));
analogWrite(RIGHT_PWM, (unsigned) abs(right));
}
/**
* Set LED color
*/
void setColor(int r, int g, int b) {
analogWrite(LED_RED_PIN, r);
analogWrite(LED_GREEN_PIN, g);
analogWrite(LED_BLUE_PIN, b);
}
/**
* Serial debug info
*/
void debug()
{
unsigned long t;
switch(state) {
case SEARCH:
Serial.print("SEARCH");
break;
case FOLLOW:
Serial.print("FOLLOW");
break;
case FOLLOW_L:
Serial.print("FOLLOW_L");
break;
case FOLLOW_R:
Serial.print("FOLLOW_R");
break;
case LOST:
Serial.print("LOST");
t = millis();
Serial.print(" (");
Serial.print(t - lost_time);
Serial.print("ms)");
break;
case LOST_L:
Serial.print("LOST_L");
break;
case LOST_R:
Serial.print("LOST_R");
break;
}
Serial.print("\tSens:");
Serial.print("\tL: ");
Serial.print(sharp_left);
Serial.print("\tF: ");
Serial.print(sharp_front);
Serial.print("\tR: ");
Serial.print(sharp_right);
Serial.print("\tSpeed:");
Serial.print("\tL: ");
Serial.print(speed_l);
Serial.print("\tR: ");
Serial.print(speed_r);
t = (100 * (bat - BAT_LOW)) / (BAT_FULL - BAT_LOW);
Serial.print("\tBat: ");
Serial.print(t);
Serial.print("%");
Serial.println();
}
/**
* Check voltage of battery
*/
void battery_check() {
bat = analogRead(BAT_PIN);
if (bat <= BAT_LOW) {
// Low battery, stop motors and signal with LED
setSpeed(0, 0);
while (1) {
Serial.println("LOW BAT!");
setColor(255, 0, 0);
delay(500);
setColor(0, 0, 0);
delay(500);
}
}
}
/**
* Search mode
*/
void search() {
/*if (speed_l == 0 && speed_r == 0) {
speed_l = SPEED_L;
speed_r = SPEED_R;
}
speed_l += random(-5, 6);
speed_r += random(-5, 6);
speed_l = constrain(speed_l, -SPEED_L, SPEED_L);
speed_r = constrain(speed_r, -SPEED_R, SPEED_R);
Serial.print(speed_l);
Serial.print(' ');
Serial.print(speed_r);
Serial.println();
setSpeed(speed_l, speed_r);*/
setColor(255, 255, 0);
// Look for any objects
if (sharp_front > SHARP_THRESH || sharp_left > SHARP_THRESH || sharp_right > SHARP_THRESH) {
state = FOLLOW;
} else {
// No objects found, continue search
speed_l = SPEED_L;
speed_r = SPEED_R;
}
}
/**
* In the follow wall state
*/
void follow() {
if (sharp_front > SHARP_THRESH) {
// Object detected in front
setColor(255, 255, 255);
speed = map(sharp_front, SHARP_THRESH, SHARP_MAX, 150, 500);
if (sharp_right > sharp_left) {
// turn left
speed_l = SPEED_L - speed;
speed_r = SPEED_R;
} else {
// turn right
speed_l = SPEED_L;
speed_r = SPEED_R - speed;
}
sharp_last = FRONT;
} else if (sharp_right > SHARP_THRESH || sharp_left > SHARP_THRESH) {
if (sharp_right > sharp_left) {
// Object detected to the right
setColor(0, 255, 0);
if (sharp_right < SHARP_FOLLOW) {
// Turn towards object
speed_l = SPEED_L;
speed_r = map(sharp_right, SHARP_THRESH, SHARP_FOLLOW, 100, SPEED_R);
} else {
// Turn away from object
speed_l = map(sharp_right, SHARP_FOLLOW, SHARP_MAX, SPEED_L, 100);
speed_r = SPEED_R;
}
sharp_last = RIGHT;
} else {
// Object detected to the left
setColor(255, 0, 0);
if (sharp_left < SHARP_FOLLOW) {
// Turn towards object
speed_l = map(sharp_left, SHARP_THRESH, SHARP_FOLLOW, 100, SPEED_L);
speed_r = SPEED_R;
} else {
// Turn away from object
speed_l = SPEED_L;
speed_r = map(sharp_left, SHARP_FOLLOW, SHARP_MAX, SPEED_R, 100);
}
sharp_last = LEFT;
}
} else {
state = LOST;
lost_time = millis();
}
}
/**
* Object lost
*/
void lost()
{
unsigned long t;
if (sharp_front > SHARP_THRESH || sharp_left > SHARP_THRESH || sharp_right > SHARP_THRESH) {
state = FOLLOW;
return;
}
// Check for timeout
t = millis();
if (t - lost_time > LOST_TIMEOUT) {
state = SEARCH;
return;
}
if (sharp_last == RIGHT) {
// Last seen to the right => turn right
setColor(0, 255, 255);
speed_l = SPEED_L;
speed_r = 60;
} else if (sharp_last == LEFT) {
// Last seen to the left => turn left
setColor(255, 0, 255);
speed_l = 60;
speed_r = SPEED_R;
} else {
// Start searching...
state = SEARCH;
}
}
void setup() {
Serial.begin(9600);
pinMode(RIGHT_PWM, OUTPUT); //Set control pins to be outputs
pinMode(RIGHT_DIR, OUTPUT);
pinMode(LEFT_PWM, OUTPUT);
pinMode(LEFT_DIR, OUTPUT);
pinMode(LED_RED_PIN, OUTPUT);
pinMode(LED_GREEN_PIN, OUTPUT);
pinMode(LED_BLUE_PIN, OUTPUT);
// Set speeds
speed_l = 0;
speed_r = 0;
setSpeed(0, 0);
// Initialize state
state = SEARCH;
}
void loop() {
// Check battery
battery_check();
// Read sensors
sharp_front = analogReadMedian(SHARP_FRONT_PIN, SHARP_SAMPLES);
sharp_left = analogReadMedian(SHARP_LEFT_PIN, SHARP_SAMPLES);
sharp_right = analogReadMedian(SHARP_RIGHT_PIN, SHARP_SAMPLES);
switch (state) {
case FOLLOW:
follow();
break;
case LOST:
lost();
break;
default:
search();
break;
}
setSpeed(speed_l, speed_r);
debug();
delay(10);
}