Midnight Sun Firmware
Loading...
Searching...
No Matches
Front_Controller

Front Controller Board Firmware. More...

Classes

struct  AccelPedalCalibrationData
 
struct  AccelPedalStorage
 
struct  BrakePedalCalibrationData
 
struct  BrakePedalStorage
 
struct  CruiseControlStorage
 
struct  FrontControllerConfig
 Front Controller configuration data. More...
 
struct  FrontControllerStorage
 Front Controller storage. More...
 
struct  OpdStorage
 
struct  PowerManagerStorage
 
struct  OutputGroupDef
 
struct  Ws22MotorControlData
 Motor control command data structure. More...
 
struct  Ws22MotorTelemetryData
 Motor status and telemetry data structure. More...
 
struct  Ws22MotorCanStorage
 WS22 motor CAN interface storage structure. More...
 

Macros

#define CC_MIN_THRESHOLD   0.5f
 
#define CC_KP   0.00000005
 
#define ACCELERATION_LIMIT_M_S2   1
 
#define ACCELERATION_LIMIT_KM_HS   ACCELERATION_LIMIT_M_S2 * 3.6
 
#define ACCELERATION_LIMIT_PER_1000HZ_CYCLE   ACCELERATION_LIMIT_KM_HS / 1000
 
#define CURRENT_STEP_LIMIT   0.0001
 
#define FRONT_CONTROLLER_ACCEL_INPUT_DEADZONE   0.02f
 Front controller pedal set to 2% deadzone

 
#define FRONT_CONTROLLER_ACCEL_REMAP_MIN   0.10f
 Front controller pedal is remapped from 0 - 1 to 0.1 to 1.
 
#define FRONT_CONTROLLER_ACCEL_CURVE_EXPONENT   2.0f
 Front controller pedal exponent for non-linear feel.
 
#define FRONT_CONTROLLER_ACCEL_LPF_ALPHA   0.25f
 Front controller accel pedal alpha value for low-pass filtering.
 
#define FRONT_CONTROLLER_BRAKE_LPF_ALPHA   0.50f
 Front controller brake pedal alpha value for low-pass filtering.
 
#define FRONT_CONTROLLER_BRAKE_INPUT_DEADZONE   0.01f
 Front controller brake pedal set to 1% deadzone

 
#define WS22_CONTROLLER_MAX_VELOCITY   12000
 Max velocity value used for ws22 motor controllers.
 
#define MAX_VEHICLE_SPEED_KPH   50
 Max speed of vehicle in KPH.
 
#define FRONT_CONTROLLER_GPIO_DEF(PORT, PIN)    { .port = GPIO_PORT_##PORT, .pin = PIN }
 
#define GPIO_FRONT_CONTROLLER_CAN_RX   FRONT_CONTROLLER_GPIO_DEF(B, 8)
 Front Controller CAN RX Port.
 
#define GPIO_FRONT_CONTROLLER_CAN_TX   FRONT_CONTROLLER_GPIO_DEF(B, 9)
 Front Controller CAN TX Port.
 
#define GPIO_FRONT_CONTROLLER_LEFT_SIG_LS_ENABLE   FRONT_CONTROLLER_GPIO_DEF(B, 12)
 Front Controller left signal load switch enable.
 
#define GPIO_FRONT_CONTROLLER_RIGHT_SIG_LS_ENABLE   FRONT_CONTROLLER_GPIO_DEF(B, 13)
 Front Controller right signal load switch enable.
 
#define GPIO_FRONT_CONTROLLER_BRAKE_LIGHT_LS_ENABLE   FRONT_CONTROLLER_GPIO_DEF(A, 10)
 Front Controller brake light load switch enable.
 
#define GPIO_FRONT_CONTROLLER_BPS_LIGHT_LS_ENABLE   FRONT_CONTROLLER_GPIO_DEF(A, 9)
 Front Controller battery-protection-system light load switch enable.
 
