-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparent.ino
More file actions
184 lines (159 loc) · 3.8 KB
/
parent.ino
File metadata and controls
184 lines (159 loc) · 3.8 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
// Ultrasonic sensor
const int trigPin = D1;
const int echoPin = D8;
// Motor
#define enA D2
#define inA1 D3
#define inA2 D4
#define enB D7
#define inB1 D5
#define inB2 D6
float speedKM;
int value;
long duration;
int motorSpeed = 0; // Current motor speed
int targetSpeed = 0; // Target motor speed
// For detect break or accident
#define MAX_SIZE 3
int distance_queue[MAX_SIZE];
bool accidentHappened = false;
void setup()
{
Serial.begin(115200);
// Motor
value = 255;
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(enA, OUTPUT); // Set motor control pins as outputs
pinMode(inA1, OUTPUT);
pinMode(inA2, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(inB1, OUTPUT);
pinMode(inB2, OUTPUT);
analogWrite(enA, 0); // Initialize motor speed to 0
analogWrite(enB, 0);
// Ultrasonic
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
int locationRequest = 0;
void loop()
{
locationRequest++;
if (locationRequest == 20000)
{
Serial.println("request_send_location");
locationRequest = 0;
}
// Use Ultrasonic sensor to calculate the distance
int distance = getDistance();
enqueue(distance);
Serial.println(distance);
// Motor
// Adjust motor speed based on distance
if (distance < 6)
{
crash();
}
else if (distance < 10)
{
if (value > 122)
{
for (value = value; value >= 0; value--)
{
Serial.println("request_status_break");
moveMotorsForward(value);
delay(1);
}
Serial.println("request_status_stopped");
}
else
{
moveMotorsForward(value); // Keep the current value
}
}
else if (distance < 20)
{
Serial.println("request_status_break");
if (value > 220)
{
for (value = value; value >= 193; value--)
{
moveMotorsForward(value);
delay(10);
}
}
else
{
moveMotorsForward(value); // Keep the current value
}
}
else if (distance < 30)
{
Serial.println("request_status_break");
if (value == 255)
{
for (value = 255; value >= 225; value--)
{
moveMotorsForward(value);
delay(10);
}
}
else
{
moveMotorsForward(value); // Keep the current value
}
}
else
{
value = 255;
moveMotorsForward(value);
Serial.println("request_status_driving");
}
}
void moveMotorsForward(int speed)
{
digitalWrite(inA1, LOW);
digitalWrite(inA2, HIGH);
digitalWrite(inB1, HIGH);
digitalWrite(inB2, LOW);
analogWrite(enA, speed); // Set motor speed
analogWrite(enB, speed);
speedKM = (speed - 120) * (50 - 0) / (255 - 120);
Serial.print(speedKM);
Serial.println(" KM/h");
}
void stopMotors()
{
digitalWrite(inA1, LOW);
digitalWrite(inA2, LOW);
digitalWrite(inB1, LOW);
digitalWrite(inB2, LOW);
analogWrite(enA, 0); // Set motor speed to 0
analogWrite(enB, 0);
}
void crash()
{
accidentHappened = true;
Serial.println("request_status_accident");
Serial.println("request_accident");
}
int getDistance()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2;
return distance;
}
void enqueue(int element)
{
for (int i = MAX_SIZE - 1; i > 0; i--)
{
distance_queue[i] = distance_queue[i - 1];
}
distance_queue[0] = element;
}