Red Bird Racing VCU v2
 
Loading...
Searching...
No Matches
BMS.hpp
Go to the documentation of this file.
1/**
2 * @file BMS.hpp
3 * @author Planeson, Red Bird Racing
4 * @brief Declaration of the BMS class for managing the Accumulator (Kclear BMS) via CAN bus
5 * @version 1.2
6 * @date 2026-02-04
7 * @see BMS.cpp
8 * @dir BMS @brief The BMS library contains the BMS class for managing the Accumulator (Kclear BMS) via CAN bus, including starting HV and checking BMS status.
9 */
10
11#ifndef BMS_HPP
12#define BMS_HPP
13
14#include "Scheduler.hpp"
15#include "CarState.hpp"
16
17// ignore -Wpedantic warnings for mcp2515.h
18#pragma GCC diagnostic push
19#pragma GCC diagnostic ignored "-Wpedantic"
20#include <mcp2515.h>
21#pragma GCC diagnostic pop
22
23constexpr uint32_t BMS_COMMAND = 0x1801F340; /**< BMS command ID */
24constexpr uint32_t BMS_SEND_CMD = BMS_COMMAND | CAN_EFF_FLAG; /**< BMS command ID with Extended Frame Format flag */
25
26constexpr uint32_t BMS_INFO = 0x186040F3; /**< BMS info ID */
27constexpr uint32_t BMS_INFO_EXT = BMS_INFO | CAN_EFF_FLAG; /**< BMS info ID with Extended Frame Format flag */
28
29/** Start HV command frame */
30constexpr can_frame start_hv_msg = {
31 BMS_SEND_CMD, /**< can_id */
32 2, /**< can_dlc */
33 {0x01, 0x01} /**< data: MainRlyCmd = 1 (output HV), ShutDownCmd = 1 (do not shutdown) */
34};
35
36/** Stop HV command frame */
37constexpr can_frame stop_hv_msg = {
38 BMS_SEND_CMD, /**< can_id */
39 2, /**< can_dlc */
40 {0x00, 0x00} /**< data: MainRlyCmd = 0 (break open HV), ShutDownCmd = 0 (shutdown) */
41};
42
43/**
44 * @brief BMS class for managing the Accumulator (Kclear BMS) via CAN bus
45 */
46class BMS
47{
48public:
49 BMS(MCP2515 &bms_can_, CarState &car_);
50 /**
51 * @brief Returns true if HV has been started
52 * @return true if HV started, false otherwise
53 */
54 bool hvReady() const { return car.pedal.status.bits.hv_ready; };
55 void initFilter();
56 void checkHv();
57
58private:
59 MCP2515 &bms_can; /**< Reference to MCP2515 for BMS CAN bus */
60 /** Local storage for received BMS CAN frame */
61 can_frame rx_bms_msg = {
62 0, /**< can_id */
63 0, /**< can_dlc */
64 {0}, /**< data */
65 };
66 CarState &car; /**< Reference to CarState, for the status flags and setting BMS data */
67};
68#endif // BMS_HPP
constexpr can_frame start_hv_msg
Definition: BMS.hpp:30
constexpr uint32_t BMS_COMMAND
Definition: BMS.hpp:23
constexpr uint32_t BMS_INFO_EXT
Definition: BMS.hpp:27
constexpr can_frame stop_hv_msg
Definition: BMS.hpp:37
constexpr uint32_t BMS_SEND_CMD
Definition: BMS.hpp:24
constexpr uint32_t BMS_INFO
Definition: BMS.hpp:26
Definition of the CarState structure representing the state of the car.
Declaration of the Scheduler class template, for scheduling tasks on multiple MCP2515 instances.
BMS class for managing the Accumulator (Kclear BMS) via CAN bus.
Definition: BMS.hpp:47
can_frame rx_bms_msg
Definition: BMS.hpp:61
MCP2515 & bms_can
Definition: BMS.hpp:59
void checkHv()
Attempts to start HV. First check BMS is in standby(3) state, then send the HV start command....
Definition: BMS.cpp:62
void initFilter()
Initializes the CAN filters for reading BMS data. Call after constructing the BMS object and the MCP2...
Definition: BMS.cpp:45
CarState & car
Definition: BMS.hpp:66
bool hvReady() const
Returns true if HV has been started.
Definition: BMS.hpp:54
Represents the state of the car. Holds telemetry data and status, used as central data sharing struct...
Definition: CarState.hpp:158
TelemetryFramePedal pedal
Definition: CarState.hpp:159
StateByteStatus status
Definition: CarState.hpp:70
struct TelemetryFramePedal::StateByteStatus::Bits bits