-
Notifications
You must be signed in to change notification settings - Fork 0
/
self_driving_VF2.ino
242 lines (207 loc) · 6.13 KB
/
self_driving_VF2.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
/*
The Project name:Jarvis
Self driving REV 2.0 alpha basic structure left turn and right turn and moving forward and stopping
5/2/19
-Reverse has been added
-more knobs have been added such as full speed and half speed and closing distance.
- restructured switch statements
- Drive loagic has been revidsed and updated
trigger pins = pin 5
echo pins = 4 for front
7 for right
8 for left
12 for rear
servo for steering is set to pin 11
*/
#include <Servo.h>
Servo steering;
// tuning knobs
const int Center_angle = 95; // center steering angle in degrees threshold
const int Left_angle = 155; //to turn left angle in degrees threshold
const int Right_angle = 50; //to turn right angle in degrees threshold
const int closing_distance = 110; // closing distance threshhold
const int half_speed = 50; // 50% duty cycle
const int full_speed = 100; // 100% duty cycle
const int stopped = 0; // shuts off electric motor
const int setDelay = 1;// delays
const int between_sensor_delay = 10; // delay for indidual sensor bring up
const int sensor_timer_on = 20; //ultra sonic sensor timer
const int sensor_timer_off = 10; //ultra sonic sensor timer
//pin alocation
const int velo = 3; //motor speed using duty cycle
const int dir = 2; // forward or reverse
const int trig = 5; // ultrasonic sensor trigger (pwm) front of car
const int front_echo = 4; // ultrasonic echo front of car
const int right_echo = 7; // ultrasonic echo front of car
const int left_echo = 8; // ultrasonic echo of car
const int rear_echo = 12; // ultrasonic echo rear of car
int front_distance_cm = 10; // front of car distance in cm readout
int right_distance_cm = 10; // right of car distance in cm readout
int left_distance_cm = 10; //left of car distance in cm readout
int rear_distance_cm = 10; // rear of car distance in cm readout
void setup()
{
//pin setup
Serial.begin(9600);
steering.attach(11);
pinMode(velo,OUTPUT);
pinMode(dir,OUTPUT);
pinMode(trig,OUTPUT);
pinMode(front_echo,INPUT);
pinMode(right_echo,INPUT);
pinMode(left_echo,INPUT);
pinMode(rear_echo,INPUT);
}
void loop()
{
//calling sensor setup functions
front_sensor();
delay(between_sensor_delay);
left_sensor();
delay(between_sensor_delay);
right_sensor();
delay(between_sensor_delay);
rear_sensor();
delay(between_sensor_delay);
// self driving logic
int mode = 5;
// Find which path is clearest
if (front_distance_cm > closing_distance && front_distance_cm >= right_distance_cm && front_distance_cm >= left_distance_cm && front_distance_cm >= rear_distance_cm)
{
mode = 1;
}
else if(left_distance_cm > closing_distance && left_distance_cm > front_distance_cm && left_distance_cm > right_distance_cm && left_distance_cm > rear_distance_cm)
{
mode = 2;
}
else if(right_distance_cm > closing_distance && right_distance_cm > front_distance_cm && right_distance_cm > left_distance_cm && right_distance_cm > rear_distance_cm)
{
mode = 3;
}
else if (rear_distance_cm > closing_distance && rear_distance_cm > right_distance_cm && rear_distance_cm > left_distance_cm)
{
mode = 4;
}
else if (rear_distance_cm < closing_distance && front_distance_cm < closing_distance && rear_distance_cm < closing_distance )
{
mode = 5;
}
switch (mode)
{
case 1:
//Straight
steering.write(Center_angle);
digitalWrite(dir,HIGH);
analogWrite(velo,full_speed);
break;
case 2:
//Right
steering.write(Right_angle);
_delay_ms(100);
digitalWrite(dir,HIGH);
analogWrite(velo,half_speed);
delay(setDelay);
break;
case 3:
//Left
steering.write(Left_angle);
_delay_ms(100);
digitalWrite(dir,HIGH);
analogWrite(velo,half_speed);
delay(setDelay);
break;
case 4:
//Middle
steering.write(Center_angle);
digitalWrite(dir,LOW);
analogWrite(velo,half_speed);
delay(setDelay);
break;
default:
//Stop
digitalWrite(dir,HIGH);
analogWrite(velo,stopped);
break;
}
} //end main
/*******************************************************/
void front_sensor() //front sensor setup
{
long duration; // ultrasonic value storage
digitalWrite(trig,LOW);
delay(sensor_timer_off);
digitalWrite(trig,HIGH);
delay(sensor_timer_on);
digitalWrite(trig,LOW);
pinMode(front_echo,INPUT);
duration = pulseIn(front_echo,HIGH);
//delay(sensor_timer_off);
front_distance_cm = (duration/2)/29.1;
/*
Serial.print("front sensor: "); //for sensor debug
Serial.print(front_distance_cm);
Serial.print("cm ");
Serial.println();
*/
}
/*******************************************************/
void right_sensor() //right sensor setup
{
long duration; // ultrasonic value storage
digitalWrite(trig,LOW);
delay(sensor_timer_off);
digitalWrite(trig,HIGH);
delay(sensor_timer_on);
digitalWrite(trig,LOW);
pinMode(right_echo,INPUT);
duration = pulseIn(right_echo,HIGH);
//delay(sensor_timer_off);
right_distance_cm = (duration/2)/29.1;
/*
Serial.print("right sensor: "); //for sensor debug
Serial.print(right_distance_cm);
Serial.print("cm ");
Serial.println();
*/
}
/*******************************************************/
void left_sensor() //left sensor setup
{
long duration; // ultrasonic value storage
digitalWrite(trig,LOW);
delay(sensor_timer_off);
digitalWrite(trig,HIGH);
delay(sensor_timer_on);
digitalWrite(trig,LOW);
pinMode(left_echo,INPUT);
duration = pulseIn(left_echo,HIGH);
//delay(sensor_timer_off);
left_distance_cm = (duration/2)/29.1;
/*
Serial.print("left sensor: "); //for sensor debug
Serial.print(left_distance_cm);
Serial.print("cm ");
Serial.println();
*/
}
/*******************************************************/
void rear_sensor() //rear sensor setup
{
long duration; // ultrasonic value storage
// front sonic_sensor test
digitalWrite(trig,LOW);
delay(sensor_timer_off);
digitalWrite(trig,HIGH);
delay(sensor_timer_on);
digitalWrite(trig,LOW);
pinMode(rear_echo,INPUT);
duration = pulseIn(rear_echo,HIGH);
//delay(sensor_timer_off);
rear_distance_cm = (duration/2)/29.1;
/*
Serial.print("rear sensor: "); //for sensor debug
Serial.print(rear_distance_cm);
Serial.print("cm ");
Serial.println();
*/
}