-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsketch_phase3_accurate.ino
313 lines (269 loc) · 6.51 KB
/
sketch_phase3_accurate.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
// Setup Motor A (front and rear) pins
#define PIN_R1 5
#define PIN_R2 6
#define PIN_R_enable 3
// Setup Motor B (front and rear) pins
#define PIN_L1 9
#define PIN_L2 10
#define PIN_L_enable 11
// Setup Bluetooth
#define TX 1
#define RX 0
unsigned long timer0 = 2000; //Stores the time (in millis since execution started)
unsigned long timer1 = 0; //Stores the time when the last command was received from the phone
// Define Attributes
#define NO_COMMAND 'X'
#define RESET 'C'
#define FORWARD 'F'
#define FORWARD_RIGHT 'I'
#define RIGHT 'R'
#define BACKWORD_RIGHT 'J'
#define BACKWORD 'B'
#define BACKWORD_LEFT 'H'
#define LEFT 'L'
#define FORWARD_LEFT 'G'
#define BREAK ' '
#define FOLLOWLINE 'W'
#define STOPFOLLOWING 'w'
#define STOP 'S'
#define GEAR_1 '0'
#define GEAR_2 '3'
#define GEAR_3 '5'
#define GEAR_4 '8'
#define GEAR_5 'q'
char prevCommand = NO_COMMAND;
char command = RESET;
int speed = 0;
char state = STOP;
void setup() {
// The setup code goes here and runs once only
Serial.begin(9600);
// Configure the pin modes for each drive motor
pinMode (PIN_R1, OUTPUT);
pinMode (PIN_R2, OUTPUT);
pinMode (PIN_L1, OUTPUT);
pinMode (PIN_L2, OUTPUT);
pinMode (PIN_R_enable, OUTPUT);
pinMode (PIN_L_enable, OUTPUT);
digitalWrite (PIN_L_enable, HIGH);
digitalWrite (PIN_R_enable, HIGH);
pinMode (r_sensor, INPUT);
pinMode (m_sensor, INPUT);
pinMode (l_sensor, INPUT);
}
void loop() {
// Main code goes here and will run repeatedly:
//all distamce in mm
//in a rectangle
move_forward(2000);
rotate_right(90); //degrees
move_forward(2000);
rotate_right(90);
move_forward(2000);
rotate_right(90);
move_forward(2000);
rotate_right(90);
//in a circle
move_inacircle(1000,1); //radius & n of turns
//infinity
move_forward(1000);
rotate_right(90);
move_inacircle(1000,0.25);
rotate_right(90);
move_forward(2000);
rotate_left(90);
move_inacircle(1000,0.25);
rotate_left(90);
move_forward(1000);
if (Serial.available()) {
delay(10); //Delay added to make thing stable
timer1 = millis();
command = Serial.read();
Serial.println(command);
//Change pin mode only if new command is different from previous.
if (command != prevCommand) {
setState(command);
}
}
else {
timer0 = millis(); //Get the current time (millis since execution started).
//Check if it has been 60000ms since we received last command.
if ((timer0 - timer1) > 60000) {
//More tan 60000ms have passed since last command received, car is out of range.
//Therefore stop the car.
breakRobot(1);
coast(0);
}
}
}
void setSpeed(char gear) {
if (gear == GEAR_1) {
speed = 100;
} else if (gear == GEAR_2) {
speed = 140;
} else if (gear == GEAR_3) {
speed = 180;
} else if (gear == GEAR_4) {
speed = 220;
} else if (gear == GEAR_5) {
speed = 255;
}
}
void setState(char command) {
switch (command) {
case RESET:
breakRobot(1);
coast(0);
speed = 0;
state = STOP;
break;
case BREAK:
state = BREAK;
breakRobot(1);
break;
case STOP:
state = STOP;
coast(0);
break;
case GEAR_1:
setSpeed(GEAR_1);
setState(state);
break;
case GEAR_2:
setSpeed(GEAR_2);
setState(state);
break;
case GEAR_3:
setSpeed(GEAR_3);
setState(state);
break;
case GEAR_4:
setSpeed(GEAR_4);
setState(state);
break;
case GEAR_5:
setSpeed(GEAR_5);
setState(state);
break;
case FORWARD:
state = FORWARD;
forward(1, speed);
break;
case FORWARD_RIGHT:
state = FORWARD_RIGHT;
right(1, 0.5 * speed, speed);
break;
case RIGHT:
state = RIGHT;
right(1, speed, speed);
break;
case BACKWORD_RIGHT:
state = BACKWORD_RIGHT;
left(1, 0.5 * speed, speed);
break;
case BACKWORD:
state = BACKWORD;
backward(1, speed);
break;
case BACKWORD_LEFT:
state = BACKWORD_LEFT;
right(1, speed, 0.5 * speed);
break;
case LEFT:
state = LEFT;
left(1, speed, speed);
break;
case FORWARD_LEFT:
state = FORWARD_LEFT;
left(1, speed, 0.5 * speed);
}
prevCommand = command;
}
// Create motor functions
void motorAforward(int speed) {
analogWrite (PIN_R1, speed);
digitalWrite (PIN_R2, LOW);
}
void motorBforward(int speed) {
digitalWrite (PIN_L1, LOW);
analogWrite (PIN_L2, speed);
}
void motorAbackward(int speed) {
digitalWrite (PIN_R1, LOW);
analogWrite (PIN_R2, speed);
}
void motorBbackward(int speed) {
analogWrite (PIN_L1, speed);
digitalWrite (PIN_L2, LOW);
}
void motorAstop() {
digitalWrite (PIN_R1, HIGH);
digitalWrite (PIN_R2, HIGH);
}
void motorBstop() {
digitalWrite (PIN_L1, HIGH);
digitalWrite (PIN_L2, HIGH);
}
void motorAcoast() {
digitalWrite (PIN_R1, LOW);
digitalWrite (PIN_R2, LOW);
}
void motorBcoast() {
digitalWrite (PIN_L1, LOW);
digitalWrite (PIN_L2, LOW);
}
// Setup movement functions
void forward (int duration, int speed) {
motorAforward(speed);
motorBforward(speed);
delay (duration);
}
void backward (int duration, int speed) {
motorAbackward(speed);
motorBbackward(speed);
delay (duration);
}
void left (int duration, int speedA, int speedB) {
motorAbackward(speedA);
motorBforward(speedB);
delay (duration);
}
void right (int duration, int speedA, int speedB) {
motorAforward(speedA);
motorBbackward(speedB);
delay (duration);
}
void coast (int duration) {
motorAcoast();
motorBcoast();
delay (duration);
}
void breakRobot (int duration) {
motorAstop();
motorBstop();
delay (duration);
}
void move_inacircle(int radius_mm,float nofturns){ //radius should be in mm
int d_btw_wheels= ; //distance between the right & the left wheels in mm.
int w_wheel= ; //width of the wheel in mm.
int mm_ber_sec =1050 ;
int distanceA = radius_mm ; //should be 2*pi*radius but we only need the ratio so it's ok
int distanceB = (radius_mm - d_btw_wheels - w_wheel) ;
float t = (1000*nofturns*(distanceA / mm_ber_sec)) ;
right(t,255,(255*(distanceA/distanceB)));
}
void move_forward (int distance_mm){
int mm_ber_sec =1050 ;
float t= 1000*(distance_mm / mm_ber_sec) ;
forward(t,255) ;
}
void rotate_left (int degree){
int deg_ber_sec = ;
float t= 1000*(degree / deg_ber_sec);
left(t,255,255)
}
void rotate_right (int degree){
int deg_ber_sec = ;
float t= 1000*(degree / deg_ber_sec);
right(t,255,255)
}