revert on double task wait, normal and soft start working

This commit is contained in:
2026-04-04 20:00:30 +02:00
parent b0842aadef
commit 38c595fd7b
8 changed files with 128 additions and 91 deletions

View File

@@ -26,7 +26,7 @@ void trig_isr(void *arg)
{ {
case TRIG_FLAG_12P: case TRIG_FLAG_12P:
case TRIG_FLAG_12N: case TRIG_FLAG_12N:
//if (isr_firing_count == 0) // if (isr_firing_count == 0)
{ {
// only on first trigger to avoid multiple firing due to noise, to be fixed with hardware debounce // only on first trigger to avoid multiple firing due to noise, to be fixed with hardware debounce
isr_firing_count++; isr_firing_count++;
@@ -35,10 +35,9 @@ void trig_isr(void *arg)
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken); xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
} }
break; break;
case TRIG_FLAG_34P: case TRIG_FLAG_34P:
case TRIG_FLAG_34N: case TRIG_FLAG_34N:
//if (isr_firing_count == 0) // if (isr_firing_count == 0)
{ {
// only on first trigger to avoid multiple firing due to noise, to be fixed with hardware debounce // only on first trigger to avoid multiple firing due to noise, to be fixed with hardware debounce
isr_firing_count++; isr_firing_count++;
@@ -48,24 +47,26 @@ void trig_isr(void *arg)
} }
break; break;
case SPARK_FLAG_12: case SPARK_FLAG_12:
//if (isr_firing_count > 0) // only consider spark if a trigger has been detected, otherwise noise on spark pin can cause false positives // if (isr_firing_count > 0) // only consider spark if a trigger has been detected, otherwise noise on spark pin can cause false positives
{ {
box->coils34.spark_ok = false; isr_firing_count = 0; // reset trigger timeout counter on spark event
box->coils12.spark_ok = true; box->coils34.spark_ok = false;
box->coils12.spark_time = time_us; box->coils12.spark_ok = true;
isr_firing_count = 0; // reset trigger timeout counter on spark event box->coils12.spark_time = time_us;
vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken); xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
} // vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken);
}
break; break;
case SPARK_FLAG_34: case SPARK_FLAG_34:
//if (isr_firing_count > 0) // only consider spark if a trigger has been detected, otherwise noise on spark pin can cause false positives // if (isr_firing_count > 0) // only consider spark if a trigger has been detected, otherwise noise on spark pin can cause false positives
{ {
box->coils12.spark_ok = false; isr_firing_count = 0; // reset trigger timeout counter on spark event
box->coils34.spark_ok = true; box->coils12.spark_ok = false;
box->coils34.spark_time = time_us; box->coils34.spark_ok = true;
isr_firing_count = 0; // reset trigger timeout counter on spark event box->coils34.spark_time = time_us;
vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken); xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
} // vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken);
}
break; break;
default: default:
break; break;

View File

