Compare commits
2 Commits
8171cab9cb
...
5aa5aaa07a
| Author | SHA1 | Date | |
|---|---|---|---|
| 5aa5aaa07a | |||
| 1b7a531d54 |
@@ -15,8 +15,6 @@
|
||||
#include "ADS1256.h"
|
||||
#include "SPI.h"
|
||||
|
||||
#include "DebugLog.h"
|
||||
|
||||
#define convertSigned24BitToLong(value) ((value) & (1l << 23) ? (value) - 0x1000000 : value)
|
||||
|
||||
//Constructor
|
||||
@@ -40,7 +38,6 @@ ADS1256::ADS1256(const int8_t DRDY_pin, const int8_t RESET_pin, const int8_t SYN
|
||||
pinMode(_CS_pin, OUTPUT);
|
||||
}
|
||||
|
||||
LOG_DEBUG("ADC Class Init OK");
|
||||
updateConversionParameter();
|
||||
}
|
||||
|
||||
@@ -66,7 +63,7 @@ void ADS1256::InitializeADC()
|
||||
}
|
||||
|
||||
#ifndef ADS1256_SPI_ALREADY_STARTED //Guard macro to allow external initialization of the SPI
|
||||
//_spi->begin();
|
||||
_spi->begin();
|
||||
#endif
|
||||
|
||||
//Applying arbitrary default values to speed up the starting procedure if the user just want to get quick readouts
|
||||
@@ -99,17 +96,13 @@ void ADS1256::InitializeADC()
|
||||
|
||||
void ADS1256::waitForLowDRDY()
|
||||
{
|
||||
while (digitalRead(_DRDY_pin) == HIGH)
|
||||
{
|
||||
}
|
||||
while (digitalRead(_DRDY_pin) == HIGH) {}
|
||||
}
|
||||
|
||||
void ADS1256::waitForHighDRDY()
|
||||
{
|
||||
#if F_CPU >= 48000000 //Fast MCUs need this protection to wait until DRDY goes high after a conversion
|
||||
while (digitalRead(_DRDY_pin) == LOW)
|
||||
{
|
||||
}
|
||||
while (digitalRead(_DRDY_pin) == LOW) {}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -127,14 +120,14 @@ void ADS1256::setDRATE(uint8_t drate) // Setting DRATE (sampling frequency)
|
||||
{
|
||||
writeRegister(DRATE_REG, drate);
|
||||
_DRATE = drate;
|
||||
delayMicroseconds(500);
|
||||
delay(200);
|
||||
}
|
||||
|
||||
void ADS1256::setMUX(uint8_t mux) //Setting MUX (input channel)
|
||||
{
|
||||
writeRegister(MUX_REG, mux);
|
||||
_MUX = mux;
|
||||
// delayMicroseconds(500);
|
||||
delay(200);
|
||||
}
|
||||
|
||||
void ADS1256::setPGA(uint8_t pga) //Setting PGA (input voltage range)
|
||||
@@ -145,7 +138,7 @@ void ADS1256::setPGA(uint8_t pga) // Setting PGA (input voltage range)
|
||||
_ADCON = (_ADCON & 0b11111000) | (_PGA & 0b00000111); // Clearing and then setting bits 2-0 based on pga
|
||||
|
||||
writeRegister(ADCON_REG, _ADCON);
|
||||
delayMicroseconds(1000); // Delay to allow the PGA to settle after changing its value
|
||||
delay(200);
|
||||
|
||||
updateConversionParameter(); //Update the multiplier according top the new PGA value
|
||||
}
|
||||
@@ -188,9 +181,7 @@ void ADS1256::setCLKOUT(uint8_t clkout) // Setting CLKOUT
|
||||
bitWrite(_ADCON, 6, 1);
|
||||
bitWrite(_ADCON, 5, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
else{}
|
||||
|
||||
writeRegister(ADCON_REG, _ADCON);
|
||||
delay(100);
|
||||
@@ -226,9 +217,7 @@ void ADS1256::setSDCS(uint8_t sdcs) // Setting SDCS
|
||||
bitWrite(_ADCON, 4, 1);
|
||||
bitWrite(_ADCON, 3, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
else{}
|
||||
|
||||
writeRegister(ADCON_REG, _ADCON);
|
||||
delay(100);
|
||||
@@ -250,9 +239,7 @@ void ADS1256::setByteOrder(uint8_t byteOrder) // Setting byte order (MSB/LSB)
|
||||
bitWrite(_STATUS, 3, 1);
|
||||
//Set value of _STATUS at the third bit to 1
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
else{}
|
||||
|
||||
writeRegister(STATUS_REG, _STATUS);
|
||||
delay(100);
|
||||
@@ -281,9 +268,7 @@ void ADS1256::setAutoCal(uint8_t acal) // Setting ACAL (Automatic SYSCAL)
|
||||
bitWrite(_STATUS, 2, 1);
|
||||
//_STATUS |= B00000100;
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
else{}
|
||||
|
||||
writeRegister(STATUS_REG, _STATUS);
|
||||
delay(100);
|
||||
@@ -312,9 +297,7 @@ void ADS1256::setBuffer(uint8_t bufen) // Setting input buffer (Input impedance)
|
||||
//_STATUS |= B00000010;
|
||||
bitWrite(_STATUS, 1, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
else{}
|
||||
|
||||
writeRegister(STATUS_REG, _STATUS);
|
||||
delay(100);
|
||||
@@ -476,28 +459,24 @@ uint8_t ADS1256::readGPIO(uint8_t gpioPin) // Reading GPIO
|
||||
}
|
||||
|
||||
return GPIO_return;
|
||||
|
||||
}
|
||||
|
||||
void ADS1256::sendDirectCommand(uint8_t directCommand)
|
||||
{
|
||||
LOG_DEBUG("Direct Command");
|
||||
//Direct commands can be found in the datasheet Page 34, Table 24.
|
||||
LOG_DEBUG("Direct Command Begin");
|
||||
_spi->beginTransaction(SPISettings(1920000, MSBFIRST, SPI_MODE1));
|
||||
|
||||
CS_LOW(); //REF: P34: "CS must stay low during the entire command sequence"
|
||||
LOG_DEBUG("Direct Command CS LOW");
|
||||
delayMicroseconds(5);
|
||||
_spi->transfer(directCommand); //Send Command
|
||||
LOG_DEBUG("Transfer OK");
|
||||
delayMicroseconds(5);
|
||||
CS_HIGH(); //REF: P34: "CS must stay low during the entire command sequence"
|
||||
LOG_DEBUG("Direct Command CS HIGH");
|
||||
|
||||
_spi->endTransaction();
|
||||
LOG_DEBUG("Direct Command End");
|
||||
}
|
||||
|
||||
|
||||
float ADS1256::convertToVoltage(int32_t rawData) //Converting the 24-bit data into a voltage value
|
||||
{
|
||||
return(conversionParameter * rawData);
|
||||
@@ -506,30 +485,24 @@ float ADS1256::convertToVoltage(int32_t rawData) // Converting the 24-bit data i
|
||||
void ADS1256::writeRegister(uint8_t registerAddress, uint8_t registerValueToWrite)
|
||||
{
|
||||
waitForLowDRDY();
|
||||
LOG_DEBUG("DRDY Low");
|
||||
|
||||
_spi->beginTransaction(SPISettings(1920000, MSBFIRST, SPI_MODE1));
|
||||
//SPI_MODE1 = output edge: rising, data capture: falling; clock polarity: 0, clock phase: 1.
|
||||
LOG_DEBUG("SPI Begin");
|
||||
|
||||
CS_LOW(); //CS must stay LOW during the entire sequence [Ref: P34, T24]
|
||||
LOG_DEBUG("CS Low");
|
||||
|
||||
delayMicroseconds(5); //see t6 in the datasheet
|
||||
|
||||
_spi->transfer(0x50 | registerAddress); // 0x50 = 01010000 = WREG
|
||||
LOG_DEBUG("Transfer 1");
|
||||
|
||||
_spi->transfer(0x00); //2nd (empty) command byte
|
||||
LOG_DEBUG("Transfer 2");
|
||||
|
||||
_spi->transfer(registerValueToWrite); //pass the value to the register
|
||||
LOG_DEBUG("Transfer 3");
|
||||
|
||||
CS_HIGH();
|
||||
LOG_DEBUG("CS High");
|
||||
_spi->endTransaction();
|
||||
LOG_DEBUG("SPI End");
|
||||
delay(100);
|
||||
|
||||
}
|
||||
|
||||
long ADS1256::readRegister(uint8_t registerAddress) //Reading a register
|
||||
@@ -551,10 +524,11 @@ long ADS1256::readRegister(uint8_t registerAddress) // Reading a register
|
||||
|
||||
CS_HIGH();
|
||||
_spi->endTransaction();
|
||||
|
||||
delay(100);
|
||||
return regValue;
|
||||
}
|
||||
|
||||
|
||||
long ADS1256::readSingle() //Reading a single value ONCE using the RDATA command
|
||||
{
|
||||
_spi->beginTransaction(SPISettings(1920000, MSBFIRST, SPI_MODE1));
|
||||
@@ -621,8 +595,7 @@ long ADS1256::cycleSingle()
|
||||
CS_LOW(); //CS must stay LOW during the entire sequence [Ref: P34, T24]
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
{}
|
||||
|
||||
if(_cycle < 8)
|
||||
{
|
||||
@@ -709,8 +682,7 @@ long ADS1256::cycleDifferential()
|
||||
CS_LOW(); //CS must stay LOW during the entire sequence [Ref: P34, T24]
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
{}
|
||||
|
||||
if(_cycle < 4)
|
||||
{
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
#include "datasave.h"
|
||||
#include <math.h>
|
||||
|
||||
|
||||
|
||||
LITTLEFSGuard::LITTLEFSGuard()
|
||||
{
|
||||
if (!LittleFS.begin(true, "/littlefs", 10, "littlefs"))
|
||||
@@ -55,23 +53,24 @@ void ignitionBoxStatusFiltered::update(const ignitionBoxStatus &new_status)
|
||||
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.coils12.spark_delay = new_status.coils12.spark_delay; // incremental average calculation
|
||||
m_last.coils12.peak_p_in = new_status.coils12.peak_p_in; // incremental average calculation
|
||||
m_last.coils12.peak_n_in = new_status.coils12.peak_n_in; // incremental average calculation
|
||||
m_last.coils12.peak_p_out = new_status.coils12.peak_p_out; // incremental average calculation
|
||||
m_last.coils12.peak_n_out = new_status.coils12.peak_n_out; // incremental average calculation
|
||||
|
||||
m_last.coils34.n_events = new_status.coils34.n_events; // sum events instead of averaging
|
||||
m_last.coils34.n_missed_firing = new_status.coils34.n_missed_firing; // sum missed firings instead of averaging
|
||||
m_last.coils34.spark_status = new_status.coils34.spark_status; // take latest spark status
|
||||
m_last.coils34.sstart_status = new_status.coils34.sstart_status; // take latest soft start status
|
||||
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.coils34.spark_delay = new_status.coils34.spark_delay; // incremental average calculation
|
||||
m_last.coils34.peak_p_in = new_status.coils34.peak_p_in; // incremental average calculation
|
||||
m_last.coils34.peak_n_in = new_status.coils34.peak_n_in; // incremental average calculation
|
||||
m_last.coils34.peak_p_out = new_status.coils34.peak_p_out; // incremental average calculation
|
||||
m_last.coils34.peak_n_out = new_status.coils34.peak_n_out; // incremental average calculation
|
||||
|
||||
m_last.eng_rpm = new_status.eng_rpm; // incremental average calculation
|
||||
m_last.adc_read_time = m_last.adc_read_time; // incremental average calculation
|
||||
m_last.n_queue_errors = new_status.n_queue_errors; // take last of queue errors since it's a cumulative count of errors in the queue, not an average value
|
||||
|
||||
if (m_count >= m_max_count)
|
||||
@@ -124,4 +123,3 @@ const ArduinoJson::JsonDocument ignitionBoxStatusFiltered::toJson() const
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
|
||||
|
||||
@@ -43,18 +43,13 @@ struct Devices
|
||||
std::unique_ptr<ADS1256> m_adc_b = nullptr;
|
||||
|
||||
std::unique_ptr<ExternalIO> m_ext_io = nullptr;
|
||||
|
||||
};
|
||||
|
||||
// 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 < 5; i++)
|
||||
{
|
||||
adc->readSingle();
|
||||
}
|
||||
// ora lettura valida a 30kSPS → ~100 µs di settling
|
||||
return adc->convertToVoltage(adc->readSingle());
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ void setup()
|
||||
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");
|
||||
@@ -123,14 +123,37 @@ void loop()
|
||||
// Configure ADCs
|
||||
dev->m_adc_a->InitializeADC();
|
||||
dev->m_adc_a->setPGA(PGA_1);
|
||||
dev->m_adc_a->setDRATE(DRATE_7500SPS);
|
||||
// dev->m_adc_a->setDRATE(DRATE_15000SPS);
|
||||
#ifdef CH_B_ENABLE
|
||||
dev->m_adc_b->InitializeADC();
|
||||
dev->m_adc_b->setPGA(PGA_1);
|
||||
dev->m_adc_b->setDRATE(DRATE_7500SPS);
|
||||
dev->m_adc_b->setDRATE(DRATE_30000SPS);
|
||||
#endif
|
||||
LOG_DEBUG("Init SPI OK");
|
||||
|
||||
uint8_t chs[8] = {
|
||||
SING_0, SING_1, SING_2, SING_3, SING_4, SING_5, SING_6, SING_7
|
||||
};
|
||||
float res[8];
|
||||
|
||||
while (Serial.read() != 's') // The conversion is stopped by a character received from the serial port
|
||||
{
|
||||
clearScreen();
|
||||
auto start = esp_timer_get_time();
|
||||
for (int i = 0; i < 8; i++){
|
||||
// dev->m_adc_a->setMUX(chs[i]);
|
||||
res[i] = dev->m_adc_a->convertToVoltage(dev->m_adc_a->cycleSingle());
|
||||
}
|
||||
auto stop = esp_timer_get_time();
|
||||
for (int j = 0; j < 8; j++){
|
||||
Serial.printf("ADC_A SING_%d: %5.4f\n",j, res[j]);
|
||||
}
|
||||
Serial.printf("ADC Time: %u us\n", stop-start);
|
||||
delay(100);
|
||||
|
||||
}
|
||||
dev->m_adc_a->stopConversion();
|
||||
|
||||
//////// INIT I2C INTERFACES ////////
|
||||
LOG_DEBUG("Init I2C Interfaces");
|
||||
bool i2c_ok = true;
|
||||
|
||||
@@ -46,8 +46,7 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
|
||||
TaskStatus_t rt_task_info;
|
||||
vTaskGetInfo(NULL, &rt_task_info, pdFALSE, eInvalid);
|
||||
|
||||
const auto rt_task_name = pcTaskGetName(rt_task_info.xHandle);
|
||||
LOG_INFO("rtTask Params OK [", rt_task_name, "]");
|
||||
LOG_INFO("rtTask Params OK [", params->name.c_str(), "]");
|
||||
|
||||
ignitionBoxStatus ign_box_sts;
|
||||
|
||||
@@ -98,7 +97,7 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
|
||||
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);
|
||||
|
||||
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;
|
||||
@@ -256,10 +255,23 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
|
||||
// outputs on io expander
|
||||
if (io)
|
||||
{
|
||||
// [TODO] code to reset sample and hold and arm trigger level detectors
|
||||
// Discharge Pulse
|
||||
io->extDigitalWrite(rt_rst.sh_disch_12, true);
|
||||
io->extDigitalWrite(rt_rst.sh_disch_34, true);
|
||||
delayMicroseconds(250);
|
||||
io->extDigitalWrite(rt_rst.sh_disch_12, false);
|
||||
io->extDigitalWrite(rt_rst.sh_disch_34, false);
|
||||
// Safety delay
|
||||
delayMicroseconds(500);
|
||||
// Re-Arm Pulse
|
||||
io->extDigitalWrite(rt_rst.sh_arm_12, true);
|
||||
io->extDigitalWrite(rt_rst.sh_arm_34, true);
|
||||
delayMicroseconds(250);
|
||||
io->extDigitalWrite(rt_rst.sh_arm_12, false);
|
||||
io->extDigitalWrite(rt_rst.sh_arm_34, false);
|
||||
}
|
||||
else
|
||||
vTaskDelay(pdMS_TO_TICKS(2));
|
||||
vTaskDelay(pdMS_TO_TICKS(c_io_time));
|
||||
|
||||
// send essage to main loop with ignition info, by copy so local static variable is ok
|
||||
if (rt_queue)
|
||||
@@ -272,7 +284,7 @@ void rtIgnitionTask::rtIgnitionTask_realtime(void *pvParameters)
|
||||
}
|
||||
// Delete the timeout timer
|
||||
esp_timer_delete(timeout_timer);
|
||||
LOG_WARN("rtTask Ending [", rt_task_name, "]");
|
||||
LOG_WARN("rtTask Ending [", params->name.c_str(), "]");
|
||||
// Ignition A Interrupts DETACH
|
||||
detachInterrupt(rt_int.trig_pin_12p);
|
||||
detachInterrupt(rt_int.trig_pin_12n);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"
|
||||
+118
-45
@@ -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,9 +145,89 @@ 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;
|
||||
clearScreen();
|
||||
|
||||
Serial.printf("\t++++ Loop: %u ++++\n", count++);
|
||||
|
||||
if (isEnabled_A)
|
||||
Serial.println("==== System A is" COLOR_GREEN " ENABLED" COLOR_RESET " ====");
|
||||
else
|
||||
Serial.println("==== System A is" COLOR_RED " DISABLED" COLOR_RESET " ====");
|
||||
|
||||
if (isEnabled_B)
|
||||
Serial.println("==== System B is" COLOR_GREEN " ENABLED" COLOR_RESET " ====");
|
||||
else
|
||||
Serial.println("==== System B is" COLOR_RED " DISABLED" COLOR_RESET " ====");
|
||||
|
||||
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());
|
||||
|
||||
auto str = Serial.readStringUntil('\n');
|
||||
if (!str.isEmpty())
|
||||
{
|
||||
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;
|
||||
@@ -147,49 +239,30 @@ void loop()
|
||||
}
|
||||
stsB.soft_start = stsA.soft_start;
|
||||
stsB.spark_delay_us = stsA.spark_delay_us;
|
||||
|
||||
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;
|
||||
|
||||
if (isEnabled_A)
|
||||
LOG_INFO("==== System A is ENABLED ====");
|
||||
else
|
||||
LOG_INFO("==== System A is DISABLED ====");
|
||||
|
||||
if (isEnabled_B)
|
||||
LOG_INFO("==== System B is ENABLED ====");
|
||||
else
|
||||
LOG_INFO("==== System B is DISABLED ====");
|
||||
|
||||
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");
|
||||
|
||||
if (digitalRead(ENABLE_PIN_A) == LOW && !isEnabled_A)
|
||||
break;
|
||||
}
|
||||
case 'P':
|
||||
{
|
||||
timerStart(timerA);
|
||||
isEnabled_A = true;
|
||||
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;
|
||||
}
|
||||
else if (digitalRead(ENABLE_PIN_A) == HIGH && isEnabled_A)
|
||||
case 'C':
|
||||
{
|
||||
timerStop(timerA);
|
||||
isEnabled_A = false;
|
||||
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