From 2a80f8386f61c56fd2e87dc3b88c6add338582dc Mon Sep 17 00:00:00 2001 From: fabseman Date: Sat, 11 May 2024 17:56:51 +0200 Subject: [PATCH] Adjusted SQL-schema to store velocity as a floating point + calculates the velocity on the ESP32 given the system time delta --- include/global_def.h | 13 ++--- include/timer.h | 2 +- src/main.cpp | 24 +++------ src/pcnt.cpp | 51 +++++++++++++------ src/traffic_cycle.cpp | 3 +- src/wifi.cpp | 41 ++++++++++----- .../migrations/01_initial_migration.sql | 8 +-- web-server/src/main.rs | 4 +- 8 files changed, 88 insertions(+), 58 deletions(-) diff --git a/include/global_def.h b/include/global_def.h index 51332b1..8beb217 100644 --- a/include/global_def.h +++ b/include/global_def.h @@ -30,20 +30,21 @@ #define TICKS_WAIT 10 -extern QueueHandle_t xCarQueue, xTrafficLightQueue; -extern SemaphoreHandle_t xCarSemaphore, xTrafficLightSemaphore; - /***************************** Constants *******************************/ /***************************** Variables *******************************/ extern uint16_t car_counter; -extern uint8_t timer_change, traffic_state; +extern uint8_t timer_change, traffic_state, second; +extern float velocity; /***************************** Objects *******************************/ -extern PCNTModule pcnt0, pcnt1, pcnt2, pcnt3; -extern TrafficLights traffic_light0, traffic_light1, traffic_light2; +extern PCNTModule pcnt0, pcnt1, pcnt2, pcnt3; +extern TrafficLights traffic_light0, traffic_light1, traffic_light2; + +extern QueueHandle_t xCarQueue, xTrafficLightQueue; +extern SemaphoreHandle_t xCarSemaphore, xTrafficLightSemaphore; /***************************** Functions *******************************/ diff --git a/include/timer.h b/include/timer.h index c501434..7bc567f 100644 --- a/include/timer.h +++ b/include/timer.h @@ -32,7 +32,7 @@ /***************************** Functions *******************************/ -void IRAM_ATTR onTime(); +void IRAM_ATTR timer_interrupt(); /***************************************************************************** * Input : - * Output : - diff --git a/src/main.cpp b/src/main.cpp index 844f362..728daca 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -58,10 +58,8 @@ void setup() { setup_pcnt(); setup_traffic_lights(); connect_wifi("fabsewifi", "fabseman"); - // connect_eduroam("eduroam", "CHANGE_ME", - // "CHANGE_ME"); - - TaskHandle_t task_0, task_1, task_2, task_3, task_4, task_5; + // connect_eduroam("eduroam", "Phosphate6(Earthly(Footgear", + // "fapet22@student.sdu.dk"); xCarQueue = xQueueCreate(QUEUE_SIZE, sizeof(uint8_t)); xCarSemaphore = xSemaphoreCreateBinary(); @@ -69,21 +67,15 @@ void setup() { xTrafficLightQueue = xQueueCreate(QUEUE_SIZE, sizeof(uint8_t)); xTrafficLightSemaphore = xSemaphoreCreateBinary(); - xTaskCreate(pcnt0_task, "pcnt0_task", LARGE_STACK, &pcnt0, HIGH_PRIO, - &task_0); - xTaskCreate(pcnt1_task, "pcnt1_task", LARGE_STACK, &pcnt1, HIGH_PRIO, - &task_1); - xTaskCreate(pcnt2_task, "pcnt2_task", LARGE_STACK, &pcnt2, HIGH_PRIO, - &task_2); - xTaskCreate(pcnt3_task, "pcnt3_task", LARGE_STACK, &pcnt3, HIGH_PRIO, - &task_3); - xTaskCreate(api_task, "api_task", LARGE_STACK, NULL, MED_PRIO, &task_4); + xTaskCreate(pcnt0_task, "pcnt0_task", LARGE_STACK, &pcnt0, HIGH_PRIO, NULL); + xTaskCreate(pcnt1_task, "pcnt1_task", LARGE_STACK, &pcnt1, HIGH_PRIO, NULL); + xTaskCreate(pcnt2_task, "pcnt2_task", LARGE_STACK, &pcnt2, HIGH_PRIO, NULL); + xTaskCreate(pcnt3_task, "pcnt3_task", LARGE_STACK, &pcnt3, HIGH_PRIO, NULL); + xTaskCreate(api_task, "api_task", LARGE_STACK, NULL, MED_PRIO, NULL); // if not LOW_PRIO, the PCNT-counters doesn't work + no // api-transfer - // xTaskCreate(traffic_cycle, "cycle_task", LARGE_STACK, - // NULL, LOW_PRIO, - // &task_5); + xTaskCreate(traffic_cycle, "cycle_task", LARGE_STACK, NULL, LOW_PRIO, NULL); xSemaphoreGive(xCarSemaphore); xSemaphoreGive(xTrafficLightSemaphore); diff --git a/src/pcnt.cpp b/src/pcnt.cpp index f1cadf5..464d697 100644 --- a/src/pcnt.cpp +++ b/src/pcnt.cpp @@ -38,19 +38,28 @@ #define RESET 0 #define CAR 1 -#define PCNT_H_LIM_VAL 920 +#define PCNT_H_LIM_VAL 920 / 2 +#define PCNT_H_LIM_VAL_2 260 / 2 +#define PCNT_H_LIM_VAL_3 920 / 2 +#define PCNT_H_LIM_VAL_4 260 / 2 #define PCNT_FILTER_VAL 100 -#define TASK_DELAY 100 +#define TASK_DELAY 50 #define OVERFLOW_LIMIT 10 #define NO_FLAGS 0 +#define DISTANCE 0.18 + /***************************** Constants *******************************/ /***************************** Variables *******************************/ -uint8_t PCNTModule::object_count = RESET; +uint8_t PCNTModule::object_count = RESET; + +struct timeval system_time; +float time_pre, time_now; +uint8_t first = 0, second = 0; -/***************************** Objects *******************************/ +/***************************** Objects *******************************/ PCNTModule pcnt0, pcnt1, pcnt2, pcnt3; @@ -90,7 +99,7 @@ void PCNTModule::init_pcnt() { pcnt_config.pulse_gpio_num = input_pin; pcnt_config.pos_mode = PCNT_COUNT_INC; - pcnt_config.counter_h_lim = PCNT_H_LIM_VAL; + pcnt_config.counter_h_lim = high_limit; pcnt_config.unit = pcnt_unit; pcnt_config.channel = PCNT_CHANNEL_0; pcnt_unit_config(&pcnt_config); @@ -102,7 +111,7 @@ void PCNTModule::init_pcnt() { pcnt_isr_handler_add(pcnt_unit, &PCNTModule::static_counter_overflow, this); pcnt_intr_enable(pcnt_unit); - pcnt_set_filter_value(pcnt_unit, PCNT_FILTER_VAL); + pcnt_set_filter_value(pcnt_unit, filter_value); pcnt_filter_enable(pcnt_unit); pcnt_counter_pause(pcnt_unit); @@ -150,10 +159,22 @@ void PCNTModule::pcnt_task() { case PCNT_UNIT_2: // SEN3 traffic_light_id = traffic_light1.get_id(); traffic_light1.increment_queue(); + if (first == 1) { + gettimeofday(&system_time, NULL); + time_now = (float)system_time.tv_sec * 1000000L + + (float)system_time.tv_usec; + velocity = (DISTANCE / ((time_now - time_pre) / 1000000)) * 3.6; + first = 0; + second = 1; + } break; case PCNT_UNIT_3: // SEN4 traffic_light_id = traffic_light1.get_id(); traffic_light1.increment_queue(); + gettimeofday(&system_time, NULL); + time_pre = (float)system_time.tv_sec * 1000000L + + (float)system_time.tv_usec; + first = 1; break; default: traffic_light_id = 0; @@ -184,12 +205,12 @@ void PCNTModule::pcnt_task() { } else { overflow_counter = RESET; state = NO_CAR; - } - /* read_pcnt(); - Serial.print(pulse_counter); - Serial.print("\t"); - Serial.print(overflow_counter); - Serial.print("\n"); */ + } /* + read_pcnt(); + Serial.print(pulse_counter); + Serial.print("\t"); + Serial.print(overflow_counter); + Serial.print("\n"); */ break; @@ -206,11 +227,11 @@ void setup_pcnt() { pcnt0.init(PCNT_UNIT_0, INPUT_PIN_0, PCNT_H_LIM_VAL, PCNT_FILTER_VAL, TASK_DELAY, OVERFLOW_LIMIT); - pcnt1.init(PCNT_UNIT_1, INPUT_PIN_1, PCNT_H_LIM_VAL, PCNT_FILTER_VAL, + pcnt1.init(PCNT_UNIT_1, INPUT_PIN_1, PCNT_H_LIM_VAL_2, PCNT_FILTER_VAL, TASK_DELAY, OVERFLOW_LIMIT); - pcnt2.init(PCNT_UNIT_2, INPUT_PIN_2, PCNT_H_LIM_VAL, PCNT_FILTER_VAL, + pcnt2.init(PCNT_UNIT_2, INPUT_PIN_2, PCNT_H_LIM_VAL_3, PCNT_FILTER_VAL, TASK_DELAY, OVERFLOW_LIMIT); - pcnt3.init(PCNT_UNIT_3, INPUT_PIN_3, PCNT_H_LIM_VAL, PCNT_FILTER_VAL, + pcnt3.init(PCNT_UNIT_3, INPUT_PIN_3, PCNT_H_LIM_VAL_4, PCNT_FILTER_VAL, TASK_DELAY, OVERFLOW_LIMIT); } diff --git a/src/traffic_cycle.cpp b/src/traffic_cycle.cpp index 36152b4..5865639 100644 --- a/src/traffic_cycle.cpp +++ b/src/traffic_cycle.cpp @@ -105,7 +105,7 @@ void traffic_cycle(void *pvParameters) { traffic_algorithm(); - static uint8_t traffic_state = WE_READY; + static uint8_t traffic_state = TL_IDLE; uint8_t timer = traffic_light0.get_timer(); @@ -120,6 +120,7 @@ void traffic_cycle(void *pvParameters) { traffic_light0.set_timer(IDLE_TIME); traffic_light1.set_timer(IDLE_TIME); traffic_light2.set_timer(IDLE_TIME); + traffic_state = WE_READY; break; case WE_READY: diff --git a/src/wifi.cpp b/src/wifi.cpp index 229d3b6..aca7efe 100644 --- a/src/wifi.cpp +++ b/src/wifi.cpp @@ -26,6 +26,8 @@ /***************************** Variables *******************************/ +float velocity; + /***************************** Objects *******************************/ QueueHandle_t xCarQueue, xTrafficLightQueue; @@ -53,6 +55,7 @@ void connect_eduroam(String ssid, String password, String identity) { password); while (WiFi.status() != WL_CONNECTED) { + Serial.println("wait"); } } @@ -63,30 +66,42 @@ void api_task(void *pvParameters) { while (1) { if (WiFi.status() == WL_CONNECTED) { - uint8_t traffic_light_id = 1; + uint8_t traffic_light_id, send = 0; if (xQueueReceive(xCarQueue, &traffic_light_id, (TickType_t)TICKS_WAIT) == pdPASS) { xSemaphoreTake(xCarSemaphore, (TickType_t)TICKS_WAIT); - HTTPClient http; + HTTPClient http; + + if (traffic_light_id == 1) { + if (second == 1) { + send = 1; + second = 0; + } + } else { + send = 1; + velocity = 0; + } - const String server = "http://192.168.171.208:3000"; + if (send == 1) { - const String car_url = server + "/vehicles/insert" + - "?car_id=" + car_counter + "&velocity=" + 2 + - "&date_id=" + 1 + "&car_type_id=" + 2 + - "&traffic_light_id=" + traffic_light_id; + const String server = "http://192.168.171.208:3000"; - http.begin(car_url); - http.POST(""); + const String car_url = + server + "/vehicles/insert" + "?car_id=" + car_counter + + "&velocity=" + velocity + "&date_id=" + 1 + "&car_type_id=" + 2 + + "&traffic_light_id=" + traffic_light_id; - car_counter++; + http.begin(car_url); + http.POST(""); - http.end(); + car_counter++; - xSemaphoreGive(xCarSemaphore); - Serial.println("sent"); + http.end(); + + xSemaphoreGive(xCarSemaphore); + } if (xQueueReceive(xTrafficLightQueue, &traffic_light_id, (TickType_t)TICKS_WAIT) == pdPASS) { diff --git a/web-server/migrations/01_initial_migration.sql b/web-server/migrations/01_initial_migration.sql index 19b6610..576e225 100644 --- a/web-server/migrations/01_initial_migration.sql +++ b/web-server/migrations/01_initial_migration.sql @@ -27,7 +27,7 @@ CREATE TABLE IF NOT EXISTS traffic_light ( CREATE TABLE IF NOT EXISTS cars ( car_id INTEGER NOT NULL, - velocity INTEGER NOT NULL, + velocity DOUBLE(10, 5) NOT NULL, clock TIME(4) NOT NULL, date_id INTEGER NOT NULL, car_type_id INTEGER NOT NULL, @@ -56,6 +56,6 @@ INSERT INTO traffic_light VALUES ('2', "Yellow", "West", "Marksvej, 5530 Odense", '7'); INSERT INTO cars VALUES - ('1', "50", '12:17:00.2020', '1', '2', '0'), - ('2', "20", '14:30:39.3002', '3', '1', '1'), - ('3', "10", '09:12:21.1021', '1', '1', '2'); + ('1', "50.0", '12:17:00.2020', '1', '2', '0'), + ('2', "20.0", '14:30:39.3002', '3', '1', '1'), + ('3', "10.0", '09:12:21.1021', '1', '1', '2'); diff --git a/web-server/src/main.rs b/web-server/src/main.rs index 3ec6424..6b64fa5 100644 --- a/web-server/src/main.rs +++ b/web-server/src/main.rs @@ -49,7 +49,7 @@ impl Api { #[derive(Object)] struct Car { car_id: i64, - velocity: i64, + velocity: f64, clock: String, date_id: i64, car_type_id: i64, @@ -106,7 +106,7 @@ impl Api { /// ``` async fn vehicle_retrieve(&self) -> Result>> { let mut stream = - sqlx::query_as::<_, (i64, i64, time::Time, i64, i64, i64)>("SELECT * FROM cars") + sqlx::query_as::<_, (i64, f64, time::Time, i64, i64, i64)>("SELECT * FROM cars") .fetch(&*self.db); let mut results = Vec::new(); while let Some(res) = stream.next().await {