Skip to content

Commit

Permalink
Merge branch 'wifi_mqtt_fixes' into master-pylonforce-can
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartpittaway authored Nov 13, 2023
2 parents 14863e0 + 420c670 commit 401f037
Show file tree
Hide file tree
Showing 35 changed files with 2,838 additions and 285 deletions.
18 changes: 13 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,21 @@ jobs:
pwd
ls
- name: Build code for modules
- name: Build code for ATTINY modules
run: pio run --project-dir=/home/runner/work/diyBMSv4ESP32/diyBMSv4ESP32/ATTINYCellModule --project-conf=/home/runner/work/diyBMSv4ESP32/diyBMSv4ESP32/ATTINYCellModule/platformio.ini

- name: Copy output
run: |
cp ./ATTINYCellModule/.pio/build/V*/*.hex ~/OUTPUT/Modules/HEX/
cp ./ATTINYCellModule/.pio/build/V*/*.bin ~/OUTPUT/Modules/BIN/
- name: Build code for STM32 modules
run: pio run --project-dir=/home/runner/work/diyBMSv4ESP32/diyBMSv4ESP32/STM32All-In-One --project-conf=/home/runner/work/diyBMSv4ESP32/diyBMSv4ESP32/STM32All-In-One/platformio.ini

- name: Copy STM32 binary firmware output files
run: |
cp ./STM32All-In-One/.pio/build/V*/*.bin ~/OUTPUT/Modules/BIN/
- name: Get latest esptool
run: |
git clone https://github.com/espressif/esptool.git
Expand Down Expand Up @@ -109,11 +115,11 @@ jobs:
needs: [job_build_modulecode]
steps:
- name: Download artifact DIYBMS-Compiled
uses: actions/download-artifact@v2
uses: actions/download-artifact@v3
with:
name: DIYBMS-Compiled

- name: Display structure of downloaded files
- name: Rename over-the-air firmware
run: mv ./Controller/diybms_controller_firmware_espressif32_esp32-devkitc.bin ./Controller/esp32-controller-firmware-over-the-air.bin

- name: Display structure of downloaded files
Expand All @@ -126,12 +132,13 @@ jobs:
zip release.zip ./ControllerBoardTest/diybms_boardtest_espressif32_esp32-devkitc.bin
zip release.zip ./Modules/manifest.json
zip release.zip ./Modules/HEX/*.*
zip release.zip ./Modules/BIN/module_fw_V490*.*
- name: Get current date
id: date
run: echo "dt=$(date +'%Y-%m-%d-%H-%M')" >> $GITHUB_ENV

- name: Display structure of downloaded files
- name: Rename ZIP
run: mv release.zip release_${{ env.dt }}.zip

- name: Publish Artifact
Expand All @@ -142,6 +149,7 @@ jobs:
./Controller/*.*
./Modules/manifest.json
./Modules/HEX/*.*
./Modules/BIN/module_fw_V490*.bin
if-no-files-found: error

- name: Create Release Files on MASTER branch only
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ dkms.conf
*.log
ESPController/compile_commands.json
ATTINYCellModule/compile_commands.json
STM32All-In-One/compile_commands.json
129 changes: 56 additions & 73 deletions ESPController/include/CurrentMonitorINA229.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,6 @@ class CurrentMonitorINA229
public:
CurrentMonitorINA229()
{
INA229Installed = false;
SPI_Ptr = NULL;
chipselectpin = 0;

milliamphour_out_lifetime = 0;
milliamphour_in_lifetime = 0;

daily_milliamphour_out = 0;
daily_milliamphour_in = 0;

milliamphour_out = 0;
milliamphour_in = 0;

milliamphour_out_offset = 0;
milliamphour_in_offset = 0;

// Clear structure
memset(&registers, 0, sizeof(eeprom_regs));

Expand All @@ -181,9 +165,9 @@ class CurrentMonitorINA229

// Defaults for battery capacity/voltages
registers.batterycapacity_amphour = 280;
registers.fully_charged_voltage = 3.50 * 16;
registers.tail_current_amps = 20;
registers.charge_efficiency_factor = 99.5;
registers.fully_charged_voltage = 3.50F * 16.0F;
registers.tail_current_amps = 20.0F;
registers.charge_efficiency_factor = 99.5F;

// Default 150A shunt @ 50mV scale
registers.shunt_max_current = 150;
Expand All @@ -207,10 +191,10 @@ class CurrentMonitorINA229
CalculateLSB();

// Default Power limit = 5kW
registers.R_PWR_LIMIT = (uint16_t)((5000.0 / registers.CURRENT_LSB / 3.2) / 256.0); // 5kW
registers.R_PWR_LIMIT = (uint16_t)((5000.0F / registers.CURRENT_LSB / 3.2F) / 256.0F); // 5kW
}

bool Available()
bool Available() const
{
return INA229Installed;
}
Expand All @@ -236,43 +220,43 @@ class CurrentMonitorINA229
void GuessSOC();
void TakeReadings();

uint32_t calc_milliamphour_out() { return milliamphour_out - milliamphour_out_offset; }
uint32_t calc_milliamphour_in() { return milliamphour_in - milliamphour_in_offset; }
uint32_t calc_daily_milliamphour_out() { return daily_milliamphour_out; }
uint32_t calc_daily_milliamphour_in() { return daily_milliamphour_in; }

float calc_charge_efficiency_factor() { return registers.charge_efficiency_factor; }
float calc_state_of_charge() { return SOC / 100.0; }

float calc_voltage() { return voltage; }
float calc_current() { return current; }
float calc_power() { return power; }
uint16_t calc_shuntcalibration() { return registers.R_SHUNT_CAL; }
int16_t calc_temperature() { return (int16_t)temperature; }
uint16_t calc_shunttempcoefficient() { return registers.R_SHUNT_TEMPCO; }
float calc_tailcurrentamps() { return registers.tail_current_amps; }
float calc_fullychargedvoltage() { return registers.fully_charged_voltage; }
float calc_shuntresistance() { return 1000 * registers.RSHUNT; }

uint16_t calc_shuntmillivolt() { return registers.shunt_millivolt; }
uint16_t calc_shuntmaxcurrent() { return registers.shunt_max_current; }
uint16_t calc_batterycapacityAh() { return registers.batterycapacity_amphour; }

int16_t calc_temperaturelimit()
uint32_t calc_milliamphour_out() const{ return milliamphour_out - milliamphour_out_offset; }
uint32_t calc_milliamphour_in() const{ return milliamphour_in - milliamphour_in_offset; }
uint32_t calc_daily_milliamphour_out() const{ return daily_milliamphour_out; }
uint32_t calc_daily_milliamphour_in() const{ return daily_milliamphour_in; }

float calc_charge_efficiency_factor() const{ return registers.charge_efficiency_factor; }
float calc_state_of_charge() const{ return SOC / 100.0F; }

float calc_voltage() const{ return voltage; }
float calc_current() const{ return current; }
float calc_power() const{ return power; }
uint16_t calc_shuntcalibration() const{ return registers.R_SHUNT_CAL; }
int16_t calc_temperature() const{ return (int16_t)temperature; }
uint16_t calc_shunttempcoefficient() const{ return registers.R_SHUNT_TEMPCO; }
float calc_tailcurrentamps() const{ return registers.tail_current_amps; }
float calc_fullychargedvoltage() const{ return registers.fully_charged_voltage; }
float calc_shuntresistance() const{ return 1000 * registers.RSHUNT; }

uint16_t calc_shuntmillivolt() const{ return registers.shunt_millivolt; }
uint16_t calc_shuntmaxcurrent() const{ return registers.shunt_max_current; }
uint16_t calc_batterycapacityAh() const{ return registers.batterycapacity_amphour; }

int16_t calc_temperaturelimit() const
{
// Case unsigned to int16 to cope with negative temperatures
return ConvertFrom2sComp(registers.R_TEMP_LIMIT) * (float)0.0078125;
return (int16_t)(ConvertFrom2sComp(registers.R_TEMP_LIMIT) * 0.0078125F);
}
float calc_overpowerlimit()
float calc_overpowerlimit() const
{
return ConvertFrom2sComp(registers.R_PWR_LIMIT) * 256.0F * 3.2F * registers.CURRENT_LSB;
}
float calc_overvoltagelimit() { return (float)ConvertFrom2sComp(registers.R_BOVL) * 0.003125F; }
float calc_undervoltagelimit() { return (float)ConvertFrom2sComp(registers.R_BUVL) * 0.003125F; }
float calc_overcurrentlimit() { return ((float)ConvertFrom2sComp(registers.R_SOVL) / 1000 * 1.25) / full_scale_adc * registers.shunt_max_current; }
float calc_undercurrentlimit() { return ((float)ConvertFrom2sComp(registers.R_SUVL) / 1000 * 1.25) / full_scale_adc * registers.shunt_max_current; }
float calc_overvoltagelimit() const{ return (float)ConvertFrom2sComp(registers.R_BOVL) * 0.003125F; }
float calc_undervoltagelimit() const{ return (float)ConvertFrom2sComp(registers.R_BUVL) * 0.003125F; }
float calc_overcurrentlimit() const{ return ((float)ConvertFrom2sComp(registers.R_SOVL) / 1000.0F * 1.25F) / full_scale_adc * registers.shunt_max_current; }
float calc_undercurrentlimit() const{ return ((float)ConvertFrom2sComp(registers.R_SUVL) / 1000.0F * 1.25F) / full_scale_adc * registers.shunt_max_current; }

bool calc_tempcompenabled() { return (registers.R_CONFIG & bit(5)) != 0; }
bool calc_tempcompenabled() const { return (registers.R_CONFIG & bit(5)) != 0; }

uint16_t calc_alerts()
{
Expand All @@ -293,41 +277,41 @@ class CurrentMonitorINA229
float power = 0;
float temperature = 0;

const float full_scale_adc = 40.96;
// const float CoulombsToAmpHours = 1.0 / 3600.0;
const float CoulombsToMilliAmpHours = 1.0 / 3.6;
const float full_scale_adc = 40.96F;
// const float CoulombsToAmpHours = 1.0F / 3600.0F;
const float CoulombsToMilliAmpHours = 1.0F / 3.6F;

uint8_t max_soc_reset_counter = 0;
int64_t soc_reset_time;
int32_t last_charge_coulombs = 0;

// Pointer to SPI object class NOTE: MUTEX OVER SPI PORT MUST BE HANDLED EXTERNALLY TO THIS CLASS
SPIClass *SPI_Ptr;
SPIClass *SPI_Ptr = nullptr;
// True is chip is installed
bool INA229Installed;
bool INA229Installed = false;
// Chip select pin
uint8_t chipselectpin;
uint8_t chipselectpin = 0;
// Settings used for SPI comms (10Mhz, MSB, Mode 1)
SPISettings _spisettings = SPISettings(10000000, MSBFIRST, SPI_MODE1);

eeprom_regs registers;

uint32_t milliamphour_out_lifetime;
uint32_t milliamphour_in_lifetime;
uint32_t milliamphour_out_lifetime=0;
uint32_t milliamphour_in_lifetime=0;

uint32_t daily_milliamphour_out;
uint32_t daily_milliamphour_in;
uint32_t daily_milliamphour_out=0;
uint32_t daily_milliamphour_in=0;

uint32_t milliamphour_out;
uint32_t milliamphour_in;
uint32_t milliamphour_out=0;
uint32_t milliamphour_in=0;

uint32_t milliamphour_out_offset;
uint32_t milliamphour_in_offset;
uint32_t milliamphour_out_offset=0;
uint32_t milliamphour_in_offset=0;

volatile uint16_t diag_alrt_value = 0;

uint8_t readRegisterValue(INA_REGISTER r);
uint8_t writeRegisterValue(INA_REGISTER r);
uint8_t readRegisterValue(INA_REGISTER r) const;
uint8_t writeRegisterValue(INA_REGISTER r) const;
void CalculateLSB();

uint16_t read16bits(INA_REGISTER r);
Expand All @@ -346,18 +330,17 @@ class CurrentMonitorINA229
int64_t spi_readInt40(INA_REGISTER r);

void CalculateAmpHourCounts();
uint16_t CalculateSOC();
uint16_t CalculateSOC() const;

// Convert an int16 to a uint16 2 compliment value
uint16_t ConvertTo2sComp(int16_t value)
uint16_t ConvertTo2sComp(int16_t value) const
{
if (value >= 0)
{
return (uint16_t)value;
}

uint16_t v = -value;
//ESP_LOGI(TAG, "uint16 v=%u", v);

v = v - 1;
v = ~v;
Expand All @@ -366,11 +349,11 @@ class CurrentMonitorINA229
}

// Convert a 2 compliemnt uint16 to a signed int16 value
int16_t ConvertFrom2sComp(uint16_t value)
int16_t ConvertFrom2sComp(uint16_t value) const
{
int16_t v = (int16_t)value;
auto v = (int16_t)value;

if (value & 0x8000U == 0)
if ((value & 0x8000U) == 0)
{
// Positive number, just return it
return v;
Expand Down
2 changes: 1 addition & 1 deletion ESPController/include/HAL_ESP32.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ class HAL_ESP32
SPIClass *VSPI_Ptr();

void Led(uint8_t bits);
void ConfigureCAN(uint16_t canbusbaudrate) const;
void ConfigurePins();
void TFTScreenBacklight(bool Status);

void CANBUSEnable(bool value);
void ConfigureCAN();

bool IsVSPIMutexAvailable()
{
Expand Down
5 changes: 5 additions & 0 deletions ESPController/include/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ struct diybms_eeprom_settings

CanBusProtocolEmulation canbusprotocol;
CanBusInverter canbusinverter;
//CANBUS baud rate, 250=250k, 500=500k
uint16_t canbusbaud;
//Nominal battery capacity (amp hours)
uint16_t nominalbatcap;
// Maximum charge voltage - scale 0.1
uint16_t chargevolt;
Expand Down Expand Up @@ -231,6 +234,8 @@ struct diybms_eeprom_settings

// NOTE this array is subject to buffer overflow vulnerabilities!
bool mqtt_enabled;
// Only report basic cell data (voltage and temperture) over MQTT
bool mqtt_basic_cell_reporting;
char mqtt_uri[128 + 1];
char mqtt_topic[32 + 1];
char mqtt_username[32 + 1];
Expand Down
9 changes: 8 additions & 1 deletion ESPController/include/webserver_json_requests.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extern uint32_t canbus_messages_received_error;

extern Rules rules;
extern ControllerState _controller_state;
extern void formatCurrentDateTime(char* buf, size_t buf_size);
extern void formatCurrentDateTime(char *buf, size_t buf_size);
extern void setNoStoreCacheControl(httpd_req_t *req);
extern char CookieValue[20 + 1];
extern std::string hostname;
Expand All @@ -56,4 +56,11 @@ extern CurrentMonitorINA229 currentmon_internal;
extern History history;
extern wifi_eeprom_settings _wificonfig;
extern esp_err_t diagnosticJSON(httpd_req_t *req, char buffer[], int bufferLenMax);

extern bool mqttClient_connected;
extern uint16_t mqtt_error_connection_count;
extern uint16_t mqtt_error_transport_count;
extern uint16_t mqtt_connection_count;
extern uint16_t mqtt_disconnection_count;

#endif
2 changes: 1 addition & 1 deletion ESPController/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ upload_speed=921600
upload_port=COM3
board_build.arduino.upstream_packages = no
platform_packages =
framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#2.0.12
framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#2.0.14

board_build.partitions = diybms_partitions.csv

Expand Down
Loading

0 comments on commit 401f037

Please sign in to comment.