#define GPIO_FRONT_CONTROLLER_DRIVER_FAN_LS_ENABLE   FRONT_CONTROLLER_GPIO_DEF(B, 14)
 Front Controller driver fan load switch enable.
 
#define GPIO_FRONT_CONTROLLER_REV_CAM_LS_ENABLE   FRONT_CONTROLLER_GPIO_DEF(B, 15)
 Front Controller reverse camera load switch enable.
 
#define GPIO_FRONT_CONTROLLER_TELEM_LS_ENABLE   FRONT_CONTROLLER_GPIO_DEF(A, 8)
 Front Controller telemetry load switch enable.
 
#define GPIO_FRONT_CONTROLLER_STEERING_LS_ENABLE   FRONT_CONTROLLER_GPIO_DEF(B, 11)
 Front Controller steering load switch enable.
 
#define GPIO_FRONT_CONTROLLER_HORN_LS_ENABLE   FRONT_CONTROLLER_GPIO_DEF(A, 11)
 Front Controller horn load switch enable.
 
#define GPIO_FRONT_CONTROLLER_SPARE_1_LS_ENABLE   FRONT_CONTROLLER_GPIO_DEF(A, 12)
 Front Controller spare 12V load switch enable.
 
#define GPIO_FRONT_CONTROLLER_MUX_SEL_2   FRONT_CONTROLLER_GPIO_DEF(A, 6)
 Front Controller current sense mux select 0.
 
#define GPIO_FRONT_CONTROLLER_MUX_SEL_3   FRONT_CONTROLLER_GPIO_DEF(A, 7)
 Front Controller current sense mux select 1.
 
#define GPIO_FRONT_CONTROLLER_MUX_SEL_0   FRONT_CONTROLLER_GPIO_DEF(B, 1)
 Front Controller current sense mux select 2.
 
#define GPIO_FRONT_CONTROLLER_MUX_SEL_1   FRONT_CONTROLLER_GPIO_DEF(B, 2)
 Front Controller current sense mux select 3.
 
#define GPIO_FRONT_CONTROLLER_MUX_OUTPUT   FRONT_CONTROLLER_GPIO_DEF(B, 0)
 Front Controller current sense mux input.
 
#define GPIO_FRONT_CONTROLLER_ACCEL_PEDAL   FRONT_CONTROLLER_GPIO_DEF(A, 3)
 Front Controller accelerator pedal input.
 
#define GPIO_FRONT_CONTROLLER_BRAKE_PEDAL   FRONT_CONTROLLER_GPIO_DEF(A, 5)
 Front Controller brake pedal input.
 
#define GPIO_FRONT_CONTROLLER_BOARD_LED   FRONT_CONTROLLER_GPIO_DEF(B, 10)
 Front Controller board LED.
 
#define VEL_TO_RPM_RATIO   0.57147
 
#define WS22_CAN_BASE_CONTROL   0x500U
 
#define WS22_CAN_BASE_STATUS   0x80U
 
#define WS22_CAN_ID_DRIVE_CMD   (WS22_CAN_BASE_CONTROL + 0x01U)
 
#define WS22_CAN_ID_STATUS_INFO   (WS22_CAN_BASE_STATUS + 0x01U)
 
#define WS22_CAN_ID_BUS_MEASUREMENT   (WS22_CAN_BASE_STATUS + 0x02U)
 
#define WS22_CAN_ID_VELOCITY_MEASUREMENT   (WS22_CAN_BASE_STATUS + 0x03U)
 
#define WS22_CAN_ID_PHASE_CURRENT   (WS22_CAN_BASE_STATUS + 0x04U)
 
#define WS22_CAN_ID_MOTOR_VOLTAGE   (WS22_CAN_BASE_STATUS + 0x05U)
 
#define WS22_CAN_ID_MOTOR_CURRENT   (WS22_CAN_BASE_STATUS + 0x06U)
 