@@ -63,36 +63,41 @@ static const std::map<const sparkStatus, const char *> sparkStatusNames = {
enum softStartStatus enum softStartStatus
{ {
NORMAL, NORMAL,
SOFT_START SOFT_START,
ERROR,
}; };
const std::map<const softStartStatus, const char *> softStartStatusNames = { const std::map<const softStartStatus, const char *> softStartStatusNames = {
{NORMAL, "NORMAL"}, {NORMAL, "NORMAL"},
{SOFT_START, "SOFT_START"}, {SOFT_START, "SOFT_START"},
{ERROR, "ERROR"},
}; };
struct coilsStatus struct coilsStatus
{ {
int64_t trig_time; int64_t trig_time = 0;
int64_t spark_time; int64_t spark_time = 0;
int64_t spark_delay; int64_t spark_delay = 0; // in microseconds
sparkStatus spark_status; sparkStatus spark_status = sparkStatus::SPARK_POS_OK;
softStartStatus sstart_status; softStartStatus sstart_status = softStartStatus::NORMAL;
float peak_p_in, peak_n_in; float peak_p_in = 0.0, peak_n_in = 0.0;
float peak_p_out, peak_n_out; float peak_p_out = 0.0, peak_n_out = 0.0;
float trigger_spark; float trigger_spark = 0.0;
bool spark_ok; bool spark_ok = false;
uint32_t n_events = 0;
}; };
// Task internal Status // Task internal Status
struct ignitionBoxStatus struct ignitionBoxStatus
{ {
int64_t timestamp; int64_t timestamp = 0;
// coils pairs for each ignition // coils pairs for each ignition
coilsStatus coils12; coilsStatus coils12;
coilsStatus coils34; coilsStatus coils34;
// voltage from generator // voltage from generator
float volts_gen = 0.0; float volts_gen = 0.0;
uint32_t n_queue_errors = 0;
}; };
struct isrParams struct isrParams

View File

@@ -168,46 +168,58 @@ void loop()
LOG_INFO("Real Time Tasks A & B initialized"); LOG_INFO("Real Time Tasks A & B initialized");
////////////////////// MAIN LOOP ////////////////////// ////////////////////// MAIN LOOP //////////////////////
uint32_t count(0);
clearScreen(); clearScreen();
setCursor(0, 0); setCursor(0, 0);
ignitionBoxStatus ignA;
int64_t last = esp_timer_get_time();
uint32_t missed_firings12 = 0;
uint32_t missed_firings34 = 0;
while (running) while (running)
{ {
ignitionBoxStatus ignA; if (xQueueReceive(rt_taskA_queue, &ignA, pdMS_TO_TICKS(1000)) == pdTRUE)
if (xQueueReceive(rt_taskA_queue, &ignA, pdMS_TO_TICKS(100)) == pdTRUE)
{ {
if (count++ % 10000 == 0) if (ignA.coils12.spark_status == sparkStatus::SPARK_NEG_FAIL || ignA.coils12.spark_status == sparkStatus::SPARK_POS_FAIL)
{ missed_firings12++;
firstRun = true; if (ignA.coils34.spark_status == sparkStatus::SPARK_POS_FAIL || ignA.coils34.spark_status == sparkStatus::SPARK_NEG_FAIL)
clearScreen(); missed_firings34++;
setCursor(0, 0); clearScreen();
printField("++ Timestamp", (uint32_t)ignA.timestamp, 0, 0); setCursor(0, 0);
Serial.println("========== Coils 12 ============="); printField("++ Timestamp", (uint32_t)ignA.timestamp, 0, 0);
printField("Pickup Tim", (uint32_t)ignA.coils12.trig_time, 0, 1); Serial.println("========== Coils 12 =============");
printField("Spark Tim", (uint32_t)ignA.coils12.spark_time, 0, 2); printField("Events", (uint32_t)ignA.coils12.n_events, 0, 1);
printField("Spark Dly", (uint32_t)ignA.coils12.spark_delay, 0, 3); printField("Missed Firing", missed_firings12, 0, 2);
printField("Spark Sts", sparkStatusNames.at(ignA.coils12.spark_status), 0, 4); printField("Spark Dly", (uint32_t)ignA.coils12.spark_delay, 0, 3);
printField("Peak P_IN", ignA.coils12.peak_p_in, 0, 5); printField("Spark Sts", sparkStatusNames.at(ignA.coils12.spark_status), 0, 4);
printField("Peak P_OUT", ignA.coils12.peak_p_out, 0, 6); // printField("Peak P_IN", ignA.coils12.peak_p_in, 0, 5);
printField("Peak N_IN", ignA.coils12.peak_n_in, 0, 7); // printField("Peak P_OUT", ignA.coils12.peak_p_out, 0, 6);
printField("Peak N_OUT", ignA.coils12.peak_n_out, 0, 8); // printField("Peak N_IN", ignA.coils12.peak_n_in, 0, 7);
printField("SoftStart ", softStartStatusNames.at(ignA.coils12.sstart_status), 0, 9); // printField("Peak N_OUT", ignA.coils12.peak_n_out, 0, 8);
printField("Soft Start ", softStartStatusNames.at(ignA.coils12.sstart_status), 0, 9);
Serial.println("========== Coils 34 ============="); Serial.println("========== Coils 34 =============");
printField("Pickup Tim", (uint32_t)ignA.coils34.trig_time, 0, 11); printField("Events", (uint32_t)ignA.coils34.n_events, 0, 11);
printField("Spark Tim", (uint32_t)ignA.coils34.spark_time, 0, 12); printField("Missed Firing", missed_firings34, 0, 12);
printField("Spark Dly", (uint32_t)ignA.coils34.spark_delay, 0, 13); printField("Spark Dly", (uint32_t)ignA.coils34.spark_delay, 0, 13);
printField("Spark Sts", sparkStatusNames.at(ignA.coils34.spark_status), 0, 14); printField("Spark Sts", sparkStatusNames.at(ignA.coils34.spark_status), 0, 14);
printField("Peak P_IN", ignA.coils34.peak_p_in, 0, 15); // printField("Peak P_IN", ignA.coils34.peak_p_in, 0, 15);
printField("Peak P_OUT", ignA.coils34.peak_p_out, 0, 16); // printField("Peak P_OUT", ignA.coils34.peak_p_out, 0, 16);
printField("Peak N_IN", ignA.coils34.peak_n_in, 0, 17); // printField("Peak N_IN", ignA.coils34.peak_n_in, 0, 17);
printField("Peak N_OUT", ignA.coils34.peak_n_out, 0, 18); // printField("Peak N_OUT", ignA.coils34.peak_n_out, 0, 18);
printField("SoftStart ", softStartStatusNames.at(ignA.coils34.sstart_status), 0, 19); printField("Soft Start ", softStartStatusNames.at(ignA.coils34.sstart_status), 0, 19);
Serial.println("========== END ============="); Serial.println("========== END =============");
count = 0; Serial.println();
} auto delta = (esp_timer_get_time() - last) / 1000000.0f; //in seconds
delta = delta > 0 ? 1.0f / delta : 0; // Calculate frequency (Hz)
printField("Frequency (Hz)", delta, 0, 21);
printField("Queue Errors", (uint32_t)ignA.n_queue_errors, 0, 22);
last = esp_timer_get_time();
} else
{
setCursor(0, 22);
Serial.print("Waiting for data... ");;
Serial.flush();
} }
} }

View File

@@ -21,7 +21,7 @@ void rtIgnitionTask(void *pvParameters)
ADS1256 *adc = dev->adc_a; ADS1256 *adc = dev->adc_a;
PCA9555 *io = dev->io; PCA9555 *io = dev->io;
static ignitionBoxStatus ign_box_sts; ignitionBoxStatus ign_box_sts;
// Variables for ISR, static to be fixed in memory locations // Variables for ISR, static to be fixed in memory locations
static isrParams isr_params_t12p{ static isrParams isr_params_t12p{
@@ -69,9 +69,9 @@ void rtIgnitionTask(void *pvParameters)
LOG_WARN("rtTask Init Correct"); LOG_WARN("rtTask Init Correct");
// Global rt_task_ptr variables // Global rt_task_ptr variables
uint32_t it = 0;
uint32_t q_fail_count = 0;
bool first_cycle = true; bool first_cycle = true;
bool cycle12 = false;
bool cycle34 = false;
while (params->rt_running) while (params->rt_running)
{ {
@@ -107,13 +107,26 @@ void rtIgnitionTask(void *pvParameters)
} }
#endif #endif
// WAIT FOR SPARK TO HAPPEN // WAIT FOR SPARK TO HAPPEN OR TIMEOUT
auto spark_timeout = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(spark_timeout_max)); // auto spark_timeout = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(spark_timeout_max));
//auto spark_timeout = ulTaskNotifyTake(pdTRUE, portMAX_DELAY); // if (ign_box_sts.coils12.spark_ok || ign_box_sts.coils34.spark_ok) // otherwise timeout if none is set in the ISR
if (ign_box_sts.coils12.spark_ok || ign_box_sts.coils34.spark_ok) // otherwise timeout if none is set in the ISR // spark_flag = ign_box_sts.coils12.spark_ok ? SPARK_FLAG_12 : SPARK_FLAG_34;
spark_flag = ign_box_sts.coils12.spark_ok ? SPARK_FLAG_12 : SPARK_FLAG_34; // else
else // spark_flag = SPARK_FLAG_NIL;
spark_flag = SPARK_FLAG_NIL; // default value in case of timeout, to be set by ISR if spark event occours
// WAIT FOR SPARK TO HAPPEN OR TIMEOUT
auto sp = xTaskNotifyWait(
0x00, // non pulire all'ingresso
ULONG_MAX, // pulisci i primi 8 bit
&spark_flag, // valore ricevuto
pdMS_TO_TICKS(spark_timeout_max)); // wait for spark event or timeout
// timeout occurred, set spark flag to nil
if (sp == pdFALSE)
{
spark_flag = SPARK_FLAG_NIL; spark_flag = SPARK_FLAG_NIL;
}
#ifdef DEBUG #ifdef DEBUG
// LOG_INFO("Spark Flags: ", printBits(spark_flag).c_str()); // LOG_INFO("Spark Flags: ", printBits(spark_flag).c_str());
@@ -129,8 +142,6 @@ void rtIgnitionTask(void *pvParameters)
if ((pickup_flag == TRIG_FLAG_12P || pickup_flag == TRIG_FLAG_12N) && (spark_flag != SPARK_FLAG_12 && spark_flag != SPARK_FLAG_NIL)) if ((pickup_flag == TRIG_FLAG_12P || pickup_flag == TRIG_FLAG_12N) && (spark_flag != SPARK_FLAG_12 && spark_flag != SPARK_FLAG_NIL))
{ {
ign_box_sts.coils12.spark_status = ign_box_sts.coils34.spark_status = sparkStatus::SPARK_SYNC_FAIL; ign_box_sts.coils12.spark_status = ign_box_sts.coils34.spark_status = sparkStatus::SPARK_SYNC_FAIL;
// Save error on circular buffer and skip to next cycle //
LOG_ERROR("Spark Mismatch");
continue; continue;
} }
@@ -148,7 +159,6 @@ void rtIgnitionTask(void *pvParameters)
break; break;
} }
bool new_data = false;
switch (pickup_flag) switch (pickup_flag)
{ {
case TRIG_FLAG_12P: case TRIG_FLAG_12P:
@@ -171,8 +181,7 @@ void rtIgnitionTask(void *pvParameters)
coils->spark_status = sparkStatus::SPARK_NEG_WAIT; coils->spark_status = sparkStatus::SPARK_NEG_WAIT;
coils->sstart_status = softStartStatus::NORMAL; coils->sstart_status = softStartStatus::NORMAL;
} }
new_data = false; continue; // Do nothing more on positive pulse
break; // Do nothing more on positive pulse
} }
// CASES for NEGATIVE cycle triggering of pickup and sparks 12 & 34 // CASES for NEGATIVE cycle triggering of pickup and sparks 12 & 34
case TRIG_FLAG_12N: case TRIG_FLAG_12N:
@@ -193,7 +202,7 @@ void rtIgnitionTask(void *pvParameters)
// Timeout occourred, expected POSITIVE edge spark NOT OCCOURRED // Timeout occourred, expected POSITIVE edge spark NOT OCCOURRED
else if (spark_flag == SPARK_FLAG_NIL && expected_negative) else if (spark_flag == SPARK_FLAG_NIL && expected_negative)
{ {
coils->sstart_status = softStartStatus::NORMAL; coils->sstart_status = softStartStatus::ERROR;
coils->spark_status = sparkStatus::SPARK_NEG_FAIL; coils->spark_status = sparkStatus::SPARK_NEG_FAIL;
} }
// Timeout not occouured, unexpected negative edge spark // Timeout not occouured, unexpected negative edge spark
@@ -203,7 +212,11 @@ void rtIgnitionTask(void *pvParameters)
coils->spark_status = sparkStatus::SPARK_NEG_UNEXPECTED; coils->spark_status = sparkStatus::SPARK_NEG_UNEXPECTED;
} }
// Wait for finish of negative pulse to save data to buffer // Wait for finish of negative pulse to save data to buffer
new_data = true; coils->n_events++;
if (pickup_flag == TRIG_FLAG_12N)
cycle12 = true;
else
cycle34 = true;
break; break;
} }
default: default:
@@ -215,8 +228,10 @@ void rtIgnitionTask(void *pvParameters)
break; break;
} }
if (new_data) if (cycle12 && cycle34) // wait for both 12 and 34 cycles to complete before sending data to main loop and resetting peak detectors
{ {
cycle12 = false;
cycle34 = false;
// vTaskDelay(pdMS_TO_TICKS(1)); // delay 1ms to allow peak detectors to charge for negative cycle // vTaskDelay(pdMS_TO_TICKS(1)); // delay 1ms to allow peak detectors to charge for negative cycle
// read adc channels: pickup12, out12 [ pos + neg ] // read adc channels: pickup12, out12 [ pos + neg ]
if (adc) // read only if adc initialized if (adc) // read only if adc initialized
@@ -249,9 +264,9 @@ void rtIgnitionTask(void *pvParameters)
// send essage to main loop with ignition info, by copy so local static variable is ok // send essage to main loop with ignition info, by copy so local static variable is ok
if (rt_queue) if (rt_queue)
ign_box_sts.timestamp = esp_timer_get_time(); // update data timestamp ign_box_sts.timestamp = esp_timer_get_time(); // update data timestamp
if (xQueueSendToBack(rt_queue, (void *)&ign_box_sts, pdMS_TO_TICKS(1)) != pdPASS) if (xQueueSendToBack(rt_queue, (void *)&ign_box_sts, 0) != pdPASS)
{ {
q_fail_count++; ign_box_sts.n_queue_errors++;
LOG_ERROR("Failed to send to rt_queue"); LOG_ERROR("Failed to send to rt_queue");
} }
} }

