Red Bird Racing VCU v2
 
Loading...
Searching...
No Matches
Pedal Class Reference

Pedal class for managing throttle and brake pedal inputs. Handles filtering, fault detection, and CAN frame updates. More...

#include <Pedal.hpp>

Collaboration diagram for Pedal:

Public Member Functions

 Pedal (MCP2515 &motor_can_, CarState &car, uint16_t &pedal_final_)
 Constructor for the Pedal class. Initializes the pedal state. fault is set to true initially, so you must send update within 100ms of starting the car to clear it. Call the initMotor function to set up the motor CAN filters and cyclic reads after constructing the Pedal object and the MCP2515 object it references.
 
void update (uint16_t pedal_1, uint16_t pedal_2, uint16_t brake)
 Updates pedal sensor readings, applies filtering, and checks for faults.
 
void sendFrame ()
 Sends the appropriate CAN frame to the motor based on pedal and car state.
 
void initFilter ()
 Initializes the CAN filters for reading motor data. Call after constructing the Pedal object and the MCP2515 object it references to ensure motor data is being read correctly. This function will block until the filter is set correctly on the MCP2515. If it never succeed, the program will be stuck here. Consider that when we can't even set the filter on the MCP2515, we probably can't communicate with the motor controller at all, so being stuck here is acceptable since the car won't be drivable without motor communication anyway.
 
bool initMotor ()
 Initializes the motor CAN communication by setting up cyclic reads for motor data and configuring CAN filters. After the constructor of the Pedal class and the MCP2515 object it references are created, first call initFilter to set up the filters, then call this function to start the cyclic reads and ensure that motor data is being read correctly.
 
void readMotor ()
 Reads motor data from the CAN bus and updates the CarState.
 

Public Attributes

uint16_t & pedal_final
 

Private Member Functions

bool checkPedalFault ()
 Checks for a fault between two pedal sensor readings.
 
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. If braking requested and regen enabled, applies regen if motor RPM larger than minimum regen RPM, preventing reverse torque at low speeds. Regen is also disabled if motor rpm isn't read recently to prevent reverse power.
 
MCP2515::ERROR sendCyclicRead (uint8_t reg_id, uint8_t read_period)
 Sends a cyclic read request to the motor controller for speed (rpm).
 
bool checkCyclicRead (uint8_t reg_id)
 

Private Attributes

CarStatecar
 
MCP2515 & motor_can
 
uint32_t fault_start_millis
 
uint32_t last_motor_read_millis
 
bool got_speed
 
bool got_error
 
const can_frame stop_frame
 CAN frame to stop the motor.
 
can_frame torque_msg
 CAN frame for torque command.
 
ExponentialFilter< uint16_t, uint16_t > pedal1_filter
 
ExponentialFilter< uint16_t, uint16_t > pedal2_filter
 
ExponentialFilter< uint16_t, uint16_t > brake_filter
 

Static Private Attributes

static constexpr LinearInterp< uint16_t, int16_t, int32_t, 5 > THROTTLE_MAP {THROTTLE_TABLE}
 
static constexpr LinearInterp< uint16_t, int16_t, int32_t, 5 > BRAKE_MAP {BRAKE_TABLE}
 
static constexpr LinearInterp< uint16_t, uint16_t, uint32_t, 3 > APPS_3V3_SCALE_MAP {APPS_3V3_SCALE_TABLE}
 
static constexpr canid_t MOTOR_SEND = 0x201
 
static constexpr canid_t MOTOR_READ = 0x181
 
static constexpr uint8_t REGID_READ = 0x3D
 
static constexpr uint8_t SPEED_IST = 0x30
 
static constexpr uint8_t WARN_ERR = 0x8F
 
static constexpr uint8_t RPM_PERIOD = 20
 
static constexpr uint8_t ERR_PERIOD = 20
 

Detailed Description

Pedal class for managing throttle and brake pedal inputs. Handles filtering, fault detection, and CAN frame updates.

Definition at line 67 of file Pedal.hpp.

Constructor & Destructor Documentation

◆ Pedal()

Pedal::Pedal ( MCP2515 &  motor_can_,
CarState car_,
uint16_t &  pedal_final_ 
)

Constructor for the Pedal class. Initializes the pedal state. fault is set to true initially, so you must send update within 100ms of starting the car to clear it. Call the initMotor function to set up the motor CAN filters and cyclic reads after constructing the Pedal object and the MCP2515 object it references.

Parameters
motor_can_Reference to the MCP2515 instance for motor CAN communication.
car_Reference to the CarState structure.
pedal_final_Reference to the pedal used as the final pedal value. Although not recommended, you can set another uint16 outside Pedal to be something like 0.3 APPS_1 + 0.7 APPS_2, then reference that here. If in future, this become a sustained need, should consider adding a function pointer to find the final pedal value to let Pedal class call it itself.

