Skip to content

Commit

Permalink
Latest fixes from Andy
Browse files Browse the repository at this point in the history
  • Loading branch information
dalathegreat committed Nov 23, 2023
1 parent 6fd2ca5 commit 744db66
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 45 deletions.
22 changes: 12 additions & 10 deletions Software/Software.ino
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void loop() {
#ifdef DUAL_CAN
receive_can2();
#endif
#ifdef SERIAL_LINK_TRANSMITTER_INVERTER
#ifdef SERIAL_LINK_RECEIVER
receive_serial();
#endif

Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions Software/USER_SETTINGS.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion Software/src/battery/BATTERIES.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 17 additions & 4 deletions Software/src/battery/SERIAL-LINK-RECEIVER-FROM-BATTERY.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Software/src/battery/SERIAL-LINK-RECEIVER-FROM-BATTERY.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion Software/src/inverter/INVERTERS.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
44 changes: 38 additions & 6 deletions Software/src/inverter/SERIAL-LINK-TRANSMITTER-INVERTER.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -43,27 +47,55 @@ 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);
Expand Down
2 changes: 1 addition & 1 deletion Software/src/inverter/SERIAL-LINK-TRANSMITTER-INVERTER.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
33 changes: 18 additions & 15 deletions Software/src/lib/mackelec-SerialDataLink/SerialDataLink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand All @@ -289,13 +289,13 @@ void SerialDataLink::constructPacket()
lastSent[i] = currentTime; // Update the last sent time for this index
}
}

if (count == txBufferIndex)
{
// No data was added to the buffer, so no need to send a packet
return;
}

addToTxBuffer(eotChar);
//----- assign EOT position
txBuffer[2] = txBufferIndex - 1;
Expand Down Expand Up @@ -352,6 +352,7 @@ bool SerialDataLink::ackReceived()
if (nextByte == headerChar)
{
requestToSend = true;
transmissionError = true;
return false;
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
10 changes: 6 additions & 4 deletions Software/src/lib/mackelec-SerialDataLink/SerialDataLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@

#include <Arduino.h>



class SerialDataLink {
public:
// Constructor
Expand Down Expand Up @@ -128,12 +130,12 @@ class SerialDataLink {
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 = '<';
Expand Down

0 comments on commit 744db66

Please sign in to comment.