Skip to content

Commit

Permalink
locked and unlocked mutex when taking measurements
Browse files Browse the repository at this point in the history
  • Loading branch information
meganlai27 committed Nov 19, 2023
1 parent cf5c5f7 commit e232bfd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
14 changes: 8 additions & 6 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ osMutexId_t mutex_id;
osMutexAttr_t mutex;

struct {
osMutexAttr_t mut;
osMutexId_t mut;
I2C_HandleTypeDef i2c;
} mutI2Cbus;

Expand Down Expand Up @@ -94,8 +94,9 @@ void StartDefaultTask(void *argument);
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

void CreateMutex() {
osMutexId_t CreateMutex() {
mutex_id = osMutexNew(&mutex);
return *mutex_id;
}
/* USER CODE END 0 */

Expand All @@ -115,8 +116,9 @@ int main(void)
HAL_Init();

/* USER CODE BEGIN Init */

mutI2Cbus.mut = mutex;

CreateMutex();
mutI2Cbus.mut = mutex_id;
mutI2Cbus.i2c = hi2c1;

/* USER CODE END Init */
Expand Down Expand Up @@ -168,9 +170,9 @@ int main(void)
defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

/* USER CODE BEGIN RTOS_THREADS */
temp_monitor_handle = osThreadNew(vTempMonitor, &hi2c1, &temp_monitor_attributes);
temp_monitor_handle = osThreadNew(vTempMonitor, &mutI2Cbus, &temp_monitor_attributes); // replace with structs
watchdog_monitor_handle = osThreadNew(vWatchdogMonitor, GPIOB, &watchdog_monitor_attributes);
imu_monitor_handle = osThreadNew(vIMUMonitor, &hi2c1, &imu_monitor_attributes);
imu_monitor_handle = osThreadNew(vIMUMonitor, &mutI2Cbus, &imu_monitor_attributes);

//TODO: Get correct ADC/GPIO value
pedals_monitor_handle = osThreadNew(vPedalsMonitor, &hadc1, &pedals_monitor_attributes);
Expand Down
17 changes: 13 additions & 4 deletions Core/Src/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,31 @@ void vTempMonitor(void* pv_params)
static onboard_temp_t sensor_data;
fault_data_t fault_data = { .id = ONBOARD_TEMP_FAULT, .severity = DEFCON4 };
sht30_t temp_sensor;
I2C_HandleTypeDef* hi2c1;
osMutexId_t* i2c_mut;
can_msg_t temp_msg
= { .id = CANID_TEMP_SENSOR, .len = can_msg_len, .line = CAN_LINE_1, .data = { 0 } };

hi2c1 = (I2C_HandleTypeDef*)pv_params;
temp_sensor.i2c_handle = hi2c1;

//hi2c1 = (I2C_HandleTypeDef*)pv_params;
//temp_sensor.i2c_handle = hi2c1;

osMutexAcquire(pv_params.mutex_id, NULL); // lock

if (sht30_init(&temp_sensor)) {
fault_data.diag = "Init Failed";
osMessageQueuePut(fault_handle_queue, &fault_data, 0U, 0U);
}

osMutexRelease(pv_params.mutex_id); // unlock

for (;;) {
osMutexAcquire(pv_params.mutex_id, NULL); // lock

/* Take measurement */
if (sht30_get_temp_humid(&temp_sensor)) {
fault_data.diag = "Failed to get temp";
osMessageQueuePut(fault_handle_queue, &fault_data, 0U, 0U);
}
osMutexRelease(pv_params.mutex_id); // unlock

/* Run values through LPF of sample size */
sensor_data.temperature = (sensor_data.temperature + temp_sensor.temp) / num_samples;
Expand Down Expand Up @@ -194,6 +201,7 @@ void vIMUMonitor(void *pv_params)
}

for(;;) {
osMutexAcquire(pv_params.mutex_id, NULL); // lock
/* Take measurement */
if (lsm6dso_read_accel(&imu)) {
fault_data.diag = "Failed to get IMU acceleration";
Expand All @@ -204,6 +212,7 @@ void vIMUMonitor(void *pv_params)
fault_data.diag = "Failed to get IMU gyroscope";
osMessageQueuePut(fault_handle_queue, &fault_data , 0U, 0U);
}
osMutexRelease(pv_params.mutex_id); // unlock

/* Run values through LPF of sample size */
sensor_data.accel_x = (sensor_data.accel_x + imu.accel_data[0])
Expand Down

0 comments on commit e232bfd

Please sign in to comment.