Definition at line 34 of file Pedal.cpp.

Member Function Documentation

◆ checkCyclicRead()

bool Pedal::checkCyclicRead ( uint8_t  reg_id)
private

Definition at line 270 of file Pedal.cpp.

References motor_can, and MOTOR_READ.

Referenced by initMotor().

Here is the caller graph for this function:

◆ checkPedalFault()

bool Pedal::checkPedalFault ( )
private

Checks for a fault between two pedal sensor readings.

Scales pedal_2 to match the range of pedal_1, then calculates the absolute difference. If the difference exceeds 10% of the full-scale value (i.e., >102.4 for a 10-bit ADC), the function considers this a fault and returns true. Otherwise, returns false.

Returns
true if the difference exceeds the threshold (fault detected), false otherwise.

< MAX_DELTA is floor of 10% of APPS_5V valid range, later comparison will give rounding room

Definition at line 237 of file Pedal.cpp.

References TelemetryFramePedal::apps_3v3, APPS_3V3_SCALE_MAP, TelemetryFramePedal::apps_5v, APPS_5V_PERCENT_TABLE, car, LinearInterp< Tin, Tout, Tmid, size >::interp(), CarState::pedal, LinearInterp< Tin, Tout, Tmid, size >::range(), and THROTTLE_MAP.

Referenced by update().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initFilter()

void Pedal::initFilter ( )

Initializes the CAN filters for reading motor data. Call after constructing the Pedal object and the MCP2515 object it references to ensure motor data is being read correctly. This function will block until the filter is set correctly on the MCP2515. If it never succeed, the program will be stuck here. Consider that when we can't even set the filter on the MCP2515, we probably can't communicate with the motor controller at all, so being stuck here is acceptable since the car won't be drivable without motor communication anyway.

Definition at line 52 of file Pedal.cpp.

References motor_can, and MOTOR_READ.

Referenced by setup().

Here is the caller graph for this function:

◆ initMotor()

bool Pedal::initMotor ( )

Initializes the motor CAN communication by setting up cyclic reads for motor data and configuring CAN filters. After the constructor of the Pedal class and the MCP2515 object it references are created, first call initFilter to set up the filters, then call this function to start the cyclic reads and ensure that motor data is being read correctly.

Returns
true if both motor speed and error data are being successfully read, false otherwise, can be used for looping
See also
initFilter

Definition at line 72 of file Pedal.cpp.

References checkCyclicRead(), ERR_PERIOD, got_error, got_speed, RPM_PERIOD, sendCyclicRead(), SPEED_IST, and WARN_ERR.

Referenced by setup().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pedalTorqueMapping()

constexpr int16_t Pedal::pedalTorqueMapping ( const uint16_t  pedal,
const uint16_t  brake,
const int16_t  motor_rpm,
const bool  flip_dir 
)
constexprprivate

Maps the pedal ADC to a torque value. If no braking requested, maps throttle normally. If braking requested and regen enabled, applies regen if motor RPM larger than minimum regen RPM, preventing reverse torque at low speeds. Regen is also disabled if motor rpm isn't read recently to prevent reverse power.

Parameters
pedalPedal ADC in the range of 0-1023.
brakeBrake ADC in the range of 0-1023.
motor_rpmCurrent motor RPM for regen logic, scaled to 0-32767.
flip_dirBoolean indicating whether to flip the motor direction.
Returns
Mapped torque value in the signed range of -TORQUE_MAX to TORQUE_MAX.

Definition at line 196 of file Pedal.cpp.

References TelemetryFramePedal::StateByteStatus::bits, BRAKE_MAP, car, LinearInterp< Tin, Tout, Tmid, size >::interp(), PedalConstants::MIN_REGEN_RPM_VAL, TelemetryFramePedal::StateByteStatus::Bits::motor_no_read, CarState::pedal, pedal(), REGEN_ENABLED, TelemetryFramePedal::StateByteStatus::Bits::screenshot, LinearInterp< Tin, Tout, Tmid, size >::start(), TelemetryFramePedal::status, and THROTTLE_MAP.

Referenced by sendFrame().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readMotor()

void Pedal::readMotor ( )

Reads motor data from the CAN bus and updates the CarState.

Definition at line 286 of file Pedal.cpp.

References TelemetryFramePedal::StateByteStatus::bits, car, last_motor_read_millis, MAX_MOTOR_READ_MILLIS, CarState::millis, CarState::motor, motor_can, TelemetryFrameMotor::motor_error, TelemetryFramePedal::StateByteStatus::Bits::motor_no_read, MOTOR_READ, TelemetryFrameMotor::motor_rpm, TelemetryFrameMotor::motor_warn, CarState::pedal, SPEED_IST, TelemetryFramePedal::status, and WARN_ERR.

