Working trigger + Spark recognition!!!

This commit is contained in:
Emanuele Trabattoni
2026-03-27 17:53:05 +01:00
parent a210d808da
commit b75014854d
3 changed files with 117 additions and 84 deletions

View File

@@ -13,7 +13,7 @@
//Global Variables and Flags
static bool rt_task_running = true;
const auto spark_timeout_max = 1; // convert to microsecond timer
const auto spark_timeout_max = 1000; // convert to microsecond timer
// RT task parameters
struct rtTaskParams {
@@ -33,43 +33,55 @@ void ignitionA_task(void *pvParameters) {
Devices* dev = (Devices*) params->dev;
ADS1256* adc = dev->adc_a;
// Global task variables
uint32_t pickup_flag = 0;
uint32_t spark_flag = 0;
// Ignition A Interrupts
attachInterrupt(TRIG_A12P, trig_isr_a, RISING);
attachInterrupt(TRIG_A34P, trig_isr_a, RISING);
attachInterrupt(TRIG_A12N, trig_isr_a, RISING);
attachInterrupt(TRIG_A34N, trig_isr_a, RISING);
attachInterrupt(SPARK_A12, spark_a, RISING);
attachInterrupt(SPARK_A34, spark_a, RISING);
attachInterruptArg(TRIG_A12P, trig_isr_a, (void*)TRIG_FLAG_A12P, RISING);
attachInterruptArg(TRIG_A12N, trig_isr_a, (void*)TRIG_FLAG_A12N, RISING);
attachInterruptArg(TRIG_A34P, trig_isr_a, (void*)TRIG_FLAG_A34P, RISING);
attachInterruptArg(TRIG_A34N, trig_isr_a, (void*)TRIG_FLAG_A34N, RISING);
attachInterruptArg(SPARK_A12, trig_isr_a, (void*)SPARK_FLAG_A12, RISING);
attachInterruptArg(SPARK_A34, trig_isr_a, (void*)SPARK_FLAG_A34, RISING);
uint32_t it=0;
while (rt_task_running) {
// Global task variables
uint32_t pickup_flag = 0;
uint32_t spark_flag = 0;
// WAIT FOR PICKUP SIGNAL
xTaskNotifyWait(
ULONG_MAX, // non pulire all'ingresso
0x00, // non pulire all'ingresso
ULONG_MAX, // pulisci i primi 8 bit
&pickup_flag, // valore ricevuto
portMAX_DELAY
);
LOG_ERROR("Pickup Interrupt Status", printBits(pickup_flag).c_str());
Serial.print("\033[2J"); // clear screen
Serial.print("\033[H"); // cursor home
LOG_INFO("Iteration [", it, "]");
//LOG_INFO("Pickup Flags: ", printBits(pickup_flag).c_str());
if (!names.contains(pickup_flag)) {
LOG_ERROR("Wrong Pickup Flag");
continue;
} else {
LOG_INFO("Pickup Trigger: ", names.at(pickup_flag));
}
// WAIT FOR SPARK TO HAPPEN
auto spark_timeout = xTaskNotifyWait(
ULONG_MAX, // non pulire all'ingresso
ULONG_MAX, // pulisci gli 8 bit successivi
&spark_flag, // valore ricevuto
spark_timeout_max
);
if (spark_timeout == pdPASS){ //otherwise timeout
LOG_ERROR("Spark Interrupt Status", printBits(spark_flag).c_str());
auto spark_timeout = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(5));
//auto spark_timeout = xTaskNotifyWait(
// 0x00, // non pulire all'ingresso
// ULONG_MAX, // pulisci gli 8 bit successivi
// &spark_flag, // valore ricevuto
// spark_timeout_max
//);
digitalWrite(POT_A_CS, HIGH);
if (ignA_status.spark12 || ignA_status.spark34) { //otherwise timeout
//LOG_INFO("Spark Flags: ", printBits(spark_flag).c_str());
spark_flag = ignA_status.spark12 ? SPARK_FLAG_A12 : SPARK_FLAG_A34;
if (!names.contains(spark_flag)) {
continue;
LOG_ERROR("Wrong Spark Flag");
} else {
LOG_INFO("Spark Trigger:", names.at(spark_flag));
}
@@ -77,27 +89,32 @@ void ignitionA_task(void *pvParameters) {
LOG_INFO("Spark Timeout");
}
xTaskNotifyStateClear(NULL);
ulTaskNotifyValueClear(NULL, 0xFFFFFFFF);
// A trigger from pickup 12 is followed by a spark event on 34 or vice versa pickup 34 triggers spark on 12
if ((pickup_flag == TRIG_FLAG_A12P || pickup_flag == TRIG_FLAG_A12N) && spark_flag != SPARK_A12) {
if ((pickup_flag == TRIG_FLAG_A12P || pickup_flag == TRIG_FLAG_A12N) && spark_flag != SPARK_FLAG_A12) {
ignA_status.coils12.spark_status = ignA_status.coils34.spark_status = sparkStatus::SPARK_SYNC_FAIL;
// Save error on circular buffer and skip to next cycle //
LOG_ERROR("Spark Mismatch");
continue;
}
const uint32_t combined_flags = pickup_flag & spark_flag;
coilsStatus* c;
switch (combined_flags)
switch (pickup_flag)
{
case TRIG_A12P:
case TRIG_A12N:
case SPARK_A12:
case TRIG_FLAG_A12P:
case TRIG_FLAG_A12N:
//case SPARK_FLAG_A12:
c = &ignA_status.coils12;
case TRIG_A34P:
case TRIG_A34N:
case SPARK_A34:
break;
case TRIG_FLAG_A34P:
case TRIG_FLAG_A34N:
//case SPARK_FLAG_A34:
c = &ignA_status.coils34;
break;
}
bool new_data = false;
switch (pickup_flag) {
@@ -112,6 +129,7 @@ void ignitionA_task(void *pvParameters) {
c->soft_start_status = softStartStatus::NORMAL; // because spark on positive edge
c->spark_status = sparkStatus::SPARK_POS_OK; // do not wait for spark on negative edge
LOG_INFO("Trigger Spark POSITIVE");
LOG_INFO("Spark12 Delay Timer: ", (int)c->spark_delay);
}
// Timeout occourred, expected POSITIVE edge spark NOT OCCOURRED
else if (spark_timeout == pdFAIL) {
@@ -133,6 +151,7 @@ void ignitionA_task(void *pvParameters) {
c->soft_start_status = softStartStatus::SOFT_START;
c->spark_status == sparkStatus::SPARK_NEG_OK;
LOG_INFO("Trigger Spark NEGATIVE");
LOG_INFO("Spark12 Delay Timer: ", (int)ignA_status.coils12.spark_delay);
}
// Timeout occourred, expected POSITIVE edge spark NOT OCCOURRED
else if (spark_timeout == pdFAIL && expected_negative12) {
@@ -150,7 +169,11 @@ void ignitionA_task(void *pvParameters) {
}
default:
LOG_ERROR("Invalid A Interrupt");
break;
}
it++;
digitalWrite(POT_A_CS, LOW);
continue;
if (new_data) {
vTaskDelay(pdMS_TO_TICKS(1)); // delay 1ms to allow peak detectors to charge for negative cycle
@@ -180,11 +203,11 @@ void ignitionA_task(void *pvParameters) {
}
}
LOG_WARN("Ending realTime Task");
// Ignition A Interrupts DETACH
detachInterrupt(TRIG_A12P);
detachInterrupt(TRIG_A34P);
detachInterrupt(TRIG_A12N);
detachInterrupt(TRIG_A34P);
detachInterrupt(TRIG_A34N);
detachInterrupt(SPARK_A12);
detachInterrupt(SPARK_A34);