CM_DOG/User/task/atti_esti.c
2025-06-26 05:11:10 +08:00

133 lines
4.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
atti_esti Task
陀螺仪接收解算任务
*/
/* Includes ----------------------------------------------------------------- */
#include "task/user_task.h"
/* USER INCLUDE BEGIN*/
#include "bsp/pwm.h"
#include "bsp/gpio.h"
#include "component/ahrs.h"
#include "component/pid.h"
#include "device/bmi088.h"
#include "device/n100.h"
#include "task/user_task.h"
/* USER INCLUDE END*/
/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */
/* USER STRUCT BEGIN*/
BMI088_t bmi088;
N100_t n100;
BMI088_Cali_t bmi088_cali = {
.gyro_offset = {0.0f, 0.0f, 0.0f},
}; /* BMI088校准数据 */
AHRS_Magn_t mage = {0.0f, 0.0f, 0.0f}; /* 磁力计数据,未使用 */
AHRS_t gimbal_ahrs;
AHRS_Eulr_t eulr_to_send;
KPID_t imu_temp_ctrl_pid;
const KPID_Params_t imu_temp_ctrl_pid_param = {
.k = 0.3f,
.p = 1.0f,
.i = 0.0f,
.d = 0.0f,
.i_limit = 1.0f,
.out_limit = 1.0f,
};
/* USER STRUCT END*/
/* Private function --------------------------------------------------------- */
/* Exported functions ------------------------------------------------------- */
void Task_atti_esti(void *argument) {
(void)argument; /* 未使用argument消除警告 */
osDelay(ATTI_ESTI_INIT_DELAY); /* 延时一段时间再开启任务 */
/* USER CODE INIT BEGIN*/
switch (task_runtime.config.chassis_imu_type) {
case IMU_BOARD_BMI088:
BMI088_Init(&bmi088, &bmi088_cali); /* 初始化BMI088传感器 */
AHRS_Init(&gimbal_ahrs, &mage, BMI088_GetUpdateFreq(&bmi088)); /* 初始化姿态解算算法 */
/* 初始化IMU温度控制PID防止温漂 */
// PID_Init(&imu_temp_ctrl_pid, KPID_MODE_NO_D,
// 1.0f / BMI088_GetUpdateFreq(&bmi088), &imu_temp_ctrl_pid_param);
// BSP_PWM_Start(BSP_PWM_IMU_HEAT);
break;
case IMU_WHEELREC_N100:
N100_Init(&n100);
BSP_GPIO_SetPin(BSP_GPIO_5V_EN, BSP_GPIO_ON); /* 打开5V电源 */
break;
default:
break;
}
/* USER CODE INIT END*/
while (1) {
/* USER CODE BEGIN */
switch (task_runtime.config.chassis_imu_type) {
case IMU_BOARD_BMI088:
/* 等待IMU新数据 */
BMI088_WaitNew();
/* 开始数据接收DMA加速度计和陀螺仪共用同一个SPI接口
* 一次只能开启一个DMA
*/
BMI088_AcclStartDmaRecv();
BMI088_AcclWaitDmaCplt();
BMI088_GyroStartDmaRecv();
BMI088_GyroWaitDmaCplt();
/* 锁住RTOS内核防止数据解析过程中断造成错误 */
osKernelLock();
/* 接收完所有数据后,把数据从原始字节加工成方便计算的数据 */
BMI088_ParseAccl(&bmi088);
BMI088_ParseGyro(&bmi088);
/* 根据设备接收到的数据进行姿态解析 */
AHRS_Update(&gimbal_ahrs, &bmi088.accl, &bmi088.gyro, &mage);
/* 根据解析出来的四元数计算欧拉角 */
AHRS_GetEulr(&eulr_to_send, &gimbal_ahrs);
osKernelUnlock();
/* 将欧拉角数据放入消息队列 */
osMessageQueueReset(task_runtime.msgq.body.eulr_imu);
osMessageQueuePut(task_runtime.msgq.body.eulr_imu, &eulr_to_send, 0, 0); /* 将欧拉角数据放入消息队列 */
/* PID控制IMU温度PWM输出 */
// BSP_PWM_Set(BSP_PWM_IMU_HEAT,
// PID_Calc(&imu_temp_ctrl_pid, 40.0f, bmi088.temp, 0.0f, 0.01f));
break;
case IMU_WHEELREC_N100:
N100_StartReceiving(&n100);
if (N100_WaitDmaCplt()) {
osKernelLock();
N100_ParseData(&n100);
osKernelUnlock();
} else {
N100_HandleOffline(&n100);
}
osMessageQueueReset(task_runtime.msgq.body.eulr_imu); /* 重置消息队列 */
osMessageQueuePut(task_runtime.msgq.body.eulr_imu, &n100.eulr, 0, 0); /* 将欧拉角数据放入消息队列 */
break;
default:
break;
}
/* USER CODE END */
}
}