Skip to content

Commit

Permalink
Merge branch 'main' into feature/mqtt-on-main
Browse files Browse the repository at this point in the history
  • Loading branch information
Cabooman committed Feb 2, 2024
2 parents 7ad74ad + fa9c912 commit 2cea5bd
Show file tree
Hide file tree
Showing 9 changed files with 565 additions and 11 deletions.
68 changes: 68 additions & 0 deletions Software/Software.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
/* Only change battery specific settings in "USER_SETTINGS.h" */

#include <Arduino.h>
#include <Preferences.h>
#include "HardwareSerial.h"
#include "USER_SETTINGS.h"
#include "src/battery/BATTERIES.h"
#include "src/charger/CHARGERS.h"
#include "src/devboard/config.h"
#include "src/inverter/INVERTERS.h"
#include "src/lib/adafruit-Adafruit_NeoPixel/Adafruit_NeoPixel.h"
Expand All @@ -18,6 +20,8 @@
#include "src/devboard/webserver/webserver.h"
#endif

Preferences settings; // Store user settings

// Interval settings
int intervalUpdateValues = 4800; // Interval at which to update inverter values / Modbus registers
const int interval10 = 10; // Interval for 10ms tasks
Expand Down Expand Up @@ -68,6 +72,21 @@ uint16_t cell_max_voltage = 3700; // Stores the highest cell voltage value
uint16_t cell_min_voltage = 3700; // Stores the minimum cell voltage value in the system
bool LFP_Chemistry = false;

// Common charger parameters
volatile float charger_setpoint_HV_VDC = 0.0f;
volatile float charger_setpoint_HV_IDC = 0.0f;
volatile float charger_setpoint_HV_IDC_END = 0.0f;
bool charger_HV_enabled = false;
bool charger_aux12V_enabled = false;

// Common charger statistics, instantaneous values
float charger_stat_HVcur = 0;
float charger_stat_HVvol = 0;
float charger_stat_ACcur = 0;
float charger_stat_ACvol = 0;
float charger_stat_LVcur = 0;
float charger_stat_LVvol = 0;

