Skip to content

Commit

Permalink
Adding in skeleton of steering wheel interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nwdepatie committed Jan 2, 2024
1 parent 09bc588 commit 6b84079
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 105 deletions.
2 changes: 2 additions & 0 deletions Core/Inc/cerberus_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#define TORQUE_CALC_DELAY
#define FAULT_HANDLE_DELAY

#define STEERING_WHEEL_DEBOUNCE 10 /* ms */

/* Pin Assignments */
#define FAULT_MCU_Pin GPIO_PIN_3
#define FAULT_MCU_GPIO_Port GPIOC
Expand Down
139 changes: 35 additions & 104 deletions Core/Inc/steeringio.h
Original file line number Diff line number Diff line change
@@ -1,104 +1,35 @@
Button::Button(){}

Button::~Button(){}

void Button::checkButtonPin()
{
if(isPressed == true && state == NOT_PRESSED)
{
state = DEBOUNCING;
debounce.startTimer(BUTTON_DEBOUNCE_TIME);
}
else if(isPressed == false)
{
debounce.cancelTimer();
state = NOT_PRESSED;
}
}

bool Button::isButtonPressed()
{
if(state == PRESSED) return true;

/**
* Returns true if
* 1. The button has successfully passed the debounce period
* 2. The debounce timer has not been canceled/reset
* 3. The button is still being held
*/
if(isPressed && debounce.isTimerExpired() && !debounce.isTimerReset())
{
state = PRESSED;
return true;
}
return false;
}

bool Button::isButtonPressed_Pulse()
{
if(state == PRESSED) return false;

/**
* Returns true if
* 1. The button has successfully passed the debounce period
* 2. The debounce timer has not been canceled/reset
* 3. The button is still being held
*
* AND
*
* 4. The state of the button has not been read since the button has passed the debounce
*/
if(isPressed && debounce.isTimerExpired() && !debounce.isTimerReset())
{
state = PRESSED;
return true;
}

return false;
}

void Button::setButtonState(bool buttonState)
{
isPressed = buttonState;
}

void DriverIO::wheelIO_cb(const CAN_message_t &msg)
{
union
{
uint8_t msg[8];

struct
{
uint16_t pot_l;
uint16_t pot_r;
bool button5 : 1;
bool button6 : 1;
bool button3 : 1;
uint8_t x1 : 1;
bool paddle_r : 1;
bool paddle_l : 1;
bool button4 : 1;
bool button2 : 1;
uint8_t x2;
uint8_t x3;
uint8_t x4;
} io;
} wheelio;

for(uint8_t byte = 0; byte < 8; byte++)
{
wheelio.msg[byte] = msg.buf[byte];
}

wheelio.io.pot_l = SWITCHBYTES(wheelio.io.pot_l);
wheelio.io.pot_r = SWITCHBYTES(wheelio.io.pot_r);

decrButton.setButtonState(wheelio.io.button2);
incrButton.setButtonState(wheelio.io.button4);
regenButton.setButtonState(wheelio.io.button5);
torqueIncreasePaddle.setButtonState(wheelio.io.paddle_r);
torqueDecreasePaddle.setButtonState(wheelio.io.paddle_l);
accumulatorFanDial.setDialValue(wheelio.io.pot_l);
motorFanDial.setDialValue(wheelio.io.pot_r);
}
#ifndef STEERING_H
#define STEERING_H

#include <stdint.h>
#include <stdbool.h>
#include "ringbuffer.h"
#include "cmsis_os.h"

typedef enum {
STEERING_PADDLE_LEFT,
STEERING_PADDLE_RIGHT,
NERO_BUTTON_UP,
NERO_BUTTON_DOWN,
NERO_BUTTON_LEFT,
NERO_BUTTON_RIGHT,
NERO_BUTTON_SELECT,
MAX_STEERING_BUTTONS
} steeringio_button_t;

typedef struct {
osMutexId_t *button_mutex; /* Necessary to allow multiple threads to access same data */
osMutexId_t *ringbuffer_mutex;
osTimerId_t debounce_timers[MAX_STEERING_BUTTONS];
ringbuffer_t* debounce_buffer;
} steeringio_t;

/* Creates a new Steering Wheel interface */
steeringio_t *steeringio_init();

bool get_steeringio_button(steeringio_t *wheel, steeringio_button_t button);

/* For updating values via the wheel's CAN message */
void steeringio_update(steeringio_t *wheel, uint8_t wheel_data[], uint8_t len);

#endif /* STEERING_H */
84 changes: 84 additions & 0 deletions Core/Src/steeringio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "steeringio.h"
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include "cmsis_os.h"
#include "cerberus_conf.h"

static osMutexAttr_t steeringio_data_mutex_attributes;
static osMutexAttr_t steeringio_ringbuffer_mutex_attributes;

static void debounce_cb(void const *arg);
osTimerDef(debounce_timer, debounce_cb);

/* Creates a new Steering Wheel interface */
steeringio_t *steeringio_init()
{
steeringio_t *steeringio = malloc(sizeof(steeringio_t));
assert(steeringio);

/* Create Mutexes */
steeringio->ringbuffer_mutex = osMutexNew(&steeringio_data_mutex_attributes);
assert(steeringio->ringbuffer_mutex);

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

//TODO: Error handler for allocation
for (uint8_t i = 0; i < MAX_STEERING_BUTTONS; i++) {
steeringio->debounce_timers[i] = osTimerCreate(osTimer(debounce_timer), osTimerOnce, steeringio);
assert(steeringio->debounce_timers[i]);
}

steeringio->debounce_buffer = ringbuffer_create(MAX_STEERING_BUTTONS, sizeof(uint8_t));
assert(steeringio->debounce_buffer);

return steeringio;
}

bool get_steeringio_button(steeringio_t *wheel, steeringio_button_t button)
{
bool ret;

osMutexAcquire(wheel->button_mutex, osWaitForever);
//ret = getdatasomehow;
osRelease(wheel->button_mutex);

return ret;
}

/* Callback to see if we have successfully debounced */
static void debounce_cb(void const *arg)
{
steeringio_t *wheel = (steeringio_t *)arg;
steeringio_button_t button;

/*
* Pop off most recent button being debounced
* Note: timers are started sequentially, therefore buttons
* will be popped off in order
*
* Also needs to be atomic
*/
osMutexAcquire(wheel->ringbuffer_mutex, osWaitForever);
ringbuffer_dequeue(wheel->debounce_buffer, &button);
osRelease(wheel->ringbuffer_mutex);

if (!button)
return;

// debounce logic
}

/* For updating values via the wheel's CAN message */
void steeringio_update(steeringio_t *wheel, uint8_t wheel_data[], uint8_t len)
{
if (!wheel || !wheel_data)
return;

/* Copy message data to wheelio buffer */
//memcpy(&io, wheel_data, len);

/* If the value was set high and its not being debounced, trigger timer for debounce */
//for ()
}
2 changes: 1 addition & 1 deletion Drivers/Embedded-Base
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Core/Src/state_machine.c \
Core/Src/torque.c \
Core/Src/pdu.c \
Core/Src/mpu.c \
Core/Src/steeringio.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_can.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c \
Expand All @@ -79,6 +80,7 @@ Drivers/Embedded-Base/general/src/sht30.c \
Drivers/Embedded-Base/platforms/stm32f405/src/can.c \
Drivers/Embedded-Base/general/src/pi4ioe.c \
Drivers/Embedded-Base/middleware/src/timer.c \
Drivers/Embedded-Base/middleware/src/ringbuffer.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_adc.c \
Expand Down

0 comments on commit 6b84079

Please sign in to comment.