Skip to content

Commit

Permalink
Merging main
Browse files Browse the repository at this point in the history
  • Loading branch information
nwdepatie committed Dec 14, 2023
2 parents 45be0c3 + 2c63df2 commit 896b57a
Show file tree
Hide file tree
Showing 8 changed files with 540 additions and 277 deletions.
26 changes: 13 additions & 13 deletions .mxproject

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions Core/Inc/monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CERBERUS_MONITOR_H

#include "cmsis_os.h"
#include "stm32f4xx_hal.h"

/* Defining Temperature Monitor Task */
void vTempMonitor(void *pv_params);
Expand All @@ -13,6 +14,18 @@ void vWatchdogMonitor(void *pv_params);
extern osThreadId_t watchdog_monitor_handle;
extern const osThreadAttr_t watchdog_monitor_attributes;

typedef struct
{
ADC_HandleTypeDef *accel_adc1;
ADC_HandleTypeDef *accel_adc2;
ADC_HandleTypeDef *brake_adc;
} pedal_params_t;

/* Parameters for the pedal monitoring task */
#define MAX_ADC_VAL_12B 4096
#define PEDAL_DIFF_THRESH 10
#define PEDAL_FAULT_TIME 1000 /* ms */

/* Task for Reading in Pedal Inputs (Brakes + Accelerator) */
void vPedalsMonitor(void *pv_params);
extern osThreadId_t pedals_monitor_handle;
Expand Down
131 changes: 126 additions & 5 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@

/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;
ADC_HandleTypeDef hadc2;
ADC_HandleTypeDef hadc3;

CAN_HandleTypeDef hcan1;

Expand Down Expand Up @@ -82,6 +84,8 @@ static void MX_SPI1_Init(void);
static void MX_SPI2_Init(void);
static void MX_ADC1_Init(void);
static void MX_USART3_UART_Init(void);
static void MX_ADC2_Init(void);
static void MX_ADC3_Init(void);
void StartDefaultTask(void *argument);

/* USER CODE BEGIN PFP */
Expand Down Expand Up @@ -148,6 +152,8 @@ int main(void)
MX_SPI2_Init();
MX_ADC1_Init();
MX_USART3_UART_Init();
MX_ADC2_Init();
MX_ADC3_Init();
/* USER CODE BEGIN 2 */

HAL_GPIO_TogglePin(GPIOC, LED_2_Pin); // Toggle on LED2
Expand Down Expand Up @@ -178,6 +184,8 @@ int main(void)
/* USER CODE BEGIN RTOS_QUEUES */
onboard_temp_queue = osMessageQueueNew(ONBOARD_TEMP_QUEUE_SIZE, sizeof(onboard_temp_t), NULL);
imu_queue = osMessageQueueNew(IMU_QUEUE_SIZE, sizeof(imu_data_t), NULL);

pedal_params_t pedal_params = {.accel_adc1 = &hadc1, .accel_adc2 = &hadc2, .brake_adc = &hadc3};
pedal_data_queue = osMessageQueueNew(PEDAL_DATA_QUEUE_SIZE, sizeof(pedals_t), NULL);
/* USER CODE END RTOS_QUEUES */

Expand All @@ -191,16 +199,16 @@ int main(void)
imu_monitor_handle = osThreadNew(vIMUMonitor, &hi2c1, &imu_monitor_attributes);
serial_monitor_handle = osThreadNew(vSerialMonitor, NULL, &serial_monitor_attributes);
fault_handle = osThreadNew(vFaultHandler, NULL, &fault_handle_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);
pedals_monitor_handle = osThreadNew(vPedalsMonitor, &pedal_params, &pedals_monitor_attributes);
route_can_incoming_handle = osThreadNew(vRouteCanIncoming, &hcan1, &route_can_incoming_attributes);
can_dispatch_handle = osThreadNew(vCanDispatch, &hcan1, &can_dispatch_attributes);

/* USER CODE END RTOS_THREADS */

/* USER CODE BEGIN RTOS_EVENTS */
/* add events, ... */
/* USER CODE END RTOS_EVENTS */

/* Start scheduler */
osKernelStart();
/* We should never get here as control is now taken by the scheduler */
Expand Down Expand Up @@ -280,7 +288,7 @@ static void MX_ADC1_Init(void)
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
Expand All @@ -297,7 +305,7 @@ static void MX_ADC1_Init(void)
*/
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
Expand All @@ -308,6 +316,119 @@ static void MX_ADC1_Init(void)

}

/**
* @brief ADC2 Initialization Function
* @param None
* @retval None
*/
static void MX_ADC2_Init(void)
{

/* USER CODE BEGIN ADC2_Init 0 */

/* USER CODE END ADC2_Init 0 */

ADC_ChannelConfTypeDef sConfig = {0};

/* USER CODE BEGIN ADC2_Init 1 */

/* USER CODE END ADC2_Init 1 */

/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc2.Instance = ADC2;
hadc2.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc2.Init.Resolution = ADC_RESOLUTION_12B;
hadc2.Init.ScanConvMode = DISABLE;
hadc2.Init.ContinuousConvMode = ENABLE;
hadc2.Init.DiscontinuousConvMode = DISABLE;
hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc2.Init.NbrOfConversion = 1;
hadc2.Init.DMAContinuousRequests = DISABLE;
hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc2) != HAL_OK)
{
Error_Handler();
}

