-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcar.cpp
More file actions
201 lines (183 loc) · 6.07 KB
/
car.cpp
File metadata and controls
201 lines (183 loc) · 6.07 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
#include "mbed.h"
// Define Pins on Nucleo Board.
DigitalOut ultrasonic_sensor_trigger(D9);
DigitalIn ultrasonic_sensor_echo(D10);
Serial bluetooth(D8, D2);
DigitalOut driver1_in1(D3);
DigitalOut driver1_in2(D4);
DigitalOut driver1_in3(D5);
DigitalOut driver1_in4(D6);
DigitalOut driver2_in1(D7);
DigitalOut driver2_in2(D11);
DigitalOut driver2_in3(D12);
DigitalOut driver2_in4(D13);
// Constraints.
const int OBSTACLE_THRESHOLD = 40;
// Global Varaibles.
bool obstacle_detected = false;
char movement_command = 'S';
// Function Prototypes.
void bluetooth_communication();
void detect_obstacle();
void movement_control();
void send_alert();
int main() {
// Initialize Baud Rate.
bluetooth.baud(9600);
// Wait for everything to be Initialized.
wait_us(100000);
// Run indefinately.
while (1) {
bluetooth_communication();
detect_obstacle();
movement_control();
send_alert();
wait_us(100000);
}
}
void bluetooth_communication() {
// Check if any data has been received over Bluetooth.
if (bluetooth.readable()) {
char received = bluetooth.getc(); // Read a single character from Bluetooth.
// Update the movement command only if it's a valid command
if (received == 'F' || received == 'B' || received == 'L' ||
received == 'R' || received == 'S') {
movement_command = received; // Update the movement command
}
}
}
void detect_obstacle() {
// Ensure the trigger pin is initialized (set to output).
ultrasonic_sensor_trigger = 0; // Set trigger to low initially.
// Send pulse to ultrasonic sensor to trigger measurement.
ultrasonic_sensor_trigger = 1; // Send a 10us pulse to trigger the sensor.
wait_us(10); // Pulse duration should be 10 microseconds.
ultrasonic_sensor_trigger = 0; // Set trigger low again.
// Wait for the echo pin to go high.
Timer timer;
timer.start(); // Start the timer to measure the echo duration.
// 100ms timeout to prevent infinite loop if no echo.
while (!ultrasonic_sensor_echo) {
if (timer.read_us() > 100000) {
// No response = no obstacle detected within timeout period.
obstacle_detected = false;
printf("No echo detected (timeout).\n");
return;
}
}
// Measure the duration of the echo pulse.
timer.reset(); // Reset the timer to start measuring the pulse duration.
while (ultrasonic_sensor_echo) {
// Check if the echo pulse exceeds 380ms (maximum range).
if (timer.read_us() > 380000) {
obstacle_detected = false;
printf("Echo pulse too long (out of range).\n");
return;
}
}
// Calculate the pulse duration (time it took for the echo to return).
float pulse_duration = timer.read_us(); // Duration in microseconds.
float distance = pulse_duration * 0.034 / 2; // Distance in centimeters.
// Print distance for debugging.
printf("\033[2J\033[H"); // Clear screen and reset cursor position.
printf("Distance: %f cm\n", distance);
// Set obstacle detection flag based on distance threshold.
obstacle_detected = (distance < OBSTACLE_THRESHOLD);
if (obstacle_detected) {
printf("Obstacle detected!\n");
} else {
printf("No obstacle detected.\n");
}
}
void movement_control() {
// Stop the car if object detected.
if (obstacle_detected && movement_command != 'B') {
movement_command = 'S';
driver1_in1 = 1;
driver1_in2 = 1;
driver1_in3 = 1;
driver1_in4 = 1;
driver2_in1 = 1;
driver2_in2 = 1;
driver2_in3 = 1;
driver2_in4 = 1;
} else {
// Move Car Forwards.
if (movement_command == 'F') {
driver1_in1 = 1;
driver1_in2 = 0;
driver1_in3 = 1;
driver1_in4 = 0;
driver2_in1 = 0;
driver2_in2 = 1;
driver2_in3 = 0;
driver2_in4 = 1;
// Move Car Backwards.
} else if (movement_command == 'B') {
driver1_in1 = 0;
driver1_in2 = 1;
driver1_in3 = 0;
driver1_in4 = 1;
driver2_in1 = 1;
driver2_in2 = 0;
driver2_in3 = 1;
driver2_in4 = 0;
// Move Car Left.
} else if (movement_command == 'R') {
driver1_in1 = 1;
driver1_in2 = 0;
driver1_in3 = 1;
driver1_in4 = 0;
driver2_in1 = 1;
driver2_in2 = 0;
driver2_in3 = 1;
driver2_in4 = 0;
// Move Car Right.
} else if (movement_command == 'L') {
driver1_in1 = 0;
driver1_in2 = 1;
driver1_in3 = 0;
driver1_in4 = 1;
driver2_in1 = 0;
driver2_in2 = 1;
driver2_in3 = 0;
driver2_in4 = 1;
// Stop Car.
} else if (movement_command == 'S') {
driver1_in1 = 1;
driver1_in2 = 1;
driver1_in3 = 1;
driver1_in4 = 1;
driver2_in1 = 1;
driver2_in2 = 1;
driver2_in3 = 1;
driver2_in4 = 1;
}
}
}
void send_alert() {
// Send Alert Message for Movement Command.
switch(movement_command) {
case 'F':
bluetooth.printf("Moving Forwards.\n");
break;
case 'B':
bluetooth.printf("Moving Backwards.\n");
break;
case 'L':
bluetooth.printf("Moving Left.\n");
break;
case 'R':
bluetooth.printf("Moving Right.\n");
break;
case 'S':
bluetooth.printf("Not Moving.\n");
break;
default:
bluetooth.printf("Invalid Input, Try Again.\n");
}
// Send Alert Message for Obstacle Detected.
if (obstacle_detected) {
bluetooth.printf("Alert: Obstacle detected! Movement halted.\n");
}
}