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
416static 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
2940static 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+
3450HAL_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
0 commit comments