250 lines
8.1 KiB
C++
250 lines
8.1 KiB
C++
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
|
|
|
|
// Arduino Libraries
|
|
#include <Arduino.h>
|
|
#include <DebugLog.h>
|
|
#include <DebugLogEnable.h>
|
|
#include <SPI.h>
|
|
#include <WiFi.h>
|
|
#include <ArduinoJson.h>
|
|
|
|
// Definitions
|
|
#include <tasks.h>
|
|
#include <devices.h>
|
|
#include <datasave.h>
|
|
#include <webserver.h>
|
|
#include <ui.h>
|
|
#include <led.h>
|
|
|
|
// Defines to enable channel B
|
|
#define CH_B_ENABLE
|
|
// #define TEST
|
|
|
|
// Debug Defines
|
|
#define WIFI_SSID "AstroRotaxMonitor"
|
|
#define WIFI_PASSWORD "maledettirotax"
|
|
|
|
void setup()
|
|
{
|
|
Serial.begin(921600);
|
|
delay(250);
|
|
|
|
// Setup Logger
|
|
LOG_ATTACH_SERIAL(Serial);
|
|
LOG_SET_LEVEL(DebugLogLevel::LVL_DEBUG);
|
|
|
|
// Print Processor Info
|
|
LOG_DEBUG("ESP32 Chip:", ESP.getChipModel());
|
|
if (psramFound())
|
|
{
|
|
LOG_DEBUG("ESP32 PSram Found");
|
|
LOG_DEBUG("ESP32 PSram:", ESP.getPsramSize());
|
|
psramInit();
|
|
}
|
|
LOG_DEBUG("ESP32 Flash:", ESP.getFlashChipSize());
|
|
LOG_DEBUG("ESP32 Heap:", ESP.getHeapSize());
|
|
LOG_DEBUG("ESP32 Sketch:", ESP.getFreeSketchSpace());
|
|
|
|
// Init Wifi station
|
|
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
|
|
if (WiFi.softAP(WIFI_SSID, WIFI_PASSWORD))
|
|
{
|
|
LOG_INFO("WiFi AP Mode Started");
|
|
LOG_INFO("Wifi SSID:", WIFI_SSID);
|
|
LOG_INFO("Wifi Password:", WIFI_PASSWORD);
|
|
LOG_INFO("WiFi IP:" + WiFi.softAPIP().toString());
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("Failed to start WiFi AP Mode");
|
|
LOG_ERROR("5 seconds to restart...");
|
|
vTaskDelay(pdMS_TO_TICKS(5000));
|
|
esp_restart();
|
|
}
|
|
|
|
// Initialize Interrupt pins on PICKUP detectors
|
|
initTriggerPinsInputs();
|
|
// Initialize Interrupt pins on SPARK detectors
|
|
initSparkPinInputs();
|
|
}
|
|
|
|
////////////////////// MAIN LOOP //////////////////////
|
|
void loop()
|
|
{
|
|
// global variables
|
|
RGBled led;
|
|
led.setStatus(RGBled::LedStatus::INIT);
|
|
bool running = true;
|
|
std::mutex fs_mutex;
|
|
LITTLEFSGuard fsGuard;
|
|
|
|
//////// INIT SPI PORTS ////////
|
|
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
|
|
if (!spiA_ok || !spiB_ok)
|
|
{
|
|
LOG_ERROR("Unable to Initialize SPI Busses");
|
|
LOG_ERROR("5 seconds to restart...");
|
|
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);
|
|
|
|
// // 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);
|
|
|
|
// 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);
|
|
|
|
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},
|
|
.rt_io = rtIgnitionTask::rtTaskIOParams{
|
|
.pot_cs_12 = POT_CS_A12,
|
|
.pot_cs_34 = POT_CS_A34,
|
|
.ss_force = SS_FORCE_A,
|
|
.ss_inhibit_12 = SS_INIBHIT_A12,
|
|
.ss_inhibit_34 = SS_INHIBIT_A34,
|
|
.sh_disch_12 = SH_DISCH_A12,
|
|
.sh_disch_34 = SH_DISCH_A34,
|
|
.sh_arm_12 = SH_ARM_A12,
|
|
.sh_arm_34 = SH_ARM_A34,
|
|
.relay_in_12 = RELAY_IN_A12,
|
|
.relay_in_34 = RELAY_OUT_A12,
|
|
.relay_out_12 = RELAY_IN_A34,
|
|
.relay_out_34 = RELAY_OUT_A34,
|
|
},
|
|
.rt_queue = nullptr,
|
|
.dev = dev};
|
|
|
|
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},
|
|
.rt_io = rtIgnitionTask::rtTaskIOParams{
|
|
.pot_cs_12 = POT_CS_B12,
|
|
.pot_cs_34 = POT_CS_B34,
|
|
.ss_force = SS_FORCE_B,
|
|
.ss_inhibit_12 = SS_INIBHIT_B12,
|
|
.ss_inhibit_34 = SS_INHIBIT_B34,
|
|
.sh_disch_12 = SH_DISCH_B12,
|
|
.sh_disch_34 = SH_DISCH_B34,
|
|
.sh_arm_12 = SH_ARM_B12,
|
|
.sh_arm_34 = SH_ARM_B34,
|
|
.relay_in_12 = RELAY_IN_B12,
|
|
.relay_in_34 = RELAY_OUT_B12,
|
|
.relay_out_12 = RELAY_IN_B34,
|
|
.relay_out_34 = RELAY_OUT_B34,
|
|
},
|
|
.rt_queue = nullptr,
|
|
.dev = dev};
|
|
|
|
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);
|
|
|
|
// 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");
|
|
LOG_ERROR("5 seconds to restart...");
|
|
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);
|
|
|
|
AstroWebServer webPage(80, LittleFS); // Initialize 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;
|
|
});
|
|
|
|
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");
|
|
|
|
uint32_t monitor_loop = millis();
|
|
uint32_t data_loop = monitor_loop;
|
|
//////////////// INNER LOOP /////////////////////
|
|
while (running)
|
|
{
|
|
uint32_t this_loop = millis();
|
|
if (this_loop - monitor_loop > 2000)
|
|
{
|
|
clearScreen();
|
|
printRunningTasksMod(Serial);
|
|
monitor_loop = millis();
|
|
}
|
|
if ((data_a && data_b) || (this_loop - data_loop > 500)) {
|
|
webPage.sendWsData(json_data.as<String>());
|
|
json_data.clear();
|
|
data_a = data_b = false;
|
|
data_loop = millis();
|
|
}
|
|
} //////////////// INNER LOOP /////////////////////
|
|
|
|
} ////////////////////// MAIN LOOP //////////////////////
|