feat(gml670x4): 所有协议帧添加 CRC-8/MAXIM 校验

使用 Zephyr crc8() 对帧头之后的载荷计算 CRC,上行发送前填充、
下行接收时校验不通过则丢弃并告警。同步更新协议文档。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-09 14:38:28 +08:00
parent 0d2fd30b3a
commit dc525ea4e7
6 changed files with 61 additions and 29 deletions
+16 -10
View File
@@ -1,6 +1,7 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include <zephyr/sys/crc.h>
/* ─── 帧常量 ─── */
/* 双帧头比单字节魔数更容易在连续字节流里完成包起始判定。 */
@@ -41,9 +42,9 @@
/* 总重低于此值时 CoP 无意义(除零保护),单位 kg */
#define COP_MIN_FORCE_THRESHOLD 0.5f
/* ─── 上行: CoP 帧 (17 bytes, 50 Hz) ───
/* ─── 上行: CoP 帧 (18 bytes, 50 Hz) ───
*
* [AA 55 01 seq flags cop_x(4) cop_y(4) force(4)]
* [AA 55 01 seq flags cop_x(4) cop_y(4) force(4) crc]
*/
struct cop_frame_t {
uint8_t sync0; /* = PROTO_SYNC0 */
@@ -54,6 +55,7 @@ struct cop_frame_t {
float cop_x; /* 压力中心 X (mm),板面坐标系 */
float cop_y; /* 压力中心 Y (mm),板面坐标系 */
float force; /* 总重量 (kg) */
uint8_t crc; /* CRC-8/MAXIM, 校验 type~force */
} __attribute__((packed));
union cop_pkt_t {
@@ -61,9 +63,9 @@ union cop_pkt_t {
uint8_t bytes[sizeof(struct cop_frame_t)];
};
/* ─── 上行: 电机编码器帧 (13 bytes, 预留) ───
/* ─── 上行: 电机编码器帧 (14 bytes, 预留) ───
*
* [AA 55 02 seq flags cable_length(4) velocity(4)]
* [AA 55 02 seq flags cable_length(4) velocity(4) crc]
*/
struct encoder_frame_t {
uint8_t sync0; /* = PROTO_SYNC0 */
@@ -73,6 +75,7 @@ struct encoder_frame_t {
uint8_t flags; /* 预留,填 0 */
float cable_length; /* 拉索长度 (mm) */
float velocity; /* 拉索速度 (mm/s) */
uint8_t crc; /* CRC-8/MAXIM */
} __attribute__((packed));
union encoder_pkt_t {
@@ -80,9 +83,9 @@ union encoder_pkt_t {
uint8_t bytes[sizeof(struct encoder_frame_t)];
};
/* ─── 下行: 阻力参数帧 (15 bytes) ───
/* ─── 下行: 阻力参数帧 (16 bytes) ───
*
* [AA 55 10 K(4) B(4) Tau(4)]
* [AA 55 10 K(4) B(4) Tau(4) crc]
*/
struct resistance_frame_t {
uint8_t sync0; /* = PROTO_SYNC0 */
@@ -91,6 +94,7 @@ struct resistance_frame_t {
float K; /* 刚度 (N/m) */
float B; /* 阻尼 (Ns/m) */
float Tau; /* 时间常数 (s) */
uint8_t crc; /* CRC-8/MAXIM */
} __attribute__((packed));
union resistance_pkt_t {
@@ -98,9 +102,9 @@ union resistance_pkt_t {
uint8_t bytes[sizeof(struct resistance_frame_t)];
};
/* ─── 下行: Spotter Mode 帧 (8 bytes) ───
/* ─── 下行: Spotter Mode 帧 (9 bytes) ───
*
* [AA 55 11 threshold(4) enable]
* [AA 55 11 threshold(4) enable crc]
*/
struct spotter_frame_t {
uint8_t sync0; /* = PROTO_SYNC0 */
@@ -108,6 +112,7 @@ struct spotter_frame_t {
uint8_t type; /* = PROTO_TYPE_SPOTTER */
float threshold; /* 保护力阈值 (N) */
uint8_t enable; /* 0 = 关闭, 1 = 启用 */
uint8_t crc; /* CRC-8/MAXIM */
} __attribute__((packed));
union spotter_pkt_t {
@@ -115,15 +120,16 @@ union spotter_pkt_t {
uint8_t bytes[sizeof(struct spotter_frame_t)];
};
/* ─── 下行: 心跳帧 (4 bytes, ~1 Hz) ───
/* ─── 下行: 心跳帧 (5 bytes, ~1 Hz) ───
*
* [AA 55 20 counter]
* [AA 55 20 counter crc]
*/
struct heartbeat_frame_t {
uint8_t sync0; /* = PROTO_SYNC0 */
uint8_t sync1; /* = PROTO_SYNC1 */
uint8_t type; /* = PROTO_TYPE_HEARTBEAT */
uint8_t counter; /* 滚动计数,用于活性检测 */
uint8_t crc; /* CRC-8/MAXIM */
} __attribute__((packed));
union heartbeat_pkt_t {