rm_balance/User/task/rc.c

97 lines
3.1 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.

/*
rc Task
*/
/* Includes ----------------------------------------------------------------- */
#include "task/user_task.h"
/* USER INCLUDE BEGIN */
#include "device/dr16.h"
#include "device/mrobot.h"
#include "stm32h7xx.h"
/* USER INCLUDE END */
/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */
/* USER STRUCT BEGIN */
DR16_t dr16;
static DR16_SwitchPos_t last_sw_l = DR16_SW_ERR; /* 记录左拨杆上一次状态 */
extern bool reset;
/* USER STRUCT END */
/* Private function --------------------------------------------------------- */
/* USER PRIVATE CODE BEGIN */
/**
* @brief DR16数据打印回调
*/
static int print_dr16(const void *data, char *buf, size_t size) {
const DR16_t *dr16 = (const DR16_t *)data;
return MRobot_Snprintf(buf, size,
" Status : %s\r\n"
" ChLX : %.3f ChLY : %.3f\r\n"
" ChRX : %.3f ChRY : %.3f\r\n"
" ChRes : %.3f\r\n"
" SwL : %d SwR : %d\r\n"
" Mouse : X=%d Y=%d Z=%d (L:%d R:%d)\r\n"
" Keys : 0x%04X\r\n",
dr16->header.online ? "Online" : "Offline",
dr16->data.ch_l_x, dr16->data.ch_l_y,
dr16->data.ch_r_x, dr16->data.ch_r_y,
dr16->data.ch_res,
dr16->data.sw_l, dr16->data.sw_r,
dr16->data.mouse.x, dr16->data.mouse.y, dr16->data.mouse.z,
dr16->data.mouse.l_click, dr16->data.mouse.r_click,
dr16->data.keyboard.value);
}
/* USER PRIVATE CODE END */
/* Exported functions ------------------------------------------------------- */
void Task_rc(void *argument) {
(void)argument; /* 未使用argument消除警告 */
/* 计算任务运行到指定频率需要等待的tick数 */
const uint32_t delay_tick = osKernelGetTickFreq() / RC_FREQ;
osDelay(RC_INIT_DELAY); /* 延时一段时间再开启任务 */
uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */
/* USER CODE INIT BEGIN */
DR16_Init(&dr16);
MRobot_RegisterDevice("dr16", &dr16, print_dr16);
/* USER CODE INIT END */
while (1) {
tick += delay_tick; /* 计算下一个唤醒时刻 */
/* USER CODE BEGIN */
DR16_StartDmaRecv(&dr16);
if (DR16_WaitDmaCplt(20)) {
DR16_ParseData(&dr16);
} else {
DR16_Offline(&dr16);
}
/* 将 DR16 原始数据推入消息队列cmd 模块负责解析 */
osMessageQueueReset(task_runtime.msgq.cmd.rc);
osMessageQueuePut(task_runtime.msgq.cmd.rc, &dr16, 0, 0);
/* 检测左拨杆切换到UP位置时触发软件复位 */
if (dr16.header.online) {
/* 拨杆从非UP状态切换到UP状态且复位功能已使能触发系统复位 */
if (
dr16.data.sw_l == DR16_SW_UP &&
last_sw_l != DR16_SW_UP) {
reset=!reset;
}
last_sw_l = dr16.data.sw_l; /* 更新拨杆状态 */
}
/* USER CODE END */
osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */
}
}