Skip to content

Commit

Permalink
'#52 Can messaging and feedback'
Browse files Browse the repository at this point in the history
  • Loading branch information
Peyton-McKee authored and nwdepatie committed Oct 24, 2023
1 parent 5540033 commit fa945a0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Core/Inc/cerberus_conf.h
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#define CANID_TEMP_SENSOR 0xBEEF
#define CANID_TEMP_SENSOR 0xBEEF
#define CANID_PEDAL_SENSOR 0xDEAD
3 changes: 2 additions & 1 deletion Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ int main(void)
/* USER CODE BEGIN RTOS_THREADS */
temp_monitor_handle = osThreadNew(vTempMonitor, &hi2c1, &temp_monitor_attributes);
watchdog_monitor_handle = osThreadNew(vWatchdogMonitor, GPIOB, &watchdog_monitor_attributes);
pedals_monitor_handle = osThreadNew(vPedalsMonitor, &hi2c2, &pedals_monitor_attributes);
//TODO: Get correct ADC/GPIO value
pedals_monitor_handle = osThreadNew(vPedalsMonitor, &hadc1, &pedals_monitor_attributes);
route_can_incoming_handle = osThreadNew(vRouteCanIncoming, &hcan1, &route_can_incoming_attributes);

/* USER CODE END RTOS_THREADS */
Expand Down
42 changes: 33 additions & 9 deletions Core/Src/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,35 +106,59 @@ const osThreadAttr_t pedals_monitor_attributes = {
void vPedalsMonitor(void *pv_params) {
const uint8_t num_samples = 10;
const accelerator1PinAddr = 1;
//TODO: Get actual pin addresses
const accelerator2PinAddr = 2;
const brake1PinAddr = 3;
const brake2PinAddr = 4;
//TODO: Get actual delay time
const delayTime = 500; /* ms */
const uint8_t can_msg_len = 4; /* bytes */

static onboard_pedals_t sensor_data;
fault_data_t fault_data = {
.id = ONBOARD_PEDAL_FAULT,
.severity = DEFCON4
.severity = DEFCON1
};


can_msg_t pedal_msg = {
.id = CANID_PEDAL_SENSOR,
.len = can_msg_len,
.line = CAN_LINE_1,
.data = {0}
};

/* Handle ADC Data for two input accelerator value and two input brake value*/
ADC_HandleTypeDef *hadc1 = (ADC_HandleTypeDef *)pv_params;
uint32_t adc_data[4];

/* STM has a 12 bit resolution so we can mark each value as uint16 */
uint16_t accel_adc_data[2];
uint16_t brake_adc_data[2];


for (;;) {
/* Get the value from the adc at the brake and accelerator pin addresses and average them to the sensor data value */
adc_data[0] = HAL_ADC_GetValue(hadc1, accelerator1PinAddr);
adc_data[1] = HAL_ADC_GetValue(hadc1, accelerator2PinAddr);
adc_data[2] = HAL_ADC_GetValue(hadc1, brake1PinAddr);
adc_data[3] = HAL_ADC_GetValue(hadc1, brake2PinAddr);
accel_adc_data[0] = HAL_ADC_GetValue(hadc1, accelerator1PinAddr);
accel_adc_data[1] = HAL_ADC_GetValue(hadc1, accelerator2PinAddr);
brake_adc_data[0] = HAL_ADC_GetValue(hadc1, brake1PinAddr);
brake_adc_data[1] = HAL_ADC_GetValue(hadc1, brake2PinAddr);

sensor_data.acceleratorValue = (sensor_data.acceleratorValue + (adc_data[0] + adc_data[1]) / 2) / num_samples;
sensor_data.brakeValue = (sensor_data.brakeValue + (adc_data[2] + adc_data[3]) / 2) / num_samples;
sensor_data.acceleratorValue = (sensor_data.acceleratorValue + (accel_adc_data[0] + accel_adc_data[1]) / 2) / num_samples;
sensor_data.brakeValue = (sensor_data.brakeValue + (brake_adc_data[0] + brake_adc_data[1]) / 2) / num_samples;

//TODO:

/* Publish to Onboard Pedals Queue */
osMessageQueuePut(onboard_pedals_queue, &sensor_data, 0U, 0U);

/* Send CAN message */
memcpy(pedal_msg.data, &sensor_data, can_msg_len);
if (can_send_message(pedal_msg)) {
fault_data.diag = "Failed to send CAN message";
osMessageQueuePut(fault_handle_queue, &fault_data , 0U, 0U);
}

/* Yield to other tasks */
osThreadYield();
osDelayUntil(delayTime);
}
}

0 comments on commit fa945a0

Please sign in to comment.