#define WS22_CAN_ID_MOTOR_BACK_EMF   (WS22_CAN_BASE_STATUS + 0x07U)
 
#define WS22_CAN_ID_RAIL_15V   (WS22_CAN_BASE_STATUS + 0x08U)
 
#define WS22_CAN_ID_TEMPERATURE   (WS22_CAN_BASE_STATUS + 0x0BU)
 

Typedefs

typedef struct AccelPedalStorage AccelPedalStorage
 
typedef struct BrakePedalStorage BrakePedalStorage
 
typedef struct CruiseControlStorage CruiseControlStorage
 
typedef struct OpdStorage OpdStorage
 
typedef enum PtsRelationType PtsRelationType
 
typedef enum CurveType CurveType
 
typedef struct PowerManagerStorage PowerManagerStorage
 
typedef struct Ws22MotorCanStorage Ws22MotorCanStorage
 WS22 motor CAN interface storage structure.
 

Enumerations

enum  FrontControllerState { FRONT_CONTROLLER_STATE_IDLE = 0 , FRONT_CONTROLLER_STATE_ENGAGED , FRONT_CONTROLLER_STATE_FAULT , NUM_FRONT_CONTROLLER_STATES }
 
enum  FrontControllerEvent {
  FRONT_CONTROLLER_EVENT_NONE = 0 , FRONT_CONTROLLER_EVENT_IDLE_REQUEST , FRONT_CONTROLLER_EVENT_DRIVE_REQUEST , FRONT_CONTROLLER_EVENT_FAULT ,
  FRONT_CONTROLLER_EVENT_RESET
}
 
enum  PtsRelationType { PTS_TYPE_LINEAR , PTS_TYPE_EXPONENTIAL , PTS_TYPE_QUADRATIC }
 
enum  CurveType { CURVE_TYPE_LINEAR , CURVE_TYPE_EXPONENTIAL , CURVE_TYPE_QUADRATIC }
 
enum  OutputId {
  HORN = 0 , SPARE_1 , BRAKE_LIGHT , BPS_LIGHT ,
  TELEM , REV_CAM , DRIVER_FAN , RIGHT_SIG ,
  STEERING , LEFT_SIG , NUM_OUTPUTS
}
 Power outputs. More...
 
enum  OutputGroup {
  OUTPUT_GROUP_ALL = 0 , OUTPUT_GROUP_ACTIVE , OUTPUT_GROUP_LEFT_LIGHTS , OUTPUT_GROUP_RIGHT_LIGHTS ,
  OUTPUT_GROUP_HAZARD_LIGHTS , OUTPUT_GROUP_BPS_LIGHTS , OUTPUT_GROUP_BRAKE_LIGHTS , OUTPUT_GROUP_HORN ,
  NUM_OUTPUT_GROUPS
}
 Power output groups.
 

Functions

StatusCode accel_pedal_run ()
 Reads the pedal percentage and applies deadzone and filtering before updating the storage. More...
 
StatusCode accel_pedal_init (FrontControllerStorage *storage)
 Initializes the acceleration pedal hardware interface loads calibration data from memory. More...
 
StatusCode brake_pedal_run ()
 Reads the pedal percentage and applies deadzone and filtering before updating the brake state. More...
 
StatusCode brake_pedal_init (FrontControllerStorage *storage)
 Initializes the brake pedal hardware interface loads calibration data from memory. More...
 
StatusCode cruise_control_run ()
 Runs one cruise control calculation cycle. Called within motor_can.c when cruise control mode is on. More...
 
StatusCode cruise_control_init (FrontControllerStorage *storage)
 Initializes the cruise control module. More...
 
StatusCode front_controller_init (FrontControllerStorage *storage, FrontControllerConfig *config)
 Initialize the front controller interface. More...
 
StatusCode front_controller_state_manager_init (FrontControllerStorage *storage)
 Initialize the state machine. More...
 
