sentry_chassis_hzz/modules/master_machine/master_process.c

156 lines
4.8 KiB
C
Raw Permalink Normal View History

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
* @todo ,vofa和serial debug
* @copyright Copyright (c) 2022
2022-11-03 20:38:55 +08:00
*
*/
#include "master_process.h"
#include "seasky_protocol.h"
#include "daemon.h"
#include "bsp_log.h"
#include "robot_def.h"
2022-11-03 20:38:55 +08:00
static Vision_Recv_s recv_data;
static Vision_Send_s send_data;
static DaemonInstance *vision_daemon_instance;
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;
}
/**
* @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.");
}
#ifdef VISION_USE_UART
#include "bsp_usart.h"
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()
{
uint16_t flag_register;
DaemonReload(vision_daemon_instance); // 喂狗
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;
}
Vision_Recv_s *VisionInit(UART_HandleTypeDef *_handle)
2022-11-03 20:38:55 +08:00
{
USART_Init_Config_s conf;
conf.module_callback = DecodeVision;
conf.recv_buff_size = VISION_RECV_SIZE;
conf.usart_handle = _handle;
vision_usart_instance = USARTRegister(&conf);
// 为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);
return &recv_data;
2022-11-03 20:38:55 +08:00
}
/**
* @brief
*
2022-11-03 20:38:55 +08:00
* @param send
*
2022-11-03 20:38:55 +08:00
*/
void VisionSend()
2022-11-03 20:38:55 +08:00
{
// buff和txlen必须为static,才能保证在函数退出后不被释放,使得DMA正确完成发送
// 析构后的陷阱需要特别注意!
static uint16_t flag_register;
static uint8_t send_buff[VISION_SEND_SIZE];
static uint16_t tx_len;
2022-11-03 20:38:55 +08:00
// TODO: code to set flag_register
flag_register = 30 << 8 | 0b00000001;
2023-01-01 17:32:22 +08:00
// 将数据转化为seasky协议的数据包
get_protocol_send_data(0x02, flag_register, &send_data.yaw, 3, send_buff, &tx_len);
USARTSend(vision_usart_instance, send_buff, tx_len, USART_TRANSFER_DMA); // 和视觉通信使用IT,防止和接收使用的DMA冲突
// 此处为HAL设计的缺陷,DMASTOP会停止发送和接收,导致再也无法进入接收中断.
// 也可在发送完成中断中重新启动DMA接收,但较为复杂.因此,此处使用IT发送.
// 若使用了daemon,则也可以使用DMA发送.
}
#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)
{
uint16_t flag_register;
get_protocol_info(vis_recv_buff, &flag_register, (uint8_t *)&recv_data.pitch);
// TODO: code to resolve flag_register;
}
/* 视觉通信初始化 */
Vision_Recv_s *VisionInit(UART_HandleTypeDef *_handle)
{
UNUSED(_handle); // 仅为了消除警告
USB_Init_Config_s conf = {.rx_cbk = DecodeVision};
vis_recv_buff = USBInit(conf);
// 为master process注册daemon,用于判断视觉通信是否离线
Daemon_Init_Config_s daemon_conf = {
.callback = VisionOfflineCallback, // 离线时调用的回调函数,会重启串口接收
.owner_id = NULL,
.reload_count = 5, // 50ms
};
vision_daemon_instance = DaemonRegister(&daemon_conf);
return &recv_data;
}
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