Compare commits
6 Commits
adc
...
7c96101cdd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7c96101cdd | ||
|
|
877236ee4e | ||
|
|
668b590d7c | ||
|
|
f36cb96f21 | ||
|
|
dc44decd64 | ||
|
|
0d0db29bba |
1
RotaxMonitor/.gitignore
vendored
1
RotaxMonitor/.gitignore
vendored
@@ -3,3 +3,4 @@
|
|||||||
.vscode/c_cpp_properties.json
|
.vscode/c_cpp_properties.json
|
||||||
.vscode/launch.json
|
.vscode/launch.json
|
||||||
.vscode/ipch
|
.vscode/ipch
|
||||||
|
unpacked_fs
|
||||||
|
|||||||
3
RotaxMonitor/data/config.json
Normal file
3
RotaxMonitor/data/config.json
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
6
RotaxMonitor/partitions/no_ota_10mb_spiffs.csv
Normal file
6
RotaxMonitor/partitions/no_ota_10mb_spiffs.csv
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
# ESP32 Partition Table
|
||||||
|
# Name, Type, SubType, Offset, Size
|
||||||
|
nvs, data, nvs, 0x9000, 0x4000
|
||||||
|
phy_init, data, phy, 0xd000, 0x1000
|
||||||
|
factory, app, factory, 0x10000, 0x5F0000
|
||||||
|
spiffs, data, spiffs, 0x600000, 0xA00000
|
||||||
|
@@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
[env:esp32-s3-devkitc1-n16r8]
|
[env:esp32-s3-devkitc1-n16r8]
|
||||||
board = esp32-s3-devkitc1-n16r8
|
board = esp32-s3-devkitc1-n16r8
|
||||||
|
board_build.partitions = partitions/no_ota_10mb_spiffs.csv
|
||||||
platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip
|
platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip
|
||||||
framework = arduino
|
framework = arduino
|
||||||
lib_deps =
|
lib_deps =
|
||||||
@@ -21,11 +22,11 @@ lib_deps =
|
|||||||
|
|
||||||
;Upload protocol configuration
|
;Upload protocol configuration
|
||||||
upload_protocol = esptool
|
upload_protocol = esptool
|
||||||
upload_port = /dev/ttyACM2
|
upload_port = COM8
|
||||||
upload_speed = 921600
|
upload_speed = 921600
|
||||||
|
|
||||||
;Monitor configuration
|
;Monitor configuration
|
||||||
monitor_port = /dev/ttyACM2
|
monitor_port = COM4
|
||||||
monitor_speed = 921600
|
monitor_speed = 921600
|
||||||
|
|
||||||
; Build configuration
|
; Build configuration
|
||||||
@@ -39,17 +40,18 @@ build_flags =
|
|||||||
|
|
||||||
[env:esp32-s3-devkitc1-n16r8-debug]
|
[env:esp32-s3-devkitc1-n16r8-debug]
|
||||||
board = ${env:esp32-s3-devkitc1-n16r8.board}
|
board = ${env:esp32-s3-devkitc1-n16r8.board}
|
||||||
|
board_build.partitions = ${env:esp32-s3-devkitc1-n16r8.board_build.partitions}
|
||||||
platform = ${env:esp32-s3-devkitc1-n16r8.platform}
|
platform = ${env:esp32-s3-devkitc1-n16r8.platform}
|
||||||
framework = ${env:esp32-s3-devkitc1-n16r8.framework}
|
framework = ${env:esp32-s3-devkitc1-n16r8.framework}
|
||||||
lib_deps = ${env:esp32-s3-devkitc1-n16r8.lib_deps}
|
lib_deps = ${env:esp32-s3-devkitc1-n16r8.lib_deps}
|
||||||
|
|
||||||
;Upload protocol configuration
|
;Upload protocol configuration
|
||||||
upload_protocol = esptool
|
upload_protocol = esptool
|
||||||
upload_port = /dev/ttyACM2
|
upload_port = COM4
|
||||||
upload_speed = 921600
|
upload_speed = 921600
|
||||||
|
|
||||||
;Monitor configuration
|
;Monitor configuration
|
||||||
monitor_port = /dev/ttyACM2
|
monitor_port = COM4
|
||||||
monitor_speed = 921600
|
monitor_speed = 921600
|
||||||
|
|
||||||
; Debug configuration
|
; Debug configuration
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
// ADC Channels
|
|
||||||
|
|
||||||
#define A1_RAW 0
|
|
||||||
#define A2_RAW 1
|
|
||||||
#define B1_RAW 2
|
|
||||||
#define B2_RAW 3
|
|
||||||
|
|
||||||
#define A1_COND 4
|
|
||||||
#define A2_COND 5
|
|
||||||
#define B1_COND 6
|
|
||||||
#define B2_COND 7
|
|
||||||
88
RotaxMonitor/src/datasave.cpp
Normal file
88
RotaxMonitor/src/datasave.cpp
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
#include "datasave.h"
|
||||||
|
|
||||||
|
static const size_t min_free = 1024 * 1024; // minimum free space in SPIFFS to allow saving history (1MB)
|
||||||
|
static bool first_save = true; // flag to indicate if this is the first save (to write header)
|
||||||
|
|
||||||
|
void save_history(const PSRAMVector<ignitionBoxStatus> &history, const std::filesystem::path &file_path)
|
||||||
|
{
|
||||||
|
// Initialize SPIFFS
|
||||||
|
if (!SPIFFS.begin(true))
|
||||||
|
{
|
||||||
|
LOG_ERROR("Failed to mount SPIFFS");
|
||||||
|
LOG_ERROR("5 seconds to restart...");
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||||
|
esp_restart();
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_INFO("SPIFFS mounted successfully");
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::filesystem::path to_save = file_path;
|
||||||
|
if (file_path.root_path() != "/spiffs")
|
||||||
|
to_save = std::filesystem::path("/spiffs") / file_path;
|
||||||
|
|
||||||
|
auto save_flags = std::ios::out;
|
||||||
|
if (first_save && SPIFFS.exists(to_save.c_str()))
|
||||||
|
{
|
||||||
|
first_save = false;
|
||||||
|
save_flags |= std::ios::trunc; // overwrite existing file
|
||||||
|
SPIFFS.remove(to_save.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: ", to_save.c_str());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
save_flags |= std::ios::app; // append to new file
|
||||||
|
LOG_INFO("Saving history to SPIFFS, appending to existing file: ", to_save.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ofstream ofs(to_save, 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 SPIFFS, records written: ", history.size());
|
||||||
|
SPIFFS.end(); // unmount SPIFFS to ensure data is written and avoid corruption on next mount
|
||||||
|
}
|
||||||
38
RotaxMonitor/src/datasave.h
Normal file
38
RotaxMonitor/src/datasave.h
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
// System Includes
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <DebugLog.h>
|
||||||
|
#include <SPIFFS.h>
|
||||||
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
// Project Includes
|
||||||
|
#include "isr.h"
|
||||||
|
#include "psvector.h"
|
||||||
|
|
||||||
|
const uint32_t max_history = 256;
|
||||||
|
const bool SAVE_HISTORY_TO_SPIFFS = true; // Set to true to enable saving history to SPIFFS, false to disable
|
||||||
|
|
||||||
|
struct dataSaveParams
|
||||||
|
{
|
||||||
|
const PSRAMVector<ignitionBoxStatus> *history;
|
||||||
|
const std::filesystem::path file_path;
|
||||||
|
};
|
||||||
|
|
||||||
|
void save_history(const PSRAMVector<ignitionBoxStatus> &history, const std::filesystem::path& file_path);
|
||||||
|
|
||||||
|
static 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_INFO("Starting saving: ", file_path.c_str());
|
||||||
|
save_history(history, file_path);
|
||||||
|
//vTaskDelete(NULL);
|
||||||
|
}
|
||||||
90
RotaxMonitor/src/datastruct.h
Normal file
90
RotaxMonitor/src/datastruct.h
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
// =====================
|
||||||
|
// Event Flags (bitmask)
|
||||||
|
// =====================
|
||||||
|
static const uint32_t TRIG_FLAG_12P = (1 << 0);
|
||||||
|
static const uint32_t TRIG_FLAG_12N = (1 << 1);
|
||||||
|
static const uint32_t TRIG_FLAG_34P = (1 << 2);
|
||||||
|
static const uint32_t TRIG_FLAG_34N = (1 << 3);
|
||||||
|
|
||||||
|
static const uint32_t SPARK_FLAG_TIMEOUT = (1 << 8);
|
||||||
|
static const uint32_t SPARK_FLAG_12 = (1 << 9);
|
||||||
|
static const uint32_t SPARK_FLAG_34 = (1 << 10);
|
||||||
|
|
||||||
|
// Spark Status
|
||||||
|
enum sparkStatus
|
||||||
|
{
|
||||||
|
SPARK_POS_OK,
|
||||||
|
SPARK_NEG_OK,
|
||||||
|
SPARK_POS_SKIP,
|
||||||
|
SPARK_NEG_SKIP,
|
||||||
|
SPARK_POS_WAIT,
|
||||||
|
SPARK_NEG_WAIT,
|
||||||
|
SPARK_POS_FAIL,
|
||||||
|
SPARK_NEG_FAIL,
|
||||||
|
SPARK_POS_UNEXPECTED,
|
||||||
|
SPARK_NEG_UNEXPECTED,
|
||||||
|
SPARK_SYNC_FAIL,
|
||||||
|
};
|
||||||
|
|
||||||
|
static const std::map<const sparkStatus, const char *> sparkStatusNames = {
|
||||||
|
{SPARK_POS_OK, "SPARK_POS_OK"},
|
||||||
|
{SPARK_NEG_OK, "SPARK_NEG_OK"},
|
||||||
|
{SPARK_POS_SKIP, "SPARK_POS_SKIP"},
|
||||||
|
{SPARK_NEG_SKIP, "SPARK_NEG_SKIP"},
|
||||||
|
{SPARK_POS_WAIT, "SPARK_POS_WAIT"},
|
||||||
|
{SPARK_NEG_WAIT, "SPARK_NEG_WAIT"},
|
||||||
|
{SPARK_POS_FAIL, "SPARK_POS_FAIL"},
|
||||||
|
{SPARK_NEG_FAIL, "SPARK_NEG_FAIL"},
|
||||||
|
{SPARK_POS_UNEXPECTED, "SPARK_POS_UNEXPECTED"},
|
||||||
|
{SPARK_NEG_UNEXPECTED, "SPARK_NEG_UNEXPECTED"},
|
||||||
|
{SPARK_SYNC_FAIL, "SPARK_SYNC_FAIL"},
|
||||||
|
};
|
||||||
|
|
||||||
|
enum softStartStatus
|
||||||
|
{
|
||||||
|
NORMAL,
|
||||||
|
SOFT_START,
|
||||||
|
ERROR,
|
||||||
|
};
|
||||||
|
|
||||||
|
const std::map<const softStartStatus, const char *> softStartStatusNames = {
|
||||||
|
{NORMAL, "NORMAL"},
|
||||||
|
{SOFT_START, "SOFT_START"},
|
||||||
|
{ERROR, "ERROR"},
|
||||||
|
};
|
||||||
|
|
||||||
|
struct coilsStatus
|
||||||
|
{
|
||||||
|
int64_t trig_time = 0;
|
||||||
|
int64_t spark_time = 0;
|
||||||
|
uint32_t spark_delay = 0; // in microseconds
|
||||||
|
sparkStatus spark_status = sparkStatus::SPARK_POS_OK;
|
||||||
|
softStartStatus sstart_status = softStartStatus::NORMAL;
|
||||||
|
float peak_p_in = 0.0;
|
||||||
|
float peak_n_in = 0.0;
|
||||||
|
float peak_p_out = 0.0;
|
||||||
|
float peak_n_out = 0.0;
|
||||||
|
float level_spark = 0.0;
|
||||||
|
uint32_t n_events = 0;
|
||||||
|
uint32_t n_missed_firing = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Task internal Status
|
||||||
|
struct ignitionBoxStatus
|
||||||
|
{
|
||||||
|
int64_t timestamp = 0;
|
||||||
|
// coils pairs for each ignition
|
||||||
|
coilsStatus coils12;
|
||||||
|
coilsStatus coils34;
|
||||||
|
// voltage from generator
|
||||||
|
float volts_gen = 0.0;
|
||||||
|
// enine rpm
|
||||||
|
uint32_t eng_rpm = 0;
|
||||||
|
// debug values
|
||||||
|
uint32_t n_queue_errors = 0;
|
||||||
|
uint32_t adc_read_time = 0;
|
||||||
|
};
|
||||||
@@ -36,18 +36,13 @@ void trig_isr(void *arg)
|
|||||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||||
break;
|
break;
|
||||||
case SPARK_FLAG_12:
|
case SPARK_FLAG_12:
|
||||||
box->coils34.spark_ok = false;
|
|
||||||
box->coils12.spark_ok = true;
|
|
||||||
box->coils12.spark_time = time_us;
|
box->coils12.spark_time = time_us;
|
||||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||||
// vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken);
|
|
||||||
break;
|
break;
|
||||||
case SPARK_FLAG_34:
|
case SPARK_FLAG_34:
|
||||||
box->coils12.spark_ok = false;
|
|
||||||
box->coils34.spark_ok = true;
|
|
||||||
box->coils34.spark_time = time_us;
|
box->coils34.spark_time = time_us;
|
||||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||||
// vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken);
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -12,95 +12,12 @@
|
|||||||
#else
|
#else
|
||||||
#include "pins_test.h"
|
#include "pins_test.h"
|
||||||
#endif
|
#endif
|
||||||
|
#include "datastruct.h"
|
||||||
|
|
||||||
#define CORE_0 0
|
#define CORE_0 0
|
||||||
#define CORE_1 1
|
#define CORE_1 1
|
||||||
#define TASK_STACK 4096 // in words
|
#define RT_TASK_STACK 4096 // in words
|
||||||
#define TASK_PRIORITY (configMAX_PRIORITIES - 4) // highest priority after wifi tasks
|
#define RT_TASK_PRIORITY (configMAX_PRIORITIES - 4) // highest priority after wifi tasks
|
||||||
|
|
||||||
// =====================
|
|
||||||
// Event Flags (bitmask)
|
|
||||||
// =====================
|
|
||||||
static const uint32_t TRIG_FLAG_12P = (1 << 0);
|
|
||||||
static const uint32_t TRIG_FLAG_12N = (1 << 1);
|
|
||||||
static const uint32_t TRIG_FLAG_34P = (1 << 2);
|
|
||||||
static const uint32_t TRIG_FLAG_34N = (1 << 3);
|
|
||||||
|
|
||||||
static const uint32_t SPARK_FLAG_NIL = (1 << 8);
|
|
||||||
static const uint32_t SPARK_FLAG_12 = (1 << 9);
|
|
||||||
static const uint32_t SPARK_FLAG_34 = (1 << 10);
|
|
||||||
static const uint32_t SPARK_FLAG_TIMEOUT = (1 << 11);
|
|
||||||
|
|
||||||
// Spark Status
|
|
||||||
enum sparkStatus
|
|
||||||
{
|
|
||||||
SPARK_POS_OK,
|
|
||||||
SPARK_NEG_OK,
|
|
||||||
SPARK_POS_SKIP,
|
|
||||||
SPARK_NEG_SKIP,
|
|
||||||
SPARK_POS_WAIT,
|
|
||||||
SPARK_NEG_WAIT,
|
|
||||||
SPARK_POS_FAIL,
|
|
||||||
SPARK_NEG_FAIL,
|
|
||||||
SPARK_POS_UNEXPECTED,
|
|
||||||
SPARK_NEG_UNEXPECTED,
|
|
||||||
SPARK_SYNC_FAIL,
|
|
||||||
};
|
|
||||||
|
|
||||||
static const std::map<const sparkStatus, const char *> sparkStatusNames = {
|
|
||||||
{SPARK_POS_OK, "SPARK_POS_OK"},
|
|
||||||
{SPARK_NEG_OK, "SPARK_NEG_OK"},
|
|
||||||
{SPARK_POS_SKIP, "SPARK_POS_SKIP"},
|
|
||||||
{SPARK_NEG_SKIP, "SPARK_NEG_SKIP"},
|
|
||||||
{SPARK_POS_WAIT, "SPARK_POS_WAIT"},
|
|
||||||
{SPARK_NEG_WAIT, "SPARK_NEG_WAIT"},
|
|
||||||
{SPARK_POS_FAIL, "SPARK_POS_FAIL"},
|
|
||||||
{SPARK_NEG_FAIL, "SPARK_NEG_FAIL"},
|
|
||||||
{SPARK_POS_UNEXPECTED, "SPARK_POS_UNEXPECTED"},
|
|
||||||
{SPARK_NEG_UNEXPECTED, "SPARK_NEG_UNEXPECTED"},
|
|
||||||
{SPARK_SYNC_FAIL, "SPARK_SYNC_FAIL"},
|
|
||||||
};
|
|
||||||
|
|
||||||
enum softStartStatus
|
|
||||||
{
|
|
||||||
NORMAL,
|
|
||||||
SOFT_START,
|
|
||||||
ERROR,
|
|
||||||
};
|
|
||||||
|
|
||||||
const std::map<const softStartStatus, const char *> softStartStatusNames = {
|
|
||||||
{NORMAL, "NORMAL"},
|
|
||||||
{SOFT_START, "SOFT_START"},
|
|
||||||
{ERROR, "ERROR"},
|
|
||||||
};
|
|
||||||
|
|
||||||
struct coilsStatus
|
|
||||||
{
|
|
||||||
int64_t trig_time = 0;
|
|
||||||
int64_t spark_time = 0;
|
|
||||||
int64_t spark_delay = 0; // in microseconds
|
|
||||||
sparkStatus spark_status = sparkStatus::SPARK_POS_OK;
|
|
||||||
softStartStatus sstart_status = softStartStatus::NORMAL;
|
|
||||||
float peak_p_in = 0.0, peak_n_in = 0.0;
|
|
||||||
float peak_p_out = 0.0, peak_n_out = 0.0;
|
|
||||||
float trigger_spark = 0.0;
|
|
||||||
bool spark_ok = false;
|
|
||||||
uint32_t n_events = 0;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
// Task internal Status
|
|
||||||
struct ignitionBoxStatus
|
|
||||||
{
|
|
||||||
int64_t timestamp = 0;
|
|
||||||
// coils pairs for each ignition
|
|
||||||
coilsStatus coils12;
|
|
||||||
coilsStatus coils34;
|
|
||||||
// voltage from generator
|
|
||||||
float volts_gen = 0.0;
|
|
||||||
uint32_t n_queue_errors = 0;
|
|
||||||
uint32_t adc_read_time = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct isrParams
|
struct isrParams
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,8 +8,8 @@
|
|||||||
|
|
||||||
// Definitions
|
// Definitions
|
||||||
#include <tasks.h>
|
#include <tasks.h>
|
||||||
#include <channels.h>
|
|
||||||
#include <devices.h>
|
#include <devices.h>
|
||||||
|
#include <datasave.h>
|
||||||
#include <ui.h>
|
#include <ui.h>
|
||||||
|
|
||||||
// FreeRTOS directives
|
// FreeRTOS directives
|
||||||
@@ -19,18 +19,6 @@
|
|||||||
// #define CH_B_ENABLE
|
// #define CH_B_ENABLE
|
||||||
#define TEST
|
#define TEST
|
||||||
|
|
||||||
float freqToRPM(float freq)
|
|
||||||
{
|
|
||||||
return freq * 60.0f; // 1 pulse per revolution
|
|
||||||
}
|
|
||||||
|
|
||||||
void printTaskStats()
|
|
||||||
{
|
|
||||||
char buffer[1024];
|
|
||||||
vTaskGetRunTimeStats(buffer);
|
|
||||||
Serial.println(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(921600);
|
Serial.begin(921600);
|
||||||
@@ -62,14 +50,20 @@ void loop()
|
|||||||
{
|
{
|
||||||
// global variables
|
// global variables
|
||||||
bool running = true;
|
bool running = true;
|
||||||
static Devices dev;
|
const uint32_t max_queue = 128;
|
||||||
|
PSRAMVector<ignitionBoxStatus> ignA_history_0(max_history);
|
||||||
|
PSRAMVector<ignitionBoxStatus> ignA_history_1(max_history);
|
||||||
|
auto *active_history = &ignA_history_0;
|
||||||
|
auto *writable_history = &ignA_history_1;
|
||||||
|
|
||||||
|
// Resources Initialization
|
||||||
|
static Devices dev;
|
||||||
// Task handle
|
// Task handle
|
||||||
static TaskHandle_t trigA_TaskHandle = NULL;
|
static TaskHandle_t trigA_TaskHandle = NULL;
|
||||||
static TaskHandle_t trigB_TaskHandle = NULL;
|
static TaskHandle_t trigB_TaskHandle = NULL;
|
||||||
|
// Data Queue for real time task to main loop communication
|
||||||
static QueueHandle_t rt_taskA_queue = xQueueCreate(10, sizeof(ignitionBoxStatus));
|
static QueueHandle_t rt_taskA_queue = xQueueCreate(max_queue, sizeof(ignitionBoxStatus));
|
||||||
static QueueHandle_t rt_taskB_queue = xQueueCreate(10, sizeof(ignitionBoxStatus));
|
static QueueHandle_t rt_taskB_queue = xQueueCreate(max_queue, sizeof(ignitionBoxStatus));
|
||||||
static rtTaskParams taskA_params{
|
static rtTaskParams taskA_params{
|
||||||
.rt_running = true,
|
.rt_running = true,
|
||||||
.dev = &dev,
|
.dev = &dev,
|
||||||
@@ -88,7 +82,7 @@ void loop()
|
|||||||
LOG_INFO("Task Variables OK");
|
LOG_INFO("Task Variables OK");
|
||||||
|
|
||||||
#ifdef CH_B_ENABLE
|
#ifdef CH_B_ENABLE
|
||||||
QueueHandle_t rt_taskB_queue = xQueueCreate(10, sizeof(ignitionBoxStatus));
|
QueueHandle_t rt_taskB_queue = xQueueCreate(max_queue, sizeof(ignitionBoxStatus));
|
||||||
rtTaskParams taskB_params{
|
rtTaskParams taskB_params{
|
||||||
.rt_running = true,
|
.rt_running = true,
|
||||||
.dev = &dev,
|
.dev = &dev,
|
||||||
@@ -105,18 +99,18 @@ void loop()
|
|||||||
.rt_resets = rtTaskResets{.rst_io_12p = RST_EXT_B12P, .rst_io_12n = RST_EXT_B12N, .rst_io_34p = RST_EXT_B34P, .rst_io_34n = RST_EXT_B34N}};
|
.rt_resets = rtTaskResets{.rst_io_12p = RST_EXT_B12P, .rst_io_12n = RST_EXT_B12N, .rst_io_34p = RST_EXT_B34P, .rst_io_34n = RST_EXT_B34N}};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Spi ok flags
|
||||||
bool spiA_ok = true;
|
bool spiA_ok = true;
|
||||||
bool spiB_ok = true;
|
bool spiB_ok = true;
|
||||||
|
|
||||||
// Init 2 SPI interfaces
|
// Init 2 SPI interfaces
|
||||||
SPIClass SPI_A(FSPI);
|
SPIClass SPI_A(FSPI);
|
||||||
spiA_ok = SPI_A.begin(SPI_A_SCK, SPI_A_MISO, SPI_A_MOSI);
|
spiA_ok = SPI_A.begin(SPI_A_SCK, SPI_A_MISO, SPI_A_MOSI);
|
||||||
SPI_A.setDataMode(SPI_MODE1); // ADS1256 requires SPI mode 1
|
SPI_A.setDataMode(SPI_MODE1); // ADS1256 requires SPI mode 1
|
||||||
#ifndef TEST
|
#ifndef TEST
|
||||||
SPIClass SPI_B(HSPI);
|
SPIClass SPI_B(HSPI);
|
||||||
spiB_ok = SPI_B.begin(SPI_B_SCK, SPI_B_MISO, SPI_B_MOSI);
|
spiB_ok = SPI_B.begin(SPI_B_SCK, SPI_B_MISO, SPI_B_MOSI);
|
||||||
SPI_B.setDataMode(SPI_MODE1); // ADS1256 requires SPI mode 1
|
SPI_B.setDataMode(SPI_MODE1); // ADS1256 requires SPI mode 1
|
||||||
#endif
|
#endif
|
||||||
if (!spiA_ok || !spiB_ok)
|
if (!spiA_ok || !spiB_ok)
|
||||||
{
|
{
|
||||||
LOG_ERROR("Unable to Initialize SPI Busses");
|
LOG_ERROR("Unable to Initialize SPI Busses");
|
||||||
@@ -147,9 +141,9 @@ void loop()
|
|||||||
ignA_task_success = xTaskCreatePinnedToCore(
|
ignA_task_success = xTaskCreatePinnedToCore(
|
||||||
rtIgnitionTask,
|
rtIgnitionTask,
|
||||||
"rtIgnitionTask_boxA",
|
"rtIgnitionTask_boxA",
|
||||||
TASK_STACK,
|
RT_TASK_STACK,
|
||||||
(void *)&taskA_params,
|
(void *)&taskA_params,
|
||||||
TASK_PRIORITY,
|
RT_TASK_PRIORITY,
|
||||||
&trigA_TaskHandle,
|
&trigA_TaskHandle,
|
||||||
CORE_0);
|
CORE_0);
|
||||||
|
|
||||||
@@ -159,16 +153,16 @@ void loop()
|
|||||||
ignB_task_success = xTaskCreatePinnedToCore(
|
ignB_task_success = xTaskCreatePinnedToCore(
|
||||||
rtIgnitionTask,
|
rtIgnitionTask,
|
||||||
"rtIgnitionTask_boxB",
|
"rtIgnitionTask_boxB",
|
||||||
TASK_STACK,
|
RT_TASK_STACK,
|
||||||
(void *)&taskB_params,
|
(void *)&taskB_params,
|
||||||
TASK_PRIORITY, // priorità leggermente più alta
|
RT_TASK_PRIORITY, // priorità leggermente più alta
|
||||||
&trigB_TaskHandle,
|
&trigB_TaskHandle,
|
||||||
CORE_1);
|
CORE_1);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if ((ignA_task_success && ignB_task_success) != pdPASS)
|
if ((ignA_task_success && ignB_task_success) != pdPASS)
|
||||||
{
|
{
|
||||||
LOG_ERROR("Unble to initialize ISR task");
|
LOG_ERROR("Una ble to initialize ISR task");
|
||||||
LOG_ERROR("5 seconds to restart...");
|
LOG_ERROR("5 seconds to restart...");
|
||||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||||
esp_restart();
|
esp_restart();
|
||||||
@@ -179,59 +173,58 @@ void loop()
|
|||||||
////////////////////// MAIN LOOP //////////////////////
|
////////////////////// MAIN LOOP //////////////////////
|
||||||
clearScreen();
|
clearScreen();
|
||||||
setCursor(0, 0);
|
setCursor(0, 0);
|
||||||
ignitionBoxStatus ignA;
|
bool partial_save = false; // flag to indicate if a partial save has been done after a timeout
|
||||||
|
uint32_t counter = 0;
|
||||||
|
ignitionBoxStatus ign_info;
|
||||||
int64_t last = esp_timer_get_time();
|
int64_t last = esp_timer_get_time();
|
||||||
uint32_t missed_firings12 = 0;
|
uint32_t missed_firings12 = 0;
|
||||||
uint32_t missed_firings34 = 0;
|
uint32_t missed_firings34 = 0;
|
||||||
uint32_t counter = 0;
|
|
||||||
|
|
||||||
while (running)
|
while (running)
|
||||||
{
|
{
|
||||||
if (xQueueReceive(rt_taskA_queue, &ignA, pdMS_TO_TICKS(1000)) == pdTRUE)
|
if (counter >= active_history->size()) // not concurrent with write task
|
||||||
{
|
{
|
||||||
float freq = (esp_timer_get_time() - last) / 1000000.0f; // in seconds
|
counter = 0;
|
||||||
freq = freq > 0 ? 1.0f / freq : 0; // Calculate frequency (Hz)
|
partial_save = false; // reset partial save flag on new data cycle
|
||||||
last = esp_timer_get_time();
|
auto *temp = active_history;
|
||||||
|
active_history = writable_history; // switch active and writable buffers
|
||||||
|
writable_history = temp; // ensure writable_history points to the buffer we just filled
|
||||||
|
dataSaveParams save_params{
|
||||||
|
.history = writable_history,
|
||||||
|
.file_path = "ignition_history.csv"};
|
||||||
|
saveHistoryTask(&save_params); // directly call the save task function to save without delay, since we already switched buffers and writable_history is now empty and ready for new data
|
||||||
|
// if (SAVE_HISTORY_TO_SPIFFS)
|
||||||
|
// if (pdFAIL ==
|
||||||
|
// xTaskCreatePinnedToCore(
|
||||||
|
// saveHistoryTask,
|
||||||
|
// "saveHistoryTask",
|
||||||
|
// RT_TASK_STACK,
|
||||||
|
// &save_params,
|
||||||
|
// RT_TASK_PRIORITY - 1, // higher priority to ensure it runs asap after buffer switch
|
||||||
|
// NULL,
|
||||||
|
// CORE_1))
|
||||||
|
// {
|
||||||
|
// LOG_ERROR("Unable to create saveHistoryTask");
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
if (ignA.coils12.spark_status == sparkStatus::SPARK_POS_FAIL || ignA.coils12.spark_status == sparkStatus::SPARK_NEG_FAIL)
|
if (xQueueReceive(rt_taskA_queue, &ign_info, pdMS_TO_TICKS(1000)) == pdTRUE)
|
||||||
missed_firings12++;
|
{
|
||||||
if (ignA.coils34.spark_status == sparkStatus::SPARK_POS_FAIL || ignA.coils34.spark_status == sparkStatus::SPARK_NEG_FAIL)
|
// printInfo(ign_info);
|
||||||
missed_firings34++;
|
auto &hist = *active_history;
|
||||||
|
hist[counter++ % active_history->size()] = ign_info;
|
||||||
clearScreen();
|
Serial.print("Data Received: " + String(counter) + "/" + String(hist.size()) + '\r');
|
||||||
setCursor(0, 0);
|
|
||||||
printField("++ Timestamp", (uint32_t)ignA.timestamp);
|
|
||||||
Serial.println("========== Coils 12 =============");
|
|
||||||
printField("Events", (uint32_t)ignA.coils12.n_events);
|
|
||||||
printField("Missed Firing", missed_firings12);
|
|
||||||
printField("Spark Dly", (uint32_t)ignA.coils12.spark_delay);
|
|
||||||
printField("Spark Sts", sparkStatusNames.at(ignA.coils12.spark_status));
|
|
||||||
printField("Peak P_IN", ignA.coils12.peak_p_in);
|
|
||||||
printField("Peak N_IN", ignA.coils12.peak_n_in);
|
|
||||||
printField("Peak P_OUT", ignA.coils12.peak_p_out);
|
|
||||||
printField("Peak N_OUT", ignA.coils12.peak_n_out);
|
|
||||||
printField("Soft Start ", softStartStatusNames.at(ignA.coils12.sstart_status));
|
|
||||||
|
|
||||||
Serial.println("========== Coils 34 =============");
|
|
||||||
printField("Events", (uint32_t)ignA.coils34.n_events);
|
|
||||||
printField("Missed Firing", missed_firings34);
|
|
||||||
printField("Spark Dly", (uint32_t)ignA.coils34.spark_delay);
|
|
||||||
printField("Spark Sts", sparkStatusNames.at(ignA.coils34.spark_status));
|
|
||||||
printField("Peak P_IN", ignA.coils34.peak_p_in);
|
|
||||||
printField("Peak N_IN", ignA.coils34.peak_n_in);
|
|
||||||
printField("Peak P_OUT", ignA.coils34.peak_p_out);
|
|
||||||
printField("Peak N_OUT", ignA.coils34.peak_n_out);
|
|
||||||
printField("Soft Start ", softStartStatusNames.at(ignA.coils34.sstart_status));
|
|
||||||
|
|
||||||
Serial.println("========== END =============");
|
|
||||||
Serial.println();
|
|
||||||
printField("Engine RPM", freqToRPM(freq));
|
|
||||||
printField("ADC Read Time", (uint32_t)ignA.adc_read_time);
|
|
||||||
printField("Queue Errors", (uint32_t)ignA.n_queue_errors);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Serial.println("Waiting for data... ");
|
Serial.println("Waiting for data... ");
|
||||||
|
if (!partial_save && counter > 0) // if timeout occurs but we have unsaved data, save it before next timeout
|
||||||
|
{
|
||||||
|
counter = 0; // reset counter after saving
|
||||||
|
partial_save = true;
|
||||||
|
Serial.println("Saving history to SPIFFS...");
|
||||||
|
save_history(*active_history, "ignition_history.csv");
|
||||||
|
}
|
||||||
delay(500);
|
delay(500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
30
RotaxMonitor/src/psvector.h
Normal file
30
RotaxMonitor/src/psvector.h
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include "esp_heap_caps.h"
|
||||||
|
|
||||||
|
// Allocator custom per PSRAM
|
||||||
|
template <typename T>
|
||||||
|
struct PSRAMAllocator {
|
||||||
|
using value_type = T;
|
||||||
|
|
||||||
|
PSRAMAllocator() noexcept {}
|
||||||
|
|
||||||
|
template <typename U>
|
||||||
|
PSRAMAllocator(const PSRAMAllocator<U>&) noexcept {}
|
||||||
|
|
||||||
|
T* allocate(std::size_t n) {
|
||||||
|
void* ptr = heap_caps_malloc(n * sizeof(T), MALLOC_CAP_SPIRAM);
|
||||||
|
if (!ptr) {
|
||||||
|
throw std::bad_alloc();
|
||||||
|
}
|
||||||
|
return static_cast<T*>(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void deallocate(T* p, std::size_t) noexcept {
|
||||||
|
heap_caps_free(p);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
using PSRAMVector = std::vector<T, PSRAMAllocator<T>>;
|
||||||
@@ -2,7 +2,8 @@
|
|||||||
#include <esp_timer.h>
|
#include <esp_timer.h>
|
||||||
|
|
||||||
// Timeout callback for microsecond precision
|
// Timeout callback for microsecond precision
|
||||||
void spark_timeout_callback(void* arg) {
|
void spark_timeout_callback(void *arg)
|
||||||
|
{
|
||||||
TaskHandle_t handle = (TaskHandle_t)arg;
|
TaskHandle_t handle = (TaskHandle_t)arg;
|
||||||
xTaskNotify(handle, SPARK_FLAG_TIMEOUT, eSetValueWithOverwrite);
|
xTaskNotify(handle, SPARK_FLAG_TIMEOUT, eSetValueWithOverwrite);
|
||||||
}
|
}
|
||||||
@@ -55,17 +56,16 @@ void rtIgnitionTask(void *pvParameters)
|
|||||||
.flag = SPARK_FLAG_34,
|
.flag = SPARK_FLAG_34,
|
||||||
.ign_stat = &ign_box_sts,
|
.ign_stat = &ign_box_sts,
|
||||||
.rt_handle_ptr = rt_handle_ptr};
|
.rt_handle_ptr = rt_handle_ptr};
|
||||||
|
|
||||||
LOG_INFO("rtTask ISR Params OK");
|
LOG_INFO("rtTask ISR Params OK");
|
||||||
|
|
||||||
// Create esp_timer for microsecond precision timeout
|
// Create esp_timer for microsecond precision timeout
|
||||||
esp_timer_handle_t timeout_timer;
|
esp_timer_handle_t timeout_timer;
|
||||||
esp_timer_create_args_t timer_args = {
|
esp_timer_create_args_t timer_args = {
|
||||||
.callback = spark_timeout_callback,
|
.callback = spark_timeout_callback,
|
||||||
.arg = (void*)rt_handle_ptr,
|
.arg = (void *)rt_handle_ptr,
|
||||||
.dispatch_method = ESP_TIMER_TASK,
|
.dispatch_method = ESP_TIMER_TASK,
|
||||||
.name = "spark_timeout"
|
.name = "spark_timeout"};
|
||||||
};
|
|
||||||
esp_timer_create(&timer_args, &timeout_timer);
|
esp_timer_create(&timer_args, &timeout_timer);
|
||||||
|
|
||||||
// Attach Pin Interrupts
|
// Attach Pin Interrupts
|
||||||
@@ -89,6 +89,8 @@ void rtIgnitionTask(void *pvParameters)
|
|||||||
bool first_cycle = true;
|
bool first_cycle = true;
|
||||||
bool cycle12 = false;
|
bool cycle12 = false;
|
||||||
bool cycle34 = false;
|
bool cycle34 = false;
|
||||||
|
int64_t last_cycle_time = 0;
|
||||||
|
uint32_t n_errors = 0;
|
||||||
|
|
||||||
while (params->rt_running)
|
while (params->rt_running)
|
||||||
{
|
{
|
||||||
@@ -101,11 +103,9 @@ void rtIgnitionTask(void *pvParameters)
|
|||||||
ULONG_MAX, // pulisci i primi 8 bit
|
ULONG_MAX, // pulisci i primi 8 bit
|
||||||
&pickup_flag, // valore ricevuto
|
&pickup_flag, // valore ricevuto
|
||||||
portMAX_DELAY);
|
portMAX_DELAY);
|
||||||
|
|
||||||
if (first_cycle && pickup_flag != TRIG_FLAG_12P) // skip first cycle because of possible initial noise on pickup signals at startu
|
if (first_cycle && pickup_flag != TRIG_FLAG_12P) // skip first cycle because of possible initial noise on pickup signals at startu
|
||||||
{
|
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
Serial.print("\033[2J"); // clear screen
|
Serial.print("\033[2J"); // clear screen
|
||||||
@@ -128,35 +128,19 @@ void rtIgnitionTask(void *pvParameters)
|
|||||||
esp_timer_stop(timeout_timer); // stop timer in case it was running from previous cycle
|
esp_timer_stop(timeout_timer); // stop timer in case it was running from previous cycle
|
||||||
esp_timer_start_once(timeout_timer, spark_timeout_max);
|
esp_timer_start_once(timeout_timer, spark_timeout_max);
|
||||||
|
|
||||||
spark_flag = SPARK_FLAG_NIL; // default value in case of timeout, to be set by ISR if spark event occours
|
|
||||||
// WAIT FOR SPARK TO HAPPEN OR TIMEOUT
|
// WAIT FOR SPARK TO HAPPEN OR TIMEOUT
|
||||||
BaseType_t sp = pdFALSE;
|
xTaskNotifyWait(
|
||||||
sp = xTaskNotifyWait(
|
0x00, // non pulire all'ingresso
|
||||||
0x00, // non pulire all'ingresso
|
ULONG_MAX, // pulisci i primi 8 bit
|
||||||
ULONG_MAX, // pulisci i primi 8 bit
|
&spark_flag, // valore ricevuto
|
||||||
&spark_flag, // valore ricevuto
|
|
||||||
portMAX_DELAY); // wait indefinitely, timeout handled by esp_timer
|
portMAX_DELAY); // wait indefinitely, timeout handled by esp_timer
|
||||||
|
|
||||||
// Handle timeout or spark event
|
// Handle timeout or spark event
|
||||||
if (spark_flag == SPARK_FLAG_TIMEOUT) {
|
if (spark_flag != SPARK_FLAG_TIMEOUT)
|
||||||
spark_flag = SPARK_FLAG_NIL;
|
|
||||||
} else {
|
|
||||||
// Spark occurred, stop the timer
|
|
||||||
esp_timer_stop(timeout_timer);
|
esp_timer_stop(timeout_timer);
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
|
||||||
// LOG_INFO("Spark Flags: ", printBits(spark_flag).c_str());
|
|
||||||
LOG_INFO("Spark12:", ign_box_sts.coils12.spark_ok ? "TRUE" : "FALSE");
|
|
||||||
LOG_INFO("Spark34:", ign_box_sts.coils34.spark_ok ? "TRUE" : "FALSE");
|
|
||||||
if (names.contains(spark_flag))
|
|
||||||
LOG_INFO("Spark Trigger:", names.at(spark_flag));
|
|
||||||
#endif
|
|
||||||
xTaskNotifyStateClear(NULL);
|
|
||||||
ulTaskNotifyValueClear(NULL, 0xFFFFFFFF);
|
|
||||||
|
|
||||||
// A trigger from pickup 12 is followed by a spark event on 34 or vice versa pickup 34 triggers spark on 12
|
// A trigger from pickup 12 is followed by a spark event on 34 or vice versa pickup 34 triggers spark on 12
|
||||||
if ((pickup_flag == TRIG_FLAG_12P || pickup_flag == TRIG_FLAG_12N) && (spark_flag != SPARK_FLAG_12 && spark_flag != SPARK_FLAG_NIL))
|
if ((pickup_flag == TRIG_FLAG_12P || pickup_flag == TRIG_FLAG_12N) && (spark_flag != SPARK_FLAG_12 && spark_flag != SPARK_FLAG_TIMEOUT))
|
||||||
{
|
{
|
||||||
ign_box_sts.coils12.spark_status = ign_box_sts.coils34.spark_status = sparkStatus::SPARK_SYNC_FAIL;
|
ign_box_sts.coils12.spark_status = ign_box_sts.coils34.spark_status = sparkStatus::SPARK_SYNC_FAIL;
|
||||||
continue;
|
continue;
|
||||||
@@ -167,7 +151,14 @@ void rtIgnitionTask(void *pvParameters)
|
|||||||
switch (pickup_flag)
|
switch (pickup_flag)
|
||||||
{
|
{
|
||||||
case TRIG_FLAG_12P:
|
case TRIG_FLAG_12P:
|
||||||
|
{
|
||||||
first_cycle = false;
|
first_cycle = false;
|
||||||
|
// compute engine rpm from cycle time
|
||||||
|
auto current_time = esp_timer_get_time();
|
||||||
|
auto cycle_time = current_time - last_cycle_time;
|
||||||
|
last_cycle_time = current_time;
|
||||||
|
ign_box_sts.eng_rpm = (uint32_t)(60.0f / (cycle_time / 1000000.0f));
|
||||||
|
}
|
||||||
case TRIG_FLAG_12N:
|
case TRIG_FLAG_12N:
|
||||||
coils = &ign_box_sts.coils12;
|
coils = &ign_box_sts.coils12;
|
||||||
break;
|
break;
|
||||||
@@ -184,18 +175,14 @@ void rtIgnitionTask(void *pvParameters)
|
|||||||
case TRIG_FLAG_34P:
|
case TRIG_FLAG_34P:
|
||||||
{
|
{
|
||||||
// Timeout not occourred, expected POSITIVE edge spark OCCOURRED
|
// Timeout not occourred, expected POSITIVE edge spark OCCOURRED
|
||||||
if (spark_flag != SPARK_FLAG_NIL)
|
if (spark_flag != SPARK_FLAG_TIMEOUT)
|
||||||
{
|
{
|
||||||
coils->spark_delay = coils->spark_time - coils->trig_time;
|
coils->spark_delay = (uint32_t)(coils->spark_time - coils->trig_time);
|
||||||
coils->sstart_status = softStartStatus::NORMAL; // because spark on positive edge
|
coils->sstart_status = softStartStatus::NORMAL; // because spark on positive edge
|
||||||
coils->spark_status = sparkStatus::SPARK_POS_OK; // do not wait for spark on negative edge
|
coils->spark_status = sparkStatus::SPARK_POS_OK; // do not wait for spark on negative edge
|
||||||
#ifdef DEBUG
|
|
||||||
LOG_INFO("Spark on POSITIVE pulse");
|
|
||||||
LOG_INFO("Spark Delay Time: ", (int32_t)coils->spark_delay);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
// Timeout occourred, expected POSITIVE edge spark NOT OCCOURRED
|
// Timeout occourred, expected POSITIVE edge spark NOT OCCOURRED
|
||||||
else if (spark_flag == SPARK_FLAG_NIL)
|
else if (spark_flag == SPARK_FLAG_TIMEOUT)
|
||||||
{
|
{
|
||||||
coils->spark_status = sparkStatus::SPARK_NEG_WAIT;
|
coils->spark_status = sparkStatus::SPARK_NEG_WAIT;
|
||||||
coils->sstart_status = softStartStatus::NORMAL;
|
coils->sstart_status = softStartStatus::NORMAL;
|
||||||
@@ -208,24 +195,20 @@ void rtIgnitionTask(void *pvParameters)
|
|||||||
{
|
{
|
||||||
const bool expected_negative = coils->spark_status == sparkStatus::SPARK_NEG_WAIT;
|
const bool expected_negative = coils->spark_status == sparkStatus::SPARK_NEG_WAIT;
|
||||||
// Timeout not occourred, expected NEGATIVE edge spark OCCOURRED
|
// Timeout not occourred, expected NEGATIVE edge spark OCCOURRED
|
||||||
if (spark_flag != SPARK_FLAG_NIL && expected_negative)
|
if (spark_flag != SPARK_FLAG_TIMEOUT && expected_negative)
|
||||||
{
|
{
|
||||||
coils->spark_delay = coils->spark_time - coils->trig_time;
|
coils->spark_delay = (uint32_t)(coils->spark_time - coils->trig_time);
|
||||||
coils->sstart_status = softStartStatus::SOFT_START;
|
coils->sstart_status = softStartStatus::SOFT_START;
|
||||||
coils->spark_status = sparkStatus::SPARK_NEG_OK;
|
coils->spark_status = sparkStatus::SPARK_NEG_OK;
|
||||||
#ifdef DEBUG
|
|
||||||
LOG_INFO("Spark on NEGATIVE pulse");
|
|
||||||
LOG_INFO("Spark Delay Time: ", (int32_t)coils->spark_delay);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
// Timeout occourred, expected POSITIVE edge spark NOT OCCOURRED
|
// Timeout occourred, expected POSITIVE edge spark NOT OCCOURRED
|
||||||
else if (spark_flag == SPARK_FLAG_NIL && expected_negative)
|
else if (spark_flag == SPARK_FLAG_TIMEOUT && expected_negative)
|
||||||
{
|
{
|
||||||
coils->sstart_status = softStartStatus::ERROR;
|
coils->sstart_status = softStartStatus::ERROR;
|
||||||
coils->spark_status = sparkStatus::SPARK_NEG_FAIL;
|
coils->spark_status = sparkStatus::SPARK_NEG_FAIL;
|
||||||
}
|
}
|
||||||
// Timeout not occouured, unexpected negative edge spark
|
// Timeout not occouured, unexpected negative edge spark
|
||||||
else if (spark_flag != SPARK_FLAG_NIL && !expected_negative)
|
else if (spark_flag != SPARK_FLAG_TIMEOUT && !expected_negative)
|
||||||
{
|
{
|
||||||
coils->sstart_status = softStartStatus::SOFT_START;
|
coils->sstart_status = softStartStatus::SOFT_START;
|
||||||
coils->spark_status = sparkStatus::SPARK_NEG_UNEXPECTED;
|
coils->spark_status = sparkStatus::SPARK_NEG_UNEXPECTED;
|
||||||
@@ -239,11 +222,6 @@ void rtIgnitionTask(void *pvParameters)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
#ifdef DEUG
|
|
||||||
LOG_ERROR("Invalid Interrupt");
|
|
||||||
LOG_ERROR("Pickup Flags: ", printBits(pickup_flag).c_str());
|
|
||||||
LOG_ERROR("Spark Flags: ", printBits(spark_flag).c_str());
|
|
||||||
#endif
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -251,6 +229,12 @@ void rtIgnitionTask(void *pvParameters)
|
|||||||
{
|
{
|
||||||
cycle12 = false;
|
cycle12 = false;
|
||||||
cycle34 = false;
|
cycle34 = false;
|
||||||
|
|
||||||
|
if (ign_box_sts.coils12.spark_status == sparkStatus::SPARK_POS_FAIL || ign_box_sts.coils12.spark_status == sparkStatus::SPARK_NEG_FAIL)
|
||||||
|
ign_box_sts.coils12.n_missed_firing++;
|
||||||
|
if (ign_box_sts.coils34.spark_status == sparkStatus::SPARK_POS_FAIL || ign_box_sts.coils34.spark_status == sparkStatus::SPARK_NEG_FAIL)
|
||||||
|
ign_box_sts.coils34.n_missed_firing++;
|
||||||
|
|
||||||
// read adc channels: pickup12, out12 [ pos + neg ]
|
// read adc channels: pickup12, out12 [ pos + neg ]
|
||||||
if (adc) // read only if adc initialized
|
if (adc) // read only if adc initialized
|
||||||
{
|
{
|
||||||
@@ -285,12 +269,7 @@ void rtIgnitionTask(void *pvParameters)
|
|||||||
if (rt_queue)
|
if (rt_queue)
|
||||||
ign_box_sts.timestamp = esp_timer_get_time(); // update data timestamp
|
ign_box_sts.timestamp = esp_timer_get_time(); // update data timestamp
|
||||||
if (xQueueSendToBack(rt_queue, (void *)&ign_box_sts, 0) != pdPASS)
|
if (xQueueSendToBack(rt_queue, (void *)&ign_box_sts, 0) != pdPASS)
|
||||||
{
|
ign_box_sts.n_queue_errors = ++n_errors;
|
||||||
static uint32_t n_errors = 0;
|
|
||||||
n_errors++;
|
|
||||||
ign_box_sts.n_queue_errors = n_errors;
|
|
||||||
LOG_ERROR("Failed to send to rt_queue");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Delete the timeout timer
|
// Delete the timeout timer
|
||||||
|
|||||||
68
RotaxMonitor/src/ui.cpp
Normal file
68
RotaxMonitor/src/ui.cpp
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
#include <ui.h>
|
||||||
|
|
||||||
|
void clearScreen()
|
||||||
|
{
|
||||||
|
Serial.print("\033[2J"); // clear screen
|
||||||
|
Serial.print("\033[H"); // cursor home
|
||||||
|
Serial.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
void setCursor(const uint8_t x, const uint8_t y)
|
||||||
|
{
|
||||||
|
Serial.printf("\033[%d;%d", y, x + 1);
|
||||||
|
Serial.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
void printField(const char name[], const uint32_t val)
|
||||||
|
{
|
||||||
|
Serial.printf("%15s: %06d\n", name, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
void printField(const char name[], const int64_t val)
|
||||||
|
{
|
||||||
|
Serial.printf("%15s: %06u\n", name, (uint64_t)val);
|
||||||
|
}
|
||||||
|
|
||||||
|
void printField(const char name[], const float val)
|
||||||
|
{
|
||||||
|
Serial.printf("%15s: %4.2f\n", name, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
void printField(const char name[], const char *val)
|
||||||
|
{
|
||||||
|
Serial.printf("%15s: %s\n", name, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
void printInfo(const ignitionBoxStatus &info)
|
||||||
|
{
|
||||||
|
clearScreen();
|
||||||
|
setCursor(0, 0);
|
||||||
|
printField("++ Timestamp ++", (uint32_t)info.timestamp);
|
||||||
|
Serial.println("========== Coils 12 =============");
|
||||||
|
printField("Events", info.coils12.n_events);
|
||||||
|
printField("Events Missed", info.coils12.n_missed_firing);
|
||||||
|
printField("Spark Dly", (uint32_t)info.coils12.spark_delay);
|
||||||
|
printField("Spark Sts", sparkStatusNames.at(info.coils12.spark_status));
|
||||||
|
printField("Peak P_IN", info.coils12.peak_p_in);
|
||||||
|
printField("Peak N_IN", info.coils12.peak_n_in);
|
||||||
|
printField("Peak P_OUT", info.coils12.peak_p_out);
|
||||||
|
printField("Peak N_OUT", info.coils12.peak_n_out);
|
||||||
|
printField("Soft Start ", softStartStatusNames.at(info.coils12.sstart_status));
|
||||||
|
|
||||||
|
Serial.println("========== Coils 34 =============");
|
||||||
|
printField("Events", info.coils34.n_events);
|
||||||
|
printField("Events Missed", info.coils34.n_missed_firing);
|
||||||
|
printField("Spark Dly", (uint32_t)info.coils34.spark_delay);
|
||||||
|
printField("Spark Sts", sparkStatusNames.at(info.coils34.spark_status));
|
||||||
|
printField("Peak P_IN", info.coils34.peak_p_in);
|
||||||
|
printField("Peak N_IN", info.coils34.peak_n_in);
|
||||||
|
printField("Peak P_OUT", info.coils34.peak_p_out);
|
||||||
|
printField("Peak N_OUT", info.coils34.peak_n_out);
|
||||||
|
printField("Soft Start ", softStartStatusNames.at(info.coils34.sstart_status));
|
||||||
|
|
||||||
|
Serial.println("============ END ===============");
|
||||||
|
Serial.println();
|
||||||
|
printField("Engine RPM", info.eng_rpm);
|
||||||
|
printField("ADC Read Time", info.adc_read_time);
|
||||||
|
printField("Queue Errors", info.n_queue_errors);
|
||||||
|
}
|
||||||
@@ -1,36 +1,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <datastruct.h>
|
||||||
|
|
||||||
void clearScreen()
|
void clearScreen();
|
||||||
{
|
void setCursor(const uint8_t x, const uint8_t y);
|
||||||
Serial.print("\033[2J"); // clear screen
|
void printField(const char name[], const uint32_t val);
|
||||||
Serial.print("\033[H"); // cursor home
|
void printField(const char name[], const int64_t val);
|
||||||
Serial.flush();
|
void printField(const char name[], const float val);
|
||||||
}
|
void printField(const char name[], const char *val);
|
||||||
|
|
||||||
void setCursor(const uint8_t x, const uint8_t y)
|
void printInfo(const ignitionBoxStatus &info);
|
||||||
{
|
|
||||||
Serial.printf("\033[%d;%d", y, x + 1);
|
|
||||||
Serial.flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
void printField(const char name[], const uint32_t val)
|
|
||||||
{
|
|
||||||
Serial.printf("%15s: %06d\n", name, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
void printField(const char name[], const int64_t val)
|
|
||||||
{
|
|
||||||
Serial.printf("%15s: %06u\n", name, (uint64_t)val);
|
|
||||||
}
|
|
||||||
|
|
||||||
void printField(const char name[], const float val)
|
|
||||||
{
|
|
||||||
Serial.printf("%15s: %4.2f\n", name, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
void printField(const char name[], const char *val)
|
|
||||||
{
|
|
||||||
Serial.printf("%15s: %s\n", name, val);
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user