Refactor CAN inverter handling

This commit is contained in:
Daniel 2024-04-29 21:02:37 +03:00
parent 6f4831f4cd
commit 3aa5704d74
15 changed files with 68 additions and 165 deletions

View file

@ -114,7 +114,7 @@ static uint16_t inverter_SOC = 0;
static long inverter_timestamp = 0;
static bool initialDataSent = 0;
void update_values_can_byd() { //This function maps all the values fetched from battery CAN to the correct CAN messages
void update_values_can_inverter() { //This function maps all the values fetched from battery CAN to the correct CAN messages
//Calculate values
charge_current =
((datalayer.battery.status.max_charge_power_W * 10) /
@ -199,7 +199,7 @@ void update_values_can_byd() { //This function maps all the values fetched from
#endif
}
void receive_can_byd(CAN_frame_t rx_frame) {
void receive_can_inverter(CAN_frame_t rx_frame) {
switch (rx_frame.MsgID) {
case 0x151: //Message originating from BYD HVS compatible inverter. Reply with CAN identifier!
if (rx_frame.data.u8[0] & 0x01) { //Battery requests identification
@ -229,7 +229,7 @@ void receive_can_byd(CAN_frame_t rx_frame) {
}
}
void send_can_byd() {
void send_can_inverter() {
unsigned long currentMillis = millis();
// Send initial CAN data once on bootup
if (!initialDataSent) {

View file

@ -1,14 +1,10 @@
#ifndef BYD_CAN_H
#define BYD_CAN_H
#include <Arduino.h>
#include "../include.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"
#define INVERTER_SELECTED
#define CAN_INVERTER_SELECTED
void update_values_can_byd();
void send_can_byd();
void receive_can_byd(CAN_frame_t rx_frame);
void send_intial_data();
#endif

View file

@ -39,4 +39,11 @@
#include "SERIAL-LINK-TRANSMITTER-INVERTER.h"
#endif
#ifdef CAN_INVERTER_SELECTED
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h" // This include is annoying, consider defining a frame type in types.h
void update_values_can_inverter();
void receive_can_inverter(CAN_frame_t rx_frame);
void send_can_inverter();
#endif
#endif

View file

@ -170,7 +170,7 @@ CAN_frame_t PYLON_4291 = {.FIR = {.B =
.MsgID = 0x4291,
.data = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
void update_values_can_pylon() { //This function maps all the values fetched from battery CAN to the correct CAN messages
void update_values_can_inverter() { //This function maps all the values fetched from battery CAN to the correct CAN messages
//There are more mappings that could be added, but this should be enough to use as a starting point
// Note we map both 0 and 1 messages
@ -243,7 +243,7 @@ void update_values_can_pylon() { //This function maps all the values fetched fr
}
}
void receive_can_pylon(CAN_frame_t rx_frame) {
void receive_can_inverter(CAN_frame_t rx_frame) {
switch (rx_frame.MsgID) {
case 0x4200: //Message originating from inverter. Depending on which data is required, act accordingly
if (rx_frame.data.u8[0] == 0x02) {
@ -258,6 +258,10 @@ void receive_can_pylon(CAN_frame_t rx_frame) {
}
}
void send_can_inverter() {
// No periodic sending, we only react on received can messages
}
void send_setup_info() { //Ensemble information
#ifdef SEND_0
ESP32Can.CANWriteFrame(&PYLON_7310);

View file

@ -3,10 +3,8 @@
#include "../include.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"
#define INVERTER_SELECTED
#define CAN_INVERTER_SELECTED
void update_values_can_pylon();
void receive_can_pylon(CAN_frame_t rx_frame);
void send_system_data();
void send_setup_info();

View file

@ -5,8 +5,6 @@
#include "../include.h"
#include "../lib/mackelec-SerialDataLink/SerialDataLink.h"
#define INVERTER_SELECTED
void manageSerialLinkTransmitter();
#endif

View file

@ -103,7 +103,7 @@ static int16_t charge_current = 0;
static int16_t temperature_average = 0;
static uint16_t ampere_hours_remaining = 0;
void update_values_can_sma() { //This function maps all the values fetched from battery CAN to the correct CAN messages
void update_values_can_inverter() { //This function maps all the values fetched from battery CAN to the correct CAN messages
//Calculate values
charge_current =
((datalayer.battery.status.max_charge_power_W * 10) /
@ -226,7 +226,7 @@ void update_values_can_sma() { //This function maps all the values fetched from
*/
}
void receive_can_sma(CAN_frame_t rx_frame) {
void receive_can_inverter(CAN_frame_t rx_frame) {
switch (rx_frame.MsgID) {
case 0x360: //Message originating from SMA inverter - Voltage and current
//Frame0-1 Voltage
@ -246,7 +246,7 @@ void receive_can_sma(CAN_frame_t rx_frame) {
}
}
void send_can_sma() {
void send_can_inverter() {
unsigned long currentMillis = millis();
// Send CAN Message every 100ms

View file

@ -3,13 +3,9 @@
#include "../include.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"
#define INVERTER_SELECTED
#define CAN_INVERTER_SELECTED
#define READY_STATE 0x03
#define STOP_STATE 0x02
void update_values_can_sma();
void send_can_sma();
void receive_can_sma(CAN_frame_t rx_frame);
#endif

View file

@ -161,7 +161,7 @@ Command2Battery command2Battery = RUN;
enum InvInitState { SYSTEM_FREQUENCY, XPHASE_SYSTEM, BLACKSTART_OPERATION };
InvInitState invInitState = SYSTEM_FREQUENCY;
void update_values_can_sma_tripower() { //This function maps all the values fetched from battery CAN to the inverter CAN
void update_values_can_inverter() { //This function maps all the values fetched from battery CAN to the inverter CAN
//Calculate values
charge_current =
((datalayer.battery.status.max_charge_power_W * 10) /
@ -328,7 +328,7 @@ void update_values_can_sma_tripower() { //This function maps all the values fet
//SMA_018.data.u8[7] = BatteryName;
}
void receive_can_sma_tripower(CAN_frame_t rx_frame) {
void receive_can_inverter(CAN_frame_t rx_frame) {
switch (rx_frame.MsgID) {
case 0x00D: //Inverter Measurements
break;
@ -347,7 +347,7 @@ void receive_can_sma_tripower(CAN_frame_t rx_frame) {
}
}
void send_can_sma_tripower() {
void send_can_inverter() {
unsigned long currentMillis = millis();
// Send CAN Message every 500ms

View file

@ -3,11 +3,8 @@
#include "../include.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"
#define INVERTER_SELECTED
#define CAN_INVERTER_SELECTED
void update_values_can_sma_tripower();
void send_can_sma_tripower();
void receive_can_sma_tripower(CAN_frame_t rx_frame);
void send_tripower_init();
#endif

View file

@ -280,7 +280,7 @@ CAN_frame_t SOFAR_7C0 = {.FIR = {.B =
.MsgID = 0x7C0,
.data = {0x00, 0x00, 0x00, 0x04, 0x00, 0x04, 0x80, 0x00}};
void update_values_can_sofar() { //This function maps all the values fetched from battery CAN to the correct CAN messages
void update_values_can_inverter() { //This function maps all the values fetched from battery CAN to the correct CAN messages
//Maxvoltage (eg 400.0V = 4000 , 16bits long) Charge Cutoff Voltage
SOFAR_351.data.u8[0] = (datalayer.battery.info.max_design_voltage_dV >> 8);
@ -308,7 +308,7 @@ void update_values_can_sofar() { //This function maps all the values fetched fr
SOFAR_356.data.u8[3] = (datalayer.battery.status.temperature_max_dC & 0x00FF);
}
void receive_can_sofar(CAN_frame_t rx_frame) {
void receive_can_inverter(CAN_frame_t rx_frame) {
switch (rx_frame.MsgID) { //In here we need to respond to the inverter. TODO: make logic
case 0x605:
//frame1_605 = rx_frame.data.u8[1];
@ -323,7 +323,7 @@ void receive_can_sofar(CAN_frame_t rx_frame) {
}
}
void send_can_sofar() {
void send_can_inverter() {
unsigned long currentMillis = millis();
// Send 100ms CAN Message
if (currentMillis - previousMillis100 >= INTERVAL_100_MS) {

View file

@ -3,10 +3,6 @@
#include "../include.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"
#define INVERTER_SELECTED
void update_values_can_sofar();
void send_can_sofar();
void receive_can_sofar(CAN_frame_t rx_frame);
#define CAN_INVERTER_SELECTED
#endif

View file

@ -121,7 +121,7 @@ void CAN_WriteFrame(CAN_frame_t* tx_frame) {
#endif
}
void update_values_can_solax() { //This function maps all the values fetched from battery CAN to the correct CAN messages
void update_values_can_inverter() { //This function maps all the values fetched from battery CAN to the correct CAN messages
// If not receiveing any communication from the inverter, open contactors and return to battery announce state
if (millis() - LastFrameTime >= SolaxTimeout) {
datalayer.system.status.inverter_allows_contactor_closing = false;
@ -237,7 +237,11 @@ void update_values_can_solax() { //This function maps all the values fetched fr
SOLAX_1801.data.u8[4] = 1;
}
void receive_can_solax(CAN_frame_t rx_frame) {
void send_can_inverter() {
// No periodic sending used on this protocol, we react only on incoming CAN messages!
}
void receive_can_inverter(CAN_frame_t rx_frame) {
if (rx_frame.MsgID == 0x1871 && rx_frame.data.u8[0] == (0x01) ||
rx_frame.MsgID == 0x1871 && rx_frame.data.u8[0] == (0x02)) {
LastFrameTime = millis();

View file

@ -4,7 +4,7 @@
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"
#include "../lib/pierremolinaro-acan2515/ACAN2515.h"
#define INVERTER_SELECTED
#define CAN_INVERTER_SELECTED
extern ACAN2515 can;
@ -18,6 +18,5 @@ extern ACAN2515 can;
#define FAULT_SOLAX 3
#define UPDATING_FW 4
void update_values_can_solax();
void receive_can_solax(CAN_frame_t rx_frame);
#endif