task variable name refactoring

This commit is contained in:
2026-04-22 14:17:35 +02:00
parent d700578256
commit 15ca82b6df
2 changed files with 86 additions and 85 deletions

View File

@@ -319,28 +319,28 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
} }
///////////// CLASS MEMBER DEFINITIONS ///////////// ///////////// 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) 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_filesystemMutex(fs_mutex), m_core(core), m_historyMax(history_size)
{ {
LOG_WARN("Starting Manager for [", m_params.name.c_str(), "]"); LOG_WARN("Starting Manager for [", m_params.name.c_str(), "]");
// create queue buffers // create queue buffers
m_queue = xQueueCreate(queue_size, sizeof(ignitionBoxStatus)); m_rtQueueHandle = xQueueCreate(queue_size, sizeof(ignitionBoxStatus));
if (!m_queue) if (!m_rtQueueHandle)
{ {
LOG_ERROR("Unable To Create Task [", params.name.c_str(), "] queues"); LOG_ERROR("Unable To Create Task [", params.name.c_str(), "] queues");
m_manager_status = rtTaskStatus::ERROR; m_managerStatus = rtTaskStatus::ERROR;
return; return;
} }
else else
m_params.rt_queue = m_queue; m_params.rt_queue = m_rtQueueHandle;
try try
{ {
// create PSram history vectors // create PSram history vectors
m_history_0 = PSHistory(history_size); m_historyBuf0 = PSHistory(history_size);
m_history_1 = PSHistory(history_size); m_historyBuf1 = PSHistory(history_size);
// assing active and writable history // assing active and writable history
m_active_history = std::unique_ptr<PSHistory>(&m_history_0); m_historyActive = std::unique_ptr<PSHistory>(&m_historyBuf0);
m_save_history = std::unique_ptr<PSHistory>(&m_history_1); m_historyInactive = std::unique_ptr<PSHistory>(&m_historyBuf1);
} }
catch (std::bad_alloc &e) catch (std::bad_alloc &e)
{ {
@@ -348,86 +348,87 @@ rtIgnitionTask::rtIgnitionTask(const rtTaskParams params, const uint32_t history
return; return;
} }
m_name = (std::string("man_") + m_params.name).c_str(); m_managerTaskName = (std::string("man_") + m_params.name).c_str();
auto task_success = xTaskCreatePinnedToCore( auto task_success = xTaskCreatePinnedToCore(
rtIgnitionTask_manager, rtIgnitionTask_manager,
m_name.c_str(), m_managerTaskName.c_str(),
RT_TASK_STACK, RT_TASK_STACK,
(void *)this, (void *)this,
m_params.rt_priority >> 2, m_params.rt_priority >> 2,
&m_manager_handle, &m_managerHandle,
m_core); m_core);
if (task_success != pdPASS) if (task_success != pdPASS)
{ {
LOG_ERROR("Unable To Create Manager for [", params.name.c_str(), "]"); LOG_ERROR("Unable To Create Manager for [", params.name.c_str(), "]");
m_manager_status = rtTaskStatus::ERROR; m_managerStatus = rtTaskStatus::ERROR;
return; return;
} }
// average every 10 samples // average every 10 samples
m_info_filtered = ignitionBoxStatusFiltered(10); m_statusFiltered = ignitionBoxStatusFiltered(m_filterSize);
m_last_data = millis(); m_dataLast = millis();
m_manager_status = rtTaskStatus::OK; m_managerStatus = rtTaskStatus::OK;
} }
rtIgnitionTask::~rtIgnitionTask() rtIgnitionTask::~rtIgnitionTask()
{ {
if (m_rt_handle) if (m_rtHandle)
vTaskDelete(m_rt_handle); vTaskDelete(m_rtHandle);
if (m_manager_handle) if (m_managerHandle)
vTaskDelete(m_manager_handle); vTaskDelete(m_managerHandle);
if (m_queue) if (m_rtQueueHandle)
vQueueDelete(m_queue); vQueueDelete(m_rtQueueHandle);
} }
void rtIgnitionTask::run() void rtIgnitionTask::run()
{ {
// receive new data from the queue // receive new data from the queue
auto new_data = xQueueReceive(m_queue, &m_last_status, 0); // non blocking receive auto new_data = xQueueReceive(m_rtQueueHandle, &m_statusLast, 0); // non blocking receive
if (new_data == pdPASS) if (new_data == pdPASS)
{ {
m_last_data = millis(); m_dataLast = millis();
m_manager_status = rtTaskStatus::RUNNING; m_managerStatus = rtTaskStatus::RUNNING;
// if history buffer is full swap buffers and if enabled save history buffer // if history buffer is full swap buffers and if enabled save history buffer
if (m_counter_status >= m_max_history) if (m_statusCounter >= m_historyMax)
{ {
LOG_DEBUG("Save for Buffer Full: ", m_counter_status); LOG_DEBUG("Save for Buffer Full: ", m_statusCounter);
m_counter_status = 0; m_statusCounter = 0;
m_partial_save = false; // reset partial save flag on new data cycle m_savePartial = false; // reset partial save flag on new data cycle
std::swap(m_active_history, m_save_history); std::swap(m_historyActive, m_historyInactive);
if (m_enable_save) if (m_historySaveEnable)
saveHistory(*m_save_history, m_history_path); // directly call the save task function to save without delay saveHistory(*m_historyInactive, m_historyPath); // directly call the save task function to save without delay
LOG_INFO("Save History"); LOG_INFO("Save History");
} }
// update filtered data // update filtered data
m_info_filtered.update(m_last_status); m_statusFiltered.update(m_statusLast);
(*m_active_history)[m_counter_status] = m_last_status; (*m_historyActive)[m_statusCounter] = m_statusLast;
if (m_on_message_cb && m_counter_status % 10 == 0) // callback
if (m_onFilteredStatusUpdate && m_statusCounter % m_filterSize == 0)
{ {
m_on_message_cb(m_info_filtered); m_onFilteredStatusUpdate(m_statusFiltered);
} }
// update data counter // update data counter
m_counter_status++; m_statusCounter++;
} }
else else
{ {
if (millis() - m_last_data > c_idle_time) if (millis() - m_dataLast > c_idle_time)
{ {
if (m_counter_status > 0 && !m_partial_save) if (m_statusCounter > 0 && !m_savePartial)
{ {
LOG_DEBUG("Save Partial: ", m_counter_status); LOG_DEBUG("Save Partial: ", m_statusCounter);
m_active_history->resize(m_counter_status); m_historyActive->resize(m_statusCounter);
saveHistory(*m_active_history, m_history_path); saveHistory(*m_historyActive, m_historyPath);
m_active_history->resize(m_max_history); m_historyActive->resize(m_historyMax);
m_counter_status = 0; m_statusCounter = 0;
m_partial_save = true; m_savePartial = true;
} }
m_manager_status = rtTaskStatus::IDLE; m_managerStatus = rtTaskStatus::IDLE;
} }
} }
} }
@@ -441,22 +442,22 @@ const bool rtIgnitionTask::start()
m_params.rt_stack_size, m_params.rt_stack_size,
(void *)&m_params, (void *)&m_params,
m_params.rt_priority, m_params.rt_priority,
&m_rt_handle, &m_rtHandle,
m_core); m_core);
const bool success = task_success == pdPASS && m_rt_handle != nullptr; const bool success = task_success == pdPASS && m_rtHandle != nullptr;
if (success) if (success)
m_manager_status = rtTaskStatus::IDLE; m_managerStatus = rtTaskStatus::IDLE;
return success; return success;
} }
const bool rtIgnitionTask::stop() const bool rtIgnitionTask::stop()
{ {
LOG_WARN("Ending Task [", m_params.name.c_str(), "]"); LOG_WARN("Ending Task [", m_params.name.c_str(), "]");
if (m_rt_handle) if (m_rtHandle)
{ {
m_params.rt_running = false; m_params.rt_running = false;
m_rt_handle = nullptr; m_rtHandle = nullptr;
m_manager_status = rtTaskStatus::STOPPED; m_managerStatus = rtTaskStatus::STOPPED;
return true; return true;
} }
return false; return false;
@@ -464,26 +465,26 @@ const bool rtIgnitionTask::stop()
const ignitionBoxStatus rtIgnitionTask::getLast() const const ignitionBoxStatus rtIgnitionTask::getLast() const
{ {
return m_last_status; return m_statusLast;
} }
const ignitionBoxStatusFiltered rtIgnitionTask::getFiltered() const const ignitionBoxStatusFiltered rtIgnitionTask::getFiltered() const
{ {
return m_info_filtered; return m_statusFiltered;
} }
const rtIgnitionTask::rtTaskStatus rtIgnitionTask::getStatus() const const rtIgnitionTask::rtTaskStatus rtIgnitionTask::getStatus() const
{ {
return m_manager_status; return m_managerStatus;
} }
void rtIgnitionTask::enableSave(const bool enable, const std::filesystem::path filename) void rtIgnitionTask::enableSave(const bool enable, const std::filesystem::path filename)
{ {
m_enable_save = enable; m_historySaveEnable = enable;
if (enable && !filename.empty()) if (enable && !filename.empty())
{ {
LOG_WARN("Save History Enabled Task [", m_params.name.c_str(), "]"); LOG_WARN("Save History Enabled Task [", m_params.name.c_str(), "]");
m_history_path = m_filesystem.mountpoint() / filename; m_historyPath = m_filesystem.mountpoint() / filename;
} }
else else
{ {
@@ -493,13 +494,13 @@ void rtIgnitionTask::enableSave(const bool enable, const std::filesystem::path f
void rtIgnitionTask::onMessage(std::function<void(ignitionBoxStatusFiltered)> callaback) void rtIgnitionTask::onMessage(std::function<void(ignitionBoxStatusFiltered)> callaback)
{ {
m_on_message_cb = callaback; m_onFilteredStatusUpdate = callaback;
} }
void rtIgnitionTask::saveHistory(const rtIgnitionTask::PSHistory &history, const std::filesystem::path &file_name) void rtIgnitionTask::saveHistory(const rtIgnitionTask::PSHistory &history, const std::filesystem::path &file_name)
{ {
// Lock filesystem mutex to avoid concurrent access // Lock filesystem mutex to avoid concurrent access
std::lock_guard<std::mutex> fs_lock(m_fs_mutex); std::lock_guard<std::mutex> fs_lock(m_filesystemMutex);
// Check for free space // Check for free space
if (LittleFS.totalBytes() - LittleFS.usedBytes() < history.size() * sizeof(ignitionBoxStatus)) // check if at least 1MB is free for saving history if (LittleFS.totalBytes() - LittleFS.usedBytes() < history.size() * sizeof(ignitionBoxStatus)) // check if at least 1MB is free for saving history
@@ -516,7 +517,7 @@ void rtIgnitionTask::saveHistory(const rtIgnitionTask::PSHistory &history, const
// if firt save remove old file and create new // if firt save remove old file and create new
auto save_flags = std::ios::out; auto save_flags = std::ios::out;
if (m_first_save) if (m_saveFirst)
{ {
save_flags |= std::ios::trunc; // overwrite existing file 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 m_filesystem.remove(file_path.c_str()); // ensure file is removed before saving to avoid issues with appending to existing file in SPIFFS
@@ -536,14 +537,14 @@ void rtIgnitionTask::saveHistory(const rtIgnitionTask::PSHistory &history, const
} }
// write csv header // write csv header
if (m_first_save) if (m_saveFirst)
{ {
ofs << "TS,EVENTS_12,DLY_12,STAT_12,V_12_1,V_12_2,V_12_3,V_12_4,IGNITION_MODE_12," 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," << "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" << "ENGINE_RPM,ADC_READTIME,N_QUEUE_ERRORS"
<< std::endl; << std::endl;
ofs.flush(); ofs.flush();
m_first_save = false; m_saveFirst = false;
} }
for (const auto &entry : history) for (const auto &entry : history)

View File

@@ -123,35 +123,35 @@ private: // static functions for FreeRTOS
private: private:
bool m_running = true; bool m_running = true;
rtTaskStatus m_manager_status = INIT; rtTaskStatus m_managerStatus = INIT;
std::string m_name;
rtTaskParams m_params; rtTaskParams m_params;
const uint8_t m_core; const uint8_t m_core;
std::string m_managerTaskName;
TaskHandle_t m_rtHandle = nullptr;
TaskHandle_t m_managerHandle = nullptr;
QueueHandle_t m_rtQueueHandle = nullptr;
TaskHandle_t m_rt_handle = nullptr; const uint32_t m_historyMax;
TaskHandle_t m_manager_handle = nullptr; bool m_historySaveEnable = false;
QueueHandle_t m_queue = nullptr; std::filesystem::path m_historyPath;
PSHistory m_historyBuf0;
PSHistory m_historyBuf1;
std::unique_ptr<PSHistory> m_historyActive;
std::unique_ptr<PSHistory> m_historyInactive;
bool m_enable_save = false; bool m_savePartial = false;
std::filesystem::path m_history_path; bool m_saveFirst = true;
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; fs::FS &m_filesystem;
std::mutex &m_fs_mutex; std::mutex &m_filesystemMutex;
bool m_partial_save = false; uint8_t m_filterSize = 10;
bool m_first_save = true; uint32_t m_statusCounter = 0;
uint32_t m_dataLast = 0;
ignitionBoxStatus m_statusLast;
ignitionBoxStatusFiltered m_statusFiltered;
uint32_t m_counter_status = 0; std::function<void(ignitionBoxStatusFiltered)> m_onFilteredStatusUpdate = nullptr;
uint32_t m_last_data = 0;
ignitionBoxStatus m_last_status;
ignitionBoxStatusFiltered m_info_filtered;
std::function<void(ignitionBoxStatusFiltered)> m_on_message_cb = nullptr;
static const uint32_t c_idle_time = 10000; // in mS static const uint32_t c_idle_time = 10000; // in mS
static const uint32_t c_spark_timeout_max = 500; // uS static const uint32_t c_spark_timeout_max = 500; // uS