mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-05 02:39:57 +02:00
Latest fixes from Andy
This commit is contained in:
parent
6fd2ca52db
commit
744db66f49
10 changed files with 97 additions and 45 deletions
|
@ -131,7 +131,7 @@ void loop() {
|
|||
#ifdef DUAL_CAN
|
||||
receive_can2();
|
||||
#endif
|
||||
#ifdef SERIAL_LINK_TRANSMITTER_INVERTER
|
||||
#ifdef SERIAL_LINK_RECEIVER
|
||||
receive_serial();
|
||||
#endif
|
||||
|
||||
|
@ -156,7 +156,7 @@ void loop() {
|
|||
#ifdef DUAL_CAN
|
||||
send_can2();
|
||||
#endif
|
||||
#ifdef SERIAL_LINK_RECEIVER_FROM_BATTERY
|
||||
#ifdef SERIAL_LINK_TRANSMITTER
|
||||
send_serial();
|
||||
#endif
|
||||
}
|
||||
|
@ -226,8 +226,8 @@ void init_modbus() {
|
|||
pinMode(PIN_5V_EN, OUTPUT);
|
||||
digitalWrite(PIN_5V_EN, HIGH);
|
||||
|
||||
#if defined(SERIAL_LINK_RECEIVER_FROM_BATTERY) || defined(SERIAL_LINK_TRANSMITTER_INVERTER)
|
||||
Serial2.begin(9600); // If the Modbus RTU port will be used for serial link
|
||||
#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
|
||||
|
@ -399,20 +399,22 @@ void send_can() {
|
|||
#endif
|
||||
}
|
||||
|
||||
#ifdef SERIAL_LINK_RECEIVER_FROM_BATTERY
|
||||
void send_serial() {
|
||||
#ifdef SERIAL_LINK_RECEIVER
|
||||
//---- Receives serial data and transfers to the Inverter
|
||||
void receive_serial() {
|
||||
static unsigned long currentMillis = millis();
|
||||
if (currentMillis - previousMillis1ms >= interval1) {
|
||||
if (currentMillis - previousMillis1ms > interval1) { //--- try 2 second
|
||||
previousMillis1ms = currentMillis;
|
||||
manageSerialLinkReceiver();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SERIAL_LINK_TRANSMITTER_INVERTER
|
||||
void receive_serial() {
|
||||
#ifdef SERIAL_LINK_TRANSMITTER
|
||||
//---- Gets data from Battery and serial Transmits the data to the Receiver
|
||||
void send_serial() {
|
||||
static unsigned long currentMillis = millis();
|
||||
if (currentMillis - previousMillis1ms >= interval1) {
|
||||
if (currentMillis - previousMillis1ms > interval1) { //--- try 2 second
|
||||
previousMillis1ms = currentMillis;
|
||||
manageSerialLinkTransmitter();
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@
|
|||
//#define CONTACTOR_CONTROL //Enable this line to have pins 25,32,33 handle automatic precharge/contactor+/contactor- closing sequence
|
||||
//#define PWM_CONTACTOR_CONTROL //Enable this line to use PWM logic for contactors, which lower power consumption and heat generation
|
||||
//#define DUAL_CAN //Enable this line to activate an isolated secondary CAN Bus using add-on MCP2515 controller (Needed for FoxESS inverters)
|
||||
//#define SERIAL_LINK_RECEIVER_FROM_BATTERY //Enable this line to send battery data over Modbus pins to another Lilygo (This LilyGo interfaces with battery)
|
||||
//#define SERIAL_LINK_TRANSMITTER_INVERTER //Enable this line to receive battery data over Modbus pins from another Lilygo (This LilyGo interfaces with inverter)
|
||||
//#define SERIAL_LINK_RECEIVER //Enable this line to receive battery data over RS485 pins from another Lilygo (This LilyGo interfaces with inverter)
|
||||
//#define SERIAL_LINK_TRANSMITTER //Enable this line to send battery data over RS485 pins to another Lilygo (This LilyGo interfaces with battery)
|
||||
|
||||
#endif
|
||||
|
|
|
@ -33,7 +33,7 @@
|
|||
#include "TEST-FAKE-BATTERY.h" //See this file for more Fake battery settings
|
||||
#endif
|
||||
|
||||
#ifdef SERIAL_LINK_RECEIVER_FROM_BATTERY
|
||||
#ifdef SERIAL_LINK_RECEIVER
|
||||
#include "SERIAL-LINK-RECEIVER-FROM-BATTERY.h"
|
||||
#endif
|
||||
|
||||
|
|
|
@ -50,6 +50,7 @@ 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;
|
||||
|
@ -67,10 +68,8 @@ void manageSerialLinkReceiver() {
|
|||
}
|
||||
dataLinkReceive.run();
|
||||
bool readError = dataLinkReceive.checkReadError(true); // check for error & clear error flag
|
||||
LEDcolor = GREEN;
|
||||
|
||||
if (readError) {
|
||||
LEDcolor = RED;
|
||||
bms_status = 4; //FAULT
|
||||
Serial.print(currentTime);
|
||||
Serial.println(" - ERROR: Serial Data Link - Read Error");
|
||||
lasterror = true;
|
||||
|
@ -80,13 +79,13 @@ void manageSerialLinkReceiver() {
|
|||
Serial.print(currentTime);
|
||||
Serial.println(" - RECOVERY: Serial Data Link - Read GOOD");
|
||||
}
|
||||
lastGood = currentTime;
|
||||
}
|
||||
if (dataLinkReceive.checkNewData(true)) // true = clear Flag
|
||||
{
|
||||
__getData();
|
||||
lastGoodMaxCharge = max_target_charge_power;
|
||||
lastGoodMaxDischarge = max_target_discharge_power;
|
||||
lastGood = currentTime;
|
||||
}
|
||||
|
||||
unsigned long minutesLost = (currentTime - lastGood) / 60000UL;
|
||||
|
@ -99,6 +98,20 @@ void manageSerialLinkReceiver() {
|
|||
} else {
|
||||
max_target_charge_power = 0;
|
||||
max_target_discharge_power = 0;
|
||||
bms_status = 4; //Fault state
|
||||
LEDcolor = RED;
|
||||
//----- Throw Error
|
||||
}
|
||||
// report Lost data & Max charge / Discharge reductions
|
||||
if (minutesLost != last_minutesLost) {
|
||||
last_minutesLost = minutesLost;
|
||||
Serial.print(currentTime);
|
||||
Serial.print(" - Minutes without data : ");
|
||||
Serial.print(minutesLost);
|
||||
Serial.print(", max Charge = ");
|
||||
Serial.print(max_target_charge_power);
|
||||
Serial.print(", max Discharge = ");
|
||||
Serial.println(max_target_discharge_power);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -30,8 +30,8 @@ extern uint16_t temperature_min; //C+1, Goes thru convert2unsignedint16 funct
|
|||
extern uint16_t temperature_max; //C+1, Goes thru convert2unsignedint16 function (15.0C = 150, -15.0C = 65385)
|
||||
extern uint16_t cell_max_voltage; //mV, 0-4350
|
||||
extern uint16_t cell_min_voltage; //mV, 0-4350
|
||||
extern uint8_t LEDcolor; //Enum, 0-10
|
||||
extern bool batteryAllowsContactorClosing; //Bool, 1=true, 0=false
|
||||
extern uint8_t LEDcolor; //Enum, 0-10
|
||||
|
||||
void manageSerialLinkReceiver();
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@
|
|||
#include "SOLAX-CAN.h"
|
||||
#endif
|
||||
|
||||
#ifdef SERIAL_LINK_TRANSMITTER_INVERTER
|
||||
#ifdef SERIAL_LINK_TRANSMITTER
|
||||
#include "SERIAL-LINK-TRANSMITTER-INVERTER.h"
|
||||
#endif
|
||||
|
||||
|
|
|
@ -32,6 +32,10 @@ void manageSerialLinkTransmitter() {
|
|||
static bool initLink = false;
|
||||
static unsigned long updateTime = 0;
|
||||
static bool lasterror = false;
|
||||
static unsigned long lastNoError = 0;
|
||||
static unsigned long transmitGoodSince = 0;
|
||||
|
||||
unsigned long currentTime = millis();
|
||||
|
||||
dataLinkTransmit.run();
|
||||
|
||||
|
@ -43,28 +47,56 @@ void manageSerialLinkTransmitter() {
|
|||
}
|
||||
#endif
|
||||
|
||||
if (millis() - updateTime > 100) {
|
||||
updateTime = millis();
|
||||
if (currentTime - updateTime > 100) {
|
||||
updateTime = currentTime;
|
||||
if (!initLink) {
|
||||
initLink = true;
|
||||
// sends variables every 5000mS even if no change
|
||||
dataLinkTransmit.setUpdateInterval(5000);
|
||||
}
|
||||
bool sendError = dataLinkTransmit.checkTransmissionError(true);
|
||||
LEDcolor = GREEN;
|
||||
if (sendError) {
|
||||
LEDcolor = RED;
|
||||
Serial.print(millis());
|
||||
Serial.print(currentTime);
|
||||
Serial.println(" - ERROR: Serial Data Link - SEND Error");
|
||||
lasterror = true;
|
||||
transmitGoodSince = currentTime;
|
||||
} else {
|
||||
if (lasterror) {
|
||||
lasterror = false;
|
||||
Serial.print(millis());
|
||||
Serial.print(currentTime);
|
||||
Serial.println(" - RECOVERY: Serial Data Link - Send GOOD");
|
||||
}
|
||||
lastNoError = currentTime;
|
||||
}
|
||||
|
||||
//--- reporting every 60 seconds that transmission is good
|
||||
if (currentTime - transmitGoodSince > 60000) {
|
||||
transmitGoodSince = currentTime;
|
||||
Serial.print(currentTime);
|
||||
Serial.println(" - Transmit Good");
|
||||
}
|
||||
|
||||
//--- report that Errors been ocurring for > 60 seconds
|
||||
if (currentTime - lastNoError > 60000) // 60 seconds
|
||||
{
|
||||
Serial.print(currentTime);
|
||||
Serial.println(" - Transmit Failed : 60 seconds");
|
||||
bms_status = 4; //FAULT
|
||||
LEDcolor = RED;
|
||||
// throw error
|
||||
}
|
||||
/*
|
||||
// lastMessageReceived from CAN bus (Battery)
|
||||
if (currentTime - lastMessageReceived > (5 * 60000) ) // 5 minutes
|
||||
{
|
||||
Serial.print(millis());
|
||||
Serial.println(" - Data Stale : 5 minutes");
|
||||
// throw error
|
||||
|
||||
// stop transmitting until fresh
|
||||
}
|
||||
*/
|
||||
|
||||
dataLinkTransmit.updateData(0, SOC);
|
||||
dataLinkTransmit.updateData(1, StateOfHealth);
|
||||
dataLinkTransmit.updateData(2, battery_voltage);
|
||||
|
|
|
@ -24,8 +24,8 @@ extern uint16_t temperature_min; //C+1, Goes thru convert2unsignedint16 funct
|
|||
extern uint16_t temperature_max; //C+1, Goes thru convert2unsignedint16 function (15.0C = 150, -15.0C = 65385)
|
||||
extern uint16_t cell_max_voltage; //mV, 0-4350
|
||||
extern uint16_t cell_min_voltage; //mV, 0-4350
|
||||
extern uint8_t LEDcolor; //Enum, 0-10
|
||||
extern bool batteryAllowsContactorClosing; //Bool, 1=true, 0=false
|
||||
extern uint8_t LEDcolor; //Enum, 0-10
|
||||
|
||||
void manageSerialLinkTransmitter();
|
||||
|
||||
|
|
|
@ -49,9 +49,12 @@ union Convert
|
|||
};
|
||||
}convert;
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
#define SET_PA6() (GPIOA->BSRR = GPIO_BSRR_BS6)
|
||||
#define CLEAR_PA6() (GPIOA->BSRR = GPIO_BSRR_BR6)
|
||||
// Macro to toggle PA6
|
||||
#define TOGGLE_PA6() (GPIOA->ODR ^= GPIO_ODR_ODR6)
|
||||
*/
|
||||
|
||||
// Constructor
|
||||
SerialDataLink::SerialDataLink(Stream &serial, uint8_t transmitID, uint8_t receiveID, uint8_t maxIndexTX, uint8_t maxIndexRX, bool enableRetransmit)
|
||||
|
@ -167,10 +170,9 @@ void SerialDataLink::run()
|
|||
}
|
||||
else
|
||||
{
|
||||
|
||||
constructPacket(); // Construct a new packet if not currently transmitting
|
||||
|
||||
if (muteAcknowledgement)
|
||||
if (muteAcknowledgement && (needToACK || needToNACK))
|
||||
{
|
||||
needToACK = false;
|
||||
needToNACK = false;
|
||||
|
@ -269,13 +271,11 @@ void SerialDataLink::constructPacket()
|
|||
{
|
||||
lastTransmissionTime = millis();
|
||||
txBufferIndex = 0; // Reset the TX buffer index
|
||||
|
||||
addToTxBuffer(headerChar);
|
||||
addToTxBuffer(transmitID);
|
||||
addToTxBuffer(0); // EOT position - place holder
|
||||
unsigned long currentTime = millis();
|
||||
int count = txBufferIndex;
|
||||
|
||||
for (uint8_t i = 0; i < maxIndexTX; i++)
|
||||
{
|
||||
if (dataUpdated[i] || (currentTime - lastSent[i] >= updateInterval))
|
||||
|
@ -352,6 +352,7 @@ bool SerialDataLink::ackReceived()
|
|||
if (nextByte == headerChar)
|
||||
{
|
||||
requestToSend = true;
|
||||
transmissionError = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -372,12 +373,11 @@ bool SerialDataLink::ackReceived()
|
|||
requestToSend = true;
|
||||
case NACK_CODE:
|
||||
transmissionError = true;
|
||||
return false;
|
||||
return true;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return false; // No ACK, NACK, or new packet received
|
||||
|
@ -386,7 +386,9 @@ bool SerialDataLink::ackReceived()
|
|||
bool SerialDataLink::ackTimeout()
|
||||
{
|
||||
// Check if the current time has exceeded the last transmission time by the ACK timeout period
|
||||
if (millis() - lastTransmissionTime > ACK_TIMEOUT) {
|
||||
|
||||
if (millis() - lastTransmissionTime > ACK_TIMEOUT)
|
||||
{
|
||||
return true; // Timeout occurred
|
||||
}
|
||||
return false; // No timeout
|
||||
|
@ -397,9 +399,10 @@ bool SerialDataLink::ackTimeout()
|
|||
void SerialDataLink::read()
|
||||
{
|
||||
if (maxIndexRX < 1) return;
|
||||
if (serial.available())
|
||||
int count = 0;
|
||||
while (serial.available() && count < 10)
|
||||
{
|
||||
//Serial.print(".");
|
||||
count++;
|
||||
if (millis() - lastHeaderTime > PACKET_TIMEOUT && rxBufferIndex > 0)
|
||||
{
|
||||
// Timeout occurred, reset buffer and pointer
|
||||
|
|
|
@ -50,6 +50,8 @@
|
|||
|
||||
#include <Arduino.h>
|
||||
|
||||
|
||||
|
||||
class SerialDataLink {
|
||||
public:
|
||||
// Constructor
|
||||
|
@ -128,12 +130,12 @@ private:
|
|||
bool dataUpdated[dataArraySizeTX];
|
||||
unsigned long lastSent[dataArraySizeTX];
|
||||
|
||||
unsigned long updateInterval = 500;
|
||||
unsigned long ACK_TIMEOUT = 100;
|
||||
unsigned long PACKET_TIMEOUT = 100; // Timeout in milliseconds
|
||||
unsigned long updateInterval = 1000;
|
||||
unsigned long ACK_TIMEOUT = 200;
|
||||
unsigned long PACKET_TIMEOUT = 200; // Timeout in milliseconds
|
||||
|
||||
unsigned long lastStateChangeTime = 0;
|
||||
unsigned long stateChangeTimeout = 200;
|
||||
unsigned long stateChangeTimeout = 300;
|
||||
|
||||
// Special characters for packet framing
|
||||
char headerChar = '<';
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue