firmware
IEM Firmware Documentation
Loading...
Searching...
No Matches
state.c File Reference

Car state machine. More...

#include "state.h"
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include <stdlib.h>
#include <sys/types.h>
#include "can_rtos.h"
#include "can.h"
#include "can_1.h"
#include "can_2.h"
#include "constants.h"
#include "main.h"
#include "types.h"
#include "rtos.h"
#include "iem_utils.h"
Include dependency graph for state.c:

Data Structures

struct  apps

Macros

#define TIMER_RTDS_MS   3000
#define TIMER_BLINK_LED_MIN   500
#define TIMER_BLINK_LED_RANGE   2000
#define TIMER_RTDS_NAME   "tim_rtds"
#define FAULT_ACTIVE   true
#define HV_CONNECTED_ACTIVE   false
#define SHUTDOWN_CLOSED_ACTIVE   false
#define TSSI_CTRL_RED   true
#define TSSI_IMD_CLEAR_WINDOW   pdMS_TO_TICKS(3000)
#define BRAKE_THRESHOLD_PSI   70.0F
#define BP_FRONT_OFFSET   -32.0F
#define BP_REAR_OFFSET   0.0F
#define APPS_DEVIATION_THRESH   10.0F
#define SOFT_FAULT_TIME_MS   100
#define APPS_MIN_THRESH   -50.0F
#define APPS_MAX_THRESH   50.0F
#define BTSF_APPS_ACTV_THRESH   25.0F
#define BTSF_APPS_DEACTV_THRESH   5.0F
#define BTSF_EMA_DENOM   5
#define BPS_SHORT_THRESH   (uint16_t)((0.3F * (3.0F / 5.0F)) * (65535.0F / 3.3F))
#define BPS_OPEN_THRESH   (uint16_t)((4.7F * (3.0F / 5.0F)) * (65535.0F / 3.3F))
#define PUMP_ON_DUTY_CYCLE   0xFF
#define PUMP_OFF_DUTY_CYCLE   0x10

Functions

static __always_inline float brake_raw_to_pressure_r (uint16_t raw)
static __always_inline float brake_raw_to_pressure_f (uint16_t raw)
static __always_inline float bspd_raw_to_current (float cs_raw, float ref_raw)
static __always_inline float lv_batt_raw_to_voltage (float raw)
struct __aligned (4)
static void task_state (void *task_params_ptr)
 Implements the state machine task.
static void car_state_machine (union car_flags *flags_ptr, TimerHandle_t timer_rtds_hdl)
 Car state machine.
static enum btsf_fault check_btsf (bool brake_pressed, uint16_t bps_raw, float throttle_pos)
 Checks for a BTSF fault.
static enum apps_fault check_apps (struct apps *apps_ptr)
 Checks for an APPS soft fault.
static void read_gpio (union car_flags *flags_ptr)
 Polls GPIO fault and status pins for state machine inputs. This function accounts for pin active levels - that is, "true" in the flags struct indicates that the pin is in its active state whether the active state is high or low.
static void timer_rtds_callback (TimerHandle_t timer_hdl)
 RTDS FreeRTOS timer callback. Timer created in and started in car_state_machine when entering the RTDS state.
static void timer_blink_led_callback (TimerHandle_t timer_hdl)
 Very important functionality.
static void log_car_status (union car_flags flags)
 Sets values for, packs, and sends the safety_status message on CAN 1.
static void transmit_pump_enable_msg (bool enable)
void task_state_init (Global_Handles_t *handles_ptr)
 Initialize the state machine task and input queue.
float get_brake_pressure ()

Variables

DMA_VAR struct adc1_buffer g_adc1_buffer
DMA_VAR struct adc2_buffer g_adc2_buffer

Detailed Description

Car state machine.

Author
Jamie Pruett :)
Version
0.1
Date
2024-11-01

Macro Definition Documentation

◆ APPS_DEVIATION_THRESH

#define APPS_DEVIATION_THRESH   10.0F

◆ APPS_MAX_THRESH

#define APPS_MAX_THRESH   50.0F

◆ APPS_MIN_THRESH

#define APPS_MIN_THRESH   -50.0F

◆ BP_FRONT_OFFSET

#define BP_FRONT_OFFSET   -32.0F

◆ BP_REAR_OFFSET

#define BP_REAR_OFFSET   0.0F

◆ BPS_OPEN_THRESH

#define BPS_OPEN_THRESH   (uint16_t)((4.7F * (3.0F / 5.0F)) * (65535.0F / 3.3F))

◆ BPS_SHORT_THRESH

#define BPS_SHORT_THRESH   (uint16_t)((0.3F * (3.0F / 5.0F)) * (65535.0F / 3.3F))

◆ BRAKE_THRESHOLD_PSI

#define BRAKE_THRESHOLD_PSI   70.0F

◆ BTSF_APPS_ACTV_THRESH

#define BTSF_APPS_ACTV_THRESH   25.0F

◆ BTSF_APPS_DEACTV_THRESH

#define BTSF_APPS_DEACTV_THRESH   5.0F

◆ BTSF_EMA_DENOM

#define BTSF_EMA_DENOM   5

◆ FAULT_ACTIVE

