chassis/User/task/chassis_control.c
2025-09-30 22:21:26 +08:00

84 lines
2.9 KiB
C
Raw Permalink 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.

/*
chassis_control Task
一个底盘测试任务
*/
/* Includes ----------------------------------------------------------------- */
#include "task/user_task.h"
/* USER INCLUDE BEGIN */
#include "cmsis_os2.h"
#include "module/chassis.h"
#include "bsp/time.h"
//#include "component\cmd.h"
#include "device/dr16.h"
#include "component\ahrs.h"
/* USER INCLUDE END */
/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */
/* USER STRUCT BEGIN */
static Chassis_t chassis; // 底盘控制对象
static ChassisCmd_t chassis_cmd; // 底盘控制指令
static Chassis_CAN_t chassis_can; // 底盘 CAN 通信对象
static AHRS_Eulr_t mech_zero; // 底盘初始机械零点
DR16_t dr16;
#define DR16_CH_VALUE_MID (1024u)
/* USER STRUCT END */
/* Private function --------------------------------------------------------- */
/* Exported functions ------------------------------------------------------- */
void Task_chassis_control(void *argument) {
(void)argument; /* 未使用argument消除警告 */
/* 计算任务运行到指定频率需要等待的tick数 */
const uint32_t delay_tick = osKernelGetTickFreq() / CHASSIS_CONTROL_FREQ;
osDelay(CHASSIS_CONTROL_INIT_DELAY); /* 延时一段时间再开启任务 */
uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */
/* USER CODE INIT BEGIN */
DR16_Init(&dr16);
//DR16_StartDmaRecv(&dr16);
// const Chassis_Params_t chassis_param;
// Chassis_Init(&chassis, &chassis_param, &mech_zero, (float)CHASSIS_CONTROL_FREQ);
/* USER CODE INIT END */
while (1) {
tick += delay_tick; /* 计算下一个唤醒时刻 */
/* USER CODE BEGIN */
DR16_StartDmaRecv(&dr16);
if (DR16_WaitDmaCplt(20)) // timeout = 20ms
{
Chassis_UpdateFeedback(&chassis, &chassis_can);
// 构造底盘指令
chassis_cmd.mode = CHASSIS_MODE_FOLLOW_GIMBAL;
// 左摇杆控制前后、左右
chassis_cmd.ctrl_vec.vx = (dr16.data.ch_l_y - DR16_CH_VALUE_MID) * 0.002f; // 前后
chassis_cmd.ctrl_vec.vy = (dr16.data.ch_l_x - DR16_CH_VALUE_MID) * 0.002f; // 左右
// 右摇杆控制旋转
chassis_cmd.ctrl_vec.wz = (dr16.data.ch_r_x - DR16_CH_VALUE_MID) * 0.001f; // 旋转
// 控制计算
Chassis_Control(&chassis, &chassis_cmd, osKernelGetTickCount());
// 输出电流到电机
Chassis_DumpOutput(&chassis, &chassis_can);
}
else {
DR16_Offline(&dr16);
}
// DR16_StartDmaRecv(&dr16);
// if (DR16_WaitDmaCplt(20)) {
// DR16_ParseData(&dr16);
// } else {
// DR16_Offline(&dr16);
// }
/* USER CODE END */
osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */
}
}