Task A+B concurrency <check ok without ADC

This commit is contained in:
2026-04-10 22:03:09 +02:00
parent 736a8d8bd5
commit 246ba7eeb2
11 changed files with 204 additions and 354 deletions

View File

@@ -20,7 +20,7 @@
#include "freertos/task.h"
// Defines to enable channel B
// #define CH_B_ENABLE
#define CH_B_ENABLE
#define TEST
// Debug Defines
@@ -99,15 +99,17 @@ void loop()
Devices dev;
// Task handle
TaskHandle_t trigA_TaskHandle = NULL;
TaskHandle_t trigB_TaskHandle = NULL;
// Data Queue for real time task to main loop communication
QueueHandle_t rt_taskA_queue = xQueueCreate(max_queue, sizeof(ignitionBoxStatus));
QueueHandle_t rt_taskB_queue = xQueueCreate(max_queue, sizeof(ignitionBoxStatus));
rtTaskParams taskA_params{
.rt_running = true,
.dev = &dev,
.rt_handle_ptr = &trigA_TaskHandle,
.rt_queue = rt_taskA_queue,
.rt_int = rtTaskInterrupts{
.isr_ptr = trig_isr_A,
.isr_ptr = &trig_isr_A,
.trig_pin_12p = TRIG_PIN_A12P,
.trig_pin_12n = TRIG_PIN_A12N,
.trig_pin_34p = TRIG_PIN_A34P,
@@ -117,15 +119,12 @@ void loop()
.rt_resets = rtTaskResets{.rst_io_peak = RST_EXT_PEAK_DETECT_A, .rst_io_sh = RST_EXT_SAMPLE_HOLD_A}};
#ifdef CH_B_ENABLE
TaskHandle_t trigB_TaskHandle = NULL;
QueueHandle_t rt_taskB_queue = xQueueCreate(max_queue, sizeof(ignitionBoxStatus));
rtTaskParams taskB_params{
.rt_running = true,
.dev = &dev,
.rt_handle_ptr = &trigB_TaskHandle,
.rt_queue = rt_taskB_queue,
.rt_int = rtTaskInterrupts{
.isr_ptr = trig_isr_B,
.isr_ptr = &trig_isr_B,
.trig_pin_12p = TRIG_PIN_B12P,
.trig_pin_12n = TRIG_PIN_B12N,
.trig_pin_34p = TRIG_PIN_B34P,
@@ -135,7 +134,7 @@ void loop()
.rt_resets = rtTaskResets{.rst_io_peak = RST_EXT_PEAK_DETECT_B, .rst_io_sh = RST_EXT_SAMPLE_HOLD_B}};
#endif
if (!rt_taskA_queue /*|| !rt_taskB_queue*/)
if (!rt_taskA_queue || !rt_taskB_queue)
{
LOG_ERROR("Unable To Create task queues");
LOG_ERROR("5 seconds to restart...");
@@ -210,7 +209,7 @@ void loop()
(void *)&taskB_params,
RT_TASK_PRIORITY, // priorità leggermente più alta
&trigB_TaskHandle,
CORE_0);
CORE_1);
delay(100); // give some time to the thread to start
#endif
@@ -226,12 +225,18 @@ void loop()
////////////////////// MAIN LOOP //////////////////////
bool partial_save = false; // flag to indicate if a partial save has been done after a timeout
uint32_t counter = 0;
auto last_data = millis();
uint32_t counter_a = 0;
uint32_t counter_b = 0;
uint32_t wait_count = 0;
ignitionBoxStatus ign_info_A;
ignitionBoxStatus ign_info_B;
ignitionBoxStatusAverage ign_info_avg_A(filter_k);
ignitionBoxStatusAverage ign_info_avg_B(filter_k);
LITTLEFSGuard fsGuard;
WebPage webPage(80, LittleFS); // Initialize webserver and Websocket
@@ -240,66 +245,78 @@ void loop()
auto dataA = pdFALSE;
auto dataB = pdFALSE;
if (counter >= active_history_A->size()) // not concurrent with write task
dataA = xQueueReceive(rt_taskA_queue, &ign_info_A, pdMS_TO_TICKS(10));
if (counter_a >= active_history_A->size()) // not concurrent with write task
{
counter = 0;
counter_a = 0;
partial_save = false; // reset partial save flag on new data cycle
swapHistory(active_history_A, writable_history_A);
save_history(*writable_history_A, "ignition_historyA.csv"); // directly call the save task function to save without delay
}
dataA = xQueueReceive(rt_taskA_queue, &ign_info_A, pdMS_TO_TICKS(100));
#ifdef CH_B_ENABLE
if (counter >= active_history_B->size()) // not concurrent with write task
dataB = xQueueReceive(rt_taskB_queue, &ign_info_B, pdMS_TO_TICKS(10));
if (counter_b >= active_history_B->size()) // not concurrent with write task
{
counter = 0;
counter_b = 0;
partial_save = false; // reset partial save flag on new data cycle
swapHistory(active_history_B, writable_history_B);
save_history(*writable_history_B, "ignition_historyB.csv"); // directly call the save task function to save without delay
}
dataB = xQueueReceive(rt_taskB_queue, &ign_info_B, pdMS_TO_TICKS(100));
#endif
// Update last data
if (dataA == pdTRUE || dataB == pdTRUE)
{
// printInfo(ign_info);
(*active_history_A)[counter % active_history_A->size()] = ign_info_A;
#ifdef CH_B_ENABLE
(*active_history_B)[counter % active_history_B->size()] = ign_info_B;
#endif
last_data = millis();
}
if (dataA == pdTRUE)
{
(*active_history_A)[counter_a++ % active_history_A->size()] = ign_info_A;
ign_info_avg_A.update(ign_info_A); // update moving average with latest ignition status
ign_info_avg_B.update(ign_info_B); // update moving average with latest ignition status
Serial.printf("\033[2K Data Received A: %d/%d\r", counter, (*active_history_A).size());
if (counter % filter_k == 0) // send data every 10 samples
Serial.printf("Data Received A: %d/%d\n\r", counter_a, (*active_history_A).size());
if (counter_a % filter_k == 0) // send data every 10 samples
{
Serial.println();
LOG_DEBUG("Sending average ignition status to websocket clients...");
ArduinoJson::JsonDocument wsData;
wsData["box_a"] = ign_info_avg_A.toJson();
wsData["box_b"] = JsonObject();
webPage.sendWsData(wsData.as<String>());
}
}
#ifdef CH_B_ENABLE
if (dataB == pdTRUE)
{
(*active_history_B)[counter_b++ % active_history_B->size()] = ign_info_B;
ign_info_avg_B.update(ign_info_B); // update moving average with latest ignition status
Serial.printf("Data Received B: %d/%d\n\r", counter_b, (*active_history_B).size());
if (counter_b % filter_k == 0) // send data every 10 samples
{
ArduinoJson::JsonDocument wsData;
wsData["box_a"] = JsonObject();
wsData["box_b"] = ign_info_avg_B.toJson();
webPage.sendWsData(wsData.as<String>());
}
counter++;
}
else
#endif
if (dataA == pdFALSE && dataB == pdFALSE && millis() - last_data > 2000)
{
Serial.printf("[%d] Waiting for data...\r", wait_count++);
if (!partial_save && counter > 0) // if timeout occurs but we have unsaved data, save it before next timeout
if (!partial_save && counter_a > 0) // if timeout occurs but we have unsaved data, save it before next timeout
{
active_history_A->resize(counter); // resize active history to actual number of records received to avoid saving empty records
active_history_A->resize(counter_a); // resize active history to actual number of records received to avoid saving empty records
save_history(*active_history_A, "ignition_history_A.csv");
active_history_A->resize(max_history); // resize back to max history size for next data cycle
#ifdef CH_B_ENABLE
active_history_B->resize(counter); // resize active history to actual number of records received to avoid saving empty records
active_history_B->resize(counter_a); // resize active history to actual number of records received to avoid saving empty records
save_history(*active_history_B, "ignition_history_B.csv");
active_history_B->resize(max_history); // resize back to max history size for next data cycle
#endif
counter = 0; // reset counter after saving
counter_a = 0; // reset counter after saving
counter_b = 0; // reset counter after saving
partial_save = true;
first_save = true;
}
Serial.printf("[%d] Waiting for data...\r", wait_count++);
delay(500);
}
}