Skip to content

Commit

Permalink
Merge pull request #289 from stuartpittaway/stateofhealth
Browse files Browse the repository at this point in the history
State of health and various other fixes (May 2024)
  • Loading branch information
stuartpittaway authored Aug 5, 2024
2 parents 9a0a3ed + b6e1066 commit a28b623
Show file tree
Hide file tree
Showing 31 changed files with 1,914 additions and 1,242 deletions.
50 changes: 18 additions & 32 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,23 @@ jobs:
job_build_modulecode:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Cache pip
uses: actions/cache@v3
- name: Cache pio
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: ${{ runner.os }}-pip-

- name: Cache PlatformIO
uses: actions/cache@v3
with:
path: ~/.platformio
key: ${{ runner.os }}-platformio-2022-${{ hashFiles('**/lockfiles') }}
path: |
~/.cache/pip
~/.platformio/.cache
key: ${{ runner.os }}-pio

- name: Set up Python
uses: actions/setup-python@v4

- uses: actions/cache@v3
uses: actions/setup-python@v5
with:
path: ~/.local/share/virtualenvs
key: ${{ runner.os }}-pipenv-${{ hashFiles('Pipfile.lock') }}
restore-keys: ${{ runner.os }}-pipenv-
python-version: '3.11'

- name: Install PlatformIO
run: |
python -m pip install --upgrade pip
pip install --upgrade platformio
run: pip install --upgrade platformio

- name: Create folders
run: |
Expand Down Expand Up @@ -65,13 +54,6 @@ jobs:
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
cd esptool
pip install --user -e .
cd ~
python -m esptool version

- name: Build code for ESP32 controller
run: pio run --project-dir=/home/runner/work/diyBMSv4ESP32/diyBMSv4ESP32/ESPController --environment esp32-devkitc --project-conf=/home/runner/work/diyBMSv4ESP32/diyBMSv4ESP32/ESPController/platformio.ini
Expand All @@ -92,8 +74,12 @@ jobs:
run: |
cp ./ESPController/.pio/build/esp32-devkitc/diybms_controller_filesystemimage_espressif32_esp32-devkitc.bin ~/OUTPUT/Controller/
- name: Build single ESP32 image
- name: Get latest esptool / Build single ESP32 image
run: |
git clone https://github.com/espressif/esptool.git
cd esptool
pip install --user -e .
cd ~
python -m esptool --chip esp32 merge_bin -o ~/OUTPUT/Controller/esp32-controller-firmware-complete.bin --flash_mode=keep --flash_size 4MB 0x1000 ~/OUTPUT/Controller/bootloader.bin 0x8000 ~/OUTPUT/Controller/partitions.bin 0xe000 ~/OUTPUT/Controller/boot_app0.bin 0x10000 ~/OUTPUT/Controller/diybms_controller_firmware_espressif32_esp32-devkitc.bin 0x1C0000 ~/OUTPUT/Controller/diybms_controller_firmware_espressif32_esp32-devkitc.bin 0x370000 ~/OUTPUT/Controller/diybms_controller_filesystemimage_espressif32_esp32-devkitc.bin
- name: Board test code for ESP32 controller
Expand All @@ -104,7 +90,7 @@ jobs:
cp ./ESP32BoardTest/.pio/build/esp32-devkitc/diybms_boardtest_espressif32_esp32-devkitc.bin ~/OUTPUT/ControllerBoardTest/
- name: Publish Artifacts 1
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: DIYBMS-Compiled
path: ~/OUTPUT
Expand All @@ -115,7 +101,7 @@ jobs:
needs: [job_build_modulecode]
steps:
- name: Download artifact DIYBMS-Compiled
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: DIYBMS-Compiled

Expand All @@ -142,7 +128,7 @@ jobs:
run: mv release.zip release_${{ env.dt }}.zip

- name: Publish Artifact
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: DIYBMS-Release-Artifact-${{ env.dt }}
path: |
Expand Down
11 changes: 4 additions & 7 deletions ESP32BoardTest/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,22 @@ build_flags =
[env]
framework = arduino
; 4MB FLASH DEVKITC
platform = espressif32
;platform = https://github.com/platformio/platform-espressif32.git#feature/arduino-upstream
platform = espressif32@~6.4.0

