Skip to content

Commit

Permalink
removed most steeringio and wheel related code
Browse files Browse the repository at this point in the history
  • Loading branch information
tszwinglitw committed Oct 31, 2024
1 parent 6b52c99 commit 65a7dd9
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 193 deletions.
2 changes: 1 addition & 1 deletion Core/Inc/monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extern const osThreadAttr_t non_functional_data_attributes;
/* Arguments for the data collection thread */
typedef struct {
pdu_t *pdu;
steeringio_t *wheel;
steeringio_t *wheel; // TODO: this should be deprecated?
} data_collection_args_t;

/**
Expand Down
27 changes: 12 additions & 15 deletions Core/Inc/steeringio.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "timer.h"
#include <stdbool.h>
#include <stdint.h>
#include "can.h"

#define STEERING_CANID_IO 0x680

Expand All @@ -26,26 +27,22 @@ typedef enum {
typedef struct {
/* Necessary to allow multiple threads to access same data */
osMutexId_t *button_mutex;
nertimer_t *debounce_timers[MAX_STEERING_BUTTONS];
bool raw_buttons[MAX_STEERING_BUTTONS];
bool debounced_buttons[MAX_STEERING_BUTTONS];
/* Array indicating that a button has already been pressed and not unpressed */
bool debounced[MAX_STEERING_BUTTONS];
/* Array indicating state of each button */
bool button_state[MAX_STEERING_BUTTONS];
} steeringio_t;

/**
* @brief Creates a new steering wheel interface.
*
* @return steeringio_t* Pointer to struct defining steering wheel interface
*/
steeringio_t *steeringio_init();
// /**
// * @brief Creates a new steering wheel interface.
// *
// * @return steeringio_t* Pointer to struct defining steering wheel interface
// */
// steeringio_t *steeringio_init();

/**
* @brief Update the status of the steering wheel buttons.
*
* @param wheel Pointer to struct representing the steering wheel
* @param button_data Unsigned 8 bit integer where each bit is a button status
*
* @param can_msg can message containing data update
*/
void steeringio_update(steeringio_t *wheel, uint8_t button_data);
void steeringio_update(can_msg_t can_msg);

#endif /* STEERING_H */
6 changes: 6 additions & 0 deletions Core/Src/can_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ const osThreadAttr_t can_receive_attributes = {
.priority = (osPriority_t)osPriorityRealtime,
};




void vCanReceive(void *pv_params)
{
dti_t *mc = (dti_t *)pv_params;
Expand All @@ -157,6 +160,9 @@ void vCanReceive(void *pv_params)
case BMS_DCL_MSG:
handle_dcl_msg();
break;
case STEERING_CANID_IO:
steeringio_update(msg);
break;
default:
break;
}
Expand Down
6 changes: 3 additions & 3 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ int main(void)
assert(pdu);
dti_t *mc = dti_init();
assert(mc);
steeringio_t *wheel = steeringio_init();
assert(wheel);
// steeringio_t *wheel = steeringio_init();
// assert(wheel);
init_can1(&hcan1);
bms_init();

Expand Down Expand Up @@ -215,7 +215,7 @@ int main(void)

data_collection_args_t* data_args = malloc(sizeof(data_collection_args_t));
data_args->pdu = pdu;
data_args->wheel = wheel;
data_args->wheel = NULL; // TODO: clean up wheel
data_collection_thread = osThreadNew(vDataCollection, data_args, &data_collection_attributes);
assert(data_collection_thread);
// temp_monitor_handle = osThreadNew(vTempMonitor, mpu, &temp_monitor_attributes);
Expand Down
38 changes: 0 additions & 38 deletions Core/Src/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,40 +176,6 @@ void read_tsms(pdu_t *pdu)
}
}

