Skip to content

Commit

Permalink
Adjusted SQL-schema to store velocity as a floating point + calculate…
Browse files Browse the repository at this point in the history
…s the velocity on the ESP32 given the system time delta
  • Loading branch information
fabseman committed May 11, 2024
1 parent 4ed9dd9 commit 2a80f83
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 58 deletions.
13 changes: 7 additions & 6 deletions include/global_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *******************************/

Expand Down
2 changes: 1 addition & 1 deletion include/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

/***************************** Functions *******************************/

void IRAM_ATTR onTime();
void IRAM_ATTR timer_interrupt();
/*****************************************************************************
* Input : -
* Output : -
Expand Down
24 changes: 8 additions & 16 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,32 +58,24 @@ 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();

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);
Expand Down
51 changes: 36 additions & 15 deletions src/pcnt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion src/traffic_cycle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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:
Expand Down
41 changes: 28 additions & 13 deletions src/wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

/***************************** Variables *******************************/

float velocity;

/***************************** Objects *******************************/

QueueHandle_t xCarQueue, xTrafficLightQueue;
Expand Down Expand Up @@ -53,6 +55,7 @@ void connect_eduroam(String ssid, String password, String identity) {
password);

while (WiFi.status() != WL_CONNECTED) {
Serial.println("wait");
}
}

Expand All @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions web-server/migrations/01_initial_migration.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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');
4 changes: 2 additions & 2 deletions web-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -106,7 +106,7 @@ impl Api {
/// ```
async fn vehicle_retrieve(&self) -> Result<Json<Vec<Car>>> {
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 {
Expand Down

0 comments on commit 2a80f83

Please sign in to comment.