View File

@@ -1,7 +1,7 @@
{ {
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [ "recommendations": [
"Jason2866.esp-decoder",
"pioarduino.pioarduino-ide",
"platformio.platformio-ide" "platformio.platformio-ide"
], ],
"unwantedRecommendations": [ "unwantedRecommendations": [

View File

@@ -10,7 +10,7 @@
[env:esp32-devtest-release] [env:esp32-devtest-release]
board = esp32dev board = esp32dev
platform = https://github.com/pioarduino/platform-espressif32/releases/download/55.03.37/platform-espressif32.zip platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip
framework = arduino framework = arduino
lib_deps = lib_deps =
hideakitai/DebugLog@^0.8.4 hideakitai/DebugLog@^0.8.4
@@ -21,8 +21,8 @@ build_type = release
[env:esp32-devtest-debug] [env:esp32-devtest-debug]
board = esp32dev board = esp32dev
platform = https://github.com/pioarduino/platform-espressif32/releases/download/55.03.37/platform-espressif32.zip platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip
framework = arduino
lib_deps = lib_deps =
hideakitai/DebugLog@^0.8.4 hideakitai/DebugLog@^0.8.4
board_build.flash_size = 4MB board_build.flash_size = 4MB

View File

@@ -15,7 +15,7 @@ static uint32_t count = 0;
#define SPARK_DLY_MIN 10 #define SPARK_DLY_MIN 10
#define SPARK_DLY_MAX 490 #define SPARK_DLY_MAX 490
#define PAUSE_LONG_MIN 10000 #define PAUSE_LONG_MIN 5000
#define PAUSE_LONG_MAX PAUSE_LONG_MIN*100 #define PAUSE_LONG_MAX PAUSE_LONG_MIN*100
void clearScreen(){ void clearScreen(){
@@ -46,15 +46,16 @@ static timerStatus stsA = {
.clock_period_us = (uint32_t)PERIOD_US, .clock_period_us = (uint32_t)PERIOD_US,
.pause_long_us = 10000, .pause_long_us = 10000,
.pause_short_us = 1000, .pause_short_us = 1000,
.coil_pulse_us = 500, .coil_pulse_us = 1000,
.spark_pulse_us = 50, .spark_pulse_us = 100,
.spark_delay_us = 10, .spark_delay_us = 50,
.main_task = NULL}; .main_task = NULL};
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
delay(1000);
LOG_ATTACH_SERIAL(Serial); LOG_ATTACH_SERIAL(Serial);
pinMode(PIN_TRIG_A12P, OUTPUT); pinMode(PIN_TRIG_A12P, OUTPUT);
@@ -98,6 +99,9 @@ void loop()
stsA.pause_long_us = (uint32_t)filtered; stsA.pause_long_us = (uint32_t)filtered;
LOG_INFO("Spark Delay uS: ", stsA.spark_delay_us, "\tSoft Start: ", stsA.soft_start ? "TRUE" : "FALSE"); LOG_INFO("Spark Delay uS: ", stsA.spark_delay_us, "\tSoft Start: ", stsA.soft_start ? "TRUE" : "FALSE");
LOG_INFO("Pause: ", (uint32_t)(stsA.pause_long_us / 1000), "ms"); LOG_INFO("Pause: ", (uint32_t)(stsA.pause_long_us / 1000), "ms");
LOG_INFO("Coil Pulse: ", stsA.coil_pulse_us, "us");
LOG_INFO("Spark Pulse: ", stsA.spark_pulse_us, "us");
delay(100); delay(100);
clearScreen(); clearScreen();

View File

@@ -19,5 +19,5 @@
#define SPARK_B34 5 #define SPARK_B34 5
// Pot // Pot
#define SPARK_DELAY_POT 12 #define SPARK_DELAY_POT 13
#define FREQ_POT 14 #define FREQ_POT 14