Task A+B concurrency <check ok without ADC
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,57 +17,65 @@ void rtIgnitionTask(void *pvParameters)
|
||||
LOG_ERROR("Null rt_task_ptr parameters");
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
LOG_INFO("rtTask Params OK");
|
||||
|
||||
|
||||
// Task Parameters and Devices
|
||||
rtTaskParams *params = (rtTaskParams *)pvParameters;
|
||||
const rtTaskInterrupts rt_int = params->rt_int; // copy to avoid external override
|
||||
const rtTaskResets rt_rst = params->rt_resets; // copy to avoid external override
|
||||
QueueHandle_t rt_queue = params->rt_queue;
|
||||
TaskHandle_t rt_handle_ptr = *params->rt_handle_ptr;
|
||||
Devices *dev = params->dev;
|
||||
ADS1256 *adc = dev->adc_a;
|
||||
PCA9555 *io = dev->io;
|
||||
|
||||
TaskStatus_t rt_task_info;
|
||||
vTaskGetInfo(NULL, &rt_task_info, pdFALSE, eInvalid);
|
||||
|
||||
const auto rt_task_name = pcTaskGetName(rt_task_info.xHandle);
|
||||
LOG_INFO("rtTask Params OK [", rt_task_name, "]");
|
||||
|
||||
ignitionBoxStatus ign_box_sts;
|
||||
|
||||
// Variables for ISR, static to be fixed in memory locations
|
||||
static isrParams isr_params_t12p{
|
||||
isrParams isr_params_t12p{
|
||||
.flag = TRIG_FLAG_12P,
|
||||
.ign_stat = &ign_box_sts,
|
||||
.rt_handle_ptr = rt_handle_ptr};
|
||||
static isrParams isr_params_t12n{
|
||||
.rt_handle_ptr = rt_task_info.xHandle};
|
||||
isrParams isr_params_t12n{
|
||||
.flag = TRIG_FLAG_12N,
|
||||
.ign_stat = &ign_box_sts,
|
||||
.rt_handle_ptr = rt_handle_ptr};
|
||||
static isrParams isr_params_t34p{
|
||||
.rt_handle_ptr = rt_task_info.xHandle};
|
||||
isrParams isr_params_t34p{
|
||||
.flag = TRIG_FLAG_34P,
|
||||
.ign_stat = &ign_box_sts,
|
||||
.rt_handle_ptr = rt_handle_ptr};
|
||||
static isrParams isr_params_t34n{
|
||||
.rt_handle_ptr = rt_task_info.xHandle};
|
||||
isrParams isr_params_t34n{
|
||||
.flag = TRIG_FLAG_34N,
|
||||
.ign_stat = &ign_box_sts,
|
||||
.rt_handle_ptr = rt_handle_ptr};
|
||||
static isrParams isr_params_sp12{
|
||||
.rt_handle_ptr = rt_task_info.xHandle};
|
||||
isrParams isr_params_sp12{
|
||||
.flag = SPARK_FLAG_12,
|
||||
.ign_stat = &ign_box_sts,
|
||||
.rt_handle_ptr = rt_handle_ptr};
|
||||
static isrParams isr_params_sp34{
|
||||
.rt_handle_ptr = rt_task_info.xHandle};
|
||||
isrParams isr_params_sp34{
|
||||
.flag = SPARK_FLAG_34,
|
||||
.ign_stat = &ign_box_sts,
|
||||
.rt_handle_ptr = rt_handle_ptr};
|
||||
.rt_handle_ptr = rt_task_info.xHandle};
|
||||
|
||||
LOG_INFO("rtTask ISR Params OK");
|
||||
LOG_DEBUG("rtTask HDL Params OK, HDL* [", (uint32_t)rt_task_info.xHandle, "]");
|
||||
LOG_DEBUG("rtTask ISR Params OK, ISR* [", (uint32_t)rt_int.isr_ptr, "]");
|
||||
LOG_DEBUG("rtTask QUE Params OK, QUE* [", (uint32_t)rt_queue, "]");
|
||||
|
||||
// Create esp_timer for microsecond precision timeout
|
||||
esp_timer_handle_t timeout_timer;
|
||||
esp_timer_create_args_t timer_args = {
|
||||
.callback = spark_timeout_callback,
|
||||
.arg = (void *)rt_handle_ptr,
|
||||
.arg = (void *)rt_task_info.xHandle,
|
||||
.dispatch_method = ESP_TIMER_TASK,
|
||||
.name = "spark_timeout"};
|
||||
esp_timer_create(&timer_args, &timeout_timer);
|
||||
|
||||
|
||||
// Attach Pin Interrupts
|
||||
attachInterruptArg(digitalPinToInterrupt(rt_int.trig_pin_12p), rt_int.isr_ptr, (void *)&isr_params_t12p, RISING);
|
||||
attachInterruptArg(digitalPinToInterrupt(rt_int.trig_pin_12n), rt_int.isr_ptr, (void *)&isr_params_t12n, RISING);
|
||||
@@ -76,7 +84,7 @@ void rtIgnitionTask(void *pvParameters)
|
||||
attachInterruptArg(digitalPinToInterrupt(rt_int.spark_pin_12), rt_int.isr_ptr, (void *)&isr_params_sp12, RISING);
|
||||
attachInterruptArg(digitalPinToInterrupt(rt_int.spark_pin_34), rt_int.isr_ptr, (void *)&isr_params_sp34, RISING);
|
||||
|
||||
LOG_INFO("rtTask ISR Attach OK");
|
||||
LOG_INFO("rtTask ISR Attach OK [", rt_task_name, "]");
|
||||
|
||||
// Global rt_task_ptr variables
|
||||
bool first_cycle = true;
|
||||
|
||||
@@ -55,7 +55,6 @@ struct rtTaskParams
|
||||
{
|
||||
bool rt_running; // run flag, false to terminate
|
||||
Devices *dev;
|
||||
TaskHandle_t* rt_handle_ptr;
|
||||
const QueueHandle_t rt_queue;
|
||||
const rtTaskInterrupts rt_int; // interrupt pins to attach
|
||||
const rtTaskResets rt_resets; // reset ping for peak detectors
|
||||
|
||||
@@ -14,191 +14,3 @@ void printField(const char name[], const char *val);
|
||||
|
||||
void printInfo(const ignitionBoxStatus &info);
|
||||
|
||||
static const std::string htmlTest = R"rawliteral(
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>ESP32 Dashboard</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial;
|
||||
text-align: center;
|
||||
margin-top: 40px;
|
||||
}
|
||||
|
||||
table {
|
||||
margin: auto;
|
||||
border-collapse: collapse;
|
||||
width: 100%;
|
||||
max-width: 900px;
|
||||
}
|
||||
|
||||
th, td {
|
||||
border: 1px solid #ccc;
|
||||
padding: 10px;
|
||||
font-size: 16px;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
th {
|
||||
background-color: #f4f4f4;
|
||||
}
|
||||
|
||||
button {
|
||||
margin: 10px;
|
||||
padding: 10px 20px;
|
||||
font-size: 16px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h2>RotaxMonitor realtime data</h2>
|
||||
|
||||
<button onclick="start()">Start</button>
|
||||
<button onclick="stop()">Stop</button>
|
||||
|
||||
<div style="max-width: 900px; margin: 0 auto; text-align: left;">
|
||||
<p><strong>Timestamp:</strong> <span id="timestamp">-</span></p>
|
||||
<p><strong>Data Valid:</strong> <span id="datavalid">-</span></p>
|
||||
<p><strong>Generator voltage:</strong> <span id="volts_gen">-</span></p>
|
||||
<p><strong>Engine RPM:</strong> <span id="eng_rpm">-</span></p>
|
||||
<p><strong>ADC read time:</strong> <span id="adc_read_time">-</span></p>
|
||||
<p><strong>Queue errors:</strong> <span id="n_queue_errors">-</span></p>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Property</th>
|
||||
<th>Coils 12</th>
|
||||
<th>Coils 34</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>Spark delay</td>
|
||||
<td id="coils12_spark_delay">-</td>
|
||||
<td id="coils34_spark_delay">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Spark status</td>
|
||||
<td id="coils12_spark_status">-</td>
|
||||
<td id="coils34_spark_status">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Soft start status</td>
|
||||
<td id="coils12_sstart_status">-</td>
|
||||
<td id="coils34_sstart_status">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Peak P in</td>
|
||||
<td id="coils12_peak_p_in">-</td>
|
||||
<td id="coils34_peak_p_in">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Peak N in</td>
|
||||
<td id="coils12_peak_n_in">-</td>
|
||||
<td id="coils34_peak_n_in">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Peak P out</td>
|
||||
<td id="coils12_peak_p_out">-</td>
|
||||
<td id="coils34_peak_p_out">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Peak N out</td>
|
||||
<td id="coils12_peak_n_out">-</td>
|
||||
<td id="coils34_peak_n_out">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Level spark</td>
|
||||
<td id="coils12_level_spark">-</td>
|
||||
<td id="coils34_level_spark">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Events</td>
|
||||
<td id="coils12_n_events">-</td>
|
||||
<td id="coils34_n_events">-</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Missed firings</td>
|
||||
<td id="coils12_n_missed_firing">-</td>
|
||||
<td id="coils34_n_missed_firing">-</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
let ws;
|
||||
|
||||
function connectWS() {
|
||||
ws = new WebSocket("ws://" + location.host + "/ws");
|
||||
|
||||
ws.onopen = () => {
|
||||
console.log("WebSocket connesso");
|
||||
};
|
||||
|
||||
ws.onclose = () => {
|
||||
console.log("WebSocket disconnesso, retry...");
|
||||
setTimeout(connectWS, 5000);
|
||||
};
|
||||
|
||||
ws.onmessage = (event) => {
|
||||
let data;
|
||||
|
||||
try {
|
||||
data = JSON.parse(event.data);
|
||||
} catch (e) {
|
||||
console.error("Invalid JSON received", e);
|
||||
return;
|
||||
}
|
||||
|
||||
document.getElementById("datavalid").textContent = data.datavalid ?? "-";
|
||||
document.getElementById("timestamp").textContent = data.timestamp ?? "-";
|
||||
document.getElementById("volts_gen").textContent = data.volts_gen ?? "-";
|
||||
document.getElementById("eng_rpm").textContent = data.eng_rpm ?? "-";
|
||||
document.getElementById("adc_read_time").textContent = data.adc_read_time ?? "-";
|
||||
document.getElementById("n_queue_errors").textContent = data.n_queue_errors ?? "-";
|
||||
|
||||
const coils12 = data.coils12 || {};
|
||||
const coils34 = data.coils34 || {};
|
||||
|
||||
document.getElementById("coils12_spark_delay").textContent = coils12.spark_delay ?? "-";
|
||||
document.getElementById("coils34_spark_delay").textContent = coils34.spark_delay ?? "-";
|
||||
document.getElementById("coils12_spark_status").textContent = coils12.spark_status ?? "-";
|
||||
document.getElementById("coils34_spark_status").textContent = coils34.spark_status ?? "-";
|
||||
document.getElementById("coils12_sstart_status").textContent = coils12.sstart_status ?? "-";
|
||||
document.getElementById("coils34_sstart_status").textContent = coils34.sstart_status ?? "-";
|
||||
document.getElementById("coils12_peak_p_in").textContent = coils12.peak_p_in ?? "-";
|
||||
document.getElementById("coils34_peak_p_in").textContent = coils34.peak_p_in ?? "-";
|
||||
document.getElementById("coils12_peak_n_in").textContent = coils12.peak_n_in ?? "-";
|
||||
document.getElementById("coils34_peak_n_in").textContent = coils34.peak_n_in ?? "-";
|
||||
document.getElementById("coils12_peak_p_out").textContent = coils12.peak_p_out ?? "-";
|
||||
document.getElementById("coils34_peak_p_out").textContent = coils34.peak_p_out ?? "-";
|
||||
document.getElementById("coils12_peak_n_out").textContent = coils12.peak_n_out ?? "-";
|
||||
document.getElementById("coils34_peak_n_out").textContent = coils34.peak_n_out ?? "-";
|
||||
document.getElementById("coils12_level_spark").textContent = coils12.level_spark ?? "-";
|
||||
document.getElementById("coils34_level_spark").textContent = coils34.level_spark ?? "-";
|
||||
document.getElementById("coils12_n_events").textContent = coils12.n_events ?? "-";
|
||||
document.getElementById("coils34_n_events").textContent = coils34.n_events ?? "-";
|
||||
document.getElementById("coils12_n_missed_firing").textContent = coils12.n_missed_firing ?? "-";
|
||||
document.getElementById("coils34_n_missed_firing").textContent = coils34.n_missed_firing ?? "-";
|
||||
};
|
||||
}
|
||||
|
||||
function start() {
|
||||
fetch("/start");
|
||||
}
|
||||
|
||||
function stop() {
|
||||
fetch("/stop");
|
||||
}
|
||||
|
||||
connectWS();
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
)rawliteral";
|
||||
Reference in New Issue
Block a user