From 65a7dd91433bd8e1a3915d7a7e353a0a0148cb33 Mon Sep 17 00:00:00 2001
From: tszwingli
Date: Thu, 31 Oct 2024 17:31:58 -0400
Subject: [PATCH] removed most steeringio and wheel related code
---
Core/Inc/monitor.h | 2 +-
Core/Inc/steeringio.h | 27 +++---
Core/Src/can_handler.c | 6 ++
Core/Src/main.c | 6 +-
Core/Src/monitor.c | 38 ---------
Core/Src/steeringio.c | 183 +++++++++++------------------------------
6 files changed, 69 insertions(+), 193 deletions(-)
diff --git a/Core/Inc/monitor.h b/Core/Inc/monitor.h
index 9098266..ccfe6f2 100644
--- a/Core/Inc/monitor.h
+++ b/Core/Inc/monitor.h
@@ -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;
/**
diff --git a/Core/Inc/steeringio.h b/Core/Inc/steeringio.h
index e4294ff..183bef6 100644
--- a/Core/Inc/steeringio.h
+++ b/Core/Inc/steeringio.h
@@ -6,6 +6,7 @@
#include "timer.h"
#include
#include
+#include "can.h"
#define STEERING_CANID_IO 0x680
@@ -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 */
diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c
index ee8c24e..713f97b 100644
--- a/Core/Src/can_handler.c
+++ b/Core/Src/can_handler.c
@@ -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;
@@ -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;
}
diff --git a/Core/Src/main.c b/Core/Src/main.c
index 42a8059..d5a777c 100644
--- a/Core/Src/main.c
+++ b/Core/Src/main.c
@@ -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();
@@ -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);
diff --git a/Core/Src/monitor.c b/Core/Src/monitor.c
index 1bfb332..8a424cc 100644
--- a/Core/Src/monitor.c
+++ b/Core/Src/monitor.c
@@ -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 = {
@@ -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;
@@ -232,9 +197,6 @@ void vDataCollection(void *pv_params)
for (;;) {
read_tsms(pdu);
osDelay(delay);
-
- steeringio_monitor(wheel);
- osDelay(delay);
}
}
diff --git a/Core/Src/steeringio.c b/Core/Src/steeringio.c
index 3eaf99c..47ce594 100644
--- a/Core/Src/steeringio.c
+++ b/Core/Src/steeringio.c
@@ -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()
{
@@ -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);
-}