mirror of
https://gitee.com/dlmu-cone/bf_original_balance_chassis
synced 2026-07-24 03:27:45 +08:00
init commit
This commit is contained in:
116
modules/remote/remote_control.c
Normal file
116
modules/remote/remote_control.c
Normal file
@@ -0,0 +1,116 @@
|
||||
#include "remote_control.h"
|
||||
#include "string.h"
|
||||
#include "bsp_usart.h"
|
||||
|
||||
#define RC_CHANNAL_ERROR_VALUE 700
|
||||
#define REMOTE_CONTROL_FRAME_SIZE 18u
|
||||
#define RC_abs(x) (x)>0?(x):(-x)
|
||||
#define RC_USART_SERVICE_ID 1
|
||||
|
||||
//remote control data
|
||||
static RC_ctrl_t rc_ctrl;
|
||||
static usart_instance rc_usart_instance;
|
||||
|
||||
/**
|
||||
* @brief remote control protocol resolution
|
||||
* @param[in] sbus_buf: raw data point
|
||||
* @param[out] rc_ctrl: remote control data struct point
|
||||
* @retval none
|
||||
*/
|
||||
static void sbus_to_rc(volatile const uint8_t *sbus_buf, RC_ctrl_t *rc_ctrl);
|
||||
|
||||
/**
|
||||
* @brief protocol resolve callback
|
||||
* this func would be called when usart3 idle interrupt happens
|
||||
*
|
||||
*/
|
||||
static void ReceiveCallback()
|
||||
{
|
||||
sbus_to_rc(rc_usart_instance.recv_buff,&rc_ctrl);
|
||||
}
|
||||
|
||||
void RC_init(UART_HandleTypeDef* rc_usart_handle)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
const RC_ctrl_t *get_remote_control_point(void)
|
||||
{
|
||||
return &rc_ctrl;
|
||||
}
|
||||
|
||||
uint8_t RC_data_is_error(void)
|
||||
{
|
||||
if (RC_abs(rc_ctrl.rc.ch[0]) > RC_CHANNAL_ERROR_VALUE)
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
if (RC_abs(rc_ctrl.rc.ch[1]) > RC_CHANNAL_ERROR_VALUE)
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
if (RC_abs(rc_ctrl.rc.ch[2]) > RC_CHANNAL_ERROR_VALUE)
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
if (RC_abs(rc_ctrl.rc.ch[3]) > RC_CHANNAL_ERROR_VALUE)
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
if (rc_ctrl.rc.s[0] == 0)
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
if (rc_ctrl.rc.s[1] == 0)
|
||||
{
|
||||
goto error;
|
||||
}
|
||||
return 0;
|
||||
error:
|
||||
rc_ctrl.rc.ch[0] = 0;
|
||||
rc_ctrl.rc.ch[1] = 0;
|
||||
rc_ctrl.rc.ch[2] = 0;
|
||||
rc_ctrl.rc.ch[3] = 0;
|
||||
rc_ctrl.rc.ch[4] = 0;
|
||||
rc_ctrl.rc.s[0] = RC_SW_DOWN;
|
||||
rc_ctrl.rc.s[1] = RC_SW_DOWN;
|
||||
rc_ctrl.mouse.x = 0;
|
||||
rc_ctrl.mouse.y = 0;
|
||||
rc_ctrl.mouse.z = 0;
|
||||
rc_ctrl.mouse.press_l = 0;
|
||||
rc_ctrl.mouse.press_r = 0;
|
||||
rc_ctrl.key.v = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
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
|
||||
(sbus_buf[4] << 10)) &0x07ff;
|
||||
rc_ctrl->rc.ch[3] = ((sbus_buf[4] >> 1) | (sbus_buf[5] << 7)) & 0x07ff; //!< Channel 3
|
||||
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
|
||||
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
|
||||
rc_ctrl->rc.ch[4] = sbus_buf[16] | (sbus_buf[17] << 8); //NULL
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
99
modules/remote/remote_control.h
Normal file
99
modules/remote/remote_control.h
Normal file
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
****************************(C) COPYRIGHT 2016 DJI****************************
|
||||
* @file remote_control.c/h
|
||||
* @brief 遥控器处理,遥控器是通过类似SBUS的协议传输,利用DMA传输方式节约CPU
|
||||
* 资源,利用串口空闲中断来拉起处理函数,同时提供一些掉线重启DMA,串口
|
||||
* 的方式保证热插拔的稳定性。
|
||||
* @note
|
||||
* @history
|
||||
* Version Date Author Modification
|
||||
* V1.0.0 Dec-26-2018 RM 1. done
|
||||
* V1.0.0 Nov-11-2019 RM 1. support development board tpye c
|
||||
*
|
||||
@verbatim
|
||||
==============================================================================
|
||||
|
||||
==============================================================================
|
||||
@endverbatim
|
||||
****************************(C) COPYRIGHT 2016 DJI****************************
|
||||
*/
|
||||
#ifndef REMOTE_CONTROL_H
|
||||
#define REMOTE_CONTROL_H
|
||||
|
||||
#include "struct_typedef.h"
|
||||
#include "main.h"
|
||||
|
||||
#define RC_CH_VALUE_MIN ((uint16_t)364)
|
||||
#define RC_CH_VALUE_OFFSET ((uint16_t)1024)
|
||||
#define RC_CH_VALUE_MAX ((uint16_t)1684)
|
||||
/* ----------------------- RC Switch Definition----------------------------- */
|
||||
#define RC_SW_UP ((uint16_t)1)
|
||||
#define RC_SW_MID ((uint16_t)3)
|
||||
#define RC_SW_DOWN ((uint16_t)2)
|
||||
#define switch_is_down(s) (s == RC_SW_DOWN)
|
||||
#define switch_is_mid(s) (s == RC_SW_MID)
|
||||
#define switch_is_up(s) (s == RC_SW_UP)
|
||||
/* ----------------------- PC Key Definition-------------------------------- */
|
||||
#define KEY_PRESSED_OFFSET_W ((uint16_t)1 << 0)
|
||||
#define KEY_PRESSED_OFFSET_S ((uint16_t)1 << 1)
|
||||
#define KEY_PRESSED_OFFSET_A ((uint16_t)1 << 2)
|
||||
#define KEY_PRESSED_OFFSET_D ((uint16_t)1 << 3)
|
||||
#define KEY_PRESSED_OFFSET_SHIFT ((uint16_t)1 << 4)
|
||||
#define KEY_PRESSED_OFFSET_CTRL ((uint16_t)1 << 5)
|
||||
#define KEY_PRESSED_OFFSET_E ((uint16_t)1 << 7)
|
||||
#define KEY_PRESSED_OFFSET_Q ((uint16_t)1 << 6)
|
||||
#define KEY_PRESSED_OFFSET_R ((uint16_t)1 << 8)
|
||||
#define KEY_PRESSED_OFFSET_F ((uint16_t)1 << 9)
|
||||
#define KEY_PRESSED_OFFSET_G ((uint16_t)1 << 10)
|
||||
#define KEY_PRESSED_OFFSET_Z ((uint16_t)1 << 11)
|
||||
#define KEY_PRESSED_OFFSET_X ((uint16_t)1 << 12)
|
||||
#define KEY_PRESSED_OFFSET_C ((uint16_t)1 << 13)
|
||||
#define KEY_PRESSED_OFFSET_V ((uint16_t)1 << 14)
|
||||
#define KEY_PRESSED_OFFSET_B ((uint16_t)1 << 15)
|
||||
/* ----------------------- Data Struct ------------------------------------- */
|
||||
typedef __packed struct
|
||||
{
|
||||
__packed struct
|
||||
{
|
||||
int16_t ch[5];
|
||||
char s[2];
|
||||
} rc;
|
||||
__packed struct
|
||||
{
|
||||
int16_t x;
|
||||
int16_t y;
|
||||
int16_t z;
|
||||
uint8_t press_l;
|
||||
uint8_t press_r;
|
||||
} mouse;
|
||||
__packed struct
|
||||
{
|
||||
uint16_t v;
|
||||
} key;
|
||||
|
||||
} RC_ctrl_t;
|
||||
/* ----------------------- Internal Data ----------------------------------- */
|
||||
|
||||
/**
|
||||
* @brief register remote control into usart_serive
|
||||
*
|
||||
* @attention remember to assign the correct handler
|
||||
*
|
||||
*/
|
||||
void RC_init(UART_HandleTypeDef* rc_usart_handle);
|
||||
|
||||
/**
|
||||
* @brief Get the remote control point object,then you can access data
|
||||
*
|
||||
* @return const RC_ctrl_t*
|
||||
*/
|
||||
extern const RC_ctrl_t *get_remote_control_point(void);
|
||||
|
||||
/**
|
||||
* @brief check whether there is an error in rc data
|
||||
*
|
||||
* @return uint8_t
|
||||
*/
|
||||
extern uint8_t RC_data_is_error(void);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user