StatusCode front_controller_state_manager_step (FrontControllerEvent event)
 Advance the state machine based on inputs/events. More...
 
FrontControllerState front_controller_state_manager_get_state (void)
 Get current state. More...
 
StatusCode front_controller_update_state_manager_medium_cycle ()
 Update the state/set load switches based on information recieved from CAN (medium cycle). More...
 
StatusCode horn_init ()
 Initializes the horn pin. More...
 
StatusCode motor_can_update_target_current_velocity ()
 Updates target current and velocity of motors based on drive state. More...
 
StatusCode motor_can_init (FrontControllerStorage *storage)
 Initializes the motor can, loads front controller storage data. More...
 
StatusCode motor_can_get_current_state (VehicleDriveState *current_state)
 retrieve current vehicle drive state
 
char * motor_can_get_current_state_str ()
 print a state as a string
 
StatusCode opd_calculate_handler (float pedal_percentage, PtsRelationType relation_type, float *calculated_reading, CurveType curve_type)
 handler for opd_calculate command, runs either linear or quadratic calculation based on CurveType enum
 
StatusCode opd_linear_calculate (float pedal_percentage, PtsRelationType relation_type, float *calculated_reading)
 Perform linear OPD calculation. More...
 
StatusCode opd_quadratic_calculate (float pedal_percentage, PtsRelationType relation_type, float *calculated_reading)
 Perform quadratic OPD calculation.
 
StatusCode opd_run ()
 Reads the pedal percentage from AccelPedalStorage and saves an adjusted acceleration percentage to front controller based on OPD algorithm. More...
 
StatusCode opd_init (FrontControllerStorage *storage)
 Initializes the opd, loads front controller storage. More...
 
StatusCode pedal_calib_read (FrontControllerStorage *storage)
 Reads pedal calibration data. More...
 
StatusCode power_manager_init (FrontControllerStorage *storage)
 Initialize all the GPIO pins. More...
 
StatusCode power_manager_set_output_group (OutputGroup group, bool enable)
 Set an output group as active or inactive.
 
StatusCode power_manager_toggle_output_group (OutputGroup group)
 Toggle an output group.
 
StatusCode power_manager_run_current_sense (OutputGroup group)
 Run current sensing.
 
StatusCode ws22_motor_can_init (FrontControllerStorage *storage)
 Initialize the WS22 motor CAN interface. More...
 
StatusCode ws22_motor_can_set_current (float current)
 Set motor control current command. More...
 
StatusCode ws22_motor_can_set_velocity (float velocity)
 Set motor control velocity command. More...
 
StatusCode ws22_motor_can_transmit_drive_command (void)
 Build and transmit motor drive command. More...
 
StatusCode ws22_motor_can_process_rx (CanMessage *msg)
 Process received CAN message for motor controller. More...
 
Ws22MotorControlDataws22_motor_can_get_control_data (void)
 Get current motor control data. More...
 
Ws22MotorTelemetryDataws22_motor_can_get_telemetry_data (void)
 Get current motor telemetry data. More...
 

Detailed Description

Front Controller Board Firmware.

Macro Definition Documentation

◆ WS22_CAN_BASE_CONTROL

#define WS22_CAN_BASE_CONTROL   0x500U

Base address for control messages

◆ WS22_CAN_BASE_STATUS

#define WS22_CAN_BASE_STATUS   0x80U

Base address for status messages

Enumeration Type Documentation

◆ FrontControllerState

Enumerator
FRONT_CONTROLLER_STATE_IDLE 

Ready but not driving or charging, cars in neutral

FRONT_CONTROLLER_STATE_ENGAGED 

Driving: motor relays closed, LV enabled

FRONT_CONTROLLER_STATE_FAULT 

Faulted: relays open, latched until reset

◆ OutputId

enum OutputId

Power outputs.

The order of this enum matters. It matches the schematic mux indices

Function Documentation

