Webpage is OK, html in memory since SPIFFS is to slow.
Moving average to be fixed
This commit is contained in:
@@ -2,12 +2,106 @@
|
||||
|
||||
static const size_t min_free = 1024 * 1024; // minimum free space in SPIFFS to allow saving history (1MB)
|
||||
|
||||
void ignitionBoxStatusAverage::reset()
|
||||
{
|
||||
m_last = ignitionBoxStatus();
|
||||
m_count = 0;
|
||||
m_data_valid = false;
|
||||
}
|
||||
|
||||
void ignitionBoxStatusAverage::update(const ignitionBoxStatus &new_status)
|
||||
{
|
||||
if (m_count == 0)
|
||||
{
|
||||
m_last = new_status;
|
||||
}
|
||||
else
|
||||
{
|
||||
// simple moving average calculation
|
||||
m_last.timestamp = new_status.timestamp; // keep timestamp of latest status
|
||||
|
||||
m_last.coils12.n_events = new_status.coils12.n_events; // sum events instead of averaging
|
||||
m_last.coils12.n_missed_firing = new_status.coils12.n_missed_firing; // sum missed firings instead of averaging
|
||||
m_last.coils12.spark_status = new_status.coils12.spark_status; // take latest spark status
|
||||
m_last.coils12.sstart_status = new_status.coils12.sstart_status; // take latest soft start status
|
||||
m_last.coils12.spark_delay += (uint32_t)(0.1f * (float)(new_status.coils12.spark_delay - m_last.coils12.spark_delay)); // incremental average calculation
|
||||
m_last.coils12.peak_p_in += 1.0f / m_max_count * (new_status.coils12.peak_p_in - m_last.coils12.peak_p_in); // incremental average calculation
|
||||
m_last.coils12.peak_n_in += 1.0f / m_max_count * (new_status.coils12.peak_n_in - m_last.coils12.peak_n_in); // incremental average calculation
|
||||
m_last.coils12.peak_p_out += 1.0f / m_max_count * (new_status.coils12.peak_p_out - m_last.coils12.peak_p_out); // incremental average calculation
|
||||
m_last.coils12.peak_n_out += 1.0f / m_max_count * (new_status.coils12.peak_n_out - m_last.coils12.peak_n_out); // incremental average calculation
|
||||
|
||||
m_last.coils34.n_events = new_status.coils34.n_events; // sum events instead of averaging
|
||||
m_last.coils34.n_missed_firing = new_status.coils34.n_missed_firing; // sum missed firings instead of averaging
|
||||
m_last.coils34.spark_status = new_status.coils34.spark_status; // take latest spark status
|
||||
m_last.coils34.sstart_status = new_status.coils34.sstart_status; // take latest soft start status
|
||||
m_last.coils34.spark_delay += (uint32_t)(0.1f * (float)(new_status.coils34.spark_delay - m_last.coils34.spark_delay)); // incremental average calculation
|
||||
m_last.coils34.peak_p_in += 1.0f / m_max_count * (new_status.coils34.peak_p_in - m_last.coils34.peak_p_in); // incremental average calculation
|
||||
m_last.coils34.peak_n_in += 1.0f / m_max_count * (new_status.coils34.peak_n_in - m_last.coils34.peak_n_in); // incremental average calculation
|
||||
m_last.coils34.peak_p_out += 1.0f / m_max_count * (new_status.coils34.peak_p_out - m_last.coils34.peak_p_out); // incremental average calculation
|
||||
m_last.coils34.peak_n_out += 1.0f / m_max_count * (new_status.coils34.peak_n_out - m_last.coils34.peak_n_out); // incremental average calculation
|
||||
|
||||
m_last.eng_rpm += (uint32_t)(0.1f * (float)(new_status.eng_rpm - m_last.eng_rpm)); // incremental average calculation
|
||||
m_last.adc_read_time += (uint32_t)(0.1f * (float)(new_status.adc_read_time - m_last.adc_read_time)); // incremental average calculation
|
||||
m_last.n_queue_errors = new_status.n_queue_errors; // take last of queue errors since it's a cumulative count of errors in the queue, not an average value
|
||||
}
|
||||
m_count++;
|
||||
if (m_count >= m_max_count)
|
||||
{
|
||||
m_count = 0; // reset count after reaching max samples to average
|
||||
m_data_valid = true; // set data valid flag after first average is calculated
|
||||
}
|
||||
}
|
||||
|
||||
const bool ignitionBoxStatusAverage::get(ignitionBoxStatus &status) const
|
||||
{
|
||||
if (m_data_valid)
|
||||
{
|
||||
status = m_last;
|
||||
}
|
||||
return m_data_valid;
|
||||
}
|
||||
|
||||
const ArduinoJson::JsonDocument ignitionBoxStatusAverage::toJson() const
|
||||
{
|
||||
ArduinoJson::JsonDocument doc;
|
||||
if (m_data_valid)
|
||||
{
|
||||
doc["timestamp"] = m_last.timestamp;
|
||||
|
||||
doc["coils12"]["n_events"] = m_last.coils12.n_events;
|
||||
doc["coils12"]["n_missed_firing"] = m_last.coils12.n_missed_firing;
|
||||
doc["coils12"]["spark_delay"] = m_last.coils12.spark_delay;
|
||||
doc["coils12"]["spark_status"] = sparkStatusNames.at(m_last.coils12.spark_status);
|
||||
doc["coils12"]["peak_p_in"] = m_last.coils12.peak_p_in;
|
||||
doc["coils12"]["peak_n_in"] = m_last.coils12.peak_n_in;
|
||||
doc["coils12"]["peak_p_out"] = m_last.coils12.peak_p_out;
|
||||
doc["coils12"]["peak_n_out"] = m_last.coils12.peak_n_out;
|
||||
doc["coils12"]["sstart_status"] = softStartStatusNames.at(m_last.coils12.sstart_status);
|
||||
|
||||
doc["coils34"]["n_events"] = m_last.coils34.n_events;
|
||||
doc["coils34"]["n_missed_firing"] = m_last.coils34.n_missed_firing;
|
||||
doc["coils34"]["spark_delay"] = m_last.coils34.spark_delay;
|
||||
doc["coils34"]["spark_status"] = sparkStatusNames.at(m_last.coils34.spark_status);
|
||||
doc["coils34"]["peak_p_in"] = m_last.coils34.peak_p_in;
|
||||
doc["coils34"]["peak_n_in"] = m_last.coils34.peak_n_in;
|
||||
doc["coils34"]["peak_p_out"] = m_last.coils34.peak_p_out;
|
||||
doc["coils34"]["peak_n_out"] = m_last.coils34.peak_n_out;
|
||||
doc["coils34"]["sstart_status"] = softStartStatusNames.at(m_last.coils34.sstart_status);
|
||||
|
||||
doc["eng_rpm"] = m_last.eng_rpm;
|
||||
doc["adc_read_time"] = m_last.adc_read_time;
|
||||
doc["n_queue_errors"] = m_last.n_queue_errors;
|
||||
}
|
||||
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) {
|
||||
if (!params)
|
||||
{
|
||||
LOG_ERROR("Invalid parameters for saveHistoryTask");
|
||||
return;
|
||||
}
|
||||
@@ -19,9 +113,10 @@ void saveHistoryTask(void *pvParameters)
|
||||
void save_history(const PSRAMVector<ignitionBoxStatus> &history, const std::filesystem::path &file_name)
|
||||
{
|
||||
// Initialize SPIFFS
|
||||
auto spiffs_guard = SPIFFSGuard(); // use RAII guard to ensure SPIFFS is properly mounted and unmounted
|
||||
if (!SAVE_HISTORY_TO_SPIFFS) return;
|
||||
//auto spiffs_guard = SPIFFSGuard(); // use RAII guard to ensure SPIFFS is properly mounted and unmounted
|
||||
|
||||
if (SPIFFS.totalBytes() - SPIFFS.usedBytes() < min_free ) // check if at least 1MB is free for saving history
|
||||
if (SPIFFS.totalBytes() - SPIFFS.usedBytes() < min_free) // check if at least 1MB is free for saving history
|
||||
{
|
||||
LOG_ERROR("Not enough space in SPIFFS to save history");
|
||||
return;
|
||||
@@ -35,7 +130,7 @@ void save_history(const PSRAMVector<ignitionBoxStatus> &history, const std::file
|
||||
if (first_save && SPIFFS.exists(file_path.c_str()))
|
||||
{
|
||||
first_save = false;
|
||||
save_flags |= std::ios::trunc; // overwrite existing file
|
||||
save_flags |= std::ios::trunc; // overwrite existing file
|
||||
SPIFFS.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 SPIFFS, new file:", file_path.c_str());
|
||||
}
|
||||
@@ -58,7 +153,8 @@ void save_history(const PSRAMVector<ignitionBoxStatus> &history, const std::file
|
||||
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;
|
||||
ENGINE_RPM,ADC_READTIME,N_QUEUE_ERRORS"
|
||||
<< std::endl;
|
||||
ofs.flush();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user