Skip to content

Commit

Permalink
Added high level BMS functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jsphuebner committed Apr 21, 2023
1 parent 7fe97f8 commit 0aec57f
Show file tree
Hide file tree
Showing 17 changed files with 858 additions and 64 deletions.
6 changes: 6 additions & 0 deletions FlyingAdcBms.cbp
Expand Up @@ -35,12 +35,15 @@
</Build>
<Unit filename="Makefile" />
<Unit filename="include/anain_prj.h" />
<Unit filename="include/bmsalgo.h" />
<Unit filename="include/bmsfsm.h" />
<Unit filename="include/digio_prj.h" />
<Unit filename="include/errormessage_prj.h" />
<Unit filename="include/flyingadcbms.h" />
<Unit filename="include/hwdefs.h" />
<Unit filename="include/hwinit.h" />
<Unit filename="include/param_prj.h" />
<Unit filename="include/temp_meas.h" />
<Unit filename="libopeninv/include/anain.h" />
<Unit filename="libopeninv/include/canhardware.h" />
<Unit filename="libopeninv/include/canmap.h" />
Expand Down Expand Up @@ -89,9 +92,12 @@
<Unit filename="libopeninv/src/terminal.cpp" />
<Unit filename="libopeninv/src/terminalcommands.cpp" />
<Unit filename="linker.ld" />
<Unit filename="src/bmsalgo.cpp" />
<Unit filename="src/bmsfsm.cpp" />
<Unit filename="src/flyingadcbms.cpp" />
<Unit filename="src/hwinit.cpp" />
<Unit filename="src/main.cpp" />
<Unit filename="src/temp_meas.cpp" />
<Unit filename="src/terminal_prj.cpp" />
<Extensions>
<DoxyBlocks>
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -39,7 +39,7 @@ LDFLAGS = -Llibopencm3/lib -T$(LDSCRIPT) -march=armv7 -nostartfiles -Wl,--gc-
OBJSL = main.o hwinit.o stm32scheduler.o params.o \
my_string.o digio.o my_fp.o printf.o anain.o \
param_save.o errormessage.o stm32_can.o canhardware.o canmap.o \
terminalcommands.o flyingadcbms.o
terminalcommands.o flyingadcbms.o bmsfsm.o bmsalgo.o temp_meas.o

OBJS = $(patsubst %.o,obj/%.o, $(OBJSL))
vpath %.c src/ libopeninv/src
Expand Down
17 changes: 7 additions & 10 deletions include/anain_prj.h
Expand Up @@ -3,17 +3,14 @@

#include "hwdefs.h"

/* Here we specify how many samples are combined into one filtered result. Following values are possible:
* - NUM_SAMPLES = 1: Most recent raw value is returned
* - NUM_SAMPLES = 3: Median of last 3 values is returned
* - NUM_SAMPLES = 9: Median of last 3 medians is returned
* - NUM_SAMPLES = 12: Average of last 4 medians is returned
*/
#define NUM_SAMPLES 12
#define SAMPLE_TIME ADC_SMPR_SMP_7DOT5CYC //Sample&Hold time for each pin. Increases sample time, might increase accuracy
#define NUM_SAMPLES 64
#define SAMPLE_TIME ADC_SMPR_SMP_7DOT5CYC

//Here you specify a list of analog inputs, see main.cpp on how to use them
#define ANA_IN_LIST \
ANA_IN_ENTRY(test, GPIOC, 1) \
ANA_IN_ENTRY(curneg, GPIOA, 0) \
ANA_IN_ENTRY(curpos, GPIOA, 1) \
ANA_IN_ENTRY(temp1, GPIOA, 2) \
ANA_IN_ENTRY(temp2, GPIOA, 3) \
ANA_IN_ENTRY(enalevel, GPIOA, 4) \

