-
Notifications
You must be signed in to change notification settings - Fork 0
/
MotorShieldCar.ino
270 lines (238 loc) · 5.98 KB
/
MotorShieldCar.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
#include <MotorDriver.h>
#include <Eventually.h>
#include <Servo.h>
MotorDriver m;
EvtManager mgr;
Servo servo1;
const int trigPin = A1;
const int echoPin = A0;
#define START_STOP_PIN A5
//const int START_STOP_PIN = A5;
const int speedLimit = 150;
const int turnSpeed = 100;
const int safeDistance = 40;
const int leftAngle = 10;
const int neutralAngle = 90;
const int rightAngle = 170;
const int directionLeft = -1;
const int directionNeutral = 0;
const int directionBack = 10;
const int directionRight = 1;
long duration;
int distance;
bool goingForwards = false;
bool goingBackwards = false;
bool turning = false;
bool shouldMove = false;
void setup()
{
Serial.begin(115200);
Serial.println("Serial at 115200 ");
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(START_STOP_PIN, INPUT_PULLUP);
servo1.attach(10);
lookLeft();
delay(1200);
lookRight();
delay(1200);
lookFront();
mgr.addListener(new EvtPinListener(START_STOP_PIN, 40, LOW, (EvtAction)switchDrivingState));
// mgr.addListener(new EvtPinListener(startButtonPin, (EvtAction)switchDrivingState));
mgr.addListener(new EvtTimeListener(100, true, (EvtAction)doKIstuff));
mgr.addListener(new EvtTimeListener(1000, true, (EvtAction)debugPrint));
}
bool switchDrivingState() {
shouldMove = !shouldMove;
Serial.print("Is driving: ");
Serial.println(shouldMove ? "YES" : "NO");
}
USE_EVENTUALLY_LOOP(mgr) // Use this instead of your loop() function.
boolean debugPrint() {
Serial.print("Distance: ");
Serial.println(distance, DEC);
// Serial.print("Turning: ");
// Serial.println(turning ? "YES" : "NO");
Serial.print("Is driving: ");
Serial.println(shouldMove ? "YES" : "NO");
// Serial.print("goingForwards: ");
// Serial.println(goingForwards ? "YES" : "NO");
}
bool measure() {
distance = getDistance();
return true;
}
int getDistance() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
return distance;
}
int getFreeDirection() {
lookFront();
delay(5000);
lookLeft();
delay(5000);
int leftDistance = getDistance();
Serial.println("leftDistance: " + leftDistance);
lookFront();
delay(5000);
lookRight();
delay(2000);
int rightDistance = getDistance();
Serial.println("rightDistance: " + rightDistance);
lookFront();
delay(2000);
if (rightDistance < 80 && leftDistance > 80 ) {
Serial.println("go right");
return directionRight;
} else if (rightDistance > 80 && leftDistance < 80 ) {
Serial.println("go left");
return directionLeft;
} else {
Serial.println("go right");
return directionBack;
}
}
bool release() {
if (turning || goingForwards) {
Serial.println("Stopping Car");
m.motor(1, RELEASE, 0);
m.motor(2, RELEASE, 0);
m.motor(3, RELEASE, 0);
m.motor(4, RELEASE, 0);
goingForwards = false;
turning = false;
} else {
Serial.print(".");
}
return true;
}
void lookRight() {
servo1.write(rightAngle);
// Serial.println("Looking right");
}
void lookLeft() {
servo1.write(leftAngle);
// Serial.println("Looking left");
}
void lookFront() {
servo1.write(neutralAngle);
// Serial.println("Looking front");
}
bool backwards() {
if (!goingBackwards && shouldMove) {
Serial.println("Starting going backwards");
goingBackwards = true;
goingForwards = false;
turning = false;
for (int i = 10; i <= speedLimit; i = i + 5) {
m.motor(1, BACKWARD, i);
m.motor(2, BACKWARD, i);
m.motor(3, BACKWARD, i);
m.motor(4, BACKWARD, i);
delay(5);
}
delay(3000);
}
return true;
}
boolean forwards() {
if (goingForwards && shouldMove) {
// keep goingForwards
} else {
Serial.println("Starting going forwards");
goingForwards = true;
turning = false;
for (int i = 10; i <= speedLimit; i = i + 10) {
m.motor(1, FORWARD, i);
m.motor(2, FORWARD, i);
m.motor(3, FORWARD, i);
m.motor(4, FORWARD, i);
delay(10);
}
}
return true;
}
boolean left() {
if (!turning && shouldMove) {
Serial.println("Starting left turn");
if (goingForwards) {
release();
}
turning = true;
for (int i = 10; i <= turnSpeed; i = i + 10) {
m.motor(1, FORWARD, i);
m.motor(3, FORWARD, i);
m.motor(2, BACKWARD, i);
m.motor(4, BACKWARD, i);
}
}
return true;
}
boolean right() {
if (!turning && shouldMove) {
Serial.println("Starting right turn");
if (goingForwards) {
release();
}
turning = true;
for (int i = 10; i <= turnSpeed; i = i + 10) {
m.motor(1, BACKWARD, i);
m.motor(3, BACKWARD, i);
m.motor(2, FORWARD, i);
m.motor(4, FORWARD, i);
}
}
return true;
}
// the loop
bool doKIstuff() {
measure();
reactToDistance();
return true;
}
int chooseDirection() {
int direction = getFreeDirection();
if (direction == directionLeft) {
left();
} else if (direction == directionRight) {
right();
} else if (direction == directionBack) {
backwards();
}
return direction;
}
bool reactToDistance()
{
if (distance < 5 ) {
// error reading of "0" or emergency stop
release();
} else if (distance > safeDistance && !goingForwards) {
Serial.println("Start goingForwards");
forwards();
} else if (distance > safeDistance && goingForwards) {
// keep goingForwards
} else if (distance <= safeDistance) {
if (turning) {
// keep turning
} else { //
Serial.println("Choosing direction");
if (chooseDirection() == directionBack); {
chooseDirection(); // after driving back choose again
}
}
} else {
release(); // should not happen
}
delay(1000);
return true;
}