// LED parameters
Adafruit_NeoPixel pixels(1, WS2812_PIN, NEO_GRB + NEO_KHZ800);
static uint8_t brightness = 0;
Expand Down Expand Up @@ -102,6 +121,8 @@ bool inverterAllowsContactorClosing = true;
void setup() {
init_serial();

init_stored_settings();

#ifdef WEBSERVER
init_webserver();
#endif
Expand Down Expand Up @@ -172,6 +193,37 @@ void init_serial() {
Serial.println("__ OK __");
}

void init_stored_settings() {
settings.begin("batterySettings", false);

#ifndef LOAD_SAVED_SETTINGS_ON_BOOT
settings.clear(); // If this clear function is executed, no settings will be read from storage
#endif

static uint16_t temp = 0;
temp = settings.getUInt("BATTERY_WH_MAX", false);
if (temp != 0) {
BATTERY_WH_MAX = temp;
}
temp = settings.getUInt("MAXPERCENTAGE", false);
if (temp != 0) {
MAXPERCENTAGE = temp;
}
temp = settings.getUInt("MINPERCENTAGE", false);
if (temp != 0) {
MINPERCENTAGE = temp;
}
temp = settings.getUInt("MAXCHARGEAMP", false);
if (temp != 0) {
MAXCHARGEAMP = temp;
}
temp = settings.getUInt("MAXDISCHARGEAMP", false);
if (temp != 0) {
MAXDISCHARGEAMP = temp;
}
settings.end();
}

void init_CAN() {
// CAN pins
pinMode(CAN_SE_PIN, OUTPUT);
Expand Down Expand Up @@ -363,6 +415,9 @@ void receive_can() { // This section checks if we have a complete CAN message i
#endif
#ifdef SMA_CAN
receive_can_sma(rx_frame);
#endif
#ifdef CHEVYVOLT_CHARGER
receive_can_chevyvolt_charger(rx_frame);
#endif
} else {
//printf("New extended frame");
Expand Down Expand Up @@ -422,6 +477,9 @@ void send_can() {
#ifdef TEST_FAKE_BATTERY
send_can_test_battery();
#endif
#ifdef CHEVYVOLT_CHARGER
send_can_chevyvolt_charger();
#endif
}

#ifdef DUAL_CAN
Expand Down Expand Up @@ -672,3 +730,13 @@ void init_serialDataLink() {
Serial2.begin(9600, SERIAL_8N1, RS485_RX_PIN, RS485_TX_PIN);
#endif
}

void storeSettings() {
settings.begin("batterySettings", false);
settings.putUInt("BATTERY_WH_MAX", BATTERY_WH_MAX);
settings.putUInt("MAXPERCENTAGE", MAXPERCENTAGE);
settings.putUInt("MINPERCENTAGE", MINPERCENTAGE);
settings.putUInt("MAXCHARGEAMP", MAXCHARGEAMP);
settings.putUInt("MAXDISCHARGEAMP", MAXDISCHARGEAMP);
settings.end();
}
9 changes: 8 additions & 1 deletion Software/USER_SETTINGS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ volatile uint16_t MAXDISCHARGEAMP =
const char* mqtt_user = "REDACTED";
const char* mqtt_password = "REDACTED";
#endif // USE_MQTT
/* Charger settings */
volatile float CHARGER_SET_HV = 384; // Reasonably appropriate 4.0v per cell charging of a 96s pack
volatile float CHARGER_MAX_HV = 420; // Max permissible output (VDC) of charger
volatile float CHARGER_MIN_HV = 200; // Min permissible output (VDC) of charger
volatile float CHARGER_MAX_POWER = 3300; // Max power capable of charger, as a ceiling for validating config
volatile float CHARGER_MAX_A = 11.5; // Max current output (amps) of charger
volatile float CHARGER_END_A = 1.0; // Current at which charging is considered complete

#ifdef WEBSERVER
volatile uint8_t AccessPointEnabled =
Expand All @@ -29,5 +36,5 @@ const char* ssid = "REPLACE_WITH_YOUR_SSID"; // Maximum of 63 character
const char* password = "REPLACE_WITH_YOUR_PASSWORD"; // Minimum of 8 characters;
const char* ssidAP = "Battery Emulator"; // Maximum of 63 characters;
const char* passwordAP = "123456789"; // Minimum of 8 characters; set to NULL if you want the access point to be open
const char* versionNumber = "4.4.0"; // The current software version, shown on webserver
const char* versionNumber = "4.5.0"; // The current software version, shown on webserver
#endif
18 changes: 18 additions & 0 deletions Software/USER_SETTINGS.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
//#define SERIAL_LINK_TRANSMITTER //Enable this line to send battery data over RS485 pins to another Lilygo (This LilyGo interfaces with battery)
//#define WEBSERVER //Enable this line to enable WiFi, and to run the webserver. See USER_SETTINGS.cpp for the Wifi settings.
//#define MQTT // Enable this line to enable MQTT
//#define LOAD_SAVED_SETTINGS_ON_BOOT //Enable this line to read settings stored via the webserver on boot

/* Select charger used (Optional) */
//#define CHEVYVOLT_CHARGER //Enable this line to control a Chevrolet Volt charger connected to battery - for example, when generator charging or using an inverter without a charging function.

/* Battery limits: These are set in the USER_SETTINGS.cpp file, or later on via the Webserver */
extern volatile uint16_t BATTERY_WH_MAX;
Expand All @@ -46,4 +50,18 @@ extern volatile uint16_t MINPERCENTAGE;
extern volatile uint16_t MAXCHARGEAMP;
extern volatile uint16_t MAXDISCHARGEAMP;
extern volatile uint8_t AccessPointEnabled;

/* Charger limits: Set in the USER_SETTINGS.cpp or later in the webserver */
extern volatile float charger_setpoint_HV_VDC;
extern volatile float charger_setpoint_HV_IDC;
extern volatile float charger_setpoint_HV_IDC_END;
extern volatile float CHARGER_SET_HV;
extern volatile float CHARGER_MAX_HV;
extern volatile float CHARGER_MIN_HV;
extern volatile float CHARGER_MAX_POWER;
extern volatile float CHARGER_MAX_A;
extern volatile float CHARGER_END_A;
extern bool charger_HV_enabled;
extern bool charger_aux12V_enabled;

#endif
13 changes: 4 additions & 9 deletions Software/src/battery/TESLA-MODEL-3-BATTERY.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ static uint16_t volts = 0; // V
static int16_t amps = 0; // A
static int16_t power = 0; // W
static uint16_t raw_amps = 0; // A
static int16_t max_temp = 6; // C*
static int16_t min_temp = 5; // C*
static int16_t max_temp = 0; // C*
static int16_t min_temp = 0; // C*
static uint16_t energy_buffer = 0;
static uint16_t energy_to_charge_complete = 0;
static uint16_t expected_energy_remaining = 0;
Expand Down Expand Up @@ -209,10 +209,8 @@ void update_values_tesla_model_3_battery() { //This function maps all the value
power = ((volts / 10) * amps);
stat_batt_power = convert2unsignedInt16(power);

min_temp = (min_temp * 10);
temperature_min = convert2unsignedInt16(min_temp);

max_temp = (max_temp * 10);
temperature_max = convert2unsignedInt16(max_temp);

cell_max_voltage = cell_max_v;
Expand Down Expand Up @@ -443,11 +441,8 @@ void receive_can_tesla_model_3_battery(CAN_frame_t rx_frame) {
}
if (mux == 0) //Temperature sensors
{
temp = rx_frame.data.u8[2];
max_temp = (temp * 0.5) - 40; //in celcius, Example 24

temp = rx_frame.data.u8[3];
min_temp = (temp * 0.5) - 40; //in celcius , Example 24
max_temp = (rx_frame.data.u8[2] * 5) - 400; //Temperature values have 40.0*C offset, 0.5*C /bit
min_temp = (rx_frame.data.u8[3] * 5) - 400; //Multiply by 5 and remove offset to get C+1 (0x61*5=485-400=8.5*C)
}
break;
case 0x2d2:
Expand Down
8 changes: 8 additions & 0 deletions Software/src/charger/CHARGERS.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef CHARGERS_H
#define CHARGERS_H

#ifdef CHEVYVOLT_CHARGER
#include "chevyvolt.h"
#endif

#endif
Loading

0 comments on commit 2cea5bd

Please sign in to comment.