-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_move_with_obstacle_avoidance.ino
479 lines (396 loc) · 13 KB
/
new_move_with_obstacle_avoidance.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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include <Wire.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#include "I2Cdev.h"
#include "MPU6050.h"
// Create servo objects
Servo Servo1;
Servo Servo2;
// Declare the Servo pins
int servoPin1 = 9;
int ServoPin2 = 10;
//mpu interrupt pin = 2
MPU6050 mpu;
// Declare Sr04 pins
int echoPin1 = 12;
int trigPin1 = A2;
int echoPin2 = 11;
int trigPin2 = 13;
// Declare Motor Pins
const int RightMotorIN3 = 5; // This pin is used to enable or disable the Right motor. Connected to the base of an NPN transistor.
const int RightMotorIN4 = 3; // This pin is used to enable or disable the Right motor. Connected to the base of an NPN transistor.
const int LeftMotorIN1 = 7; // This pin is used to enable or disable the Left motor. Connected to the base of an NPN transistor.
const int LeftMotorIN2 = 6; // This pin is used to enable or disable the Left motor. Connected to the base of an NPN transistor.
// Declare photoresistor pins
const int FrontSensor = A0;
const int BackSensor = A3 ;
// Variable definitions
int degreeMax = -1;
bool max_in_front;
int maxLX;
int distance;
long duration;
int front_distance;
int back_distance;
// Variable for mpu
int16_t ax, ay, az;
int16_t gx, gy, gz;
float x=0;
int cx=0;
float bx=0;
float rx=0;
float y=0;
int cy=0;
float by=0;
float ry=0;
float z=0;
int cz=0;
float bz=0;
float rz=0;
//Variable use for rotation of robot
int robot_init_degree = 0;
int robot_current_degree = 0;
// Water pomp
int Pomp = 8 ;
// Moisture sensor
int Moisture_ain=A1;
int ad_value;
uint16_t measureMoistureTime = 0 ;
// variables for bluetooth and moisture value
SoftwareSerial BTSerial(10, 11); // RX | TX
int min_Moisture;
char bt_val;
void move_right() {
digitalWrite(RightMotorIN3, LOW);
digitalWrite(RightMotorIN4, HIGH);
digitalWrite(LeftMotorIN1, HIGH);
digitalWrite(LeftMotorIN2, LOW);
}
void move_left() {
digitalWrite(RightMotorIN3, HIGH);
digitalWrite(RightMotorIN4, LOW);
digitalWrite(LeftMotorIN1, LOW);
digitalWrite(LeftMotorIN2, HIGH);
}
void move_forward() {
digitalWrite(RightMotorIN3, HIGH);
digitalWrite(RightMotorIN4, LOW);
digitalWrite(LeftMotorIN1, HIGH);
digitalWrite(LeftMotorIN2, LOW);
}
void move_backward() {
digitalWrite(RightMotorIN3, LOW);
digitalWrite(RightMotorIN4, HIGH);
digitalWrite(LeftMotorIN1, LOW);
digitalWrite(LeftMotorIN2, HIGH);
}
void stop_robot() {
digitalWrite(RightMotorIN3, HIGH);
digitalWrite(RightMotorIN4, HIGH);
digitalWrite(LeftMotorIN1, HIGH);
digitalWrite(LeftMotorIN2, HIGH);
}
void get_min_moisture_from_user(){
while(true){
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (BTSerial.available()){
bt_val = BTSerial.read();
Serial.println(bt_val);
if(bt_val == '1' || bt_val == '2' || bt_val == '3'){
if(bt_val == '1')
min_Moisture = 550;
else if(bt_val == '2')
min_Moisture = 600;
else
min_Moisture = 650;
Serial.println(min_Moisture);
BTSerial.end();
break;
}
else
BTSerial.write("Invalid Arguement, please try again");
}
// Keep reading from Arduino Serial Monitor and send to HC-05
if (Serial.available())
BTSerial.write(Serial.read());
}
return;
}
int calc_light_max_degree(int luxes_and_degrees[14][3]){
Serial.println(" Calc_degree called");
Servo1.write(0);
Servo2.write(0);
maxLX = 0 ;
degreeMax = -1;
for(int j = 0; j <= 6; j++){
Servo1.write(j*30);
Servo2.write(j*30);
delay(100);
uint16_t luxb = analogRead(BackSensor);
delay(100);
uint16_t luxf = analogRead(FrontSensor);
back_distance = 2001 ;
int count = 0 ;
while ( back_distance > 2000 && count < 11 ) {
back_distance = calc_distance(2);
count++;
}
if ( back_distance > 60){
luxes_and_degrees[j+7][0] = luxb;
if (luxes_and_degrees[j+7][1] == 2) {
(luxes_and_degrees[j+7][1] = 2) ;
}
else if ( luxes_and_degrees[j+7][1] != 2 )
{
(luxes_and_degrees[j+7][1] = 1);
}
luxes_and_degrees[j+7][2] = back_distance;
}
else if(back_distance <= 60 && back_distance >=30){
luxes_and_degrees[j+7][0] = luxb;
luxes_and_degrees[j+7][1] = 0;
luxes_and_degrees[j+7][2] = back_distance;
}
else if(back_distance < 30){
if(j+7-1 >= 7){
luxes_and_degrees[j+7][1] = 0;
luxes_and_degrees[j+7-1][1] = 0;
luxes_and_degrees[j+7][2] = back_distance;
}
if(j+7+1 <= 13){
luxes_and_degrees[j+7][1] = 0;
luxes_and_degrees[j+7][2] = back_distance;
luxes_and_degrees[j+7+1][1] = 2;
}
}
front_distance = 2001 ;
count = 0;
while ( front_distance > 2000 && count < 11 )
{
front_distance = calc_distance(1);
count++;
}
if ( front_distance > 60 ){
luxes_and_degrees[j][0] = luxf;
if (luxes_and_degrees[j][1] == 2) {
(luxes_and_degrees[j][1] = 2);
}
else if (luxes_and_degrees[j][1] != 2) {
(luxes_and_degrees[j][1] = 1);
}
luxes_and_degrees[j][2] = front_distance;
}
else if(front_distance <= 60 && front_distance >= 25){
luxes_and_degrees[j][0] = luxf;
luxes_and_degrees[j][1] = 0;
luxes_and_degrees[j][2] = front_distance;
}
else if(front_distance < 25){
if(j-1 >= 0){
luxes_and_degrees[j][1] = 0;
luxes_and_degrees[j-1][1] = 0;
luxes_and_degrees[j][2] = front_distance;
}
if(j+1 <= 6){
luxes_and_degrees[j][1] = 0;
luxes_and_degrees[j][2] = front_distance;
luxes_and_degrees[j+1][1] = 2;
Serial.print(" Changing "); Serial.print(j+1); Serial.print("to 2");
}
}
delay(1000);
}
maxLX = 0;
for(int cnt = 0; cnt <= 13; cnt++){
if ( cnt <= 6 )
{
Serial.println(cnt*30);
}
Serial.print("luxes_and_degrees[cnt][0] " );
Serial.println(luxes_and_degrees[cnt][0]);
Serial.print("luxes_and_degrees[cnt][1] " );
Serial.println(luxes_and_degrees[cnt][1]);
Serial.print("luxes_and_degrees[cnt][2] " );
Serial.println(luxes_and_degrees[cnt][2]);
if( (luxes_and_degrees[cnt][1] == 1) && (luxes_and_degrees[cnt][0] > maxLX)){
maxLX = luxes_and_degrees[cnt][0];
if ( cnt <= 6 ){
degreeMax = cnt * 30;
max_in_front = true;
}
else if ( cnt >= 7 ){
degreeMax = (cnt - 7) * 30;
max_in_front = false;
}
}
}
return degreeMax;
}
void init_mpu()
{
if(millis()<2000){
delay(2000);
for(int i=0; i<1000; i++) {
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
bx=bx+gx;
by=by+gy;
bz=bz+gz;
}
bx=bx/10000;
by=by/10000;
bz=bz/10000;
}
}
int get_wheels_degree()
{
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
x=x+gx-bx;
y=y+gy-by;
z=z+gz-bz;
cz=map(z, -750000, 750000, -90, 90)*2;
return cz;
}
int calc_distance(int mode)
{
// Clears the trigPin
if ( mode == 1)
digitalWrite(trigPin1, LOW);
else if ( mode == 2 )
digitalWrite(trigPin2, LOW);
delayMicroseconds(20);
// Sets the trigPin on HIGH state for 10 micro seconds
if ( mode == 1)
digitalWrite(trigPin1, HIGH);
else if ( mode == 2 )
digitalWrite(trigPin2, HIGH);
delayMicroseconds(100);
if ( mode == 1)
digitalWrite(trigPin1, LOW);
else if ( mode == 2 )
digitalWrite(trigPin2, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
if ( mode == 1)
duration = pulseIn(echoPin1, HIGH);
else if ( mode == 2 )
duration = pulseIn(echoPin2, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
return distance;
}
void setup() {
pinMode(Moisture_ain,INPUT);
pinMode(Pomp,OUTPUT);
pinMode(LeftMotorIN1, OUTPUT); // Defines this pin as an output. The Arduino will write values to this pin.
pinMode(LeftMotorIN2, OUTPUT); // Defines this pin as an output. The Arduino will write values to this pin.
pinMode(RightMotorIN3, OUTPUT); // Defines this pin as an output. The Arduino will write values to this pin.
pinMode(RightMotorIN4, OUTPUT); // Defines this pin as an output. The Arduino will write values to this pin.
// SR05 setup
pinMode(trigPin1, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin1, INPUT); // Sets the echoPin as an Input
pinMode(trigPin2, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin2, INPUT); // Sets the echoPin as an Input
// We need to attach the servo to the used pin number
Servo1.attach(servoPin1);
Servo2.attach(ServoPin2);
Serial.begin(9600);
Serial.println("Start Bluetooth");
BTSerial.begin(9600);
BTSerial.write("Enter minimum moisture value : ");
BTSerial.write("**Enter 1 for low moisture \n");
BTSerial.write("**Enter 2 for medium moisture \n");
BTSerial.write("**Enter 3 for high moisture \n");
Serial.println("Initialize MPU");
get_min_moisture_from_user();
Serial.println("value successfully received");
Serial.println(min_Moisture);
mpu.initialize();
Serial.println(mpu.testConnection() ? "Connected" : "Connection failed");
Serial.println(" \nBeginning Light Seeking Behavior"); // Placed at the very end of void Setup() so that it is runs once, right before the void Loop()
Wire.begin();
}
void loop() {
int luxes_and_degrees[14][3];
init_mpu();
if(measureMoistureTime % 150 == 0 ) {
ad_value=analogRead(Moisture_ain);
Serial.print(ad_value);
Serial.print (" moisture sensor value received" );
if(ad_value < min_Moisture){
digitalWrite(Pomp,HIGH);
Serial.println("Thirsty");
while(ad_value < min_Moisture){
ad_value=analogRead(Moisture_ain);
delay(100);
}
digitalWrite(Pomp,LOW);
}
delay(100);
}
degreeMax = calc_light_max_degree(luxes_and_degrees);
if (degreeMax == 0 || degreeMax == 30 || degreeMax == 60 ) {
robot_init_degree = get_wheels_degree();
Serial.println(robot_init_degree);
while( abs(robot_current_degree - robot_init_degree) < (100 - degreeMax) ){
Serial.print("init : ");
Serial.print(robot_init_degree);
Serial.println("current : " );
Serial.println(robot_current_degree);
move_left();
robot_current_degree = get_wheels_degree();
}
stop_robot();
delay(500);
if(max_in_front) {
move_forward();
Serial.println("degreeMax == 0 || degreeMax == 30 || degreeMax == 60 front") ;
}
else{
move_backward();
Serial.println("degreeMax == 0 || degreeMax == 30 || degreeMax == 60 back") ;
}
delay(2000);
stop_robot();
}
else if (degreeMax == 90 ) {
if(max_in_front){
Serial.println("degreeMax 90 front") ;
move_forward();
delay(2000);
stop_robot();
}
else{
Serial.println("degreeMax 90 back") ;
move_backward();
delay(2000);
stop_robot();
}
}
else if (degreeMax == 120 || degreeMax == 150 || degreeMax == 180 ){
robot_init_degree = get_wheels_degree();
while( abs(robot_current_degree - robot_init_degree) < ( degreeMax - 85 ) ){
Serial.print("init : ");
Serial.print(robot_init_degree);
Serial.println("current : " );
Serial.println(robot_current_degree);
move_right();
robot_current_degree = get_wheels_degree();
}
stop_robot();
delay(500);
if(max_in_front) {
move_forward();
Serial.println("degreeMax == 0 || degreeMax == 30 || degreeMax == 60 front") ;
}
else{
move_backward();
Serial.println("degreeMax == 0 || degreeMax == 30 || degreeMax == 60 back") ;
}
delay(2000);
stop_robot();
}
delay ( 1000);
measureMoistureTime++;
}