2022-11-03 20:38:55 +08:00
|
|
|
/**
|
|
|
|
* @file master_process.c
|
|
|
|
* @author neozng
|
|
|
|
* @brief module for recv&send vision data
|
|
|
|
* @version beta
|
|
|
|
* @date 2022-11-03
|
2022-11-05 21:59:03 +08:00
|
|
|
* @todo 增加对串口调试助手协议的支持,包括vofa和serial debug
|
2022-11-29 16:33:34 +08:00
|
|
|
* @copyright Copyright (c) 2022
|
2022-11-03 20:38:55 +08:00
|
|
|
*
|
|
|
|
*/
|
2022-11-29 16:33:34 +08:00
|
|
|
#include "master_process.h"
|
|
|
|
#include "seasky_protocol.h"
|
2023-06-22 21:52:46 +08:00
|
|
|
#include "daemon.h"
|
2023-03-18 20:36:21 +08:00
|
|
|
#include "bsp_log.h"
|
2023-05-04 21:38:35 +08:00
|
|
|
#include "robot_def.h"
|
2022-11-03 20:38:55 +08:00
|
|
|
|
2022-11-29 23:11:37 +08:00
|
|
|
static Vision_Recv_s recv_data;
|
2023-04-29 22:33:16 +08:00
|
|
|
static Vision_Send_s send_data;
|
2023-06-22 21:52:46 +08:00
|
|
|
static DaemonInstance *vision_daemon_instance;
|
2023-05-04 21:38:35 +08:00
|
|
|
|
|
|
|
void VisionSetFlag(Enemy_Color_e enemy_color, Work_Mode_e work_mode, Bullet_Speed_e bullet_speed)
|
|
|
|
{
|
|
|
|
send_data.enemy_color = enemy_color;
|
|
|
|
send_data.work_mode = work_mode;
|
|
|
|
send_data.bullet_speed = bullet_speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisionSetAltitude(float yaw, float pitch, float roll)
|
|
|
|
{
|
|
|
|
send_data.yaw = yaw;
|
|
|
|
send_data.pitch = pitch;
|
|
|
|
send_data.roll = roll;
|
|
|
|
}
|
|
|
|
|
2023-06-22 21:52:46 +08:00
|
|
|
/**
|
|
|
|
* @brief 离线回调函数,将在daemon.c中被daemon task调用
|
|
|
|
* @attention 由于HAL库的设计问题,串口开启DMA接收之后同时发送有概率出现__HAL_LOCK()导致的死锁,使得无法
|
|
|
|
* 进入接收中断.通过daemon判断数据更新,重新调用服务启动函数以解决此问题.
|
|
|
|
*
|
|
|
|
* @param id vision_usart_instance的地址,此处没用.
|
|
|
|
*/
|
|
|
|
static void VisionOfflineCallback(void *id)
|
|
|
|
{
|
|
|
|
#ifdef VISION_USE_UART
|
|
|
|
USARTServiceInit(vision_usart_instance);
|
|
|
|
#endif // !VISION_USE_UART
|
|
|
|
LOGWARNING("[vision] vision offline, restart communication.");
|
|
|
|
}
|
|
|
|
|
2023-05-04 21:38:35 +08:00
|
|
|
#ifdef VISION_USE_UART
|
|
|
|
|
|
|
|
#include "bsp_usart.h"
|
|
|
|
|
2022-12-05 21:01:26 +08:00
|
|
|
static USARTInstance *vision_usart_instance;
|
2022-11-03 20:38:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief 接收解包回调函数,将在bsp_usart.c中被usart rx callback调用
|
2022-11-12 18:03:18 +08:00
|
|
|
* @todo 1.提高可读性,将get_protocol_info的第四个参数增加一个float类型buffer
|
2022-11-03 20:38:55 +08:00
|
|
|
* 2.添加标志位解码
|
|
|
|
*/
|
|
|
|
static void DecodeVision()
|
|
|
|
{
|
2023-03-23 18:22:24 +08:00
|
|
|
uint16_t flag_register;
|
2023-03-18 20:36:21 +08:00
|
|
|
DaemonReload(vision_daemon_instance); // 喂狗
|
2022-12-05 21:01:26 +08:00
|
|
|
get_protocol_info(vision_usart_instance->recv_buff, &flag_register, (uint8_t *)&recv_data.pitch);
|
2022-11-03 20:38:55 +08:00
|
|
|
// TODO: code to resolve flag_register;
|
2023-03-18 20:36:21 +08:00
|
|
|
}
|
|
|
|
|
2022-11-29 16:33:34 +08:00
|
|
|
Vision_Recv_s *VisionInit(UART_HandleTypeDef *_handle)
|
2022-11-03 20:38:55 +08:00
|
|
|
{
|
2022-11-28 17:54:07 +08:00
|
|
|
USART_Init_Config_s conf;
|
|
|
|
conf.module_callback = DecodeVision;
|
|
|
|
conf.recv_buff_size = VISION_RECV_SIZE;
|
2022-11-29 16:33:34 +08:00
|
|
|
conf.usart_handle = _handle;
|
|
|
|
vision_usart_instance = USARTRegister(&conf);
|
2023-03-18 20:36:21 +08:00
|
|
|
|
|
|
|
// 为master process注册daemon,用于判断视觉通信是否离线
|
|
|
|
Daemon_Init_Config_s daemon_conf = {
|
|
|
|
.callback = VisionOfflineCallback, // 离线时调用的回调函数,会重启串口接收
|
|
|
|
.owner_id = vision_usart_instance,
|
|
|
|
.reload_count = 10,
|
|
|
|
};
|
|
|
|
vision_daemon_instance = DaemonRegister(&daemon_conf);
|
|
|
|
|
2022-11-05 21:59:03 +08:00
|
|
|
return &recv_data;
|
2022-11-03 20:38:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief 发送函数
|
2023-05-04 21:38:35 +08:00
|
|
|
*
|
2022-11-03 20:38:55 +08:00
|
|
|
* @param send 待发送数据
|
2023-05-04 21:38:35 +08:00
|
|
|
*
|
2022-11-03 20:38:55 +08:00
|
|
|
*/
|
2023-04-29 22:33:16 +08:00
|
|
|
void VisionSend()
|
2022-11-03 20:38:55 +08:00
|
|
|
{
|
2023-04-20 09:54:04 +08:00
|
|
|
// buff和txlen必须为static,才能保证在函数退出后不被释放,使得DMA正确完成发送
|
|
|
|
// 析构后的陷阱需要特别注意!
|
|
|
|
static uint16_t flag_register;
|
2023-04-29 22:33:16 +08:00
|
|
|
static uint8_t send_buff[VISION_SEND_SIZE];
|
2023-04-20 09:54:04 +08:00
|
|
|
static uint16_t tx_len;
|
2022-11-03 20:38:55 +08:00
|
|
|
// TODO: code to set flag_register
|
2023-04-20 09:54:04 +08:00
|
|
|
flag_register = 30 << 8 | 0b00000001;
|
2023-01-01 17:32:22 +08:00
|
|
|
// 将数据转化为seasky协议的数据包
|
2023-04-29 22:33:16 +08:00
|
|
|
get_protocol_send_data(0x02, flag_register, &send_data.yaw, 3, send_buff, &tx_len);
|
2023-03-18 20:36:21 +08:00
|
|
|
USARTSend(vision_usart_instance, send_buff, tx_len, USART_TRANSFER_DMA); // 和视觉通信使用IT,防止和接收使用的DMA冲突
|
2023-03-18 14:50:50 +08:00
|
|
|
// 此处为HAL设计的缺陷,DMASTOP会停止发送和接收,导致再也无法进入接收中断.
|
|
|
|
// 也可在发送完成中断中重新启动DMA接收,但较为复杂.因此,此处使用IT发送.
|
2023-03-18 20:36:21 +08:00
|
|
|
// 若使用了daemon,则也可以使用DMA发送.
|
2023-04-29 22:33:16 +08:00
|
|
|
}
|
|
|
|
|
2023-05-04 21:38:35 +08:00
|
|
|
#endif // VISION_USE_UART
|
|
|
|
|
|
|
|
#ifdef VISION_USE_VCP
|
|
|
|
|
|
|
|
#include "bsp_usb.h"
|
|
|
|
static uint8_t *vis_recv_buff;
|
|
|
|
|
|
|
|
static void DecodeVision(uint16_t recv_len)
|
2023-04-29 22:33:16 +08:00
|
|
|
{
|
2023-05-04 21:38:35 +08:00
|
|
|
uint16_t flag_register;
|
|
|
|
get_protocol_info(vis_recv_buff, &flag_register, (uint8_t *)&recv_data.pitch);
|
|
|
|
// TODO: code to resolve flag_register;
|
2023-04-29 22:33:16 +08:00
|
|
|
}
|
|
|
|
|
2023-05-04 21:38:35 +08:00
|
|
|
/* 视觉通信初始化 */
|
|
|
|
Vision_Recv_s *VisionInit(UART_HandleTypeDef *_handle)
|
2023-04-29 22:33:16 +08:00
|
|
|
{
|
2023-05-04 21:38:35 +08:00
|
|
|
UNUSED(_handle); // 仅为了消除警告
|
2023-06-22 21:52:46 +08:00
|
|
|
USB_Init_Config_s conf = {.rx_cbk = DecodeVision};
|
2023-05-04 21:38:35 +08:00
|
|
|
vis_recv_buff = USBInit(conf);
|
2023-06-22 21:52:46 +08:00
|
|
|
|
|
|
|
// 为master process注册daemon,用于判断视觉通信是否离线
|
|
|
|
Daemon_Init_Config_s daemon_conf = {
|
|
|
|
.callback = VisionOfflineCallback, // 离线时调用的回调函数,会重启串口接收
|
|
|
|
.owner_id = NULL,
|
|
|
|
.reload_count = 5, // 50ms
|
|
|
|
};
|
|
|
|
vision_daemon_instance = DaemonRegister(&daemon_conf);
|
|
|
|
|
2023-05-04 21:38:35 +08:00
|
|
|
return &recv_data;
|
2023-04-29 22:33:16 +08:00
|
|
|
}
|
2023-05-04 21:38:35 +08:00
|
|
|
|
|
|
|
void VisionSend()
|
|
|
|
{
|
|
|
|
static uint16_t flag_register;
|
|
|
|
static uint8_t send_buff[VISION_SEND_SIZE];
|
|
|
|
static uint16_t tx_len;
|
|
|
|
// TODO: code to set flag_register
|
|
|
|
flag_register = 30 << 8 | 0b00000001;
|
|
|
|
// 将数据转化为seasky协议的数据包
|
|
|
|
get_protocol_send_data(0x02, flag_register, &send_data.yaw, 3, send_buff, &tx_len);
|
|
|
|
USBTransmit(send_buff, tx_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // VISION_USE_VCP
|