rm_balance/User/task/imu.c
2025-10-05 03:25:18 +08:00

129 lines
5.2 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.

/*
imu Task
*/
/* Includes ----------------------------------------------------------------- */
#include "task/user_task.h"
/* USER INCLUDE BEGIN */
#include "bsp/can.h"
#include "bsp/time.h"
#include "component/ahrs.h"
#include <string.h>
#include <stdint.h>
/* USER INCLUDE END */
/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* AHRS数据CAN ID定义 */
#define CAN_ID_AHRS_ACCL 0x301 /* 加速度计数据 */
#define CAN_ID_AHRS_GYRO 0x302 /* 陀螺仪数据 */
#define CAN_ID_AHRS_EULR 0x303 /* 欧拉角数据 */
#define CAN_ID_AHRS_QUAT 0x304 /* 四元数数据 */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */
/* USER STRUCT BEGIN */
AHRS_Accl_t accl;
AHRS_Gyro_t gyro;
AHRS_Eulr_t eulr;
AHRS_Quaternion_t quat;
/* USER STRUCT END */
/* Private function --------------------------------------------------------- */
/* Exported functions ------------------------------------------------------- */
void Task_imu(void *argument) {
(void)argument; /* 未使用argument消除警告 */
/* 计算任务运行到指定频率需要等待的tick数 */
const uint32_t delay_tick = osKernelGetTickFreq() / IMU_FREQ;
osDelay(IMU_INIT_DELAY); /* 延时一段时间再开启任务 */
uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */
/* USER CODE INIT BEGIN */
BSP_CAN_Init();
/* USER CODE INIT END */
while (1) {
tick += delay_tick; /* 计算下一个唤醒时刻 */
/* USER CODE BEGIN */
/* 获取加速度计数据并通过CAN发送 - 压缩为16位整数 */
if (osMessageQueueGet(task_runtime.msgq.imu.accl, &accl, NULL, 0) == osOK) {
BSP_CAN_StdDataFrame_t accl_frame;
accl_frame.id = CAN_ID_AHRS_ACCL;
accl_frame.dlc = 6; /* 3个int16_t每个2字节共6字节 */
/* 将float转换为int16_t (乘以100加速度一般在±20g范围内) */
int16_t x_int = (int16_t)(accl.x * 100);
int16_t y_int = (int16_t)(accl.y * 100);
int16_t z_int = (int16_t)(accl.z * 100);
memcpy(&accl_frame.data[0], &x_int, sizeof(int16_t));
memcpy(&accl_frame.data[2], &y_int, sizeof(int16_t));
memcpy(&accl_frame.data[4], &z_int, sizeof(int16_t));
BSP_CAN_TransmitStdDataFrame(BSP_CAN_1, &accl_frame);
}
/* 获取陀螺仪数据并通过CAN发送 - 压缩为16位整数 */
if (osMessageQueueGet(task_runtime.msgq.imu.gyro, &gyro, NULL, 0) == osOK) {
BSP_CAN_StdDataFrame_t gyro_frame;
gyro_frame.id = CAN_ID_AHRS_GYRO;
gyro_frame.dlc = 6; /* 3个int16_t每个2字节共6字节 */
/* 将float转换为int16_t (乘以10陀螺仪一般在±2000°/s范围内) */
int16_t x_int = (int16_t)(gyro.x * 10);
int16_t y_int = (int16_t)(gyro.y * 10);
int16_t z_int = (int16_t)(gyro.z * 10);
memcpy(&gyro_frame.data[0], &x_int, sizeof(int16_t));
memcpy(&gyro_frame.data[2], &y_int, sizeof(int16_t));
memcpy(&gyro_frame.data[4], &z_int, sizeof(int16_t));
BSP_CAN_TransmitStdDataFrame(BSP_CAN_1, &gyro_frame);
}
/* 获取欧拉角数据并通过CAN发送 - 压缩为16位整数 */
if (osMessageQueueGet(task_runtime.msgq.imu.eulr, &eulr, NULL, 0) == osOK) {
BSP_CAN_StdDataFrame_t eulr_frame;
eulr_frame.id = CAN_ID_AHRS_EULR;
eulr_frame.dlc = 6; /* 3个int16_t每个2字节共6字节 */
/* 将角度转换为int16_t (乘以100角度范围-180~180°精度0.01°) */
int16_t yaw_int = (int16_t)(eulr.yaw * 100);
int16_t pit_int = (int16_t)(eulr.pit * 100);
int16_t rol_int = (int16_t)(eulr.rol * 100);
memcpy(&eulr_frame.data[0], &yaw_int, sizeof(int16_t));
memcpy(&eulr_frame.data[2], &pit_int, sizeof(int16_t));
memcpy(&eulr_frame.data[4], &rol_int, sizeof(int16_t));
BSP_CAN_TransmitStdDataFrame(BSP_CAN_1, &eulr_frame);
}
/* 获取四元数数据并通过CAN发送 - 压缩为16位整数 */
if (osMessageQueueGet(task_runtime.msgq.imu.quat, &quat, NULL, 0) == osOK) {
BSP_CAN_StdDataFrame_t quat_frame;
quat_frame.id = CAN_ID_AHRS_QUAT;
quat_frame.dlc = 8; /* 4个int16_t每个2字节共8字节 */
/* 将四元数转换为int16_t (乘以32000四元数范围-1~1充分利用int16_t范围) */
int16_t q0_int = (int16_t)(quat.q0 * 32000);
int16_t q1_int = (int16_t)(quat.q1 * 32000);
int16_t q2_int = (int16_t)(quat.q2 * 32000);
int16_t q3_int = (int16_t)(quat.q3 * 32000);
memcpy(&quat_frame.data[0], &q0_int, sizeof(int16_t));
memcpy(&quat_frame.data[2], &q1_int, sizeof(int16_t));
memcpy(&quat_frame.data[4], &q2_int, sizeof(int16_t));
memcpy(&quat_frame.data[6], &q3_int, sizeof(int16_t));
BSP_CAN_TransmitStdDataFrame(BSP_CAN_1, &quat_frame);
}
/* USER CODE END */
osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */
}
}