Skip to content

Commit

Permalink
Made define-names more descriptive given the context
Browse files Browse the repository at this point in the history
  • Loading branch information
fabseman committed May 11, 2024
1 parent 2a80f83 commit b96e69e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 75 deletions.
3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@
#define LARGE_STACK 2024

#define BAUDRATE 921600
#define RESET 0
#define QUEUE_SIZE 10

#define RESET 0

/***************************** Constants *******************************/

/***************************** Variables *******************************/
Expand Down
74 changes: 36 additions & 38 deletions src/pcnt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,34 @@

/***************************** Defines *******************************/

#define PCNT_FREQ_UNIT_0 PCNT_UNIT_0
#define PCNT_FREQ_UNIT_1 PCNT_UNIT_1
#define PCNT_FREQ_UNIT_2 PCNT_UNIT_2
#define PCNT_FREQ_UNIT_3 PCNT_UNIT_3

#define INPUT_PIN_0 7
#define INPUT_PIN_1 8
#define INPUT_PIN_2 9
#define INPUT_PIN_3 21

#define NO_CAR 0
#define RESET 0
#define CAR 1

#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 50
#define OVERFLOW_LIMIT 10
#define NO_FLAGS 0

#define DISTANCE 0.18
#define PCNT_FREQ_UNIT_0 PCNT_UNIT_0
#define PCNT_FREQ_UNIT_1 PCNT_UNIT_1
#define PCNT_FREQ_UNIT_2 PCNT_UNIT_2
#define PCNT_FREQ_UNIT_3 PCNT_UNIT_3

#define INPUT_PIN_0 7
#define INPUT_PIN_1 8
#define INPUT_PIN_2 9
#define INPUT_PIN_3 21

#define PCNT_H_LIM_VAL 460
#define PCNT_H_LIM_VAL_2 130
#define PCNT_H_LIM_VAL_3 460
#define PCNT_H_LIM_VAL_4 130
#define PCNT_FILTER_VAL 100
#define TASK_DELAY 50
#define OVERFLOW_LIMIT 10
#define NO_FLAGS 0

#define DISTANCE_M 0.18
#define VELOCITY_CONVERSION 3.6

#define NO_CAR 0
#define CAR 1

#define RESET 0
#define FALSE 0
#define TRUE 1

/***************************** Constants *******************************/

Expand All @@ -57,7 +61,7 @@ uint8_t PCNTModule::object_count = RESET;

struct timeval system_time;
float time_pre, time_now;
uint8_t first = 0, second = 0;
uint8_t first, second;

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