/**
* @brief Read the buttons of the steering wheel, debounce them, and send a CAN message containing the raw data.
*
* @param wheel Pointer to struct defining wheel interface
*/
void steeringio_monitor(steeringio_t *wheel)
{
can_msg_t msg = { .id = 0x680, .len = 8, .data = { 0 } };
fault_data_t fault_data = { .id = BUTTONS_MONITOR_FAULT,
.severity = DEFCON5 };

uint8_t button_1 = !HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4);
uint8_t button_2 = !HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_5);
uint8_t button_3 = !HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_6);
uint8_t button_4 = !HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_7);
uint8_t button_5 = !HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_4);
uint8_t button_6 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_5);
uint8_t button_7 = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0);
uint8_t button_8 = !HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1);

uint8_t button_data = (button_1 << 7) | (button_2 << 6) |
(button_3 << 5) | (button_4 << 4) |
(button_5 << 3) | (button_6 << 2) |
(button_7 << 1) | (button_8);

steeringio_update(wheel, button_data);

/* Set the first byte to be the first 8 buttons with each bit representing the pin status */
msg.data[0] = button_data;
if (queue_can_msg(msg)) {
fault_data.diag = "Failed to send steering buttons can message";
queue_fault(&fault_data);
}
}

osThreadId_t data_collection_thread;
const osThreadAttr_t data_collection_attributes = {
Expand All @@ -222,7 +188,6 @@ void vDataCollection(void *pv_params)
{
data_collection_args_t *args = (data_collection_args_t *)pv_params;
pdu_t *pdu = args->pdu;
steeringio_t *wheel = args->wheel;
free(args);

static const uint8_t delay = 20;
Expand All @@ -232,9 +197,6 @@ void vDataCollection(void *pv_params)
for (;;) {
read_tsms(pdu);
osDelay(delay);

steeringio_monitor(wheel);
osDelay(delay);
}
}

Expand Down
183 changes: 47 additions & 136 deletions Core/Src/steeringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,26 @@

#define CAN_QUEUE_SIZE 5 /* messages */

static osMutexAttr_t steeringio_data_mutex_attributes;

static void debounce_cb(void *arg);

enum { NOT_PRESSED, PRESSED };

typedef struct debounce_cb_args_t {
steeringio_t *wheel;
steeringio_button_t button;
} debounce_cb_args_t;

steeringio_t *steeringio_init()
{
steeringio_t *steeringio = malloc(sizeof(steeringio_t));
assert(steeringio);

steeringio->button_mutex =
osMutexNew(&steeringio_data_mutex_attributes);
assert(steeringio->button_mutex);

/* Create debounce utilities */
for (uint8_t i = 0; i < MAX_STEERING_BUTTONS; i++) {
steeringio->debounce_timers[i] = malloc(sizeof(nertimer_t));
assert(steeringio->debounce_timers[i]);
}

return steeringio;
}
// steeringio_t *steeringio_init()
// {
// steeringio_t *steeringio = malloc(sizeof(steeringio_t));
// assert(steeringio);

bool get_steeringio_button(steeringio_t *wheel, steeringio_button_t button)
{
if (!wheel)
return 0;
// steeringio->button_mutex =
// osMutexNew(&steeringio_data_mutex_attributes);
// assert(steeringio->button_mutex);

bool ret = 0;
// /* Initialize button states */
// for (uint8_t i = 0; i < MAX_STEERING_BUTTONS; i++) {
// steeringio->button_state[i] = NOT_PRESSED;
// }
// return steeringio;
// }

osMutexAcquire(wheel->button_mutex, osWaitForever);
ret = wheel->debounced_buttons[button];
osMutexRelease(wheel->button_mutex);

return ret;
}

static void paddle_left_cb()
{
Expand All @@ -71,108 +49,41 @@ static void paddle_right_cb()
}
}

