refactor(modules): 按角色重排 modules/ 目录,消除命名不一致
原 modules/ 下目录大小写混用(BMI088/BUZZER/DR16/IST8310/LED/PID 为大写,
attitude/c_comm/filter/gimbal/ins/motor/referee/shoot 为小写),且把第三方
移植代码(QuaternionEKF/kalman_filter)与本工程封装(ins)混在同一目录。
新结构(全部小写,按角色分层,第三方代码单独隔离):
modules/
algorithm/ pid.c/h filter.c/h 通用算法
imu/ attitude.c/h ins.c/h 姿态与惯导封装
imu/qekf/ QuaternionEKF.c/h kalman_filter.c/h
(移植自 Wang Hongxi INS 例程)
sensor/ bmi088.c/h ist8310.c/h 板载传感器驱动
comms/ dr16.c/h c_comm.c/h referee.c/h
motor/ dji_motor.c/h 电机抽象
control/ gimbal.c/h shoot.c/h 整车控制逻辑
(底盘为 chassis.c/h)
dev/ led.c/h buzzer.c/h 板级指示/发声
共移动 58 个文件(每板 29 个),旧目录全部删除。
工程文件同步(每个工程各 2 个):
- .uvprojx: Modules 分组 16 个 <FilePath> 改为新路径;
2 个非空 <IncludePath> 元素:15 个旧模块路径收敛为 8 个新目录,
并顺带删除陈旧项 ../modules/AHRS(目录根本不存在,legacy 时代残留)
与冗余项 ..\bsp(与 ../Bsp/Inc 重复)
- .uvoptx: 16 个 <PathWithFileName> 同步
关于"为什么不用改源码": Keil 的 include 靠 IncludePath 解析,本工程所有
#include 都是纯文件名形式(无路径限定),所以移动目录只需改工程配置。
另外 .h 完全不登记在工程里(只有 16 个 .c 被登记),故头文件只需移动。
回归验证(无法在此环境编译,用等价判据):
- 两个工程引用的 98 / 95 个 <FilePath> 全部真实存在
- 磁盘上 47 / 45 个 .c 全部仍被工程引用 —— 即没有任何文件掉出编译
- 2 个 IncludePath 元素均含全部 8 个新模块目录,无旧路径残留
- 所有 IncludePath 指向的目录均真实存在
- .uvprojx/.uvoptx XML 良构
- 两棵树结构一致(chassis/gimbal 等板级差异符合预期)
⚠️ 仍需在 Keil 里点一次编译确认(见 README 的构建章节)。
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
/* =============================================================================
|
||||
* c_comm.c - 云台<->底盘 板间通讯(CToC,CAN2,帧 ID 与 legacy 完全一致)
|
||||
* 帧: 0x149 云->底 摇杆(4 个大端 u16) 0x189 云->底 控制状态
|
||||
* 0x019 底->云 发射机构上电状态
|
||||
* 云台板(主机): c_comm_master_send() 每 2ms 发摇杆帧+状态帧;
|
||||
* 键鼠在线时按 legacy 规则把 WASD/鼠标合成进摇杆量;
|
||||
* Data[0] 必须实时判定 dr16_offline()(dr16_status 掉线后不会自己清零)
|
||||
* 底盘板(从机): c_comm_slave_send() 每 2ms 回传发射机构状态;
|
||||
* 中断内解析 0x149/0x189 填充 ctoc_remote(底盘的"虚拟遥控器")
|
||||
* ========================================================================== */
|
||||
#include "c_comm.h"
|
||||
#include "robot_param.h"
|
||||
#include "referee.h"
|
||||
#include <string.h>
|
||||
|
||||
volatile ctoc_remote_t ctoc_remote;
|
||||
volatile uint8_t ctoc_shooter_power = 1;
|
||||
volatile uint32_t ctoc_reply_last_tick = 0;
|
||||
volatile uint8_t ctoc_remote_fault = 0;
|
||||
volatile float ctoc_gimbal_yaw = 0.0f;
|
||||
volatile uint8_t ctoc_att_ready = 0;
|
||||
static volatile uint32_t ctoc_att_last_tick = 0;
|
||||
/* 云台自检下发的编码器方向:+1=tick增向CCW -1=CW 0=未检测(0x1A9 flags) */
|
||||
volatile int8_t ctoc_encoder_ccw = 0;
|
||||
|
||||
/* 0x1A9 帧新鲜判定(两板公共:底盘跟随用它门控,云台留作诊断) */
|
||||
uint8_t ctoc_att_fresh(void)
|
||||
{
|
||||
return (ctoc_att_last_tick != 0 &&
|
||||
HAL_GetTick() - ctoc_att_last_tick < CTOC_OFFLINE_TIME_MS);
|
||||
}
|
||||
|
||||
static void c_comm_pack_uint16(uint8_t data[8], const uint16_t val[4])
|
||||
{
|
||||
for (uint8_t i = 0; i < 4; i++)
|
||||
{
|
||||
data[2 * i] = (uint8_t)(val[i] >> 8);
|
||||
data[2 * i + 1] = (uint8_t)(val[i] & 0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
static void c_comm_send_frame(hcan_t * hcan, uint32_t std_id, const uint8_t data[8])
|
||||
{
|
||||
CanCtrlData_s msg;
|
||||
msg.hcan = hcan;
|
||||
msg.tx_header.StdId = std_id;
|
||||
msg.tx_header.IDE = CAN_ID_STD;
|
||||
msg.tx_header.RTR = CAN_RTR_DATA;
|
||||
msg.tx_header.DLC = 8;
|
||||
msg.tx_header.TransmitGlobalTime = DISABLE;
|
||||
for (uint8_t i = 0; i < 8; i++)
|
||||
msg.tx_data[i] = data[i];
|
||||
CAN_SendTxMessage(&msg);
|
||||
}
|
||||
|
||||
#if defined(ROBOT_TYPE_GIMBAL)
|
||||
|
||||
/* ============================ 云台板(主机) ============================ */
|
||||
|
||||
static void c_comm_rx_0x019(uint32_t std_id, const uint8_t rx_data[8], uint32_t dlc)
|
||||
{
|
||||
(void)std_id;
|
||||
(void)dlc;
|
||||
ctoc_shooter_power = rx_data[0]; // 发射机构上电状态
|
||||
ctoc_remote_fault = rx_data[1] & 0x01; // 底盘电机故障位
|
||||
ctoc_reply_last_tick = HAL_GetTick();
|
||||
}
|
||||
|
||||
void c_comm_init(void)
|
||||
{
|
||||
can_rx_register(&hcan2, CTOC_MASTER_ID1, c_comm_rx_0x019);
|
||||
}
|
||||
|
||||
uint8_t ctoc_chassis_fault(void)
|
||||
{
|
||||
/* 从未收到过 0x019 视为底盘不在场,不判故障(便于仅云台台架调试);
|
||||
* 收到过之后,故障位或帧超时都判故障 */
|
||||
if (ctoc_reply_last_tick == 0)
|
||||
return 0;
|
||||
if (HAL_GetTick() - ctoc_reply_last_tick > CTOC_OFFLINE_TIME_MS)
|
||||
return 1;
|
||||
return ctoc_remote_fault;
|
||||
}
|
||||
|
||||
/* 云台板:0x1A9 姿态帧 */
|
||||
void c_comm_master_send_attitude(float yaw_deg, uint8_t flags)
|
||||
{
|
||||
uint8_t data[8] = {0};
|
||||
memcpy(data, &yaw_deg, 4); // float 小端
|
||||
data[4] = flags;
|
||||
c_comm_send_frame(&hcan2, CTOC_SLAVE_ID3, data);
|
||||
}
|
||||
|
||||
void c_comm_master_send(uint8_t keyboard_online, const uint8_t kb[12], uint8_t motor_fault, uint8_t spin_enable)
|
||||
{
|
||||
uint8_t data[8];
|
||||
|
||||
if (!keyboard_online)
|
||||
{
|
||||
// 遥控器控制:直接转发原始摇杆值
|
||||
const uint16_t sticks[4] = {dr16.rc_r_RL, dr16.rc_r_UD, dr16.rc_l_RL, dr16.rc_l_UD};
|
||||
c_comm_pack_uint16(data, sticks);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 键鼠控制:合成摇杆量(与 legacy 一致)
|
||||
uint8_t key_go = kb[8] & 0x01;
|
||||
uint8_t key_back = (kb[8] >> 1) & 0x01;
|
||||
uint8_t key_left = (kb[8] >> 2) & 0x01;
|
||||
uint8_t key_right = (kb[8] >> 3) & 0x01;
|
||||
int16_t mouse_rl = (int16_t)(((uint16_t)kb[1] << 8) | kb[0]);
|
||||
int16_t mouse_du = (int16_t)(((uint16_t)kb[3] << 8) | kb[2]);
|
||||
|
||||
const uint16_t sticks[4] = {
|
||||
(uint16_t)(1024 + (key_right - key_left) * 660), // AD->右摇杆RL
|
||||
(uint16_t)(1024 + (key_go - key_back) * 660), // WS->右摇杆UD
|
||||
(uint16_t)(1024 + mouse_rl * PC_MOUSE_RL_SENSITIVITY), // 鼠标->左摇杆RL
|
||||
(uint16_t)(1024 + mouse_du * PC_MOUSE_DU_SENSITIVITY), // 鼠标->左摇杆UD
|
||||
};
|
||||
c_comm_pack_uint16(data, sticks);
|
||||
}
|
||||
c_comm_send_frame(&hcan2, CTOC_SLAVE_ID1, data);
|
||||
|
||||
/* 在线状态必须实时判定:dr16_status 只在收帧时置 1,掉线后不会自己清零,
|
||||
* 若直接转发会让底盘永远认为遥控器在线(停车保护失效) */
|
||||
data[0] = (dr16_status && !dr16_offline()) ? 1 : 0;
|
||||
data[1] = dr16.s2;
|
||||
data[2] = dr16.key_push_ctrl;
|
||||
data[3] = dr16.key_push_shift;
|
||||
data[4] = dr16_start_flag;
|
||||
data[5] = dr16.s1;
|
||||
data[6] = (motor_fault ? 0x80 : 0) | (spin_enable ? 0x04 : 0) |
|
||||
(dr16.key_alpha1 ? 0x01 : 0) |
|
||||
(dr16.key_alpha2 ? 0x02 : 0); // bit7=故障 bit2=陀螺使能(扩展)
|
||||
data[7] = 0;
|
||||
c_comm_send_frame(&hcan2, CTOC_SLAVE_ID2, data);
|
||||
}
|
||||
|
||||
#elif defined(ROBOT_TYPE_CHASSIS)
|
||||
|
||||
/* ============================ 底盘板(从机) ============================ */
|
||||
|
||||
static void c_comm_rx_0x149(uint32_t std_id, const uint8_t rx_data[8], uint32_t dlc)
|
||||
{
|
||||
(void)std_id;
|
||||
(void)dlc;
|
||||
ctoc_remote.rc_r_rl = ((uint16_t)rx_data[0] << 8) | rx_data[1];
|
||||
ctoc_remote.rc_r_ud = ((uint16_t)rx_data[2] << 8) | rx_data[3];
|
||||
ctoc_remote.rc_l_rl = ((uint16_t)rx_data[4] << 8) | rx_data[5];
|
||||
ctoc_remote.rc_l_ud = ((uint16_t)rx_data[6] << 8) | rx_data[7];
|
||||
ctoc_remote.last_tick = HAL_GetTick();
|
||||
}
|
||||
|
||||
static void c_comm_rx_0x189(uint32_t std_id, const uint8_t rx_data[8], uint32_t dlc)
|
||||
{
|
||||
(void)std_id;
|
||||
(void)dlc;
|
||||
ctoc_remote.status = rx_data[0];
|
||||
ctoc_remote.s2 = rx_data[1];
|
||||
ctoc_remote.ctrl_push = rx_data[2];
|
||||
ctoc_remote.shift_push = rx_data[3];
|
||||
ctoc_remote.start_flag = rx_data[4];
|
||||
ctoc_remote.s1 = rx_data[5];
|
||||
ctoc_remote.spin_flag = (rx_data[6] >> 2) & 0x01; // 小陀螺使能位
|
||||
ctoc_remote.gimbal_fault = (rx_data[6] & 0x80) ? 1 : 0; // 云台电机故障位
|
||||
ctoc_remote.last_tick = HAL_GetTick();
|
||||
}
|
||||
|
||||
static void c_comm_rx_0x1a9(uint32_t std_id, const uint8_t rx_data[8], uint32_t dlc)
|
||||
{
|
||||
(void)std_id;
|
||||
(void)dlc;
|
||||
float yaw;
|
||||
memcpy(&yaw, rx_data, 4);
|
||||
ctoc_gimbal_yaw = yaw;
|
||||
ctoc_att_ready = rx_data[4] & 0x01;
|
||||
if (rx_data[4] & 0x04) // 云台自检完成位
|
||||
ctoc_encoder_ccw = (rx_data[4] & 0x02) ? 1 : -1; // tick 增方向
|
||||
ctoc_att_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);
|
||||
}
|
||||
|
||||
void c_comm_slave_send(uint8_t motor_fault)
|
||||
{
|
||||
uint8_t data[8] = {0};
|
||||
data[0] = referee.shooter_power_on; // 发射机构上电状态
|
||||
data[1] = motor_fault ? 0x01 : 0; // 底盘电机故障位(扩展)
|
||||
c_comm_send_frame(&hcan2, CTOC_MASTER_ID1, data);
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user