Expand Down Expand Up @@ -159,13 +163,14 @@ void PCNTModule::pcnt_task() {
case PCNT_UNIT_2: // SEN3
traffic_light_id = traffic_light1.get_id();
traffic_light1.increment_queue();
if (first == 1) {
if (first == TRUE) {
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;
velocity = (DISTANCE_M / ((time_now - time_pre) / 1000000)) *
VELOCITY_CONVERSION;
first = RESET;
second = TRUE;
}
break;
case PCNT_UNIT_3: // SEN4
Expand All @@ -174,7 +179,7 @@ void PCNTModule::pcnt_task() {
gettimeofday(&system_time, NULL);
time_pre = (float)system_time.tv_sec * 1000000L +
(float)system_time.tv_usec;
first = 1;
first = TRUE;
break;
default:
traffic_light_id = 0;
Expand All @@ -188,7 +193,6 @@ void PCNTModule::pcnt_task() {
overflow_counter = RESET;
} else {
overflow_counter = RESET;
// Serial.printf("not changed");
}
break;

Expand All @@ -205,13 +209,7 @@ 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"); */

}
break;

default:
Expand Down
35 changes: 21 additions & 14 deletions src/traffic_cycle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@

#define IDLE_TIME 10
#define NS_READY_TIME 2
#define NS_GO_TIME 7
#define NS_GO_TIME 18
#define NS_WAIT_TIME 3
#define WE_READY_TIME 2
#define WE_GO_TIME 7
#define WE_GO_TIME 18
#define WE_WAIT_TIME 3

#define QUEUE_LIMIT 5
#define MINIMUM_QUEUE 1

#define RESET 0
#define EMPTY 0
#define SECOND_PASSED 1
#define ONE_SECOND 1

Expand Down Expand Up @@ -69,27 +73,30 @@ void traffic_algorithm() {
uint8_t we_queue_size_2 = traffic_light1.get_queue_size();
uint8_t ns_queue_size = traffic_light2.get_queue_size();

if (((we_queue_size_1 > 5 || we_queue_size_2 > 5) && we_state == "Green") ||
(ns_queue_size > 5 && ns_state == "Green")) {
if (((we_queue_size_1 > QUEUE_LIMIT || we_queue_size_2 > QUEUE_LIMIT) &&
we_state == "Green") ||
(ns_queue_size > QUEUE_LIMIT && ns_state == "Green")) {
traffic_light0.increment_timer(ONE_SECOND);
traffic_light2.increment_timer(ONE_SECOND);
} else if (((we_queue_size_1 > 5 || we_queue_size_2 > 5) &&
} else if (((we_queue_size_1 > QUEUE_LIMIT ||
we_queue_size_2 > QUEUE_LIMIT) &&
we_state == "Red") ||
(ns_queue_size > 5 && ns_state == "Red")) {
(ns_queue_size > QUEUE_LIMIT && ns_state == "Red")) {
traffic_light0.decrement_timer(ONE_SECOND);
traffic_light2.decrement_timer(ONE_SECOND);
}

if ((we_queue_size_1 >= 1 || we_queue_size_2 >= 1) &&
if ((we_queue_size_1 >= MINIMUM_QUEUE ||
we_queue_size_2 >= MINIMUM_QUEUE) &&
(we_state != "Green" || we_state != "RedYellow") &&
ns_queue_size == 0) {
traffic_light0.set_timer(0);
traffic_light2.set_timer(0);
} else if (ns_queue_size >= 1 &&
ns_queue_size == EMPTY) {
traffic_light0.set_timer(RESET);
traffic_light2.set_timer(RESET);
} else if (ns_queue_size >= MINIMUM_QUEUE &&
(ns_state != "Green" || ns_state != "RedYellow") &&
(we_queue_size_1 == 0 && we_queue_size_2 == 0)) {
traffic_light0.set_timer(0);
traffic_light2.set_timer(0);
(we_queue_size_1 == EMPTY && we_queue_size_2 == EMPTY)) {
traffic_light0.set_timer(RESET);
traffic_light2.set_timer(RESET);
}

timer_change = RESET;
Expand Down
48 changes: 26 additions & 22 deletions src/wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

/***************************** Defines *******************************/

#define RESET 0
#define FALSE 0
#define TRUE 1

/***************************** Constants *******************************/

/***************************** Variables *******************************/
Expand Down Expand Up @@ -66,7 +70,7 @@ void api_task(void *pvParameters) {

while (1) {
if (WiFi.status() == WL_CONNECTED) {
uint8_t traffic_light_id, send = 0;
uint8_t traffic_light_id, send = FALSE;

if (xQueueReceive(xCarQueue, &traffic_light_id, (TickType_t)TICKS_WAIT) ==
pdPASS) {
Expand All @@ -76,15 +80,15 @@ void api_task(void *pvParameters) {

if (traffic_light_id == 1) {
if (second == 1) {
send = 1;
second = 0;
send = TRUE;
second = RESET;
}
} else {
send = 1;
velocity = 0;
send = TRUE;
velocity = RESET;
}

if (send == 1) {
if (send == TRUE) {

const String server = "http://192.168.171.208:3000";

Expand All @@ -102,32 +106,32 @@ void api_task(void *pvParameters) {

xSemaphoreGive(xCarSemaphore);
}
}

if (xQueueReceive(xTrafficLightQueue, &traffic_light_id,
(TickType_t)TICKS_WAIT) == pdPASS) {
xSemaphoreTake(xTrafficLightSemaphore, (TickType_t)TICKS_WAIT);
if (xQueueReceive(xTrafficLightQueue, &traffic_light_id,
(TickType_t)TICKS_WAIT) == pdPASS) {
xSemaphoreTake(xTrafficLightSemaphore, (TickType_t)TICKS_WAIT);

HTTPClient http;
HTTPClient http;

const String traffic_light0_url = traffic_light0.get_url();
const String traffic_light0_url = traffic_light0.get_url();

http.begin(traffic_light0_url);
http.POST("");
http.begin(traffic_light0_url);
http.POST("");

const String traffic_light1_url = traffic_light1.get_url();
const String traffic_light1_url = traffic_light1.get_url();

http.begin(traffic_light1_url);
http.POST("");
http.begin(traffic_light1_url);
http.POST("");

const String traffic_light2_url = traffic_light2.get_url();
const String traffic_light2_url = traffic_light2.get_url();

http.begin(traffic_light2_url);
http.POST("");
http.begin(traffic_light2_url);
http.POST("");

http.end();
http.end();

xSemaphoreGive(xTrafficLightSemaphore);
}
xSemaphoreGive(xTrafficLightSemaphore);
}
}
}
Expand Down

0 comments on commit b96e69e

Please sign in to comment.