Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
10 changes: 10 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}
Binary file added bms-firmware.zip
Binary file not shown.
Binary file added bms-firmware/.DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions bms-firmware/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
10 changes: 10 additions & 0 deletions bms-firmware/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}
3 changes: 3 additions & 0 deletions bms-firmware/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"FSharp.suggestGitignore": false
}
114 changes: 114 additions & 0 deletions bms-firmware/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# BMS Firmware Documentation
> WARNING: Not all features described by this document have been implemented/tested yet.

## Table of Contents
- [Overview](#overview)
- [main](#main)
- [bms_interface](#bms_interface)
- [enable_adc](#enable_adc)
- [enable_discharge_mosfet](#enable_discharge_mosfet)
- [set_cell_overvoltage_protection](#set_cell_overvoltage_protection)
- [set_cell_undervoltage_protection](#set_cell_undervoltage_protection)
- [set_cell_overcurrent_discharge_protection](#set_overcurrent_discharge_protection)
- [set_cell_short_circuit_protection](#set_short_circuit_protection)
## TODO
- Implement *Setpoints.h*. This should be a simple matter of writing a bunch of #defines.
- Implement *read_cell_voltages* in *bms_interface*.
- Clean up *main.cpp*. This is the hardest task, so I broke it down into subtasks:
- Many functions, global constants, #defines, etc. may become reduntant once the other tasks have been implemented. Remove them.
- Anyone reading *main.cpp* should have a high-level idea of *what* the program is doing without having to understand the specifics of *how* the program is doing it. If you find yourself writing highly specific code (such as talking directly to a bms chip via I2C), that code probably belongs in a library, not *main.cpp*.
- Consider that additional features will be added to this code in the future (such as reading current values and errors, then publishing them). Keep this in mind when writing your code so that you can do this easily in the future.
- If you feel up to it, you can also update the table of contents for this document, since it is now missing some headers.

Specifications for each of these tasks can be found further below in the documentation.
## Overview

## main
### setup
The setup block sets all the initial vaulues for each bms chip. It does so by using the *bms_interface* and *Setpoints* libraries.
### loop
The program's main loop polls the state of each bms chip at regular intervals and prints all the data to the serial monitor.

## bms_interface
The *bms_interface.cpp* file abstracts away the BMS chip's details, leading to simpler implementation. Below is the usage guide for this interface:

### enable_adc
```cpp
void enable_adc(byte i2c_addr);
```
Enables ADCs (analog-to-digital converters) on the selected BMS so it can read the appropriate voltage and current values.
- **params:**
- **i2c_addr (byte):** I2C address of the BMS chip with which to communicate.
- **returns:**
- none

### enable_discharge_mosfet
```cpp
void enable_discharge_mosfet(byte i2c_addr);
```
Enables the selected BMS's discharge MOSFET, which allows the battery to discharge power into the rest of the rover.
- **params:**
- **i2c_addr (byte):** I2C address of the BMS chip with which to communicate.
- **returns:**
- none

### set_cell_overvoltage_protection
```cpp
void set_cell_overvoltage_protection(byte i2c_addr, int voltage_mv);
```
Sets the cell overvoltage protection level for the selected BMS chip. The BMS will shut down the rover's power if any cell exceeds this voltage level.
- **params:**
- **i2c_addr (byte):** I2C address of the BMS chip with which to communicate.
- **voltage_mv (int):** Voltage (in mV) to set the overvoltage protection level to.
- **returns:**
- none

### set_cell_undervoltage_protection
```cpp
void set_cell_undervoltage_protection(byte i2c_addr, int voltage_mv);
```
Sets the cell undervoltage protection level for the selected BMS chip. The BMS will shut down the rover's power if any cell falls below this voltage level.
- **params:**
- **i2c_addr (byte):** I2C address of the BMS chip with which to communicate.
- **voltage_mv (int):** Voltage (in mV) to set the undervoltage protection level to.
- **returns:**
- none

### set_overcurrent_discharge_protection
```cpp
void set_overcurrent_discharge_protection(byte i2c_addr, int curent_ma);
```
Sets the current limit for the appropriate BMS chip. The BMS will shut down the rover's power if the power system exceeds this current level.
- **params:**
- **i2c_addr (byte):** I2C address of the BMS chip with which to communicate.
- **current_ma (int):** Current (in mA) to set the overcurrent discharge protection level to.
- **returns:**
- none

### set_short_circuit_protection
```cpp
void set_short_circuit_protection(byte i2c_addr, int current_ma);
```
Sets the current level for detecting a short circuit. Selected BMS will shut down when this current level has been exceeded.
- **params:**
- **i2c_addr (byte):** I2C address of the BMS chip with which to communicate.
- **current_ma (int):** Current (in mA) to set the short circuit protection level to.
- **returns:**
- none
### read_cell_voltages
```cpp
void read_cell_voltages(byte i2c_addr, int *cell_voltage_array, int array_len);
```
Reads all cell voltages from the selected bms chip. Note that the cell voltage array is modified in-place, therefore this function has no return value.
- **params:**
- **i2c_addr (byte):** I2C address of the BMS chip with which to communicate.
- **cell_voltage_array (int array):** Array of cell voltages to modify (in-place).
- **array_len (int):** Length of the array to modify.
## _read_register
when i have time ill add the params and everytjing for these two fxns too
## _write_register

## Registers
The *Registers.h* file contains the addresses for each bms chip's corresponding registers. It's a simple layer of abstraction that allows for easier development when interfacing directly with the bms chips.
## Setpoints
The *Setpoints.h* file contains each bms chip's intitial undervoltage level, overvoltage level, etc. It allows the Electrical division to tune the bms without having to understand the code.
39 changes: 39 additions & 0 deletions bms-firmware/include/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

This directory is intended for project header files.

A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.

```src/main.c

#include "header.h"

int main (void)
{
...
}
```

Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.

In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.

Read more about using header files in official GCC documentation:

* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes

https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
Binary file added bms-firmware/lib/.DS_Store
Binary file not shown.
46 changes: 46 additions & 0 deletions bms-firmware/lib/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.

The source code of each library should be placed in an own separate directory
("lib/your_library_name/[here are source files]").

For example, see a structure of the following two libraries `Foo` and `Bar`:

|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c

and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>

int main (void)
{
...
}

```

PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.

More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html
52 changes: 52 additions & 0 deletions bms-firmware/lib/Registers/Registers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// register map of addresses for I2C communication

#define SYS_STAT 0x00
#define CELLBAL1 0x01
#define CELLBAL2 0x02
#define CELLBAL3 0x03
#define SYS_CTRL1 0x04
#define SYS_CTRL2 0x05
#define PROTECT1 0x06
#define PROTECT2 0x07
#define PROTECT3 0x08
#define OV_TRIP 0x09
#define UV_TRIP 0x0A
#define CC_CFG 0x0B

#define VC1_HI_BYTE 0x0C
#define VC1_LO_BYTE 0x0D
#define VC2_HI_BYTE 0x0E
#define VC2_LO_BYTE 0x0F
#define VC3_HI_BYTE 0x10
#define VC3_LO_BYTE 0x11
#define VC4_HI_BYTE 0x12
#define VC4_LO_BYTE 0x13
#define VC5_HI_BYTE 0x14
#define VC5_LO_BYTE 0x15
#define VC6_HI_BYTE 0x16
#define VC6_LO_BYTE 0x17
#define VC7_HI_BYTE 0x18
#define VC7_LO_BYTE 0x19
#define VC8_HI_BYTE 0x1A
#define VC8_LO_BYTE 0x1B
#define VC9_HI_BYTE 0x1C
#define VC9_LO_BYTE 0x1D
#define VC10_HI_BYTE 0x1E
#define VC10_LO_BYTE 0x1F

#define BAT_HI_BYTE 0x2A
#define BAT_LO_BYTE 0x2B

#define TS1_HI_BYTE 0x2C
#define TS1_LO_BYTE 0x2D
#define TS2_HI_BYTE 0x2E
#define TS2_LO_BYTE 0x2F
#define TS3_HI_BYTE 0x30
#define TS3_LO_BYTE 0x31

#define CC_HI_BYTE 0x32
#define CC_LO_BYTE 0x33

#define ADCGAIN1 0x50
#define ADCOFFSET 0x51
#define ADCGAIN2 0x59
5 changes: 5 additions & 0 deletions bms-firmware/lib/Setpoints/Setpoints.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#define OV_THRESH_MV 4500 //4.5V for overvoltage
#define UV_THRESH_MV 3000 //3.0V for undervoltage
#define OC_THRESH_MA 50000 //50A for Overcurrent (im just taking values from past code, maybe change)
#define SC_THRESH_MA 75000 //75A for Short Circuit
// gotta do a bunch of tests to see general setpoints for all necessary values
Loading