Compare commits
3 Commits
main
...
task-refac
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fdba6d5ad5 | ||
|
|
d1b96e932c | ||
| 684c34e209 |
@@ -1,7 +1,7 @@
|
||||
#include "datasave.h"
|
||||
#include <math.h>
|
||||
|
||||
static const size_t min_free = 1024 * 1024; // minimum free space in LittleFS to allow saving history (1MB)
|
||||
|
||||
|
||||
LITTLEFSGuard::LITTLEFSGuard()
|
||||
{
|
||||
@@ -22,26 +22,26 @@ LITTLEFSGuard::~LITTLEFSGuard()
|
||||
LOG_INFO("LittleFS unmounted successfully");
|
||||
}
|
||||
|
||||
void ignitionBoxStatusAverage::filter(int32_t &old, const int32_t value, const uint32_t k)
|
||||
void ignitionBoxStatusFiltered::filter(int32_t &old, const int32_t value, const uint32_t k)
|
||||
{
|
||||
float alpha = 1.0f / (float)k;
|
||||
old = old + (int32_t)(alpha * (float)(value - old));
|
||||
}
|
||||
|
||||
void ignitionBoxStatusAverage::filter(float &old, const float value, const uint32_t k)
|
||||
void ignitionBoxStatusFiltered::filter(float &old, const float value, const uint32_t k)
|
||||
{
|
||||
float alpha = 1.0f / (float)k;
|
||||
old = old + (float)(alpha * (float)(value - old));
|
||||
}
|
||||
|
||||
void ignitionBoxStatusAverage::reset()
|
||||
void ignitionBoxStatusFiltered::reset()
|
||||
{
|
||||
m_last = ignitionBoxStatus();
|
||||
m_count = 0;
|
||||
m_data_valid = false;
|
||||
}
|
||||
|
||||
void ignitionBoxStatusAverage::update(const ignitionBoxStatus &new_status)
|
||||
void ignitionBoxStatusFiltered::update(const ignitionBoxStatus &new_status)
|
||||
{
|
||||
if (m_count == 0 && !m_data_valid)
|
||||
{
|
||||
@@ -81,7 +81,7 @@ void ignitionBoxStatusAverage::update(const ignitionBoxStatus &new_status)
|
||||
}
|
||||
}
|
||||
|
||||
const bool ignitionBoxStatusAverage::get(ignitionBoxStatus &status) const
|
||||
const bool ignitionBoxStatusFiltered::get(ignitionBoxStatus &status) const
|
||||
{
|
||||
if (m_data_valid)
|
||||
{
|
||||
@@ -90,7 +90,7 @@ const bool ignitionBoxStatusAverage::get(ignitionBoxStatus &status) const
|
||||
return m_data_valid;
|
||||
}
|
||||
|
||||
const ArduinoJson::JsonDocument ignitionBoxStatusAverage::toJson() const
|
||||
const ArduinoJson::JsonDocument ignitionBoxStatusFiltered::toJson() const
|
||||
{
|
||||
ArduinoJson::JsonDocument doc;
|
||||
if (m_data_valid)
|
||||
@@ -125,97 +125,3 @@ const ArduinoJson::JsonDocument ignitionBoxStatusAverage::toJson() const
|
||||
return doc;
|
||||
}
|
||||
|
||||
void saveHistoryTask(void *pvParameters)
|
||||
{
|
||||
const auto *params = static_cast<dataSaveParams *>(pvParameters);
|
||||
const auto &history = *params->history;
|
||||
const auto &file_path = params->file_path;
|
||||
if (!params)
|
||||
{
|
||||
LOG_ERROR("Invalid parameters for saveHistoryTask");
|
||||
return;
|
||||
}
|
||||
LOG_DEBUG("Starting saving: ", file_path.c_str());
|
||||
save_history(history, file_path);
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void save_history(const PSRAMVector<ignitionBoxStatus> &history, const std::filesystem::path &file_name)
|
||||
{
|
||||
// Initialize SPIFFS
|
||||
if (!SAVE_HISTORY_TO_LITTLEFS)
|
||||
return;
|
||||
|
||||
auto littlefs_guard = LITTLEFSGuard(); // use RAII guard to ensure LittleFS is properly mounted and unmounted
|
||||
|
||||
if (LittleFS.totalBytes() - LittleFS.usedBytes() < min_free) // check if at least 1MB is free for saving history
|
||||
{
|
||||
LOG_ERROR("Not enough space in SPIFFS to save history");
|
||||
return;
|
||||
}
|
||||
|
||||
std::filesystem::path file_path = file_name;
|
||||
if (file_name.root_path() != "/littlefs")
|
||||
file_path = std::filesystem::path("/littlefs") / file_name;
|
||||
|
||||
auto save_flags = std::ios::out;
|
||||
if (first_save && LittleFS.exists(file_path.c_str()))
|
||||
{
|
||||
first_save = false;
|
||||
save_flags |= std::ios::trunc; // overwrite existing file
|
||||
LittleFS.remove(file_path.c_str()); // ensure file is removed before saving to avoid issues with appending to existing file in SPIFFS
|
||||
LOG_INFO("Saving history to LittleFS, new file:", file_path.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
save_flags |= std::ios::app; // append to new file
|
||||
LOG_INFO("Saving history to LittleFS, appending to existing file:", file_path.c_str());
|
||||
}
|
||||
|
||||
std::ofstream ofs(file_path, save_flags);
|
||||
if (ofs.fail())
|
||||
{
|
||||
LOG_ERROR("Failed to open file for writing");
|
||||
return;
|
||||
}
|
||||
|
||||
// write csv header
|
||||
if (first_save)
|
||||
{
|
||||
ofs << "TS,\
|
||||
EVENTS_12,DLY_12,STAT_12,V_12_1,V_12_2,V_12_3,V_12_4,IGNITION_MODE_12,\
|
||||
EVENTS_34,DLY_34,STAT_34,V_34_1,V_34_2,V_34_3,V_34_4,IGNITION_MODE_34,\
|
||||
ENGINE_RPM,ADC_READTIME,N_QUEUE_ERRORS"
|
||||
<< std::endl;
|
||||
ofs.flush();
|
||||
}
|
||||
|
||||
for (const auto &entry : history)
|
||||
{
|
||||
ofs << std::to_string(entry.timestamp) << ","
|
||||
<< std::to_string(entry.coils12.n_events) << ","
|
||||
<< std::to_string(entry.coils12.spark_delay) << ","
|
||||
<< std::string(sparkStatusNames.at(entry.coils12.spark_status)) << ","
|
||||
<< std::to_string(entry.coils12.peak_p_in) << ","
|
||||
<< std::to_string(entry.coils12.peak_n_in) << ","
|
||||
<< std::to_string(entry.coils12.peak_p_out) << ","
|
||||
<< std::to_string(entry.coils12.peak_n_out) << ","
|
||||
<< std::string(softStartStatusNames.at(entry.coils12.sstart_status)) << ","
|
||||
<< std::to_string(entry.coils34.n_events) << ","
|
||||
<< std::to_string(entry.coils34.spark_delay) << ","
|
||||
<< std::string(sparkStatusNames.at(entry.coils34.spark_status)) << ","
|
||||
<< std::to_string(entry.coils34.peak_p_in) << ","
|
||||
<< std::to_string(entry.coils34.peak_n_in) << ","
|
||||
<< std::to_string(entry.coils34.peak_p_out) << ","
|
||||
<< std::to_string(entry.coils34.peak_n_out) << ","
|
||||
<< std::string(softStartStatusNames.at(entry.coils34.sstart_status)) << ","
|
||||
<< std::to_string(entry.eng_rpm) << ","
|
||||
<< std::to_string(entry.adc_read_time) << ","
|
||||
<< std::to_string(entry.n_queue_errors);
|
||||
ofs << std::endl;
|
||||
ofs.flush();
|
||||
}
|
||||
|
||||
ofs.close();
|
||||
LOG_INFO("Ignition A history saved to LittleFS, records written: ", history.size());
|
||||
}
|
||||
|
||||
@@ -15,14 +15,6 @@
|
||||
#include "psvector.h"
|
||||
|
||||
const uint32_t max_history = 256;
|
||||
const bool SAVE_HISTORY_TO_LITTLEFS = false; // Set to true to enable saving history to LittleFS, false to disable
|
||||
static bool first_save = true; // flag to indicate if this is the first save (to write header)
|
||||
|
||||
struct dataSaveParams
|
||||
{
|
||||
const PSRAMVector<ignitionBoxStatus> *history;
|
||||
const std::filesystem::path file_path;
|
||||
};
|
||||
|
||||
class LITTLEFSGuard
|
||||
{
|
||||
@@ -31,7 +23,7 @@ public:
|
||||
~LITTLEFSGuard();
|
||||
};
|
||||
|
||||
class ignitionBoxStatusAverage
|
||||
class ignitionBoxStatusFiltered
|
||||
{
|
||||
private:
|
||||
ignitionBoxStatus m_last;
|
||||
@@ -40,8 +32,8 @@ private:
|
||||
bool m_data_valid = false; // flag to indicate if the average data is valid (i.e. at least one sample has been added)
|
||||
|
||||
public:
|
||||
ignitionBoxStatusAverage() = default;
|
||||
ignitionBoxStatusAverage(const uint32_t max_count) : m_max_count(max_count)
|
||||
ignitionBoxStatusFiltered() = default;
|
||||
ignitionBoxStatusFiltered(const uint32_t max_count) : m_max_count(max_count)
|
||||
{
|
||||
m_data_valid = false;
|
||||
m_count = 0;
|
||||
@@ -56,7 +48,3 @@ private:
|
||||
void filter(int32_t &old, const int32_t value, const uint32_t k);
|
||||
void filter(float &old, const float value, const uint32_t k);
|
||||
};
|
||||
|
||||
// Task and function declarations
|
||||
void saveHistoryTask(void *pvParameters);
|
||||
void save_history(const PSRAMVector<ignitionBoxStatus> &history, const std::filesystem::path &file_path);
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
// Library defines
|
||||
#define ADS1256_SPI_ALREADY_STARTED
|
||||
|
||||
// System Includes
|
||||
#include <memory>
|
||||
|
||||
// Device Libraries
|
||||
#include <ADS1256.h>
|
||||
#include <AD5292.h>
|
||||
@@ -19,17 +22,34 @@
|
||||
#define ADC_CH_PEAK_34N_OUT SING_7
|
||||
|
||||
// Device Pointer structs for tasks
|
||||
struct Devices {
|
||||
AD5292 *pot_a = NULL, *pot_b = NULL;
|
||||
ADS1256 *adc_a = NULL, *adc_b = NULL;
|
||||
PCA9555* io = NULL;
|
||||
struct Devices
|
||||
{
|
||||
std::unique_ptr<SPIClass> m_spi_a = nullptr;
|
||||
std::unique_ptr<SPIClass> m_spi_b = nullptr;
|
||||
|
||||
std::unique_ptr<AD5292> m_pot_a = nullptr;
|
||||
std::unique_ptr<AD5292> m_pot_b = nullptr;
|
||||
|
||||
std::unique_ptr<ADS1256> m_adc_a = nullptr;
|
||||
std::unique_ptr<ADS1256> m_adc_b = nullptr;
|
||||
|
||||
std::unique_ptr<PCA9555> m_expander_a = nullptr;
|
||||
std::unique_ptr<PCA9555> m_expander_b = nullptr;
|
||||
std::unique_ptr<PCA9555> m_expander_inputs_ab = nullptr;
|
||||
|
||||
std::mutex m_spi_a_mutex;
|
||||
std::mutex m_spi_b_mutex;
|
||||
|
||||
std::mutex m_i2c_mutex;
|
||||
};
|
||||
|
||||
// Adc read channel wrapper to selet mux before reading
|
||||
inline float adcReadChannel(ADS1256* adc, const uint8_t ch){
|
||||
inline float adcReadChannel(ADS1256 *adc, const uint8_t ch)
|
||||
{
|
||||
adc->setMUX(ch);
|
||||
// scarta 3 conversioni
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
adc->readSingle();
|
||||
}
|
||||
// ora lettura valida a 30kSPS → ~100 µs di settling
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
// Defines to enable channel B
|
||||
#define CH_B_ENABLE
|
||||
#define TEST
|
||||
// #define TEST
|
||||
|
||||
// Debug Defines
|
||||
#define WIFI_SSID "AstroRotaxMonitor"
|
||||
@@ -52,6 +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
|
||||
if (WiFi.softAP(WIFI_SSID, WIFI_PASSWORD))
|
||||
{
|
||||
LOG_INFO("WiFi AP Mode Started");
|
||||
@@ -80,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 = &dev,
|
||||
.rt_queue = rt_taskA_queue,
|
||||
.rt_int = rtTaskInterrupts{
|
||||
.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_resets = rtTaskResets{.rst_io_peak = RST_EXT_PEAK_DETECT_A, .rst_io_sh = RST_EXT_SAMPLE_HOLD_A}};
|
||||
|
||||
#ifdef CH_B_ENABLE
|
||||
rtTaskParams taskB_params{
|
||||
.rt_running = true,
|
||||
.dev = &dev,
|
||||
.rt_queue = rt_taskB_queue,
|
||||
.rt_int = rtTaskInterrupts{
|
||||
.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_resets = rtTaskResets{.rst_io_peak = RST_EXT_PEAK_DETECT_B, .rst_io_sh = RST_EXT_SAMPLE_HOLD_B}};
|
||||
#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
|
||||
@@ -152,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)
|
||||
{
|
||||
@@ -167,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,
|
||||
"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,
|
||||
"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");
|
||||
@@ -224,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;
|
||||
|
||||
ignitionBoxStatusAverage ign_info_avg_A(filter_k);
|
||||
ignitionBoxStatusAverage 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;
|
||||
|
||||
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 //////////////////////
|
||||
|
||||
@@ -80,31 +80,86 @@
|
||||
#define SPARK_PIN_B34 2
|
||||
|
||||
// =====================
|
||||
// PCA9555 (I2C EXPANDER)
|
||||
// PCA9555 I/O EXPANDER BOX_A
|
||||
// =====================
|
||||
|
||||
// --- RESET LINES ---
|
||||
#define RST_EXT_PEAK_DETECT_A 0
|
||||
#define RST_EXT_SAMPLE_HOLD_A 1
|
||||
#define RST_EXT_PEAK_DETECT_B 2
|
||||
#define RST_EXT_SAMPLE_HOLD_B 3
|
||||
#define BTN_3 4
|
||||
#define BTN_4 5
|
||||
#define BTN_5 6
|
||||
#define BTN_6 7
|
||||
#define EXPANDER_A_ADDR 0x010101
|
||||
|
||||
// --- DIGITAL POT CHIP SELECT LINES ---
|
||||
#define POT_CS_A12 0
|
||||
#define POT_CS_A34 1
|
||||
|
||||
// --- SOFT START FORCE LINES ---
|
||||
#define SS_FORCE_A 2
|
||||
#define SS_INIBHIT_A12 3
|
||||
#define SS_INHIBIT_A34 4
|
||||
|
||||
// --- SAMPLE AND HOLD ARM AND DISCHARGE ---
|
||||
#define SH_DISCH_A12 5
|
||||
#define SH_DISCH_A34 6
|
||||
#define SH_ARM_A12 7
|
||||
#define SH_ARM_A34 8
|
||||
|
||||
// --- RELAY ---
|
||||
#define EXT_RELAY_A 8
|
||||
#define EXT_RELAY_B 9
|
||||
#define RELAY_IN_A12 9
|
||||
#define RELAY_OUT_A12 10
|
||||
#define RELAY_IN_A34 11
|
||||
#define RELAY_OUT_A34 12
|
||||
|
||||
// --- STATUS / BUTTON ---
|
||||
#define BTN_7 10
|
||||
#define BTN_8 11
|
||||
#define STA_1 12
|
||||
#define STA_2 13
|
||||
#define STA_3 14
|
||||
#define STA_4 15
|
||||
|
||||
// =====================
|
||||
// PCA9555 I/O EXPANDER BOX_B
|
||||
// =====================
|
||||
|
||||
#define EXPANDER_B_ADDR 0x101010
|
||||
|
||||
// --- DIGITAL POT CHIP SELECT LINES ---
|
||||
#define POT_CS_B12 0
|
||||
#define POT_CS_B34 1
|
||||
|
||||
// --- SOFT START FORCE LINES ---
|
||||
#define SS_FORCE_B 2
|
||||
#define SS_INIBHIT_B12 3
|
||||
#define SS_INHIBIT_B34 4
|
||||
|
||||
// --- SAMPLE AND HOLD ARM AND DISCHARGE ---
|
||||
#define SH_DISCH_B12 5
|
||||
#define SH_DISCH_B34 6
|
||||
#define SH_ARM_B12 7
|
||||
#define SH_ARM_B34 8
|
||||
|
||||
// --- RELAY ---
|
||||
#define RELAY_IN_B12 9
|
||||
#define RELAY_OUT_B12 10
|
||||
#define RELAY_IN_B34 11
|
||||
#define RELAY_OUT_B34 12
|
||||
|
||||
// --- STATUS / BUTTON ---
|
||||
#define STA_2 13
|
||||
#define STA_3 14
|
||||
#define STA_4 15
|
||||
|
||||
// =====================
|
||||
// PCA9555 I/O EXPANDER INPUTS A+B
|
||||
// =====================
|
||||
|
||||
#define EXPANDER_IN_ADDR 0x0a0a0a
|
||||
|
||||
#define SS_A12_ON
|
||||
#define SS_A12_OFF
|
||||
#define SS_A34_ON
|
||||
#define SS_A34_OFF
|
||||
|
||||
#define SS_B12_ON
|
||||
#define SS_B12_OFF
|
||||
#define SS_B34_ON
|
||||
#define SS_B34_OFF
|
||||
|
||||
|
||||
// Init Pin Functions
|
||||
inline void initTriggerPinsInputs()
|
||||
{
|
||||
|
||||
@@ -65,7 +65,7 @@
|
||||
#define RST_EXT_A34N 3
|
||||
|
||||
// --- RELAY ---
|
||||
#define EXT_RELAY_A 8
|
||||
#define SH_ARM_A34 8
|
||||
|
||||
|
||||
// Init Pin Functions
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
#include "tasks.h"
|
||||
#include <esp_timer.h>
|
||||
#include <datasave.h>
|
||||
|
||||
//// GLOBAL STATIC FUNCTIONS
|
||||
|
||||
// Timeout callback for microsecond precision
|
||||
void spark_timeout_callback(void *arg)
|
||||
@@ -8,7 +11,18 @@ void spark_timeout_callback(void *arg)
|
||||
xTaskNotify(handle, SPARK_FLAG_TIMEOUT, eSetValueWithOverwrite);
|
||||
}
|
||||
|
||||
void rtIgnitionTask(void *pvParameters)
|
||||
// Manages queue receive, save data and callback to external tasks for communication
|
||||
void rtIgnitionTask::rtIgnitionTask_manager(void *pvParameters)
|
||||
{
|
||||
rtIgnitionTask *cls = (rtIgnitionTask *)pvParameters;
|
||||
while (cls->m_running)
|
||||
{
|
||||
cls->run();
|
||||
}
|
||||
}
|
||||
|
||||
// Static task function
|
||||
void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
|
||||
{
|
||||
|
||||
// Invalid real time rt_task_ptr parameters, exit immediate
|
||||
@@ -18,15 +32,14 @@ void rtIgnitionTask(void *pvParameters)
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
|
||||
// Task Parameters and Devices
|
||||
rtTaskParams *params = (rtTaskParams *)pvParameters;
|
||||
const rtTaskInterrupts rt_int = params->rt_int; // copy to avoid external override
|
||||
const rtTaskResets rt_rst = params->rt_resets; // copy to avoid external override
|
||||
const rtTaskInterruptParams rt_int = params->rt_int; // copy to avoid external override
|
||||
const rtTaskIOParams rt_rst = params->rt_io; // copy to avoid external override
|
||||
QueueHandle_t rt_queue = params->rt_queue;
|
||||
Devices *dev = params->dev;
|
||||
ADS1256 *adc = dev->adc_a;
|
||||
PCA9555 *io = dev->io;
|
||||
Devices *dev = params->dev.get();
|
||||
ADS1256 *adc = dev->m_adc_a.get();
|
||||
PCA9555 *io = dev->m_expander_a.get();
|
||||
|
||||
TaskStatus_t rt_task_info;
|
||||
vTaskGetInfo(NULL, &rt_task_info, pdFALSE, eInvalid);
|
||||
@@ -75,7 +88,6 @@ void rtIgnitionTask(void *pvParameters)
|
||||
.name = "spark_timeout"};
|
||||
esp_timer_create(&timer_args, &timeout_timer);
|
||||
|
||||
|
||||
// Attach Pin Interrupts
|
||||
attachInterruptArg(digitalPinToInterrupt(rt_int.trig_pin_12p), rt_int.isr_ptr, (void *)&isr_params_t12p, RISING);
|
||||
attachInterruptArg(digitalPinToInterrupt(rt_int.trig_pin_12n), rt_int.isr_ptr, (void *)&isr_params_t12n, RISING);
|
||||
@@ -235,17 +247,13 @@ void rtIgnitionTask(void *pvParameters)
|
||||
ign_box_sts.adc_read_time = (int32_t)(esp_timer_get_time() - start_adc_read);
|
||||
}
|
||||
else // simulate adc read timig
|
||||
vTaskDelay(pdMS_TO_TICKS(1));
|
||||
vTaskDelay(pdMS_TO_TICKS(c_adc_time));
|
||||
|
||||
// reset peak detectors + sample and hold
|
||||
// outputs on io expander
|
||||
if (io)
|
||||
{
|
||||
const uint16_t iostat = io->read();
|
||||
const uint16_t rst_bitmask = (0x0001 << rt_rst.rst_io_peak);
|
||||
io->write(iostat | rst_bitmask);
|
||||
vTaskDelay(pdMS_TO_TICKS(1));
|
||||
io->write(iostat & ~rst_bitmask);
|
||||
// [TODO] code to reset sample and hold and arm trigger level detectors
|
||||
}
|
||||
else
|
||||
vTaskDelay(pdMS_TO_TICKS(1));
|
||||
@@ -261,7 +269,7 @@ void rtIgnitionTask(void *pvParameters)
|
||||
}
|
||||
// Delete the timeout timer
|
||||
esp_timer_delete(timeout_timer);
|
||||
LOG_WARN("Ending realTime Task");
|
||||
LOG_WARN("rtTask Ending [", rt_task_name, "]");
|
||||
// Ignition A Interrupts DETACH
|
||||
detachInterrupt(rt_int.trig_pin_12p);
|
||||
detachInterrupt(rt_int.trig_pin_12n);
|
||||
@@ -272,3 +280,237 @@ void rtIgnitionTask(void *pvParameters)
|
||||
// delete present task
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
///////////// CLASS MEMBER DEFINITIONS /////////////
|
||||
rtIgnitionTask::rtIgnitionTask(const rtTaskParams params, const uint32_t history_size, const uint32_t queue_size, const uint8_t core, std::mutex &fs_mutex, fs::FS &filesystem) : m_params(params), m_filesystem(filesystem), m_fs_mutex(fs_mutex), m_core(core), m_max_history(history_size)
|
||||
{
|
||||
// create queue buffers
|
||||
m_queue = xQueueCreate(queue_size, sizeof(ignitionBoxStatus));
|
||||
if (!m_queue)
|
||||
{
|
||||
LOG_ERROR("Unable To Create Task [", params.name.c_str(), "] queues");
|
||||
m_manager_status = rtTaskStatus::ERROR;
|
||||
return;
|
||||
}
|
||||
else
|
||||
m_params.rt_queue = m_queue;
|
||||
|
||||
// create PSram history vectors
|
||||
m_history_0.resize(history_size);
|
||||
m_history_1.resize(history_size);
|
||||
// assing active and writable history
|
||||
m_active_history = std::unique_ptr<PSHistory>(&m_history_0);
|
||||
m_save_history = std::unique_ptr<PSHistory>(&m_history_1);
|
||||
|
||||
LOG_WARN("Starting Manager for [", m_params.name.c_str(), "]");
|
||||
auto task_success = xTaskCreate(
|
||||
rtIgnitionTask_manager,
|
||||
(std::string("man_") + m_params.name).c_str(),
|
||||
m_params.rt_stack_size,
|
||||
(void *)this,
|
||||
1,
|
||||
&m_manager_handle);
|
||||
|
||||
if (task_success != pdPASS)
|
||||
{
|
||||
LOG_ERROR("Unable To Create Manager for [", params.name.c_str(), "]");
|
||||
m_manager_status = rtTaskStatus::ERROR;
|
||||
return;
|
||||
}
|
||||
|
||||
// average every 10 samples
|
||||
m_info_filtered = ignitionBoxStatusFiltered(10);
|
||||
m_last_data = millis();
|
||||
m_manager_status = rtTaskStatus::OK;
|
||||
}
|
||||
|
||||
rtIgnitionTask::~rtIgnitionTask()
|
||||
{
|
||||
if (m_rt_handle)
|
||||
vTaskDelete(m_rt_handle);
|
||||
if (m_manager_handle)
|
||||
vTaskDelete(m_manager_handle);
|
||||
if (m_queue)
|
||||
vQueueDelete(m_queue);
|
||||
}
|
||||
|
||||
void rtIgnitionTask::run()
|
||||
{
|
||||
// receive new data from the queue
|
||||
auto new_data = xQueueReceive(m_queue, &m_last_status, 0); // non blocking receive
|
||||
|
||||
if (new_data == pdPASS)
|
||||
{
|
||||
m_manager_status = rtTaskStatus::RUNNING;
|
||||
// if history buffer is full swap buffers and if enabled save history buffer
|
||||
if (m_counter_status >= m_active_history->size())
|
||||
{
|
||||
m_counter_status = 0;
|
||||
m_partial_save = false; // reset partial save flag on new data cycle
|
||||
std::swap(m_active_history, m_save_history);
|
||||
if (m_enable_save)
|
||||
saveHistory(*m_save_history, m_history_path); // directly call the save task function to save without delay
|
||||
}
|
||||
|
||||
// update filtered data
|
||||
m_info_filtered.update(m_last_status);
|
||||
(*m_active_history)[m_counter_status] = m_last_status;
|
||||
|
||||
// update data counter
|
||||
m_counter_status++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (millis() - m_last_data > c_idle_time){
|
||||
if (m_counter_status > 0 && !m_partial_save){
|
||||
m_active_history->resize(m_counter_status);
|
||||
saveHistory(*m_active_history, m_history_path);
|
||||
m_active_history->resize(m_max_history);
|
||||
m_counter_status = 0;
|
||||
m_partial_save = true;
|
||||
}
|
||||
m_manager_status = rtTaskStatus::IDLE;
|
||||
}
|
||||
delay(5); // yeld to another task
|
||||
}
|
||||
}
|
||||
|
||||
const bool rtIgnitionTask::start()
|
||||
{
|
||||
LOG_WARN("Starting rtTask [", m_params.name.c_str(), "]");
|
||||
auto task_success = xTaskCreatePinnedToCore(
|
||||
rtIgnitionTask_realtime,
|
||||
m_params.name.c_str(),
|
||||
m_params.rt_stack_size,
|
||||
(void *)&m_params,
|
||||
m_params.rt_priority,
|
||||
&m_rt_handle,
|
||||
m_core);
|
||||
const bool success = task_success == pdPASS && m_rt_handle != nullptr;
|
||||
if (success)
|
||||
m_manager_status = rtTaskStatus::IDLE;
|
||||
return success;
|
||||
}
|
||||
|
||||
const bool rtIgnitionTask::stop()
|
||||
{
|
||||
LOG_WARN("Ending Task [", m_params.name.c_str(), "]");
|
||||
if (m_rt_handle)
|
||||
{
|
||||
m_params.rt_running = false;
|
||||
m_rt_handle = nullptr;
|
||||
m_manager_status = rtTaskStatus::STOPPED;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const ignitionBoxStatus rtIgnitionTask::getLast() const
|
||||
{
|
||||
return m_last_status;
|
||||
}
|
||||
|
||||
const ignitionBoxStatusFiltered rtIgnitionTask::getFiltered() const
|
||||
{
|
||||
return m_info_filtered;
|
||||
}
|
||||
|
||||
const rtIgnitionTask::rtTaskStatus rtIgnitionTask::getStatus() const
|
||||
{
|
||||
return m_manager_status;
|
||||
}
|
||||
|
||||
void rtIgnitionTask::enableSave(const bool enable, const std::filesystem::path filename)
|
||||
{
|
||||
m_enable_save = enable;
|
||||
if (enable && !filename.empty())
|
||||
{
|
||||
LOG_WARN("Save History Enabled Task [", m_params.name.c_str(), "]");
|
||||
m_history_path = m_filesystem.mountpoint() / filename;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_WARN("Save History Disabled Task [", m_params.name.c_str(), "]");
|
||||
}
|
||||
}
|
||||
|
||||
void rtIgnitionTask::saveHistory(const rtIgnitionTask::PSHistory &history, const std::filesystem::path &file_name)
|
||||
{
|
||||
// Lock filesystem mutex to avoid concurrent access
|
||||
std::lock_guard<std::mutex> fs_lock(m_fs_mutex);
|
||||
|
||||
// Check for free space
|
||||
if (LittleFS.totalBytes() - LittleFS.usedBytes() < history.size() * sizeof(ignitionBoxStatus) * 200) // check if at least 1MB is free for saving history
|
||||
{
|
||||
LOG_ERROR("Not enough space in SPIFFS to save history");
|
||||
return;
|
||||
}
|
||||
|
||||
// create complete file path
|
||||
const std::filesystem::path mount_point = std::filesystem::path(m_filesystem.mountpoint());
|
||||
std::filesystem::path file_path = file_name;
|
||||
if (file_name.root_path() != mount_point)
|
||||
file_path = mount_point / file_name;
|
||||
|
||||
// if firt save remove old file and create new
|
||||
auto save_flags = std::ios::out;
|
||||
if (m_first_save && m_filesystem.exists(file_path.c_str()))
|
||||
{
|
||||
m_first_save = false;
|
||||
save_flags |= std::ios::trunc; // overwrite existing file
|
||||
m_filesystem.remove(file_path.c_str()); // ensure file is removed before saving to avoid issues with appending to existing file in SPIFFS
|
||||
LOG_INFO("Saving history to Flash, new file:", file_path.c_str());
|
||||
}
|
||||
else // else append to existing file
|
||||
{
|
||||
save_flags |= std::ios::app; // append to new file
|
||||
LOG_INFO("Saving history to Flash, appending to existing file:", file_path.c_str());
|
||||
}
|
||||
|
||||
std::ofstream ofs(file_path, save_flags);
|
||||
if (ofs.fail())
|
||||
{
|
||||
LOG_ERROR("Failed to open file for writing");
|
||||
return;
|
||||
}
|
||||
|
||||
// write csv header
|
||||
if (m_first_save)
|
||||
{
|
||||
ofs << "TS,\
|
||||
EVENTS_12,DLY_12,STAT_12,V_12_1,V_12_2,V_12_3,V_12_4,IGNITION_MODE_12,\
|
||||
EVENTS_34,DLY_34,STAT_34,V_34_1,V_34_2,V_34_3,V_34_4,IGNITION_MODE_34,\
|
||||
ENGINE_RPM,ADC_READTIME,N_QUEUE_ERRORS"
|
||||
<< std::endl;
|
||||
ofs.flush();
|
||||
}
|
||||
|
||||
for (const auto &entry : history)
|
||||
{
|
||||
ofs << std::to_string(entry.timestamp) << ","
|
||||
<< std::to_string(entry.coils12.n_events) << ","
|
||||
<< std::to_string(entry.coils12.spark_delay) << ","
|
||||
<< std::string(sparkStatusNames.at(entry.coils12.spark_status)) << ","
|
||||
<< std::to_string(entry.coils12.peak_p_in) << ","
|
||||
<< std::to_string(entry.coils12.peak_n_in) << ","
|
||||
<< std::to_string(entry.coils12.peak_p_out) << ","
|
||||
<< std::to_string(entry.coils12.peak_n_out) << ","
|
||||
<< std::string(softStartStatusNames.at(entry.coils12.sstart_status)) << ","
|
||||
<< std::to_string(entry.coils34.n_events) << ","
|
||||
<< std::to_string(entry.coils34.spark_delay) << ","
|
||||
<< std::string(sparkStatusNames.at(entry.coils34.spark_status)) << ","
|
||||
<< std::to_string(entry.coils34.peak_p_in) << ","
|
||||
<< std::to_string(entry.coils34.peak_n_in) << ","
|
||||
<< std::to_string(entry.coils34.peak_p_out) << ","
|
||||
<< std::to_string(entry.coils34.peak_n_out) << ","
|
||||
<< std::string(softStartStatusNames.at(entry.coils34.sstart_status)) << ","
|
||||
<< std::to_string(entry.eng_rpm) << ","
|
||||
<< std::to_string(entry.adc_read_time) << ","
|
||||
<< std::to_string(entry.n_queue_errors);
|
||||
ofs << std::endl;
|
||||
ofs.flush();
|
||||
}
|
||||
|
||||
ofs.close();
|
||||
LOG_INFO("Ignition Box history saved to Flash, records written: ", history.size());
|
||||
}
|
||||
|
||||
@@ -2,12 +2,18 @@
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
|
||||
|
||||
// Serial debug flag
|
||||
//#define DEBUG
|
||||
// #define DEBUG
|
||||
|
||||
// Arduino Libraries
|
||||
#include <Arduino.h>
|
||||
#include <DebugLog.h>
|
||||
#include "utils.h"
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <filesystem>
|
||||
#include <FS.h>
|
||||
#include <LittleFS.h>
|
||||
#include <datasave.h>
|
||||
|
||||
// ISR
|
||||
#include "isr.h"
|
||||
@@ -31,9 +37,14 @@ static const std::map<const uint32_t, const char *> names = {
|
||||
};
|
||||
#endif
|
||||
|
||||
// RT task Interrupt parameters
|
||||
struct rtTaskInterrupts
|
||||
class rtIgnitionTask
|
||||
{
|
||||
using PSHistory = PSRAMVector<ignitionBoxStatus>;
|
||||
|
||||
public:
|
||||
// RT task Interrupt parameters
|
||||
struct rtTaskInterruptParams
|
||||
{
|
||||
void (*isr_ptr)(void *);
|
||||
const uint8_t trig_pin_12p;
|
||||
const uint8_t trig_pin_12n;
|
||||
@@ -41,23 +52,103 @@ struct rtTaskInterrupts
|
||||
const uint8_t trig_pin_34n;
|
||||
const uint8_t spark_pin_12;
|
||||
const uint8_t spark_pin_34;
|
||||
};
|
||||
};
|
||||
|
||||
// RT Task Peak Detector Reset pins
|
||||
struct rtTaskResets
|
||||
{
|
||||
const uint8_t rst_io_peak;
|
||||
const uint8_t rst_io_sh;
|
||||
};
|
||||
// RT Task Peak Detector Reset pins
|
||||
struct rtTaskIOParams
|
||||
{
|
||||
const uint32_t expander_addr;
|
||||
const uint8_t pot_cs_12;
|
||||
const uint8_t pot_cs_34;
|
||||
const uint8_t ss_force;
|
||||
const uint8_t ss_inhibit_12;
|
||||
const uint8_t ss_inhibit_34;
|
||||
const uint8_t sh_disch_12;
|
||||
const uint8_t sh_disch_34;
|
||||
const uint8_t sh_arm_12;
|
||||
const uint8_t sh_arm_34;
|
||||
const uint8_t relay_in_12;
|
||||
const uint8_t relay_in_34;
|
||||
const uint8_t relay_out_12;
|
||||
const uint8_t relay_out_34;
|
||||
};
|
||||
|
||||
// RT task parameters
|
||||
struct rtTaskParams
|
||||
{
|
||||
// RT task parameters
|
||||
struct rtTaskParams
|
||||
{
|
||||
bool rt_running; // run flag, false to terminate
|
||||
Devices *dev;
|
||||
const QueueHandle_t rt_queue;
|
||||
const rtTaskInterrupts rt_int; // interrupt pins to attach
|
||||
const rtTaskResets rt_resets; // reset ping for peak detectors
|
||||
};
|
||||
const std::string name;
|
||||
const uint32_t rt_stack_size;
|
||||
const uint32_t rt_priority;
|
||||
const rtTaskInterruptParams rt_int; // interrupt pins to attach
|
||||
const rtTaskIOParams rt_io; // reset ping for peak detectors
|
||||
QueueHandle_t rt_queue; // queue for task io
|
||||
const std::shared_ptr<Devices> dev;
|
||||
};
|
||||
|
||||
void rtIgnitionTask(void *pvParameters);
|
||||
enum rtTaskStatus
|
||||
{
|
||||
INIT,
|
||||
OK,
|
||||
ERROR,
|
||||
RUNNING,
|
||||
IDLE,
|
||||
STOPPED
|
||||
};
|
||||
|
||||
public:
|
||||
rtIgnitionTask(const rtTaskParams params, const uint32_t history_size, const uint32_t queue_size, const uint8_t core, std::mutex &fs_mutex, fs::FS &filesystem = LittleFS);
|
||||
~rtIgnitionTask();
|
||||
|
||||
void run();
|
||||
const bool start();
|
||||
const bool stop();
|
||||
|
||||
const ignitionBoxStatus getLast() const;
|
||||
const ignitionBoxStatusFiltered getFiltered() const;
|
||||
|
||||
const rtTaskStatus getStatus() const;
|
||||
|
||||
void enableSave(const bool enable, const std::filesystem::path filename);
|
||||
|
||||
private:
|
||||
void saveHistory(const rtIgnitionTask::PSHistory &history, const std::filesystem::path &file_name);
|
||||
|
||||
private: // static functions for FreeRTOS
|
||||
static void rtIgnitionTask_manager(void *pvParameters);
|
||||
static void rtIgnitionTask_realtime(void *pvParameters);
|
||||
|
||||
private:
|
||||
bool m_running = true;
|
||||
rtTaskStatus m_manager_status = INIT;
|
||||
|
||||
rtTaskParams m_params;
|
||||
const uint8_t m_core;
|
||||
|
||||
TaskHandle_t m_rt_handle = nullptr;
|
||||
TaskHandle_t m_manager_handle = nullptr;
|
||||
QueueHandle_t m_queue = nullptr;
|
||||
|
||||
bool m_enable_save = false;
|
||||
std::filesystem::path m_history_path;
|
||||
const uint32_t m_max_history;
|
||||
PSHistory m_history_0;
|
||||
PSHistory m_history_1;
|
||||
std::unique_ptr<PSHistory> m_active_history;
|
||||
std::unique_ptr<PSHistory> m_save_history;
|
||||
fs::FS &m_filesystem;
|
||||
std::mutex &m_fs_mutex;
|
||||
|
||||
bool m_partial_save = false;
|
||||
bool m_first_save = true;
|
||||
|
||||
uint32_t m_counter_status = 0;
|
||||
uint32_t m_last_data = 0;
|
||||
ignitionBoxStatus m_last_status;
|
||||
ignitionBoxStatusFiltered m_info_filtered;
|
||||
|
||||
static const uint32_t c_idle_time = 2000; // in mS
|
||||
static const uint32_t c_spark_timeout_max = 500; // uS
|
||||
static const uint8_t c_adc_time = 4; // in mS
|
||||
static const uint8_t c_io_time = 2; // in mS
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user