refactor continued, at least it compiles
This commit is contained in:
@@ -18,7 +18,7 @@
|
||||
|
||||
// Defines to enable channel B
|
||||
#define CH_B_ENABLE
|
||||
//#define TEST
|
||||
// #define TEST
|
||||
|
||||
// Debug Defines
|
||||
#define WIFI_SSID "AstroRotaxMonitor"
|
||||
@@ -52,7 +52,7 @@ void setup()
|
||||
IPAddress gateway(10, 11, 12, 1);
|
||||
IPAddress subnet(255, 255, 255, 0);
|
||||
WiFi.softAPConfig(local_IP, gateway, subnet);
|
||||
WiFi.setTxPower(WIFI_POWER_5dBm); // reduce wifi power
|
||||
WiFi.setTxPower(WIFI_POWER_5dBm); // reduce wifi power
|
||||
if (WiFi.softAP(WIFI_SSID, WIFI_PASSWORD))
|
||||
{
|
||||
LOG_INFO("WiFi AP Mode Started");
|
||||
@@ -81,71 +81,9 @@ void loop()
|
||||
RGBled led;
|
||||
led.setStatus(RGBled::LedStatus::INIT);
|
||||
bool running = true;
|
||||
const uint32_t max_queue = 128;
|
||||
const uint32_t filter_k = 10;
|
||||
std::mutex fs_mutex;
|
||||
|
||||
PSRAMVector<ignitionBoxStatus> ignA_history_0(max_history);
|
||||
PSRAMVector<ignitionBoxStatus> ignA_history_1(max_history);
|
||||
auto *active_history_A = &ignA_history_0;
|
||||
auto *writable_history_A = &ignA_history_1;
|
||||
|
||||
#ifdef CH_B_ENABLE
|
||||
PSRAMVector<ignitionBoxStatus> ignB_history_0(max_history);
|
||||
PSRAMVector<ignitionBoxStatus> ignB_history_1(max_history);
|
||||
auto *active_history_B = &ignB_history_0;
|
||||
auto *writable_history_B = &ignB_history_1;
|
||||
#endif
|
||||
|
||||
// Resources Initialization
|
||||
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 = std::make_shared<Devices>(dev),
|
||||
.rt_queue = rt_taskA_queue,
|
||||
.rt_int = rtTaskInterruptParams{
|
||||
.isr_ptr = &trig_isr_A,
|
||||
.trig_pin_12p = TRIG_PIN_A12P,
|
||||
.trig_pin_12n = TRIG_PIN_A12N,
|
||||
.trig_pin_34p = TRIG_PIN_A34P,
|
||||
.trig_pin_34n = TRIG_PIN_A34N,
|
||||
.spark_pin_12 = SPARK_PIN_A12,
|
||||
.spark_pin_34 = SPARK_PIN_A34},
|
||||
.rt_io = rtTaskIOParams{.rst_io_peak = 0, .rst_io_sh = 0}};
|
||||
|
||||
#ifdef CH_B_ENABLE
|
||||
rtTaskParams taskB_params{
|
||||
.rt_running = true,
|
||||
.dev = &dev,
|
||||
.rt_queue = rt_taskB_queue,
|
||||
.rt_int = rtTaskInterruptParams{
|
||||
.isr_ptr = &trig_isr_B,
|
||||
.trig_pin_12p = TRIG_PIN_B12P,
|
||||
.trig_pin_12n = TRIG_PIN_B12N,
|
||||
.trig_pin_34p = TRIG_PIN_B34P,
|
||||
.trig_pin_34n = TRIG_PIN_B34N,
|
||||
.spark_pin_12 = SPARK_PIN_B12,
|
||||
.spark_pin_34 = SPARK_PIN_B34},
|
||||
.rt_io = rtTaskIOParams{.rst_io_peak = SS_FORCE_A, .rst_io_sh = SS_INIBHIT_A12}};
|
||||
#endif
|
||||
|
||||
if (!rt_taskA_queue || !rt_taskB_queue)
|
||||
{
|
||||
LOG_ERROR("Unable To Create task queues");
|
||||
LOG_ERROR("5 seconds to restart...");
|
||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||
esp_restart();
|
||||
}
|
||||
else
|
||||
LOG_DEBUG("Task Variables OK");
|
||||
|
||||
// Spi ok flags
|
||||
//////// INIT SPI PORTS ////////
|
||||
bool spiA_ok = true;
|
||||
bool spiB_ok = true;
|
||||
// Init 2 SPI interfaces
|
||||
@@ -153,11 +91,9 @@ void loop()
|
||||
spiA_ok = SPI_A.begin(SPI_A_SCK, SPI_A_MISO, SPI_A_MOSI);
|
||||
SPI_A.setDataMode(SPI_MODE1); // ADS1256 requires SPI mode 1
|
||||
#ifdef CH_B_ENABLE
|
||||
#ifndef TEST
|
||||
SPIClass SPI_B(HSPI);
|
||||
spiB_ok = SPI_B.begin(SPI_B_SCK, SPI_B_MISO, SPI_B_MOSI);
|
||||
SPI_B.setDataMode(SPI_MODE1); // ADS1256 requires SPI mode 1
|
||||
#endif
|
||||
#endif
|
||||
if (!spiA_ok || !spiB_ok)
|
||||
{
|
||||
@@ -168,52 +104,91 @@ void loop()
|
||||
}
|
||||
LOG_DEBUG("Init SPI OK");
|
||||
|
||||
#ifndef TEST
|
||||
// Init ADC_A
|
||||
dev.adc_a = new ADS1256(ADC_A_DRDY, ADS1256::PIN_UNUSED, ADS1256::PIN_UNUSED, ADC_A_CS, 2.5, &SPI_A);
|
||||
dev.adc_a->InitializeADC();
|
||||
dev.adc_a->setPGA(PGA_1);
|
||||
dev.adc_a->setDRATE(DRATE_7500SPS);
|
||||
#endif
|
||||
#ifdef CH_B_ENABLE
|
||||
#ifndef TEST
|
||||
// Init ADC_B
|
||||
dev.adc_a = new ADS1256(ADC_B_DRDY, ADS1256::PIN_UNUSED, ADS1256::PIN_UNUSED, ADC_B_CS, 2.5, &SPI_B);
|
||||
dev.adc_a->InitializeADC();
|
||||
dev.adc_a->setPGA(PGA_1);
|
||||
dev.adc_a->setDRATE(DRATE_1000SPS);
|
||||
#endif
|
||||
#endif
|
||||
// Resources Initialization
|
||||
std::shared_ptr<Devices> dev = std::make_shared<Devices>();
|
||||
dev->m_spi_a = std::make_unique<SPIClass>(SPI_A);
|
||||
dev->m_spi_b = std::make_unique<SPIClass>(SPI_B);
|
||||
|
||||
LOG_DEBUG("Init ADC OK");
|
||||
// Init ADC_A
|
||||
dev->m_adc_a = std::make_unique<ADS1256>(ADC_A_DRDY, ADS1256::PIN_UNUSED, ADS1256::PIN_UNUSED, ADC_A_CS, 2.5, &SPI_A);
|
||||
dev->m_adc_b = std::make_unique<ADS1256>(ADC_B_DRDY, ADS1256::PIN_UNUSED, ADS1256::PIN_UNUSED, ADC_B_CS, 2.5, &SPI_B);
|
||||
|
||||
dev->m_adc_a->InitializeADC();
|
||||
dev->m_adc_a->setPGA(PGA_1);
|
||||
dev->m_adc_a->setDRATE(DRATE_7500SPS);
|
||||
|
||||
dev->m_adc_b->InitializeADC();
|
||||
dev->m_adc_b->setPGA(PGA_1);
|
||||
dev->m_adc_b->setDRATE(DRATE_7500SPS);
|
||||
|
||||
const rtIgnitionTask::rtTaskParams taskA_params{
|
||||
.rt_running = true,
|
||||
.name = "rtIgnTask_A",
|
||||
.rt_stack_size = RT_TASK_STACK,
|
||||
.rt_priority = RT_TASK_PRIORITY,
|
||||
.rt_int = rtIgnitionTask::rtTaskInterruptParams{
|
||||
.isr_ptr = &trig_isr_A,
|
||||
.trig_pin_12p = TRIG_PIN_A12P,
|
||||
.trig_pin_12n = TRIG_PIN_A12N,
|
||||
.trig_pin_34p = TRIG_PIN_A34P,
|
||||
.trig_pin_34n = TRIG_PIN_A34N,
|
||||
.spark_pin_12 = SPARK_PIN_A12,
|
||||
.spark_pin_34 = SPARK_PIN_A34},
|
||||
.rt_io = rtIgnitionTask::rtTaskIOParams{
|
||||
.pot_cs_12 = POT_CS_A12,
|
||||
.pot_cs_34 = POT_CS_A34,
|
||||
.ss_force = SS_FORCE_A,
|
||||
.ss_inhibit_12 = SS_INIBHIT_A12,
|
||||
.ss_inhibit_34 = SS_INHIBIT_A34,
|
||||
.sh_disch_12 = SH_DISCH_A12,
|
||||
.sh_disch_34 = SH_DISCH_A34,
|
||||
.sh_arm_12 = SH_ARM_A12,
|
||||
.sh_arm_34 = SH_ARM_A34,
|
||||
.relay_in_12 = RELAY_IN_A12,
|
||||
.relay_in_34 = RELAY_OUT_A12,
|
||||
.relay_out_12 = RELAY_IN_A34,
|
||||
.relay_out_34 = RELAY_OUT_A34,
|
||||
},
|
||||
.rt_queue = nullptr,
|
||||
.dev = dev};
|
||||
|
||||
const rtIgnitionTask::rtTaskParams taskB_params{
|
||||
.rt_running = true,
|
||||
.name = "rtIgnTask_B",
|
||||
.rt_stack_size = RT_TASK_STACK,
|
||||
.rt_priority = RT_TASK_PRIORITY,
|
||||
.rt_int = rtIgnitionTask::rtTaskInterruptParams{
|
||||
.isr_ptr = &trig_isr_B,
|
||||
.trig_pin_12p = TRIG_PIN_B12P,
|
||||
.trig_pin_12n = TRIG_PIN_B12N,
|
||||
.trig_pin_34p = TRIG_PIN_B34P,
|
||||
.trig_pin_34n = TRIG_PIN_B34N,
|
||||
.spark_pin_12 = SPARK_PIN_B12,
|
||||
.spark_pin_34 = SPARK_PIN_B34},
|
||||
.rt_io = rtIgnitionTask::rtTaskIOParams{
|
||||
.pot_cs_12 = POT_CS_B12,
|
||||
.pot_cs_34 = POT_CS_B34,
|
||||
.ss_force = SS_FORCE_B,
|
||||
.ss_inhibit_12 = SS_INIBHIT_B12,
|
||||
.ss_inhibit_34 = SS_INHIBIT_B34,
|
||||
.sh_disch_12 = SH_DISCH_B12,
|
||||
.sh_disch_34 = SH_DISCH_B34,
|
||||
.sh_arm_12 = SH_ARM_B12,
|
||||
.sh_arm_34 = SH_ARM_B34,
|
||||
.relay_in_12 = RELAY_IN_B12,
|
||||
.relay_in_34 = RELAY_OUT_B12,
|
||||
.relay_out_12 = RELAY_IN_B34,
|
||||
.relay_out_34 = RELAY_OUT_B34,
|
||||
},
|
||||
.rt_queue = nullptr,
|
||||
.dev = dev};
|
||||
|
||||
auto task_A = rtIgnitionTask(taskA_params, 1024, 128, CORE_0, fs_mutex);
|
||||
auto task_B = rtIgnitionTask(taskA_params, 1024, 128, CORE_1, fs_mutex);
|
||||
|
||||
// Ignition A on Core 0
|
||||
auto ignA_task_success = pdPASS;
|
||||
ignA_task_success = xTaskCreatePinnedToCore(
|
||||
rtIgnitionTask_realtime,
|
||||
"rtTask_A",
|
||||
RT_TASK_STACK,
|
||||
(void *)&taskA_params,
|
||||
RT_TASK_PRIORITY,
|
||||
&trigA_TaskHandle,
|
||||
CORE_0);
|
||||
delay(100); // give some time to the thread to start
|
||||
|
||||
// Ignition B on Core 1
|
||||
auto ignB_task_success = pdPASS;
|
||||
|
||||
#ifdef CH_B_ENABLE
|
||||
ignB_task_success = xTaskCreatePinnedToCore(
|
||||
rtIgnitionTask_realtime,
|
||||
"rtTask_B",
|
||||
RT_TASK_STACK,
|
||||
(void *)&taskB_params,
|
||||
RT_TASK_PRIORITY, // priorità leggermente più alta
|
||||
&trigB_TaskHandle,
|
||||
CORE_1);
|
||||
delay(100); // give some time to the thread to start
|
||||
#endif
|
||||
|
||||
auto ignA_task_success = task_A.getStatus() == rtIgnitionTask::OK ? pdPASS : pdFAIL;
|
||||
auto ignB_task_success = task_B.getStatus() == rtIgnitionTask::OK ? pdPASS : pdFAIL;
|
||||
if (ignA_task_success != pdPASS || ignB_task_success != pdPASS)
|
||||
{
|
||||
LOG_ERROR("Unable to initialize ISR task");
|
||||
@@ -225,123 +200,23 @@ void loop()
|
||||
LOG_DEBUG("Real Time Tasks A & B initialized");
|
||||
led.setStatus(RGBled::LedStatus::OK);
|
||||
|
||||
bool partial_save = false; // flag to indicate if a partial save has been done after a timeout
|
||||
auto last_data = millis();
|
||||
auto last_info = millis();
|
||||
|
||||
uint32_t counter_a = 0;
|
||||
uint32_t counter_b = 0;
|
||||
uint32_t wait_count = 0;
|
||||
|
||||
ignitionBoxStatus ign_info_A;
|
||||
ignitionBoxStatus ign_info_B;
|
||||
|
||||
ignitionBoxStatusFiltered ign_info_avg_A(filter_k);
|
||||
ignitionBoxStatusFiltered ign_info_avg_B(filter_k);
|
||||
|
||||
LITTLEFSGuard fsGuard;
|
||||
WebPage webPage(80, LittleFS); // Initialize webserver and Websocket
|
||||
|
||||
uint32_t last_loop = millis();
|
||||
//////////////// INNER LOOP /////////////////////
|
||||
while (running)
|
||||
{
|
||||
auto dataA = pdFALSE;
|
||||
auto dataB = pdFALSE;
|
||||
led.setStatus(RGBled::LedStatus::IDLE);
|
||||
delay(100);
|
||||
|
||||
dataA = xQueueReceive(rt_taskA_queue, &ign_info_A, 0);
|
||||
if (counter_a >= active_history_A->size()) // not concurrent with write task
|
||||
{
|
||||
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
|
||||
}
|
||||
|
||||
#ifdef CH_B_ENABLE
|
||||
dataB = xQueueReceive(rt_taskB_queue, &ign_info_B, 0);
|
||||
if (counter_b >= active_history_B->size()) // not concurrent with write task
|
||||
{
|
||||
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
|
||||
}
|
||||
#endif
|
||||
// Update last data
|
||||
if (dataA == pdTRUE || dataB == pdTRUE)
|
||||
last_data = millis();
|
||||
|
||||
// Update Led color
|
||||
if (dataA == pdTRUE && dataB == pdFALSE)
|
||||
led.setStatus(RGBled::DATA_A);
|
||||
else if (dataB == pdTRUE && dataA == pdFALSE)
|
||||
led.setStatus(RGBled::DATA_B);
|
||||
else
|
||||
led.setStatus(RGBled::DATA_ALL);
|
||||
|
||||
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
|
||||
// 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
|
||||
{
|
||||
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>());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if (dataA == pdFALSE && dataB == pdFALSE && (millis() - last_data) > 2000)
|
||||
{
|
||||
if (!partial_save && counter_a > 0) // if timeout occurs but we have unsaved data, save it before next timeout
|
||||
{
|
||||
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_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_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++);
|
||||
led.setStatus(RGBled::LedStatus::IDLE);
|
||||
delay(100);
|
||||
}
|
||||
|
||||
if ((millis() - last_info) > 1000)
|
||||
if ((millis() - last_loop) > 1000)
|
||||
{
|
||||
clearScreen();
|
||||
Serial.println();
|
||||
printRunningTasksMod(Serial);
|
||||
last_info = millis();
|
||||
last_loop = millis();
|
||||
}
|
||||
} //////////////// INNER LOOP /////////////////////
|
||||
|
||||
if (trigA_TaskHandle)
|
||||
vTaskDelete(trigA_TaskHandle);
|
||||
if (trigB_TaskHandle)
|
||||
vTaskDelete(trigB_TaskHandle);
|
||||
|
||||
} ////////////////////// MAIN LOOP //////////////////////
|
||||
|
||||
Reference in New Issue
Block a user