Skip to content

Commit

Permalink
brake is read
Browse files Browse the repository at this point in the history
  • Loading branch information
Myers-Ty committed Jan 17, 2025
1 parent aa2a1dc commit 83c272a
Show file tree
Hide file tree
Showing 6 changed files with 2,276 additions and 2,186 deletions.
9 changes: 9 additions & 0 deletions Core/Inc/pdu.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,13 @@ void vRTDS(void *arg);
extern osThreadId_t rtds_thread;
extern const osThreadAttr_t rtds_attributes;

/**
* @brief Read the status of brakes
*
* @param pdu Pointer to struct representing the PDU
* @param status Buffer that fuse data will be written to
* @return int8_t Error code.
*/
int8_t read_brake_state(pdu_t *pdu, bool *status);

#endif /* PDU_H */
8 changes: 0 additions & 8 deletions Core/Inc/pedals.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ void increase_torque_limit();
*/
void decrease_torque_limit();

/**
* @brief Get state of brakes
*
* @return true Brakes engaged
* @return false Brakes not engaged
*/
bool get_brake_state();

/**
* @brief Get the current torque limit percentage
*
Expand Down
25 changes: 25 additions & 0 deletions Core/Src/pdu.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#define CTRL_ADDR PCA_I2C_ADDR_2
#define RTDS_DURATION 1750 /* ms at 1kHz tick rate */

#define BRAKE_STATE false

static osMutexAttr_t pdu_mutex_attributes;

//hi2c2 variable to pass to the function wrappers (defined in main.c)
Expand Down Expand Up @@ -368,3 +370,26 @@ int8_t read_shutdown(pdu_t *pdu, bool status[MAX_SHUTDOWN_STAGES])
osMutexRelease(pdu->mutex);
return 0;
}

int8_t read_brake_state(pdu_t *pdu, bool *status)
{
if (!pdu)
return -1;

osStatus_t stat = osMutexAcquire(pdu->mutex, MUTEX_TIMEOUT);
if (stat)
return stat;

/* read pin over i2c */
uint8_t config = 0;
HAL_StatusTypeDef error = pca9539_read_pin(
pdu->ctrl_expander, PCA_INPUT_1_REG, BRKLIGHT_CTRL, &config);
if (error != HAL_OK) {
osMutexRelease(pdu->mutex);
return error;
}
*status = config;

osMutexRelease(pdu->mutex);
return 0;
}
23 changes: 0 additions & 23 deletions Core/Src/pedals.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ float torque_limit_percentage = 1.0;
#define PEDAL_DIFF_THRESH 30
#define PEDAL_FAULT_TIME 500 /* ms */

static bool brake_state = false;
osMutexId_t brake_mutex;

enum { ACCELPIN_2, ACCELPIN_1, BRAKEPIN_1, BRAKEPIN_2 };

void increase_torque_limit()
Expand All @@ -60,22 +57,6 @@ void decrease_torque_limit()
}
}

void set_brake_state(bool new_brake_state)
{
osMutexAcquire(brake_mutex, osWaitForever);
brake_state = new_brake_state;
osMutexRelease(brake_mutex);
}

bool get_brake_state()
{
bool temp;
osMutexAcquire(brake_mutex, osWaitForever);
temp = brake_state;
osMutexRelease(brake_mutex);
return temp;
}

float get_torque_limit_percentage()
{
return torque_limit_percentage;
Expand Down Expand Up @@ -413,9 +394,6 @@ void vProcessPedals(void *pv_params)
/* Send CAN messages with raw pedal readings, we do not care if it fails*/
osTimerStart(send_pedal_data_timer, 100);

/* Mutexes for setting and getting pedal values and brake state */
brake_mutex = osMutexNew(NULL);

const uint16_t delay_time = 10; /* ms */
/* End application if we try to update motor at freq below this value */
assert(delay_time < MAX_COMMAND_DELAY);
Expand All @@ -441,7 +419,6 @@ void vProcessPedals(void *pv_params)

/* Turn brakelight on or off */
write_brakelight(pdu, brake_val > PEDAL_BRAKE_THRESH);
set_brake_state(brake_val > PEDAL_BRAKE_THRESH);

/* 0.0 - 1.0 */
float accelerator_value = (float)accel_val / 100.0;
Expand Down
8 changes: 6 additions & 2 deletions Core/Src/state_machine.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,16 @@ static int transition_functional_state(func_state_t new_state, pdu_t *pdu,
return 2;
}
/* Only turn on motor if brakes engaged and tsms is on */
bool brake_state;
if (!read_brake_state(pdu, &brake_state)) {
return 2;
}
#ifdef TSMS_OVERRIDE
if (!get_brake_state()) {
if (!brake_state) {
return 3;
}
#else
if (!get_brake_state() || !get_tsms()) {
if (!brake_state || !get_tsms()) {
return 3;
}
#endif
Expand Down
Loading

0 comments on commit 83c272a

Please sign in to comment.