Referenced by schedulerMotorRead().

Here is the caller graph for this function:

◆ sendCyclicRead()

MCP2515::ERROR Pedal::sendCyclicRead ( uint8_t  reg_id,
uint8_t  read_period 
)
private

Sends a cyclic read request to the motor controller for speed (rpm).

Parameters
reg_idRegister ID to read from the motor controller.
read_periodPeriod of reading motor data in ms.

< can_id

< can_dlc

< data, register ID

< data, sub ID

< data, read period in ms

Definition at line 258 of file Pedal.cpp.

References motor_can, MOTOR_SEND, and REGID_READ.

Referenced by initMotor().

Here is the caller graph for this function:

◆ sendFrame()

void Pedal::sendFrame ( )

Sends the appropriate CAN frame to the motor based on pedal and car state.

Definition at line 156 of file Pedal.cpp.

References TelemetryFramePedal::apps_3v3, TelemetryFramePedal::apps_5v, TelemetryFramePedal::StateByteStatus::bits, TelemetryFramePedal::brake, brake_filter, car, TelemetryFramePedal::StateByteStatus::Bits::car_status, FLIP_MOTOR_DIR, TelemetryFramePedal::StateByteStatus::Bits::force_stop, ExponentialFilter< TypeInput, TypeMid, OLD_RATIO, NEW_RATIO >::getFiltered(), CarState::motor, motor_can, TelemetryFrameMotor::motor_rpm, CarState::pedal, pedal1_filter, pedal2_filter, pedal_final, pedalTorqueMapping(), TelemetryFramePedal::status, stop_frame, torque_msg, and TelemetryFrameMotor::torque_val.

Referenced by schedulerPedalSend().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ update()

void Pedal::update ( uint16_t  pedal_1,
uint16_t  pedal_2,
uint16_t  brake 
)

Updates pedal sensor readings, applies filtering, and checks for faults.

Stores new pedal readings, applies an average filter, and updates car state. If a fault is detected between pedal sensors, sets fault flags and logs status.

Parameters
pedal_1Raw value from pedal sensor 1.
pedal_2Raw value from pedal sensor 2.
brakeRaw value from brake sensor.

Definition at line 99 of file Pedal.cpp.

References ExponentialFilter< TypeInput, TypeMid, OLD_RATIO, NEW_RATIO >::addSample(), TelemetryFramePedal::StateByteFaults::Bits::apps_3v3_high, TelemetryFramePedal::StateByteFaults::Bits::apps_3v3_low, APPS_3V3_MAX, APPS_3V3_MIN, TelemetryFramePedal::StateByteFaults::Bits::apps_5v_high, TelemetryFramePedal::StateByteFaults::Bits::apps_5v_low, APPS_5V_MAX, APPS_5V_MIN, TelemetryFramePedal::StateByteStatus::bits, TelemetryFramePedal::StateByteFaults::bits, brake_filter, TelemetryFramePedal::StateByteFaults::Bits::brake_high, TelemetryFramePedal::StateByteFaults::Bits::brake_low, brake_max, brake_min, TelemetryFramePedal::StateByteFaults::byte, car, checkPedalFault(), TelemetryFramePedal::StateByteFaults::Bits::fault_active, FAULT_CHECK_HEX, TelemetryFramePedal::StateByteFaults::Bits::fault_exceeded, fault_start_millis, TelemetryFramePedal::faults, TelemetryFramePedal::StateByteStatus::Bits::force_stop, CarState::millis, CarState::pedal, pedal1_filter, pedal2_filter, and TelemetryFramePedal::status.

Referenced by loop().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ APPS_3V3_SCALE_MAP

constexpr LinearInterp<uint16_t, uint16_t, uint32_t, 3> Pedal::APPS_3V3_SCALE_MAP {APPS_3V3_SCALE_TABLE}
staticconstexprprivate

Interpolation map for APPS_3V3->APPS_5V

Definition at line 114 of file Pedal.hpp.

Referenced by checkPedalFault().

◆ brake_filter

ExponentialFilter<uint16_t, uint16_t> Pedal::brake_filter
private

Filter for brake sensor input

Definition at line 110 of file Pedal.hpp.

Referenced by sendFrame(), and update().

◆ BRAKE_MAP

constexpr LinearInterp<uint16_t, int16_t, int32_t, 5> Pedal::BRAKE_MAP {BRAKE_TABLE}
staticconstexprprivate

Interpolation map for brake torque

Definition at line 113 of file Pedal.hpp.

Referenced by pedalTorqueMapping().

◆ car

