feat(ui): 新增裁判系统自定义UI(0x0301)并打通键鼠→底盘链路

键鼠控制(此前 WASD 无反应):
- 云台 robot_cmd_task:0x0304 与 USART3 帧内键鼠合并为共享视图 keyboard_merged(0x0304 优先);
  键鼠模式由帧内键鼠动作进入,动云台左摇杆退出
- c_comm_task/c_comm:键鼠在控时底盘摇杆由 WASD+鼠标合成,改读共享视图(此前只认 0x0304)
- dr16/调试输出:新增帧内键鼠原始字节诊断(key_lo/key_hi/mouse_*/s1/frames)

自定义UI(底盘板经裁判系统 USART6 绘制到选手端):
- 新增 底盘/modules/comms/referee_ui.c|h:15 字节图形组包(带编译期断言),
  v5 卡片式 HUD = 俯仰梯(pitch) + 罗盘刻度带(底盘朝向) + 预估底盘功率 + 超级电容
  + 摩擦轮 L/R 转速 + 小陀螺状态,yaw 在刻度带右侧;5Hz 刷新,约 50 图元/8 包
- referee.c 新增 referee_send_frame()(cmd 0x0301 机器人交互数据,CRC8+CRC16,USART6 TX DMA)
- 新增 CToC 0x1B9:云台把 pitch 与摩擦轮转速带给底盘(底盘本地没有这两个量)
- 版式避开官方保留区:顶部比分条/左下小地图/右下战车模块/中部两块官方保留

