Skip to content

Commit

Permalink
Final fixes from andy
Browse files Browse the repository at this point in the history
  • Loading branch information
dalathegreat committed Dec 17, 2023
1 parent ca21dae commit 90bb3fe
Show file tree
Hide file tree
Showing 7 changed files with 429 additions and 235 deletions.
103 changes: 48 additions & 55 deletions Software/Software.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
#include "src/lib/miwagner-ESP32-Arduino-CAN/CAN_config.h"
#include "src/lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"

#ifdef WEBSERVER
#include "src/devboard/webserver/webserver.h"
#endif

// Interval settings
int intervalUpdateValues = 4800; // Interval at which to update inverter values / Modbus registers
const int interval1 = 1; // Interval for 1ms tasks
const int interval10 = 10; // Interval for 10ms tasks
unsigned long previousMillis1ms = 0;
unsigned long previousMillis10ms = 50;
unsigned long previousMillisUpdateVal = 0;

Expand All @@ -38,7 +40,7 @@ static ACAN2515_Buffer16 gBuffer;
#define MB_RTU_NUM_VALUES 30000
#endif
#if defined(LUNA2000_MODBUS)
#define MB_RTU_NUM_VALUES 50000
#define MB_RTU_NUM_VALUES 30000
#endif
#if defined(BYD_MODBUS) || defined(LUNA2000_MODBUS)
uint16_t mbPV[MB_RTU_NUM_VALUES]; // Process variable memory
Expand Down Expand Up @@ -75,6 +77,7 @@ uint16_t bms_status = ACTIVE; // ACTIVE - [0..5]<>[STANDBY,INACTIVE,DARKSTA
uint16_t stat_batt_power = 0; // Power going in/out of battery
uint16_t cell_max_voltage = 3700; // Stores the highest cell voltage value in the system
uint16_t cell_min_voltage = 3700; // Stores the minimum cell voltage value in the system
bool LFP_Chemistry = false;

// LED parameters
Adafruit_NeoPixel pixels(1, WS2812_PIN, NEO_GRB + NEO_KHZ800);
Expand Down Expand Up @@ -104,19 +107,24 @@ unsigned long negativeStartTime = 0;
unsigned long timeSpentInFaultedMode = 0;
#endif
bool batteryAllowsContactorClosing = false;
bool inverterAllowsContactorClosing = false;
bool inverterAllowsContactorClosing = true;

