mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-06 03:50:13 +02:00
Merge branch 'main' into feature/event-CAN-timing
This commit is contained in:
commit
4fbfe6f5e9
1 changed files with 50 additions and 33 deletions
|
@ -17,6 +17,10 @@
|
||||||
#include "src/lib/eModbus-eModbus/scripts/mbServerFCs.h"
|
#include "src/lib/eModbus-eModbus/scripts/mbServerFCs.h"
|
||||||
#include "src/lib/miwagner-ESP32-Arduino-CAN/CAN_config.h"
|
#include "src/lib/miwagner-ESP32-Arduino-CAN/CAN_config.h"
|
||||||
#include "src/lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"
|
#include "src/lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"
|
||||||
|
#include "freertos/FreeRTOS.h"
|
||||||
|
#include "freertos/task.h"
|
||||||
|
#include "esp_system.h"
|
||||||
|
#include "esp_task_wdt.h"
|
||||||
|
|
||||||
#ifdef WEBSERVER
|
#ifdef WEBSERVER
|
||||||
#include <ESPmDNS.h>
|
#include <ESPmDNS.h>
|
||||||
|
@ -119,6 +123,8 @@ unsigned long timeSpentInFaultedMode = 0;
|
||||||
bool batteryAllowsContactorClosing = false;
|
bool batteryAllowsContactorClosing = false;
|
||||||
bool inverterAllowsContactorClosing = true;
|
bool inverterAllowsContactorClosing = true;
|
||||||
|
|
||||||
|
TaskHandle_t mainLoopTask;
|
||||||
|
|
||||||
// Initialization
|
// Initialization
|
||||||
void setup() {
|
void setup() {
|
||||||
init_serial();
|
init_serial();
|
||||||
|
@ -149,60 +155,71 @@ void setup() {
|
||||||
|
|
||||||
// BOOT button at runtime is used as an input for various things
|
// BOOT button at runtime is used as an input for various things
|
||||||
pinMode(0, INPUT_PULLUP);
|
pinMode(0, INPUT_PULLUP);
|
||||||
|
|
||||||
|
esp_task_wdt_deinit(); // Disable watchdog
|
||||||
|
|
||||||
|
xTaskCreatePinnedToCore((TaskFunction_t)&mainLoop, "mainLoop", 4096, NULL, 8, &mainLoopTask, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform main program functions
|
// Perform main program functions
|
||||||
void loop() {
|
void loop() {
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
void mainLoop(void* pvParameters) {
|
||||||
|
while (true) {
|
||||||
|
|
||||||
#ifdef WEBSERVER
|
#ifdef WEBSERVER
|
||||||
// Over-the-air updates by ElegantOTA
|
// Over-the-air updates by ElegantOTA
|
||||||
wifi_monitor();
|
wifi_monitor();
|
||||||
ElegantOTA.loop();
|
ElegantOTA.loop();
|
||||||
#ifdef MQTT
|
#ifdef MQTT
|
||||||
mqtt_loop();
|
mqtt_loop();
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Input
|
// Input
|
||||||
receive_can(); // Receive CAN messages. Runs as fast as possible
|
receive_can(); // Receive CAN messages. Runs as fast as possible
|
||||||
#ifdef DUAL_CAN
|
#ifdef DUAL_CAN
|
||||||
receive_can2();
|
receive_can2();
|
||||||
#endif
|
#endif
|
||||||
#if defined(SERIAL_LINK_RECEIVER) || defined(SERIAL_LINK_TRANSMITTER)
|
#if defined(SERIAL_LINK_RECEIVER) || defined(SERIAL_LINK_TRANSMITTER)
|
||||||
runSerialDataLink();
|
runSerialDataLink();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Process
|
// Process
|
||||||
if (millis() - previousMillis10ms >= INTERVAL_10_MS) // Every 10ms
|
if (millis() - previousMillis10ms >= INTERVAL_10_MS) // Every 10ms
|
||||||
{
|
{
|
||||||
previousMillis10ms = millis();
|
previousMillis10ms = millis();
|
||||||
handle_LED_state(); // Set the LED color according to state
|
handle_LED_state(); // Set the LED color according to state
|
||||||
#ifdef CONTACTOR_CONTROL
|
#ifdef CONTACTOR_CONTROL
|
||||||
handle_contactors(); // Take care of startup precharge/contactor closing
|
handle_contactors(); // Take care of startup precharge/contactor closing
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
|
|
||||||
if (millis() - previousMillisUpdateVal >= intervalUpdateValues) // Every 5s normally
|
|
||||||
{
|
|
||||||
previousMillisUpdateVal = millis();
|
|
||||||
update_SOC(); // Check if real or calculated SOC% value should be sent
|
|
||||||
update_values(); // Update values heading towards inverter. Prepare for sending on CAN, or write directly to Modbus.
|
|
||||||
if (DUMMY_EVENT_ENABLED) {
|
|
||||||
set_event(EVENT_DUMMY_ERROR, (uint8_t)millis());
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Output
|
if (millis() - previousMillisUpdateVal >= intervalUpdateValues) // Every 5s normally
|
||||||
send_can(); // Send CAN messages
|
{
|
||||||
|
previousMillisUpdateVal = millis();
|
||||||
|
update_SOC(); // Check if real or calculated SOC% value should be sent
|
||||||
|
update_values(); // Update values heading towards inverter. Prepare for sending on CAN, or write directly to Modbus.
|
||||||
|
if (DUMMY_EVENT_ENABLED) {
|
||||||
|
set_event(EVENT_DUMMY_ERROR, (uint8_t)millis());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output
|
||||||
|
send_can(); // Send CAN messages
|
||||||
#ifdef DUAL_CAN
|
#ifdef DUAL_CAN
|
||||||
send_can2();
|
send_can2();
|
||||||
#endif
|
#endif
|
||||||
run_event_handling();
|
run_event_handling();
|
||||||
|
|
||||||
if (digitalRead(0) == HIGH) {
|
if (digitalRead(0) == HIGH) {
|
||||||
test_all_colors = false;
|
test_all_colors = false;
|
||||||
} else {
|
} else {
|
||||||
test_all_colors = true;
|
test_all_colors = true;
|
||||||
|
}
|
||||||
|
delay(2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -347,7 +364,7 @@ void init_modbus() {
|
||||||
MBserver.registerWorker(MBTCP_ID, WRITE_MULT_REGISTERS, &FC16);
|
MBserver.registerWorker(MBTCP_ID, WRITE_MULT_REGISTERS, &FC16);
|
||||||
MBserver.registerWorker(MBTCP_ID, R_W_MULT_REGISTERS, &FC23);
|
MBserver.registerWorker(MBTCP_ID, R_W_MULT_REGISTERS, &FC23);
|
||||||
// Start ModbusRTU background task
|
// Start ModbusRTU background task
|
||||||
MBserver.begin(Serial2);
|
MBserver.begin(Serial2, 0);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue