Fixed spark timing compute

This commit is contained in:
Emanuele Trabattoni
2026-03-26 14:09:55 +01:00
parent b573f64a39
commit 68ed8a2282
3 changed files with 143 additions and 132 deletions

View File

@@ -12,108 +12,104 @@
#include <tasks.h>
#include <devices.h>
void setup()
{
delay(250);
Serial.begin(115200);
void setup() {
delay(250);
Serial.begin(115200);
// Setup Logger
LOG_ATTACH_SERIAL(Serial);
LOG_SET_LEVEL(DebugLogLevel::LVL_INFO);
// Setup Logger
LOG_ATTACH_SERIAL(Serial);
LOG_SET_LEVEL(DebugLogLevel::LVL_INFO);
// Print Processor Info
LOG_INFO("ESP32 Chip:", ESP.getChipModel());
LOG_INFO("ESP32 PSram:", ESP.getPsramSize());
LOG_INFO("ESP32 Flash:", ESP.getFlashChipSize());
LOG_INFO("ESP32 Heap:", ESP.getHeapSize());
LOG_INFO("ESP32 Sketch:", ESP.getFreeSketchSpace());
// Print Processor Info
LOG_INFO("ESP32 Chip:", ESP.getChipModel());
LOG_INFO("ESP32 PSram:", ESP.getPsramSize());
LOG_INFO("ESP32 Flash:", ESP.getFlashChipSize());
LOG_INFO("ESP32 Heap:", ESP.getHeapSize());
LOG_INFO("ESP32 Sketch:", ESP.getFreeSketchSpace());
// Initialize Interrupt pins on coil detectors
pinMode(TRIG_A12P, INPUT_PULLDOWN);
pinMode(TRIG_A12N, INPUT_PULLDOWN);
pinMode(TRIG_A34P, INPUT_PULLDOWN);
pinMode(TRIG_A34N, INPUT_PULLDOWN);
pinMode(TRIG_B12P, INPUT_PULLDOWN);
pinMode(TRIG_B12N, INPUT_PULLDOWN);
pinMode(TRIG_B34P, INPUT_PULLDOWN);
pinMode(TRIG_B34N, INPUT_PULLDOWN);
initTriggerPinMapping();
// Initialize Interrupt pins on spark detectors
pinMode(SPARK_A12, INPUT_PULLDOWN);
pinMode(SPARK_A34, INPUT_PULLDOWN);
pinMode(SPARK_B12, INPUT_PULLDOWN);
pinMode(SPARK_B34, INPUT_PULLDOWN);
initSparkPinMapping();
// Initialize Interrupt pins on coil detectors
pinMode(TRIG_A12P, INPUT_PULLDOWN);
pinMode(TRIG_A12N, INPUT_PULLDOWN);
pinMode(TRIG_A34P, INPUT_PULLDOWN);
pinMode(TRIG_A34N, INPUT_PULLDOWN);
pinMode(TRIG_B12P, INPUT_PULLDOWN);
pinMode(TRIG_B12N, INPUT_PULLDOWN);
pinMode(TRIG_B34P, INPUT_PULLDOWN);
pinMode(TRIG_B34N, INPUT_PULLDOWN);
initTriggerPinMapping();
// Initialize Interrupt pins on spark detectors
pinMode(SPARK_A12, INPUT_PULLDOWN);
pinMode(SPARK_A34, INPUT_PULLDOWN);
pinMode(SPARK_B12, INPUT_PULLDOWN);
pinMode(SPARK_B34, INPUT_PULLDOWN);
initSparkPinMapping();
// 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);
// Ignition B Interrupts
attachInterrupt(TRIG_B12P, trig_isr_b, RISING);
attachInterrupt(TRIG_B34P, trig_isr_b, RISING);
attachInterrupt(TRIG_B12N, trig_isr_b, RISING);
attachInterrupt(TRIG_B34N, trig_isr_b, RISING);
attachInterrupt(SPARK_B12, spark_b, RISING);
attachInterrupt(SPARK_B34, spark_b, RISING);
// 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);
// Ignition B Interrupts
attachInterrupt(TRIG_B12P, trig_isr_b, RISING);
attachInterrupt(TRIG_B34P, trig_isr_b, RISING);
attachInterrupt(TRIG_B12N, trig_isr_b, RISING);
attachInterrupt(TRIG_B34N, trig_isr_b, RISING);
attachInterrupt(SPARK_B12, spark_b, RISING);
attachInterrupt(SPARK_B34, spark_b, RISING);
// Init SPI interface
SPI.begin();
// Init SPI interface
SPI.begin();
}
void loop() {
// global variables
bool running = true;
Devices dev;
void loop()
{
// global variables
bool running = true;
Devices dev;
// Init devices
dev.adc = new ADS1256(ADC_DRDY, ADC_RST, ADC_SYNC, ADC_CS, 2.5, &SPI);
dev.adc->InitializeADC();
dev.adc->setPGA(PGA_1);
dev.adc->setDRATE(DRATE_1000SPS);
// Init devices
dev.adc = new ADS1256(ADC_DRDY, ADC_RST, ADC_SYNC, ADC_CS, 2.5, &SPI);
dev.adc->InitializeADC();
dev.adc->setPGA(PGA_1);
dev.adc->setDRATE(DRATE_1000SPS);
// Ignition A on Core 0
// Ignition A on Core 0
auto ignA_task_success = xTaskCreatePinnedToCore(
ignitionA_task,
"ignitionA_task",
TASK_STACK,
(void*) &dev,
TASK_PRIORITY,
(void *)&dev,
TASK_PRIORITY,
&trigA_TaskHandle,
CORE_0
);
CORE_0);
// Ignition A on Core 1
// Ignition B on Core 1
auto ignB_task_success = xTaskCreatePinnedToCore(
ignitionB_task,
"ignitionB_task",
TASK_STACK,
(void*) &dev,
(void *)&dev,
TASK_PRIORITY, // priorità leggermente più alta
&trigA_TaskHandle,
CORE_1
);
CORE_1);
if ((ignA_task_success && ignB_task_success) != pdPASS){
LOG_ERROR("Unble to initialize ISR task");
if ((ignA_task_success && ignB_task_success) != pdPASS)
{
LOG_ERROR("Unble to initialize ISR task");
}
LOG_INFO("Real Time Tasks A&B initialized");
////////////////////// MAIN LOOP //////////////////////
while (running) {
while (running)
{
}
if (trigA_TaskHandle)
vTaskDelete(trigA_TaskHandle);
vTaskDelete(trigA_TaskHandle);
if (trigB_TaskHandle)
vTaskDelete(trigB_TaskHandle);
vTaskDelete(trigB_TaskHandle);
////////////////////// MAIN LOOP //////////////////////
}