// Initialization
void setup() {
init_serial();

#ifdef WEBSERVER
init_webserver();
#endif

init_CAN();

init_LED();

init_contactors();

init_modbus();
init_serialDataLink();

inform_user_on_inverter();

Expand All @@ -125,14 +133,18 @@ void setup() {

// Perform main program functions
void loop() {

runSerialDataLink();
#ifdef WEBSERVER
// Over-the-air updates by ElegantOTA
ElegantOTA.loop();
#endif

// Input
receive_can(); // Receive CAN messages. Runs as fast as possible
#ifdef DUAL_CAN
receive_can2();
#endif
#ifdef SERIAL_LINK_RECEIVER
receive_serial();
#endif

// Process
if (millis() - previousMillis10ms >= interval10) // Every 10ms
Expand All @@ -155,9 +167,6 @@ void loop() {
#ifdef DUAL_CAN
send_can2();
#endif
#ifdef SERIAL_LINK_TRANSMITTER
send_serial();
#endif
}

// Initialization functions
Expand Down Expand Up @@ -225,13 +234,6 @@ void init_modbus() {
pinMode(PIN_5V_EN, OUTPUT);
digitalWrite(PIN_5V_EN, HIGH);

#if defined(SERIAL_LINK_RECEIVER) || defined(SERIAL_LINK_TRANSMITTER)
Serial2.begin(9600, SERIAL_8N1, RS485_RX_PIN, RS485_TX_PIN); // If the Modbus RTU port will be used for serial link
#if defined(BYD_MODBUS) || defined(LUNA2000_MODBUS)
#error Modbus pins cannot be used for Serial and Modbus at the same time!
#endif
#endif

#ifdef BYD_MODBUS
// Init Static data to the RTU Modbus
handle_static_data_modbus_byd();
Expand All @@ -254,36 +256,24 @@ void inform_user_on_inverter() {
// Inform user what Inverter is used
#ifdef BYD_CAN
Serial.println("BYD CAN protocol selected");
bool inverterAllowsContactorClosing =
true; // The inverter does not care when contactors are actuated, OK to start with them ON
#endif
#ifdef BYD_MODBUS
Serial.println("BYD Modbus RTU protocol selected");
bool inverterAllowsContactorClosing =
true; // The inverter does not care when contactors are actuated, OK to start with them ON
#endif
#ifdef LUNA2000_MODBUS
Serial.println("Luna2000 Modbus RTU protocol selected");
bool inverterAllowsContactorClosing =
true; // The inverter does not care when contactors are actuated, OK to start with them ON
#endif
#ifdef PYLON_CAN
Serial.println("PYLON CAN protocol selected");
bool inverterAllowsContactorClosing =
true; // The inverter does not care when contactors are actuated, OK to start with them ON
#endif
#ifdef SMA_CAN
Serial.println("SMA CAN protocol selected");
bool inverterAllowsContactorClosing =
true; // The inverter does not care when contactors are actuated, OK to start with them ON
#endif
#ifdef SOFAR_CAN
Serial.println("SOFAR CAN protocol selected");
bool inverterAllowsContactorClosing =
true; // The inverter does not care when contactors are actuated, OK to start with them ON
#endif
#ifdef SOLAX_CAN
inverterAllowsContactorClosing = false; // The inverter needs to allow first on this protocol!
inverterAllowsContactorClosing = false; // The inverter needs to allow first on this protocol
intervalUpdateValues = 800; // This protocol also requires the values to be updated faster
Serial.println("SOLAX CAN protocol selected");
#endif
Expand Down Expand Up @@ -315,6 +305,9 @@ void inform_user_on_battery() {
#ifdef TEST_FAKE_BATTERY
Serial.println("Test mode with fake battery selected");
#endif
#ifdef SERIAL_LINK_RECEIVER
Serial.println("SERIAL_DATA_LINK_RECEIVER selected");
#endif
#if !defined(ABSOLUTE_MAX_VOLTAGE)
#error No battery selected! Choose one from the USER_SETTINGS.h file
#endif
Expand Down Expand Up @@ -413,30 +406,6 @@ void send_can() {
#endif
}

#ifdef SERIAL_LINK_RECEIVER
//---- Receives serial data and transfers to the Inverter
void receive_serial() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis1ms >= interval1) { //--- try 2 second
previousMillis1ms = currentMillis;
manageSerialLinkReceiver();
}
}
#endif

#ifdef SERIAL_LINK_TRANSMITTER
//---- Gets data from Battery and serial Transmits the data to the Receiver
void send_serial() {
unsigned long currentMillis = millis();
if (bms_status == ACTIVE) {
if (currentMillis - previousMillis1ms >= interval1) { //--- try 2 second
previousMillis1ms = currentMillis;
manageSerialLinkTransmitter();
}
}
}
#endif

#ifdef DUAL_CAN
void receive_can2() { // This function is similar to receive_can, but just takes care of inverters in the 2nd bus.
// Depending on which inverter is selected, we forward this to their respective CAN routines
Expand Down Expand Up @@ -647,10 +616,34 @@ void update_values() {
#ifdef SMA_CAN
update_values_can_sma();
#endif
#ifdef SOFAR_CAN
update_values_can_sofar();
#endif
#ifdef SOLAX_CAN
update_values_can_solax();
#endif
}

void runSerialDataLink() {
static unsigned long sdlTimer = 0;
unsigned long currentMillis = millis();
#ifdef SERIAL_LINK_RECEIVER
update_values_serial_link();
if (currentMillis - sdlTimer >= 1) { //--- try 2 second
sdlTimer = currentMillis;
manageSerialLinkReceiver();
}
#endif

#ifdef SERIAL_LINK_TRANSMITTER
if (currentMillis - sdlTimer >= 1) { //--- try 2 second
sdlTimer = currentMillis;
manageSerialLinkTransmitter();
}
#endif
}

void init_serialDataLink() {
#if defined(SERIAL_LINK_RECEIVER) || defined(SERIAL_LINK_TRANSMITTER)
Serial2.begin(9600, SERIAL_8N1, RS485_RX_PIN, RS485_TX_PIN);
#endif
}
81 changes: 67 additions & 14 deletions Software/src/battery/SERIAL-LINK-RECEIVER-FROM-BATTERY.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,22 @@ const uint8_t sendingNumVariables = INVERTER_SEND_NUM_VARIABLES;
const uint8_t sendingNumVariables = 0;
#endif

// txid,rxid, num_send,num_recv
SerialDataLink dataLinkReceive(Serial2, 0, 0x01, sendingNumVariables,
#ifdef TESTBENCH
// In the testbench environment, the receiver uses Serial3
#define SerialReceiver Serial3
#else
// In the production environment, the receiver uses Serial2
#define SerialReceiver Serial2
#endif

#define REPORT_SDL_DATA 1

// txid,rxid, num_send,num_recv
SerialDataLink dataLinkReceive(SerialReceiver, 0, 0x01, sendingNumVariables,
INVERTER_RECV_NUM_VARIABLES); // ...

static bool batteryFault = false; // used locally - mainly to indicate Battery CAN failure

void __getData() {
SOC = (uint16_t)dataLinkReceive.getReceivedData(0);
StateOfHealth = (uint16_t)dataLinkReceive.getReceivedData(1);
Expand All @@ -24,14 +36,20 @@ void __getData() {
remaining_capacity_Wh = (uint16_t)dataLinkReceive.getReceivedData(5);
max_target_discharge_power = (uint16_t)dataLinkReceive.getReceivedData(6);
max_target_charge_power = (uint16_t)dataLinkReceive.getReceivedData(7);
bms_status = (uint16_t)dataLinkReceive.getReceivedData(8);
uint16_t _bms_status = (uint16_t)dataLinkReceive.getReceivedData(8);
bms_status = _bms_status;
bms_char_dis_status = (uint16_t)dataLinkReceive.getReceivedData(9);
stat_batt_power = (uint16_t)dataLinkReceive.getReceivedData(10);
temperature_min = (uint16_t)dataLinkReceive.getReceivedData(11);
temperature_max = (uint16_t)dataLinkReceive.getReceivedData(12);
cell_max_voltage = (uint16_t)dataLinkReceive.getReceivedData(13);
cell_min_voltage = (uint16_t)dataLinkReceive.getReceivedData(14);
batteryAllowsContactorClosing = (uint16_t)dataLinkReceive.getReceivedData(15);
LFP_Chemistry = (bool)dataLinkReceive.getReceivedData(15);
batteryAllowsContactorClosing = (uint16_t)dataLinkReceive.getReceivedData(16);

batteryFault = false;
if (_bms_status == FAULT)
batteryFault = true;
}

void updateData() {
Expand All @@ -47,13 +65,16 @@ void updateData() {
*/

void manageSerialLinkReceiver() {

static bool lasterror = false;
static unsigned long last_minutesLost = 0;
static unsigned long lastGood;
static uint16_t lastGoodMaxCharge;
static uint16_t lastGoodMaxDischarge;
static bool initLink = false;

static unsigned long reportTime = 0;
static uint16_t reads = 0;
static uint16_t errors = 0;
unsigned long currentTime = millis();

if (!initLink) {
Expand All @@ -69,21 +90,26 @@ void manageSerialLinkReceiver() {

if (readError) {
Serial.print(currentTime);
Serial.println(" - ERROR: Serial Data Link - Read Error");
Serial.println(" - ERROR: SerialDataLink - Read Error");
lasterror = true;
} else {
if (lasterror) {
lasterror = false;
Serial.print(currentTime);
Serial.println(" - RECOVERY: Serial Data Link - Read GOOD");
}
errors++;
}

if (dataLinkReceive.checkNewData(true)) // true = clear Flag
{
__getData();
reads++;
lastGoodMaxCharge = max_target_charge_power;
lastGoodMaxDischarge = max_target_discharge_power;
lastGood = currentTime;
//--- if BatteryFault then assume Data is stale
if (!batteryFault)
lastGood = currentTime;
//bms_status = ACTIVE; // just testing
if (lasterror) {
lasterror = false;
Serial.print(currentTime);
Serial.println(" - RECOVERY: SerialDataLink - Read GOOD");
}
}

unsigned long minutesLost = (currentTime - lastGood) / 60000UL;
Expand All @@ -93,6 +119,7 @@ void manageSerialLinkReceiver() {
max_target_charge_power = (lastGoodMaxCharge * (4 - minutesLost)) / 4;
max_target_discharge_power = (lastGoodMaxDischarge * (4 - minutesLost)) / 4;
} else {
// Times Up -
max_target_charge_power = 0;
max_target_discharge_power = 0;
bms_status = 4; //Fault state
Expand All @@ -103,7 +130,11 @@ void manageSerialLinkReceiver() {
if (minutesLost != last_minutesLost) {
last_minutesLost = minutesLost;
Serial.print(currentTime);
Serial.print(" - Minutes without data : ");
if (batteryFault) {
Serial.print("Battery Fault (minutes) : ");
} else {
Serial.print(" - Minutes without data : ");
}
Serial.print(minutesLost);
Serial.print(", max Charge = ");
Serial.print(max_target_charge_power);
Expand All @@ -112,6 +143,24 @@ void manageSerialLinkReceiver() {
}
}

if (currentTime - reportTime > 59999) {
reportTime = currentTime;
Serial.print(currentTime);
Serial.print(" SerialDataLink-Receiver - NewData :");
Serial.print(reads);
Serial.print(" Errors : ");
Serial.println(errors);
reads = 0;
errors = 0;

// --- printUsefullData();
//Serial.print("SOC = ");
//Serial.println(SOC);
#ifdef REPORT_SDL_DATA
update_values_serial_link();
#endif
}

static unsigned long updateTime = 0;

#ifdef INVERTER_SEND_NUM_VARIABLES
Expand Down Expand Up @@ -156,8 +205,12 @@ void update_values_serial_link() {
Serial.print(cell_max_voltage);
Serial.print(" Cell min: ");
Serial.print(cell_min_voltage);
Serial.print(" LFP : ");
Serial.print(LFP_Chemistry);
Serial.print(" batteryAllowsContactorClosing: ");
Serial.print(batteryAllowsContactorClosing);
Serial.print(" inverterAllowsContactorClosing: ");
Serial.print(inverterAllowsContactorClosing);

Serial.println("");
}
Loading

0 comments on commit 90bb3fe

Please sign in to comment.