#endif // ANAIN_PRJ_H_INCLUDED
40 changes: 40 additions & 0 deletions include/bmsalgo.h
@@ -0,0 +1,40 @@
/*
* This file is part of the tumanako_vc project.
*
* Copyright (C) 2018 Johannes Huebner <dev@johanneshuebner.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BMSALGO_H
#define BMSALGO_H

#include <stdint.h>

class BmsAlgo
{
public:
static float EstimateSocFromVoltage(float lowestVoltage);
static float CalculateSocFromIntegration(float lastSoc, float asDiff);
static float GetChargeCurrent(float soc);
static float LimitMaximumCellVoltage(float maxVoltage);
static float LimitMinumumCellVoltage(float minVoltage);
static void SetNominalCapacity(float c) { nominalCapacity = c; }

private:
static float nominalCapacity;
static uint16_t voltageToSoc[11];
static uint16_t socToChargeCurrent[11];
};

#endif // BMSALGO_H
51 changes: 51 additions & 0 deletions include/bmsfsm.h
@@ -0,0 +1,51 @@
/*
* This file is part of the FlyingAdcBms project.
*
* Copyright (C) 2023 Johannes Huebner <openinverter.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BMSFSM_H
#define BMSFSM_H
#include "canmap.h"
#include "canhardware.h"
#include "params.h"

class BmsFsm: public CanCallback
{
public:
enum bmsstate { BOOT, GET_ADDR, SET_ADDR, INIT, RUN, RUNBALANCE };

BmsFsm(CanMap* cm);
bmsstate Run(bmsstate currentState);
int GetNumberOfModules() { return numModules; }
Param::PARAM_NUM GetDataItem(Param::PARAM_NUM baseItem, int modNum = -1);
bool HandleRx(uint32_t canId, uint32_t data[2]);
void HandleClear();
bool IsFirst();

private:
void MapCanSubmodule();
void MapCanMainmodule();

CanMap *canMap;
bool recvBoot;
bool isMain;
uint8_t recvAddr;
uint8_t ourAddr;
uint8_t numModules;
uint32_t cycles;
};

#endif // BMSFSM_H
6 changes: 5 additions & 1 deletion include/digio_prj.h
Expand Up @@ -9,8 +9,12 @@
*/

#define DIG_IO_LIST \
DIG_IO_ENTRY(i2c_scl, GPIOB, GPIO13, PinMode::OUTPUT) \
DIG_IO_ENTRY(i2c_di, GPIOB, GPIO14, PinMode::INPUT_FLT) \
DIG_IO_ENTRY(i2c_scl, GPIOB, GPIO13, PinMode::OUTPUT) \
DIG_IO_ENTRY(i2c_do, GPIOB, GPIO15, PinMode::OUTPUT) \
DIG_IO_ENTRY(muxena_out, GPIOB, GPIO0, PinMode::OUTPUT) \
DIG_IO_ENTRY(nextena_out,GPIOA, GPIO9, PinMode::OUTPUT) \
DIG_IO_ENTRY(selfena_out,GPIOA, GPIO8, PinMode::OUTPUT) \
DIG_IO_ENTRY(led_out, GPIOB, GPIO1, PinMode::OUTPUT) \

#endif // PinMode_PRJ_H_INCLUDED
1 change: 1 addition & 0 deletions include/flyingadcbms.h
Expand Up @@ -26,6 +26,7 @@ class FlyingAdcBms
enum BalanceCommand { BAL_OFF, BAL_DISCHARGE, BAL_CHARGE };
enum BalanceStatus { STT_OFF, STT_DISCHARGE, STT_CHARGEPOS, STT_CHARGENEG };

static void Init();
static void MuxOff();
static void SelectChannel(uint8_t channel);
static void StartAdc();
Expand Down
58 changes: 51 additions & 7 deletions include/param_prj.h
Expand Up @@ -39,26 +39,62 @@
*/

//Define a version string of your firmware here
#define VER 1.00.R
#define VER 0.03.B

