Pedal class for managing throttle and brake pedal inputs. Handles filtering, fault detection, and CAN frame updates. More...
#include <Pedal.hpp>
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 | |
| CarState & | car |
| 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 |
Pedal class for managing throttle and brake pedal inputs. Handles filtering, fault detection, and CAN frame updates.
| 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.
| 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. |
|
private |
Definition at line 270 of file Pedal.cpp.
References motor_can, and MOTOR_READ.
Referenced by initMotor().
|
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.
< 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().
| 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().
| 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.
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().
|
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.
| pedal | Pedal ADC in the range of 0-1023. |
| brake | Brake ADC in the range of 0-1023. |
| motor_rpm | Current motor RPM for regen logic, scaled to 0-32767. |
| flip_dir | Boolean indicating whether to flip the motor direction. |
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().
| 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().
|
private |
Sends a cyclic read request to the motor controller for speed (rpm).
| reg_id | Register ID to read from the motor controller. |
| read_period | Period 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().
| 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().
| 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.
| pedal_1 | Raw value from pedal sensor 1. |
| pedal_2 | Raw value from pedal sensor 2. |
| brake | Raw 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().
|
staticconstexprprivate |
Interpolation map for APPS_3V3->APPS_5V
Definition at line 114 of file Pedal.hpp.
Referenced by checkPedalFault().
|
private |
Filter for brake sensor input
Definition at line 110 of file Pedal.hpp.
Referenced by sendFrame(), and update().
|
staticconstexprprivate |
Interpolation map for brake torque
Definition at line 113 of file Pedal.hpp.
Referenced by pedalTorqueMapping().
|
private |
Reference to CarState
Definition at line 79 of file Pedal.hpp.
Referenced by checkPedalFault(), pedalTorqueMapping(), readMotor(), sendFrame(), and update().
|
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().
|
private |
|
private |
Flag indicating if motor error data has been successfully read
Definition at line 85 of file Pedal.hpp.
Referenced by initMotor().
|
private |
Flag indicating if motor speed data has been successfully read
Definition at line 84 of file Pedal.hpp.
Referenced by initMotor().
|
private |
Timestamp for the last motor data read
Definition at line 82 of file Pedal.hpp.
Referenced by readMotor().
|
private |
Reference to MCP2515 for sending CAN messages
Definition at line 80 of file Pedal.hpp.
Referenced by checkCyclicRead(), initFilter(), readMotor(), sendCyclicRead(), and sendFrame().
|
staticconstexprprivate |
Motor read CAN ID
Definition at line 117 of file Pedal.hpp.
Referenced by checkCyclicRead(), initFilter(), and readMotor().
|
staticconstexprprivate |
|
private |
Filter for first pedal sensor input
Definition at line 108 of file Pedal.hpp.
Referenced by sendFrame(), and update().
|
private |
Filter for second pedal sensor input
Definition at line 109 of file Pedal.hpp.
Referenced by sendFrame(), and update().
| 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().
|
staticconstexprprivate |
Register ID for reading motor data
Definition at line 119 of file Pedal.hpp.
Referenced by sendCyclicRead().
|
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().
|
staticconstexprprivate |
Register ID for "actual speed value"
Definition at line 121 of file Pedal.hpp.
Referenced by initMotor(), and readMotor().
|
private |
CAN frame to stop the motor.
Definition at line 90 of file Pedal.hpp.
Referenced by sendFrame().
|
staticconstexprprivate |
Interpolation map for throttle torque
Definition at line 112 of file Pedal.hpp.
Referenced by checkPedalFault(), and pedalTorqueMapping().
|
private |
CAN frame for torque command.
Definition at line 100 of file Pedal.hpp.
Referenced by sendFrame().
|
staticconstexprprivate |
Register ID for warnings and errors
Definition at line 122 of file Pedal.hpp.
Referenced by initMotor(), and readMotor().