-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
167 lines (148 loc) · 3.98 KB
/
main.cpp
File metadata and controls
167 lines (148 loc) · 3.98 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
#include "mbed.h"
#include "ISM43362Interface.h"
#include "HCSR04.h"
#include "VL53L0X.h"
#include <cstdint>
#include <cstdio>
static DevI2C devI2c(PB_11,PB_10);
static DigitalOut shutdown_pin(PC_6);
static VL53L0X laser(&devI2c, &shutdown_pin, PC_7);
ISM43362Interface wifi(false);
InterruptIn button(BUTTON1);
// DigitalOut socket_led(LED1);
DigitalOut car_led(LED2);
DigitalOut obs_led(LED3);
Timer sonic_timer;
HCSR04 sonic(D2, D4); // (echo, trigger)
EventQueue queue(32*EVENTS_EVENT_SIZE);
Thread button_thread;
TCPSocket socket;
SocketAddress addr("192.168.0.15",9000);
nsapi_error_t response;
bool obsExistence, carExistence;
float obsDistance;
bool pause = 0;
void sensor_init() {
laser.init_sensor(VL53L0X_DEFAULT_ADDRESS);
sonic.setRanges(2, 70);
printf("Obstacle detecting range: %g ~ %g cm\n\n", sonic.getMinRange(), sonic.getMaxRange());
}
void get_car_existence() {
uint32_t dist1, dist2, difference;
laser.get_distance(&dist1);
wait_us(25000);
laser.get_distance(&dist2);
if (dist1 > dist2) {
difference = dist1 - dist2;
}
else {
difference = dist2 - dist1;
}
if (difference>=100) {
carExistence = true;
car_led = 1;
}
else {
carExistence = false;
car_led = 0;
}
}
void get_obs_existence() {
if (obsDistance>=450) {
obsExistence = false;
obs_led = 0;
}
else {
obsExistence = true;
obs_led = 1;
}
}
void button_pressed()
{
pause = !pause;
// carExistence = !carExistence;
// car_led = !car_led;
// obs_led = !obs_led;
// obsExistence = !obsExistence;
// printf("\nObstacle: %d\n", obsExistence);
}
void get_obs_distance() {
sonic_timer.reset();
sonic_timer.start();
sonic.startMeasurement();
while(!sonic.isNewDataReady()) {
// wait for new data
}
obsDistance = sonic.getDistance_mm();
sonic_timer.stop();
// wait_us(500000 - sonic_timer.elapsed_time().count());
}
void wifi_connect() {
printf("Connecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
if (ret != 0) {
printf("Connection error\n");
}
printf("Success\n\n");
printf("MAC: %s\n", wifi.get_mac_address());
printf("IP: %s\n", wifi.get_ip_address());
printf("Netmask: %s\n", wifi.get_netmask());
printf("Gateway: %s\n", wifi.get_gateway());
printf("RSSI: %d\n\n", wifi.get_rssi());
}
int socket_test(NetworkInterface *net) {
response = socket.open(net);
if (0 != response){
printf("Error opening: %d\n", response);
return 0;
}
response = socket.connect(addr);
if (0 != response){
printf("Error connecting: %d\n", response);
return 0;
}
return 1;
}
void send_road_state(NetworkInterface *net) {
// socket_led = 1;
char obs_info[64];
get_obs_distance();
get_obs_existence();
int len = sprintf(obs_info,
"{\"car\":%d,\"obs\":%d,\"dist\":%3.1f}",
carExistence, obsExistence, obsDistance);
response = socket.send(obs_info,len);
if (response > 0) {
printf("sent %s\n",obs_info);
}
else {
printf("Error sending: %d\n", response);
}
// socket_led = 0;
// wait_us(2000000);
}
int main()
{
wifi_connect();
bool isServerAvailable = socket_test(&wifi);
if (!isServerAvailable)
return -1;
button_thread.start(callback(&queue,&EventQueue::dispatch_forever));
button.fall(queue.event(&button_pressed));
printf("\nSensor init...\n");
sensor_init();
bool lastCarExistence = carExistence;
send_road_state(&wifi);
while (1) {
if (!pause){
get_car_existence();
if (lastCarExistence != carExistence) {
send_road_state(&wifi);
}
if (carExistence) {
ThisThread::sleep_for(5s);
}
lastCarExistence = carExistence;
}
}
}