CarState& Pedal::car
private

Reference to CarState

Definition at line 79 of file Pedal.hpp.

Referenced by checkPedalFault(), pedalTorqueMapping(), readMotor(), sendFrame(), and update().

◆ ERR_PERIOD

constexpr uint8_t Pedal::ERR_PERIOD = 20
staticconstexprprivate

Period of reading motor errors in ms, set to 20ms to get 10ms reads alongside rpm

Definition at line 125 of file Pedal.hpp.

Referenced by initMotor().

◆ fault_start_millis

uint32_t Pedal::fault_start_millis
private

Timestamp for when a fault started

Definition at line 81 of file Pedal.hpp.

Referenced by update().

◆ got_error

bool Pedal::got_error
private

Flag indicating if motor error data has been successfully read

Definition at line 85 of file Pedal.hpp.

Referenced by initMotor().

◆ got_speed

bool Pedal::got_speed
private

Flag indicating if motor speed data has been successfully read

Definition at line 84 of file Pedal.hpp.

Referenced by initMotor().

◆ last_motor_read_millis

uint32_t Pedal::last_motor_read_millis
private

Timestamp for the last motor data read

Definition at line 82 of file Pedal.hpp.

Referenced by readMotor().

◆ motor_can

MCP2515& Pedal::motor_can
private

Reference to MCP2515 for sending CAN messages

Definition at line 80 of file Pedal.hpp.

Referenced by checkCyclicRead(), initFilter(), readMotor(), sendCyclicRead(), and sendFrame().

◆ MOTOR_READ

constexpr canid_t Pedal::MOTOR_READ = 0x181
staticconstexprprivate

Motor read CAN ID

Definition at line 117 of file Pedal.hpp.

Referenced by checkCyclicRead(), initFilter(), and readMotor().

◆ MOTOR_SEND

constexpr canid_t Pedal::MOTOR_SEND = 0x201
staticconstexprprivate

Motor send CAN ID

Definition at line 116 of file Pedal.hpp.

Referenced by sendCyclicRead().

◆ pedal1_filter

ExponentialFilter<uint16_t, uint16_t> Pedal::pedal1_filter
private

Filter for first pedal sensor input

Definition at line 108 of file Pedal.hpp.

Referenced by sendFrame(), and update().

◆ pedal2_filter

ExponentialFilter<uint16_t, uint16_t> Pedal::pedal2_filter
private

Filter for second pedal sensor input

Definition at line 109 of file Pedal.hpp.

Referenced by sendFrame(), and update().

◆ pedal_final

uint16_t& Pedal::pedal_final

Final pedal value is taken directly from apps_5v, see initializer

Definition at line 76 of file Pedal.hpp.

Referenced by loop(), and sendFrame().

◆ REGID_READ

constexpr uint8_t Pedal::REGID_READ = 0x3D
staticconstexprprivate

Register ID for reading motor data

Definition at line 119 of file Pedal.hpp.

Referenced by sendCyclicRead().

◆ RPM_PERIOD

constexpr uint8_t Pedal::RPM_PERIOD = 20
staticconstexprprivate

Period of reading motor data in ms, set to 20ms to get 10ms reads alongside errors

Definition at line 124 of file Pedal.hpp.

Referenced by initMotor().

◆ SPEED_IST

constexpr uint8_t Pedal::SPEED_IST = 0x30
staticconstexprprivate

Register ID for "actual speed value"

Definition at line 121 of file Pedal.hpp.

Referenced by initMotor(), and readMotor().

◆ stop_frame

const can_frame Pedal::stop_frame
private
Initial value:
= {
3,
0x90,
0x00,
0x00}
static constexpr canid_t MOTOR_SEND
Definition: Pedal.hpp:116

CAN frame to stop the motor.

Definition at line 90 of file Pedal.hpp.

Referenced by sendFrame().

◆ THROTTLE_MAP

constexpr LinearInterp<uint16_t, int16_t, int32_t, 5> Pedal::THROTTLE_MAP {THROTTLE_TABLE}
staticconstexprprivate

Interpolation map for throttle torque

Definition at line 112 of file Pedal.hpp.

Referenced by checkPedalFault(), and pedalTorqueMapping().

◆ torque_msg

can_frame Pedal::torque_msg
private
Initial value:
= {
3,
0x90,
0x00,
0x00}

CAN frame for torque command.

Definition at line 100 of file Pedal.hpp.

Referenced by sendFrame().

◆ WARN_ERR

constexpr uint8_t Pedal::WARN_ERR = 0x8F
staticconstexprprivate

Register ID for warnings and errors

Definition at line 122 of file Pedal.hpp.

Referenced by initMotor(), and readMotor().


The documentation for this class was generated from the following files: