92 lines
3.1 KiB
C
92 lines
3.1 KiB
C
/*
|
||
ctrl_gimbal Task
|
||
|
||
*/
|
||
|
||
/* Includes ----------------------------------------------------------------- */
|
||
#include "task/user_task.h"
|
||
/* USER INCLUDE BEGIN */
|
||
#include "component/ahrs.h"
|
||
#include "device/mrobot.h"
|
||
#include "module/gimbal.h"
|
||
#include "module/config.h"
|
||
#include "bsp/can.h"
|
||
/* USER INCLUDE END */
|
||
|
||
/* Private typedef ---------------------------------------------------------- */
|
||
/* Private define ----------------------------------------------------------- */
|
||
/* Private macro ------------------------------------------------------------ */
|
||
/* Private variables -------------------------------------------------------- */
|
||
/* USER STRUCT BEGIN */
|
||
Gimbal_t gimbal;
|
||
Gimbal_CMD_t gimbal_cmd;
|
||
Gimbal_AI_t gimbal_ai;
|
||
// BSP_FDCAN_StdDataFrame_t can_frame;
|
||
/* USER STRUCT END */
|
||
|
||
/* Private function --------------------------------------------------------- */
|
||
/* USER PRIVATE CODE BEGIN */
|
||
|
||
/**
|
||
* @brief 云台数据打印回调
|
||
*/
|
||
static int print_gimbal(const void *data, char *buf, size_t size) {
|
||
const Gimbal_t *gimbal = (const Gimbal_t *)data;
|
||
return MRobot_Snprintf(buf, size,
|
||
" Mode : %d\r\n"
|
||
" IMU : Roll=%.2f Pitch=%.2f Yaw=%.2f (deg)\r\n"
|
||
" GyroX : %.3f GyroY : %.3f GyroZ : %.3f (rad/s)\r\n"
|
||
" Motor : Yaw=%.1f(rpm) Pit=%.1f(rpm)\r\n"
|
||
" Output : Yaw=%.1f Pit=%.1f\r\n",
|
||
gimbal->mode,
|
||
gimbal->imu.data.eulr.rol, gimbal->imu.data.eulr.pit, gimbal->imu.data.eulr.yaw,
|
||
gimbal->imu.data.gyro.x, gimbal->imu.data.gyro.y, gimbal->imu.data.gyro.z,
|
||
gimbal->feedback.motor.yaw.rotor_speed, gimbal->feedback.motor.pit.rotor_speed,
|
||
gimbal->out.yaw, gimbal->out.pit);
|
||
return 0;
|
||
}
|
||
|
||
/* USER PRIVATE CODE END */
|
||
/* Exported functions ------------------------------------------------------- */
|
||
void Task_ctrl_gimbal(void *argument) {
|
||
(void)argument; /* 未使用argument,消除警告 */
|
||
|
||
|
||
/* 计算任务运行到指定频率需要等待的tick数 */
|
||
const uint32_t delay_tick = osKernelGetTickFreq() / CTRL_GIMBAL_FREQ;
|
||
|
||
osDelay(CTRL_GIMBAL_INIT_DELAY); /* 延时一段时间再开启任务 */
|
||
|
||
uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */
|
||
/* USER CODE INIT BEGIN */
|
||
|
||
Gimbal_Init(&gimbal, &Config_GetRobotParam()->gimbal_param, CTRL_GIMBAL_FREQ);
|
||
MRobot_RegisterDevice("gimbal", &gimbal, print_gimbal);
|
||
/* USER CODE INIT END */
|
||
|
||
while (1) {
|
||
tick += delay_tick; /* 计算下一个唤醒时刻 */
|
||
/* USER CODE BEGIN */
|
||
|
||
// can_frame.id = 0x200;
|
||
// can_frame.dlc = 8;
|
||
// BSP_CAN_TransmitStdDataFrame(BSP_CAN_2, &can_frame);
|
||
|
||
Gimbal_UpdateIMU(&gimbal);
|
||
osMessageQueueGet(task_runtime.msgq.gimbal.cmd, &gimbal_cmd, NULL, 0);
|
||
osMessageQueueGet(task_runtime.msgq.gimbal.ai_cmd, &gimbal_ai, NULL, 0);
|
||
|
||
Gimbal_UpdateFeedback(&gimbal);
|
||
|
||
osMessageQueueReset(task_runtime.msgq.chassis.yaw); // 重置消息队列,防止阻塞
|
||
osMessageQueuePut(task_runtime.msgq.chassis.yaw, &gimbal.feedback.motor.yaw, 0, 0);
|
||
|
||
Gimbal_Control(&gimbal, &gimbal_cmd, &gimbal_ai);
|
||
|
||
Gimbal_Output(&gimbal);
|
||
|
||
/* USER CODE END */
|
||
osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */
|
||
}
|
||
|
||
} |