Skip to content

Commit

Permalink
Add fixes from AndyMack-e-
Browse files Browse the repository at this point in the history
  • Loading branch information
dalathegreat committed Nov 21, 2023
1 parent 3379cba commit 0bfb757
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 150 deletions.
4 changes: 4 additions & 0 deletions Software/Software.ino
Original file line number Diff line number Diff line change
Expand Up @@ -399,21 +399,25 @@ void send_can() {
#endif
}

#ifdef SERIAL_LINK_RECEIVER_FROM_BATTERY
void send_serial() {
static unsigned long currentMillis = millis();
if (currentMillis - previousMillis1ms >= interval1) {
previousMillis1ms = currentMillis;
manageSerialLinkReceiver();
}
}
#endif

#ifdef SERIAL_LINK_TRANSMITTER_INVERTER
void receive_serial() {
static unsigned long currentMillis = millis();
if (currentMillis - previousMillis1ms >= interval1) {
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.
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
#define SERIAL_LINK_TRANSMITTER_INVERTER //Enable this line to receive battery data over Modbus pins from another Lilygo
//#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)

#endif
114 changes: 54 additions & 60 deletions Software/src/battery/SERIAL-LINK-RECEIVER-FROM-BATTERY.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,79 @@

#include "SERIAL-LINK-RECEIVER-FROM-BATTERY.h"

#define INVERTER_SEND_NUM_VARIABLES 3 //--- comment out if nothing to send
#define INVERTER_RECV_NUM_VARIABLES 16
//#define INVERTER_SEND_NUM_VARIABLES 3 //--- comment out if nothing to send
#define INVERTER_RECV_NUM_VARIABLES 16

#ifdef INVERTER_SEND_NUM_VARIABLES
const uint8_t sendingNumVariables = INVERTER_SEND_NUM_VARIABLES;
const uint8_t sendingNumVariables = INVERTER_SEND_NUM_VARIABLES;
#else
const uint8_t sendingNumVariables = 0;
const uint8_t sendingNumVariables = 0;
#endif

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




void __getData()
{
SOC = (uint16_t) dataLinkReceive.getReceivedData(0);
StateOfHealth = (uint16_t) dataLinkReceive.getReceivedData(1);
battery_voltage = (uint16_t) dataLinkReceive.getReceivedData(2);
battery_current = (uint16_t) dataLinkReceive.getReceivedData(3);
capacity_Wh = (uint16_t) dataLinkReceive.getReceivedData(4);
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);
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);
void __getData() {
SOC = (uint16_t)dataLinkReceive.getReceivedData(0);
StateOfHealth = (uint16_t)dataLinkReceive.getReceivedData(1);
battery_voltage = (uint16_t)dataLinkReceive.getReceivedData(2);
battery_current = (uint16_t)dataLinkReceive.getReceivedData(3);
capacity_Wh = (uint16_t)dataLinkReceive.getReceivedData(4);
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);
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);
}

void updateData()
{
// --- update with fresh data
/*
void updateData() {
// --- update with fresh data
/*
dataLinkReceive.updateData(0,var1);
dataLinkReceive.updateData(1,var2);
dataLinkReceive.updateData(2,var3);
*/
}


/*
* @ 9600bps, assume void manageSerialLinkReceiver()
* is called every 1mS
*/

