Red Bird Racing VCU
v2
Loading...
Searching...
No Matches
BMS.cpp
Go to the documentation of this file.
1
/**
2
* @file BMS.cpp
3
* @author Planeson, Chiho, Red Bird Racing
4
* @brief Implementation of the BMS class for managing the Accumulator (Kclear BMS) via CAN bus
5
* @version 1.4
6
* @date 2026-02-25
7
* @see BMS.hpp
8
*/
9
10
#include "
BMS.hpp
"
11
#include "
Debug.hpp
"
12
#include "
Enums.hpp
"
13
#include "
CarState.hpp
"
14
15
// ignore -Wunused-parameter warnings for Debug.h
16
#pragma GCC diagnostic push
17
#pragma GCC diagnostic ignored "-Wunused-parameter"
18
#include "
Debug.hpp
"
// DBGLN_GENERAL
19
#pragma GCC diagnostic pop
20
21
// ignore -Wpedantic warnings for mcp2515.h
22
#pragma GCC diagnostic push
23
#pragma GCC diagnostic ignored "-Wpedantic"
24
#include <mcp2515.h>
// CAN frames, sending CAN frame, reading CAN frame
25
#pragma GCC diagnostic pop
26
27
/**
28
* @brief Construct a new BMS object, initing car.pedal.status.bits.hv_ready to false
29
* @param bms_can_ Reference to MCP2515 for BMS CAN bus
30
* @param car_ Reference to CarState, for the status flags and setting BMS data
31
*/
32
BMS::BMS
(MCP2515 &bms_can_,
CarState
&car_)
33
: bms_can(bms_can_),
car
(car_)
34
{
35
car
.
pedal
.
status
.
bits
.
hv_ready
=
false
;
36
}
37
38
/**
39
* @brief Initializes the CAN filters for reading BMS data.
40
* Call after constructing the BMS object and the MCP2515 object it references to ensure BMS data is being read correctly.
41
* This function will block until the filter is set correctly on the MCP2515. If it never succeed, the program will be stuck here.
42
* Consider that in the case that we can't even set the filter on the MCP2515, we probably can't communicate with the BMS at all,
43
* so being stuck here is acceptable since the car won't start without BMS communication anyway.
44
*/
45
void
BMS::initFilter
()
46
{
47
bms_can
.setConfigMode();
48
while
(
bms_can
.setFilterMask(MCP2515::MASK0,
true
, 0x7FF) != MCP2515::ERROR_OK)
49
;
50
while
(
bms_can
.setFilter(MCP2515::RXF0,
true
,
BMS_INFO_EXT
) != MCP2515::ERROR_OK)
51
;
52
bms_can
.setNormalMode();
53
}
54
55
/**
56
* @brief Attempts to start HV.
57
* First check BMS is in standby(3) state, then send the HV start command.
58
* Keep sending the command until the BMS state changes to precharge(4).
59
* Sets car.pedal.status.bits.hv_ready to true when BMS state changes to run(5).
60
*
61
*/
62
void
BMS::checkHv
()
63
{
64
car
.
pedal
.
status
.
bits
.
bms_no_msg
=
false
;
65
if
(
car
.
pedal
.
status
.
bits
.
hv_ready
)
66
return
;
// already started
67
car
.
pedal
.
status
.
bits
.
hv_ready
=
false
;
68
if
(
bms_can
.readMessage(&
rx_bms_msg
) == MCP2515::ERROR_NOMSG)
69
{
70
car
.
pedal
.
status
.
bits
.
bms_no_msg
=
true
;
71
return
;
72
}
73
74
/* now that we set filter on MCP2515, it's impossible to get wrong ID
75
// Check if the BMS is in standby state (0x3 in upper 4 bits)
76
if (rx_bms_msg.can_id != BMS_INFO_EXT)
77
{
78
car.pedal.status.bits.bms_wrong_id = true;
79
return;
80
} // Not a BMS info frame, retry
81
*/
82
83
switch
(
rx_bms_msg
.data[6] & 0xF0)
84
{
85
case
0x30:
// Standby state
86
bms_can
.sendMessage(&
start_hv_msg
);
87
DBGLN_GENERAL
(
"BMS in standby state, sent start HV cmd"
);
88
// sent start HV cmd, wait for BMS to change state
89
return
;
90
case
0x40:
// Precharge state
91
bms_can
.sendMessage(&
start_hv_msg
);
92
DBGLN_GENERAL
(
"BMS in precharge state, HV starting"
);
93
return
;
// BMS is in precharge state, wait
94
case
0x50:
// Run state
95
DBGLN_GENERAL
(
"BMS in run state, HV started"
);
96
car
.
pedal
.
status
.
bits
.
hv_ready
=
true
;
// BMS is in run state
97
return
;
98
default
:
99
DBGLN_GENERAL
(
"BMS in unknown state, retrying..."
);
100
return
;
// Unknown state, retry
101
}
102
}
BMS.hpp
Declaration of the BMS class for managing the Accumulator (Kclear BMS) via CAN bus.
start_hv_msg
constexpr can_frame start_hv_msg
Definition:
BMS.hpp:30
BMS_INFO_EXT
constexpr uint32_t BMS_INFO_EXT
Definition:
BMS.hpp:27
CarState.hpp
Definition of the CarState structure representing the state of the car.
Debug.hpp
Debugging macros and functions for serial and CAN output.
DBGLN_GENERAL
void DBGLN_GENERAL(const char *x)
Prints a line to the serial console for general debug.
Definition:
Debug.hpp:56
Enums.hpp
Enumeration definitions for the VCU.
BMS::rx_bms_msg
can_frame rx_bms_msg
Definition:
BMS.hpp:61
BMS::bms_can
MCP2515 & bms_can
Definition:
BMS.hpp:59
BMS::checkHv
void checkHv()
Attempts to start HV. First check BMS is in standby(3) state, then send the HV start command....
Definition:
BMS.cpp:62
BMS::initFilter
void initFilter()
Initializes the CAN filters for reading BMS data. Call after constructing the BMS object and the MCP2...
Definition:
BMS.cpp:45
BMS::BMS
BMS(MCP2515 &bms_can_, CarState &car_)
Construct a new BMS object, initing car.pedal.status.bits.hv_ready to false.
Definition:
BMS.cpp:32
BMS::car
CarState & car
Definition:
BMS.hpp:66
car
struct CarState car
Global car state structure.
Definition:
main.cpp:65
CarState
Represents the state of the car. Holds telemetry data and status, used as central data sharing struct...
Definition:
CarState.hpp:158
CarState::pedal
TelemetryFramePedal pedal
Definition:
CarState.hpp:159
TelemetryFramePedal::StateByteStatus::Bits::bms_no_msg
bool bms_no_msg
Definition:
CarState.hpp:42
TelemetryFramePedal::StateByteStatus::Bits::hv_ready
bool hv_ready
Definition:
CarState.hpp:41
TelemetryFramePedal::status
StateByteStatus status
Definition:
CarState.hpp:70
TelemetryFramePedal::StateByteStatus::bits
struct TelemetryFramePedal::StateByteStatus::Bits bits
vehicle_control_unit
lib
BMS
BMS.cpp
Generated by
1.9.6