-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelfDriveScript.m
367 lines (263 loc) · 7.73 KB
/
SelfDriveScript.m
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
%initialize sensors
brick.SetColorMode(3, 2);
brick.GyroCalibrate(2);
distance = brick.UltrasonicDist(1);
%%%%%%%%%%%%%%%%%%%
%%% MAIN SCRIPT %%%
%%%%%%%%%%%%%%%%%%%
% PHASE 1: search for blue, then switch to remote
autoDrive(brick, 2);
stopProgram = remoteControl(brick);
% PHASE 2: search for yellow, then switch to remote
if (~stopProgram)
autoDrive(brick, 4);
stopProgram = remoteControl(brick);
end
% PHASE 3: search for green, then end script
if (~stopProgram)
autoDrive(brick, 3);
end
brick.StopMotor('AB', 'Coast');
%%%%%%%%%%%%%%%%%%%%%%
%%% MAIN FUNCTIONS %%%
%%%%%%%%%%%%%%%%%%%%%%
% Function for Autonomous Driving
function autoDrive(brick, colorToLookFor)
% set variables
forwardSpeed = -50;
turnSpeed = 60;
forkliftSpeed = -10;
timeSinceLook = 0;
% bool vals to decide what to do
solvingMaze = true;
manualMode = false;
turning = false;
checkingRightWall = false;
brick.MoveMotor('AB', forwardSpeed);
while (solvingMaze)
% update distance
distance = brick.UltrasonicDist(1);
timeSinceLook = timeSinceLook + 1;
[solvingMaze, manualMode] = checkForColors(brick, colorToLookFor);
% check for opening to the right every 5 seconds
if (mod(timeSinceLook, 5) == 0)
checkingRightWall = true;
brick.MoveMotorAngleRel('D', 50, 90, 'Brake');
[solvingMaze, manualMode] = checkForColors(brick, colorToLookFor);
pause(1);
distance = brick.UltrasonicDist(1);
[solvingMaze, manualMode] = checkForColors(brick, colorToLookFor);
% if open path to the right, turn right
if (distance > 45)
brick.StopAllMotors('Brake');
brick.GyroCalibrate(2);
[solvingMaze, manualMode] = checkForColors(brick, colorToLookFor);
pause(1.5);
turnRight(brick);
end
% reset dist sensor, start driving again
brick.MoveMotorAngleRel('D', 50, -90, 'Brake');
[solvingMaze, manualMode] = checkForColors(brick, colorToLookFor);
pause(1);
distance = brick.UltrasonicDist(1);
checkingRightWall = false;
brick.MoveMotor('AB', forwardSpeed);
end
% if wall is close, look for the next turn option
if (distance < 25 && ~checkingRightWall)
brick.StopAllMotors('Brake');
brick.GyroCalibrate(2);
brick.MoveMotorAngleRel('D', 50, 90, 'Brake');
pause(2);
distance = brick.UltrasonicDist(1);
% if wall to right, check other options
if (distance < 40)
brick.MoveMotorAngleRel('D', 50, -180, 'Brake');
pause(2);
distance = brick.UltrasonicDist(1);
pause(0.2);
% if wall to left, turn around
if (distance < 40)
brick.MoveMotorAngleRel('D', 50, 90, 'Brake');
pause(2);
distance = brick.UltrasonicDist(1);
turnAround(brick);
pause(1);
brick.MoveMotor('AB', forwardSpeed);
% if no wall to left, turn left
else
brick.MoveMotorAngleRel('D', 50, 90, 'Brake');
pause(2);
turnLeft(brick);
pause(1);
brick.MoveMotor('AB', forwardSpeed);
end
% if no wall to right, turn right
else
brick.MoveMotorAngleRel('D', 50, -90, 'Brake');
pause(2);
distance = brick.UltrasonicDist(1);
turnRight(brick);
pause(1);
brick.MoveMotor('AB', forwardSpeed);
end
end
pause(0.2);
end
end
% Function for Remote Control Driving
function stopProgram = remoteControl(brick)
% initialize keyboard
global key
InitKeyboard();
% set variables
forwardSpeed = -50;
turnSpeed = 60;
forkliftSpeed = -10;
timeSinceLook = 0;
% bool vals to decide what to do
solvingMaze = false;
manualMode = true;
turning = false;
checkingRightWall = false;
while(manualMode)
pause(0.1);
% check keys for press
switch key
% w moves forward
case 'w'
brick.MoveMotor('AB', forwardSpeed);
% d moves right
case 'd'
brick.MoveMotor('A', -turnSpeed);
brick.MoveMotor('B', turnSpeed);
% a moves left
case 'a'
brick.MoveMotor('A', turnSpeed);
brick.MoveMotor('B', -turnSpeed);
% s moves backwards
case 's'
brick.MoveMotor('AB', -forwardSpeed);
% e lifts forklift
case 'e'
brick.MoveMotor('C', forkliftSpeed);
% q lowers forklift
case 'q'
brick.MoveMotor('C', -forkliftSpeed);
% p ends manual mode
case 'x'
manualMode = false;
stopProgram = true;
break;
case 'c'
stopProgram = false;
manualMode = false;
break;
case 'rightarrow'
brick.MoveMotor('D', 10);
case 'leftarrow'
brick.MoveMotor('D', -10);
% dont move if nothing is pressed
case 0
brick.StopAllMotors('Brake');
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%
%%% OTHER FUNCTIONS %%%
%%%%%%%%%%%%%%%%%%%%%%%
%%% checks for colors: Red-pause, input one other color to stop for %%%
function [solvingMaze, manualMode] = checkForColors(brick, secondColor)
forwardSpeed = -50;
% pause at color red
if (brick.ColorCode(3) == 5)
brick.StopMotor('AB', 'Brake');
pause(2);
brick.MoveMotor('AB', forwardSpeed);
pause(1);
solvingMaze = true;
manualMode = false;
% manual mode at color blue
elseif (brick.ColorCode(3) == 2 && secondColor == 2)
brick.StopAllMotors('Brake');
brick.beep();
pause(0.5);
brick.beep();
solvingMaze = false;
manualMode = true;
elseif (brick.ColorCode(3) == 3 && secondColor == 3)
brick.StopAllMotors('Brake');
brick.beep();
pause(0.5);
brick.beep();
pause(0.5);
brick.beep();
solvingMaze = false;
manualMode = true;
elseif (brick.ColorCode(3) == 4 && secondColor == 4)
brick.StopAllMotors('Brake');
brick.beep();
pause(0.5);
brick.beep();
pause(0.5);
brick.beep();
pause(0.5);
brick.beep();
solvingMaze = false;
manualMode = true;
else
solvingMaze = true;
manualMode = false;
end
end
%%% Turn Right 90 degrees %%%
function turnRight(brick)
turnSpeed = 60;
turning = true;
brick.MoveMotor('A', -turnSpeed);
brick.MoveMotor('B', turnSpeed);
% loop until not turning
while (turning)
% if 90 degrees from start
if (brick.GyroAngle(2) > 90)
% change to forward drive
brick.StopMotor('AB', 'Brake');
turning = false;
%brick.MoveMotor('AB', forwardSpeed);
end
end
end
%%% Turn left 90 degrees %%%
function turnLeft(brick)
turnSpeed = 60;
turning = true;
brick.MoveMotor('A', turnSpeed);
brick.MoveMotor('B', -turnSpeed);
% loop until not turning
while (turning)
% if 90 degrees from start
if (brick.GyroAngle(2) < -90)
% change to forward drive
brick.StopMotor('AB', 'Brake');
turning = false;
%brick.MoveMotor('AB', forwardSpeed);
end
end
end
%%% turn 180 degrees %%%
function turnAround(brick)
turnSpeed = 60;
turning = true;
brick.MoveMotor('A', -turnSpeed);
brick.MoveMotor('B', turnSpeed);
% loop until not turning
while (turning)
% if 90 degrees from start
if (brick.GyroAngle(2) > 180)
% change to forward drive
brick.StopMotor('AB', 'Brake');
turning = false;
%brick.MoveMotor('AB', forwardSpeed);
end
end
end