board = esp32dev
monitor_speed = 115200
monitor_port=COM4
monitor_port=COM3
monitor_filters = log2file, esp32_exception_decoder
board_build.flash_mode = dout
board_build.filesystem = littlefs
extra_scripts =
pre:buildscript_versioning.py



upload_speed=921600
upload_port=COM4
upload_port=COM3

platform_packages =
framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#2.0.6
framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#2.0.14

board_build.partitions = diybms_partitions.csv

Expand Down
78 changes: 76 additions & 2 deletions ESP32BoardTest/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ static constexpr const char *const TAG = "diybms";
#define RS485_TX GPIO_NUM_22
#define RS485_ENABLE GPIO_NUM_25

#define INA229_CHIPSELECT GPIO_NUM_33
#define INA229_INTERRUPT_PIN GPIO_NUM_35

enum RGBLED : uint8_t
{
OFF = 0,
Expand Down Expand Up @@ -605,7 +608,7 @@ void testSerial()
delay(10);

ESP_LOGI(TAG, "Test serial TX1/RX1");
for (size_t i = 0; i < 50; i++)
for (size_t i = 0; i < 20; i++)
{
SERIAL_DATA.println("test!");

Expand All @@ -623,6 +626,65 @@ void testSerial()
}
}

enum INA_REGISTER : uint8_t
{
CONFIG = 0,
ADC_CONFIG = 1,
// Shunt Calibration
SHUNT_CAL = 2,
// Shunt Temperature Coefficient
SHUNT_TEMPCO = 3,
// Shunt Voltage Measurement 24bit
VSHUNT = 4,
// Bus Voltage Measurement 24bit
VBUS = 5,
DIETEMP = 6,
// Current Result 24bit
CURRENT = 7,
// Power Result 24bit
POWER = 8,
// Energy Result 40bit
ENERGY = 9,
// Charge Result 40bit
CHARGE = 0x0A,
// Alert triggers
DIAG_ALRT = 0x0b,
// Shunt Overvoltage Threshold
// overcurrent protection
SOVL = 0x0c,
// Shunt Undervoltage Threshold
// undercurrent protection
SUVL = 0x0d,
// Bus Overvoltage Threshold
BOVL = 0x0e,
// Bus Undervoltage Threshold
BUVL = 0x0f,
// Temperature Over-Limit Threshold
TEMP_LIMIT = 0x10,
// Power Over-Limit Threshold
PWR_LIMIT = 0x11,
// Manufacturer ID
MANUFACTURER_ID = 0x3E,
// Device ID
DEVICE_ID = 0x3F

};

uint16_t read16bits(INA_REGISTER r)
{
SPISettings _spisettings = SPISettings(10000000, MSBFIRST, SPI_MODE1);
vspi.beginTransaction(_spisettings);
digitalWrite(INA229_CHIPSELECT, LOW);
// The transfers are always a step behind, so the transfer reads the previous value/command
vspi.write((uint8_t)((r << 2U) | B00000001));
uint16_t value = vspi.transfer16(0);
digitalWrite(INA229_CHIPSELECT, HIGH);
vspi.endTransaction();

ESP_LOGD(TAG, "Read register 0x%02x = 0x%04x", r, value);
return value;
}

