Skip to content

Commit 7e35462

Browse files
committed
Issue #22: Implementation the FreeRTOS timer
1 parent 6d7ea94 commit 7e35462

3 files changed

Lines changed: 36 additions & 5 deletions

File tree

KPI_Rover/IMU/ulImu.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
#include "ulImu.h"
2+
#include "cmsis_os.h"
3+
#include "ulog.h"
4+
#include "Database/ulDatabase.h"
5+
6+
7+
#define IMU_POLL_PERIOD_MS 20
8+
9+
10+
static osTimerId_t imuTimerHandle;
11+
12+
static float accel_bias[3] = {0.0f, 0.0f, 0.0f};
13+
static float gyro_bias[3] = {0.0f, 0.0f, 0.0f};
214

315

416
static MPU_Config_t imuCfg = {
@@ -22,15 +34,19 @@ static const float GyroScales[] = {
2234
};
2335

2436

25-
static float accel_bias[3] = {0.0f, 0.0f, 0.0f};
26-
static float gyro_bias[3] = {0.0f, 0.0f, 0.0f};
37+
void ulImu_Update(I2C_HandleTypeDef *hi2c);
2738

2839

2940
static void ulImu_CalibrateGyro(I2C_HandleTypeDef *hi2c) {
3041
// TODO calibrate
3142
ulDatabase_setUint8(IMU_IS_CALIBRATED, 1);
3243
}
3344

45+
void ulImu_TimerCallback(void *argument) {
46+
I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)argument;
47+
ulImu_Update(hi2c);
48+
}
49+
3450
HAL_StatusTypeDef ulImu_Init(I2C_HandleTypeDef *hi2c) {
3551
HAL_StatusTypeDef status;
3652

@@ -41,6 +57,20 @@ HAL_StatusTypeDef ulImu_Init(I2C_HandleTypeDef *hi2c) {
4157

4258
ulImu_CalibrateGyro(hi2c);
4359

60+
const osTimerAttr_t imuTimer_attributes = {
61+
.name = "IMUTimer"
62+
};
63+
64+
imuTimerHandle = osTimerNew(ulImu_TimerCallback, osTimerPeriodic, (void*)hi2c, &imuTimer_attributes);
65+
if (imuTimerHandle == NULL) {
66+
ULOG_ERROR("Failed to create IMU timer");
67+
return HAL_ERROR;
68+
}
69+
if (osTimerStart(imuTimerHandle, IMU_POLL_PERIOD_MS) != osOK) {
70+
ULOG_ERROR("Failed to start IMU timer");
71+
return HAL_ERROR;
72+
}
73+
4474
return HAL_OK;
4575
}
4676

KPI_Rover/IMU/ulImu.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
#define IMU_ULIMU_H_
33

44
#include "drvImu.h"
5-
#include "Database/ulDatabase.h"
65

76

87
#define GRAVITY_MS2 9.81f
98

109

1110
HAL_StatusTypeDef ulImu_Init(I2C_HandleTypeDef *hi2c);
12-
void ulImu_Update(I2C_HandleTypeDef *hi2c);
1311

1412
#endif /* IMU_ULIMU_H_ */

KPI_Rover/KPIRover.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "cmsis_os.h"
33

44
#include "Database/ulDatabase.h"
5-
5+
#include "IMU/ulImu.h"
66

77
static struct ulDatabase_ParamMetadata ulDatabase_params[] = {
88
{0, INT32, false, 0}, // MOTOR_FL_RPM,
@@ -20,7 +20,10 @@ static struct ulDatabase_ParamMetadata ulDatabase_params[] = {
2020
{0, UINT8, false, 0}, // IMU_IS_CALIBRATED
2121
};
2222

23+
extern I2C_HandleTypeDef hi2c3;
24+
2325
void KPIRover_Init(void) {
2426
ulDatabase_init(ulDatabase_params, sizeof(ulDatabase_params) / sizeof(struct ulDatabase_ParamMetadata));
2527
ulEncoder_Init();
28+
ulImu_Init(&hi2c3);
2629
}

0 commit comments

Comments
 (0)