验证:云台 Code=99540 RO=1764 RW=420 ZI=62572;底盘 Code=99452 RO=1928 RW=416 ZI=62584;均 0 错误 0 警告。
未做实机验证(当前无板/无图传);UI 协议常量(UI_RECEIVER_ID/UI_SEND_HZ)与版式坐标集中在 referee_ui.h 宏区,便于按协议 PDF 一处校正。
This commit is contained in:
lyf
2026-10-16 04:24:32 +08:00
parent 370d4519b5
commit 267a659242
21 changed files with 867 additions and 58 deletions
+54 -7
View File
@@ -1,5 +1,5 @@
/* =============================================================================
* debug_task.c - USB 虚拟串口(CDC)姿态输出(欧拉角 + 姿态链路诊断)
* debug_task.c - USB 虚拟串口(CDC)姿态输出(欧拉角 + 姿态链路诊断 + 控制量/模式)
* 每 DEBUG_OUT_PERIOD_MS 输出一行。单位: 角度 ° / 加速度 m/s² / 角速度 rad/s
*
* ---- 欧拉角(QuaternionEKF 解算结果,见 modules/ins) ----
@@ -25,7 +25,20 @@
* se bmi088_spi_errors:SPI/DMA 出错次数;st=bmi088_spi_state()
* (1=READY,非 1 说明 SPI 被卡住,数据路会因此停摆)
*
* 开关: DEBUG_ATT_HEALTH=0 只输出三个欧拉角;DEBUG_ATT_DIAG=0 去掉诊断位。
* ---- 控制量 / 控制模式(DEBUG_CMD_OUT=1 时追加) ----
* mode 底盘模式 0=OFF 1=MANUAL 2=FOLLOW 3=SPIN_SLOW 4=SPIN_FAST
* (由本板依据收到的 s2/spin 推出;模式逻辑见 robot_cmd_task.c)
* online 1=板间通讯(CToC)在线 —— 本板唯一的"控制源在线"判据
* vx/vy 底盘平移速度指令(世界系映射值) wz [已废弃,恒 0]
* spin **收到的**小陀螺使能(0x189 bit2,云台侧做 Ctrl 沿检测)
* s2 **收到的**遥控 S2 档位(小陀螺为真时它被覆盖)
* reset 跟随朝向重锁请求(跟随模式重连时置位)
* 注意:本板拿不到"遥控器还是键鼠"这一信息——0x149/0x189 不带该位,
* robot_cmd_task 里 cmd_robot.source 恒被置 0,故这里不输出假字段。
* 与姿态同源(cmd_robot/ctoc_remote),一起看才能判断"模式对不对、量给没给"。
*
* 开关: DEBUG_ATT_HEALTH=0 只输出三个欧拉角;DEBUG_ATT_DIAG=0 去掉诊断位;
* DEBUG_CMD_OUT=1 追加控制量/控制模式。
* ========================================================================== */
#include "debug_task.h"
#include "cmsis_os.h"
@@ -35,6 +48,8 @@
#include "attitude.h"
#include "ins.h"
#include "bmi088.h"
#include "robot_typedef.h"
#include "c_comm.h"
#include <stdio.h>
#include <math.h>
@@ -42,13 +57,31 @@
#define DEBUG_ATT_HEALTH 1
/* 1-再附加原始陀螺/零偏/采样计数(排障用); 0-只留上面的通用健康位 */
#define DEBUG_ATT_DIAG 1
/* 1-追加控制量/控制模式(见文件头); 0-保持原姿态输出不变 */
#define DEBUG_CMD_OUT 1
/* 输出周期(ms)。姿态观测建议 100ms(10Hz) */
#define DEBUG_OUT_PERIOD_MS 100
#if DEBUG_CMD_OUT
/* 底盘模式名(便于人读;数值定义见 robot_typedef.h 的 chassis_mode_e) */
static const char * cmd_mode_name(chassis_mode_e m)
{
switch (m)
{
case CHASSIS_MODE_OFF: return "OFF";
case CHASSIS_MODE_MANUAL: return "MANUAL";
case CHASSIS_MODE_FOLLOW: return "FOLLOW";
case CHASSIS_MODE_SPIN_SLOW: return "SPIN_SLOW";
case CHASSIS_MODE_SPIN_FAST: return "SPIN_FAST";
default: return "?";
}
}
#endif
void debug_task(void const * argument)
{
char line[192];
char line[448];
char tail[112];
for (;;)
@@ -70,15 +103,29 @@ void debug_task(void const * argument)
(unsigned long)bmi088_spi_errors, (unsigned)bmi088_spi_state());
#endif
#if DEBUG_CMD_OUT
char ctl[192];
snprintf(ctl, sizeof(ctl),
" mode=%s online=%u vx=%.3f vy=%.3f wz=%.3f spin=%u s2=%u reset=%u",
cmd_mode_name(cmd_robot.chassis_mode),
(unsigned)cmd_robot.online,
cmd_robot.vx, cmd_robot.vy, cmd_robot.wz_manual,
(unsigned)ctoc_remote.spin_flag,
(unsigned)ctoc_remote.s2,
(unsigned)cmd_robot.gimbal_reset);
#else
const char * ctl = "";
#endif
#if DEBUG_ATT_HEALTH
int len = snprintf(line, sizeof(line),
"yaw=%.2f pitch=%.2f roll=%.2f cvg=%u rdy=%u imu=%u an=%.2f gn=%.3f%s\r\n",
"yaw=%.2f pitch=%.2f roll=%.2f cvg=%u rdy=%u imu=%u an=%.2f gn=%.3f%s%s\r\n",
att_deg_yaw, att_deg_pitch, att_deg_roll,
(unsigned)INS.ConvergeFlag, (unsigned)att_ready,
(unsigned)bmi088_online, an, gn, tail);
(unsigned)bmi088_online, an, gn, tail, ctl);
#else
int len = snprintf(line, sizeof(line), "yaw=%.2f pitch=%.2f roll=%.2f\r\n",
att_deg_yaw, att_deg_pitch, att_deg_roll);
int len = snprintf(line, sizeof(line), "yaw=%.2f pitch=%.2f roll=%.2f%s\r\n",
att_deg_yaw, att_deg_pitch, att_deg_roll, ctl);
#endif
if (len > 0)
@@ -7,6 +7,9 @@
#include "cmsis_os.h"
#include "bsp_usart.h"
#include "referee.h"
#include "referee_ui.h"
#include "c_comm.h"
#include "attitude.h"
#include "robot_param.h"
/* referee_task:裁判系统链路监测
@@ -29,6 +32,37 @@ void referee_task(void const * argument)
HAL_GetTick() - referee.last_update_tick > DEVICE_OFFLINE_TIME_MS * 10)
referee.link_status = 0;
/* ---- 自定义UI:机器人 -> 裁判系统 -> 选手端 ------------------------
* 本任务 100ms 一轮,内部按 UI_SEND_HZ 分频(5Hz:约 40 图形/帧,
* 115200 链路上约 3.8KB/s)。数据来源:
* 底盘本地 = att_deg_yaw(朝向) + referee(功率/电容)
* 云台 CToC = pitch + 摩擦轮 L/R 转速(0x1B9) + 云台 yaw(0x1A9) + 陀螺位(0x189)
* ------------------------------------------------------------------ */
{
static uint8_t ui_div = 0;
if (++ui_div >= (uint8_t)(10 / UI_SEND_HZ))
{
ui_div = 0;
ui_data_t ui;
ui.pitch_deg = ctoc_ui_pitch_deg;
ui.yaw_deg = ctoc_gimbal_yaw;
ui.hdg_deg = att_deg_yaw;
ui.pwr_w = referee.chassis_power;
ui.pwr_limit_w = (float)referee.chassis_power_limit;
ui.buf_j = referee.buffer_energy;
ui.cap_pct = (uint8_t)(referee.buffer_energy >= 60.0f
? 100
: (referee.buffer_energy * 100.0f / 60.0f));
ui.fric_l_rpm = ctoc_ui_fric_l_rpm;
ui.fric_r_rpm = ctoc_ui_fric_r_rpm;
ui.spin_on = ctoc_remote.spin_flag;
ui.ref_online = referee.link_status;
referee_ui_update(&ui);
}
}
monitor_heartbeat(HB_REFEREE);
osDelay(100);
}
+1
View File
@@ -124,6 +124,7 @@
#define CTOC_SLAVE_ID1 0x149 // 云台->底盘:摇杆数据
#define CTOC_SLAVE_ID2 0x189 // 云台->底盘:控制状态+故障/陀螺使能位
#define CTOC_SLAVE_ID3 0x1A9 // 云台->底盘:云台世界系 yaw(float)+att_ready
#define CTOC_SLAVE_ID4 0x1B9 // 云台->底盘:UI 数据(pitch + 摩擦轮 L/R 转速)
/* ============================ CAN ID ============================ */
/* 云台板:CAN1 = Pitch GM6020(0x206,0x1FE)+摩擦轮 M3508(0x201/0x202,0x200)
+26 -14
View File
@@ -1246,6 +1246,18 @@
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\modules\comms\referee_ui.c</PathWithFileName>
<FilenameWithoutPath>referee_ui.c</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
<File>
<GroupNumber>10</GroupNumber>
<FileNumber>82</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\modules\dev\led.c</PathWithFileName>
<FilenameWithoutPath>led.c</FilenameWithoutPath>
<RteFlg>0</RteFlg>
@@ -1253,7 +1265,7 @@
</File>
<File>
<GroupNumber>10</GroupNumber>
<FileNumber>82</FileNumber>
<FileNumber>83</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@@ -1265,7 +1277,7 @@
</File>
<File>
<GroupNumber>10</GroupNumber>
<FileNumber>83</FileNumber>
<FileNumber>84</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@@ -1277,7 +1289,7 @@
</File>
<File>
<GroupNumber>10</GroupNumber>
<FileNumber>84</FileNumber>
<FileNumber>85</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@@ -1289,7 +1301,7 @@
</File>
<File>
<GroupNumber>10</GroupNumber>
<FileNumber>85</FileNumber>
<FileNumber>86</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@@ -1301,7 +1313,7 @@
</File>
<File>
<GroupNumber>10</GroupNumber>
<FileNumber>86</FileNumber>
<FileNumber>87</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@@ -1313,7 +1325,7 @@
</File>
<File>
<GroupNumber>10</GroupNumber>
<FileNumber>87</FileNumber>
<FileNumber>88</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@@ -1325,7 +1337,7 @@
</File>
<File>
<GroupNumber>10</GroupNumber>
<FileNumber>88</FileNumber>
<FileNumber>89</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@@ -1337,7 +1349,7 @@
</File>
<File>
<GroupNumber>10</GroupNumber>
<FileNumber>89</FileNumber>
<FileNumber>90</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@@ -1349,7 +1361,7 @@
</File>
<File>
<GroupNumber>10</GroupNumber>
<FileNumber>90</FileNumber>
<FileNumber>91</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@@ -1369,7 +1381,7 @@
<RteFlg>0</RteFlg>
<File>
<GroupNumber>11</GroupNumber>
<FileNumber>91</FileNumber>
<FileNumber>92</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@@ -1381,7 +1393,7 @@
</File>
<File>
<GroupNumber>11</GroupNumber>
<FileNumber>92</FileNumber>
<FileNumber>93</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@@ -1393,7 +1405,7 @@
</File>
<File>
<GroupNumber>11</GroupNumber>
<FileNumber>93</FileNumber>
<FileNumber>94</FileNumber>
<FileType>5</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@@ -1405,7 +1417,7 @@
</File>
<File>
<GroupNumber>11</GroupNumber>
<FileNumber>94</FileNumber>
<FileNumber>95</FileNumber>
<FileType>5</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@@ -1425,7 +1437,7 @@
<RteFlg>0</RteFlg>
<File>
<GroupNumber>12</GroupNumber>
<FileNumber>95</FileNumber>
<FileNumber>96</FileNumber>
<FileType>4</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@@ -2363,6 +2363,11 @@
<FileType>1</FileType>
<FilePath>..\modules\comms\referee.c</FilePath>
</File>
<File>
<FileName>referee_ui.c</FileName>
<FileType>1</FileType>
<FilePath>..\modules\comms\referee_ui.c</FilePath>
</File>
<File>
<FileName>led.c</FileName>
<FileType>1</FileType>
+22
View File
@@ -23,6 +23,13 @@ static volatile uint32_t ctoc_att_last_tick = 0;
/* 云台自检下发的编码器方向:+1=tick增向CCW -1=CW 0=未检测(0x1A9 flags) */
volatile int8_t ctoc_encoder_ccw = 0;
/* ---- 0x1B9 自定义UI 专用量(云台提供,底盘本地没有) ---- */
volatile float ctoc_ui_pitch_deg = 0.0f;
volatile float ctoc_ui_fric_l_rpm = 0.0f;
volatile float ctoc_ui_fric_r_rpm = 0.0f;
volatile uint8_t ctoc_ui_spin_flag = 0;
volatile uint32_t ctoc_ui_last_tick = 0;
/* 0x1A9 帧新鲜判定(两板公共:底盘跟随用它门控,云台留作诊断) */
uint8_t ctoc_att_fresh(void)
{
@@ -179,11 +186,26 @@ static void c_comm_rx_0x1a9(uint32_t std_id, const uint8_t rx_data[8], uint32_t
ctoc_att_last_tick = HAL_GetTick();
}
/* 0x1B9 云台->底盘:自定义UI 需要而底盘本地没有的量
* [0..1] pitch(0.01°) [2..3] 左摩擦轮(rpm/100) [4..5] 右摩擦轮(rpm/100)
* [6] bit0=小陀螺使能 */
static void c_comm_rx_0x1b9(uint32_t std_id, const uint8_t rx_data[8], uint32_t dlc)
{
(void)std_id;
(void)dlc;
ctoc_ui_pitch_deg = (float)(int16_t)(((uint16_t)rx_data[1] << 8) | rx_data[0]) * 0.01f;
ctoc_ui_fric_l_rpm = (float)(int16_t)(((uint16_t)rx_data[3] << 8) | rx_data[2]) * 100.0f;
ctoc_ui_fric_r_rpm = (float)(int16_t)(((uint16_t)rx_data[5] << 8) | rx_data[4]) * 100.0f;
ctoc_ui_spin_flag = rx_data[6] & 0x01;
ctoc_ui_last_tick = HAL_GetTick();
}
void c_comm_init(void)
{
can_rx_register(&hcan2, CTOC_SLAVE_ID1, c_comm_rx_0x149);
can_rx_register(&hcan2, CTOC_SLAVE_ID2, c_comm_rx_0x189);
can_rx_register(&hcan2, CTOC_SLAVE_ID3, c_comm_rx_0x1a9);
can_rx_register(&hcan2, CTOC_SLAVE_ID4, c_comm_rx_0x1b9);
}
void c_comm_slave_send(uint8_t motor_fault)
+12
View File
@@ -63,6 +63,18 @@ extern volatile int8_t ctoc_encoder_ccw;
/* 底盘板:0x1A9 帧是否新鲜(掉线检测) */
extern uint8_t ctoc_att_fresh(void);
/* ---- 0x1B9 云台->底盘:自定义UI 专用数据 ---------------------------------
* 底盘本地没有、但 UI 要显示的量(见 referee_ui):pitch 角度与左右摩擦轮转速。
* 单位:0.01° / rpm(×100 压缩成 int16 传输) */
extern volatile float ctoc_ui_pitch_deg;
extern volatile float ctoc_ui_fric_l_rpm;
extern volatile float ctoc_ui_fric_r_rpm;
extern volatile uint8_t ctoc_ui_spin_flag;
extern volatile uint32_t ctoc_ui_last_tick;
/* 云台板:发送 0x1B9 UI 数据帧(2ms 周期或按需,c_comm_task 调用) */
extern void c_comm_master_send_ui(float pitch_deg, float fric_l_rpm, float fric_r_rpm, uint8_t spin_on);
/* 注册 CAN 接收回调(can_start() 之后调用) */
extern void c_comm_init(void);
+45
View File
@@ -216,3 +216,48 @@ void referee_parse_bytes(const uint8_t * data, uint16_t len)
while (len--)
referee_parse_byte(*data++);
}
/* ============================================================================
* 发送:裁判系统帧(机器人 -> 裁判系统 -> 选手端)
* 帧: A5 | data_length(2,LE,=payload 字节数,不含 cmd_id) | seq | CRC8
* | cmd_id(2,LE) | data | CRC16(2,LE,低字节先)
* CRC8 覆盖前 4 字节;CRC16 覆盖除末 2 字节外全部。
* 自定义UI 走 cmd 0x0301(payload 首 6 字节为 data_cmd_id/sender_ID/receiver_ID)。
* USART6 TX DMA:静态缓冲,发送期间不得被覆盖;UI 刷新率 5Hz 远低于链路能力。
* ==========================================================================*/
#include "bsp_usart.h"
static uint8_t ref_tx_seq = 0;
void referee_send_frame(uint16_t cmd_id, const uint8_t * data, uint16_t len)
{
static uint8_t frame[REF_FRAME_MAX_LEN];
uint16_t i = 0;
if (len > (uint16_t)(REF_FRAME_MAX_LEN - 9) || data == NULL)
return;
frame[i++] = 0xA5;
frame[i++] = (uint8_t)(len & 0xFF);
frame[i++] = (uint8_t)(len >> 8);
frame[i++] = ref_tx_seq++;
uint8_t crc8 = 0xFF;
for (uint16_t k = 0; k < 4; k++)
crc8 = crc8_update(crc8, frame[k]);
frame[i++] = crc8;
frame[i++] = (uint8_t)(cmd_id & 0xFF);
frame[i++] = (uint8_t)(cmd_id >> 8);
for (uint16_t k = 0; k < len; k++)
frame[i++] = data[k];
uint16_t crc16 = 0xFFFF;
for (uint16_t k = 0; k < i; k++)
crc16 = crc16_update(crc16, frame[k]);
frame[i++] = (uint8_t)(crc16 & 0xFF);
frame[i++] = (uint8_t)(crc16 >> 8);
usart6_tx_dma_enable(frame, i);
}
+6
View File
@@ -30,4 +30,10 @@ extern void referee_parse_bytes(const uint8_t * data, uint16_t len);
typedef void (*referee_keyboard_hook_f)(const uint8_t data[12]);
extern void referee_set_keyboard_hook(referee_keyboard_hook_f hook);
/* ---- 发送(机器人 -> 裁判系统/选手端) -------------------------------------
* 用于自定义UI(0x0301 机器人交互数据)。data_length 取 payload 字节数(不含 cmd_id,
* 与解析器一致);CRC8 覆盖前 4 字节,CRC16 覆盖除末 2 字节外全部(低字节先)。
* 内部静态缓冲 + USART6 TX DMA,立即返回,勿高频调用(链路 115200)。 */
extern void referee_send_frame(uint16_t cmd_id, const uint8_t * data, uint16_t len);
#endif
+243
View File
@@ -0,0 +1,243 @@
/* =============================================================================
* referee_ui.c - 裁判系统自定义UI组包与发送(版式见 referee_ui.h / hud_mock5.png)
*
* 组包: 每包 = ext_student_interactive_header_data_t{data_cmd_id, sender_ID,
* receiver_ID} + N×15 字节图形数据,N 由 data_cmd_id 决定(1/2/5/7)。
* 图形名用 3 字符自增计数器(base36)保证唯一;首轮 ADD,之后 MODIFY。
* 发送: referee_send_frame(0x0301, payload, 6 + N*15) -> USART6 DMA
* ==========================================================================*/
#include "referee_ui.h"
#include "referee.h"
#include "robot_param.h"
#include <string.h>
/* 协议要求:图形数据恰好 15 字节(头 11 + 尾 4)。若 pack(1) 失效或位域被重排,
* 这里直接编译失败,而不是把错位的图形发给选手端。 */
_Static_assert(sizeof(ui_head_t) == 11, "ui_head_t 必须是 11 字节");
_Static_assert(sizeof(ui_draw_t) == 15, "ui_draw_t 必须是 15 字节");
_Static_assert(sizeof(ui_num_t) == 15, "ui_num_t 必须是 15 字节");
_Static_assert(sizeof(ui_graphic_t) == 15, "ui_graphic_t 必须是 15 字节");
/* ---- 图形名生成:base26 三字符(17576 个组合,足够) ---- */
static uint16_t ui_name_seq = 0;
static void ui_next_name(uint8_t name[3])
{
uint16_t v = ui_name_seq++;
name[0] = (uint8_t)('A' + (v / 676) % 26);
name[1] = (uint8_t)('A' + (v / 26) % 26);
name[2] = (uint8_t)('A' + v % 26);
}
/* ---- 打包缓冲 ---- */
#define UI_PAYLOAD_MAX (6 + UI_MAX_PER_PACKET * sizeof(ui_graphic_t))
static uint8_t ui_pkt[UI_PAYLOAD_MAX];
static uint8_t ui_pkt_n; /* 当前包里的图形个数 */
static uint8_t ui_op = UI_OP_ADD; /* 首轮 ADD,之后 MOD */
static uint8_t ui_ready = 0; /* 首轮是否已发完 */
static void ui_flush(void)
{
if (ui_pkt_n == 0)
return;
uint16_t cmd;
switch (ui_pkt_n)
{
case 1: cmd = UI_CMD_1; break;
case 2: cmd = UI_CMD_2; break;
case 5: cmd = UI_CMD_5; break;
case 7: cmd = UI_CMD_7; break;
default: return; /* 协议只支持 1/2/5/7,不该发生 */
}
/* header: data_cmd_id / sender_ID / receiver_ID,均小端 */
ui_pkt[0] = (uint8_t)(cmd & 0xFF);
ui_pkt[1] = (uint8_t)(cmd >> 8);
uint16_t sid = (uint16_t)referee.robot_id;
ui_pkt[2] = (uint8_t)(sid & 0xFF);
ui_pkt[3] = (uint8_t)(sid >> 8);
ui_pkt[4] = (uint8_t)(UI_RECEIVER_ID & 0xFF);
ui_pkt[5] = (uint8_t)(UI_RECEIVER_ID >> 8);
referee_send_frame(UI_CMD_BASE, ui_pkt, (uint16_t)(6 + ui_pkt_n * sizeof(ui_graphic_t)));
ui_pkt_n = 0;
}
static ui_graphic_t * ui_new(uint8_t type, uint8_t layer, uint8_t color)
{
if (ui_pkt_n >= UI_MAX_PER_PACKET)
ui_flush();
ui_graphic_t * g = (ui_graphic_t *)&ui_pkt[6 + ui_pkt_n * sizeof(ui_graphic_t)];
memset(g, 0, sizeof(*g));
ui_next_name(g->d.h.name); /* d.h 与 n.h 同址 */
g->d.h.type = type;
g->d.h.layer = layer;
g->d.h.color = color;
g->d.h.operate = ui_op;
ui_pkt_n++;
return g;
}
/* ---- 图元封装(绘制类写 d.*,数值类写 n.val) ---- */
static void ui_line(uint8_t layer, uint8_t color, int x0, int y0, int x1, int y1, int w)
{
ui_graphic_t * g = ui_new(UI_T_LINE, layer, color);
g->d.h.x0 = (uint32_t)x0; g->d.h.y0 = (uint32_t)y0;
g->d.x1 = (uint32_t)x1; g->d.y1 = (uint32_t)y1;
g->d.h.width = (uint32_t)w;
}
static void ui_rect(uint8_t layer, uint8_t color, int x0, int y0, int x1, int y1)
{
ui_graphic_t * g = ui_new(UI_T_RECT, layer, color);
g->d.h.x0 = (uint32_t)x0; g->d.h.y0 = (uint32_t)y0;
g->d.x1 = (uint32_t)x1; g->d.y1 = (uint32_t)y1;
}
static void ui_fval(uint8_t layer, uint8_t color, int x, int y, float v, int digits)
{
ui_graphic_t * g = ui_new(UI_T_FLOAT, layer, color);
g->n.h.x0 = (uint32_t)x; g->n.h.y0 = (uint32_t)y;
g->n.h.width = (uint32_t)digits; /* 浮点:小数位数 */
g->n.val.f = v;
}
static void ui_ival(uint8_t layer, uint8_t color, int x, int y, int32_t v)
{
ui_graphic_t * g = ui_new(UI_T_INT, layer, color);
g->n.h.x0 = (uint32_t)x; g->n.h.y0 = (uint32_t)y;
g->n.val.i = v;
}
static void ui_char(uint8_t layer, uint8_t color, int x, int y, const char * s)
{
ui_graphic_t * g = ui_new(UI_T_CHAR, layer, color);
g->n.h.x0 = (uint32_t)x; g->n.h.y0 = (uint32_t)y;
g->n.h.width = (uint32_t)strlen(s);
for (int i = 0; i < 4; i++) /* 最多 4 字节 ASCII */
g->n.val.s[i] = s[i];
}
/* 角度归一到 [0,360) */
static float ui_norm360(float a)
{
while (a < 0.0f) a += 360.0f;
while (a >= 360.0f) a -= 360.0f;
return a;
}
void referee_ui_update(const ui_data_t * d)
{
if (d == NULL)
return;
/* 裁判系统离线:发一轮删除图层,然后停发(避免选手端残留最后一帧) */
if (!d->ref_online)
{
if (ui_ready)
{
ui_pkt_n = 0;
ui_graphic_t * g = ui_new(UI_T_RECT, UI_L_FRAME, UI_C_BLACK);
g->d.h.operate = UI_OP_DEL;
g->d.h.x0 = 0; g->d.h.y0 = 0; g->d.x1 = 0; g->d.y1 = 0;
ui_flush();
ui_ready = 0;
ui_name_seq = 0;
ui_op = UI_OP_ADD;
}
return;
}
/* ---------------- 左上:预估底盘功率 ---------------- */
uint8_t pw_col = (d->pwr_w > d->pwr_limit_w) ? UI_C_RED
: (d->pwr_w > d->pwr_limit_w * 0.8f) ? UI_C_ORANGE : UI_C_GREEN;
ui_rect(UI_L_FRAME, UI_C_CYAN, UI_PWR_X0, UI_PWR_Y0, UI_PWR_X1, UI_PWR_Y0 + 4);
ui_char(UI_L_TEXT, UI_C_CYAN, UI_PWR_X0 + 20, UI_PWR_Y0 + 30, "PWR");
ui_fval(UI_L_TEXT, UI_C_WHITE, UI_PWR_X0 + 20, UI_PWR_Y0 + 64, d->pwr_w, 1);
ui_char(UI_L_TEXT, UI_C_WHITE, UI_PWR_X0 + 210, UI_PWR_Y0 + 84, "W");
int pw_w_px = (int)((UI_PWR_X1 - UI_PWR_X0 - 40) * (d->pwr_w / (d->pwr_limit_w > 1.0f ? d->pwr_limit_w : 1.0f)));
if (pw_w_px > UI_PWR_X1 - UI_PWR_X0 - 40) pw_w_px = UI_PWR_X1 - UI_PWR_X0 - 40;
if (pw_w_px < 0) pw_w_px = 0;
ui_rect(UI_L_GAUGE, pw_col, UI_PWR_X0 + 20, UI_PWR_Y0 + 120, UI_PWR_X0 + 20 + pw_w_px, UI_PWR_Y0 + 142);
ui_line(UI_L_GAUGE, UI_C_WHITE, UI_PWR_X0 + 20 + (UI_PWR_X1 - UI_PWR_X0 - 40) - 3, UI_PWR_Y0 + 116,
UI_PWR_X0 + 20 + (UI_PWR_X1 - UI_PWR_X0 - 40) - 3, UI_PWR_Y0 + 146, 2);
/* ---------------- 左中:超级电容 ---------------- */
uint8_t cap_col = (d->cap_pct < 15) ? UI_C_RED : (d->cap_pct < 40) ? UI_C_ORANGE : UI_C_CYAN;
ui_rect(UI_L_FRAME, cap_col, UI_CAP_X0, UI_CAP_Y0, UI_CAP_X1, UI_CAP_Y0 + 4);
ui_char(UI_L_TEXT, cap_col, UI_CAP_X0 + 20, UI_CAP_Y0 + 30, "CAP");
ui_ival(UI_L_TEXT, UI_C_WHITE, UI_CAP_X0 + 20, UI_CAP_Y0 + 64, (int32_t)d->cap_pct);
ui_char(UI_L_TEXT, UI_C_WHITE, UI_CAP_X0 + 130, UI_CAP_Y0 + 84, "%");
int cap_px = (int)((UI_CAP_X1 - UI_CAP_X0 - 40) * (d->cap_pct / 100.0f));
ui_rect(UI_L_GAUGE, cap_col, UI_CAP_X0 + 20, UI_CAP_Y0 + 118, UI_CAP_X0 + 20 + cap_px, UI_CAP_Y0 + 140);
/* ---------------- 顶部:罗盘刻度带(底盘正方向朝向) ---------------- */
{
float h = ui_norm360(d->hdg_deg);
ui_rect(UI_L_FRAME, UI_C_CYAN, UI_HDG_X0, UI_HDG_Y0, UI_HDG_X1, UI_HDG_Y0 + 4);
ui_line(UI_L_FRAME, UI_C_CYAN, UI_HDG_X0 + 20, UI_HDG_Y0 + 62, UI_HDG_X1 - 20, UI_HDG_Y0 + 62, 2);
int cx = (UI_HDG_X0 + UI_HDG_X1) / 2;
for (int k = -1; k <= 1; k++) /* 3 条刻度 */
{
int x = cx + k * (UI_HDG_TAPE_HALF / 2);
ui_line(UI_L_FRAME, UI_C_CYAN, x, UI_HDG_Y0 + 40, x, UI_HDG_Y0 + 62, k == 0 ? 3 : 2);
}
ui_ival(UI_L_TEXT, UI_C_CYAN, cx - UI_HDG_TAPE_HALF / 2 - 24, UI_HDG_Y0 + 18,
(int32_t)ui_norm360(h - 30.0f));
ui_ival(UI_L_TEXT, UI_C_CYAN, cx + UI_HDG_TAPE_HALF / 2 - 24, UI_HDG_Y0 + 18,
(int32_t)ui_norm360(h + 30.0f));
ui_line(UI_L_FRAME, UI_C_WHITE, cx, UI_HDG_Y0 + 62, cx - 8, UI_HDG_Y0 + 50, 2);
ui_line(UI_L_FRAME, UI_C_WHITE, cx, UI_HDG_Y0 + 62, cx + 8, UI_HDG_Y0 + 50, 2);
ui_char(UI_L_TEXT, UI_C_CYAN, UI_HDG_X0 + 24, UI_HDG_Y0 + 70, "HDG");
ui_ival(UI_L_TEXT, UI_C_WHITE, UI_HDG_X0 + 90, UI_HDG_Y0 + 66, (int32_t)h);
ui_char(UI_L_TEXT, UI_C_CYAN, UI_HDG_X1 - 170, UI_HDG_Y0 + 70, "YAW");
ui_ival(UI_L_TEXT, UI_C_WHITE, UI_HDG_X1 - 104, UI_HDG_Y0 + 66, (int32_t)ui_norm360(d->yaw_deg));
}
/* ---------------- 中央:俯仰梯 ---------------- */
ui_line(UI_L_FRAME, UI_C_WHITE, UI_LAD_CX - 44, UI_LAD_CY, UI_LAD_CX - 14, UI_LAD_CY, 4);
ui_line(UI_L_FRAME, UI_C_WHITE, UI_LAD_CX + 14, UI_LAD_CY, UI_LAD_CX + 44, UI_LAD_CY, 4);
for (int i = -2; i <= 2; i++) /* ±10/±5/0 五条梯线 */
{
if (i == 0) continue;
int v = i * 5;
int yy = UI_LAD_CY - v * UI_LAD_PX_PER_DEG;
int half = (i % 2 == 0) ? UI_LAD_HALF_LONG : UI_LAD_HALF_SHORT;
ui_line(UI_L_GAUGE, UI_C_CYAN, UI_LAD_CX - half, yy, UI_LAD_CX - 18, yy, 2);
ui_line(UI_L_GAUGE, UI_C_CYAN, UI_LAD_CX + 18, yy, UI_LAD_CX + half, yy, 2);
}
ui_ival(UI_L_TEXT, UI_C_CYAN, UI_LAD_CX - UI_LAD_HALF_SHORT - 40, UI_LAD_CY - 10 * UI_LAD_PX_PER_DEG - 10, 10);
ui_ival(UI_L_TEXT, UI_C_CYAN, UI_LAD_CX + UI_LAD_HALF_SHORT + 12, UI_LAD_CY - 10 * UI_LAD_PX_PER_DEG - 10, 10);
{
int py = UI_LAD_CY - (int)(d->pitch_deg * UI_LAD_PX_PER_DEG);
ui_line(UI_L_GAUGE, UI_C_ORANGE, UI_LAD_CX - UI_LAD_HALF_LONG - 20, py, UI_LAD_CX - UI_LAD_HALF_LONG - 4, py, 4);
ui_char(UI_L_TEXT, UI_C_ORANGE, UI_LAD_CX - UI_LAD_HALF_LONG - 132, py - 10, "PIT");
ui_fval(UI_L_TEXT, UI_C_WHITE, UI_LAD_CX - UI_LAD_HALF_LONG - 76, py - 14, d->pitch_deg, 1);
}
/* ---------------- 右上:摩擦轮速度 ---------------- */
{
uint8_t f_col = (d->fric_l_rpm > 1000.0f && d->fric_r_rpm > 1000.0f) ? UI_C_GREEN : UI_C_ORANGE;
ui_rect(UI_L_FRAME, f_col, UI_FRIC_X0, UI_FRIC_Y0, UI_FRIC_X1, UI_FRIC_Y0 + 4);
ui_char(UI_L_TEXT, UI_C_CYAN, UI_FRIC_X0 + 24, UI_FRIC_Y0 + 34, "FRIC");
ui_ival(UI_L_TEXT, UI_C_WHITE, UI_FRIC_X0 + 24, UI_FRIC_Y0 + 74, (int32_t)d->fric_l_rpm);
ui_ival(UI_L_TEXT, UI_C_WHITE, UI_FRIC_X0 + 250, UI_FRIC_Y0 + 74, (int32_t)d->fric_r_rpm);
ui_char(UI_L_TEXT, f_col, UI_FRIC_X0 + 24, UI_FRIC_Y1 - 60,
(d->fric_l_rpm > 1000.0f && d->fric_r_rpm > 1000.0f) ? "ARMED" : "IDLE");
int bl = (int)((UI_FRIC_Y1 - UI_FRIC_Y0 - 140) * (d->fric_l_rpm / 7000.0f));
int br = (int)((UI_FRIC_Y1 - UI_FRIC_Y0 - 140) * (d->fric_r_rpm / 7000.0f));
if (bl < 0) bl = 0; if (bl > UI_FRIC_Y1 - UI_FRIC_Y0 - 140) bl = UI_FRIC_Y1 - UI_FRIC_Y0 - 140;
if (br < 0) br = 0; if (br > UI_FRIC_Y1 - UI_FRIC_Y0 - 140) br = UI_FRIC_Y1 - UI_FRIC_Y0 - 140;
ui_rect(UI_L_GAUGE, UI_C_GREEN, UI_FRIC_X0 + 30, UI_FRIC_Y1 - 40 - bl, UI_FRIC_X0 + 74, UI_FRIC_Y1 - 40);
ui_rect(UI_L_GAUGE, UI_C_GREEN, UI_FRIC_X0 + 256, UI_FRIC_Y1 - 40 - br, UI_FRIC_X0 + 300, UI_FRIC_Y1 - 40);
}
/* ---------------- 底部:小陀螺状态 ---------------- */
ui_rect(UI_L_FRAME, d->spin_on ? UI_C_GREEN : UI_C_CYAN, UI_SPIN_X0, UI_SPIN_Y0, UI_SPIN_X1, UI_SPIN_Y0 + 4);
ui_char(UI_L_TEXT, UI_C_CYAN, UI_SPIN_X0 + 24, UI_SPIN_Y0 + 34, "SPIN");
if (d->spin_on)
ui_rect(UI_L_GAUGE, UI_C_GREEN, UI_SPIN_X0 + 160, UI_SPIN_Y0 + 26, UI_SPIN_X0 + 268, UI_SPIN_Y0 + 66);
ui_char(UI_L_TEXT, d->spin_on ? UI_C_BLACK : UI_C_WHITE, UI_SPIN_X0 + 184, UI_SPIN_Y0 + 36,
d->spin_on ? "ON" : "OFF");
/* 收尾:最后一包;首轮发完切换为 MODIFY */
ui_flush();
if (!ui_ready)
{
ui_ready = 1;
ui_op = UI_OP_MOD;
}
}
+159
View File
@@ -0,0 +1,159 @@
#ifndef REFEREE_UI_H
#define REFEREE_UI_H
#include <stdint.h>
/* ============================================================================
* referee_ui.h - 裁判系统"自定义UI"(机器人 -> 选手端 图形绘制)
*
* 通道: 底盘板 USART6(裁判系统) --cmd 0x0301 机器人交互数据--> 选手端叠加显示
* 依据: 裁判系统串口协议 自定义UI 章节 + ARUW Taproot / Triton TR-mbed 两套实现
* 版式: 详见 E:\\ELCT\\_mcp_recon\\hud_mock5.png(坐标与本文 UI_* 宏一一对应)
*
* 硬约束(协议):
* 1. 一条消息最多携带 7 个图形(0x0101/0x0102/0x0103/0x0104 = 1/2/5/7 个)
* 2. 图形总数有限,且图形用"名称"标识 -> 首轮用 ADD,之后一律 MODIFY,
* 否则会在选手端把图形表撑爆
* 3. 图层数 0~9;坐标原点在屏幕左上角,x 向右、y 向下,画布 1920x1080
* 4. 不得绘制官方保留区(顶部比分条 / 左下小地图 / 右下战车模块 / 中部两块保留)
* 5. 协议无"圆角矩形/渐变/图标"原语,只能用 线/矩形/圆/椭圆/弧 + 数值/字符
* ==========================================================================*/
/* ---- 图形数据 -------------------------------------------------------------
* 绘制类(线/矩形/圆/椭圆/弧) 与 数值类(浮点/整型/字符) 都是 15 字节:
* 头 11 字节(所有类型共用): name[3] + operate:3 type:3 layer:4 color:4
* sa:9 ea:9 width:10 x0:11 y0:11
* 后 4 字节: 绘制类 = radius:10 x1:11 y1:11;数值类 = 一个 float/int32/char[4]
* 所以用"共用头 + 联合体"表达,两种视图都是 15 字节。
* 必须 pack(1):否则位域会对齐到 4 字节,整体变 16 字节。 */
#pragma pack(push, 1)
typedef struct
{
uint8_t name[3]; /* 图形名(3 字节 ASCII) */
uint32_t operate : 3; /* UI_OP_* 空/加/改/删 */
uint32_t type : 3; /* UI_T_* 图形类型 */
uint32_t layer : 4; /* UI_L_* 图层 0~9 */
uint32_t color : 4; /* UI_C_* 颜色 */
uint32_t sa : 9; /* 起始角(弧/椭圆) */
uint32_t ea : 9; /* 结束角 */
uint32_t width : 10; /* 线宽 / 小数位 / 字符串长度 */
uint32_t x0 : 11; /* 起点 x */
uint32_t y0 : 11; /* 起点 y */
} ui_head_t; /* 3 + 8 = 11 字节 */
typedef struct { ui_head_t h; uint32_t radius : 10, x1 : 11, y1 : 11; } ui_draw_t;
typedef struct { ui_head_t h; union { float f; int32_t i; char s[4]; } val; } ui_num_t;
typedef union
{
ui_draw_t d; /* 绘制类视图 */
ui_num_t n; /* 数值类视图 */
uint8_t raw[15];
} ui_graphic_t;
#pragma pack(pop)
/* ---- 命令 / 操作码 / 图形类型 -------------------------------------------- */
#define UI_CMD_BASE 0x0301 /* 机器人交互数据 */
#define UI_CMD_1 0x0101
#define UI_CMD_2 0x0102
#define UI_CMD_5 0x0103
#define UI_CMD_7 0x0104
#define UI_MAX_PER_PACKET 7
#define UI_OP_NOP 0
#define UI_OP_ADD 1
#define UI_OP_MOD 2 /* 稳态刷新一律用它 */
#define UI_OP_DEL 3
#define UI_T_LINE 0
#define UI_T_RECT 1
#define UI_T_CIRCLE 2
#define UI_T_ELLIPSE 3
#define UI_T_ARC 4
#define UI_T_FLOAT 5
#define UI_T_INT 6
#define UI_T_CHAR 7
/* ---- 颜色(官方 0~8) ------------------------------------------------------ */
#define UI_C_RED 0
#define UI_C_YELLOW 1
#define UI_C_GREEN 2
#define UI_C_ORANGE 3
#define UI_C_PURPLE 4
#define UI_C_PINK 5
#define UI_C_CYAN 6
#define UI_C_BLACK 7
#define UI_C_WHITE 8
/* ---- 图层(协议 0~9) ------------------------------------------------------ */
#define UI_L_FRAME 0 /* 卡片边框/刻度 */
#define UI_L_GAUGE 1 /* 能量条/进度条 */
#define UI_L_TEXT 2 /* 数值/字符 */
/* ---- 刷新率:5Hz 约 3.8KB/s(115200 链路),协议允许再调高 ---------------- */
#define UI_SEND_HZ 5
/* ---- 发送/接收方 ID -----------------------------------------------------
* sender = 本机机器人 ID(referee.robot_id,无裁判系统时用 0)
* receiver= 选手端/客户端(官方 UI 约定 0x8080);若你的协议版本要求填机器人 ID,
* 改这里一处即可。 */
#define UI_RECEIVER_ID 0x8080
/* ---- 画布 ---------------------------------------------------------------- */
#define UI_SCREEN_W 1920
#define UI_SCREEN_H 1080
/* ---- 版式坐标(与 hud_mock5.png 对应,全部避开官方保留区) ---------------- */
/* 左上:预估底盘功率 */ /* 左上 x56..520 y176..386 */
#define UI_PWR_X0 56
#define UI_PWR_Y0 176
#define UI_PWR_X1 520
#define UI_PWR_Y1 386
/* 左中:超级电容 */ /* x56..520 y404..566 */
#define UI_CAP_X0 56
#define UI_CAP_Y0 404
#define UI_CAP_X1 520
#define UI_CAP_Y1 566
/* 顶部:罗盘刻度带(底盘正方向) */ /* x626..1258 y186..270 */
#define UI_HDG_X0 626
#define UI_HDG_Y0 186
#define UI_HDG_X1 1258
#define UI_HDG_Y1 270
#define UI_HDG_TAPE_HALF 216 /* 刻度带半宽(像素,对应 ±30°) */
/* 中央:俯仰梯 */ /* 以屏幕中心为基准 */
#define UI_LAD_CX 960
#define UI_LAD_CY 540
#define UI_LAD_PX_PER_DEG 11 /* 每度 11 像素 */
#define UI_LAD_HALF_LONG 96
#define UI_LAD_HALF_SHORT 62
/* 右上:摩擦轮速度 */ /* x1420..1864 y176..520 */
#define UI_FRIC_X0 1420
#define UI_FRIC_Y0 176
#define UI_FRIC_X1 1864
#define UI_FRIC_Y1 520
/* 底部:小陀螺状态 */ /* x700..1220 y874..1000 */
#define UI_SPIN_X0 700
#define UI_SPIN_Y0 874
#define UI_SPIN_X1 1220
#define UI_SPIN_Y1 1000
/* ---- 输入数据(referee_task 每周期填) ----------------------------------- */
typedef struct
{
float pitch_deg; /* 云台 pitch(来自 CToC) */
float yaw_deg; /* 云台 yaw(来自 CToC 姿态帧) */
float hdg_deg; /* 底盘正方向朝向(本板 att_deg_yaw) */
float pwr_w; /* 底盘实时功率(裁判 0x0202) */
float pwr_limit_w; /* 功率上限(裁判 0x0201) */
float buf_j; /* 缓冲能量/超级电容(J) */
uint8_t cap_pct; /* 电容百分比(0~100) */
float fric_l_rpm; /* 左摩擦轮转速(来自 CToC) */
float fric_r_rpm; /* 右摩擦轮转速 */
uint8_t spin_on; /* 小陀螺使能 */
uint8_t ref_online; /* 裁判系统在线(离线时整屏删图形) */
} ui_data_t;
/* 周期调用(建议 5Hz):组包并发送;裁判系统离线时发一次"删除全部图层" */
extern void referee_ui_update(const ui_data_t * d);
#endif