sentry_chassis_hzz/modules/remote/remote_control.c

70 lines
2.7 KiB
C
Raw Normal View History

2022-10-20 17:13:02 +08:00
#include "remote_control.h"
#include "string.h"
#include "bsp_usart.h"
2022-11-01 22:32:15 +08:00
#define REMOTE_CONTROL_FRAME_SIZE 18u // 遥控器接收的buffer大小
2022-10-20 17:13:02 +08:00
2022-11-01 22:32:15 +08:00
// 遥控器数据
2022-10-20 17:13:02 +08:00
static RC_ctrl_t rc_ctrl;
2022-11-01 22:32:15 +08:00
// 遥控器拥有的串口实例
2022-10-20 17:13:02 +08:00
static usart_instance rc_usart_instance;
/**
2022-11-01 22:32:15 +08:00
* @brief remote control protocol resolution
* @param[in] sbus_buf: raw data point
* @param[out] rc_ctrl: remote control data struct point
* @retval none
2022-10-20 17:13:02 +08:00
*/
static void sbus_to_rc(volatile const uint8_t *sbus_buf, RC_ctrl_t *rc_ctrl)
{
if (sbus_buf == NULL || rc_ctrl == NULL)
{
return;
}
rc_ctrl->rc.ch[0] = (sbus_buf[0] | (sbus_buf[1] << 8)) & 0x07ff; //!< Channel 0
rc_ctrl->rc.ch[1] = ((sbus_buf[1] >> 3) | (sbus_buf[2] << 5)) & 0x07ff; //!< Channel 1
rc_ctrl->rc.ch[2] = ((sbus_buf[2] >> 6) | (sbus_buf[3] << 2) | //!< Channel 2
2022-11-01 22:32:15 +08:00
(sbus_buf[4] << 10)) &
0x07ff;
2022-10-20 17:13:02 +08:00
rc_ctrl->rc.ch[3] = ((sbus_buf[4] >> 1) | (sbus_buf[5] << 7)) & 0x07ff; //!< Channel 3
2022-11-01 22:32:15 +08:00
rc_ctrl->rc.s[0] = ((sbus_buf[5] >> 4) & 0x0003); //!< Switch left
rc_ctrl->rc.s[1] = ((sbus_buf[5] >> 4) & 0x000C) >> 2; //!< Switch right
2022-10-20 17:13:02 +08:00
rc_ctrl->mouse.x = sbus_buf[6] | (sbus_buf[7] << 8); //!< Mouse X axis
rc_ctrl->mouse.y = sbus_buf[8] | (sbus_buf[9] << 8); //!< Mouse Y axis
rc_ctrl->mouse.z = sbus_buf[10] | (sbus_buf[11] << 8); //!< Mouse Z axis
rc_ctrl->mouse.press_l = sbus_buf[12]; //!< Mouse Left Is Press ?
rc_ctrl->mouse.press_r = sbus_buf[13]; //!< Mouse Right Is Press ?
rc_ctrl->key.v = sbus_buf[14] | (sbus_buf[15] << 8); //!< KeyBoard value
2022-11-01 22:32:15 +08:00
rc_ctrl->rc.ch[4] = sbus_buf[16] | (sbus_buf[17] << 8); // NULL
2022-10-20 17:13:02 +08:00
rc_ctrl->rc.ch[0] -= RC_CH_VALUE_OFFSET;
rc_ctrl->rc.ch[1] -= RC_CH_VALUE_OFFSET;
rc_ctrl->rc.ch[2] -= RC_CH_VALUE_OFFSET;
rc_ctrl->rc.ch[3] -= RC_CH_VALUE_OFFSET;
rc_ctrl->rc.ch[4] -= RC_CH_VALUE_OFFSET;
}
2022-11-01 22:32:15 +08:00
/**
* @brief protocol resolve callback
* this func would be called when usart3 idle interrupt happens
* sbus_to_rc的简单封装
*
*/
static void ReceiveCallback()
{
sbus_to_rc(rc_usart_instance.recv_buff, &rc_ctrl);
}
2022-11-09 19:53:23 +08:00
RC_ctrl_t *RC_init(UART_HandleTypeDef *rc_usart_handle)
2022-11-01 22:32:15 +08:00
{
rc_usart_instance.module_callback = ReceiveCallback;
rc_usart_instance.usart_handle = rc_usart_handle;
rc_usart_instance.recv_buff_size = REMOTE_CONTROL_FRAME_SIZE;
USARTRegister(&rc_usart_instance);
return &rc_ctrl;
2022-11-01 22:32:15 +08:00
}
2022-11-09 19:53:23 +08:00
const RC_ctrl_t *get_remote_control_point(void)
2022-11-01 22:32:15 +08:00
{
2022-11-09 19:53:23 +08:00
return &rc_ctrl;
2022-11-01 22:32:15 +08:00
}