◆ accel_pedal_init()

StatusCode accel_pedal_init ( FrontControllerStorage storage)

Initializes the acceleration pedal hardware interface loads calibration data from memory.

Returns
STATUS_CODE_OK if pedal is initialized successfully STATUS_CODE_INVALID_ARGS if an invalid parameter is provided

◆ accel_pedal_run()

StatusCode accel_pedal_run ( )

Reads the pedal percentage and applies deadzone and filtering before updating the storage.

Returns
STATUS_CODE_OK if pedal is read successfully STATUS_CODE_UNINITIALIZED if pedal is not initialized STATUS_CODE_INTERNAL_ERROR if ADC fails

Convert ADC Reading to readable voltage by normalizing with calibration data and dividing to get percentage press. Negatives and > 100 values will be clamped

◆ brake_pedal_init()

StatusCode brake_pedal_init ( FrontControllerStorage storage)

Initializes the brake pedal hardware interface loads calibration data from memory.

Returns
STATUS_CODE_OK if pedal is initialized successfully STATUS_CODE_INVALID_ARGS if an invalid parameter is provided

◆ brake_pedal_run()

StatusCode brake_pedal_run ( )

Reads the pedal percentage and applies deadzone and filtering before updating the brake state.

Returns
STATUS_CODE_OK if pedal is read successfully STATUS_CODE_UNINITIALIZED if pedal is not initialized STATUS_CODE_INTERNAL_ERROR if ADC fails

◆ cruise_control_init()

StatusCode cruise_control_init ( FrontControllerStorage storage)

Initializes the cruise control module.

Returns
STATUS_CODE_OK if initialization is successful STATUS_CODE_INVALID_ARGS if storage is null

◆ cruise_control_run()

StatusCode cruise_control_run ( )

Runs one cruise control calculation cycle. Called within motor_can.c when cruise control mode is on.

Returns
STATUS_CODE_OK if successful STATUS_CODE_INVALID_ARGS if cruise control is not initialized

Perform PID calculations on current and target motor velocity...

  1. calculate eP 1.1. if eP < min_threshold, set current directly
  2. calculate eI, eD 2.1. bound eI if needed
  3. calculate result kP * eP + ...
  4. compare result to previous set_motor_velocity -> acceleration limiting

Notes: eI must be reset on any pedal action

◆ front_controller_init()

StatusCode front_controller_init ( FrontControllerStorage storage,
FrontControllerConfig config 
)

Initialize the front controller interface.

Parameters
storagePointer to the front controller storage
configPointer to the front controller config
Returns
STATUS_CODE_OK if front controller initialization succeeded STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect

◆ front_controller_state_manager_get_state()

FrontControllerState front_controller_state_manager_get_state ( void  )

Get current state.

Returns
Current front controller state (see FrontControllerState)

◆ front_controller_state_manager_init()

StatusCode front_controller_state_manager_init ( FrontControllerStorage storage)

Initialize the state machine.

Parameters
storagePointer to the front controller storage
Returns
STATUS_CODE_OK if initialized successfully STATUS_CODE_RESOURCE_EXHAUSTED if initialized already

◆ front_controller_state_manager_step()

StatusCode front_controller_state_manager_step ( FrontControllerEvent  event)

Advance the state machine based on inputs/events.

Parameters
eventThe triggering event
Returns
STATUS_CODE_OK if initialized successfully STATUS_CODE_UNINITIALIZED if uninitialized

◆ front_controller_update_state_manager_medium_cycle()

StatusCode front_controller_update_state_manager_medium_cycle ( )

Update the state/set load switches based on information recieved from CAN (medium cycle).

Returns
STATUS_CODE_OK if udpated successfully

◆ horn_init()

StatusCode horn_init ( )

Initializes the horn pin.

Returns
STATUS_CODE_OK if horn is initialized successfully

◆ motor_can_init()

StatusCode motor_can_init ( FrontControllerStorage storage)