/**
* @brief Callback after a button has been debounced.
*
* @param arg Pointer to debounce_cb_args_t
*/
static void debounce_cb(void *arg)
void steeringio_update(can_msg_t msg)
{
debounce_cb_args_t *args = (debounce_cb_args_t *)arg;
if (!args)
return;

steeringio_button_t button = args->button;

if (!button)
return;

steeringio_t *wheel = args->wheel;

if (wheel->raw_buttons[button] && !wheel->debounced[button]) {
wheel->debounced[button] = true;
switch (button) {
case STEERING_PADDLE_LEFT:
paddle_left_cb();
break;
case STEERING_PADDLE_RIGHT:
paddle_right_cb();
break;
case NERO_BUTTON_UP:
serial_print("Up button pressed \r\n");
decrement_nero_index();
break;
case NERO_BUTTON_DOWN:
serial_print("Down button pressed \r\n");
increment_nero_index();
break;
case NERO_BUTTON_LEFT:
// doesnt effect cerb for now
break;
case NERO_BUTTON_RIGHT:
// doesnt effect cerb for now
break;
case NERO_BUTTON_SELECT:
printf("Select button pressed \r\n");
select_nero_index();
break;
case NERO_HOME:
serial_print("Home button pressed \r\n");
set_home_mode();
break;
default:
break;
}
} else if (!wheel->raw_buttons[button]) {
/* Button is no longer pressed, so it can be pressed again */
wheel->debounced[button] = false;
uint8_t button_id = msg.data[0];

switch (button_id) {
case STEERING_PADDLE_LEFT:
paddle_left_cb();
break;
case STEERING_PADDLE_RIGHT:
paddle_right_cb();
break;
case NERO_BUTTON_UP:
serial_print("Up button pressed \r\n");
decrement_nero_index();
break;
case NERO_BUTTON_DOWN:
serial_print("Down button pressed \r\n");
increment_nero_index();
break;
case NERO_BUTTON_LEFT:
// doesnt effect cerb for now
break;
case NERO_BUTTON_RIGHT:
// doesnt effect cerb for now
break;
case NERO_BUTTON_SELECT:
printf("Select button pressed \r\n");
select_nero_index();
break;
case NERO_HOME:
serial_print("Home button pressed \r\n");
set_home_mode();
break;
default:
break;
}
}

/**
* @brief Debounce every button input and update the state of the steering wheel.
*
* @param wheel Pointer to struct defining steering wheel interface.
* @param button_data Buffer containing button data where each bit is a button.
*/
void steeringio_update(steeringio_t *wheel, uint8_t button_data)
{
/* Copy message data to wheelio buffer */
osMutexAcquire(wheel->button_mutex, osWaitForever);
/* Data is formatted with each bit within the first byte representing a button and the first two bits of the second byte representing the paddle shifters */

/* Update raw buttons */
for (uint8_t i = 0; i < MAX_STEERING_BUTTONS - 2; i++) {
wheel->raw_buttons[i] = (button_data >> i) & 0x01;
}

/* Update raw paddle shifters */
wheel->raw_buttons[STEERING_PADDLE_LEFT] =
NOT_PRESSED; //(wheel_data[1] >> 0) & 0x01;
wheel->raw_buttons[STEERING_PADDLE_RIGHT] =
NOT_PRESSED; //(wheel_data[1] >> 1) & 0x01;

/* Debounce buttons */
for (uint8_t i = 0; i < MAX_STEERING_BUTTONS; i++) {
debounce_cb_args_t *debounce_args =
malloc(sizeof(debounce_cb_args_t));
debounce_args->wheel = wheel;
debounce_args->button = i;

if (wheel->raw_buttons[i])
debounce(wheel->raw_buttons[i],
wheel->debounce_timers[i],
STEERING_WHEEL_DEBOUNCE, &debounce_cb,
debounce_args);

/* Debounce that the button has been unpressed */
else
debounce(!wheel->raw_buttons[i],
wheel->debounce_timers[i],
STEERING_WHEEL_DEBOUNCE, &debounce_cb,
debounce_args);

free(debounce_args);
}
osMutexRelease(wheel->button_mutex);
}

0 comments on commit 65a7dd9

Please sign in to comment.