-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLine_tracking_car_bl_AGV_GREEN.ino
314 lines (275 loc) · 6.51 KB
/
Line_tracking_car_bl_AGV_GREEN.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
//www.elegoo.com
//Line Tracking IO define
#define PIN_R 10
#define PIN_M 4
#define PIN_L 2
#define LT_R !digitalRead(PIN_R)
#define LT_M !digitalRead(PIN_M)
#define LT_L !digitalRead(PIN_L)
#define ENA 5
#define ENB 6
#define IN1 7
#define IN2 8
#define IN3 9
#define IN4 11
#define LED 13
#define fwdSpeed 150
#define turnSpeed 170
// Constants
#define moveRobot true
#define debug false // ONLY when not working with AGV
// Bluetooth commands
#define AGV2_AT_P20 0x34
#define AGV2_AT_P21 0x35
#define MOVE_COMMAND_AGV2 0x36
#define AGV2_MOVING 0x37
// Booleans to activate when AGV sensors detect stop
bool AGV2atP20 = false;
bool AGV2atP21 = false;
// Boolean to activate when AGV receives "move" command from RASPI
bool AGV2moving = false;
// Int to store last position
int lastPos = 1;
// uint32 for resending moving data
uint32_t tTimeout= 0;
uint32_t maxTimeoutMs = 200;
// unit32 for not checking stop after a stop
uint32_t tTimestop= 0;
uint32_t timestopMs = 500;
int vl;
int vr;
void forward(int lspeed, int rspeed){
if(moveRobot){
analogWrite(ENA, lspeed);
analogWrite(ENB, rspeed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
if(debug){Serial.println("go forward!");}
}
void lForward(int mspeed){
if(moveRobot){
analogWrite(ENA, mspeed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
}
if(debug){Serial.println("L go forward!");}
}
void rForward(int mspeed){
if(moveRobot){
analogWrite(ENB, mspeed);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
if(debug){Serial.println("R go forward!");}
}
void back(int mspeed){
if(moveRobot){
analogWrite(ENA, mspeed);
analogWrite(ENB, mspeed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
if(debug){Serial.println("go back!");}
}
void lBack(int mspeed){
if(moveRobot){
analogWrite(ENA, mspeed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
}
if(debug){Serial.println("L go back!");}
}
void rBack(int mspeed){
if(moveRobot){
analogWrite(ENB, mspeed);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
if(debug){Serial.println("R go back!");}
}
void left(int mspeed){
if(moveRobot){
analogWrite(ENA, mspeed);
analogWrite(ENB, mspeed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
if(debug){Serial.println("go left!");}
}
void right(int mspeed){
if(moveRobot){
analogWrite(ENA, mspeed);
analogWrite(ENB, mspeed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
if(debug){Serial.println("go right!");}
}
void stop(){
digitalWrite(ENA, LOW);
digitalWrite(ENB, LOW);
if(debug){Serial.println("Stop!");}
}
void Lmove(int mspeed){
if(mspeed < 0){
lBack(mspeed);
} else {
lForward(mspeed);
}
}
void Rmove(int mspeed){
if(mspeed < 0){
rBack(-mspeed);
} else {
rForward(mspeed);
}
}
void setup(){
// Set initial speed
vl = fwdSpeed;
vr = fwdSpeed;
// We begin in the road 1->0
lastPos = 1;
// Initialise timeouts
tTimeout = millis();
tTimestop = millis();
// Initialise other stuff
Serial.begin(9600);
pinMode(PIN_R,INPUT);
pinMode(PIN_M,INPUT);
pinMode(PIN_L,INPUT);
pinMode(LED,OUTPUT);
}
void waitForMoveCommand(int refresh_ms, uint32_t timeout_ms) {
bool received_move_command = false;
int received_command = 0x00;
uint32_t tStart = millis();
if (debug) {Serial.println("Waiting for command...");}
// 0 - Wait until we receive a command to move
while (not(received_move_command))
{
// 1 - Check for messages
if (Serial.available())
{
if (debug) {Serial.println("Command received!");}
// Make sure we are reading the newest command
while(Serial.available())
{
received_command = Serial.read();
}
// Check that the command is correct, exit while loop
if (received_command == MOVE_COMMAND_AGV2)
{
if (debug) {Serial.println("command = MOVE");}
received_move_command = true;
}
}
// 2 - If there are no messages and dt > timeout, resend our position
else
{
if (millis() - tStart > timeout_ms)
{
if (debug) {Serial.println("Timeout: no command received. Resending info...");}
// Resend position
if (AGV2atP20)
{
Serial.write(AGV2_AT_P20);
}
else if (AGV2atP21)
{
Serial.write(AGV2_AT_P21);
}
else
{
if (debug) {Serial.println("Error: AGV is waiting but neither in P20 nor in P21");}
}
// Reset timeout
tStart = millis();
}
}
// 3 - Wait and retry
delay(refresh_ms);
}
// 4 - We have received the command
return;
}
void loop() {
if(LT_L && LT_R && LT_M){ // detecting stop -> stop
// only stop if there has passed enough time after a stop
if (millis() - tTimestop > timestopMs)
{
stop();
if (lastPos==1)
{
AGV2atP20 = true;
AGV2atP21 = false;
AGV2moving = false;
Serial.write(AGV2_AT_P20);
waitForMoveCommand(200, 100);
lastPos = 0;
}
else
{
AGV2atP20 = false;
AGV2atP21 = true;
AGV2moving = false;
Serial.write(AGV2_AT_P21);
waitForMoveCommand(200, 100);
lastPos = 1;
}
forward(fwdSpeed,fwdSpeed);
// reset tTimestop
tTimestop = millis();
}
}
else
{ // no stop -> move
AGV2atP20 = false;
AGV2atP21 = false;
AGV2moving = true;
// Send message every maxTimeoutMs
if (millis() - tTimeout > maxTimeoutMs)
{
Serial.write(AGV2_MOVING);
tTimeout = millis();
}
if(LT_L){ // detecting left -> go left
vr = turnSpeed;
vl = -turnSpeed;
} else if (LT_R) { // detecting right -> go right
vr = -turnSpeed;
vl = turnSpeed;
} else if(LT_M){ // detecting middle line -> go forward
vr = fwdSpeed;
vl = fwdSpeed;
} else { // not detecting anything
vl = 0;
vr = 0;
}
Lmove(vl);
Rmove(vr);
//forward(vl,vr);
}
if(debug){
Serial.print("M \t");
Serial.print(LT_M);
Serial.print("\t R \t");
Serial.print(LT_R);
Serial.print("\t L \t");
Serial.print(LT_L);
Serial.print("\t vl \t");
Serial.print(vl);
Serial.print("\t vr \t");
Serial.print(vr);
Serial.print("\n");
}
}