/* Entries must be ordered as follows:
1. Saveable parameters (id != 0)
2. Temporary parameters (id = 0)
3. Display values
*/
//Next param id (increase when adding new parameter!): 3
//Next value Id: 2040
//Next param id (increase when adding new parameter!): 14
//Next value Id: 2066
/* category name unit min max default id */
#define PARAM_LIST \
PARAM_ENTRY(CAT_BMS, gain, "mV/dig", 1, 100000, 585, 3 ) \
PARAM_ENTRY(CAT_BMS, numchan, "", 1, 16, 8, 4 ) \
PARAM_ENTRY(CAT_BMS, balance, OFFON, 0, 1, 0, 5 ) \
PARAM_ENTRY(CAT_BMS, idlewait, "s", 0, 100000, 60, 12 ) \
PARAM_ENTRY(CAT_BMS, nomcap, "Ah", 0, 1000, 100, 9 ) \
PARAM_ENTRY(CAT_SENS, idcgain, "dig/A", -1000, 1000, 10, 6 ) \
PARAM_ENTRY(CAT_SENS, idcofs, "dig", -4095, 4095, 0, 7 ) \
PARAM_ENTRY(CAT_SENS, idcmode, IDCMODES, 0, 3, 0, 8 ) \
PARAM_ENTRY(CAT_SENS, tempsns, TEMPSNS, -1, 3, -1, 13 ) \
PARAM_ENTRY(CAT_COMM, pdobase, "", 0, 2047, 500, 10 ) \
PARAM_ENTRY(CAT_COMM, sdobase, "", 0, 63, 3, 11 ) \
PARAM_ENTRY(CAT_TEST, enable, OFFON, 0, 1, 1, 0 ) \
VALUE_ENTRY(opmode, OPMODES, 2000 ) \
VALUE_ENTRY(version, VERSTR, 2001 ) \
VALUE_ENTRY(opmode, OPMODES,2000 ) \
VALUE_ENTRY(version, VERSTR, 2001 ) \
VALUE_ENTRY(modaddr, "", 2045 ) \
VALUE_ENTRY(modnum, "", 2046 ) \
VALUE_ENTRY(chargein, "As", 2040 ) \
VALUE_ENTRY(chargeout, "As", 2041 ) \
VALUE_ENTRY(soc, "As", 2065 ) \
VALUE_ENTRY(chargelim, "A", 2066 ) \
VALUE_ENTRY(dischargelim,"A", 2067 ) \
VALUE_ENTRY(idc, "A", 2042 ) \
VALUE_ENTRY(idcavg, "A", 2043 ) \
VALUE_ENTRY(temp, "°C", 2044 ) \
VALUE_ENTRY(uavg, "mV", 2002 ) \
VALUE_ENTRY(umin, "mV", 2003 ) \
VALUE_ENTRY(umax, "mV", 2004 ) \
VALUE_ENTRY(uavg0, "mV", 2047 ) \
VALUE_ENTRY(umin0, "mV", 2048 ) \
VALUE_ENTRY(umax0, "mV", 2049 ) \
VALUE_ENTRY(uavg1, "mV", 2050 ) \
VALUE_ENTRY(umin1, "mV", 2051 ) \
VALUE_ENTRY(umax1, "mV", 2052 ) \
VALUE_ENTRY(uavg2, "mV", 2053 ) \
VALUE_ENTRY(umin2, "mV", 2054 ) \
VALUE_ENTRY(umax2, "mV", 2055 ) \
VALUE_ENTRY(uavg3, "mV", 2056 ) \
VALUE_ENTRY(umin3, "mV", 2057 ) \
VALUE_ENTRY(umax3, "mV", 2058 ) \
VALUE_ENTRY(uavg4, "mV", 2059 ) \
VALUE_ENTRY(umin4, "mV", 2060 ) \
VALUE_ENTRY(umax4, "mV", 2061 ) \
VALUE_ENTRY(uavg5, "mV", 2062 ) \
VALUE_ENTRY(umin5, "mV", 2063 ) \
VALUE_ENTRY(umax5, "mV", 2064 ) \
VALUE_ENTRY(udelta, "mV", 2005 ) \
VALUE_ENTRY(utotal, "mV", 2039 ) \
VALUE_ENTRY(u0, "mV", 2006 ) \
Expand Down Expand Up @@ -97,16 +133,24 @@


/***** Enum String definitions *****/
#define OPMODES "0=Off, 1=Run"
#define OPMODES "0=Boot, 1=GetAddr, 2=SetAddr, 3=Init, 4=Run, 5=RunBalance"
#define OFFON "0=Off, 1=On"
#define BAL "0=None, 1=Discharge, 2=ChargePos, 3=ChargeNeg"
#define IDCMODES "0=Off, 1=AdcSingle, 2=AdcDifferential, 3=IsaCan"
#define TEMPSNS "-1=Off, 0=JCurve, 1=KTY81, 2=PT1000, 3=Leaf"
#define CAT_TEST "Testing"
#define CAT_BMS "BMS"
#define CAT_SENS "Sensor setup"
#define CAT_COMM "Communication"

#define VERSTR STRINGIFY(4=VER-name)
#define VERSTR STRINGIFY(4=VER)

/***** enums ******/

enum
{
IDC_OFF, IDC_SINGLE, IDC_DIFFERENTIAL, IDC_ISACAN
};

enum _canspeeds
{
Expand Down

0 comments on commit 0aec57f

Please sign in to comment.