20#pragma GCC diagnostic push
21#pragma GCC diagnostic ignored "-Wunused-parameter"
23#pragma GCC diagnostic pop
35 : pedal_final(pedal_final_),
37 motor_can(motor_can_),
38 fault_start_millis(0),
39 last_motor_read_millis(0),
56 while (
motor_can.setFilterMask(MCP2515::MASK0,
false, 0x7FF) != MCP2515::ERROR_OK)
246 if (delta > MAX_DELTA || delta < -MAX_DELTA)
260 const can_frame cyclic_request = {
267 return motor_can.sendMessage(&cyclic_request);
273 if (
motor_can.readMessage(&rx_frame) == MCP2515::ERROR_OK &&
275 rx_frame.can_dlc > 3 &&
276 rx_frame.data[0] == reg_id)
289 if (
motor_can.readMessage(&rx_frame) == MCP2515::ERROR_OK)
291 if (rx_frame.can_id ==
MOTOR_READ && rx_frame.can_dlc > 3)
297 car.
motor.
motor_rpm =
static_cast<int16_t
>(rx_frame.data[1] | (rx_frame.data[2] << 8));
300 else if (rx_frame.data[0] ==
WARN_ERR)
303 car.
motor.
motor_warn =
static_cast<uint16_t
>(rx_frame.data[3] | (rx_frame.data[4] << 8));
Definition of the CarState structure representing the state of the car.
Definition of throttle and brake mapping tables.
constexpr uint16_t APPS_3V3_MIN
constexpr uint16_t brake_min
constexpr TablePoint< uint16_t, uint16_t > APPS_5V_PERCENT_TABLE[2]
APPS_5V to percent mapping table, maps 5V readings to percent throttle (0-60000)
constexpr uint16_t APPS_3V3_MAX
constexpr uint16_t brake_max
constexpr uint16_t APPS_5V_MIN
constexpr uint16_t APPS_5V_MAX
Debugging macros and functions for serial and CAN output.
Declaration and definition of the LinearInterp class template for linear interpolation.
Declaration of the Pedal class for handling throttle and brake pedal inputs.
constexpr uint16_t FAULT_CHECK_HEX
constexpr bool FLIP_MOTOR_DIR
constexpr bool REGEN_ENABLED
constexpr uint32_t MAX_MOTOR_READ_MILLIS
Declaration of a simple RingBuffer (circular buffer) template class.
Declaration of signal processing functions.
void addSample(TypeInput sample) override
Adds a new sample to the ExponentialFilter. Updates the filtered value using the exponential moving a...
TypeInput getFiltered() const override
Retrieves the filtered value from the ExponentialFilter.
constexpr Tout interp(Tin input) const
Performs linear interpolation for the given input value, using the table.
constexpr Tin range() const
Returns the input range of the interpolation table (last input - first input)
constexpr Tin start() const
Returns the starting input value of the interpolation table.
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.
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,...
bool initMotor()
Initializes the motor CAN communication by setting up cyclic reads for motor data and configuring CAN...
bool checkCyclicRead(uint8_t reg_id)
bool checkPedalFault()
Checks for a fault between two pedal sensor readings.
static constexpr uint8_t WARN_ERR
static constexpr LinearInterp< uint16_t, int16_t, int32_t, 5 > BRAKE_MAP
void initFilter()
Initializes the CAN filters for reading motor data. Call after constructing the Pedal object and the ...
static constexpr LinearInterp< uint16_t, int16_t, int32_t, 5 > THROTTLE_MAP
static constexpr canid_t MOTOR_READ
static constexpr LinearInterp< uint16_t, uint16_t, uint32_t, 3 > APPS_3V3_SCALE_MAP
const can_frame stop_frame
CAN frame to stop the motor.
static constexpr uint8_t REGID_READ
void readMotor()
Reads motor data from the CAN bus and updates the CarState.
uint32_t last_motor_read_millis
static constexpr uint8_t RPM_PERIOD
ExponentialFilter< uint16_t, uint16_t > brake_filter
static constexpr canid_t MOTOR_SEND
ExponentialFilter< uint16_t, uint16_t > pedal1_filter
static constexpr uint8_t SPEED_IST
can_frame torque_msg
CAN frame for torque command.
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....
static constexpr uint8_t ERR_PERIOD
MCP2515::ERROR sendCyclicRead(uint8_t reg_id, uint8_t read_period)
Sends a cyclic read request to the motor controller for speed (rpm).
uint32_t fault_start_millis
ExponentialFilter< uint16_t, uint16_t > pedal2_filter
Pedal pedal(mcp2515_DL, car, car.pedal.apps_5v)
struct CarState car
Global car state structure.
constexpr int16_t MIN_REGEN_RPM_VAL
Represents the state of the car. Holds telemetry data and status, used as central data sharing struct...
TelemetryFramePedal pedal
TelemetryFrameMotor motor
struct TelemetryFramePedal::StateByteFaults::Bits bits
struct TelemetryFramePedal::StateByteStatus::Bits bits