void manageSerialLinkReceiver()
{
dataLinkReceive.run();
bool readError = dataLinkReceive.checkReadError(true); // check for error & clear error flag
if (dataLinkReceive.checkNewData(true)) // true = clear Flag
{
__getData();
}
#ifdef INVERTER_SEND_NUM_VARIABLES
static bool initLink=false;
static unsigned long updateTime = 0;
if (! initLink)
{
initLink = true;
// sends variables every 5000mS even if no change
dataLinkReceive.setUpdateInterval(5000);
}
unsigned long currentTime = millis();
if (currentTime - updateTime > 100)
{
updateTime = currentTime;
dataLinkReceive.run();
bool sendError = dataLinkReceive.checkTransmissionError(true); // check for error & clear error flag
updateData();
}
#endif
void manageSerialLinkReceiver() {
dataLinkReceive.run();
bool readError = dataLinkReceive.checkReadError(true); // check for error & clear error flag
LEDcolor = GREEN;
if (readError) {
LEDcolor = RED;
Serial.println("ERROR: Serial Data Link - Read Error");
}
if (dataLinkReceive.checkNewData(true)) // true = clear Flag
{
__getData();
}

#ifdef INVERTER_SEND_NUM_VARIABLES
static bool initLink = false;
static unsigned long updateTime = 0;
if (!initLink) {
initLink = true;
// sends variables every 5000mS even if no change
dataLinkReceive.setUpdateInterval(5000);
}
unsigned long currentTime = millis();
if (currentTime - updateTime > 100) {
updateTime = currentTime;
dataLinkReceive.run();
bool sendError = dataLinkReceive.checkTransmissionError(true); // check for error & clear error flag
updateData();
}
#endif
}
21 changes: 4 additions & 17 deletions Software/src/battery/SERIAL-LINK-RECEIVER-FROM-BATTERY.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

#include <Arduino.h>
#include "../../USER_SETTINGS.h"
#include "../lib/SerialDataLink/SerialDataLink.h"
#include "../devboard/config.h" // Needed for LED defines
#include "../lib/mackelec-SerialDataLink/SerialDataLink.h"

// https://github.com/mackelec/SerialDataLink

#define ABSOLUTE_MAX_VOLTAGE \
4040 // 404.4V,if battery voltage goes over this, charging is not possible (goes into forced discharge)
#define ABSOLUTE_MIN_VOLTAGE 3100 // 310.0V if battery voltage goes under this, discharging further is disabled


// These parameters need to be mapped for the inverter
extern uint16_t SOC; //SOC%, 0-100.00 (0-10000)
extern uint16_t StateOfHealth; //SOH%, 0-100.00 (0-10000)
Expand All @@ -31,21 +31,8 @@ extern uint16_t temperature_max; //C+1, Goes thru convert2unsignedint16 funct
extern uint16_t cell_max_voltage; //mV, 0-4350
extern uint16_t cell_min_voltage; //mV, 0-4350
extern bool batteryAllowsContactorClosing; //Bool, 1=true, 0=false





extern uint8_t LEDcolor; //Enum, 0-10

void manageSerialLinkReceiver();










#endif
#endif
110 changes: 53 additions & 57 deletions Software/src/inverter/SERIAL-LINK-TRANSMITTER-INVERTER.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,74 +8,70 @@
* Will transmit max 16 int variable - receive none
*/

#define BATTERY_SEND_NUM_VARIABLES 16
#define BATTERY_RECV_NUM_VARIABLES 3 //--- comment out if nothing to receive
#define BATTERY_SEND_NUM_VARIABLES 16
//#define BATTERY_RECV_NUM_VARIABLES 3 //--- comment out if nothing to receive

#ifdef BATTERY_RECV_NUM_VARIABLES
const uint8_t receivingNumVariables = BATTERY_RECV_NUM_VARIABLES;
const uint8_t receivingNumVariables = BATTERY_RECV_NUM_VARIABLES;
#else
const uint8_t receivingNumVariables = 0;
const uint8_t receivingNumVariables = 0;
#endif

// txid,rxid,num_tx,num_rx
SerialDataLink dataLinkTransmit(Serial2, 0x01, 0, BATTERY_SEND_NUM_VARIABLES,
receivingNumVariables );
SerialDataLink dataLinkTransmit(Serial2, 0x01, 0, BATTERY_SEND_NUM_VARIABLES, receivingNumVariables);


void _getData()
{
/*
void _getData() {
/*
var1 = dataLinkTransmit.getReceivedData(0);
var2 = dataLinkTransmit.getReceivedData(1);
var3 = dataLinkTransmit.getReceivedData(2);
*/
}

void manageSerialLinkTransmitter() {
static bool initLink = false;
static unsigned long updateTime = 0;

dataLinkTransmit.run();

void manageSerialLinkTransmitter()
{
static bool initLink=false;
static unsigned long updateTime = 0;

dataLinkTransmit.run();

#ifdef BATTERY_RECV_NUM_VARIABLES
bool readError = dataLinkTransmit.checkReadError(true); // check for error & clear error flag
if (dataLinkTransmit.checkNewData(true)) // true = clear Flag
{
_getData();
}
#endif

if (millis() - updateTime > 100)
{
updateTime = millis();
if (! initLink)
{
initLink = true;
// sends variables every 5000mS even if no change
dataLinkTransmit.setUpdateInterval(5000);
}
bool sendError = dataLinkTransmit.checkTransmissionError(true);
// todo some error management - LEDS etc

dataLinkTransmit.updateData(0,SOC);
dataLinkTransmit.updateData(1,StateOfHealth);
dataLinkTransmit.updateData(2,battery_voltage);
dataLinkTransmit.updateData(3,battery_current);
dataLinkTransmit.updateData(4,capacity_Wh);
dataLinkTransmit.updateData(5,remaining_capacity_Wh);
dataLinkTransmit.updateData(6,max_target_discharge_power);
dataLinkTransmit.updateData(7,max_target_charge_power);
dataLinkTransmit.updateData(8,bms_status);
dataLinkTransmit.updateData(9,bms_char_dis_status);
dataLinkTransmit.updateData(10,stat_batt_power);
dataLinkTransmit.updateData(11,temperature_min);
dataLinkTransmit.updateData(12,temperature_max);
dataLinkTransmit.updateData(13,cell_max_voltage);
dataLinkTransmit.updateData(14,cell_min_voltage);
dataLinkTransmit.updateData(15,batteryAllowsContactorClosing);
}


}
#ifdef BATTERY_RECV_NUM_VARIABLES
bool readError = dataLinkTransmit.checkReadError(true); // check for error & clear error flag
if (dataLinkTransmit.checkNewData(true)) // true = clear Flag
{
_getData();
}
#endif

if (millis() - updateTime > 100) {
updateTime = millis();
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.println("ERROR: Serial Data Link - SEND Error");
}
// todo some error management - LEDS etc

dataLinkTransmit.updateData(0, SOC);
dataLinkTransmit.updateData(1, StateOfHealth);
dataLinkTransmit.updateData(2, battery_voltage);
dataLinkTransmit.updateData(3, battery_current);
dataLinkTransmit.updateData(4, capacity_Wh);
dataLinkTransmit.updateData(5, remaining_capacity_Wh);
dataLinkTransmit.updateData(6, max_target_discharge_power);
dataLinkTransmit.updateData(7, max_target_charge_power);
dataLinkTransmit.updateData(8, bms_status);
dataLinkTransmit.updateData(9, bms_char_dis_status);
dataLinkTransmit.updateData(10, stat_batt_power);
dataLinkTransmit.updateData(11, temperature_min);
dataLinkTransmit.updateData(12, temperature_max);
dataLinkTransmit.updateData(13, cell_max_voltage);
dataLinkTransmit.updateData(14, cell_min_voltage);
dataLinkTransmit.updateData(15, batteryAllowsContactorClosing);
}
}
19 changes: 5 additions & 14 deletions Software/src/inverter/SERIAL-LINK-TRANSMITTER-INVERTER.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
//SERIAL-LINK-TRANSMITTER-INVERTER.h

#ifndef SERIAL_LINK_TRANSMITTER_INVERTER_H
#define SERIAL_LINK_TRANSMITTER_INVERTER_H

#define SERIAL_LINK_TRANSMITTER_INVERTER_H

#include <Arduino.h>
#include "../../USER_SETTINGS.h"
#include "../lib/SerialDataLink/SerialDataLink.h"

#include "../devboard/config.h" // Needed for LED defines
#include "../lib/mackelec-SerialDataLink/SerialDataLink.h"

// These parameters need to be mapped for the inverter
extern uint16_t SOC; //SOC%, 0-100.00 (0-10000)
Expand All @@ -26,16 +25,8 @@ extern uint16_t temperature_max; //C+1, Goes thru convert2unsignedint16 funct
extern uint16_t cell_max_voltage; //mV, 0-4350
extern uint16_t cell_min_voltage; //mV, 0-4350
extern bool batteryAllowsContactorClosing; //Bool, 1=true, 0=false


extern uint8_t LEDcolor; //Enum, 0-10

void manageSerialLinkTransmitter();








#endif
#endif

0 comments on commit 0bfb757

Please sign in to comment.