#define FAULT_ACTIVE   true

◆ HV_CONNECTED_ACTIVE

#define HV_CONNECTED_ACTIVE   false

◆ PUMP_OFF_DUTY_CYCLE

#define PUMP_OFF_DUTY_CYCLE   0x10

◆ PUMP_ON_DUTY_CYCLE

#define PUMP_ON_DUTY_CYCLE   0xFF

◆ SHUTDOWN_CLOSED_ACTIVE

#define SHUTDOWN_CLOSED_ACTIVE   false

◆ SOFT_FAULT_TIME_MS

#define SOFT_FAULT_TIME_MS   100

◆ TIMER_BLINK_LED_MIN

#define TIMER_BLINK_LED_MIN   500

◆ TIMER_BLINK_LED_RANGE

#define TIMER_BLINK_LED_RANGE   2000

◆ TIMER_RTDS_MS

#define TIMER_RTDS_MS   3000

◆ TIMER_RTDS_NAME

#define TIMER_RTDS_NAME   "tim_rtds"

◆ TSSI_CTRL_RED

#define TSSI_CTRL_RED   true

◆ TSSI_IMD_CLEAR_WINDOW

#define TSSI_IMD_CLEAR_WINDOW   pdMS_TO_TICKS(3000)

Function Documentation

◆ __aligned()

struct __aligned ( 4 )

◆ brake_raw_to_pressure_f()

__always_inline float brake_raw_to_pressure_f ( uint16_t raw)
static
Here is the caller graph for this function:

◆ brake_raw_to_pressure_r()

__always_inline float brake_raw_to_pressure_r ( uint16_t raw)
static
Here is the caller graph for this function:

◆ bspd_raw_to_current()

__always_inline float bspd_raw_to_current ( float cs_raw,
float ref_raw )
static
Here is the caller graph for this function:

◆ car_state_machine()

void car_state_machine ( union car_flags * flags_ptr,
TimerHandle_t timer_rtds_hdl )
static

Car state machine.

Parameters
flags_ptrCar state flags
timer_rtds_hdlRTDS FreeRTOS timer handle
Here is the caller graph for this function:

◆ check_apps()

enum apps_fault check_apps ( struct apps * apps_ptr)
static

Checks for an APPS soft fault.

Parameters
a_rawAPPS A raw value
b_rawAPPS B raw value
Returns
bool true if there's an APPS fault
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_btsf()

enum btsf_fault check_btsf ( bool brake_pressed,
uint16_t bps_raw,
float throttle_pos )
static

Checks for a BTSF fault.

Parameters
brake_pressedWhether the brake is pressed
bps_rawFront brake pressure sensor raw ADC reading
throttle_posThrottle position (percent)
Returns
BTSF fault state
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_brake_pressure()

float get_brake_pressure ( )
Here is the call graph for this function:
Here is the caller graph for this function:

◆ log_car_status()

void log_car_status ( union car_flags flags)
static

Sets values for, packs, and sends the safety_status message on CAN 1.

Parameters
flags
adc_buffer_ptrPointer to the ADC DMA buffer struct
Here is the call graph for this function:
Here is the caller graph for this function:

◆ lv_batt_raw_to_voltage()

__always_inline float lv_batt_raw_to_voltage ( float raw)
static
Here is the caller graph for this function:

◆ read_gpio()

void read_gpio ( union car_flags * flags_ptr)
static

Polls GPIO fault and status pins for state machine inputs. This function accounts for pin active levels - that is, "true" in the flags struct indicates that the pin is in its active state whether the active state is high or low.

Parameters
flags_ptr
Here is the caller graph for this function:

◆ task_state()

void task_state ( void * task_params_ptr)
static

Implements the state machine task.

Parameters
task_params_ptrPointer to the FreeRTOS handles struct

This task is scheduled periodically. It checks all state machine inputs (GPIO states, soft fault states, data from CAN messages, software timers) to see if they have changed. If they have, it runs the car state machine and any state-dependent code. The safety_status message is sent here on CAN 2 for logging every time the task is scheduled.

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

◆ task_state_init()

void task_state_init ( Global_Handles_t * handles_ptr)

Initialize the state machine task and input queue.

Parameters
handles_ptrPointer to FreeRTOS handles struct
Here is the call graph for this function:
Here is the caller graph for this function:

◆ timer_blink_led_callback()

void timer_blink_led_callback ( TimerHandle_t timer_hdl)
static

Very important functionality.

Parameters
timer_hdlFreeRTOS timer handle

◆ timer_rtds_callback()

void timer_rtds_callback ( TimerHandle_t timer_hdl)
static

RTDS FreeRTOS timer callback. Timer created in and started in car_state_machine when entering the RTDS state.

Parameters
timer_hdlFreeRTOS timer handle. The timer ID is the state machine task handle
Here is the call graph for this function:
Here is the caller graph for this function:

◆ transmit_pump_enable_msg()

void transmit_pump_enable_msg ( bool enable)
static
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ g_adc1_buffer

DMA_VAR struct adc1_buffer g_adc1_buffer

◆ g_adc2_buffer

DMA_VAR struct adc2_buffer g_adc2_buffer