Initializes the motor can, loads front controller storage data.

Returns
STATUS_CODE_OK if front controller is initialized successfully STATUS_CODE_INVALID_ARGS if storage pointer is invalid

◆ motor_can_update_target_current_velocity()

StatusCode motor_can_update_target_current_velocity ( )

Updates target current and velocity of motors based on drive state.

Returns
STATUS_CODE_OK if values are set successfully STATUS_CODE_INVALID_ARGS if drive state is invalid STATUS_CODE_UNINITIALIZED if motor can is uninitialized

◆ opd_init()

StatusCode opd_init ( FrontControllerStorage storage)

Initializes the opd, loads front controller storage.

Returns
STATUS_CODE_OK if opd is initialized successfully STATUS_CODE_INVALID_ARGS if storage pointer is invalid

◆ opd_linear_calculate()

StatusCode opd_linear_calculate ( float  pedal_percentage,
PtsRelationType  relation_type,
float *  calculated_reading 
)

Perform linear OPD calculation.

Linearly reduce regenerative braking when it's within a certain delta of the max cell voltage

◆ opd_run()

StatusCode opd_run ( )

Reads the pedal percentage from AccelPedalStorage and saves an adjusted acceleration percentage to front controller based on OPD algorithm.

Returns
STATUS_CODE_OK if acceleration percentage is successfully stored STATUS_CODE_UNINITIALIZED if front controller storage is uninitialized

◆ pedal_calib_read()

StatusCode pedal_calib_read ( FrontControllerStorage storage)

Reads pedal calibration data.

Parameters
storagePointer to the front controller storage
Returns
STATUS_CODE_OK if calibration data is read successfully STATUS_CODE_INVALID_ARGS if storage is NULL

◆ power_manager_init()

StatusCode power_manager_init ( FrontControllerStorage storage)

Initialize all the GPIO pins.

Parameters
storagePointer to the front controller storage
Returns
STATUS_CODE_OK if initialized succesfully STATUS_CODE_INVALID_ARGS if an invalid argument is passed in

◆ ws22_motor_can_get_control_data()

Ws22MotorControlData * ws22_motor_can_get_control_data ( void  )

Get current motor control data.

Returns
Pointer to motor control data (read-only)

◆ ws22_motor_can_get_telemetry_data()

Ws22MotorTelemetryData * ws22_motor_can_get_telemetry_data ( void  )

Get current motor telemetry data.

Returns
Pointer to motor telemetry data (read-only)

◆ ws22_motor_can_init()

StatusCode ws22_motor_can_init ( FrontControllerStorage storage)

Initialize the WS22 motor CAN interface.

Parameters
storagePointer to Front controller storage structure
Returns
STATUS_CODE_OK on success, error code otherwise

◆ ws22_motor_can_process_rx()

StatusCode ws22_motor_can_process_rx ( CanMessage msg)

Process received CAN message for motor controller.

Parameters
msgPointer to received CAN message
Returns
STATUS_CODE_OK on success, STATUS_CODE_UNIMPLEMENTED for unknown message IDs

◆ ws22_motor_can_set_current()

StatusCode ws22_motor_can_set_current ( float  current)

Set motor control current command.

Parameters
currentMotor current command (0.0 to 1.0)
Returns
STATUS_CODE_OK on success, STATUS_CODE_INVALID_ARGS if out of range

◆ ws22_motor_can_set_velocity()

StatusCode ws22_motor_can_set_velocity ( float  velocity)

Set motor control velocity command.

Parameters
velocityMotor velocity command (rpm)
Returns
STATUS_CODE_OK on success, STATUS_CODE_INVALID_ARGS if out of range

◆ ws22_motor_can_transmit_drive_command()

StatusCode ws22_motor_can_transmit_drive_command ( void  )

Build and transmit motor drive command.

Returns
STATUS_CODE_OK on success, error code otherwise