Red Bird Racing VCU v2
 
Loading...
Searching...
No Matches
CarState.hpp
Go to the documentation of this file.
1/**
2 * @file CarState.hpp
3 * @author Planeson, Red Bird Racing
4 * @brief Definition of the CarState structure representing the state of the car
5 * @version 1.4.1
6 * @date 2026-02-09
7 * @see can.h, Enums.h
8 */
9
10#ifndef CAR_STATE_HPP
11#define CAR_STATE_HPP
12
13#include "Enums.hpp"
14#include <can.h>
15#include <stdint.h>
16
17constexpr canid_t TELEMETRY_PEDAL_MSG = 0x700; /**< Telemetry: Pedal readings message */
18constexpr canid_t TELEMETRY_MOTOR_MSG = 0x701; /**< Telemetry: Digital signals message */
19constexpr canid_t TELEMETRY_BMS_MSG = 0x710; /**< Telemetry: Car state message */
20
21/**
22 * @brief Telemetry frame structure for the Pedals.
23 */
25{
26 uint16_t apps_5v; /**< ADC reading for 5V APPS */
27 uint16_t apps_3v3; /**< ADC reading for 3.3V APPS */
28 uint16_t brake; /**< ADC reading for brake pedal */
29 uint16_t hall_sensor; /**< ADC reading for hall sensor */
30
31 /** @brief Union of bits for car status besides Pedal */
33 {
34 uint8_t byte; /**< Byte representation of the status bits */
35
36 /** @brief Bitfield representation of the status bits */
37 struct Bits
38 {
39 CarStatus car_status : 2; /**< Current car status, produces compiler warning before GCC 9.3 due to bug */
40 bool state_unknown : 1; /**< Unknown car state */
41 bool hv_ready : 1; /**< High voltage ready */
42 bool bms_no_msg : 1; /**< BMS read no message */
43 bool motor_no_read : 1; /**< MCU read no message */
44 bool screenshot : 1; /**< Screenshot, throttle + brake > threshold */
45 bool force_stop : 1; /**< Fault forced car to stop */
47 };
48 /** @brief Union of bits for pedal faults */
50 {
51 uint8_t byte; /**< Byte representation of the fault bits */
52
53 /** @brief Bitfield representation of the fault bits */
54 struct Bits
55 {
56 bool fault_active : 1; /**< Pedal faulty now, only one resetable */
57 bool fault_exceeded : 1; /**< Current pedal fault exceeded allowed time */
58 bool apps_5v_low : 1; /**< APPS 5V considered shorted to ground*/
59 bool apps_5v_high : 1; /**< APPS 5V considered shorted to rail */
60 bool apps_3v3_low : 1; /**< APPS 3V3 considered shorted to ground */
61 bool apps_3v3_high : 1; /**< APPS 3V3 considered shorted to rail */
62 bool brake_low : 1; /**< Brake considered shorted to ground */
63 bool brake_high : 1; /**< Brake considered shorted to rail */
65 };
66
67 static_assert(sizeof(StateByteStatus) == 1, "TelemetryStateByte0 must be 1 byte"); // ensure compile is shoving the bits as expected
68 static_assert(sizeof(StateByteFaults) == 1, "TelemetryStateByte1 must be 1 byte");
69
70 StateByteStatus status; /**< Car Status */
71 StateByteFaults faults; /**< Pedal Faults */
72
73 /**
74 * @brief Converts the TelemetryFramePedal to a CAN frame.
75 * @return CAN frame representing the Pedal telemetry signals.
76 */
77 constexpr can_frame toCanFrame() const
78 {
79 return can_frame{
80 TELEMETRY_PEDAL_MSG, // can_id
81 8, // can_dlc
82 static_cast<__u8>(apps_5v & 0xFF), // data
83 static_cast<__u8>(((apps_5v >> 8) & 0x03) | ((apps_3v3 & 0x3F) << 2)),
84 static_cast<__u8>(((apps_3v3 >> 6) & 0x0F) | ((brake & 0x0F) << 4)),
85 static_cast<__u8>(((brake >> 4) & 0x3F) | ((hall_sensor & 0x03) << 6)),
86 static_cast<__u8>((hall_sensor >> 2) & 0xFF),
89 0x00};
90 }
91};
92
93/**
94 * @brief Telemetry frame structure for motor signals.
95 */
97{
98 int16_t torque_val; /**< Torque value sent to motor controller*/
99 uint16_t motor_rpm; /**< Motor RPM */
100 uint16_t motor_error; /**< Motor status byte */
101 uint16_t motor_warn; /**< Motor error/warning byte */
102
103 /**
104 * @brief Converts the TelemetryFrameMotor to a CAN frame.
105 * @return CAN frame representing the telemetry motor signals.
106 */
107 constexpr can_frame toCanFrame() const
108 {
109 return can_frame{
110 TELEMETRY_MOTOR_MSG, // can_id
111 8, // can_dlc
112 static_cast<__u8>(torque_val & 0xFF),
113 static_cast<__u8>((torque_val >> 8) & 0xFF),
114 static_cast<__u8>(motor_rpm & 0xFF),
115 static_cast<__u8>((motor_rpm >> 8) & 0xFF),
116 static_cast<__u8>(motor_error & 0xFF),
117 static_cast<__u8>((motor_error >> 8) & 0xFF),
118 static_cast<__u8>(motor_warn & 0xFF),
119 static_cast<__u8>((motor_warn >> 8) & 0xFF)};
120 }
121};
122
123/**
124 * @brief Telemetry frame structure for the BMS data.
125 */
127{
128
129 uint8_t bms_data[8]; /**< Raw BMS data bytes */
130
131 /**
132 * @brief Converts the TelemetryFrameBms to a CAN frame.
133 * @return CAN frame representing the telemetry BMS data.
134 */
135 constexpr can_frame toCanFrame() const
136 {
137 return can_frame{
138 TELEMETRY_BMS_MSG, // can_id
139 8, // can_dlc
140 bms_data[0],
141 bms_data[1],
142 bms_data[2],
143 bms_data[3],
144 bms_data[4],
145 bms_data[5],
146 bms_data[6],
147 bms_data[7]};
148 }
149};
150
151/**
152 * @brief Represents the state of the car.
153 * Holds telemetry data and status, used as central data sharing structure.
154 *
155 * @see TelemetryFramePedal, TelemetryFrameMotor, TelemetryFrameBms
156 */
158{
159 TelemetryFramePedal pedal; /**< Struct holding pedal telemetry data, ready for sending over CAN */
160 TelemetryFrameMotor motor; /**< Struct holding motor telemetry data, ready for sending over CAN */
161 TelemetryFrameBms bms; /**< Struct holding BMS telemetry data, ready for sending over CAN */
162 uint32_t status_millis; /**< Millisecond counter for the current car status (for state transitions) */
163 uint32_t millis; /**< Current time in milliseconds for the current loop iteration */
164};
165#endif // CAR_STATE_HPP
constexpr canid_t TELEMETRY_PEDAL_MSG
Definition: CarState.hpp:17
constexpr canid_t TELEMETRY_MOTOR_MSG
Definition: CarState.hpp:18
constexpr canid_t TELEMETRY_BMS_MSG
Definition: CarState.hpp:19
Enumeration definitions for the VCU.
CarStatus
Main car status state machine.
Definition: Enums.hpp:20
Represents the state of the car. Holds telemetry data and status, used as central data sharing struct...
Definition: CarState.hpp:158
uint32_t millis
Definition: CarState.hpp:163
TelemetryFramePedal pedal
Definition: CarState.hpp:159
TelemetryFrameMotor motor
Definition: CarState.hpp:160
uint32_t status_millis
Definition: CarState.hpp:162
TelemetryFrameBms bms
Definition: CarState.hpp:161
Telemetry frame structure for the BMS data.
Definition: CarState.hpp:127
constexpr can_frame toCanFrame() const
Converts the TelemetryFrameBms to a CAN frame.
Definition: CarState.hpp:135
uint8_t bms_data[8]
Definition: CarState.hpp:129
Telemetry frame structure for motor signals.
Definition: CarState.hpp:97
uint16_t motor_error
Definition: CarState.hpp:100
constexpr can_frame toCanFrame() const
Converts the TelemetryFrameMotor to a CAN frame.
Definition: CarState.hpp:107
uint16_t motor_rpm
Definition: CarState.hpp:99
Bitfield representation of the fault bits.
Definition: CarState.hpp:55
Bitfield representation of the status bits.
Definition: CarState.hpp:38
Telemetry frame structure for the Pedals.
Definition: CarState.hpp:25
StateByteStatus status
Definition: CarState.hpp:70
uint16_t hall_sensor
Definition: CarState.hpp:29
StateByteFaults faults
Definition: CarState.hpp:71
constexpr can_frame toCanFrame() const
Converts the TelemetryFramePedal to a CAN frame.
Definition: CarState.hpp:77
Union of bits for pedal faults.
Definition: CarState.hpp:50
struct TelemetryFramePedal::StateByteFaults::Bits bits
Union of bits for car status besides Pedal.
Definition: CarState.hpp:33
struct TelemetryFramePedal::StateByteStatus::Bits bits