/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC2_Init 2 */

/* USER CODE END ADC2_Init 2 */

}

/**
* @brief ADC3 Initialization Function
* @param None
* @retval None
*/
static void MX_ADC3_Init(void)
{

/* USER CODE BEGIN ADC3_Init 0 */

/* USER CODE END ADC3_Init 0 */

ADC_ChannelConfTypeDef sConfig = {0};

/* USER CODE BEGIN ADC3_Init 1 */

/* USER CODE END ADC3_Init 1 */

/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc3.Instance = ADC3;
hadc3.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc3.Init.Resolution = ADC_RESOLUTION_12B;
hadc3.Init.ScanConvMode = ENABLE;
hadc3.Init.ContinuousConvMode = ENABLE;
hadc3.Init.DiscontinuousConvMode = DISABLE;
hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc3.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc3.Init.NbrOfConversion = 2;
hadc3.Init.DMAContinuousRequests = DISABLE;
hadc3.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc3) != HAL_OK)
{
Error_Handler();
}

/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_2;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
{
Error_Handler();
}

/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_3;
sConfig.Rank = 2;
if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC3_Init 2 */

/* USER CODE END ADC3_Init 2 */

}

/**
* @brief CAN1 Initialization Function
* @param None
Expand Down
50 changes: 41 additions & 9 deletions Core/Src/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "task.h"
#include "lsm6dso.h"
#include "serial_monitor.h"
#include "timer.h"

osThreadId_t temp_monitor_handle;
const osThreadAttr_t temp_monitor_attributes = {
Expand Down Expand Up @@ -86,7 +87,6 @@ void vWatchdogMonitor(void* pv_params)
}
}


osThreadId_t pedals_monitor_handle;
const osThreadAttr_t pedals_monitor_attributes = {
.name = "PedalMonitor",
Expand All @@ -101,29 +101,63 @@ void vPedalsMonitor(void* pv_params)
const uint16_t delayTime = 2; /* ms */
const uint8_t can_msg_len = 4; /* bytes */

nertimer_t diff_timer;
nertimer_t sc_timer;
nertimer_t oc_timer;

static pedals_t sensor_data;
fault_data_t fault_data = { .id = ONBOARD_PEDAL_FAULT, .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;
pedal_params_t* params = (pedal_params_t*)pv_params;

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

HAL_ADC_Start(hadc1);
HAL_ADC_Start(params->accel_adc1);
HAL_ADC_Start(params->accel_adc2);
HAL_ADC_Start(params->brake_adc);

for (;;) {
serial_print("Pedals Task\r\n");
/*
* Get the value from the adc at the brake and accelerator
* pin addresses and average them to the sensor data value
*/
//TODO: This probably will not work. We need to use DMA better
//HAL_ADC_PollForConversion(hadc1, HAL_MAX_DELAY);
//memcpy(adc_data, HAL_ADC_GetValue(hadc1), sizeof(adc_data));
adc_data[ACCELPIN_1] = HAL_ADC_PollForConversion(params->accel_adc1, HAL_MAX_DELAY);
adc_data[ACCELPIN_2] = HAL_ADC_PollForConversion(params->accel_adc2, HAL_MAX_DELAY);
adc_data[BRAKEPIN_1] = HAL_ADC_PollForConversion(params->brake_adc, HAL_MAX_DELAY);
adc_data[BRAKEPIN_2] = HAL_ADC_PollForConversion(params->brake_adc, HAL_MAX_DELAY);

/* Evaluate accelerator faults */
if (is_timer_expired(&oc_timer))
//todo queue fault
continue;
else if ((adc_data[ACCELPIN_1] == MAX_ADC_VAL_12B || adc_data[ACCELPIN_2] == MAX_ADC_VAL_12B) &&
!is_timer_active(&oc_timer))
start_timer(&oc_timer, PEDAL_FAULT_TIME);
else
cancel_timer(&oc_timer);

if (is_timer_expired(&sc_timer))
//todo queue fault
continue;
else if ((adc_data[ACCELPIN_1] == 0 || adc_data[ACCELPIN_2] == 0) &&
!is_timer_active(&sc_timer))
start_timer(&sc_timer, PEDAL_FAULT_TIME);
else
cancel_timer(&sc_timer);

if (is_timer_expired(&diff_timer))
//todo queue fault
continue;
else if ((adc_data[ACCELPIN_1] - adc_data[ACCELPIN_2] > PEDAL_DIFF_THRESH * MAX_ADC_VAL_12B) &&
!is_timer_active(&diff_timer))
start_timer(&diff_timer, PEDAL_FAULT_TIME);
else
cancel_timer(&diff_timer);

//sensor_data.acceleratorValue
// = (sensor_data.acceleratorValue + (adc_data[ACCELPIN_1] + adc_data[ACCELPIN_2]) / 2)
Expand All @@ -132,8 +166,6 @@ void vPedalsMonitor(void* pv_params)
// = (sensor_data.brakeValue + (adc_data[BRAKEPIN_1] + adc_data[BRAKEPIN_2]) / 2)
// / num_samples;

// TODO: detect pedal errors

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

Expand Down
Loading

0 comments on commit 896b57a

Please sign in to comment.