Red Bird Racing VCU v2
 
Loading...
Searching...
No Matches
Pedal.hpp
Go to the documentation of this file.
1/**
2 * @file Pedal.hpp
3 * @author Planeson, Red Bird Racing
4 * @brief Declaration of the Pedal class for handling throttle and brake pedal inputs
5 * @version 1.6
6 * @date 2026-02-12
7 * @see Pedal.cpp
8 * @dir Pedal @brief The Pedal library contains the Pedal class to manage throttle and brake pedal inputs, including filtering, fault detection, and CAN communication.
9 */
10
11#ifndef PEDAL_HPP
12#define PEDAL_HPP
13
14#include <stdint.h>
15#include "CarState.hpp"
16#include "Interp.hpp"
17#include "Curves.hpp"
18#include "SignalProcessing.hpp"
19
20// ignore -Wpedantic warnings for mcp2515.h
21#pragma GCC diagnostic push
22#pragma GCC diagnostic ignored "-Wpedantic"
23#include <mcp2515.h>
24#pragma GCC diagnostic pop
25
26// Constants
27
28constexpr bool REGEN_ENABLED = true; /**< Boolean toggle for regenerative braking; false disables reverse torque. */
29
30constexpr bool FLIP_MOTOR_DIR = false; /**< Boolean toggle to flip motor direction; true inverts torque commands. */
31
32constexpr bool BRAKE_RELIABLE = true; /**< brake assumed reliable; enable checking of min/max (narrow range); set to false compromises safety! */
33
34constexpr uint16_t FAULT_CHECK_HEX = BRAKE_RELIABLE ? 0xFE : 0x3E; /**< Hex mask for fault checking based on brake reliability. */
35
36constexpr uint32_t MAX_MOTOR_READ_MILLIS = 100; /**< Maximum time in milliseconds between motor data reads before disabling regen. */
37
38/**
39 * @brief Namespace for pedal-related constants, such as thresholds and calculation parameters.
40 * This is to avoid polluting the Pedal class with intermediate results.
41 */
43{
44 constexpr uint8_t MIN_REGEN_KMH = 10; /**< Minimum speed (km/h) for regenerative braking to be active. */
45 constexpr uint8_t GEAR_RATIO_NUMERATOR = 60; /**< Gear ratio numerator of the drivetrain. */
46 constexpr uint8_t GEAR_RATIO_DENOMINATOR = 13; /**< Gear ratio denominator of the drivetrain. */
47
48 // === Calculation for RPM threshold ===
49 constexpr uint8_t WHEEL_DIAMETER_INCH = 13; /**< Wheel diameter in inches. */
50 constexpr uint16_t MAX_MOTOR_RPM = 7000; /**< Maximum motor RPM. */
51 constexpr uint16_t MAX_TORQUE_VAL = 32767; /**< Maximum torque value for motor controller. */
52
53 constexpr uint16_t INCH_PER_KM = 39370; /**< Inches per kilometer. */
54 constexpr uint8_t MINUTES_PER_HOUR = 60; /**< Minutes per hour. */
55 constexpr double PI_ = 3.1415926535897932384626433832795; /**< Value of pi, unnamed to avoid clashing with Arduino.h's definition. */
56
57 /** Final RPM = KMH -> Inches per Hour -> Inch per minute -> RPM at wheel -> RPM at motor */
58 constexpr int16_t MIN_REGEN_RPM_VAL =
59 (double)MIN_REGEN_KMH / MINUTES_PER_HOUR * INCH_PER_KM / WHEEL_DIAMETER_INCH / PI_ * GEAR_RATIO_NUMERATOR / GEAR_RATIO_DENOMINATOR * MAX_TORQUE_VAL / MAX_MOTOR_RPM; /**< Minimum RPM for regenerative braking to be active. */
60} // namespace PedalConstants
61constexpr uint8_t ADC_BUFFER_SIZE = 16; /**< Size of the ADC reading buffer for filtering. */
62
63/**
64 * @brief Pedal class for managing throttle and brake pedal inputs.
65 * Handles filtering, fault detection, and CAN frame updates.
66 */
67class Pedal
68{
69public:
70 Pedal(MCP2515 &motor_can_, CarState &car, uint16_t &pedal_final_);
71 void update(uint16_t pedal_1, uint16_t pedal_2, uint16_t brake);
72 void sendFrame();
73 void initFilter();
74 bool initMotor();
75 void readMotor();
76 uint16_t &pedal_final; /**< Final pedal value is taken directly from apps_5v, see initializer */
77
78private:
79 CarState &car; /**< Reference to CarState */
80 MCP2515 &motor_can; /**< Reference to MCP2515 for sending CAN messages */
81 uint32_t fault_start_millis; /**< Timestamp for when a fault started */
82 uint32_t last_motor_read_millis; /**< Timestamp for the last motor data read */
83
84 bool got_speed; /**< Flag indicating if motor speed data has been successfully read */
85 bool got_error; /**< Flag indicating if motor error data has been successfully read */
86
87 /**
88 * @brief CAN frame to stop the motor
89 */
90 const can_frame stop_frame = {
91 MOTOR_SEND, /**< can_id */
92 3, /**< can_dlc */
93 0x90, /**< data, torque command */
94 0x00, /**< data, 0 torque * 2 */
95 0x00};
96
97 /**
98 * @brief CAN frame for torque command
99 */
100 can_frame torque_msg = {
101 MOTOR_SEND, /**< can_id */
102 3, /**< can_dlc */
103 0x90, /**< data, torque command */
104 0x00, /**< data, init as 0 torque * 2 */
105 0x00};
106
107 // Filters for pedal and brake inputs, see Signal_Processing.hpp for options
108 ExponentialFilter<uint16_t, uint16_t> pedal1_filter; /**< Filter for first pedal sensor input */
109 ExponentialFilter<uint16_t, uint16_t> pedal2_filter; /**< Filter for second pedal sensor input */
110 ExponentialFilter<uint16_t, uint16_t> brake_filter; /**< Filter for brake sensor input */
111
112 static constexpr LinearInterp<uint16_t, int16_t, int32_t, 5> THROTTLE_MAP{THROTTLE_TABLE}; /**< Interpolation map for throttle torque */
113 static constexpr LinearInterp<uint16_t, int16_t, int32_t, 5> BRAKE_MAP{BRAKE_TABLE}; /**< Interpolation map for brake torque */
114 static constexpr LinearInterp<uint16_t, uint16_t, uint32_t, 3> APPS_3V3_SCALE_MAP{APPS_3V3_SCALE_TABLE}; /**< Interpolation map for APPS_3V3->APPS_5V */
115
116 static constexpr canid_t MOTOR_SEND = 0x201; /**< Motor send CAN ID */
117 static constexpr canid_t MOTOR_READ = 0x181; /**< Motor read CAN ID */
118
119 static constexpr uint8_t REGID_READ = 0x3D; /**< Register ID for reading motor data */
120
121 static constexpr uint8_t SPEED_IST = 0x30; /**< Register ID for "actual speed value" */
122 static constexpr uint8_t WARN_ERR = 0x8F; /**< Register ID for warnings and errors */
123
124 static constexpr uint8_t RPM_PERIOD = 20; /**< Period of reading motor data in ms, set to 20ms to get 10ms reads alongside errors */
125 static constexpr uint8_t ERR_PERIOD = 20; /**< Period of reading motor errors in ms, set to 20ms to get 10ms reads alongside rpm */
126
127 bool checkPedalFault();
128 constexpr int16_t pedalTorqueMapping(const uint16_t pedal, const uint16_t brake, const int16_t motor_rpm, const bool flip_dir);
129
130 MCP2515::ERROR sendCyclicRead(uint8_t reg_id, uint8_t read_period);
131 bool checkCyclicRead(uint8_t reg_id);
132};
133
134#endif // PEDAL_HPP
Definition of the CarState structure representing the state of the car.
Definition of throttle and brake mapping tables.
constexpr TablePoint< uint16_t, int16_t > THROTTLE_TABLE[5]
Throttle mapping table (calculated), maps APPS_5V readings to torque values.
Definition: Curves.hpp:89
constexpr TablePoint< uint16_t, int16_t > BRAKE_TABLE[5]
Brake mapping table, negative values for regen.
Definition: Curves.hpp:48
constexpr TablePoint< uint16_t, uint16_t > APPS_3V3_SCALE_TABLE[3]
APPS_3V3 mapping table, maps 3V3 readings to 5V readings.
Definition: Curves.hpp:58
Declaration and definition of the LinearInterp class template for linear interpolation.
constexpr uint16_t FAULT_CHECK_HEX
Definition: Pedal.hpp:34
constexpr bool FLIP_MOTOR_DIR
Definition: Pedal.hpp:30
constexpr uint8_t ADC_BUFFER_SIZE
Definition: Pedal.hpp:61
constexpr bool BRAKE_RELIABLE
Definition: Pedal.hpp:32
constexpr bool REGEN_ENABLED
Definition: Pedal.hpp:28
constexpr uint32_t MAX_MOTOR_READ_MILLIS
Definition: Pedal.hpp:36
Declaration of signal processing functions.
Filter with exponential moving average algorithm.
Class template for performing linear interpolation using a lookup table.
Definition: Interp.hpp:35
Pedal class for managing throttle and brake pedal inputs. Handles filtering, fault detection,...
Definition: Pedal.hpp:68
void update(uint16_t pedal_1, uint16_t pedal_2, uint16_t brake)
Updates pedal sensor readings, applies filtering, and checks for faults.
Definition: Pedal.cpp:99
void sendFrame()
Sends the appropriate CAN frame to the motor based on pedal and car state.
Definition: Pedal.cpp:156
bool initMotor()
Initializes the motor CAN communication by setting up cyclic reads for motor data and configuring CAN...
Definition: Pedal.cpp:72
bool checkCyclicRead(uint8_t reg_id)
Definition: Pedal.cpp:270
bool checkPedalFault()
Checks for a fault between two pedal sensor readings.
Definition: Pedal.cpp:237
static constexpr uint8_t WARN_ERR
Definition: Pedal.hpp:122
static constexpr LinearInterp< uint16_t, int16_t, int32_t, 5 > BRAKE_MAP
Definition: Pedal.hpp:113
void initFilter()
Initializes the CAN filters for reading motor data. Call after constructing the Pedal object and the ...
Definition: Pedal.cpp:52
MCP2515 & motor_can
Definition: Pedal.hpp:80
static constexpr LinearInterp< uint16_t, int16_t, int32_t, 5 > THROTTLE_MAP
Definition: Pedal.hpp:112
CarState & car
Definition: Pedal.hpp:79
static constexpr canid_t MOTOR_READ
Definition: Pedal.hpp:117
static constexpr LinearInterp< uint16_t, uint16_t, uint32_t, 3 > APPS_3V3_SCALE_MAP
Definition: Pedal.hpp:114
const can_frame stop_frame
CAN frame to stop the motor.
Definition: Pedal.hpp:90
static constexpr uint8_t REGID_READ
Definition: Pedal.hpp:119
void readMotor()
Reads motor data from the CAN bus and updates the CarState.
Definition: Pedal.cpp:286
uint32_t last_motor_read_millis
Definition: Pedal.hpp:82
static constexpr uint8_t RPM_PERIOD
Definition: Pedal.hpp:124
ExponentialFilter< uint16_t, uint16_t > brake_filter
Definition: Pedal.hpp:110
uint16_t & pedal_final
Definition: Pedal.hpp:76
static constexpr canid_t MOTOR_SEND
Definition: Pedal.hpp:116
ExponentialFilter< uint16_t, uint16_t > pedal1_filter
Definition: Pedal.hpp:108
static constexpr uint8_t SPEED_IST
Definition: Pedal.hpp:121
can_frame torque_msg
CAN frame for torque command.
Definition: Pedal.hpp:100
constexpr int16_t pedalTorqueMapping(const uint16_t pedal, const uint16_t brake, const int16_t motor_rpm, const bool flip_dir)
Maps the pedal ADC to a torque value. If no braking requested, maps throttle normally....
Definition: Pedal.cpp:196
static constexpr uint8_t ERR_PERIOD
Definition: Pedal.hpp:125
MCP2515::ERROR sendCyclicRead(uint8_t reg_id, uint8_t read_period)
Sends a cyclic read request to the motor controller for speed (rpm).
Definition: Pedal.cpp:258
uint32_t fault_start_millis
Definition: Pedal.hpp:81
ExponentialFilter< uint16_t, uint16_t > pedal2_filter
Definition: Pedal.hpp:109
bool got_error
Definition: Pedal.hpp:85
bool got_speed
Definition: Pedal.hpp:84
Pedal pedal(mcp2515_DL, car, car.pedal.apps_5v)
Namespace for pedal-related constants, such as thresholds and calculation parameters....
Definition: Pedal.hpp:43
constexpr double PI_
Definition: Pedal.hpp:55
constexpr uint16_t INCH_PER_KM
Definition: Pedal.hpp:53
constexpr uint16_t MAX_MOTOR_RPM
Definition: Pedal.hpp:50
constexpr uint8_t GEAR_RATIO_DENOMINATOR
Definition: Pedal.hpp:46
constexpr uint8_t GEAR_RATIO_NUMERATOR
Definition: Pedal.hpp:45
constexpr uint8_t WHEEL_DIAMETER_INCH
Definition: Pedal.hpp:49
constexpr uint8_t MIN_REGEN_KMH
Definition: Pedal.hpp:44
constexpr uint16_t MAX_TORQUE_VAL
Definition: Pedal.hpp:51
constexpr uint8_t MINUTES_PER_HOUR
Definition: Pedal.hpp:54
constexpr int16_t MIN_REGEN_RPM_VAL
Definition: Pedal.hpp:58
Represents the state of the car. Holds telemetry data and status, used as central data sharing struct...
Definition: CarState.hpp:158