void setup()
{
// We are not testing ESP32, so switch off WIFI + BT
Expand All @@ -638,6 +700,18 @@ void setup()
ConfigureI2C();
ConfigureVSPI();


uint16_t value = read16bits(INA_REGISTER::DEVICE_ID);
if ((value >> 4) == 0x229)
{
ESP_LOGI(TAG, "FOUND CURRENT SHUNT CHIP INA%02x, Revision=%u", value >> 4, value & B1111);
}
else
{
// Stop here - no chip found
ESP_LOGW(TAG, "** CURRENT SHUNT CHIP INA229 CHIP ABSENT **");
}

mountSDCard();

// Create a test file
Expand Down Expand Up @@ -681,7 +755,7 @@ void setup()
Led(RGBLED::OFF);

// Test SERIAL
SERIAL_DATA.begin(2400, SERIAL_8N1, 2, 32); // Serial for comms to modules
SERIAL_DATA.begin(5000, SERIAL_8N1, 2, 32); // Serial for comms to modules
testSerial();

init_tft_display();
Expand Down
2 changes: 1 addition & 1 deletion ESPController/data/cfg_lifepo4_16cells.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"diybms_settings":{"totalNumberOfBanks":1,"totalNumberOfSeriesModules":16,"baudRate":10000,"interpacketgap":3000,"graph_voltagehigh":3650,"graph_voltagelow":3000,"BypassOverTempShutdown":65,"BypassThresholdmV":3450,"timeZone":0,"minutesTimeZone":0,"daylight":false,"ntpServer":"time.google.com","loggingEnabled":false,"loggingFrequencySeconds":15,"currentMonitoringEnabled":true,"currentMonitoringModBusAddress":90,"rs485baudrate":19200,"rs485databits":3,"rs485parity":0,"rs485stopbits":1,"language":"en","mqtt":{"enabled":true,"uri":"mqtt://192.168.0.26:1883","topic":"emon/diybms","username":"emonpi","password":"emonpimqtt2016"},"influxdb":{"enabled":false,"apitoken":"","bucket":"bucketname","org":"organisation","url":"http://192.168.0.49:8086/api/v2/write","logfreq":15},"outputs":{"default":[153,153,153,153],"type":[0,0,0,0]},"rules":{"EmergencyStop":{"value":0,"hysteresis":0,"state":[0,0,0,0]},"BMSError":{"value":0,"hysteresis":0,"state":[0,0,0,0]},"CurrentMonitorOverCurrentAmps":{"value":100,"hysteresis":100,"state":[0,0,0,0]},"ModuleOverVoltage":{"value":3650,"hysteresis":3500,"state":[255,0,0,0]},"ModuleUnderVoltage":{"value":2900,"hysteresis":3050,"state":[255,0,0,0]},"ModuleOverTemperatureInternal":{"value":75,"hysteresis":75,"state":[0,0,0,0]},"ModuleUnderTemperatureInternal":{"value":5,"hysteresis":5,"state":[0,0,0,0]},"ModuleOverTemperatureExternal":{"value":50,"hysteresis":50,"state":[0,0,0,0]},"ModuleUnderTemperatureExternal":{"value":2,"hysteresis":2,"state":[0,0,0,0]},"CurrentMonitorOverVoltage":{"value":57600,"hysteresis":57000,"state":[0,0,0,0]},"CurrentMonitorUnderVoltage":{"value":48000,"hysteresis":49000,"state":[0,0,0,0]},"BankOverVoltage":{"value":57600,"hysteresis":57000,"state":[0,0,0,0]},"BankUnderVoltage":{"value":48000,"hysteresis":49000,"state":[0,0,0,0]},"Timer2":{"value":1020,"hysteresis":1020,"state":[0,0,0,0]},"Timer1":{"value":480,"hysteresis":480,"state":[0,0,0,0]}},"canbusprotocol":2,"nominalbatcap":280,"chargevolt":568,"chargecurrent":650,"dischargecurrent":650,"dischargevolt":488,"chargetemplow":0,"chargetemphigh":50,"dischargetemplow":-30,"dischargetemphigh":55,"stopchargebalance":false,"socoverride":false,"socforcelow":false,"dynamiccharge":true,"preventdischarge":false,"preventcharging":false,"cellminmv":3050,"cellmaxmv":3460,"kneemv":3320,"cellmaxspikemv":3550,"sensitivity":27,"cur_val1":36,"cur_val2":7,"tilevisibility":[49152,0,62209,0,0]}}
{"diybms_settings":{"totalNumberOfBanks":1,"totalNumberOfSeriesModules":16,"baudRate":10000,"interpacketgap":3000,"graph_voltagehigh":3650,"graph_voltagelow":3000,"BypassOverTempShutdown":65,"BypassThresholdmV":3450,"timeZone":0,"minutesTimeZone":0,"daylight":false,"ntpServer":"time.google.com","loggingEnabled":false,"loggingFrequencySeconds":15,"currentMonitoringEnabled":true,"currentMonitoringModBusAddress":90,"rs485baudrate":19200,"rs485databits":3,"rs485parity":0,"rs485stopbits":1,"language":"en","mqtt":{"enabled":true,"uri":"mqtt://192.168.0.26:1883","topic":"emon/diybms","username":"emonpi","password":"emonpimqtt2016"},"influxdb":{"enabled":false,"apitoken":"","bucket":"bucketname","org":"organisation","url":"http://192.168.0.49:8086/api/v2/write","logfreq":15},"outputs":{"default":[153,153,153,153],"type":[0,0,0,0]},"rules":{"EmergencyStop":{"value":0,"hysteresis":0,"state":[0,0,0,0]},"BMSError":{"value":0,"hysteresis":0,"state":[0,0,0,0]},"CurrentMonitorOverCurrentAmps":{"value":100,"hysteresis":100,"state":[0,0,0,0]},"ModuleOverVoltage":{"value":3650,"hysteresis":3500,"state":[255,0,0,0]},"ModuleUnderVoltage":{"value":2900,"hysteresis":3050,"state":[255,0,0,0]},"ModuleOverTemperatureInternal":{"value":75,"hysteresis":75,"state":[0,0,0,0]},"ModuleUnderTemperatureInternal":{"value":5,"hysteresis":5,"state":[0,0,0,0]},"ModuleOverTemperatureExternal":{"value":50,"hysteresis":50,"state":[0,0,0,0]},"ModuleUnderTemperatureExternal":{"value":2,"hysteresis":2,"state":[0,0,0,0]},"CurrentMonitorOverVoltage":{"value":57600,"hysteresis":57000,"state":[0,0,0,0]},"CurrentMonitorUnderVoltage":{"value":48000,"hysteresis":49000,"state":[0,0,0,0]},"BankOverVoltage":{"value":57600,"hysteresis":57000,"state":[0,0,0,0]},"BankUnderVoltage":{"value":48000,"hysteresis":49000,"state":[0,0,0,0]},"Timer2":{"value":1020,"hysteresis":1020,"state":[0,0,0,0]},"Timer1":{"value":480,"hysteresis":480,"state":[0,0,0,0]}},"protocol":2,"nominalbatcap":280,"chargevolt":568,"chargecurrent":650,"dischargecurrent":650,"dischargevolt":488,"chargetemplow":0,"chargetemphigh":50,"dischargetemplow":-30,"dischargetemphigh":55,"stopchargebalance":false,"socoverride":false,"socforcelow":false,"dynamiccharge":true,"preventdischarge":false,"preventcharging":false,"cellminmv":3050,"cellmaxmv":3460,"kneemv":3320,"cellmaxspikemv":3550,"sensitivity":27,"cur_val1":36,"cur_val2":7,"tilevisibility":[49152,0,62209,0,0]}}
8 changes: 7 additions & 1 deletion ESPController/include/CurrentMonitorINA229.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,12 @@ class CurrentMonitorINA229
uint16_t shunttempcoefficient,
bool TemperatureCompEnabled);

void GuessSOC();
void DefaultSOC();
void TakeReadings();

uint32_t raw_milliamphour_out() const{ return milliamphour_out; }
uint32_t raw_milliamphour_in() const{ return milliamphour_in; }

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; }
Expand Down Expand Up @@ -264,12 +267,15 @@ class CurrentMonitorINA229
return registers.R_DIAG_ALRT & ALL_ALERT_BITS;
}
void SetSOC(uint16_t value);
void SetSOCByMilliAmpCounter(uint32_t in,uint32_t out);

void ResetDailyAmpHourCounters() {
daily_milliamphour_out=0;
daily_milliamphour_in=0;
}

uint16_t raw_stateofcharge() const { return SOC; }

private:
uint16_t SOC = 0;
float voltage = 0;
Expand Down
Loading

0 comments on commit a28b623

Please sign in to comment.