Compare commits
23 Commits
main
...
web-interf
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
28a2bf75e3 | ||
| c9887a563e | |||
| 15ca82b6df | |||
| d700578256 | |||
| 10f8026c6d | |||
| dc56990f1e | |||
| a9d5bcfd66 | |||
| 9bb66a9459 | |||
| aa9935ef22 | |||
| 79dbd5db5d | |||
| 94c5c7491a | |||
| 5ca3d3a46b | |||
| 6f372fcb49 | |||
| fec59815a6 | |||
| 7e7d0a1c59 | |||
| 59e4e955ff | |||
|
|
dce6b0fd4f | ||
|
|
bea29dc8f5 | ||
|
|
1b8ba88b05 | ||
| 5aa5aaa07a | |||
| 1b7a531d54 | |||
| 8171cab9cb | |||
|
|
899c8cffbc |
BIN
Datasheet/esp32-s3-wroom-1_wroom-1u_datasheet_en.pdf
Normal file
BIN
Datasheet/esp32-s3-wroom-1_wroom-1u_datasheet_en.pdf
Normal file
Binary file not shown.
@@ -36,7 +36,6 @@
|
||||
<div class="box-data">
|
||||
<p><strong>Timestamp:</strong> <span id="a_timestamp">-</span></p>
|
||||
<p><strong>Data Valid:</strong> <span id="a_datavalid">-</span></p>
|
||||
<p><strong>Generator voltage:</strong> <span id="a_volts_gen">-</span></p>
|
||||
<p><strong>ADC read time:</strong> <span id="a_adc_read_time">-</span></p>
|
||||
<p><strong>Queue errors:</strong> <span id="a_n_queue_errors">-</span></p>
|
||||
</div>
|
||||
@@ -111,7 +110,6 @@
|
||||
<div class="box-data">
|
||||
<p><strong>Timestamp:</strong> <span id="b_timestamp">-</span></p>
|
||||
<p><strong>Data Valid:</strong> <span id="b_datavalid">-</span></p>
|
||||
<p><strong>Generator voltage:</strong> <span id="b_volts_gen">-</span></p>
|
||||
<p><strong>ADC read time:</strong> <span id="b_adc_read_time">-</span></p>
|
||||
<p><strong>Queue errors:</strong> <span id="b_n_queue_errors">-</span></p>
|
||||
</div>
|
||||
|
||||
@@ -78,34 +78,31 @@ function connectWS() {
|
||||
const boxA = data.box_a;
|
||||
document.getElementById("a_datavalid").textContent = boxA.datavalid ?? "-";
|
||||
document.getElementById("a_timestamp").textContent = boxA.timestamp ?? "-";
|
||||
document.getElementById("a_volts_gen").textContent = boxA.volts_gen ?? "-";
|
||||
document.getElementById("a_eng_rpm").textContent = boxA.eng_rpm ?? "-";
|
||||
document.getElementById("a_adc_read_time").textContent = boxA.adc_read_time ?? "-";
|
||||
document.getElementById("a_n_queue_errors").textContent = boxA.n_queue_errors ?? "-";
|
||||
document.getElementById("a_eng_rpm").textContent = boxA.engRpm ?? "-";
|
||||
document.getElementById("a_adc_read_time").textContent = boxA.adcReadTime ?? "-";
|
||||
document.getElementById("a_n_queue_errors").textContent = boxA.nQueueErrors ?? "-";
|
||||
|
||||
const coils12A = boxA.coils12 || {};
|
||||
const coils34A = boxA.coils34 || {};
|
||||
|
||||
document.getElementById("a_coils12_spark_delay").textContent = coils12A.spark_delay ?? "-";
|
||||
document.getElementById("a_coils34_spark_delay").textContent = coils34A.spark_delay ?? "-";
|
||||
document.getElementById("a_coils12_spark_status").textContent = coils12A.spark_status ?? "-";
|
||||
document.getElementById("a_coils34_spark_status").textContent = coils34A.spark_status ?? "-";
|
||||
document.getElementById("a_coils12_sstart_status").textContent = coils12A.sstart_status ?? "-";
|
||||
document.getElementById("a_coils34_sstart_status").textContent = coils34A.sstart_status ?? "-";
|
||||
document.getElementById("a_coils12_peak_p_in").textContent = coils12A.peak_p_in ?? "-";
|
||||
document.getElementById("a_coils34_peak_p_in").textContent = coils34A.peak_p_in ?? "-";
|
||||
document.getElementById("a_coils12_peak_n_in").textContent = coils12A.peak_n_in ?? "-";
|
||||
document.getElementById("a_coils34_peak_n_in").textContent = coils34A.peak_n_in ?? "-";
|
||||
document.getElementById("a_coils12_peak_p_out").textContent = coils12A.peak_p_out ?? "-";
|
||||
document.getElementById("a_coils34_peak_p_out").textContent = coils34A.peak_p_out ?? "-";
|
||||
document.getElementById("a_coils12_peak_n_out").textContent = coils12A.peak_n_out ?? "-";
|
||||
document.getElementById("a_coils34_peak_n_out").textContent = coils34A.peak_n_out ?? "-";
|
||||
document.getElementById("a_coils12_level_spark").textContent = coils12A.level_spark ?? "-";
|
||||
document.getElementById("a_coils34_level_spark").textContent = coils34A.level_spark ?? "-";
|
||||
document.getElementById("a_coils12_n_events").textContent = coils12A.n_events ?? "-";
|
||||
document.getElementById("a_coils34_n_events").textContent = coils34A.n_events ?? "-";
|
||||
document.getElementById("a_coils12_n_missed_firing").textContent = coils12A.n_missed_firing ?? "-";
|
||||
document.getElementById("a_coils34_n_missed_firing").textContent = coils34A.n_missed_firing ?? "-";
|
||||
document.getElementById("a_coils12_spark_delay").textContent = coils12A.sparkDelay ?? "-";
|
||||
document.getElementById("a_coils34_spark_delay").textContent = coils34A.sparkDelay ?? "-";
|
||||
document.getElementById("a_coils12_spark_status").textContent = coils12A.sparkStatus ?? "-";
|
||||
document.getElementById("a_coils34_spark_status").textContent = coils34A.sparkStatus ?? "-";
|
||||
document.getElementById("a_coils12_sstart_status").textContent = coils12A.softStartStatus ?? "-";
|
||||
document.getElementById("a_coils34_sstart_status").textContent = coils34A.softStartStatus ?? "-";
|
||||
document.getElementById("a_coils12_peak_p_in").textContent = coils12A.peakPos ?? "-";
|
||||
document.getElementById("a_coils34_peak_p_in").textContent = coils34A.peakPos ?? "-";
|
||||
document.getElementById("a_coils12_peak_n_in").textContent = coils12A.peakNeg ?? "-";
|
||||
document.getElementById("a_coils34_peak_n_in").textContent = coils34A.peakNeg ?? "-";
|
||||
document.getElementById("a_coils12_peak_p_out").textContent = coils12A.trigLevelPos ?? "-";
|
||||
document.getElementById("a_coils34_peak_p_out").textContent = coils34A.trigLevelPos ?? "-";
|
||||
document.getElementById("a_coils12_peak_n_out").textContent = coils12A.trigLevelNeg ?? "-";
|
||||
document.getElementById("a_coils34_peak_n_out").textContent = coils34A.trigLevelNeg ?? "-";
|
||||
document.getElementById("a_coils12_n_events").textContent = coils12A.nEvents ?? "-";
|
||||
document.getElementById("a_coils34_n_events").textContent = coils34A.nEvents ?? "-";
|
||||
document.getElementById("a_coils12_n_missed_firing").textContent = coils12A.nMissedFiring ?? "-";
|
||||
document.getElementById("a_coils34_n_missed_firing").textContent = coils34A.nMissedFiring ?? "-";
|
||||
}
|
||||
|
||||
// Update Box_B
|
||||
@@ -113,34 +110,31 @@ function connectWS() {
|
||||
const boxB = data.box_b;
|
||||
document.getElementById("b_datavalid").textContent = boxB.datavalid ?? "-";
|
||||
document.getElementById("b_timestamp").textContent = boxB.timestamp ?? "-";
|
||||
document.getElementById("b_volts_gen").textContent = boxB.volts_gen ?? "-";
|
||||
document.getElementById("b_eng_rpm").textContent = boxB.eng_rpm ?? "-";
|
||||
document.getElementById("b_adc_read_time").textContent = boxB.adc_read_time ?? "-";
|
||||
document.getElementById("b_n_queue_errors").textContent = boxB.n_queue_errors ?? "-";
|
||||
document.getElementById("b_eng_rpm").textContent = boxB.engRpm ?? "-";
|
||||
document.getElementById("b_adc_read_time").textContent = boxB.adcReadTime ?? "-";
|
||||
document.getElementById("b_n_queue_errors").textContent = boxB.nQueueErrors ?? "-";
|
||||
|
||||
const coils12B = boxB.coils12 || {};
|
||||
const coils34B = boxB.coils34 || {};
|
||||
|
||||
document.getElementById("b_coils12_spark_delay").textContent = coils12B.spark_delay ?? "-";
|
||||
document.getElementById("b_coils34_spark_delay").textContent = coils34B.spark_delay ?? "-";
|
||||
document.getElementById("b_coils12_spark_status").textContent = coils12B.spark_status ?? "-";
|
||||
document.getElementById("b_coils34_spark_status").textContent = coils34B.spark_status ?? "-";
|
||||
document.getElementById("b_coils12_sstart_status").textContent = coils12B.sstart_status ?? "-";
|
||||
document.getElementById("b_coils34_sstart_status").textContent = coils34B.sstart_status ?? "-";
|
||||
document.getElementById("b_coils12_peak_p_in").textContent = coils12B.peak_p_in ?? "-";
|
||||
document.getElementById("b_coils34_peak_p_in").textContent = coils34B.peak_p_in ?? "-";
|
||||
document.getElementById("b_coils12_peak_n_in").textContent = coils12B.peak_n_in ?? "-";
|
||||
document.getElementById("b_coils34_peak_n_in").textContent = coils34B.peak_n_in ?? "-";
|
||||
document.getElementById("b_coils12_peak_p_out").textContent = coils12B.peak_p_out ?? "-";
|
||||
document.getElementById("b_coils34_peak_p_out").textContent = coils34B.peak_p_out ?? "-";
|
||||
document.getElementById("b_coils12_peak_n_out").textContent = coils12B.peak_n_out ?? "-";
|
||||
document.getElementById("b_coils34_peak_n_out").textContent = coils34B.peak_n_out ?? "-";
|
||||
document.getElementById("b_coils12_level_spark").textContent = coils12B.level_spark ?? "-";
|
||||
document.getElementById("b_coils34_level_spark").textContent = coils34B.level_spark ?? "-";
|
||||
document.getElementById("b_coils12_n_events").textContent = coils12B.n_events ?? "-";
|
||||
document.getElementById("b_coils34_n_events").textContent = coils34B.n_events ?? "-";
|
||||
document.getElementById("b_coils12_n_missed_firing").textContent = coils12B.n_missed_firing ?? "-";
|
||||
document.getElementById("b_coils34_n_missed_firing").textContent = coils34B.n_missed_firing ?? "-";
|
||||
document.getElementById("a_coils12_spark_delay").textContent = coils12B.sparkDelay ?? "-";
|
||||
document.getElementById("a_coils34_spark_delay").textContent = coils34B.sparkDelay ?? "-";
|
||||
document.getElementById("a_coils12_spark_status").textContent = coils12B.sparkStatus ?? "-";
|
||||
document.getElementById("a_coils34_spark_status").textContent = coils34B.sparkStatus ?? "-";
|
||||
document.getElementById("a_coils12_sstart_status").textContent = coils12B.softStartStatus ?? "-";
|
||||
document.getElementById("a_coils34_sstart_status").textContent = coils34B.softStartStatus ?? "-";
|
||||
document.getElementById("a_coils12_peak_p_in").textContent = coils12B.peakPos ?? "-";
|
||||
document.getElementById("a_coils34_peak_p_in").textContent = coils34B.peakPos ?? "-";
|
||||
document.getElementById("a_coils12_peak_n_in").textContent = coils12B.peakNeg ?? "-";
|
||||
document.getElementById("a_coils34_peak_n_in").textContent = coils34B.peakNeg ?? "-";
|
||||
document.getElementById("a_coils12_peak_p_out").textContent = coils12B.trigLevelPos ?? "-";
|
||||
document.getElementById("a_coils34_peak_p_out").textContent = coils34B.trigLevelPos ?? "-";
|
||||
document.getElementById("a_coils12_peak_n_out").textContent = coils12B.trigLevelNeg ?? "-";
|
||||
document.getElementById("a_coils34_peak_n_out").textContent = coils34B.trigLevelNeg ?? "-";
|
||||
document.getElementById("a_coils12_n_events").textContent = coils12B.nEvents ?? "-";
|
||||
document.getElementById("a_coils34_n_events").textContent = coils34B.nEvents ?? "-";
|
||||
document.getElementById("a_coils12_n_missed_firing").textContent = coils12B.nMissedFiring ?? "-";
|
||||
document.getElementById("a_coils34_n_missed_firing").textContent = coils34B.nMissedFiring ?? "-";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
18619
RotaxMonitor/data/vue.global.js
Normal file
18619
RotaxMonitor/data/vue.global.js
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,64 +1,67 @@
|
||||
//ADS1256 header file
|
||||
// ADS1256 header file
|
||||
/*
|
||||
Name: ADS1256.h
|
||||
Created: 2022/07/14
|
||||
Author: Curious Scientist
|
||||
Editor: Notepad++
|
||||
Comment: Visit https://curiousscientist.tech/blog/ADS1256-custom-library
|
||||
Special thanks to
|
||||
Special thanks to
|
||||
Abraão Queiroz for spending time on the code and suggesting corrections for ESP32 microcontrollers
|
||||
Benjamin Pelletier for pointing out and fixing an issue around the handling of the DRDY signal
|
||||
*/
|
||||
|
||||
#ifndef _ADS1256_h
|
||||
#define _ADS1256_h
|
||||
#pragma once
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
//Differential inputs
|
||||
#define DIFF_0_1 0b00000001 //A0 + A1 as differential input
|
||||
#define DIFF_2_3 0b00100011 //A2 + A3 as differential input
|
||||
#define DIFF_4_5 0b01000101 //A4 + A5 as differential input
|
||||
#define DIFF_6_7 0b01100111 //A6 + A7 as differential input
|
||||
// SPI Frequency
|
||||
#define SPI_FREQ 1920000
|
||||
|
||||
//Single-ended inputs
|
||||
#define SING_0 0b00001111 //A0 + GND (common) as single-ended input
|
||||
#define SING_1 0b00011111 //A1 + GND (common) as single-ended input
|
||||
#define SING_2 0b00101111 //A2 + GND (common) as single-ended input
|
||||
#define SING_3 0b00111111 //A3 + GND (common) as single-ended input
|
||||
#define SING_4 0b01001111 //A4 + GND (common) as single-ended input
|
||||
#define SING_5 0b01011111 //A5 + GND (common) as single-ended input
|
||||
#define SING_6 0b01101111 //A6 + GND (common) as single-ended input
|
||||
#define SING_7 0b01111111 //A7 + GND (common) as single-ended input
|
||||
// Differential inputs
|
||||
#define DIFF_0_1 0b00000001 // A0 + A1 as differential input
|
||||
#define DIFF_2_3 0b00100011 // A2 + A3 as differential input
|
||||
#define DIFF_4_5 0b01000101 // A4 + A5 as differential input
|
||||
#define DIFF_6_7 0b01100111 // A6 + A7 as differential input
|
||||
|
||||
//PGA settings //Input voltage range
|
||||
#define PGA_1 0b00000000 //± 5 V
|
||||
#define PGA_2 0b00000001 //± 2.5 V
|
||||
#define PGA_4 0b00000010 //± 1.25 V
|
||||
#define PGA_8 0b00000011 //± 625 mV
|
||||
#define PGA_16 0b00000100 //± 312.5 mV
|
||||
// Single-ended inputs
|
||||
#define SING_0 0b00001111 // A0 + GND (common) as single-ended input
|
||||
#define SING_1 0b00011111 // A1 + GND (common) as single-ended input
|
||||
#define SING_2 0b00101111 // A2 + GND (common) as single-ended input
|
||||
#define SING_3 0b00111111 // A3 + GND (common) as single-ended input
|
||||
#define SING_4 0b01001111 // A4 + GND (common) as single-ended input
|
||||
#define SING_5 0b01011111 // A5 + GND (common) as single-ended input
|
||||
#define SING_6 0b01101111 // A6 + GND (common) as single-ended input
|
||||
#define SING_7 0b01111111 // A7 + GND (common) as single-ended input
|
||||
|
||||
// PGA settings //Input voltage range
|
||||
#define PGA_1 0b00000000 // ± 5 V
|
||||
#define PGA_2 0b00000001 // ± 2.5 V
|
||||
#define PGA_4 0b00000010 // ± 1.25 V
|
||||
#define PGA_8 0b00000011 // ± 625 mV
|
||||
#define PGA_16 0b00000100 // ± 312.5 mV
|
||||
#define PGA_32 0b00000101 //+ 156.25 mV
|
||||
#define PGA_64 0b00000110 //± 78.125 mV
|
||||
#define PGA_64 0b00000110 // ± 78.125 mV
|
||||
|
||||
//Datarate //DEC
|
||||
#define DRATE_30000SPS 0b11110000 //240
|
||||
#define DRATE_15000SPS 0b11100000 //224
|
||||
#define DRATE_7500SPS 0b11010000 //208
|
||||
#define DRATE_3750SPS 0b11000000 //192
|
||||
#define DRATE_2000SPS 0b10110000 //176
|
||||
#define DRATE_1000SPS 0b10100001 //161
|
||||
#define DRATE_500SPS 0b10010010 //146
|
||||
#define DRATE_100SPS 0b10000010 //130
|
||||
#define DRATE_60SPS 0b01110010 //114
|
||||
#define DRATE_50SPS 0b01100011 //99
|
||||
#define DRATE_30SPS 0b01010011 //83
|
||||
#define DRATE_25SPS 0b01000011 //67
|
||||
#define DRATE_15SPS 0b00110011 //51
|
||||
#define DRATE_10SPS 0b00100011 //35
|
||||
#define DRATE_5SPS 0b00010011 //19
|
||||
#define DRATE_2SPS 0b00000011 //3
|
||||
// Datarate //DEC
|
||||
#define DRATE_30000SPS 0b11110000 // 240
|
||||
#define DRATE_15000SPS 0b11100000 // 224
|
||||
#define DRATE_7500SPS 0b11010000 // 208
|
||||
#define DRATE_3750SPS 0b11000000 // 192
|
||||
#define DRATE_2000SPS 0b10110000 // 176
|
||||
#define DRATE_1000SPS 0b10100001 // 161
|
||||
#define DRATE_500SPS 0b10010010 // 146
|
||||
#define DRATE_100SPS 0b10000010 // 130
|
||||
#define DRATE_60SPS 0b01110010 // 114
|
||||
#define DRATE_50SPS 0b01100011 // 99
|
||||
#define DRATE_30SPS 0b01010011 // 83
|
||||
#define DRATE_25SPS 0b01000011 // 67
|
||||
#define DRATE_15SPS 0b00110011 // 51
|
||||
#define DRATE_10SPS 0b00100011 // 35
|
||||
#define DRATE_5SPS 0b00010011 // 19
|
||||
#define DRATE_2SPS 0b00000011 // 3
|
||||
|
||||
//Status register
|
||||
// Status register
|
||||
#define BITORDER_MSB 0
|
||||
#define BITORDER_LSB 1
|
||||
#define ACAL_DISABLED 0
|
||||
@@ -66,7 +69,7 @@
|
||||
#define BUFFER_DISABLED 0
|
||||
#define BUFFER_ENABLED 1
|
||||
|
||||
//Register addresses
|
||||
// Register addresses
|
||||
#define STATUS_REG 0x00
|
||||
#define MUX_REG 0x01
|
||||
#define ADCON_REG 0x02
|
||||
@@ -79,7 +82,7 @@
|
||||
#define FSC1_REG 0x09
|
||||
#define FSC2_REG 0x0A
|
||||
|
||||
//Command definitions
|
||||
// Command definitions
|
||||
#define WAKEUP 0b00000000
|
||||
#define RDATA 0b00000001
|
||||
#define RDATAC 0b00000011
|
||||
@@ -96,26 +99,30 @@
|
||||
#define RESET 0b11111110
|
||||
//----------------------------------------------------------------
|
||||
|
||||
|
||||
class ADS1256
|
||||
{
|
||||
{
|
||||
public:
|
||||
static constexpr int8_t PIN_UNUSED = -1;
|
||||
static constexpr int8_t PIN_UNUSED = -1;
|
||||
|
||||
//Constructor
|
||||
ADS1256(const int8_t DRDY_pin, const int8_t RESET_pin, const int8_t SYNC_pin, const int8_t CS_pin, float VREF, SPIClass* spi = &SPI);
|
||||
|
||||
//Initializing function
|
||||
void InitializeADC();
|
||||
//ADS1256(int drate, int pga, int byteOrder, bool bufen);
|
||||
|
||||
//Read a register
|
||||
// Constructor
|
||||
ADS1256(const int8_t DRDY_pin, const int8_t RESET_pin, const int8_t SYNC_pin, const int8_t CS_pin, float VREF, SPIClass *spi = &SPI);
|
||||
~ADS1256()
|
||||
{
|
||||
vSemaphoreDelete(m_drdyHigh);
|
||||
vSemaphoreDelete(m_drdyLow);
|
||||
}
|
||||
|
||||
// Initializing function
|
||||
void InitializeADC();
|
||||
// ADS1256(int drate, int pga, int byteOrder, bool bufen);
|
||||
|
||||
// Read a register
|
||||
long readRegister(uint8_t registerAddress);
|
||||
|
||||
//Write a register
|
||||
void writeRegister(uint8_t registerAddress, uint8_t registerValueToWrite);
|
||||
|
||||
//Individual methods
|
||||
// Write a register
|
||||
void writeRegister(uint8_t registerAddress, uint8_t registerValueToWrite);
|
||||
|
||||
// Individual methods
|
||||
void setDRATE(uint8_t drate);
|
||||
void setPGA(uint8_t pga);
|
||||
uint8_t getPGA();
|
||||
@@ -128,62 +135,70 @@ static constexpr int8_t PIN_UNUSED = -1;
|
||||
uint8_t getAutoCal();
|
||||
void setGPIO(uint8_t dir0, uint8_t dir1, uint8_t dir2, uint8_t dir3);
|
||||
void writeGPIO(uint8_t dir0value, uint8_t dir1value, uint8_t dir2value, uint8_t dir3value);
|
||||
uint8_t readGPIO(uint8_t gpioPin);
|
||||
uint8_t readGPIO(uint8_t gpioPin);
|
||||
void setCLKOUT(uint8_t clkout);
|
||||
void setSDCS(uint8_t sdcs);
|
||||
void sendDirectCommand(uint8_t directCommand);
|
||||
void setSDCS(uint8_t sdcs);
|
||||
void sendDirectCommand(uint8_t directCommand);
|
||||
|
||||
//Get a single conversion
|
||||
// Get a single conversion
|
||||
long readSingle();
|
||||
|
||||
//Single input continuous reading
|
||||
|
||||
// Single input continuous reading
|
||||
long readSingleContinuous();
|
||||
|
||||
//Cycling through the single-ended inputs
|
||||
long cycleSingle(); //Ax + COM
|
||||
|
||||
//Cycling through the differential inputs
|
||||
long cycleDifferential(); //Ax + Ay
|
||||
|
||||
//Converts the reading into a voltage value
|
||||
|
||||
// Cycling through the single-ended inputs
|
||||
long cycleSingle(); // Ax + COM
|
||||
|
||||
// Cycling through the differential inputs
|
||||
long cycleDifferential(); // Ax + Ay
|
||||
|
||||
// Converts the reading into a voltage value
|
||||
float convertToVoltage(int32_t rawData);
|
||||
|
||||
//Stop AD
|
||||
|
||||
// Stop AD
|
||||
void stopConversion();
|
||||
|
||||
|
||||
// functions for callback, public to be accessed by static callback
|
||||
inline uint8_t getDRDYpin();
|
||||
inline SemaphoreHandle_t getDRDYsemaphoreHigh();
|
||||
inline SemaphoreHandle_t getDRDYsemaphoreLow();
|
||||
|
||||
private:
|
||||
|
||||
SPIClass* _spi; //Pointer to an SPIClass object
|
||||
SPIClass *_spi; // Pointer to an SPIClass object
|
||||
|
||||
void waitForLowDRDY(); // Block until DRDY is low
|
||||
void waitForHighDRDY(); // Block until DRDY is high
|
||||
void updateMUX(uint8_t muxValue);
|
||||
inline void CS_LOW();
|
||||
inline void CS_HIGH();
|
||||
void waitForLowDRDY(); // Block until DRDY is low
|
||||
void waitForHighDRDY(); // Block until DRDY is high
|
||||
void updateMUX(uint8_t muxValue);
|
||||
inline void CS_LOW();
|
||||
inline void CS_HIGH();
|
||||
inline void enableDRDYinterrupt();
|
||||
inline void disableDRDYinterrupt();
|
||||
|
||||
void updateConversionParameter(); //Refresh the conversion parameter based on the PGA
|
||||
void updateConversionParameter(); // Refresh the conversion parameter based on the PGA
|
||||
|
||||
float _VREF = 0; //Value of the reference voltage
|
||||
float conversionParameter = 0; //PGA-dependent multiplier
|
||||
//Pins
|
||||
int8_t _DRDY_pin; //Pin assigned for DRDY
|
||||
int8_t _RESET_pin; //Pin assigned for RESET
|
||||
int8_t _SYNC_pin; //Pin assigned for SYNC
|
||||
int8_t _CS_pin; //Pin assigned for CS
|
||||
float m_VREF = 0; // Value of the reference voltage
|
||||
float m_conversionParameter = 0; // PGA-dependent multiplier
|
||||
// Pins
|
||||
int8_t m_DRDY_pin; // Pin assigned for DRDY
|
||||
int8_t m_RESET_pin; // Pin assigned for RESET
|
||||
int8_t m_SYNC_pin; // Pin assigned for SYNC
|
||||
int8_t m_CS_pin; // Pin assigned for CS
|
||||
|
||||
//Register values
|
||||
byte _DRATE; //Value of the DRATE register
|
||||
byte _ADCON; //Value of the ADCON register
|
||||
byte _MUX; //Value of the MUX register
|
||||
byte _PGA; //Value of the PGA (within ADCON)
|
||||
byte _GPIO; //Value of the GPIO register
|
||||
byte _STATUS; //Value of the status register
|
||||
byte _GPIOvalue; //GPIO value
|
||||
byte _ByteOrder; //Byte order
|
||||
// Register values
|
||||
uint8_t m_DRATE; // Value of the DRATE register
|
||||
uint8_t m_ADCON; // Value of the ADCON register
|
||||
uint8_t m_MUX; // Value of the MUX register
|
||||
uint8_t m_PGA; // Value of the PGA (within ADCON)
|
||||
uint8_t m_GPIO; // Value of the GPIO register
|
||||
uint8_t m_STATUS; // Value of the status register
|
||||
uint8_t m_GPIOvalue; // GPIO value
|
||||
uint8_t m_ByteOrder; // Byte order
|
||||
|
||||
byte _outputBuffer[3]; //3-byte (24-bit) buffer for the fast acquisition - Single-channel, continuous
|
||||
long _outputValue; //Combined value of the _outputBuffer[3]
|
||||
bool _isAcquisitionRunning; //bool that keeps track of the acquisition (running or not)
|
||||
uint8_t _cycle; //Tracks the cycles as the MUX is cycling through the input channels
|
||||
uint8_t m_outputBuffer[3]; // 3-byte (24-bit) buffer for the fast acquisition - Single-channel, continuous
|
||||
int32_t m_outputValue; // Combined value of the m_outputBuffer[3]
|
||||
bool m_isAcquisitionRunning; // bool that keeps track of the acquisition (running or not)
|
||||
uint8_t m_cycle; // Tracks the cycles as the MUX is cycling through the input channels
|
||||
|
||||
SemaphoreHandle_t m_drdyHigh;
|
||||
SemaphoreHandle_t m_drdyLow;
|
||||
};
|
||||
#endif
|
||||
@@ -4,6 +4,7 @@ RGBled::RGBled(const uint8_t pin) : m_led(pin)
|
||||
{
|
||||
pinMode(m_led, OUTPUT);
|
||||
writeStatus(RGBled::ERROR);
|
||||
m_brightness = 1.0f;
|
||||
}
|
||||
|
||||
RGBled::~RGBled()
|
||||
@@ -11,6 +12,11 @@ RGBled::~RGBled()
|
||||
pinMode(m_led, INPUT);
|
||||
}
|
||||
|
||||
void RGBled::setBrightness(const float b)
|
||||
{
|
||||
m_brightness = b;
|
||||
}
|
||||
|
||||
void RGBled::setStatus(const LedStatus s)
|
||||
{
|
||||
if (m_status == s)
|
||||
@@ -27,6 +33,6 @@ const RGBled::LedStatus RGBled::getSatus(void)
|
||||
|
||||
void RGBled::writeStatus(const RGBled::LedStatus s)
|
||||
{
|
||||
RGBled::color_u u{.status = s};
|
||||
rgbLedWrite(m_led, u.color.r, u.color.g, u.color.b);
|
||||
const RGBled::color_u u{.status = s};
|
||||
rgbLedWrite(m_led, (uint8_t)(m_brightness*u.color.r), (uint8_t)(m_brightness*u.color.g), (uint8_t)(m_brightness*u.color.b));
|
||||
}
|
||||
|
||||
@@ -50,6 +50,7 @@ public:
|
||||
RGBled(const uint8_t pin = 48);
|
||||
~RGBled();
|
||||
|
||||
void setBrightness(const float b);
|
||||
void setStatus(const LedStatus s);
|
||||
const LedStatus getSatus(void);
|
||||
|
||||
@@ -59,5 +60,6 @@ private:
|
||||
private:
|
||||
LedStatus m_status = LedStatus::IDLE;
|
||||
std::mutex m_mutex;
|
||||
float m_brightness;
|
||||
const uint8_t m_led;
|
||||
};
|
||||
@@ -20,11 +20,10 @@ lib_deps =
|
||||
hideakitai/PCA95x5@^0.1.3
|
||||
me-no-dev/AsyncTCP@^3.3.2
|
||||
me-no-dev/ESPAsyncWebServer@^3.6.0
|
||||
adafruit/Adafruit NeoPixel@^1.15.4
|
||||
upload_protocol = esptool
|
||||
upload_port = /dev/ttyACM1
|
||||
upload_port = /dev/ttyACM0
|
||||
upload_speed = 921600
|
||||
monitor_port = /dev/ttyACM0
|
||||
monitor_port = /dev/ttyACM1
|
||||
monitor_speed = 921600
|
||||
build_type = release
|
||||
build_flags =
|
||||
@@ -36,7 +35,7 @@ build_flags =
|
||||
-DCONFIG_ASYNC_TCP_QUEUE_SIZE=64
|
||||
-DCONFIG_ASYNC_TCP_RUNNING_CORE=1
|
||||
-DCONFIG_ASYNC_TCP_STACK_SIZE=4096
|
||||
-fstack-protector-all
|
||||
-fstack-protector-strong
|
||||
|
||||
[env:esp32-s3-devkitc1-n16r8-debug]
|
||||
board = ${env:esp32-s3-devkitc1-n16r8.board}
|
||||
@@ -46,11 +45,10 @@ platform = ${env:esp32-s3-devkitc1-n16r8.platform}
|
||||
framework = ${env:esp32-s3-devkitc1-n16r8.framework}
|
||||
lib_deps =
|
||||
${env:esp32-s3-devkitc1-n16r8.lib_deps}
|
||||
adafruit/Adafruit NeoPixel@^1.15.4
|
||||
upload_protocol = esptool
|
||||
upload_port = /dev/ttyACM1
|
||||
upload_port = /dev/ttyACM0
|
||||
upload_speed = 921600
|
||||
monitor_port = /dev/ttyACM0
|
||||
monitor_port = /dev/ttyACM1
|
||||
monitor_speed = 921600
|
||||
debug_tool = esp-builtin
|
||||
debug_speed = 15000
|
||||
@@ -64,7 +62,6 @@ build_flags =
|
||||
-DARDUINO_USB_MODE=0
|
||||
-DCONFIG_ASYNC_TCP_MAX_ACK_TIME=5000
|
||||
-DCONFIG_ASYNC_TCP_PRIORITY=21
|
||||
-DCONFIG_ASYNC_TCP_QUEUE_SIZE=128
|
||||
-DCONFIG_ASYNC_TCP_QUEUE_SIZE=64
|
||||
-DCONFIG_ASYNC_TCP_RUNNING_CORE=1
|
||||
-DCONFIG_ASYNC_TCP_STACK_SIZE=8192
|
||||
-fstack-protector-all
|
||||
-DCONFIG_ASYNC_TCP_STACK_SIZE=4096
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
#include "datasave.h"
|
||||
#include <math.h>
|
||||
|
||||
|
||||
|
||||
LITTLEFSGuard::LITTLEFSGuard()
|
||||
{
|
||||
if (!LittleFS.begin(true, "/littlefs", 10, "littlefs"))
|
||||
@@ -12,7 +10,7 @@ LITTLEFSGuard::LITTLEFSGuard()
|
||||
else
|
||||
{
|
||||
LOG_INFO("LittleFS mounted successfully");
|
||||
LOG_INFO("LittleFS Free KBytes:", (LittleFS.totalBytes() - LittleFS.usedBytes()) /1024);
|
||||
LOG_INFO("LittleFS Free KBytes:", (LittleFS.totalBytes() - LittleFS.usedBytes()) / 1024);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,30 +47,29 @@ void ignitionBoxStatusFiltered::update(const ignitionBoxStatus &new_status)
|
||||
}
|
||||
m_count++;
|
||||
// simple moving average calculation
|
||||
m_last.timestamp = new_status.timestamp; // keep timestamp of latest status
|
||||
m_last.timestamp = new_status.timestamp; // keep timestamp of latest status
|
||||
m_last.coils12.nEvents = new_status.coils12.nEvents; // sum events instead of averaging
|
||||
m_last.coils12.nMissedFiring = new_status.coils12.nMissedFiring; // sum missed firings instead of averaging
|
||||
m_last.coils12.sparkStatus = new_status.coils12.sparkStatus; // take latest spark status
|
||||
m_last.coils12.softStartStatus = new_status.coils12.softStartStatus; // take latest soft start status
|
||||
filter(m_last.coils12.sparkDelay, new_status.coils12.sparkDelay, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils12.peakPos, new_status.coils12.peakPos, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils12.peakNeg, new_status.coils12.peakNeg, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils12.trigLevelPos, new_status.coils12.trigLevelPos, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils12.trigLevelNeg, new_status.coils12.trigLevelNeg, m_max_count); // incremental average calculation
|
||||
|
||||
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
|
||||
filter(m_last.coils12.spark_delay, new_status.coils12.spark_delay, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils12.peak_p_in, new_status.coils12.peak_p_in, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils12.peak_n_in, new_status.coils12.peak_n_in, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils12.peak_p_out, new_status.coils12.peak_p_out, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils12.peak_n_out, new_status.coils12.peak_n_out, m_max_count); // 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
|
||||
filter(m_last.coils34.spark_delay, new_status.coils34.spark_delay, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils34.peak_p_in, new_status.coils34.peak_p_in, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils34.peak_n_in, new_status.coils34.peak_n_in, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils34.peak_p_out, new_status.coils34.peak_p_out, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils34.peak_n_out, new_status.coils34.peak_n_out, m_max_count); // incremental average calculation
|
||||
filter(m_last.eng_rpm, new_status.eng_rpm, m_max_count); // incremental average calculation // incremental average calculation
|
||||
filter(m_last.adc_read_time, m_last.adc_read_time, m_max_count); // 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_last.coils34.nEvents = new_status.coils34.nEvents; // sum events instead of averaging
|
||||
m_last.coils34.nMissedFiring = new_status.coils34.nMissedFiring; // sum missed firings instead of averaging
|
||||
m_last.coils34.sparkStatus = new_status.coils34.sparkStatus; // take latest spark status
|
||||
m_last.coils34.softStartStatus = new_status.coils34.softStartStatus; // take latest soft start status
|
||||
filter(m_last.coils34.sparkDelay, new_status.coils34.sparkDelay, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils34.peakPos, new_status.coils34.peakPos, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils34.peakNeg, new_status.coils34.peakNeg, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils34.trigLevelPos, new_status.coils34.trigLevelPos, m_max_count); // incremental average calculation
|
||||
filter(m_last.coils34.trigLevelNeg, new_status.coils34.trigLevelNeg, m_max_count); // incremental average calculation
|
||||
filter(m_last.engRpm, new_status.engRpm, m_max_count); // incremental average calculation // incremental average calculation
|
||||
filter(m_last.adcReadTime, m_last.adcReadTime, m_max_count); // incremental average calculation
|
||||
m_last.nQueueErrors = new_status.nQueueErrors;
|
||||
|
||||
if (m_count >= m_max_count)
|
||||
{
|
||||
@@ -98,30 +95,29 @@ const ArduinoJson::JsonDocument ignitionBoxStatusFiltered::toJson() const
|
||||
doc["timestamp"] = m_last.timestamp;
|
||||
doc["datavalid"] = m_data_valid ? "TRUE" : "FALSE";
|
||||
|
||||
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["coils12"]["nEvents"] = m_last.coils12.nEvents;
|
||||
doc["coils12"]["nMissedFiring"] = m_last.coils12.nMissedFiring;
|
||||
doc["coils12"]["sparkDelay"] = m_last.coils12.sparkDelay;
|
||||
doc["coils12"]["sparkStatus"] = sparkStatusNames.at(m_last.coils12.sparkStatus);
|
||||
doc["coils12"]["peakPos"] = m_last.coils12.peakPos;
|
||||
doc["coils12"]["peakNeg"] = m_last.coils12.peakNeg;
|
||||
doc["coils12"]["trigLevelPos"] = m_last.coils12.trigLevelPos;
|
||||
doc["coils12"]["trigLevelNeg"] = m_last.coils12.trigLevelNeg;
|
||||
doc["coils12"]["softStartStatus"] = softStartStatusNames.at(m_last.coils12.softStartStatus);
|
||||
|
||||
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["coils34"]["nEvents"] = m_last.coils34.nEvents;
|
||||
doc["coils34"]["nMissedFiring"] = m_last.coils34.nMissedFiring;
|
||||
doc["coils34"]["sparkDelay"] = m_last.coils34.sparkDelay;
|
||||
doc["coils34"]["sparkStatus"] = sparkStatusNames.at(m_last.coils34.sparkStatus);
|
||||
doc["coils34"]["peakPos"] = m_last.coils34.peakPos;
|
||||
doc["coils34"]["peakNeg"] = m_last.coils34.peakNeg;
|
||||
doc["coils34"]["trigLevelPos"] = m_last.coils34.trigLevelPos;
|
||||
doc["coils34"]["trigLevelNeg"] = m_last.coils34.trigLevelNeg;
|
||||
doc["coils34"]["softStartStatus"] = softStartStatusNames.at(m_last.coils34.softStartStatus);
|
||||
|
||||
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;
|
||||
doc["engRpm"] = m_last.engRpm;
|
||||
doc["adcReadTime"] = m_last.adcReadTime;
|
||||
doc["nQueueErrors"] = m_last.nQueueErrors;
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,8 +14,6 @@
|
||||
#include "isr.h"
|
||||
#include "psvector.h"
|
||||
|
||||
const uint32_t max_history = 256;
|
||||
|
||||
class LITTLEFSGuard
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -16,7 +16,7 @@ static const uint32_t SPARK_FLAG_12 = (1 << 9);
|
||||
static const uint32_t SPARK_FLAG_34 = (1 << 10);
|
||||
|
||||
// Spark Status
|
||||
enum sparkStatus
|
||||
enum sparkStatusEnum
|
||||
{
|
||||
SPARK_POS_OK,
|
||||
SPARK_NEG_OK,
|
||||
@@ -31,7 +31,7 @@ enum sparkStatus
|
||||
SPARK_SYNC_FAIL,
|
||||
};
|
||||
|
||||
static const std::map<const sparkStatus, const char *> sparkStatusNames = {
|
||||
static const std::map<const sparkStatusEnum, const char *> sparkStatusNames = {
|
||||
{SPARK_POS_OK, "SPARK_POS_OK"},
|
||||
{SPARK_NEG_OK, "SPARK_NEG_OK"},
|
||||
{SPARK_POS_SKIP, "SPARK_POS_SKIP"},
|
||||
@@ -45,14 +45,14 @@ static const std::map<const sparkStatus, const char *> sparkStatusNames = {
|
||||
{SPARK_SYNC_FAIL, "SPARK_SYNC_FAIL"},
|
||||
};
|
||||
|
||||
enum softStartStatus
|
||||
enum softStartStatusEnum
|
||||
{
|
||||
NORMAL,
|
||||
SOFT_START,
|
||||
ERROR,
|
||||
};
|
||||
|
||||
const std::map<const softStartStatus, const char *> softStartStatusNames = {
|
||||
const std::map<const softStartStatusEnum, const char *> softStartStatusNames = {
|
||||
{NORMAL, "NORMAL"},
|
||||
{SOFT_START, "SOFT_START"},
|
||||
{ERROR, "ERROR"},
|
||||
@@ -60,18 +60,17 @@ const std::map<const softStartStatus, const char *> softStartStatusNames = {
|
||||
|
||||
struct coilsStatus
|
||||
{
|
||||
int64_t trig_time = 0;
|
||||
int64_t spark_time = 0;
|
||||
int32_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;
|
||||
int64_t coilTime = 0;
|
||||
int64_t sparkTime = 0;
|
||||
int32_t sparkDelay = 0; // in microseconds
|
||||
sparkStatusEnum sparkStatus = sparkStatusEnum::SPARK_POS_OK;
|
||||
softStartStatusEnum softStartStatus = softStartStatusEnum::NORMAL;
|
||||
float peakPos = 0.0;
|
||||
float peakNeg = 0.0;
|
||||
float trigLevelPos = 0.0;
|
||||
float trigLevelNeg = 0.0;
|
||||
uint32_t nEvents = 0;
|
||||
uint32_t nMissedFiring = 0;
|
||||
};
|
||||
|
||||
// Task internal Status
|
||||
@@ -81,13 +80,12 @@ struct ignitionBoxStatus
|
||||
// coils pairs for each ignition
|
||||
coilsStatus coils12;
|
||||
coilsStatus coils34;
|
||||
// voltage from generator
|
||||
float volts_gen = 0.0;
|
||||
// enine rpm
|
||||
int32_t eng_rpm = 0;
|
||||
int32_t engRpm = 0;
|
||||
// debug values
|
||||
uint32_t n_queue_errors = 0;
|
||||
int32_t adc_read_time = 0;
|
||||
uint32_t nQueueErrors = 0;
|
||||
int32_t adcReadTime = 0;
|
||||
int32_t ioReadWriteTime = 0;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -9,7 +9,8 @@
|
||||
// Device Libraries
|
||||
#include <ADS1256.h>
|
||||
#include <AD5292.h>
|
||||
#include <PCA95x5.h>
|
||||
#include <extio.h>
|
||||
#include <Wire.h>
|
||||
|
||||
// ADC Channel mapping
|
||||
#define ADC_CH_PEAK_12P_IN SING_0
|
||||
@@ -24,34 +25,31 @@
|
||||
// Device Pointer structs for tasks
|
||||
struct Devices
|
||||
{
|
||||
std::unique_ptr<SPIClass> m_spi_a = nullptr;
|
||||
std::unique_ptr<SPIClass> m_spi_b = nullptr;
|
||||
|
||||
std::unique_ptr<AD5292> m_pot_a = nullptr;
|
||||
std::unique_ptr<AD5292> m_pot_b = nullptr;
|
||||
|
||||
std::unique_ptr<ADS1256> m_adc_a = nullptr;
|
||||
std::unique_ptr<ADS1256> m_adc_b = nullptr;
|
||||
|
||||
std::unique_ptr<PCA9555> m_expander_a = nullptr;
|
||||
std::unique_ptr<PCA9555> m_expander_b = nullptr;
|
||||
std::unique_ptr<PCA9555> m_expander_inputs_ab = nullptr;
|
||||
// Busses
|
||||
TwoWire *m_i2c = NULL;
|
||||
SPIClass *m_spi_a = NULL;
|
||||
SPIClass *m_spi_b = NULL;
|
||||
|
||||
// Bus Mutextes
|
||||
std::mutex m_spi_a_mutex;
|
||||
std::mutex m_spi_b_mutex;
|
||||
|
||||
std::mutex m_i2c_mutex;
|
||||
|
||||
// Device Pointers
|
||||
AD5292 *m_pot_a = NULL;
|
||||
AD5292 *m_pot_b = NULL;
|
||||
|
||||
ADS1256 *m_adc_a = NULL;
|
||||
ADS1256 *m_adc_b = NULL;
|
||||
|
||||
ExternalIO *m_ext_io = NULL;
|
||||
};
|
||||
|
||||
// Adc read channel wrapper to selet mux before reading
|
||||
inline float adcReadChannel(ADS1256 *adc, const uint8_t ch)
|
||||
{
|
||||
adc->setMUX(ch);
|
||||
// scarta 3 conversioni
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
adc->readSingle();
|
||||
}
|
||||
adc->readSingle();
|
||||
// ora lettura valida a 30kSPS → ~100 µs di settling
|
||||
return adc->convertToVoltage(adc->readSingle());
|
||||
}
|
||||
|
||||
129
RotaxMonitor/src/extio.cpp
Normal file
129
RotaxMonitor/src/extio.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
#include <extio.h>
|
||||
|
||||
// Static interrupt callback
|
||||
static void onExpanderInterrupt(void *arg)
|
||||
{
|
||||
auto cls = (ExternalIO *)(arg);
|
||||
if (!cls) // invalid args
|
||||
return;
|
||||
cls->extReadInterrupt();
|
||||
}
|
||||
|
||||
ExternalIO::ExternalIO(TwoWire &i2c, std::mutex &i2c_mutex, const uint8_t int_pin) : m_i2cMutex(i2c_mutex), m_i2c(i2c), m_intPin(int_pin)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_i2cMutex);
|
||||
// Attach OUT expanders on BUS
|
||||
m_outMap[EXPANDER_A_OUT_ADDR] = std::make_unique<PCA9555>();
|
||||
m_outMap[EXPANDER_A_OUT_ADDR]->attach(m_i2c, EXPANDER_A_OUT_ADDR);
|
||||
m_outMap[EXPANDER_B_OUT_ADDR] = std::make_unique<PCA9555>();
|
||||
m_outMap[EXPANDER_B_OUT_ADDR]->attach(m_i2c, EXPANDER_B_OUT_ADDR);
|
||||
|
||||
for (auto &[a, e] : m_outMap)
|
||||
{
|
||||
e->direction(PCA95x5::Direction::OUT_ALL);
|
||||
e->polarity(PCA95x5::Polarity::ORIGINAL_ALL);
|
||||
};
|
||||
|
||||
// Attach IN Expanders on Bus
|
||||
m_inMap[EXPANDER_A_IN_ADDR] = std::make_unique<PCA9555>();
|
||||
m_inMap[EXPANDER_A_IN_ADDR]->attach(m_i2c, EXPANDER_A_IN_ADDR);
|
||||
m_inMap[EXPANDER_B_IN_ADDR] = std::make_unique<PCA9555>();
|
||||
m_inMap[EXPANDER_B_IN_ADDR]->attach(m_i2c, EXPANDER_B_IN_ADDR);
|
||||
|
||||
for (auto &[a, e] : m_inMap)
|
||||
{
|
||||
e->direction(PCA95x5::Direction::IN_ALL);
|
||||
e->polarity(PCA95x5::Polarity::ORIGINAL_ALL);
|
||||
m_lastInputState[a] = e->read(); /// initialize input state to collect interrupts
|
||||
};
|
||||
}
|
||||
ExternalIO::~ExternalIO() {
|
||||
|
||||
}
|
||||
|
||||
void ExternalIO::extDigitalWrite(const uint32_t mappedPin, const bool val)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_i2cMutex);
|
||||
const io_t pa = map2pin(mappedPin);
|
||||
if (!m_outMap.contains(pa.addr))
|
||||
{
|
||||
LOG_ERROR("Undefined IO Expander addr: [", pa.addr, "]");
|
||||
return;
|
||||
}
|
||||
auto &io = m_outMap.at(pa.addr);
|
||||
if (!io->write(static_cast<PCA95x5::Port::Port>(pa.pin), val ? PCA95x5::Level::H : PCA95x5::Level::L))
|
||||
{
|
||||
LOG_ERROR("IO Expander [", pa.addr, "] Unable to WRITE Port [", pa.pin, "] to [", val ? "HIGH" : "LOW");
|
||||
LOG_ERROR("IO Expander Error [", io->i2c_error(), "]");
|
||||
}
|
||||
}
|
||||
|
||||
const bool ExternalIO::extDigitalRead(const uint32_t mappedPin)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_i2cMutex);
|
||||
const io_t pa = map2pin(mappedPin);
|
||||
if (!m_inMap.contains(pa.addr))
|
||||
{
|
||||
LOG_ERROR("Undefined IO Expander addr: [", pa.addr, "]");
|
||||
return false;
|
||||
}
|
||||
auto &io = m_inMap.at(pa.addr);
|
||||
const bool rv = io->read(static_cast<PCA95x5::Port::Port>(pa.pin)) == PCA95x5::Level::H ? true : false; // read value
|
||||
const uint8_t err = io->i2c_error();
|
||||
if (err)
|
||||
{
|
||||
LOG_ERROR("IO Expander [", pa.addr, "] Unable to READ Port [", pa.pin, "]");
|
||||
LOG_ERROR("IO Expander Error [", err, "]");
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
void ExternalIO::extAttachInterrupt(ExtInterruptCb cb)
|
||||
{
|
||||
attachInterruptArg(EXPANDER_ALL_INTERRUPT, onExpanderInterrupt, (void *)(this), FALLING);
|
||||
m_extInterruptCb = cb;
|
||||
}
|
||||
|
||||
void ExternalIO::extDetachInterrupt()
|
||||
{
|
||||
detachInterrupt(EXPANDER_ALL_INTERRUPT);
|
||||
}
|
||||
|
||||
void ExternalIO::extReadInterrupt()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_i2cMutex);
|
||||
disableInterrupt(EXPANDER_ALL_INTERRUPT);
|
||||
// read all registers and collect
|
||||
IOstate interruptState;
|
||||
for (auto &[a, e] : m_inMap)
|
||||
{
|
||||
interruptState[a] = e->read();
|
||||
}
|
||||
m_lastInputState = interruptState; // restore to current values
|
||||
// compare to last state to see the difference
|
||||
if (m_extInterruptCb)
|
||||
{
|
||||
for (auto &[a, v] : interruptState)
|
||||
{
|
||||
if (v)
|
||||
m_extInterruptCb(stat2map(a, v));
|
||||
}
|
||||
}
|
||||
|
||||
enableInterrupt(EXPANDER_ALL_INTERRUPT);
|
||||
}
|
||||
|
||||
const ExternalIO::io_t ExternalIO::map2pin(const uint32_t mappedIO)
|
||||
{
|
||||
return io_t{
|
||||
.addr = (uint8_t)((mappedIO >> 16) & (uint8_t)0xFF),
|
||||
.pin = (uint8_t)(mappedIO && (uint32_t)0xFF),
|
||||
};
|
||||
}
|
||||
|
||||
const uint32_t ExternalIO::stat2map(const uint8_t addr, const uint16_t stat)
|
||||
{
|
||||
if (!stat)
|
||||
return 0;
|
||||
return (uint32_t)(addr << 16) | (1UL << __builtin_ctz(stat));
|
||||
}
|
||||
49
RotaxMonitor/src/extio.h
Normal file
49
RotaxMonitor/src/extio.h
Normal file
@@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <DebugLog.h>
|
||||
#include <PCA95x5.h>
|
||||
|
||||
#include <pins.h>
|
||||
|
||||
#include <memory>
|
||||
#include <map>
|
||||
|
||||
class ExternalIO
|
||||
{
|
||||
using IOptr = std::unique_ptr<PCA9555>;
|
||||
using IOmap = std::map<const uint8_t, IOptr>;
|
||||
using IOstate = std::map<const uint8_t, uint16_t>;
|
||||
using ExtInterruptCb = std::function<void(const uint32_t)>;
|
||||
|
||||
struct io_t
|
||||
{
|
||||
uint8_t addr;
|
||||
uint8_t pin;
|
||||
};
|
||||
|
||||
public:
|
||||
ExternalIO(TwoWire &i2c, std::mutex &i2c_mutex, const uint8_t int_pin);
|
||||
~ExternalIO();
|
||||
|
||||
void extDigitalWrite(const uint32_t mappedPin, const bool val);
|
||||
const bool extDigitalRead(const uint32_t mappedPin);
|
||||
void extAttachInterrupt(ExtInterruptCb cb = nullptr);
|
||||
void extDetachInterrupt();
|
||||
void extReadInterrupt();
|
||||
|
||||
private:
|
||||
const io_t map2pin(const uint32_t mappedIO);
|
||||
const uint32_t stat2map(const uint8_t addr, const uint16_t stat);
|
||||
|
||||
private:
|
||||
const uint8_t m_intPin;
|
||||
IOmap m_inMap;
|
||||
IOmap m_outMap;
|
||||
uint8_t m_intPinChanged;
|
||||
IOstate m_lastInputState;
|
||||
ExtInterruptCb m_extInterruptCb = nullptr;
|
||||
std::mutex &m_i2cMutex;
|
||||
TwoWire &m_i2c;
|
||||
};
|
||||
@@ -26,22 +26,22 @@ void trig_isr_A(void *arg)
|
||||
case TRIG_FLAG_12P:
|
||||
case TRIG_FLAG_12N:
|
||||
// only on first trigger to avoid multiple firing due to noise, to be fixed with hardware debounce
|
||||
box->coils12.trig_time = time_us;
|
||||
box->coils12.coilTime = time_us;
|
||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
case TRIG_FLAG_34P:
|
||||
case TRIG_FLAG_34N:
|
||||
// only on first trigger to avoid multiple firing due to noise, to be fixed with hardware debounce
|
||||
box->coils34.trig_time = time_us;
|
||||
box->coils34.coilTime = time_us;
|
||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
case SPARK_FLAG_12:
|
||||
|
||||
box->coils12.spark_time = time_us;
|
||||
box->coils12.sparkTime = time_us;
|
||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
case SPARK_FLAG_34:
|
||||
box->coils34.spark_time = time_us;
|
||||
box->coils34.sparkTime = time_us;
|
||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
default:
|
||||
@@ -75,22 +75,22 @@ void trig_isr_B(void *arg)
|
||||
case TRIG_FLAG_12P:
|
||||
case TRIG_FLAG_12N:
|
||||
// only on first trigger to avoid multiple firing due to noise, to be fixed with hardware debounce
|
||||
box->coils12.trig_time = time_us;
|
||||
box->coils12.coilTime = time_us;
|
||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
case TRIG_FLAG_34P:
|
||||
case TRIG_FLAG_34N:
|
||||
// only on first trigger to avoid multiple firing due to noise, to be fixed with hardware debounce
|
||||
box->coils34.trig_time = time_us;
|
||||
box->coils34.coilTime = time_us;
|
||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
case SPARK_FLAG_12:
|
||||
|
||||
box->coils12.spark_time = time_us;
|
||||
box->coils12.sparkTime = time_us;
|
||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
case SPARK_FLAG_34:
|
||||
box->coils34.spark_time = time_us;
|
||||
box->coils34.sparkTime = time_us;
|
||||
xTaskNotifyFromISR(task_handle, params->flag, eSetValueWithOverwrite, &xHigherPriorityTaskWoken);
|
||||
break;
|
||||
default:
|
||||
|
||||
@@ -1,22 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
// Test device Flag
|
||||
// #define TEST
|
||||
|
||||
// Arduino Libraries
|
||||
#include <Arduino.h>
|
||||
#include "soc/gpio_struct.h"
|
||||
#include <map>
|
||||
#ifndef TEST
|
||||
|
||||
#include "pins.h"
|
||||
#else
|
||||
#include "pins_test.h"
|
||||
#endif
|
||||
#include "datastruct.h"
|
||||
|
||||
#define CORE_0 0
|
||||
#define CORE_1 1
|
||||
#define RT_TASK_STACK 2048 // in words
|
||||
#define RT_TASK_STACK 4096 // in words
|
||||
#define RT_TASK_PRIORITY (configMAX_PRIORITIES - 5) // highest priority after wifi tasks
|
||||
|
||||
struct isrParams
|
||||
|
||||
@@ -16,13 +16,19 @@
|
||||
#include <ui.h>
|
||||
#include <led.h>
|
||||
|
||||
// Defines to enable channel B
|
||||
#define CH_B_ENABLE
|
||||
// #define TEST
|
||||
// #define CH_A_ENABLE
|
||||
// #define CH_B_ENABLE
|
||||
#define CH_A_RT_ENABLE
|
||||
#define CH_B_RT_ENABLE
|
||||
// #define I2C_ENABLE
|
||||
#define WEB_ENABLE
|
||||
|
||||
// Debug Defines
|
||||
#define WIFI_SSID "AstroRotaxMonitor"
|
||||
#define WIFI_PASSWORD "maledettirotax"
|
||||
#define PSRAM_MAX 4096
|
||||
#define QUEUE_MAX 128
|
||||
#define HTOP_DELAY 2000
|
||||
|
||||
void setup()
|
||||
{
|
||||
@@ -45,14 +51,15 @@ void setup()
|
||||
LOG_DEBUG("ESP32 Heap:", ESP.getHeapSize());
|
||||
LOG_DEBUG("ESP32 Sketch:", ESP.getFreeSketchSpace());
|
||||
|
||||
// Init Wifi station
|
||||
// Init Wifi station
|
||||
#ifdef WEB_ENABLE
|
||||
LOG_INFO("Initializing WiFi...");
|
||||
WiFi.mode(WIFI_AP);
|
||||
IPAddress local_IP(10, 11, 12, 1);
|
||||
IPAddress gateway(10, 11, 12, 1);
|
||||
IPAddress subnet(255, 255, 255, 0);
|
||||
WiFi.softAPConfig(local_IP, gateway, subnet);
|
||||
WiFi.setTxPower(WIFI_POWER_13dBm); // reduce wifi power
|
||||
WiFi.setTxPower(WIFI_POWER_5dBm); // reduce wifi power
|
||||
if (WiFi.softAP(WIFI_SSID, WIFI_PASSWORD))
|
||||
{
|
||||
LOG_INFO("WiFi AP Mode Started");
|
||||
@@ -67,6 +74,7 @@ void setup()
|
||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||
esp_restart();
|
||||
}
|
||||
#endif
|
||||
|
||||
// Initialize Interrupt pins on PICKUP detectors
|
||||
initTriggerPinsInputs();
|
||||
@@ -79,23 +87,55 @@ void loop()
|
||||
{
|
||||
// global variables
|
||||
RGBled led;
|
||||
led.setBrightness(0.025f);
|
||||
led.setStatus(RGBled::LedStatus::INIT);
|
||||
|
||||
Devices dev;
|
||||
bool running = true;
|
||||
std::mutex fs_mutex;
|
||||
LITTLEFSGuard fsGuard;
|
||||
|
||||
//////// INIT SPI PORTS ////////
|
||||
//////// INIT SPI INTERFACES ////////
|
||||
bool spiA_ok = true;
|
||||
bool spiB_ok = true;
|
||||
// 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
|
||||
// #ifdef CH_B_ENABLE
|
||||
// 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
|
||||
//////// INIT SPI INTERFACES ////////
|
||||
LOG_DEBUG("Init SPI Interfaces");
|
||||
#ifdef CH_A_ENABLE
|
||||
LOG_DEBUG("Begin Init SPI_A");
|
||||
SPIClass SPI_A(HSPI);
|
||||
spiA_ok = SPI_A.begin(SPI_A_SCK, SPI_A_MISO, SPI_A_MOSI);
|
||||
SPI_A.setDataMode(SPI_MODE1); // ADS1256 requires SPI mode 1
|
||||
LOG_DEBUG("Init SPI_A -> OK");
|
||||
delay(100);
|
||||
LOG_DEBUG("Begin Init ADC_A");
|
||||
ADS1256 ADC_A(ADC_A_DRDY, ADS1256::PIN_UNUSED, ADS1256::PIN_UNUSED, ADC_A_CS, 2.5, &SPI_A);
|
||||
ADC_A.InitializeADC();
|
||||
ADC_A.setPGA(PGA_1);
|
||||
ADC_A.setDRATE(DRATE_7500SPS);
|
||||
dev.m_adc_a = &ADC_A;
|
||||
dev.m_spi_a = &SPI_A;
|
||||
LOG_DEBUG("Init ADC_A -> OK");
|
||||
delay(100);
|
||||
#endif
|
||||
|
||||
#ifdef CH_B_ENABLE
|
||||
LOG_DEBUG("Begin Init SPI_B");
|
||||
SPIClass SPI_B(FSPI);
|
||||
spiB_ok = SPI_B.begin(SPI_B_SCK, SPI_B_MISO, SPI_B_MOSI);
|
||||
SPI_B.setDataMode(SPI_MODE1); // ADS1256 requires SPI mode 1
|
||||
LOG_DEBUG("Init SPI_B -> OK");
|
||||
delay(100);
|
||||
LOG_DEBUG("Begin Init ADC_B");
|
||||
ADS1256 ADC_B(ADC_B_DRDY, ADS1256::PIN_UNUSED, ADS1256::PIN_UNUSED, ADC_B_CS, 2.5, &SPI_B);
|
||||
ADC_B.InitializeADC();
|
||||
ADC_B.setPGA(PGA_1);
|
||||
ADC_B.setDRATE(DRATE_7500SPS);
|
||||
dev.m_adc_b = &ADC_B;
|
||||
dev.m_spi_b = &SPI_B;
|
||||
LOG_DEBUG("Init ADC_B -> OK");
|
||||
delay(100);
|
||||
#endif
|
||||
|
||||
if (!spiA_ok || !spiB_ok)
|
||||
{
|
||||
LOG_ERROR("Unable to Initialize SPI Busses");
|
||||
@@ -103,38 +143,43 @@ void loop()
|
||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||
esp_restart();
|
||||
}
|
||||
LOG_DEBUG("Init SPI OK");
|
||||
|
||||
// Resources Initialization
|
||||
std::shared_ptr<Devices> dev = std::make_shared<Devices>();
|
||||
// dev->m_spi_a = std::make_unique<SPIClass>(SPI_A);
|
||||
// dev->m_spi_b = std::make_unique<SPIClass>(SPI_B);
|
||||
LOG_DEBUG("Init SPI -> OK");
|
||||
|
||||
// // Init ADC_A
|
||||
// dev->m_adc_a = std::make_unique<ADS1256>(ADC_A_DRDY, ADS1256::PIN_UNUSED, ADS1256::PIN_UNUSED, ADC_A_CS, 2.5, &SPI_A);
|
||||
// dev->m_adc_b = std::make_unique<ADS1256>(ADC_B_DRDY, ADS1256::PIN_UNUSED, ADS1256::PIN_UNUSED, ADC_B_CS, 2.5, &SPI_B);
|
||||
//////// INIT I2C INTERFACES ////////
|
||||
#ifdef I2C_ENABLE
|
||||
LOG_DEBUG("Init I2C Interfaces");
|
||||
bool i2c_ok = true;
|
||||
i2c_ok = Wire.begin(SDA, SCL, 100000);
|
||||
if (!i2c_ok)
|
||||
{
|
||||
LOG_ERROR("Unable to Initialize I2C Bus");
|
||||
LOG_ERROR("5 seconds to restart...");
|
||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||
esp_restart();
|
||||
}
|
||||
LOG_DEBUG("Init I2c ok");
|
||||
|
||||
// dev->m_adc_a->InitializeADC();
|
||||
// dev->m_adc_a->setPGA(PGA_1);
|
||||
// dev->m_adc_a->setDRATE(DRATE_7500SPS);
|
||||
|
||||
// dev->m_adc_b->InitializeADC();
|
||||
// dev->m_adc_b->setPGA(PGA_1);
|
||||
// dev->m_adc_b->setDRATE(DRATE_7500SPS);
|
||||
// Init IO Expanders
|
||||
ExternalIO extIo(Wire, dev.m_i2c_mutex, EXPANDER_ALL_INTERRUPT);
|
||||
dev.m_ext_io = &extIo;
|
||||
#endif
|
||||
|
||||
//////// INIT REALTIME TASKS PARAMETERS ////////
|
||||
#ifdef CH_A_RT_ENABLE
|
||||
const rtIgnitionTask::rtTaskParams taskA_params{
|
||||
.rt_running = true,
|
||||
.name = "rtIgnTask_A",
|
||||
.rt_stack_size = RT_TASK_STACK,
|
||||
.rt_priority = RT_TASK_PRIORITY,
|
||||
.rt_int = rtIgnitionTask::rtTaskInterruptParams{
|
||||
.isr_ptr = &trig_isr_A,
|
||||
.trig_pin_12p = TRIG_PIN_A12P,
|
||||
.trig_pin_12n = TRIG_PIN_A12N,
|
||||
.trig_pin_34p = TRIG_PIN_A34P,
|
||||
.trig_pin_34n = TRIG_PIN_A34N,
|
||||
.spark_pin_12 = SPARK_PIN_A12,
|
||||
.spark_pin_34 = SPARK_PIN_A34},
|
||||
.isrPtr = &trig_isr_A,
|
||||
.trigPin_12p = TRIG_PIN_A12P,
|
||||
.trigPin_12n = TRIG_PIN_A12N,
|
||||
.trigPin_34p = TRIG_PIN_A34P,
|
||||
.trigPin_34n = TRIG_PIN_A34N,
|
||||
.sparkPin_12 = SPARK_PIN_A12,
|
||||
.sparkPin_34 = SPARK_PIN_A34},
|
||||
.rt_io = rtIgnitionTask::rtTaskIOParams{
|
||||
.pot_cs_12 = POT_CS_A12,
|
||||
.pot_cs_34 = POT_CS_A34,
|
||||
@@ -151,21 +196,22 @@ void loop()
|
||||
.relay_out_34 = RELAY_OUT_A34,
|
||||
},
|
||||
.rt_queue = nullptr,
|
||||
.dev = dev};
|
||||
|
||||
.dev = &dev};
|
||||
#endif
|
||||
#ifdef CH_B_RT_ENABLE
|
||||
const rtIgnitionTask::rtTaskParams taskB_params{
|
||||
.rt_running = true,
|
||||
.name = "rtIgnTask_B",
|
||||
.rt_stack_size = RT_TASK_STACK,
|
||||
.rt_priority = RT_TASK_PRIORITY,
|
||||
.rt_int = rtIgnitionTask::rtTaskInterruptParams{
|
||||
.isr_ptr = &trig_isr_B,
|
||||
.trig_pin_12p = TRIG_PIN_B12P,
|
||||
.trig_pin_12n = TRIG_PIN_B12N,
|
||||
.trig_pin_34p = TRIG_PIN_B34P,
|
||||
.trig_pin_34n = TRIG_PIN_B34N,
|
||||
.spark_pin_12 = SPARK_PIN_B12,
|
||||
.spark_pin_34 = SPARK_PIN_B34},
|
||||
.isrPtr = &trig_isr_B,
|
||||
.trigPin_12p = TRIG_PIN_B12P,
|
||||
.trigPin_12n = TRIG_PIN_B12N,
|
||||
.trigPin_34p = TRIG_PIN_B34P,
|
||||
.trigPin_34n = TRIG_PIN_B34N,
|
||||
.sparkPin_12 = SPARK_PIN_B12,
|
||||
.sparkPin_34 = SPARK_PIN_B34},
|
||||
.rt_io = rtIgnitionTask::rtTaskIOParams{
|
||||
.pot_cs_12 = POT_CS_B12,
|
||||
.pot_cs_34 = POT_CS_B34,
|
||||
@@ -182,15 +228,30 @@ void loop()
|
||||
.relay_out_34 = RELAY_OUT_B34,
|
||||
},
|
||||
.rt_queue = nullptr,
|
||||
.dev = dev};
|
||||
.dev = &dev};
|
||||
#endif
|
||||
|
||||
auto task_A = rtIgnitionTask(taskA_params, 4096, 256, CORE_0, fs_mutex);
|
||||
delay(50);
|
||||
auto task_B = rtIgnitionTask(taskB_params, 4096, 256, CORE_1, fs_mutex);
|
||||
//////// SPAWN REALTIME TASKS ////////
|
||||
bool tasK_A_rt = true;
|
||||
bool task_B_rt = true;
|
||||
BaseType_t ignA_task_success = pdPASS;
|
||||
BaseType_t ignB_task_success = pdPASS;
|
||||
|
||||
#ifdef CH_A_RT_ENABLE
|
||||
auto task_A = rtIgnitionTask(taskA_params, PSRAM_MAX, QUEUE_MAX, CORE_0, fs_mutex);
|
||||
ignA_task_success = task_A.getStatus() == rtIgnitionTask::OK ? pdPASS : pdFAIL;
|
||||
tasK_A_rt = task_A.start();
|
||||
delay(100);
|
||||
#endif
|
||||
|
||||
#ifdef CH_B_RT_ENABLE
|
||||
auto task_B = rtIgnitionTask(taskB_params, PSRAM_MAX, QUEUE_MAX, CORE_1, fs_mutex);
|
||||
ignB_task_success = task_B.getStatus() == rtIgnitionTask::OK ? pdPASS : pdFAIL;
|
||||
task_B_rt = task_B.start();
|
||||
delay(100);
|
||||
#endif
|
||||
|
||||
// Ignition A on Core 0
|
||||
auto ignA_task_success = task_A.getStatus() == rtIgnitionTask::OK ? pdPASS : pdFAIL;
|
||||
auto ignB_task_success = task_B.getStatus() == rtIgnitionTask::OK ? pdPASS : pdFAIL;
|
||||
if (ignA_task_success != pdPASS || ignB_task_success != pdPASS)
|
||||
{
|
||||
LOG_ERROR("Unable to initialize ISR task");
|
||||
@@ -198,33 +259,70 @@ void loop()
|
||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||
esp_restart();
|
||||
}
|
||||
|
||||
const bool tasK_A_rt = task_A.start();
|
||||
delay(50);
|
||||
const bool task_B_rt = task_B.start();
|
||||
if (tasK_A_rt != true || task_B_rt != true)
|
||||
{
|
||||
led.setStatus(RGBled::LedStatus::ERROR);
|
||||
LOG_ERROR("Unable to start realtime tasks");
|
||||
} else
|
||||
LOG_DEBUG("Real Time Tasks A & B initialized");
|
||||
led.setStatus(RGBled::LedStatus::OK);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_DEBUG("Real Time Tasks A & B initialized");
|
||||
led.setStatus(RGBled::LedStatus::OK);
|
||||
}
|
||||
|
||||
AstroWebServer webPage(80, LittleFS); // Initialize webserver and Websocket
|
||||
//////// SPAWN WEBSERVER and WEBSOCKET ////////
|
||||
ArduinoJson::JsonDocument json_data;
|
||||
bool data_a, data_b;
|
||||
task_A.onMessage([&webPage, &json_data, &data_a](ignitionBoxStatusFiltered sts){
|
||||
json_data["box_a"] = sts.toJson();
|
||||
data_a = true;
|
||||
bool data_a = false, data_b = false;
|
||||
#ifdef WEB_ENABLE
|
||||
AstroWebServer webPage(80, LittleFS);
|
||||
delay(100);
|
||||
|
||||
#ifdef CH_A_RT_ENABLE
|
||||
task_A.onMessage([&webPage, &json_data, &data_a](ignitionBoxStatusFiltered sts)
|
||||
{
|
||||
json_data["box_a"] = sts.toJson();
|
||||
data_a = true; });
|
||||
#endif
|
||||
|
||||
#ifdef CH_B_RT_ENABLE
|
||||
task_B.onMessage([&webPage, &json_data, &data_b](ignitionBoxStatusFiltered sts)
|
||||
{
|
||||
json_data["box_b"] = sts.toJson();
|
||||
data_b = true; });
|
||||
#endif
|
||||
|
||||
webPage.registerWsCommand("saveEnable", [&task_A, &task_B](const ArduinoJson::JsonDocument &doc) {
|
||||
if(!doc["params"].is<ArduinoJson::JsonObject>()) return;
|
||||
if(!doc["filename_a"].is<std::string>() ||!doc["filename_b"].is<std::string>()){
|
||||
LOG_ERROR("saveEnable invalid or missing filenames");
|
||||
return;
|
||||
}
|
||||
|
||||
task_A.enableSave(true, doc["filename_a"].as<std::string>());
|
||||
task_B.enableSave(true, doc["filename_a"].as<std::string>());
|
||||
return; });
|
||||
|
||||
webPage.registerWsCommand("saveDisable", [&task_A, &task_B](const ArduinoJson::JsonDocument &doc) {
|
||||
task_A.enableSave(false, "");
|
||||
task_B.enableSave(false, ""); });
|
||||
|
||||
webPage.registerWsCommand("downloadHistory", [](const ArduinoJson::JsonDocument &doc) {
|
||||
LOG_WARN("Command downloadHistory not Implemented");
|
||||
});
|
||||
|
||||
webPage.registerWsCommand("clearHistory", [](const ArduinoJson::JsonDocument &doc) {
|
||||
LOG_WARN("Command clearHistory not Implemented");
|
||||
});
|
||||
|
||||
webPage.registerWsCommand("startTest", [](const ArduinoJson::JsonDocument &doc) {
|
||||
LOG_WARN("Command startTest not Implemented");
|
||||
});
|
||||
|
||||
webPage.registerWsCommand("stopTest", [](const ArduinoJson::JsonDocument &doc) {
|
||||
LOG_WARN("Command stopTest not Implemented");
|
||||
});
|
||||
|
||||
task_B.onMessage([&webPage, &json_data, &data_b](ignitionBoxStatusFiltered sts){
|
||||
json_data["box_b"] = sts.toJson();
|
||||
data_b = true;
|
||||
});
|
||||
|
||||
// task_A.enableSave(true, "ignitionA_test.csv");
|
||||
// task_B.enableSave(true, "ignitionB_test.csv");
|
||||
#endif
|
||||
|
||||
uint32_t monitor_loop = millis();
|
||||
uint32_t data_loop = monitor_loop;
|
||||
@@ -232,18 +330,22 @@ void loop()
|
||||
while (running)
|
||||
{
|
||||
uint32_t this_loop = millis();
|
||||
if (this_loop - monitor_loop > 2000)
|
||||
if (this_loop - monitor_loop > HTOP_DELAY)
|
||||
{
|
||||
clearScreen();
|
||||
printRunningTasksMod(Serial);
|
||||
monitor_loop = millis();
|
||||
}
|
||||
if ((data_a && data_b) || (this_loop - data_loop > 500)) {
|
||||
#ifdef WEB_ENABLE
|
||||
if ((data_a && data_b) || ((this_loop - data_loop > 500) && (data_b || data_b)))
|
||||
{
|
||||
webPage.sendWsData(json_data.as<String>());
|
||||
json_data.clear();
|
||||
data_a = data_b = false;
|
||||
data_loop = millis();
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
#endif
|
||||
} //////////////// INNER LOOP /////////////////////
|
||||
|
||||
} ////////////////////// MAIN LOOP //////////////////////
|
||||
|
||||
@@ -33,16 +33,15 @@
|
||||
// =====================
|
||||
// SPI BUS ADC2 (HSPI)
|
||||
// =====================
|
||||
#define SPI_B_MOSI 36
|
||||
#define SPI_B_SCK 37
|
||||
#define SPI_B_MISO 38
|
||||
#define SPI_B_MOSI 17
|
||||
#define SPI_B_SCK 18
|
||||
#define SPI_B_MISO 8
|
||||
|
||||
// =====================
|
||||
// I2C BUS (PCA9555)
|
||||
// =====================
|
||||
#define SDA 8
|
||||
#define SCL 9
|
||||
#define I2C_INT 17
|
||||
#define SDA 21
|
||||
#define SCL 47
|
||||
|
||||
// =====================
|
||||
// ADC CONTROL
|
||||
@@ -50,14 +49,8 @@
|
||||
#define ADC_A_CS 14
|
||||
#define ADC_A_DRDY 13
|
||||
|
||||
#define ADC_B_CS 21
|
||||
#define ADC_B_DRDY 47
|
||||
|
||||
// =====================
|
||||
// DIGITAL POT
|
||||
// =====================
|
||||
#define POT_A_CS 18
|
||||
#define POT_B_CS 35
|
||||
#define ADC_B_CS 3
|
||||
#define ADC_B_DRDY 46
|
||||
|
||||
// =====================
|
||||
// TRIGGER INPUT INTERRUPTS
|
||||
@@ -79,86 +72,87 @@
|
||||
#define SPARK_PIN_B12 1
|
||||
#define SPARK_PIN_B34 2
|
||||
|
||||
// =====================
|
||||
// PCA9555 I/O EXPANDER BOX_A
|
||||
// =====================
|
||||
// +++++++++++++++++++++
|
||||
// MACRO TO COMBINE PIN NUMBER AND ADDRESS
|
||||
#define PIN2ADDR(p, a) ((1UL << p) | ((uint32_t)(a) << 16))
|
||||
// +++++++++++++++++++++
|
||||
|
||||
#define EXPANDER_A_ADDR 0x010101
|
||||
// =====================
|
||||
// PCA9555 I/O EXPANDER INTERRUPT (Common)
|
||||
// =====================
|
||||
#define EXPANDER_ALL_INTERRUPT 45
|
||||
|
||||
// =====================
|
||||
// PCA9555 I/O EXPANDER BOX_A (OUT)
|
||||
// =====================
|
||||
#define EXPANDER_A_OUT_ADDR 0x7F
|
||||
|
||||
// --- DIGITAL POT CHIP SELECT LINES ---
|
||||
#define POT_CS_A12 0
|
||||
#define POT_CS_A34 1
|
||||
#define POT_CS_A12 PIN2ADDR(0, EXPANDER_A_OUT_ADDR)
|
||||
#define POT_CS_A34 PIN2ADDR(1, EXPANDER_A_OUT_ADDR)
|
||||
|
||||
// --- SOFT START FORCE LINES ---
|
||||
#define SS_FORCE_A 2
|
||||
#define SS_INIBHIT_A12 3
|
||||
#define SS_INHIBIT_A34 4
|
||||
#define SS_FORCE_A PIN2ADDR(2, EXPANDER_A_OUT_ADDR)
|
||||
#define SS_INIBHIT_A12 PIN2ADDR(3, EXPANDER_A_OUT_ADDR)
|
||||
#define SS_INHIBIT_A34 PIN2ADDR(4, EXPANDER_A_OUT_ADDR)
|
||||
|
||||
// --- SAMPLE AND HOLD ARM AND DISCHARGE ---
|
||||
#define SH_DISCH_A12 5
|
||||
#define SH_DISCH_A34 6
|
||||
#define SH_ARM_A12 7
|
||||
#define SH_ARM_A34 8
|
||||
#define SH_DISCH_A12 PIN2ADDR(5, EXPANDER_A_OUT_ADDR)
|
||||
#define SH_DISCH_A34 PIN2ADDR(6, EXPANDER_A_OUT_ADDR)
|
||||
#define SH_ARM_A12 PIN2ADDR(7, EXPANDER_A_OUT_ADDR)
|
||||
#define SH_ARM_A34 PIN2ADDR(8, EXPANDER_A_OUT_ADDR)
|
||||
|
||||
// --- RELAY ---
|
||||
#define RELAY_IN_A12 9
|
||||
#define RELAY_OUT_A12 10
|
||||
#define RELAY_IN_A34 11
|
||||
#define RELAY_OUT_A34 12
|
||||
|
||||
// --- STATUS / BUTTON ---
|
||||
#define STA_2 13
|
||||
#define STA_3 14
|
||||
#define STA_4 15
|
||||
#define RELAY_IN_A12 PIN2ADDR(9, EXPANDER_A_OUT_ADDR)
|
||||
#define RELAY_OUT_A12 PIN2ADDR(10, EXPANDER_A_OUT_ADDR)
|
||||
#define RELAY_IN_A34 PIN2ADDR(11, EXPANDER_A_OUT_ADDR)
|
||||
#define RELAY_OUT_A34 PIN2ADDR(12, EXPANDER_A_OUT_ADDR)
|
||||
|
||||
// =====================
|
||||
// PCA9555 I/O EXPANDER BOX_B
|
||||
// PCA9555 I/O EXPANDER BOX_A (IN)
|
||||
// =====================
|
||||
#define EXPANDER_A_IN_ADDR 0x7F
|
||||
|
||||
#define EXPANDER_B_ADDR 0x101010
|
||||
#define SS_A12_ON PIN2ADDR(0, EXPANDER_A_IN_ADDR)
|
||||
#define SS_A12_OFF PIN2ADDR(1, EXPANDER_A_IN_ADDR)
|
||||
#define SS_A34_ON PIN2ADDR(2, EXPANDER_A_IN_ADDR)
|
||||
#define SS_A34_OFF PIN2ADDR(3, EXPANDER_A_IN_ADDR)
|
||||
|
||||
// =====================
|
||||
// PCA9555 I/O EXPANDER BOX_B (OUT)
|
||||
// =====================
|
||||
#define EXPANDER_B_OUT_ADDR 0x7F
|
||||
|
||||
// --- DIGITAL POT CHIP SELECT LINES ---
|
||||
#define POT_CS_B12 0
|
||||
#define POT_CS_B34 1
|
||||
#define POT_CS_B12 PIN2ADDR(0, EXPANDER_B_OUT_ADDR)
|
||||
#define POT_CS_B34 PIN2ADDR(1, EXPANDER_B_OUT_ADDR)
|
||||
|
||||
// --- SOFT START FORCE LINES ---
|
||||
#define SS_FORCE_B 2
|
||||
#define SS_INIBHIT_B12 3
|
||||
#define SS_INHIBIT_B34 4
|
||||
#define SS_FORCE_B PIN2ADDR(2, EXPANDER_B_OUT_ADDR)
|
||||
#define SS_INIBHIT_B12 PIN2ADDR(3, EXPANDER_B_OUT_ADDR)
|
||||
#define SS_INHIBIT_B34 PIN2ADDR(4, EXPANDER_B_OUT_ADDR)
|
||||
|
||||
// --- SAMPLE AND HOLD ARM AND DISCHARGE ---
|
||||
#define SH_DISCH_B12 5
|
||||
#define SH_DISCH_B34 6
|
||||
#define SH_ARM_B12 7
|
||||
#define SH_ARM_B34 8
|
||||
#define SH_DISCH_B12 PIN2ADDR(5, EXPANDER_B_OUT_ADDR)
|
||||
#define SH_DISCH_B34 PIN2ADDR(6, EXPANDER_B_OUT_ADDR)
|
||||
#define SH_ARM_B12 PIN2ADDR(7, EXPANDER_B_OUT_ADDR)
|
||||
#define SH_ARM_B34 PIN2ADDR(8, EXPANDER_B_OUT_ADDR)
|
||||
|
||||
// --- RELAY ---
|
||||
#define RELAY_IN_B12 9
|
||||
#define RELAY_OUT_B12 10
|
||||
#define RELAY_IN_B34 11
|
||||
#define RELAY_OUT_B34 12
|
||||
|
||||
// --- STATUS / BUTTON ---
|
||||
#define STA_2 13
|
||||
#define STA_3 14
|
||||
#define STA_4 15
|
||||
#define RELAY_IN_B12 PIN2ADDR(9, EXPANDER_B_OUT_ADDR)
|
||||
#define RELAY_OUT_B12 PIN2ADDR(10, EXPANDER_B_OUT_ADDR)
|
||||
#define RELAY_IN_B34 PIN2ADDR(11, EXPANDER_B_OUT_ADDR)
|
||||
#define RELAY_OUT_B34 PIN2ADDR(12, EXPANDER_B_OUT_ADDR)
|
||||
|
||||
// =====================
|
||||
// PCA9555 I/O EXPANDER INPUTS A+B
|
||||
// PCA9555 I/O EXPANDER BOX_B (IN)
|
||||
// =====================
|
||||
#define EXPANDER_B_IN_ADDR 0x7F
|
||||
|
||||
#define EXPANDER_IN_ADDR 0x0a0a0a
|
||||
|
||||
#define SS_A12_ON
|
||||
#define SS_A12_OFF
|
||||
#define SS_A34_ON
|
||||
#define SS_A34_OFF
|
||||
|
||||
#define SS_B12_ON
|
||||
#define SS_B12_OFF
|
||||
#define SS_B34_ON
|
||||
#define SS_B34_OFF
|
||||
|
||||
#define SS_B12_ON PIN2ADDR(0, EXPANDER_B_IN_ADDR)
|
||||
#define SS_B12_OFF PIN2ADDR(1, EXPANDER_B_IN_ADDR)
|
||||
#define SS_B34_ON PIN2ADDR(2, EXPANDER_B_IN_ADDR)
|
||||
#define SS_B34_OFF PIN2ADDR(3, EXPANDER_B_IN_ADDR)
|
||||
|
||||
// Init Pin Functions
|
||||
inline void initTriggerPinsInputs()
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
|
||||
// =====================
|
||||
// UART DEBUG
|
||||
// =====================
|
||||
#define UART_TX 1 // TX0 (USB seriale)
|
||||
#define UART_RX 3 // RX0
|
||||
|
||||
|
||||
// =====================
|
||||
// SPI BUS
|
||||
// =====================
|
||||
#define SPI_A_MOSI 23
|
||||
#define SPI_A_MISO 19
|
||||
#define SPI_A_SCK 18
|
||||
|
||||
|
||||
// =====================
|
||||
// I2C BUS
|
||||
// =====================
|
||||
#define SDA 21
|
||||
#define SCL 22
|
||||
|
||||
|
||||
// =====================
|
||||
// ADC CONTROL (SPI + interrupt safe)
|
||||
// =====================
|
||||
#define ADC_A_CS 5 // chip select
|
||||
#define ADC_A_DRDY 34 // input only + interrupt perfetto
|
||||
#define ADC_A_RST 27 // output
|
||||
#define ADC_A_SYNC 26 // output
|
||||
|
||||
|
||||
// =====================
|
||||
// DIGITAL OUT
|
||||
// =====================
|
||||
#define POT_A_CS 25
|
||||
#define POT_B_CS 33
|
||||
|
||||
|
||||
// =====================
|
||||
// TRIGGER INPUT INTERRUPTS
|
||||
// =====================
|
||||
#define TRIG_PIN_A12P 35
|
||||
#define TRIG_PIN_A12N 32
|
||||
#define TRIG_PIN_A34P 39
|
||||
#define TRIG_PIN_A34N 36
|
||||
|
||||
|
||||
// =====================
|
||||
// SPARK DETECT INTERRUPTS
|
||||
// =====================
|
||||
#define SPARK_PIN_A12 4
|
||||
#define SPARK_PIN_A34 2
|
||||
|
||||
// =====================
|
||||
// PCA9555 (I2C EXPANDER)
|
||||
// =====================
|
||||
|
||||
// --- RESET LINES ---
|
||||
#define RST_EXT_A12P 0
|
||||
#define RST_EXT_A12N 1
|
||||
#define RST_EXT_A34P 2
|
||||
#define RST_EXT_A34N 3
|
||||
|
||||
// --- RELAY ---
|
||||
#define SH_ARM_A34 8
|
||||
|
||||
|
||||
// Init Pin Functions
|
||||
inline void initTriggerPinsInputs()
|
||||
{
|
||||
pinMode(TRIG_PIN_A12P, INPUT_PULLDOWN);
|
||||
pinMode(TRIG_PIN_A12N, INPUT_PULLDOWN);
|
||||
pinMode(TRIG_PIN_A34P, INPUT_PULLDOWN);
|
||||
pinMode(TRIG_PIN_A34N, INPUT_PULLDOWN);
|
||||
}
|
||||
|
||||
inline void initSparkPinInputs()
|
||||
{
|
||||
pinMode(SPARK_PIN_A12, INPUT_PULLDOWN);
|
||||
pinMode(SPARK_PIN_A34, INPUT_PULLDOWN);
|
||||
}
|
||||
@@ -1,11 +1,12 @@
|
||||
#include "tasks.h"
|
||||
#include <esp_timer.h>
|
||||
#include <datasave.h>
|
||||
#include <mutex>
|
||||
|
||||
//// GLOBAL STATIC FUNCTIONS
|
||||
|
||||
// Timeout callback for microsecond precision
|
||||
void spark_timeout_callback(void *arg)
|
||||
void IRAM_ATTR spark_timeout_callback(void *arg)
|
||||
{
|
||||
TaskHandle_t handle = (TaskHandle_t)arg;
|
||||
xTaskNotify(handle, SPARK_FLAG_TIMEOUT, eSetValueWithOverwrite);
|
||||
@@ -15,9 +16,12 @@ void spark_timeout_callback(void *arg)
|
||||
void rtIgnitionTask::rtIgnitionTask_manager(void *pvParameters)
|
||||
{
|
||||
rtIgnitionTask *cls = (rtIgnitionTask *)pvParameters;
|
||||
auto last_loop = millis();
|
||||
uint32_t count(0);
|
||||
while (cls->m_running)
|
||||
{
|
||||
cls->run();
|
||||
vTaskDelay(pdMS_TO_TICKS(1));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,155 +37,156 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
|
||||
}
|
||||
|
||||
// Task Parameters and Devices
|
||||
rtTaskParams *params = (rtTaskParams *)pvParameters;
|
||||
const rtTaskInterruptParams rt_int = params->rt_int; // copy to avoid external override
|
||||
const rtTaskIOParams rt_rst = params->rt_io; // copy to avoid external override
|
||||
QueueHandle_t rt_queue = params->rt_queue;
|
||||
Devices *dev = params->dev.get();
|
||||
ADS1256 *adc = dev->m_adc_a.get();
|
||||
PCA9555 *io = dev->m_expander_a.get();
|
||||
const rtTaskParams *params = (const rtTaskParams *)pvParameters;
|
||||
const rtTaskInterruptParams rtInterrupts = params->rt_int; // copy to avoid external override
|
||||
const rtTaskIOParams rtResets = params->rt_io; // copy to avoid external override
|
||||
QueueHandle_t rtQueue = params->rt_queue;
|
||||
Devices *dev = params->dev;
|
||||
ExternalIO *io = dev->m_ext_io;
|
||||
ADS1256 *adc = params->name == "rtIgnTask_A" ? dev->m_adc_a : dev->m_adc_b;
|
||||
std::mutex &spi_mutex = params->name == "rtIgnTask_A" ? dev->m_spi_a_mutex : dev->m_spi_b_mutex;
|
||||
|
||||
TaskStatus_t rt_task_info;
|
||||
vTaskGetInfo(NULL, &rt_task_info, pdFALSE, eInvalid);
|
||||
// Geta task name and additiona info for debug messages
|
||||
TaskStatus_t rtTaskInfo;
|
||||
vTaskGetInfo(NULL, &rtTaskInfo, pdFALSE, eInvalid);
|
||||
LOG_INFO("rtTask Params OK [", params->name.c_str(), "]");
|
||||
|
||||
const auto rt_task_name = pcTaskGetName(rt_task_info.xHandle);
|
||||
LOG_INFO("rtTask Params OK [", rt_task_name, "]");
|
||||
|
||||
ignitionBoxStatus ign_box_sts;
|
||||
// Status of ignition box for this task, to be used locally and passed to isr to get timing
|
||||
ignitionBoxStatus ignBoxStatus;
|
||||
|
||||
// Variables for ISR, static to be fixed in memory locations
|
||||
isrParams isr_params_t12p{
|
||||
isrParams isrParams_t12p{
|
||||
.flag = TRIG_FLAG_12P,
|
||||
.ign_stat = &ign_box_sts,
|
||||
.rt_handle_ptr = rt_task_info.xHandle};
|
||||
isrParams isr_params_t12n{
|
||||
.ign_stat = &ignBoxStatus,
|
||||
.rt_handle_ptr = rtTaskInfo.xHandle};
|
||||
isrParams isrParams_t12n{
|
||||
.flag = TRIG_FLAG_12N,
|
||||
.ign_stat = &ign_box_sts,
|
||||
.rt_handle_ptr = rt_task_info.xHandle};
|
||||
isrParams isr_params_t34p{
|
||||
.ign_stat = &ignBoxStatus,
|
||||
.rt_handle_ptr = rtTaskInfo.xHandle};
|
||||
isrParams isrParams_t34p{
|
||||
.flag = TRIG_FLAG_34P,
|
||||
.ign_stat = &ign_box_sts,
|
||||
.rt_handle_ptr = rt_task_info.xHandle};
|
||||
isrParams isr_params_t34n{
|
||||
.ign_stat = &ignBoxStatus,
|
||||
.rt_handle_ptr = rtTaskInfo.xHandle};
|
||||
isrParams isrParams_t34n{
|
||||
.flag = TRIG_FLAG_34N,
|
||||
.ign_stat = &ign_box_sts,
|
||||
.rt_handle_ptr = rt_task_info.xHandle};
|
||||
isrParams isr_params_sp12{
|
||||
.ign_stat = &ignBoxStatus,
|
||||
.rt_handle_ptr = rtTaskInfo.xHandle};
|
||||
isrParams isrParams_sp12{
|
||||
.flag = SPARK_FLAG_12,
|
||||
.ign_stat = &ign_box_sts,
|
||||
.rt_handle_ptr = rt_task_info.xHandle};
|
||||
isrParams isr_params_sp34{
|
||||
.ign_stat = &ignBoxStatus,
|
||||
.rt_handle_ptr = rtTaskInfo.xHandle};
|
||||
isrParams isrParams_sp34{
|
||||
.flag = SPARK_FLAG_34,
|
||||
.ign_stat = &ign_box_sts,
|
||||
.rt_handle_ptr = rt_task_info.xHandle};
|
||||
|
||||
LOG_DEBUG("rtTask HDL Params OK, HDL* [", (uint32_t)rt_task_info.xHandle, "]");
|
||||
LOG_DEBUG("rtTask ISR Params OK, ISR* [", (uint32_t)rt_int.isr_ptr, "]");
|
||||
LOG_DEBUG("rtTask QUE Params OK, QUE* [", (uint32_t)rt_queue, "]");
|
||||
.ign_stat = &ignBoxStatus,
|
||||
.rt_handle_ptr = rtTaskInfo.xHandle};
|
||||
|
||||
// Create esp_timer for microsecond precision timeout
|
||||
esp_timer_handle_t timeout_timer;
|
||||
esp_timer_create_args_t timer_args = {
|
||||
esp_timer_handle_t timeoutTimer;
|
||||
esp_timer_create_args_t timeoutTimerArgs = {
|
||||
.callback = spark_timeout_callback,
|
||||
.arg = (void *)rt_task_info.xHandle,
|
||||
.arg = (void *)rtTaskInfo.xHandle,
|
||||
.dispatch_method = ESP_TIMER_TASK,
|
||||
.name = "spark_timeout"};
|
||||
esp_timer_create(&timer_args, &timeout_timer);
|
||||
if (esp_timer_create(&timeoutTimerArgs, &timeoutTimer) != ESP_OK)
|
||||
{
|
||||
LOG_INFO("rtTask [", params->name.c_str(), "] Fail to allocate timeoutTimer");
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
// Attach Pin Interrupts
|
||||
attachInterruptArg(digitalPinToInterrupt(rt_int.trig_pin_12p), rt_int.isr_ptr, (void *)&isr_params_t12p, RISING);
|
||||
attachInterruptArg(digitalPinToInterrupt(rt_int.trig_pin_12n), rt_int.isr_ptr, (void *)&isr_params_t12n, RISING);
|
||||
attachInterruptArg(digitalPinToInterrupt(rt_int.trig_pin_34p), rt_int.isr_ptr, (void *)&isr_params_t34p, RISING);
|
||||
attachInterruptArg(digitalPinToInterrupt(rt_int.trig_pin_34n), rt_int.isr_ptr, (void *)&isr_params_t34n, RISING);
|
||||
attachInterruptArg(digitalPinToInterrupt(rt_int.spark_pin_12), rt_int.isr_ptr, (void *)&isr_params_sp12, RISING);
|
||||
attachInterruptArg(digitalPinToInterrupt(rt_int.spark_pin_34), rt_int.isr_ptr, (void *)&isr_params_sp34, RISING);
|
||||
attachInterruptArg(digitalPinToInterrupt(rtInterrupts.trigPin_12p), rtInterrupts.isrPtr, (void *)&isrParams_t12p, RISING);
|
||||
attachInterruptArg(digitalPinToInterrupt(rtInterrupts.trigPin_12n), rtInterrupts.isrPtr, (void *)&isrParams_t12n, RISING);
|
||||
attachInterruptArg(digitalPinToInterrupt(rtInterrupts.trigPin_34p), rtInterrupts.isrPtr, (void *)&isrParams_t34p, RISING);
|
||||
attachInterruptArg(digitalPinToInterrupt(rtInterrupts.trigPin_34n), rtInterrupts.isrPtr, (void *)&isrParams_t34n, RISING);
|
||||
attachInterruptArg(digitalPinToInterrupt(rtInterrupts.sparkPin_12), rtInterrupts.isrPtr, (void *)&isrParams_sp12, RISING);
|
||||
attachInterruptArg(digitalPinToInterrupt(rtInterrupts.sparkPin_34), rtInterrupts.isrPtr, (void *)&isrParams_sp34, RISING);
|
||||
|
||||
LOG_INFO("rtTask ISR Attach OK [", rt_task_name, "]");
|
||||
LOG_INFO("rtTask ISR Attach OK [", params->name.c_str(), "]");
|
||||
|
||||
// Global rt_task_ptr variables
|
||||
bool first_cycle = true;
|
||||
bool firstCycle = true;
|
||||
bool cycle12 = false;
|
||||
bool cycle34 = false;
|
||||
int64_t last_cycle_time = 0;
|
||||
uint32_t n_errors = 0;
|
||||
int64_t lastCycleTime = 0;
|
||||
uint32_t nErrors = 0;
|
||||
|
||||
while (params->rt_running)
|
||||
{
|
||||
uint32_t pickup_flag = 0;
|
||||
uint32_t spark_flag = 0;
|
||||
uint32_t pickupFlag = 0;
|
||||
uint32_t sparkFlag = 0;
|
||||
|
||||
// WAIT FOR PICKUP SIGNAL
|
||||
xTaskNotifyWait(
|
||||
0x00, // non pulire all'ingresso
|
||||
ULONG_MAX, // pulisci i primi 8 bit
|
||||
&pickup_flag, // valore ricevuto
|
||||
&pickupFlag, // 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
|
||||
if (firstCycle && pickupFlag != TRIG_FLAG_12P) // skip first cycle because of possible initial noise on pickup signals at startu
|
||||
continue;
|
||||
|
||||
// Start microsecond precision timeout timer
|
||||
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_stop(timeoutTimer); // stop timer in case it was running from previous cycle
|
||||
esp_timer_start_once(timeoutTimer, c_sparkTimeoutMax);
|
||||
|
||||
// WAIT FOR SPARK TO HAPPEN OR TIMEOUT
|
||||
xTaskNotifyWait(
|
||||
0x00, // non pulire all'ingresso
|
||||
ULONG_MAX, // pulisci i primi 8 bit
|
||||
&spark_flag, // valore ricevuto
|
||||
&sparkFlag, // valore ricevuto
|
||||
portMAX_DELAY); // wait indefinitely, timeout handled by esp_timer
|
||||
|
||||
// Handle timeout or spark event
|
||||
if (spark_flag != SPARK_FLAG_TIMEOUT)
|
||||
esp_timer_stop(timeout_timer);
|
||||
if (sparkFlag != SPARK_FLAG_TIMEOUT)
|
||||
esp_timer_stop(timeoutTimer);
|
||||
|
||||
// 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_TIMEOUT))
|
||||
if ((pickupFlag == TRIG_FLAG_12P || pickupFlag == TRIG_FLAG_12N) && (sparkFlag != SPARK_FLAG_12 && sparkFlag != SPARK_FLAG_TIMEOUT))
|
||||
{
|
||||
ign_box_sts.coils12.spark_status = ign_box_sts.coils34.spark_status = sparkStatus::SPARK_SYNC_FAIL;
|
||||
ignBoxStatus.coils12.sparkStatus = ignBoxStatus.coils34.sparkStatus = sparkStatusEnum::SPARK_SYNC_FAIL;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Select coil status reference based on pickup_flag
|
||||
// Select coil status reference based on pickupFlag
|
||||
coilsStatus *coils;
|
||||
switch (pickup_flag)
|
||||
switch (pickupFlag)
|
||||
{
|
||||
case TRIG_FLAG_12P:
|
||||
{
|
||||
first_cycle = false;
|
||||
firstCycle = 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 = (int32_t)(60.0f / (cycle_time / 1000000.0f));
|
||||
auto currentTime = esp_timer_get_time();
|
||||
auto cycleTime = currentTime - lastCycleTime;
|
||||
lastCycleTime = currentTime;
|
||||
ignBoxStatus.engRpm = (int32_t)(60.0f / (cycleTime / 1000000.0f));
|
||||
}
|
||||
case TRIG_FLAG_12N:
|
||||
coils = &ign_box_sts.coils12;
|
||||
coils = &ignBoxStatus.coils12;
|
||||
break;
|
||||
case TRIG_FLAG_34P:
|
||||
case TRIG_FLAG_34N:
|
||||
coils = &ign_box_sts.coils34;
|
||||
coils = &ignBoxStatus.coils34;
|
||||
break;
|
||||
}
|
||||
|
||||
// Select logic based on pickup and spark flags
|
||||
switch (pickup_flag)
|
||||
switch (pickupFlag)
|
||||
{
|
||||
case TRIG_FLAG_12P:
|
||||
case TRIG_FLAG_34P:
|
||||
{
|
||||
// Timeout not occourred, expected POSITIVE edge spark OCCOURRED
|
||||
if (spark_flag != SPARK_FLAG_TIMEOUT)
|
||||
if (sparkFlag != SPARK_FLAG_TIMEOUT)
|
||||
{
|
||||
coils->spark_delay = (int32_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
|
||||
coils->sparkDelay = (int32_t)(coils->sparkTime - coils->coilTime);
|
||||
coils->softStartStatus = softStartStatusEnum::NORMAL; // because spark on positive edge
|
||||
coils->sparkStatus = sparkStatusEnum::SPARK_POS_OK; // do not wait for spark on negative edge
|
||||
}
|
||||
// Timeout occourred, expected POSITIVE edge spark NOT OCCOURRED
|
||||
else if (spark_flag == SPARK_FLAG_TIMEOUT)
|
||||
else if (sparkFlag == SPARK_FLAG_TIMEOUT)
|
||||
{
|
||||
coils->spark_status = sparkStatus::SPARK_NEG_WAIT;
|
||||
coils->sstart_status = softStartStatus::NORMAL;
|
||||
coils->sparkStatus = sparkStatusEnum::SPARK_NEG_WAIT;
|
||||
coils->softStartStatus = softStartStatusEnum::NORMAL;
|
||||
}
|
||||
continue; // Do nothing more on positive pulse
|
||||
}
|
||||
@@ -189,29 +194,29 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
|
||||
case TRIG_FLAG_12N:
|
||||
case TRIG_FLAG_34N:
|
||||
{
|
||||
const bool expected_negative = coils->spark_status == sparkStatus::SPARK_NEG_WAIT;
|
||||
const bool negativeSparkExpected = coils->sparkStatus == sparkStatusEnum::SPARK_NEG_WAIT;
|
||||
// Timeout not occourred, expected NEGATIVE edge spark OCCOURRED
|
||||
if (spark_flag != SPARK_FLAG_TIMEOUT && expected_negative)
|
||||
if (sparkFlag != SPARK_FLAG_TIMEOUT && negativeSparkExpected)
|
||||
{
|
||||
coils->spark_delay = (int32_t)(coils->spark_time - coils->trig_time);
|
||||
coils->sstart_status = softStartStatus::SOFT_START;
|
||||
coils->spark_status = sparkStatus::SPARK_NEG_OK;
|
||||
coils->sparkDelay = (int32_t)(coils->sparkTime - coils->coilTime);
|
||||
coils->softStartStatus = softStartStatusEnum::SOFT_START;
|
||||
coils->sparkStatus = sparkStatusEnum::SPARK_NEG_OK;
|
||||
}
|
||||
// Timeout occourred, expected POSITIVE edge spark NOT OCCOURRED
|
||||
else if (spark_flag == SPARK_FLAG_TIMEOUT && expected_negative)
|
||||
else if (sparkFlag == SPARK_FLAG_TIMEOUT && negativeSparkExpected)
|
||||
{
|
||||
coils->sstart_status = softStartStatus::ERROR;
|
||||
coils->spark_status = sparkStatus::SPARK_NEG_FAIL;
|
||||
coils->softStartStatus = softStartStatusEnum::ERROR;
|
||||
coils->sparkStatus = sparkStatusEnum::SPARK_NEG_FAIL;
|
||||
}
|
||||
// Timeout not occouured, unexpected negative edge spark
|
||||
else if (spark_flag != SPARK_FLAG_TIMEOUT && !expected_negative)
|
||||
else if (sparkFlag != SPARK_FLAG_TIMEOUT && !negativeSparkExpected)
|
||||
{
|
||||
coils->sstart_status = softStartStatus::SOFT_START;
|
||||
coils->spark_status = sparkStatus::SPARK_NEG_UNEXPECTED;
|
||||
coils->softStartStatus = softStartStatusEnum::SOFT_START;
|
||||
coils->sparkStatus = sparkStatusEnum::SPARK_NEG_UNEXPECTED;
|
||||
}
|
||||
// Wait for finish of negative pulse to save data to buffer
|
||||
coils->n_events++;
|
||||
if (pickup_flag == TRIG_FLAG_12N)
|
||||
coils->nEvents++;
|
||||
if (pickupFlag == TRIG_FLAG_12N)
|
||||
cycle12 = true;
|
||||
else
|
||||
cycle34 = true;
|
||||
@@ -223,167 +228,211 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
|
||||
|
||||
if (cycle12 && cycle34) // wait for both 12 and 34 cycles to complete before sending data to main loop and resetting peak detectors
|
||||
{
|
||||
// disable interrupts during adc samples
|
||||
disableInterrupt(digitalPinToInterrupt(rtInterrupts.trigPin_12p));
|
||||
disableInterrupt(digitalPinToInterrupt(rtInterrupts.trigPin_12n));
|
||||
disableInterrupt(digitalPinToInterrupt(rtInterrupts.trigPin_34p));
|
||||
disableInterrupt(digitalPinToInterrupt(rtInterrupts.trigPin_34n));
|
||||
disableInterrupt(digitalPinToInterrupt(rtInterrupts.sparkPin_12));
|
||||
disableInterrupt(digitalPinToInterrupt(rtInterrupts.sparkPin_34));
|
||||
|
||||
// reset coils 12 and 34 cycles
|
||||
cycle12 = 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++;
|
||||
if (ignBoxStatus.coils12.sparkStatus == sparkStatusEnum::SPARK_POS_FAIL || ignBoxStatus.coils12.sparkStatus == sparkStatusEnum::SPARK_NEG_FAIL)
|
||||
ignBoxStatus.coils12.nMissedFiring++;
|
||||
if (ignBoxStatus.coils34.sparkStatus == sparkStatusEnum::SPARK_POS_FAIL || ignBoxStatus.coils34.sparkStatus == sparkStatusEnum::SPARK_NEG_FAIL)
|
||||
ignBoxStatus.coils34.nMissedFiring++;
|
||||
|
||||
// read adc channels: pickup12, out12 [ pos + neg ]
|
||||
if (adc) // read only if adc initialized
|
||||
{
|
||||
uint32_t start_adc_read = esp_timer_get_time();
|
||||
std::lock_guard<std::mutex> lock(spi_mutex);
|
||||
uint32_t startAdcReadTime = 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);
|
||||
ign_box_sts.coils34.peak_p_in = adcReadChannel(adc, ADC_CH_PEAK_34P_IN);
|
||||
ign_box_sts.coils34.peak_n_in = adcReadChannel(adc, ADC_CH_PEAK_34N_IN);
|
||||
ign_box_sts.coils12.peak_p_out = adcReadChannel(adc, ADC_CH_PEAK_12P_OUT);
|
||||
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 = (int32_t)(esp_timer_get_time() - start_adc_read);
|
||||
ignBoxStatus.coils12.peakPos = adc->convertToVoltage(adc->cycleSingle());
|
||||
ignBoxStatus.coils12.peakNeg = adc->convertToVoltage(adc->cycleSingle());
|
||||
ignBoxStatus.coils34.peakPos = adc->convertToVoltage(adc->cycleSingle());
|
||||
ignBoxStatus.coils34.peakNeg = adc->convertToVoltage(adc->cycleSingle());
|
||||
ignBoxStatus.coils12.trigLevelPos = adc->convertToVoltage(adc->cycleSingle());
|
||||
ignBoxStatus.coils12.trigLevelNeg = adc->convertToVoltage(adc->cycleSingle());
|
||||
ignBoxStatus.coils34.trigLevelPos = adc->convertToVoltage(adc->cycleSingle());
|
||||
ignBoxStatus.coils34.trigLevelNeg = adc->convertToVoltage(adc->cycleSingle());
|
||||
adc->stopConversion();
|
||||
ignBoxStatus.adcReadTime = (int32_t)(esp_timer_get_time() - startAdcReadTime);
|
||||
}
|
||||
else // simulate adc read timig
|
||||
vTaskDelay(pdMS_TO_TICKS(c_adc_time));
|
||||
vTaskDelay(pdMS_TO_TICKS(c_adcTime));
|
||||
|
||||
// reset peak detectors + sample and hold
|
||||
// outputs on io expander
|
||||
if (io)
|
||||
{
|
||||
// [TODO] code to reset sample and hold and arm trigger level detectors
|
||||
uint32_t startIoReadWriteTime = esp_timer_get_time();
|
||||
// Discharge Pulse
|
||||
io->extDigitalWrite(rtResets.sh_disch_12, true);
|
||||
io->extDigitalWrite(rtResets.sh_disch_34, true);
|
||||
delayMicroseconds(250);
|
||||
io->extDigitalWrite(rtResets.sh_disch_12, false);
|
||||
io->extDigitalWrite(rtResets.sh_disch_34, false);
|
||||
// Safety delay
|
||||
delayMicroseconds(500);
|
||||
// Re-Arm Pulse
|
||||
io->extDigitalWrite(rtResets.sh_arm_12, true);
|
||||
io->extDigitalWrite(rtResets.sh_arm_34, true);
|
||||
delayMicroseconds(250);
|
||||
io->extDigitalWrite(rtResets.sh_arm_12, false);
|
||||
io->extDigitalWrite(rtResets.sh_arm_34, false);
|
||||
ignBoxStatus.ioReadWriteTime = (int32_t)(esp_timer_get_time() - startIoReadWriteTime);
|
||||
}
|
||||
else
|
||||
vTaskDelay(pdMS_TO_TICKS(1));
|
||||
vTaskDelay(pdMS_TO_TICKS(c_ioTime));
|
||||
|
||||
// send essage to main loop with ignition info, by copy so local static variable is ok
|
||||
if (rt_queue)
|
||||
if (rtQueue)
|
||||
{
|
||||
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 = ++n_errors;
|
||||
ignBoxStatus.timestamp = esp_timer_get_time(); // update data timestamp
|
||||
if (xQueueSendToBack(rtQueue, (void *)&ignBoxStatus, 0) != pdPASS)
|
||||
ignBoxStatus.nQueueErrors = ++nErrors;
|
||||
}
|
||||
|
||||
// enable interrupts ready for a new cycle
|
||||
enableInterrupt(digitalPinToInterrupt(rtInterrupts.trigPin_12p));
|
||||
enableInterrupt(digitalPinToInterrupt(rtInterrupts.trigPin_12n));
|
||||
enableInterrupt(digitalPinToInterrupt(rtInterrupts.trigPin_34p));
|
||||
enableInterrupt(digitalPinToInterrupt(rtInterrupts.trigPin_34n));
|
||||
enableInterrupt(digitalPinToInterrupt(rtInterrupts.sparkPin_12));
|
||||
enableInterrupt(digitalPinToInterrupt(rtInterrupts.sparkPin_34));
|
||||
}
|
||||
}
|
||||
// Delete the timeout timer
|
||||
esp_timer_delete(timeout_timer);
|
||||
LOG_WARN("rtTask Ending [", rt_task_name, "]");
|
||||
esp_timer_stop(timeoutTimer);
|
||||
esp_timer_delete(timeoutTimer);
|
||||
LOG_WARN("rtTask Ending [", params->name.c_str(), "]");
|
||||
// Ignition A Interrupts DETACH
|
||||
detachInterrupt(rt_int.trig_pin_12p);
|
||||
detachInterrupt(rt_int.trig_pin_12n);
|
||||
detachInterrupt(rt_int.trig_pin_34p);
|
||||
detachInterrupt(rt_int.trig_pin_34n);
|
||||
detachInterrupt(rt_int.spark_pin_12);
|
||||
detachInterrupt(rt_int.spark_pin_34);
|
||||
detachInterrupt(rtInterrupts.trigPin_12p);
|
||||
detachInterrupt(rtInterrupts.trigPin_12n);
|
||||
detachInterrupt(rtInterrupts.trigPin_34p);
|
||||
detachInterrupt(rtInterrupts.trigPin_34n);
|
||||
detachInterrupt(rtInterrupts.sparkPin_12);
|
||||
detachInterrupt(rtInterrupts.sparkPin_34);
|
||||
// delete present task
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
///////////// CLASS MEMBER DEFINITIONS /////////////
|
||||
rtIgnitionTask::rtIgnitionTask(const rtTaskParams params, const uint32_t history_size, const uint32_t queue_size, const uint8_t core, std::mutex &fs_mutex, fs::FS &filesystem) : m_params(params), m_filesystem(filesystem), m_fs_mutex(fs_mutex), m_core(core), m_max_history(history_size)
|
||||
rtIgnitionTask::rtIgnitionTask(const rtTaskParams params, const uint32_t history_size, const uint32_t queue_size, const uint8_t core, std::mutex &fs_mutex, fs::FS &filesystem) : m_params(params), m_filesystem(filesystem), m_filesystemMutex(fs_mutex), m_core(core), m_historyMax(history_size)
|
||||
{
|
||||
LOG_WARN("Starting Manager for [", m_params.name.c_str(), "]");
|
||||
// create queue buffers
|
||||
m_queue = xQueueCreate(queue_size, sizeof(ignitionBoxStatus));
|
||||
if (!m_queue)
|
||||
m_rtQueueHandle = xQueueCreate(queue_size, sizeof(ignitionBoxStatus));
|
||||
if (!m_rtQueueHandle)
|
||||
{
|
||||
LOG_ERROR("Unable To Create Task [", params.name.c_str(), "] queues");
|
||||
m_manager_status = rtTaskStatus::ERROR;
|
||||
m_managerStatus = rtTaskStatus::ERROR;
|
||||
return;
|
||||
}
|
||||
else
|
||||
m_params.rt_queue = m_queue;
|
||||
m_params.rt_queue = m_rtQueueHandle;
|
||||
|
||||
// create PSram history vectors
|
||||
m_history_0 = PSHistory(history_size);
|
||||
m_history_1 = PSHistory(history_size);
|
||||
// assing active and writable history
|
||||
m_active_history = std::unique_ptr<PSHistory>(&m_history_0);
|
||||
m_save_history = std::unique_ptr<PSHistory>(&m_history_1);
|
||||
try
|
||||
{
|
||||
// create PSram history vectors
|
||||
m_historyBuf0 = PSHistory(history_size);
|
||||
m_historyBuf1 = PSHistory(history_size);
|
||||
// assing active and writable history
|
||||
m_historyActive = std::unique_ptr<PSHistory>(&m_historyBuf0);
|
||||
m_historyInactive = std::unique_ptr<PSHistory>(&m_historyBuf1);
|
||||
}
|
||||
catch (std::bad_alloc &e)
|
||||
{
|
||||
LOG_ERROR("Task [", params.name.c_str(), "] Unable to allocate history PSRAM: ", e.what());
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_WARN("Starting Manager for [", m_params.name.c_str(), "]");
|
||||
// auto task_success = pdPASS;
|
||||
m_managerTaskName = (std::string("man_") + m_params.name).c_str();
|
||||
auto task_success = xTaskCreatePinnedToCore(
|
||||
rtIgnitionTask_manager,
|
||||
(std::string("man_") + m_params.name).c_str(),
|
||||
8192,
|
||||
m_managerTaskName.c_str(),
|
||||
RT_TASK_STACK,
|
||||
(void *)this,
|
||||
m_params.rt_priority >> 2,
|
||||
&m_manager_handle,
|
||||
&m_managerHandle,
|
||||
m_core);
|
||||
|
||||
if (task_success != pdPASS)
|
||||
{
|
||||
LOG_ERROR("Unable To Create Manager for [", params.name.c_str(), "]");
|
||||
m_manager_status = rtTaskStatus::ERROR;
|
||||
m_managerStatus = rtTaskStatus::ERROR;
|
||||
return;
|
||||
}
|
||||
|
||||
// average every 10 samples
|
||||
m_info_filtered = ignitionBoxStatusFiltered(10);
|
||||
m_last_data = millis();
|
||||
m_manager_status = rtTaskStatus::OK;
|
||||
m_statusFiltered = ignitionBoxStatusFiltered(m_filterSize);
|
||||
m_dataLast = millis();
|
||||
m_managerStatus = rtTaskStatus::OK;
|
||||
}
|
||||
|
||||
rtIgnitionTask::~rtIgnitionTask()
|
||||
{
|
||||
if (m_rt_handle)
|
||||
vTaskDelete(m_rt_handle);
|
||||
if (m_manager_handle)
|
||||
vTaskDelete(m_manager_handle);
|
||||
if (m_queue)
|
||||
vQueueDelete(m_queue);
|
||||
if (m_rtHandle)
|
||||
vTaskDelete(m_rtHandle);
|
||||
if (m_managerHandle)
|
||||
vTaskDelete(m_managerHandle);
|
||||
if (m_rtQueueHandle)
|
||||
vQueueDelete(m_rtQueueHandle);
|
||||
}
|
||||
|
||||
void rtIgnitionTask::run()
|
||||
{
|
||||
// receive new data from the queue
|
||||
auto new_data = xQueueReceive(m_queue, &m_last_status, 0); // non blocking receive
|
||||
auto new_data = xQueueReceive(m_rtQueueHandle, &m_statusLast, 0); // non blocking receive
|
||||
|
||||
if (new_data == pdPASS)
|
||||
{
|
||||
m_last_data = millis();
|
||||
m_manager_status = rtTaskStatus::RUNNING;
|
||||
m_dataLast = millis();
|
||||
m_managerStatus = rtTaskStatus::RUNNING;
|
||||
// if history buffer is full swap buffers and if enabled save history buffer
|
||||
if (m_counter_status >= m_active_history->size())
|
||||
if (m_statusCounter >= m_historyMax)
|
||||
{
|
||||
LOG_DEBUG("Save for Buffer Full: ", m_counter_status);
|
||||
m_counter_status = 0;
|
||||
m_partial_save = false; // reset partial save flag on new data cycle
|
||||
std::swap(m_active_history, m_save_history);
|
||||
if (m_enable_save)
|
||||
saveHistory(*m_save_history, m_history_path); // directly call the save task function to save without delay
|
||||
LOG_DEBUG("Save for Buffer Full: ", m_statusCounter);
|
||||
m_statusCounter = 0;
|
||||
m_savePartial = false; // reset partial save flag on new data cycle
|
||||
std::swap(m_historyActive, m_historyInactive);
|
||||
if (m_historySaveEnable)
|
||||
saveHistory(*m_historyInactive, m_historyPath); // directly call the save task function to save without delay
|
||||
LOG_INFO("Save History");
|
||||
}
|
||||
|
||||
// update filtered data
|
||||
m_info_filtered.update(m_last_status);
|
||||
(*m_active_history)[m_counter_status] = m_last_status;
|
||||
m_statusFiltered.update(m_statusLast);
|
||||
(*m_historyActive)[m_statusCounter] = m_statusLast;
|
||||
|
||||
if (m_on_message_cb && m_counter_status % 10 == 0)
|
||||
// callback
|
||||
if (m_onFilteredStatusUpdate && m_statusCounter % m_filterSize == 0)
|
||||
{
|
||||
m_on_message_cb(m_info_filtered);
|
||||
m_onFilteredStatusUpdate(m_statusFiltered);
|
||||
}
|
||||
|
||||
// update data counter
|
||||
m_counter_status++;
|
||||
m_statusCounter++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (millis() - m_last_data > c_idle_time)
|
||||
if (millis() - m_dataLast > c_idleTime)
|
||||
{
|
||||
if (m_counter_status > 0 && !m_partial_save)
|
||||
if (m_statusCounter > 0 && !m_savePartial)
|
||||
{
|
||||
LOG_DEBUG("Save Partial: ", m_counter_status);
|
||||
m_active_history->resize(m_counter_status);
|
||||
saveHistory(*m_active_history, m_history_path);
|
||||
m_active_history->resize(m_max_history);
|
||||
m_counter_status = 0;
|
||||
m_partial_save = true;
|
||||
LOG_DEBUG("Save Partial: ", m_statusCounter);
|
||||
m_historyActive->resize(m_statusCounter);
|
||||
saveHistory(*m_historyActive, m_historyPath);
|
||||
m_historyActive->resize(m_historyMax);
|
||||
m_statusCounter = 0;
|
||||
m_savePartial = true;
|
||||
}
|
||||
m_manager_status = rtTaskStatus::IDLE;
|
||||
m_managerStatus = rtTaskStatus::IDLE;
|
||||
}
|
||||
delay(5); // yeld to another task
|
||||
}
|
||||
}
|
||||
|
||||
@@ -396,22 +445,22 @@ const bool rtIgnitionTask::start()
|
||||
m_params.rt_stack_size,
|
||||
(void *)&m_params,
|
||||
m_params.rt_priority,
|
||||
&m_rt_handle,
|
||||
&m_rtHandle,
|
||||
m_core);
|
||||
const bool success = task_success == pdPASS && m_rt_handle != nullptr;
|
||||
const bool success = task_success == pdPASS && m_rtHandle != nullptr;
|
||||
if (success)
|
||||
m_manager_status = rtTaskStatus::IDLE;
|
||||
m_managerStatus = rtTaskStatus::IDLE;
|
||||
return success;
|
||||
}
|
||||
|
||||
const bool rtIgnitionTask::stop()
|
||||
{
|
||||
LOG_WARN("Ending Task [", m_params.name.c_str(), "]");
|
||||
if (m_rt_handle)
|
||||
if (m_rtHandle)
|
||||
{
|
||||
m_params.rt_running = false;
|
||||
m_rt_handle = nullptr;
|
||||
m_manager_status = rtTaskStatus::STOPPED;
|
||||
m_rtHandle = nullptr;
|
||||
m_managerStatus = rtTaskStatus::STOPPED;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -419,26 +468,26 @@ const bool rtIgnitionTask::stop()
|
||||
|
||||
const ignitionBoxStatus rtIgnitionTask::getLast() const
|
||||
{
|
||||
return m_last_status;
|
||||
return m_statusLast;
|
||||
}
|
||||
|
||||
const ignitionBoxStatusFiltered rtIgnitionTask::getFiltered() const
|
||||
{
|
||||
return m_info_filtered;
|
||||
return m_statusFiltered;
|
||||
}
|
||||
|
||||
const rtIgnitionTask::rtTaskStatus rtIgnitionTask::getStatus() const
|
||||
{
|
||||
return m_manager_status;
|
||||
return m_managerStatus;
|
||||
}
|
||||
|
||||
void rtIgnitionTask::enableSave(const bool enable, const std::filesystem::path filename)
|
||||
{
|
||||
m_enable_save = enable;
|
||||
m_historySaveEnable = enable;
|
||||
if (enable && !filename.empty())
|
||||
{
|
||||
LOG_WARN("Save History Enabled Task [", m_params.name.c_str(), "]");
|
||||
m_history_path = m_filesystem.mountpoint() / filename;
|
||||
m_historyPath = m_filesystem.mountpoint() / filename;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -448,13 +497,13 @@ void rtIgnitionTask::enableSave(const bool enable, const std::filesystem::path f
|
||||
|
||||
void rtIgnitionTask::onMessage(std::function<void(ignitionBoxStatusFiltered)> callaback)
|
||||
{
|
||||
m_on_message_cb = callaback;
|
||||
m_onFilteredStatusUpdate = callaback;
|
||||
}
|
||||
|
||||
void rtIgnitionTask::saveHistory(const rtIgnitionTask::PSHistory &history, const std::filesystem::path &file_name)
|
||||
void rtIgnitionTask::saveHistory(const rtIgnitionTask::PSHistory &history, const std::filesystem::path &fileName)
|
||||
{
|
||||
// Lock filesystem mutex to avoid concurrent access
|
||||
std::lock_guard<std::mutex> fs_lock(m_fs_mutex);
|
||||
std::lock_guard<std::mutex> fs_lock(m_filesystemMutex);
|
||||
|
||||
// Check for free space
|
||||
if (LittleFS.totalBytes() - LittleFS.usedBytes() < history.size() * sizeof(ignitionBoxStatus)) // check if at least 1MB is free for saving history
|
||||
@@ -464,26 +513,26 @@ void rtIgnitionTask::saveHistory(const rtIgnitionTask::PSHistory &history, const
|
||||
}
|
||||
|
||||
// create complete file path
|
||||
const std::filesystem::path mount_point = std::filesystem::path(m_filesystem.mountpoint());
|
||||
std::filesystem::path file_path = file_name;
|
||||
if (file_name.root_path() != mount_point)
|
||||
file_path = mount_point / file_name;
|
||||
const std::filesystem::path mountPoint = std::filesystem::path(m_filesystem.mountpoint());
|
||||
std::filesystem::path filePath = fileName;
|
||||
if (fileName.root_path() != mountPoint)
|
||||
filePath = mountPoint / fileName;
|
||||
|
||||
// if firt save remove old file and create new
|
||||
auto save_flags = std::ios::out;
|
||||
if (m_first_save)
|
||||
auto saveFlags = std::ios::out;
|
||||
if (m_saveFirst)
|
||||
{
|
||||
save_flags |= std::ios::trunc; // overwrite existing file
|
||||
m_filesystem.remove(file_path.c_str()); // ensure file is removed before saving to avoid issues with appending to existing file in SPIFFS
|
||||
LOG_INFO("Saving history to Flash, new file:", file_path.c_str());
|
||||
saveFlags |= std::ios::trunc; // overwrite existing file
|
||||
m_filesystem.remove(filePath.c_str()); // ensure file is removed before saving to avoid issues with appending to existing file in SPIFFS
|
||||
LOG_INFO("Saving history to Flash, new file:", filePath.c_str());
|
||||
}
|
||||
else // else append to existing file
|
||||
{
|
||||
save_flags |= std::ios::app; // append to new file
|
||||
LOG_INFO("Saving history to Flash, appending to existing file:", file_path.c_str());
|
||||
saveFlags |= std::ios::app; // append to new file
|
||||
LOG_INFO("Saving history to Flash, appending to existing file:", filePath.c_str());
|
||||
}
|
||||
|
||||
std::ofstream ofs(file_path, save_flags);
|
||||
std::ofstream ofs(filePath, saveFlags);
|
||||
if (ofs.fail())
|
||||
{
|
||||
LOG_ERROR("Failed to open file for writing");
|
||||
@@ -491,38 +540,38 @@ void rtIgnitionTask::saveHistory(const rtIgnitionTask::PSHistory &history, const
|
||||
}
|
||||
|
||||
// write csv header
|
||||
if (m_first_save)
|
||||
if (m_saveFirst)
|
||||
{
|
||||
ofs << "TS,EVENTS_12,DLY_12,STAT_12,V_12_1,V_12_2,V_12_3,V_12_4,IGNITION_MODE_12,"
|
||||
<< "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();
|
||||
m_first_save = false;
|
||||
m_saveFirst = false;
|
||||
}
|
||||
|
||||
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);
|
||||
<< std::to_string(entry.coils12.nEvents) << ","
|
||||
<< std::to_string(entry.coils12.sparkDelay) << ","
|
||||
<< std::string(sparkStatusNames.at(entry.coils12.sparkStatus)) << ","
|
||||
<< std::to_string(entry.coils12.peakPos) << ","
|
||||
<< std::to_string(entry.coils12.peakNeg) << ","
|
||||
<< std::to_string(entry.coils12.trigLevelPos) << ","
|
||||
<< std::to_string(entry.coils12.trigLevelNeg) << ","
|
||||
<< std::string(softStartStatusNames.at(entry.coils12.softStartStatus)) << ","
|
||||
<< std::to_string(entry.coils34.nEvents) << ","
|
||||
<< std::to_string(entry.coils34.sparkDelay) << ","
|
||||
<< std::string(sparkStatusNames.at(entry.coils34.sparkStatus)) << ","
|
||||
<< std::to_string(entry.coils34.peakPos) << ","
|
||||
<< std::to_string(entry.coils34.peakNeg) << ","
|
||||
<< std::to_string(entry.coils34.trigLevelPos) << ","
|
||||
<< std::to_string(entry.coils34.trigLevelNeg) << ","
|
||||
<< std::string(softStartStatusNames.at(entry.coils34.softStartStatus)) << ","
|
||||
<< std::to_string(entry.engRpm) << ","
|
||||
<< std::to_string(entry.adcReadTime) << ","
|
||||
<< std::to_string(entry.nQueueErrors);
|
||||
ofs << std::endl;
|
||||
ofs.flush();
|
||||
}
|
||||
|
||||
@@ -22,9 +22,6 @@
|
||||
// DEVICES
|
||||
#include "devices.h"
|
||||
|
||||
// Global Variables and Flags
|
||||
const uint32_t spark_timeout_max = 500; // in microseconds
|
||||
|
||||
// Debug Variables
|
||||
#ifdef DEBUG
|
||||
static const std::map<const uint32_t, const char *> names = {
|
||||
@@ -46,32 +43,32 @@ public:
|
||||
// RT task Interrupt parameters
|
||||
struct rtTaskInterruptParams
|
||||
{
|
||||
void (*isr_ptr)(void *);
|
||||
const uint8_t trig_pin_12p;
|
||||
const uint8_t trig_pin_12n;
|
||||
const uint8_t trig_pin_34p;
|
||||
const uint8_t trig_pin_34n;
|
||||
const uint8_t spark_pin_12;
|
||||
const uint8_t spark_pin_34;
|
||||
void (*isrPtr)(void *);
|
||||
const uint8_t trigPin_12p;
|
||||
const uint8_t trigPin_12n;
|
||||
const uint8_t trigPin_34p;
|
||||
const uint8_t trigPin_34n;
|
||||
const uint8_t sparkPin_12;
|
||||
const uint8_t sparkPin_34;
|
||||
};
|
||||
|
||||
// RT Task Peak Detector Reset pins
|
||||
struct rtTaskIOParams
|
||||
{
|
||||
const uint32_t expander_addr;
|
||||
const uint8_t pot_cs_12;
|
||||
const uint8_t pot_cs_34;
|
||||
const uint8_t ss_force;
|
||||
const uint8_t ss_inhibit_12;
|
||||
const uint8_t ss_inhibit_34;
|
||||
const uint8_t sh_disch_12;
|
||||
const uint8_t sh_disch_34;
|
||||
const uint8_t sh_arm_12;
|
||||
const uint8_t sh_arm_34;
|
||||
const uint8_t relay_in_12;
|
||||
const uint8_t relay_in_34;
|
||||
const uint8_t relay_out_12;
|
||||
const uint8_t relay_out_34;
|
||||
const uint32_t pot_cs_12;
|
||||
const uint32_t pot_cs_34;
|
||||
const uint32_t ss_force;
|
||||
const uint32_t ss_inhibit_12;
|
||||
const uint32_t ss_inhibit_34;
|
||||
const uint32_t sh_disch_12;
|
||||
const uint32_t sh_disch_34;
|
||||
const uint32_t sh_arm_12;
|
||||
const uint32_t sh_arm_34;
|
||||
const uint32_t relay_in_12;
|
||||
const uint32_t relay_in_34;
|
||||
const uint32_t relay_out_12;
|
||||
const uint32_t relay_out_34;
|
||||
};
|
||||
|
||||
// RT task parameters
|
||||
@@ -84,7 +81,7 @@ public:
|
||||
const rtTaskInterruptParams rt_int; // interrupt pins to attach
|
||||
const rtTaskIOParams rt_io; // reset ping for peak detectors
|
||||
QueueHandle_t rt_queue; // queue for task io
|
||||
const std::shared_ptr<Devices> dev;
|
||||
Devices *dev;
|
||||
};
|
||||
|
||||
enum rtTaskStatus
|
||||
@@ -123,37 +120,39 @@ private: // static functions for FreeRTOS
|
||||
|
||||
private:
|
||||
bool m_running = true;
|
||||
rtTaskStatus m_manager_status = INIT;
|
||||
|
||||
rtTaskStatus m_managerStatus = INIT;
|
||||
rtTaskParams m_params;
|
||||
|
||||
const uint8_t m_core;
|
||||
std::string m_managerTaskName;
|
||||
TaskHandle_t m_rtHandle = nullptr;
|
||||
TaskHandle_t m_managerHandle = nullptr;
|
||||
QueueHandle_t m_rtQueueHandle = nullptr;
|
||||
|
||||
TaskHandle_t m_rt_handle = nullptr;
|
||||
TaskHandle_t m_manager_handle = nullptr;
|
||||
QueueHandle_t m_queue = nullptr;
|
||||
const uint32_t m_historyMax;
|
||||
bool m_historySaveEnable = false;
|
||||
std::filesystem::path m_historyPath;
|
||||
PSHistory m_historyBuf0;
|
||||
PSHistory m_historyBuf1;
|
||||
std::unique_ptr<PSHistory> m_historyActive;
|
||||
std::unique_ptr<PSHistory> m_historyInactive;
|
||||
|
||||
bool m_enable_save = false;
|
||||
std::filesystem::path m_history_path;
|
||||
const uint32_t m_max_history;
|
||||
PSHistory m_history_0;
|
||||
PSHistory m_history_1;
|
||||
std::unique_ptr<PSHistory> m_active_history;
|
||||
std::unique_ptr<PSHistory> m_save_history;
|
||||
bool m_savePartial = false;
|
||||
bool m_saveFirst = true;
|
||||
fs::FS &m_filesystem;
|
||||
std::mutex &m_fs_mutex;
|
||||
std::mutex &m_filesystemMutex;
|
||||
|
||||
bool m_partial_save = false;
|
||||
bool m_first_save = true;
|
||||
uint8_t m_filterSize = 10;
|
||||
uint32_t m_statusCounter = 0;
|
||||
uint32_t m_dataLast = 0;
|
||||
ignitionBoxStatus m_statusLast;
|
||||
ignitionBoxStatusFiltered m_statusFiltered;
|
||||
|
||||
uint32_t m_counter_status = 0;
|
||||
uint32_t m_last_data = 0;
|
||||
ignitionBoxStatus m_last_status;
|
||||
ignitionBoxStatusFiltered m_info_filtered;
|
||||
std::function<void(ignitionBoxStatusFiltered)> m_onFilteredStatusUpdate = nullptr;
|
||||
|
||||
std::function<void(ignitionBoxStatusFiltered)> m_on_message_cb = nullptr;
|
||||
|
||||
static const uint32_t c_idle_time = 10000; // in mS
|
||||
static const uint32_t c_spark_timeout_max = 500; // uS
|
||||
static const uint8_t c_adc_time = 4; // in mS
|
||||
static const uint8_t c_io_time = 2; // in mS
|
||||
// Global Variables and Flags
|
||||
static const uint32_t c_sparkTimeoutMax = 500; // in microseconds
|
||||
static const uint32_t c_idleTime = 10000; // in mS
|
||||
static const uint8_t c_adcTime = 4; // in mS
|
||||
static const uint8_t c_ioTime = 2; // in mS
|
||||
};
|
||||
|
||||
@@ -44,30 +44,30 @@ void printInfo(const ignitionBoxStatus &info)
|
||||
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));
|
||||
printField("Events", info.coils12.nEvents);
|
||||
printField("Events Missed", info.coils12.nMissedFiring);
|
||||
printField("Spark Dly", (uint32_t)info.coils12.sparkDelay);
|
||||
printField("Spark Sts", sparkStatusNames.at(info.coils12.sparkStatus));
|
||||
printField("Peak P_IN", info.coils12.peakPos);
|
||||
printField("Peak N_IN", info.coils12.peakNeg);
|
||||
printField("Peak P_OUT", info.coils12.trigLevelPos);
|
||||
printField("Peak N_OUT", info.coils12.trigLevelNeg);
|
||||
printField("Soft Start ", softStartStatusNames.at(info.coils12.softStartStatus));
|
||||
|
||||
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));
|
||||
printField("Events", info.coils34.nEvents);
|
||||
printField("Events Missed", info.coils34.nMissedFiring);
|
||||
printField("Spark Dly", (uint32_t)info.coils34.sparkDelay);
|
||||
printField("Spark Sts", sparkStatusNames.at(info.coils34.sparkStatus));
|
||||
printField("Peak P_IN", info.coils34.peakPos);
|
||||
printField("Peak N_IN", info.coils34.peakNeg);
|
||||
printField("Peak P_OUT", info.coils34.trigLevelPos);
|
||||
printField("Peak N_OUT", info.coils34.trigLevelNeg);
|
||||
printField("Soft Start ", softStartStatusNames.at(info.coils34.softStartStatus));
|
||||
|
||||
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);
|
||||
printField("Engine RPM", info.engRpm);
|
||||
printField("ADC Read Time", info.adcReadTime);
|
||||
printField("Queue Errors", info.nQueueErrors);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
#include "esp_heap_caps.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_spi_flash.h"
|
||||
#include "spi_flash_mmap.h"
|
||||
#include "esp_partition.h"
|
||||
#include "LittleFS.h"
|
||||
|
||||
@@ -49,23 +49,27 @@ void printBar(Print &printer, const char *label, size_t used, size_t total, cons
|
||||
{
|
||||
float perc = total > 0 ? ((float)used / total) : 0;
|
||||
int filled = perc * BAR_WIDTH;
|
||||
char str[256] = {0};
|
||||
uint16_t k(0);
|
||||
|
||||
printer.printf("%s%-12s [" COLOR_RESET, color, label);
|
||||
k += sprintf(str, "%s%-12s [" COLOR_RESET, color, label);
|
||||
|
||||
for (int i = 0; i < BAR_WIDTH; i++)
|
||||
{
|
||||
if (i < filled)
|
||||
printer.printf("%s#%s", color, COLOR_RESET);
|
||||
k += sprintf(&str[k], "%s#%s", color, COLOR_RESET);
|
||||
else
|
||||
printer.printf("-");
|
||||
k += sprintf(&str[k], "-");
|
||||
}
|
||||
|
||||
printer.printf("] %s%6.2f%%%s (%5.3f/%5.3f)MB\n",
|
||||
sprintf(&str[k], "] %s%6.2f%%%s (%5.3f/%5.3f)MB",
|
||||
color,
|
||||
perc * 100.0,
|
||||
COLOR_RESET,
|
||||
(used / 1024.0f / 1024.0f),
|
||||
(total / 1024.0f / 1024.0f));
|
||||
|
||||
printer.println(str);
|
||||
}
|
||||
|
||||
void printRunningTasksMod(Print &printer, std::function<bool(const TaskStatus_t &a, const TaskStatus_t &b)> orderBy)
|
||||
@@ -95,18 +99,19 @@ void printRunningTasksMod(Print &printer, std::function<bool(const TaskStatus_t
|
||||
|
||||
// Compute system total runtime
|
||||
ulCurrentRunTime = ulTotalRunTime - ulLastRunTime;
|
||||
ulCurrentRunTime = ulCurrentRunTime > 0 ? ulCurrentRunTime : 1;
|
||||
ulLastRunTime = ulTotalRunTime;
|
||||
|
||||
// PRINT MEMORY INFO
|
||||
printer.printf("\033[H");
|
||||
printer.printf(COLOR_LBLUE "=================== ESP32 SYSTEM MONITOR ===================\n" COLOR_RESET);
|
||||
printer.printf(COLOR_WHITE "====================== ESP32 SYSTEM MONITOR ======================\n" COLOR_RESET);
|
||||
|
||||
std::string buffer;
|
||||
time_t now = time(nullptr);
|
||||
struct tm *t = localtime(&now);
|
||||
buffer.resize(64);
|
||||
strftime(buffer.data(), sizeof(buffer), "%Y-%m-%d %H:%M:%S", t);
|
||||
printer.printf(COLOR_YELLOW "=============== Datetime: %s ===============\n\n" COLOR_RESET, buffer.c_str());
|
||||
printer.printf(COLOR_WHITE "=================== Datetime: %s ==================\n\n" COLOR_RESET, buffer.c_str());
|
||||
|
||||
// ===== HEAP =====
|
||||
size_t freeHeap = esp_get_free_heap_size();
|
||||
@@ -116,7 +121,7 @@ void printRunningTasksMod(Print &printer, std::function<bool(const TaskStatus_t
|
||||
// ===== RAM INTERNA =====
|
||||
size_t freeInternal = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
|
||||
size_t totalInternal = heap_caps_get_total_size(MALLOC_CAP_INTERNAL);
|
||||
printBar(printer, "INTERNAL", totalInternal - freeInternal, totalInternal, COLOR_BLUE);
|
||||
printBar(printer, "INTERNAL", totalInternal - freeInternal, totalInternal, COLOR_CYAN);
|
||||
|
||||
// ===== PSRAM =====
|
||||
size_t totalPsram = heap_caps_get_total_size(MALLOC_CAP_SPIRAM);
|
||||
@@ -134,17 +139,6 @@ void printRunningTasksMod(Print &printer, std::function<bool(const TaskStatus_t
|
||||
ESP_PARTITION_SUBTYPE_APP_FACTORY,
|
||||
NULL);
|
||||
|
||||
if (app_partition)
|
||||
{
|
||||
size_t totalAPP = app_partition->size; // dimensione reale partizione
|
||||
size_t sketchSize = ESP.getSketchSize();
|
||||
printBar(printer, "FLASH APP", sketchSize, totalAPP, COLOR_CYAN);
|
||||
}
|
||||
else
|
||||
{
|
||||
printer.printf(COLOR_YELLOW "%-12s [NOT FOUND]\n" COLOR_RESET, "FLASH APP");
|
||||
}
|
||||
|
||||
// ===== LITTLEFS (corretto con partition table) =====
|
||||
const esp_partition_t *fs_partition =
|
||||
esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
|
||||
@@ -164,13 +158,15 @@ void printRunningTasksMod(Print &printer, std::function<bool(const TaskStatus_t
|
||||
|
||||
// ===== MIN HEAP =====
|
||||
size_t minHeap = esp_get_minimum_free_heap_size();
|
||||
printer.printf("%s\nMin Heap Ever:%s %u KB\n\n", COLOR_RED, COLOR_RESET, minHeap / 1024);
|
||||
printer.printf("%s\nMin Heap Ever:%s %u KB\n", COLOR_RED, COLOR_RESET, minHeap / 1024);
|
||||
size_t max_block = heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM);
|
||||
printer.printf("%sMax PSRAM Block:%s %u KB\n\n", COLOR_RED, COLOR_RESET, max_block / 1024);
|
||||
|
||||
// Print Runtime Information
|
||||
printer.printf("Tasks: %u, Runtime: %lus, Period: %luus\r\n", uxArraySize, ulTotalRunTime / 1000000, ulCurrentRunTime);
|
||||
printer.printf("Tasks: %u, Runtime: %lus, Period: %luus\n", uxArraySize, ulTotalRunTime / 1000000, ulCurrentRunTime);
|
||||
|
||||
// Print Task Headers
|
||||
printer.printf("Num\t Name\tLoad\tPrio\t Free\tCore\tState\r\n");
|
||||
printer.printf("Num\t Name\tLoad\tPrio\t Free\tCore\tState\n");
|
||||
for (const auto &task : pxTaskStatusArray)
|
||||
{
|
||||
|
||||
@@ -183,7 +179,7 @@ void printRunningTasksMod(Print &printer, std::function<bool(const TaskStatus_t
|
||||
"\t%3lu%%"
|
||||
"\t%4u\t%5lu"
|
||||
"\t%4c"
|
||||
"\t%s\r\n",
|
||||
"\t%s\n",
|
||||
task.xTaskNumber, task.pcTaskName,
|
||||
ulTaskRunTime,
|
||||
task.uxCurrentPriority, task.usStackHighWaterMark,
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
#include <webserver.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
static std::map<const std::string, AstroWebServer::c_commandEnum> s_webserverCommands = {
|
||||
{"setTime", AstroWebServer::SET_TIME},
|
||||
};
|
||||
|
||||
void on_ping(TimerHandle_t xTimer)
|
||||
{
|
||||
@@ -14,7 +9,7 @@ void on_ping(TimerHandle_t xTimer)
|
||||
ws->cleanupClients();
|
||||
}
|
||||
|
||||
AstroWebServer::AstroWebServer(const uint8_t port, fs::FS &filesystem) : m_port(port), m_webserver(AsyncWebServer(port)), m_websocket(AsyncWebSocket("/ws")), m_filesystem(filesystem)
|
||||
AstroWebServer::AstroWebServer(const uint8_t port, fs::FS &filesystem) : c_port(port), m_webserver(AsyncWebServer(port)), m_websocket(AsyncWebSocket("/ws")), m_filesystem(filesystem)
|
||||
{
|
||||
LOG_DEBUG("Initializing Web Server");
|
||||
m_websocket.onEvent([this](AsyncWebSocket *server, AsyncWebSocketClient *client,
|
||||
@@ -31,12 +26,18 @@ AstroWebServer::AstroWebServer(const uint8_t port, fs::FS &filesystem) : m_port(
|
||||
m_webserver.begin();
|
||||
m_websocket.enable(true);
|
||||
|
||||
m_pingTimer = xTimerCreate("wsPingTimer", pdMS_TO_TICKS(2000), pdTRUE, (void *)&m_websocket, on_ping);
|
||||
m_pingTimer = xTimerCreate("wsPingTimer", pdMS_TO_TICKS(c_pingTime), pdTRUE, (void *)&m_websocket, on_ping);
|
||||
xTimerStart(m_pingTimer, pdMS_TO_TICKS(10));
|
||||
|
||||
registerWsCommand("setTime", [this](const ArduinoJson::JsonDocument &doc)
|
||||
{ onSetTme(doc); });
|
||||
|
||||
LOG_DEBUG("Webserver Init OK");
|
||||
}
|
||||
|
||||
AstroWebServer::~AstroWebServer()
|
||||
{
|
||||
xTimerStop(m_pingTimer, 0);
|
||||
xTimerDelete(m_pingTimer, pdMS_TO_TICKS(10));
|
||||
m_webserver.removeHandler(&m_websocket);
|
||||
m_webserver.end();
|
||||
@@ -50,18 +51,33 @@ void AstroWebServer::sendWsData(const String &data)
|
||||
}
|
||||
}
|
||||
|
||||
void AstroWebServer::registerWsCommand(const std::string &cmd, const WScommand func)
|
||||
{
|
||||
if (cmd.empty() || m_webserverCommands.contains(cmd))
|
||||
return;
|
||||
if (!func)
|
||||
return;
|
||||
m_webserverCommands[cmd] = func;
|
||||
}
|
||||
|
||||
void AstroWebServer::unRegisterWsCommand(const std::string &cmd)
|
||||
{
|
||||
if (m_webserverCommands.contains(cmd))
|
||||
m_webserverCommands.erase(cmd);
|
||||
}
|
||||
|
||||
void AstroWebServer::onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case WS_EVT_CONNECT:
|
||||
LOG_DEBUG("WS client IP[", client->remoteIP().toString().c_str(), "]-ID[", client->id(), "] CONNECTED");
|
||||
LOG_DEBUG("WS client IP [", client->remoteIP().toString().c_str(), "]-ID [", client->id(), "] CONNECTED");
|
||||
break;
|
||||
case WS_EVT_DISCONNECT:
|
||||
LOG_DEBUG("WS client IP[", client->remoteIP().toString().c_str(), "]-ID[", client->id(), "] DISCONNECTED");
|
||||
LOG_DEBUG("WS client IP [", client->remoteIP().toString().c_str(), "]-ID [", client->id(), "] DISCONNECTED");
|
||||
break;
|
||||
case WS_EVT_PONG:
|
||||
LOG_DEBUG("WS client IP[", client->remoteIP().toString().c_str(), "]-ID[", client->id(), "] PONG");
|
||||
LOG_DEBUG("WS client IP [", client->remoteIP().toString().c_str(), "]-ID [", client->id(), "] PONG");
|
||||
break;
|
||||
case WS_EVT_DATA:
|
||||
{
|
||||
@@ -75,38 +91,13 @@ void AstroWebServer::onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *cli
|
||||
LOG_ERROR("WS Client unable to deserialize Json");
|
||||
return;
|
||||
}
|
||||
if (!doc["cmd"].is<std::string>() || !s_webserverCommands.contains(doc["cmd"]))
|
||||
if (!doc["cmd"].is<std::string>() || !m_webserverCommands.contains(doc["cmd"]))
|
||||
{
|
||||
LOG_WARN("WS Client Invalid Json command [", doc["cmd"].as<std::string>().c_str(), "]");
|
||||
return;
|
||||
}
|
||||
std::string buffer;
|
||||
switch (s_webserverCommands.at(doc["cmd"]))
|
||||
{
|
||||
case SET_TIME:
|
||||
{
|
||||
auto epoch = doc["time"].as<time_t>();
|
||||
timeval te{
|
||||
.tv_sec = epoch,
|
||||
.tv_usec = 0,
|
||||
};
|
||||
timezone tz{
|
||||
.tz_minuteswest = 0,
|
||||
.tz_dsttime = DST_MET,
|
||||
};
|
||||
settimeofday(&te, &tz);
|
||||
time_t now = time(nullptr);
|
||||
struct tm *t = localtime(&now);
|
||||
buffer.resize(64);
|
||||
strftime(buffer.data(), sizeof(buffer), "%Y-%m-%d %H:%M:%S", t);
|
||||
LOG_DEBUG("WS Client set Datetime to: ", buffer.c_str());
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
// call external command callback
|
||||
break;
|
||||
}
|
||||
// execute callback function
|
||||
m_webserverCommands[doc["cmd"]](doc);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -164,3 +155,23 @@ void AstroWebServer::onUploadHandler(AsyncWebServerRequest *request, const Strin
|
||||
LOG_INFO("Uploaded file to LittleFS:", filename.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void AstroWebServer::onSetTme(const ArduinoJson::JsonDocument &doc)
|
||||
{
|
||||
std::string buffer;
|
||||
auto epoch = doc["time"].as<time_t>();
|
||||
timeval te{
|
||||
.tv_sec = epoch,
|
||||
.tv_usec = 0,
|
||||
};
|
||||
timezone tz{
|
||||
.tz_minuteswest = 0,
|
||||
.tz_dsttime = DST_MET,
|
||||
};
|
||||
settimeofday(&te, &tz);
|
||||
time_t now = time(nullptr);
|
||||
struct tm *t = localtime(&now);
|
||||
buffer.resize(64);
|
||||
strftime(buffer.data(), sizeof(buffer), "%Y-%m-%d %H:%M:%S", t);
|
||||
LOG_DEBUG("WS Client set Datetime to: ", buffer.c_str());
|
||||
}
|
||||
|
||||
@@ -8,11 +8,13 @@
|
||||
#include <AsyncTCP.h>
|
||||
#include <filesystem>
|
||||
#include <map>
|
||||
|
||||
#include <FS.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
class AstroWebServer
|
||||
{
|
||||
public:
|
||||
using WScommand = std::function<void(const ArduinoJson::JsonDocument &)>;
|
||||
|
||||
public:
|
||||
AstroWebServer(const uint8_t port, fs::FS &filesystem);
|
||||
@@ -20,6 +22,9 @@ public:
|
||||
|
||||
void sendWsData(const String &data);
|
||||
|
||||
void registerWsCommand(const std::string &cmd, const WScommand func);
|
||||
void unRegisterWsCommand(const std::string &cmd);
|
||||
|
||||
private:
|
||||
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
|
||||
AwsEventType type, void *arg, uint8_t *data, size_t len);
|
||||
@@ -27,22 +32,16 @@ private:
|
||||
void onUploadRequest(AsyncWebServerRequest *request);
|
||||
void onUploadHandler(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final);
|
||||
|
||||
void onStart(AsyncWebServerRequest *request);
|
||||
void onStop(AsyncWebServerRequest *request);
|
||||
void onDownload(AsyncWebServerRequest *request);
|
||||
void onSetTme(const ArduinoJson::JsonDocument &doc);
|
||||
|
||||
private:
|
||||
const uint8_t m_port = 80;
|
||||
const uint8_t c_port = 80;
|
||||
const uint32_t c_pingTime = 5000;
|
||||
fs::FS &m_filesystem;
|
||||
AsyncWebServer m_webserver;
|
||||
AsyncWebSocket m_websocket;
|
||||
bool m_uploadFailed = false;
|
||||
fs::File m_uploadFile;
|
||||
TimerHandle_t m_pingTimer = NULL;
|
||||
|
||||
public:
|
||||
enum c_commandEnum
|
||||
{
|
||||
SET_TIME
|
||||
};
|
||||
std::map<const std::string, AstroWebServer::WScommand> m_webserverCommands;
|
||||
};
|
||||
|
||||
@@ -22,7 +22,7 @@ build_type = release
|
||||
[env:esp32-devtest-debug]
|
||||
board = esp32dev
|
||||
platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip
|
||||
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
hideakitai/DebugLog@^0.8.4
|
||||
board_build.flash_size = 4MB
|
||||
|
||||
12
RotaxMonitorTester/src/colors.h
Normal file
12
RotaxMonitorTester/src/colors.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
// ANSI colors
|
||||
#define COLOR_RESET "\033[0m"
|
||||
#define COLOR_RED "\033[31m"
|
||||
#define COLOR_GREEN "\033[32m"
|
||||
#define COLOR_BLUE "\033[34m"
|
||||
#define COLOR_MAGENTA "\033[35m"
|
||||
#define COLOR_CYAN "\033[36m"
|
||||
#define COLOR_YELLOW "\033[33m"
|
||||
#define COLOR_WHITE "\033[37m"
|
||||
#define COLOR_LBLUE "\033[94m"
|
||||
@@ -4,6 +4,8 @@
|
||||
#include <DebugLog.h>
|
||||
|
||||
#include "timer.h"
|
||||
#include "colors.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
static hw_timer_t *timerA = NULL;
|
||||
@@ -17,6 +19,12 @@ static uint32_t count = 0;
|
||||
#define SPARK_DLY_MIN 10
|
||||
#define SPARK_DLY_MAX 490
|
||||
|
||||
#define COIL_PULSE_MIN 100
|
||||
#define COIL_PULSE_MAX 1000
|
||||
|
||||
#define SPARK_PULSE_MIN 10
|
||||
#define SPARK_PULSE_MAX 500
|
||||
|
||||
#define PAUSE_LONG_MIN 5000
|
||||
#define PAUSE_LONG_MAX PAUSE_LONG_MIN * 100
|
||||
|
||||
@@ -30,7 +38,8 @@ void clearScreen()
|
||||
Serial.flush();
|
||||
}
|
||||
|
||||
static double filtered_rpm = 0;
|
||||
static uint32_t set_rpm = 500;
|
||||
static uint32_t set_delay = 100;
|
||||
|
||||
static const std::map<const uint32_t, const char *> pin2Name = {
|
||||
{PIN_TRIG_A12P, "HIGH_PIN_TRIG_A12P"},
|
||||
@@ -68,7 +77,7 @@ static timerStatus stsB = {
|
||||
.clock_period_us = (uint32_t)PERIOD_US,
|
||||
.pause_long_us = 10000,
|
||||
.pause_short_us = 1000,
|
||||
.coil_pulse_us = 1000,
|
||||
.coil_pulse_us = 500,
|
||||
.spark_pulse_us = 100,
|
||||
.spark_delay_us = 50,
|
||||
.pins = {
|
||||
@@ -83,11 +92,14 @@ static timerStatus stsB = {
|
||||
static bool isEnabled_A = false;
|
||||
static bool isEnabled_B = false;
|
||||
|
||||
static String last_command;
|
||||
|
||||
void setup()
|
||||
{
|
||||
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
Serial.setTimeout(100);
|
||||
LOG_ATTACH_SERIAL(Serial);
|
||||
|
||||
pinMode(PIN_TRIG_A12P, OUTPUT);
|
||||
@@ -133,63 +145,124 @@ void setup()
|
||||
|
||||
void loop()
|
||||
{
|
||||
LOG_INFO("Loop: ", count++);
|
||||
uint32_t spark_delay = (uint32_t)(map(analogRead(SPARK_DELAY_POT), 0, 4096, SPARK_DLY_MIN, SPARK_DLY_MAX) / PERIOD_US);
|
||||
stsA.spark_delay_us = spark_delay * PERIOD_US;
|
||||
if (stsA.spark_delay_us > (SPARK_DLY_MIN + SPARK_DLY_MAX) / 2)
|
||||
{
|
||||
stsA.soft_start = true;
|
||||
stsA.spark_delay_us -= (SPARK_DLY_MIN + SPARK_DLY_MAX) / 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
stsA.soft_start = false;
|
||||
}
|
||||
stsB.soft_start = stsA.soft_start;
|
||||
stsB.spark_delay_us = stsA.spark_delay_us;
|
||||
clearScreen();
|
||||
|
||||
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);
|
||||
stsB.pause_long_us = stsA.pause_long_us;
|
||||
Serial.printf("\t++++ Loop: %u ++++\n", count++);
|
||||
|
||||
if (isEnabled_A)
|
||||
LOG_INFO("==== System A is ENABLED ====");
|
||||
Serial.println("==== System A is" COLOR_GREEN " ENABLED" COLOR_RESET " ====");
|
||||
else
|
||||
LOG_INFO("==== System A is DISABLED ====");
|
||||
Serial.println("==== System A is" COLOR_RED " DISABLED" COLOR_RESET " ====");
|
||||
|
||||
if (isEnabled_B)
|
||||
LOG_INFO("==== System B is ENABLED ====");
|
||||
Serial.println("==== System B is" COLOR_GREEN " ENABLED" COLOR_RESET " ====");
|
||||
else
|
||||
LOG_INFO("==== System B is DISABLED ====");
|
||||
Serial.println("==== System B is" COLOR_RED " DISABLED" COLOR_RESET " ====");
|
||||
|
||||
LOG_INFO("Spark Delay uS: ", stsA.spark_delay_us, "\tSoft Start: ", stsA.soft_start ? "TRUE" : "FALSE");
|
||||
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");
|
||||
Serial.printf("Spark Delay uS: %u\n", stsA.spark_delay_us);
|
||||
Serial.printf("Soft Start: %s\n", stsA.soft_start ? "ENABLED" : "DISABLED");
|
||||
Serial.printf("Engine Rpm: %u\n", (uint32_t)(set_rpm));
|
||||
Serial.printf("Coil Pulse: %u uS\n", stsA.coil_pulse_us);
|
||||
Serial.printf("Spark Pulse: %u uS\n", stsA.spark_pulse_us);
|
||||
Serial.println(COLOR_CYAN "-------------------------------------");
|
||||
Serial.println("E[a/b] > Enable Box a/b | D[a/b] > Disable a/b");
|
||||
Serial.println("S[ddd] > Spark Delay | R[dddd] > Engine RPM");
|
||||
Serial.println("C[ddd] > Spark Pulse | P[ddd] > Coil Pulse");
|
||||
Serial.println("-------------------------------------" COLOR_RESET);
|
||||
Serial.printf("Last Command: %s\n", last_command.c_str());
|
||||
|
||||
if (digitalRead(ENABLE_PIN_A) == LOW && !isEnabled_A)
|
||||
auto str = Serial.readStringUntil('\n');
|
||||
if (!str.isEmpty())
|
||||
{
|
||||
timerStart(timerA);
|
||||
isEnabled_A = true;
|
||||
}
|
||||
else if (digitalRead(ENABLE_PIN_A) == HIGH && isEnabled_A)
|
||||
{
|
||||
timerStop(timerA);
|
||||
isEnabled_A = false;
|
||||
last_command = str;
|
||||
const auto cmd = str.charAt(0);
|
||||
char c;
|
||||
switch (cmd)
|
||||
{
|
||||
case 'E':
|
||||
{
|
||||
char box;
|
||||
sscanf(str.c_str(), "%c%c\n", &c, &box);
|
||||
if (box == 'a' && !isEnabled_A)
|
||||
{
|
||||
timerStart(timerA);
|
||||
isEnabled_A = true;
|
||||
}
|
||||
else if (box == 'b' && !isEnabled_B)
|
||||
{
|
||||
timerStart(timerB);
|
||||
isEnabled_B = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'D':
|
||||
{
|
||||
char c;
|
||||
char box;
|
||||
sscanf(str.c_str(), "%c%c\n", &c, &box);
|
||||
if (box == 'a' && isEnabled_A)
|
||||
{
|
||||
timerStop(timerA);
|
||||
isEnabled_A = false;
|
||||
}
|
||||
else if (box == 'b' && isEnabled_B)
|
||||
{
|
||||
timerStop(timerB);
|
||||
isEnabled_B = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'R':
|
||||
{
|
||||
int new_rpm;
|
||||
sscanf(str.c_str(), "%c%d\n", &c, &new_rpm);
|
||||
new_rpm = min(RPM_MAX, max(RPM_MIN, new_rpm));
|
||||
stsA.pause_long_us = (uint32_t)(60000000.0f / (float)new_rpm / 2.0f);
|
||||
stsB.pause_long_us = stsA.pause_long_us;
|
||||
set_rpm = (uint32_t)new_rpm;
|
||||
break;
|
||||
}
|
||||
case 'S':
|
||||
{
|
||||
int new_delay;
|
||||
sscanf(str.c_str(), "%c%d\n", &c, &new_delay);
|
||||
new_delay = min(SPARK_DLY_MAX, max(SPARK_DLY_MIN, new_delay));
|
||||
stsA.spark_delay_us = (uint32_t)(new_delay);
|
||||
if (stsA.spark_delay_us > (SPARK_DLY_MIN + SPARK_DLY_MAX) / 2)
|
||||
{
|
||||
stsA.soft_start = true;
|
||||
stsA.spark_delay_us -= (SPARK_DLY_MIN + SPARK_DLY_MAX) / 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
stsA.soft_start = false;
|
||||
}
|
||||
stsB.soft_start = stsA.soft_start;
|
||||
stsB.spark_delay_us = stsA.spark_delay_us;
|
||||
break;
|
||||
}
|
||||
case 'P':
|
||||
{
|
||||
int new_pulse;
|
||||
sscanf(str.c_str(), "%c%d\n", &c, &new_pulse);
|
||||
new_pulse = min(COIL_PULSE_MAX, max(COIL_PULSE_MIN, new_pulse));
|
||||
stsA.coil_pulse_us = stsB.coil_pulse_us = (uint32_t)new_pulse;
|
||||
break;
|
||||
}
|
||||
case 'C':
|
||||
{
|
||||
int new_pulse;
|
||||
sscanf(str.c_str(), "%c%d\n", &c, &new_pulse);
|
||||
new_pulse = min(SPARK_PULSE_MAX, max(SPARK_PULSE_MIN, new_pulse));
|
||||
stsA.spark_pulse_us = stsB.spark_pulse_us = (uint32_t)new_pulse;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
Serial.read();
|
||||
}
|
||||
|
||||
if (digitalRead(ENABLE_PIN_B) == LOW && !isEnabled_B)
|
||||
{
|
||||
timerStart(timerB);
|
||||
isEnabled_B = true;
|
||||
}
|
||||
else if (digitalRead(ENABLE_PIN_B) == HIGH && isEnabled_B)
|
||||
{
|
||||
timerStop(timerB);
|
||||
isEnabled_B = false;
|
||||
}
|
||||
|
||||
delay(100);
|
||||
clearScreen();
|
||||
str.clear();
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user