Compare commits
9 Commits
debug
...
7c96101cdd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7c96101cdd | ||
|
|
877236ee4e | ||
|
|
668b590d7c | ||
|
|
f36cb96f21 | ||
|
|
dc44decd64 | ||
|
|
0d0db29bba | ||
| 5c9ef7e93b | |||
| a2d0afa0c9 | |||
| 1109681eb5 |
1
RotaxMonitor/.gitignore
vendored
1
RotaxMonitor/.gitignore
vendored
@@ -3,3 +3,4 @@
|
||||
.vscode/c_cpp_properties.json
|
||||
.vscode/launch.json
|
||||
.vscode/ipch
|
||||
unpacked_fs
|
||||
|
||||
3
RotaxMonitor/data/config.json
Normal file
3
RotaxMonitor/data/config.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
|
||||
}
|
||||
@@ -120,14 +120,14 @@ void ADS1256::setDRATE(uint8_t drate) //Setting DRATE (sampling frequency)
|
||||
{
|
||||
writeRegister(DRATE_REG, drate);
|
||||
_DRATE = drate;
|
||||
delay(200);
|
||||
delayMicroseconds(500);
|
||||
}
|
||||
|
||||
void ADS1256::setMUX(uint8_t mux) //Setting MUX (input channel)
|
||||
{
|
||||
writeRegister(MUX_REG, mux);
|
||||
_MUX = mux;
|
||||
delay(200);
|
||||
//delayMicroseconds(500);
|
||||
}
|
||||
|
||||
void ADS1256::setPGA(uint8_t pga) //Setting PGA (input voltage range)
|
||||
@@ -138,7 +138,7 @@ void ADS1256::setPGA(uint8_t pga) //Setting PGA (input voltage range)
|
||||
_ADCON = (_ADCON & 0b11111000) | (_PGA & 0b00000111); // Clearing and then setting bits 2-0 based on pga
|
||||
|
||||
writeRegister(ADCON_REG, _ADCON);
|
||||
delay(200);
|
||||
delayMicroseconds(1000); //Delay to allow the PGA to settle after changing its value
|
||||
|
||||
updateConversionParameter(); //Update the multiplier according top the new PGA value
|
||||
}
|
||||
@@ -501,8 +501,6 @@ void ADS1256::writeRegister(uint8_t registerAddress, uint8_t registerValueToWrit
|
||||
|
||||
CS_HIGH();
|
||||
_spi->endTransaction();
|
||||
delay(100);
|
||||
|
||||
}
|
||||
|
||||
long ADS1256::readRegister(uint8_t registerAddress) //Reading a register
|
||||
@@ -524,7 +522,7 @@ long ADS1256::readRegister(uint8_t registerAddress) //Reading a register
|
||||
|
||||
CS_HIGH();
|
||||
_spi->endTransaction();
|
||||
delay(100);
|
||||
|
||||
return regValue;
|
||||
}
|
||||
|
||||
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]
|
||||
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
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
@@ -21,12 +22,12 @@ lib_deps =
|
||||
|
||||
;Upload protocol configuration
|
||||
upload_protocol = esptool
|
||||
upload_port = /dev/ttyACM2
|
||||
upload_port = COM8
|
||||
upload_speed = 921600
|
||||
|
||||
;Monitor configuration
|
||||
monitor_speed = 115200
|
||||
monitor_port = /dev/ttyACM2
|
||||
monitor_port = COM4
|
||||
monitor_speed = 921600
|
||||
|
||||
; Build configuration
|
||||
build_type = release
|
||||
@@ -39,18 +40,19 @@ build_flags =
|
||||
|
||||
[env:esp32-s3-devkitc1-n16r8-debug]
|
||||
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}
|
||||
framework = ${env:esp32-s3-devkitc1-n16r8.framework}
|
||||
lib_deps = ${env:esp32-s3-devkitc1-n16r8.lib_deps}
|
||||
|
||||
;Upload protocol configuration
|
||||
upload_protocol = esptool
|
||||
upload_port = /dev/ttyACM2
|
||||
upload_port = COM4
|
||||
upload_speed = 921600
|
||||
|
||||
;Monitor configuration
|
||||
monitor_speed = 115200
|
||||
monitor_port = /dev/ttyACM2
|
||||
monitor_port = COM4
|
||||
monitor_speed = 921600
|
||||
|
||||
; Debug configuration
|
||||
debug_tool = esp-builtin
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
@@ -1,5 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
// Library defines
|
||||
#define ADS1256_SPI_ALREADY_STARTED
|
||||
|
||||
// Device Libraries
|
||||
#include <ADS1256.h>
|
||||
#include <AD5292.h>
|
||||
|
||||
@@ -36,18 +36,13 @@ void trig_isr(void *arg)
|
||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
case SPARK_FLAG_12:
|
||||
box->coils34.spark_ok = false;
|
||||
box->coils12.spark_ok = true;
|
||||
|
||||
box->coils12.spark_time = time_us;
|
||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
// vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
case SPARK_FLAG_34:
|
||||
box->coils12.spark_ok = false;
|
||||
box->coils34.spark_ok = true;
|
||||
box->coils34.spark_time = time_us;
|
||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
// vTaskNotifyGiveFromISR(task_handle, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
@@ -12,94 +12,12 @@
|
||||
#else
|
||||
#include "pins_test.h"
|
||||
#endif
|
||||
#include "datastruct.h"
|
||||
|
||||
#define CORE_0 0
|
||||
#define CORE_1 1
|
||||
#define TASK_STACK 4096 // in words
|
||||
#define 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;
|
||||
};
|
||||
#define RT_TASK_STACK 4096 // in words
|
||||
#define RT_TASK_PRIORITY (configMAX_PRIORITIES - 4) // highest priority after wifi tasks
|
||||
|
||||
struct isrParams
|
||||
{
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
|
||||
// Definitions
|
||||
#include <tasks.h>
|
||||
#include <channels.h>
|
||||
#include <devices.h>
|
||||
#include <datasave.h>
|
||||
#include <ui.h>
|
||||
|
||||
// FreeRTOS directives
|
||||
@@ -19,12 +19,6 @@
|
||||
// #define CH_B_ENABLE
|
||||
#define TEST
|
||||
|
||||
void printTaskStats() {
|
||||
char buffer[1024];
|
||||
vTaskGetRunTimeStats(buffer);
|
||||
Serial.println(buffer);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(921600);
|
||||
@@ -56,14 +50,20 @@ void loop()
|
||||
{
|
||||
// global variables
|
||||
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
|
||||
static TaskHandle_t trigA_TaskHandle = NULL;
|
||||
static TaskHandle_t trigB_TaskHandle = NULL;
|
||||
|
||||
static QueueHandle_t rt_taskA_queue = xQueueCreate(10, sizeof(ignitionBoxStatus));
|
||||
static QueueHandle_t rt_taskB_queue = xQueueCreate(10, sizeof(ignitionBoxStatus));
|
||||
// Data Queue for real time task to main loop communication
|
||||
static QueueHandle_t rt_taskA_queue = xQueueCreate(max_queue, sizeof(ignitionBoxStatus));
|
||||
static QueueHandle_t rt_taskB_queue = xQueueCreate(max_queue, sizeof(ignitionBoxStatus));
|
||||
static rtTaskParams taskA_params{
|
||||
.rt_running = true,
|
||||
.dev = &dev,
|
||||
@@ -82,7 +82,7 @@ void loop()
|
||||
LOG_INFO("Task Variables OK");
|
||||
|
||||
#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{
|
||||
.rt_running = true,
|
||||
.dev = &dev,
|
||||
@@ -99,17 +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}};
|
||||
#endif
|
||||
|
||||
// Spi ok flags
|
||||
bool spiA_ok = true;
|
||||
bool spiB_ok = true;
|
||||
|
||||
#ifndef TEST
|
||||
// Init 2 SPI interfaces
|
||||
SPIClass SPI_A(FSPI);
|
||||
spiA_ok = SPI_A.begin(SPI_A_SCK, SPI_A_MISO, SPI_A_MOSI);
|
||||
SPI_A.setDataMode(SPI_MODE1); // ADS1256 requires SPI mode 1
|
||||
#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
|
||||
|
||||
if (!spiA_ok || !spiB_ok)
|
||||
{
|
||||
LOG_ERROR("Unable to Initialize SPI Busses");
|
||||
@@ -119,13 +120,13 @@ void loop()
|
||||
}
|
||||
LOG_INFO("Init SPI OK");
|
||||
|
||||
#ifndef TEST
|
||||
// Init ADC_A
|
||||
dev.adc_a = new ADS1256(ADC_A_DRDY, ADC_A_RST, ADC_A_SYNC, ADC_A_CS, 2.5, &SPI_A);
|
||||
dev.adc_a = new ADS1256(ADC_A_DRDY, ADS1256::PIN_UNUSED, ADC_A_SYNC, ADC_A_CS, 2.5, &SPI_A);
|
||||
dev.adc_a->InitializeADC();
|
||||
dev.adc_a->setPGA(PGA_1);
|
||||
dev.adc_a->setDRATE(DRATE_1000SPS);
|
||||
dev.adc_a->setDRATE(DRATE_7500SPS);
|
||||
|
||||
#ifndef TEST
|
||||
// Init ADC_B
|
||||
dev.adc_a = new ADS1256(ADC_B_DRDY, ADC_B_RST, ADC_B_SYNC, ADC_B_CS, 2.5, &SPI_B);
|
||||
dev.adc_a->InitializeADC();
|
||||
@@ -140,9 +141,9 @@ void loop()
|
||||
ignA_task_success = xTaskCreatePinnedToCore(
|
||||
rtIgnitionTask,
|
||||
"rtIgnitionTask_boxA",
|
||||
TASK_STACK,
|
||||
RT_TASK_STACK,
|
||||
(void *)&taskA_params,
|
||||
TASK_PRIORITY,
|
||||
RT_TASK_PRIORITY,
|
||||
&trigA_TaskHandle,
|
||||
CORE_0);
|
||||
|
||||
@@ -152,16 +153,16 @@ void loop()
|
||||
ignB_task_success = xTaskCreatePinnedToCore(
|
||||
rtIgnitionTask,
|
||||
"rtIgnitionTask_boxB",
|
||||
TASK_STACK,
|
||||
RT_TASK_STACK,
|
||||
(void *)&taskB_params,
|
||||
TASK_PRIORITY, // priorità leggermente più alta
|
||||
RT_TASK_PRIORITY, // priorità leggermente più alta
|
||||
&trigB_TaskHandle,
|
||||
CORE_1);
|
||||
#endif
|
||||
|
||||
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...");
|
||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||
esp_restart();
|
||||
@@ -172,55 +173,59 @@ void loop()
|
||||
////////////////////// MAIN LOOP //////////////////////
|
||||
clearScreen();
|
||||
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();
|
||||
uint32_t missed_firings12 = 0;
|
||||
uint32_t missed_firings34 = 0;
|
||||
uint32_t counter = 0;
|
||||
|
||||
while (running)
|
||||
{
|
||||
if (xQueueReceive(rt_taskA_queue, &ignA, pdMS_TO_TICKS(1000)) == pdTRUE)
|
||||
if (counter >= active_history->size()) // not concurrent with write task
|
||||
{
|
||||
if (ignA.coils12.spark_status == sparkStatus::SPARK_NEG_FAIL || ignA.coils12.spark_status == sparkStatus::SPARK_POS_FAIL)
|
||||
missed_firings12++;
|
||||
if (ignA.coils34.spark_status == sparkStatus::SPARK_POS_FAIL || ignA.coils34.spark_status == sparkStatus::SPARK_NEG_FAIL)
|
||||
missed_firings34++;
|
||||
clearScreen();
|
||||
setCursor(0, 0);
|
||||
printField("++ Timestamp", (uint32_t)ignA.timestamp, 0, 0);
|
||||
Serial.println("========== Coils 12 =============");
|
||||
printField("Events", (uint32_t)ignA.coils12.n_events, 0, 1);
|
||||
printField("Missed Firing", missed_firings12, 0, 2);
|
||||
printField("Spark Dly", (uint32_t)ignA.coils12.spark_delay, 0, 3);
|
||||
printField("Spark Sts", sparkStatusNames.at(ignA.coils12.spark_status), 0, 4);
|
||||
// printField("Peak P_IN", ignA.coils12.peak_p_in, 0, 5);
|
||||
// printField("Peak P_OUT", ignA.coils12.peak_p_out, 0, 6);
|
||||
// printField("Peak N_IN", ignA.coils12.peak_n_in, 0, 7);
|
||||
// printField("Peak N_OUT", ignA.coils12.peak_n_out, 0, 8);
|
||||
printField("Soft Start ", softStartStatusNames.at(ignA.coils12.sstart_status), 0, 9);
|
||||
counter = 0;
|
||||
partial_save = false; // reset partial save flag on new data cycle
|
||||
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");
|
||||
// }
|
||||
}
|
||||
|
||||
Serial.println("========== Coils 34 =============");
|
||||
printField("Events", (uint32_t)ignA.coils34.n_events, 0, 11);
|
||||
printField("Missed Firing", missed_firings34, 0, 12);
|
||||
printField("Spark Dly", (uint32_t)ignA.coils34.spark_delay, 0, 13);
|
||||
printField("Spark Sts", sparkStatusNames.at(ignA.coils34.spark_status), 0, 14);
|
||||
// printField("Peak P_IN", ignA.coils34.peak_p_in, 0, 15);
|
||||
// printField("Peak P_OUT", ignA.coils34.peak_p_out, 0, 16);
|
||||
// printField("Peak N_IN", ignA.coils34.peak_n_in, 0, 17);
|
||||
// printField("Peak N_OUT", ignA.coils34.peak_n_out, 0, 18);
|
||||
printField("Soft Start ", softStartStatusNames.at(ignA.coils34.sstart_status), 0, 19);
|
||||
|
||||
Serial.println("========== END =============");
|
||||
Serial.println();
|
||||
auto delta = (esp_timer_get_time() - last) / 1000000.0f; //in seconds
|
||||
delta = delta > 0 ? 1.0f / delta : 0; // Calculate frequency (Hz)
|
||||
printField("Frequency (Hz)", delta, 0, 21);
|
||||
printField("Queue Errors", (uint32_t)ignA.n_queue_errors, 0, 22);
|
||||
last = esp_timer_get_time();
|
||||
} else
|
||||
if (xQueueReceive(rt_taskA_queue, &ign_info, pdMS_TO_TICKS(1000)) == pdTRUE)
|
||||
{
|
||||
Serial.println("Waiting for data... ");;
|
||||
// printInfo(ign_info);
|
||||
auto &hist = *active_history;
|
||||
hist[counter++ % active_history->size()] = ign_info;
|
||||
Serial.print("Data Received: " + String(counter) + "/" + String(hist.size()) + '\r');
|
||||
}
|
||||
else
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,19 +48,17 @@
|
||||
// =====================
|
||||
#define ADC_A_CS 4
|
||||
#define ADC_A_DRDY 5
|
||||
#define ADC_A_RST 6
|
||||
#define ADC_A_SYNC 7
|
||||
#define ADC_A_SYNC 6
|
||||
|
||||
#define ADC_B_CS 14
|
||||
#define ADC_B_DRDY 15
|
||||
#define ADC_B_RST 16
|
||||
#define ADC_B_SYNC 17
|
||||
#define ADC_B_SYNC 16
|
||||
|
||||
// =====================
|
||||
// DIGITAL POT
|
||||
// =====================
|
||||
//#define POT_A_CS 1
|
||||
//#define POT_B_CS 2
|
||||
#define POT_A_CS 7
|
||||
#define POT_B_CS 17
|
||||
|
||||
// =====================
|
||||
// TRIGGER INPUT INTERRUPTS
|
||||
|
||||
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>
|
||||
|
||||
// Timeout callback for microsecond precision
|
||||
void spark_timeout_callback(void* arg) {
|
||||
void spark_timeout_callback(void *arg)
|
||||
{
|
||||
TaskHandle_t handle = (TaskHandle_t)arg;
|
||||
xTaskNotify(handle, SPARK_FLAG_TIMEOUT, eSetValueWithOverwrite);
|
||||
}
|
||||
@@ -55,17 +56,16 @@ void rtIgnitionTask(void *pvParameters)
|
||||
.flag = SPARK_FLAG_34,
|
||||
.ign_stat = &ign_box_sts,
|
||||
.rt_handle_ptr = rt_handle_ptr};
|
||||
|
||||
|
||||
LOG_INFO("rtTask ISR Params OK");
|
||||
|
||||
// Create esp_timer for microsecond precision timeout
|
||||
esp_timer_handle_t timeout_timer;
|
||||
esp_timer_create_args_t timer_args = {
|
||||
.callback = spark_timeout_callback,
|
||||
.arg = (void*)rt_handle_ptr,
|
||||
.arg = (void *)rt_handle_ptr,
|
||||
.dispatch_method = ESP_TIMER_TASK,
|
||||
.name = "spark_timeout"
|
||||
};
|
||||
.name = "spark_timeout"};
|
||||
esp_timer_create(&timer_args, &timeout_timer);
|
||||
|
||||
// Attach Pin Interrupts
|
||||
@@ -89,6 +89,8 @@ void rtIgnitionTask(void *pvParameters)
|
||||
bool first_cycle = true;
|
||||
bool cycle12 = false;
|
||||
bool cycle34 = false;
|
||||
int64_t last_cycle_time = 0;
|
||||
uint32_t n_errors = 0;
|
||||
|
||||
while (params->rt_running)
|
||||
{
|
||||
@@ -101,11 +103,9 @@ void rtIgnitionTask(void *pvParameters)
|
||||
ULONG_MAX, // pulisci i primi 8 bit
|
||||
&pickup_flag, // valore ricevuto
|
||||
portMAX_DELAY);
|
||||
|
||||
|
||||
if (first_cycle && pickup_flag != TRIG_FLAG_12P) // skip first cycle because of possible initial noise on pickup signals at startu
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.print("\033[2J"); // clear screen
|
||||
@@ -128,45 +128,37 @@ void rtIgnitionTask(void *pvParameters)
|
||||
esp_timer_stop(timeout_timer); // stop timer in case it was running from previous cycle
|
||||
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
|
||||
BaseType_t sp = pdFALSE;
|
||||
sp = xTaskNotifyWait(
|
||||
0x00, // non pulire all'ingresso
|
||||
ULONG_MAX, // pulisci i primi 8 bit
|
||||
&spark_flag, // valore ricevuto
|
||||
xTaskNotifyWait(
|
||||
0x00, // non pulire all'ingresso
|
||||
ULONG_MAX, // pulisci i primi 8 bit
|
||||
&spark_flag, // valore ricevuto
|
||||
portMAX_DELAY); // wait indefinitely, timeout handled by esp_timer
|
||||
|
||||
// Handle timeout or spark event
|
||||
if (spark_flag == SPARK_FLAG_TIMEOUT) {
|
||||
spark_flag = SPARK_FLAG_NIL;
|
||||
} else {
|
||||
// Spark occurred, stop the timer
|
||||
if (spark_flag != SPARK_FLAG_TIMEOUT)
|
||||
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
|
||||
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;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Select coil status reference based on pickup_flag
|
||||
coilsStatus *coils;
|
||||
switch (pickup_flag)
|
||||
{
|
||||
case TRIG_FLAG_12P:
|
||||
{
|
||||
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:
|
||||
coils = &ign_box_sts.coils12;
|
||||
break;
|
||||
@@ -176,24 +168,21 @@ void rtIgnitionTask(void *pvParameters)
|
||||
break;
|
||||
}
|
||||
|
||||
// Select logic based on pickup and spark flags
|
||||
switch (pickup_flag)
|
||||
{
|
||||
case TRIG_FLAG_12P:
|
||||
case TRIG_FLAG_34P:
|
||||
{
|
||||
// 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->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
|
||||
else if (spark_flag == SPARK_FLAG_NIL)
|
||||
else if (spark_flag == SPARK_FLAG_TIMEOUT)
|
||||
{
|
||||
coils->spark_status = sparkStatus::SPARK_NEG_WAIT;
|
||||
coils->sstart_status = softStartStatus::NORMAL;
|
||||
@@ -206,24 +195,20 @@ void rtIgnitionTask(void *pvParameters)
|
||||
{
|
||||
const bool expected_negative = coils->spark_status == sparkStatus::SPARK_NEG_WAIT;
|
||||
// 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->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
|
||||
else if (spark_flag == SPARK_FLAG_NIL && expected_negative)
|
||||
else if (spark_flag == SPARK_FLAG_TIMEOUT && expected_negative)
|
||||
{
|
||||
coils->sstart_status = softStartStatus::ERROR;
|
||||
coils->spark_status = sparkStatus::SPARK_NEG_FAIL;
|
||||
}
|
||||
// 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->spark_status = sparkStatus::SPARK_NEG_UNEXPECTED;
|
||||
@@ -237,11 +222,6 @@ void rtIgnitionTask(void *pvParameters)
|
||||
break;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -249,10 +229,16 @@ void rtIgnitionTask(void *pvParameters)
|
||||
{
|
||||
cycle12 = false;
|
||||
cycle34 = false;
|
||||
// vTaskDelay(pdMS_TO_TICKS(1)); // delay 1ms to allow peak detectors to charge for negative cycle
|
||||
|
||||
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 ]
|
||||
if (adc) // read only if adc initialized
|
||||
{
|
||||
uint32_t start_adc_read = esp_timer_get_time();
|
||||
// from peak detector circuits
|
||||
ign_box_sts.coils12.peak_p_in = adcReadChannel(adc, ADC_CH_PEAK_12P_IN);
|
||||
ign_box_sts.coils12.peak_n_in = adcReadChannel(adc, ADC_CH_PEAK_12N_IN);
|
||||
@@ -262,6 +248,7 @@ void rtIgnitionTask(void *pvParameters)
|
||||
ign_box_sts.coils12.peak_n_out = adcReadChannel(adc, ADC_CH_PEAK_12N_OUT);
|
||||
ign_box_sts.coils34.peak_p_out = adcReadChannel(adc, ADC_CH_PEAK_34P_OUT);
|
||||
ign_box_sts.coils34.peak_n_out = adcReadChannel(adc, ADC_CH_PEAK_34N_OUT);
|
||||
ign_box_sts.adc_read_time = (uint32_t)(esp_timer_get_time() - start_adc_read);
|
||||
}
|
||||
else // simulate adc read timig
|
||||
vTaskDelay(pdMS_TO_TICKS(1));
|
||||
@@ -282,10 +269,7 @@ void rtIgnitionTask(void *pvParameters)
|
||||
if (rt_queue)
|
||||
ign_box_sts.timestamp = esp_timer_get_time(); // update data timestamp
|
||||
if (xQueueSendToBack(rt_queue, (void *)&ign_box_sts, 0) != pdPASS)
|
||||
{
|
||||
ign_box_sts.n_queue_errors++;
|
||||
LOG_ERROR("Failed to send to rt_queue");
|
||||
}
|
||||
ign_box_sts.n_queue_errors = ++n_errors;
|
||||
}
|
||||
}
|
||||
// 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,59 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <datastruct.h>
|
||||
|
||||
static bool firstRun = true;
|
||||
void clearScreen();
|
||||
void setCursor(const uint8_t x, const uint8_t y);
|
||||
void printField(const char name[], const uint32_t val);
|
||||
void printField(const char name[], const int64_t val);
|
||||
void printField(const char name[], const float val);
|
||||
void printField(const char name[], const char *val);
|
||||
|
||||
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, const uint8_t x, const uint8_t y) {
|
||||
if (firstRun) {
|
||||
setCursor(x,y);
|
||||
Serial.printf("%15s: %06d\n", name, val);
|
||||
return;
|
||||
}
|
||||
setCursor(x+16, y);
|
||||
Serial.print(val);
|
||||
}
|
||||
|
||||
void printField(const char name[], const int64_t val, const uint8_t x, const uint8_t y) {
|
||||
if (firstRun) {
|
||||
setCursor(x,y);
|
||||
Serial.printf("%15s: %06u\n", name, (uint64_t)val);
|
||||
return;
|
||||
}
|
||||
setCursor(x+16, y);
|
||||
Serial.print((uint64_t)val);
|
||||
Serial.flush();
|
||||
}
|
||||
|
||||
void printField(const char name[], const float val, const uint8_t x, const uint8_t y) {
|
||||
if (firstRun) {
|
||||
setCursor(x,y);
|
||||
Serial.printf("%15s: %4.2f\n", name, val);
|
||||
return;
|
||||
}
|
||||
setCursor(x+16, y);
|
||||
Serial.print(val);
|
||||
Serial.flush();
|
||||
}
|
||||
|
||||
void printField(const char name[], const char *val, const uint8_t x, const uint8_t y) {
|
||||
if (firstRun) {
|
||||
setCursor(x,y);
|
||||
Serial.printf("%15s: %s\n", name, val);
|
||||
return;
|
||||
}
|
||||
setCursor(x+16, y);
|
||||
Serial.print(val);
|
||||
Serial.flush();
|
||||
}
|
||||
void printInfo(const ignitionBoxStatus &info);
|
||||
@@ -16,7 +16,7 @@ lib_deps =
|
||||
hideakitai/DebugLog@^0.8.4
|
||||
board_build.flash_size = 4MB
|
||||
board_build.partitions = default.csv
|
||||
monitor_speed = 115200
|
||||
monitor_speed = 921600
|
||||
build_type = release
|
||||
|
||||
[env:esp32-devtest-debug]
|
||||
@@ -27,7 +27,7 @@ lib_deps =
|
||||
hideakitai/DebugLog@^0.8.4
|
||||
board_build.flash_size = 4MB
|
||||
board_build.partitions = default.csv
|
||||
monitor_speed = 115200
|
||||
monitor_speed = 921600
|
||||
build_type = debug
|
||||
build_flags =
|
||||
-O0
|
||||
|
||||
@@ -18,13 +18,16 @@ static uint32_t count = 0;
|
||||
#define PAUSE_LONG_MIN 5000
|
||||
#define PAUSE_LONG_MAX PAUSE_LONG_MIN*100
|
||||
|
||||
#define RPM_MIN 800
|
||||
#define RPM_MAX 5500
|
||||
|
||||
void clearScreen(){
|
||||
Serial.print("\033[2J"); // clear screen
|
||||
Serial.print("\033[H"); // cursor home
|
||||
Serial.flush();
|
||||
}
|
||||
|
||||
static double filtered = 0;
|
||||
static double filtered_rpm = 0;
|
||||
|
||||
static const std::map<const uint32_t, const char *> pin2Name = {
|
||||
{PIN_TRIG_A12P, "HIGH_PIN_TRIG_A12P"},
|
||||
@@ -54,7 +57,7 @@ static timerStatus stsA = {
|
||||
void setup()
|
||||
{
|
||||
|
||||
Serial.begin(115200);
|
||||
Serial.begin(921600);
|
||||
delay(1000);
|
||||
LOG_ATTACH_SERIAL(Serial);
|
||||
|
||||
@@ -94,11 +97,11 @@ void loop()
|
||||
stsA.soft_start = false;
|
||||
}
|
||||
|
||||
double new_val = (float)(map(analogRead(FREQ_POT), 0, 4096, PAUSE_LONG_MIN, PAUSE_LONG_MAX));
|
||||
filtered = filtered + 0.1 * (new_val - filtered);
|
||||
stsA.pause_long_us = (uint32_t)filtered;
|
||||
double new_rpm = (double)(map(analogRead(FREQ_POT), 0, 4096, RPM_MIN, RPM_MAX));
|
||||
filtered_rpm = filtered_rpm + 0.1 * (new_rpm - filtered_rpm);
|
||||
stsA.pause_long_us = (uint32_t)(60000000.0f / filtered_rpm / 2.0f);
|
||||
LOG_INFO("Spark Delay uS: ", stsA.spark_delay_us, "\tSoft Start: ", stsA.soft_start ? "TRUE" : "FALSE");
|
||||
LOG_INFO("Pause: ", (uint32_t)(stsA.pause_long_us / 1000), "ms");
|
||||
LOG_INFO("Engine Rpm: ", (uint32_t)(filtered_rpm));
|
||||
LOG_INFO("Coil Pulse: ", stsA.coil_pulse_us, "us");
|
||||
LOG_INFO("Spark Pulse: ", stsA.spark_pulse_us, "us");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user