revert on double task wait, normal and soft start working
This commit is contained in:
@@ -26,7 +26,7 @@ void trig_isr(void *arg)
|
||||
{
|
||||
case TRIG_FLAG_12P:
|
||||
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
|
||||
isr_firing_count++;
|
||||
@@ -35,10 +35,9 @@ void trig_isr(void *arg)
|
||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
}
|
||||
break;
|
||||
|
||||
case TRIG_FLAG_34P:
|
||||
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
|
||||
isr_firing_count++;
|
||||
@@ -48,24 +47,26 @@ void trig_isr(void *arg)
|
||||
}
|
||||
break;
|
||||
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
|
||||
{
|
||||
box->coils34.spark_ok = false;
|
||||
box->coils12.spark_ok = true;
|
||||
box->coils12.spark_time = time_us;
|
||||
isr_firing_count = 0; // reset trigger timeout counter on spark event
|
||||
vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken);
|
||||
}
|
||||
// if (isr_firing_count > 0) // only consider spark if a trigger has been detected, otherwise noise on spark pin can cause false positives
|
||||
{
|
||||
isr_firing_count = 0; // reset trigger timeout counter on spark event
|
||||
box->coils34.spark_ok = false;
|
||||
box->coils12.spark_ok = true;
|
||||
box->coils12.spark_time = time_us;
|
||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
// vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken);
|
||||
}
|
||||
break;
|
||||
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
|
||||
{
|
||||
box->coils12.spark_ok = false;
|
||||
box->coils34.spark_ok = true;
|
||||
box->coils34.spark_time = time_us;
|
||||
isr_firing_count = 0; // reset trigger timeout counter on spark event
|
||||
vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken);
|
||||
}
|
||||
// if (isr_firing_count > 0) // only consider spark if a trigger has been detected, otherwise noise on spark pin can cause false positives
|
||||
{
|
||||
isr_firing_count = 0; // reset trigger timeout counter on spark event
|
||||
box->coils12.spark_ok = false;
|
||||
box->coils34.spark_ok = true;
|
||||
box->coils34.spark_time = time_us;
|
||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
// vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
@@ -63,36 +63,41 @@ static const std::map<const sparkStatus, const char *> sparkStatusNames = {
|
||||
enum softStartStatus
|
||||
{
|
||||
NORMAL,
|
||||
SOFT_START
|
||||
SOFT_START,
|
||||
ERROR,
|
||||
};
|
||||
|
||||
const std::map<const softStartStatus, const char *> softStartStatusNames = {
|
||||
{NORMAL, "NORMAL"},
|
||||
{SOFT_START, "SOFT_START"},
|
||||
{ERROR, "ERROR"},
|
||||
};
|
||||
|
||||
struct coilsStatus
|
||||
{
|
||||
int64_t trig_time;
|
||||
int64_t spark_time;
|
||||
int64_t spark_delay;
|
||||
sparkStatus spark_status;
|
||||
softStartStatus sstart_status;
|
||||
float peak_p_in, peak_n_in;
|
||||
float peak_p_out, peak_n_out;
|
||||
float trigger_spark;
|
||||
bool spark_ok;
|
||||
int64_t trig_time = 0;
|
||||
int64_t spark_time = 0;
|
||||
int64_t spark_delay = 0; // in microseconds
|
||||
sparkStatus spark_status = sparkStatus::SPARK_POS_OK;
|
||||
softStartStatus sstart_status = softStartStatus::NORMAL;
|
||||
float peak_p_in = 0.0, peak_n_in = 0.0;
|
||||
float peak_p_out = 0.0, peak_n_out = 0.0;
|
||||
float trigger_spark = 0.0;
|
||||
bool spark_ok = false;
|
||||
uint32_t n_events = 0;
|
||||
|
||||
};
|
||||
|
||||
// Task internal Status
|
||||
struct ignitionBoxStatus
|
||||
{
|
||||
int64_t timestamp;
|
||||
int64_t timestamp = 0;
|
||||
// coils pairs for each ignition
|
||||
coilsStatus coils12;
|
||||
coilsStatus coils34;
|
||||
// voltage from generator
|
||||
float volts_gen = 0.0;
|
||||
uint32_t n_queue_errors = 0;
|
||||
};
|
||||
|
||||
struct isrParams
|
||||
|
||||
@@ -168,46 +168,58 @@ void loop()
|
||||
LOG_INFO("Real Time Tasks A & B initialized");
|
||||
|
||||
////////////////////// MAIN LOOP //////////////////////
|
||||
uint32_t count(0);
|
||||
clearScreen();
|
||||
setCursor(0, 0);
|
||||
ignitionBoxStatus ignA;
|
||||
int64_t last = esp_timer_get_time();
|
||||
uint32_t missed_firings12 = 0;
|
||||
uint32_t missed_firings34 = 0;
|
||||
|
||||
while (running)
|
||||
{
|
||||
ignitionBoxStatus ignA;
|
||||
if (xQueueReceive(rt_taskA_queue, &ignA, pdMS_TO_TICKS(100)) == pdTRUE)
|
||||
if (xQueueReceive(rt_taskA_queue, &ignA, pdMS_TO_TICKS(1000)) == pdTRUE)
|
||||
{
|
||||
if (count++ % 10000 == 0)
|
||||
{
|
||||
firstRun = true;
|
||||
clearScreen();
|
||||
setCursor(0, 0);
|
||||
printField("++ Timestamp", (uint32_t)ignA.timestamp, 0, 0);
|
||||
Serial.println("========== Coils 12 =============");
|
||||
printField("Pickup Tim", (uint32_t)ignA.coils12.trig_time, 0, 1);
|
||||
printField("Spark Tim", (uint32_t)ignA.coils12.spark_time, 0, 2);
|
||||
printField("Spark Dly", (uint32_t)ignA.coils12.spark_delay, 0, 3);
|
||||
printField("Spark Sts", sparkStatusNames.at(ignA.coils12.spark_status), 0, 4);
|
||||
printField("Peak P_IN", ignA.coils12.peak_p_in, 0, 5);
|
||||
printField("Peak P_OUT", ignA.coils12.peak_p_out, 0, 6);
|
||||
printField("Peak N_IN", ignA.coils12.peak_n_in, 0, 7);
|
||||
printField("Peak N_OUT", ignA.coils12.peak_n_out, 0, 8);
|
||||
printField("SoftStart ", softStartStatusNames.at(ignA.coils12.sstart_status), 0, 9);
|
||||
if (ignA.coils12.spark_status == sparkStatus::SPARK_NEG_FAIL || ignA.coils12.spark_status == sparkStatus::SPARK_POS_FAIL)
|
||||
missed_firings12++;
|
||||
if (ignA.coils34.spark_status == sparkStatus::SPARK_POS_FAIL || ignA.coils34.spark_status == sparkStatus::SPARK_NEG_FAIL)
|
||||
missed_firings34++;
|
||||
clearScreen();
|
||||
setCursor(0, 0);
|
||||
printField("++ Timestamp", (uint32_t)ignA.timestamp, 0, 0);
|
||||
Serial.println("========== Coils 12 =============");
|
||||
printField("Events", (uint32_t)ignA.coils12.n_events, 0, 1);
|
||||
printField("Missed Firing", missed_firings12, 0, 2);
|
||||
printField("Spark Dly", (uint32_t)ignA.coils12.spark_delay, 0, 3);
|
||||
printField("Spark Sts", sparkStatusNames.at(ignA.coils12.spark_status), 0, 4);
|
||||
// printField("Peak P_IN", ignA.coils12.peak_p_in, 0, 5);
|
||||
// printField("Peak P_OUT", ignA.coils12.peak_p_out, 0, 6);
|
||||
// printField("Peak N_IN", ignA.coils12.peak_n_in, 0, 7);
|
||||
// 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 =============");
|
||||
printField("Pickup Tim", (uint32_t)ignA.coils34.trig_time, 0, 11);
|
||||
printField("Spark Tim", (uint32_t)ignA.coils34.spark_time, 0, 12);
|
||||
printField("Spark Dly", (uint32_t)ignA.coils34.spark_delay, 0, 13);
|
||||
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_OUT", ignA.coils34.peak_p_out, 0, 16);
|
||||
printField("Peak N_IN", ignA.coils34.peak_n_in, 0, 17);
|
||||
printField("Peak N_OUT", ignA.coils34.peak_n_out, 0, 18);
|
||||
printField("SoftStart ", softStartStatusNames.at(ignA.coils34.sstart_status), 0, 19);
|
||||
Serial.println("========== Coils 34 =============");
|
||||
printField("Events", (uint32_t)ignA.coils34.n_events, 0, 11);
|
||||
printField("Missed Firing", missed_firings34, 0, 12);
|
||||
printField("Spark Dly", (uint32_t)ignA.coils34.spark_delay, 0, 13);
|
||||
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_OUT", ignA.coils34.peak_p_out, 0, 16);
|
||||
// printField("Peak N_IN", ignA.coils34.peak_n_in, 0, 17);
|
||||
// printField("Peak N_OUT", ignA.coils34.peak_n_out, 0, 18);
|
||||
printField("Soft Start ", softStartStatusNames.at(ignA.coils34.sstart_status), 0, 19);
|
||||
|
||||
Serial.println("========== END =============");
|
||||
count = 0;
|
||||
}
|
||||
Serial.println("========== END =============");
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ void rtIgnitionTask(void *pvParameters)
|
||||
ADS1256 *adc = dev->adc_a;
|
||||
PCA9555 *io = dev->io;
|
||||
|
||||
static ignitionBoxStatus ign_box_sts;
|
||||
ignitionBoxStatus ign_box_sts;
|
||||
|
||||
// Variables for ISR, static to be fixed in memory locations
|
||||
static isrParams isr_params_t12p{
|
||||
@@ -69,9 +69,9 @@ void rtIgnitionTask(void *pvParameters)
|
||||
|
||||
LOG_WARN("rtTask Init Correct");
|
||||
// Global rt_task_ptr variables
|
||||
uint32_t it = 0;
|
||||
uint32_t q_fail_count = 0;
|
||||
bool first_cycle = true;
|
||||
bool cycle12 = false;
|
||||
bool cycle34 = false;
|
||||
|
||||
while (params->rt_running)
|
||||
{
|
||||
@@ -107,13 +107,26 @@ void rtIgnitionTask(void *pvParameters)
|
||||
}
|
||||
#endif
|
||||
|
||||
// WAIT FOR SPARK TO HAPPEN
|
||||
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
|
||||
spark_flag = ign_box_sts.coils12.spark_ok ? SPARK_FLAG_12 : SPARK_FLAG_34;
|
||||
else
|
||||
// WAIT FOR SPARK TO HAPPEN OR TIMEOUT
|
||||
// auto spark_timeout = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(spark_timeout_max));
|
||||
// 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;
|
||||
// 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;
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
// 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))
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -148,7 +159,6 @@ void rtIgnitionTask(void *pvParameters)
|
||||
break;
|
||||
}
|
||||
|
||||
bool new_data = false;
|
||||
switch (pickup_flag)
|
||||
{
|
||||
case TRIG_FLAG_12P:
|
||||
@@ -171,8 +181,7 @@ void rtIgnitionTask(void *pvParameters)
|
||||
coils->spark_status = sparkStatus::SPARK_NEG_WAIT;
|
||||
coils->sstart_status = softStartStatus::NORMAL;
|
||||
}
|
||||
new_data = false;
|
||||
break; // Do nothing more on positive pulse
|
||||
continue; // Do nothing more on positive pulse
|
||||
}
|
||||
// CASES for NEGATIVE cycle triggering of pickup and sparks 12 & 34
|
||||
case TRIG_FLAG_12N:
|
||||
@@ -193,7 +202,7 @@ void rtIgnitionTask(void *pvParameters)
|
||||
// Timeout occourred, expected POSITIVE edge spark NOT OCCOURRED
|
||||
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;
|
||||
}
|
||||
// Timeout not occouured, unexpected negative edge spark
|
||||
@@ -203,7 +212,11 @@ void rtIgnitionTask(void *pvParameters)
|
||||
coils->spark_status = sparkStatus::SPARK_NEG_UNEXPECTED;
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
default:
|
||||
@@ -215,8 +228,10 @@ void rtIgnitionTask(void *pvParameters)
|
||||
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
|
||||
// read adc channels: pickup12, out12 [ pos + neg ]
|
||||
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
|
||||
if (rt_queue)
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user