-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFINAL
More file actions
406 lines (328 loc) · 11.6 KB
/
FINAL
File metadata and controls
406 lines (328 loc) · 11.6 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include <WiFiNINA.h>
#include <PID_v1.h>
//Setting up the credentials for our Wireless Access Point
char ssid[]="GroupX1"; //Wifi SSID
char pass[]="Team2022"; //Wifi password
//Web server port number to 80
WiFiServer server(80);
int encoderA = 12;
int encoderB = 11;
int curr_stateA;
int prev_stateA;
int counterA = 0;
int curr_stateB;
int prev_stateB;
int counterB = 0;
double curr_time;
double prev_time;
int time_change;
double speed;
double obj_speed;
// setting up the pins for the motors
// motor A
// in1 and in2 are used to define direction of left motor
const int in1 = 19;
const int in2 = 18;
// enA is used for PWM to change the speed of left motor
const int enA = 2;
// motor B
// in3 and in4 are used to define direction of right motor
const int in3 = 5;
const int in4 = 4;
// enB is used for PWM to change speed of right motor
const int enB = 6;
// pins for IR sensors
// left IR sensor
const int LEYE = 17;
// right IR sensor
const int REYE = 14;
// pin ultrasonic sensor
// pin for trigger pin
const int triggerPin = 15 ;
// pin for echo pin
const int echoPin = 16;
// defining global variables to find the distance an object is at
float distance;
float duration;
// set up bool for the ultrasonic sensor. when true it means the previous distance was bigger than ten
bool not_detected = true;
//const double Kp = 16;
const double Kp = 16;
//const double Ki = 0.14;
const double Ki = 0.5;
const double Kd = 0;
double setPoint;
double Input;
double Output;
PID myPID(&Input, &Output, &setPoint, Kp, Ki, Kd, DIRECT);
void setup() {
Serial.begin(9600);
// motors
// motor A - setting pin mode to OUTPUT
pinMode(enA,OUTPUT);
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
// motor B - setting pin mode to OUTPUT
pinMode(enB,OUTPUT);
pinMode(in3,OUTPUT);
pinMode(in4,OUTPUT);
// ir sensor - setting pin mode to INPUT
pinMode(LEYE,INPUT);
pinMode(REYE,INPUT);
// trigger pin mode set to OUTPUT since it will receive instructions from Arduino
pinMode(triggerPin,OUTPUT);
// echo pin mode set to input since it gives Arduino its state (HIGH or LOW)
pinMode(echoPin, INPUT);
// setting up the wireless access point
WiFi.beginAP(ssid, pass);
// printing IP of arduino to add it to processing
IPAddress ip= WiFi.localIP();
Serial.print("IP Address:");
Serial.println(ip);
server.begin();
setPoint = 30;
myPID.SetOutputLimits(0,255);
myPID.SetMode(AUTOMATIC);
pinMode(encoderA, INPUT_PULLUP);
prev_stateA = digitalRead(encoderA);
Serial.println("first prev state ");
Serial.println(prev_stateA);
pinMode(encoderB, INPUT_PULLUP);
prev_stateB = digitalRead(encoderB);
Serial.println("first prev state ");
Serial.println(prev_stateB);
prev_time = millis();
}
void loop() {
float prev_distance = 11;
// look if incoming clients
WiFiClient client = server.available();
// it there is a client found
if (client.connected()) {
int c = client.read();
if (c == 1){
// while the bool is true we want the loop for buggy movement to repeat
bool turned_on = true;
while(turned_on) {
// trigger pin emits pulse of 10 microseconds
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
// listen to see if there is a pulse on the ECHO PIN detected (falling). this tells you how long it took the sound wave to reach
// the object and then be detected by the receiver on the sensor
duration = pulseIn(echoPin, HIGH);
// converting duration to distance. divide by 2 since the sound travelled to object then back
distance = (duration * 0.0343) /2;
//print out the distance reported...
//Serial.print("Distance: ");
//Serial.println(distance);
//calculating the distance...
if (distance > 30) {
if(digitalRead (REYE)==LOW && digitalRead (LEYE)==LOW){
// turn off left motor
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
// turn off right motor
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
// set speed to 0
analogWrite (enA, 0);
analogWrite (enB, 0);
}
else if(digitalRead(REYE) == HIGH && digitalRead(LEYE) == HIGH){
//turn motors on
// left motor on
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// right motor on
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
//setting motor velocity
// left motor speed
analogWrite (enA, 255);
// right motor speed
analogWrite (enB, 255);
}
else if(digitalRead(REYE) == LOW && digitalRead(LEYE) == HIGH){
// turn on right motor to turn left
// left motor on
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// left motor off
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
// set motor velocity
// left motor speed is set to 180, so not max speed
analogWrite (enA, 255);
// right motor speed is null
analogWrite (enB, 0);
}
else if(digitalRead(REYE) == HIGH && digitalRead(LEYE) == LOW){
// turn on left motor to turn right
// left motor off
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
// right motor on
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// left motor speed is null
analogWrite (enA, 0);
// right motor speed is set to 180, so not max speed
analogWrite (enB, 255);
}
}
if (distance < 30) {
Input = distance;
myPID.Compute();
// if both IR sensors detect a white line (they are low) then stop motors
if(digitalRead (REYE)==LOW && digitalRead (LEYE)==LOW){
// turn off left motor
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
// turn off right motor
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
// set speed to 0
analogWrite (enA, 0);
analogWrite (enB, 0);
}
// if the distance is less than or equal to 10 then we want buggy to stop
else if (distance <= 10){
// turn off motors if object detected
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
delay(100);
// we want to print "Stopped" only the first time the object is detected
while(not_detected){
client.print("Obstacle seen: ");
client.println(distance);
// set bool to false so that it doesnt print on next loop
not_detected = false;
}
}
// if the previous distance was less than or equal to 10 but current distance is more this means object is gone
// print that its removed
else if ((prev_distance <= 10) && (distance > 10)){
client.println("Obstacle removed");
// set bool to true so that we can print "stopped" next time an object is placed
not_detected = true;
}
// if no line detected, so sensors are HIGH, we want buggy to go straight so both motor are on
else if(digitalRead(REYE) == HIGH && digitalRead(LEYE) == HIGH){
//turn motors on
// left motor on
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// right motor on
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
//setting motor velocity
// left motor speed
analogWrite (enA, (255-Output));
// right motor speed
analogWrite (enB, (255-Output));
}
// if left sensor detects line, is LOW, but right sensor is HIGH then we want the buggy to turn left
else if(digitalRead(REYE) == HIGH && digitalRead(LEYE) == LOW){
// turn on left motor to turn right
// left motor off
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
// right motor on
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// left motor speed is null
analogWrite (enA, 0);
// right motor speed is set to 180, so not max speed
analogWrite (enB, (255-Output));
}
// if right sensor dtects line, is LOW, but left sensor is HIGH then we want buggy to turn right
else if(digitalRead(REYE) == LOW && digitalRead(LEYE) == HIGH){
// turn on right motor to turn left
// left motor on
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// left motor off
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
// set motor velocity
// left motor speed is set to 180, so not max speed
analogWrite (enA, (255-Output));
// right motor speed is null
analogWrite (enB, 0);
}
}
curr_time = millis();
curr_stateA = digitalRead(encoderA);
curr_stateB = digitalRead(encoderB);
if (digitalRead(REYE) == HIGH && digitalRead(LEYE) == HIGH) {
if (curr_stateA != prev_stateA) {
counterA++;
counterB++;
}
}
else if (digitalRead(REYE) == HIGH && digitalRead(LEYE) == LOW) {
if (curr_stateB != prev_stateB) {
counterB++;
}
}
else if (digitalRead(LEYE) == HIGH && digitalRead(REYE) == LOW){
if (curr_stateA != prev_stateA) {
counterA++;
}
}
Serial.print("Counter A ");
Serial.println(counterA);
Serial.print("Counter B ");
Serial.println(counterB);
if ((counterA == 480) || (counterB == 480)) {
time_change = curr_time - prev_time;
//client.print("Time change ");
//client.println(time_change);
speed = 6.5 * PI / (time_change / 1000);
client.print("Buggy Speed: ");
client.print(speed);
client.print("cm/s");
counterA = 0;
counterB = 0;
prev_time = curr_time;
// if the distance is 30 then this means same speed
if (distance == 30) {
obj_speed = speed;
}
// if the distance > 30 then obj in front faster than buggy
// if distance < 30 then obj in front slower than buggy
else {
obj_speed = speed + ((distance - 30) / (time_change / 1000) );
}
client.print(" Object speed: ");
client.print(obj_speed);
client.println("cm/s");
}
prev_stateA = curr_stateA;
prev_stateB = curr_stateB;
// check if processing client has sent in a new int
int c = client.read();
// if its received a 2, it means the OFF button has been pressed, turn off motors
if(c == 2){
turned_on = false;
Serial.println("Motor stopped");
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
// set counter to 0
counterA = 0;
counterB = 0;
prev_time = curr_time;
}
// update value of the distance for next loop to compare
prev_distance = distance;
}
}
}
}