feat(control): 添加选手 FPS 键鼠操作与 C3 用户 UART 输出
This commit is contained in:
43 files changed
+2389
-77
No files matched your search
@@ -0,0 +1,16 @@
|
||||
# 用户 UART 接收示例
|
||||
|
||||
`rmic_control.c` / `.h` 是不依赖 HAL 的 C99 解析器。串口设置、接线和字段定义见[装甲板文档](../../../docs/hardware/armor.md)。示例仅产生操作状态,不直接驱动电机或发射机构。
|
||||
|
||||
接收任务把任意长度的 UART 数据交给 `rmic_receiver_feed`,控制循环每次调用 `rmic_receiver_poll`,再应用其返回的速度比例与发射状态。两个函数须由同一个任务调用,或由调用方保证互斥。`now_ms` 使用持续递增的 32 位毫秒计时器,支持自然回绕。
|
||||
|
||||
启动或连续 100 ms 没收到有效新帧后,输出禁能、四通道归零、发射关闭。恢复时必须先收到完整禁能帧,再接受后续使能帧;这样 C3 重启后序号可以重新从零开始。若接收器在操作中断电重启,需要选手先退出操控再重新开启,使其观察到禁能帧。帧校验用于发现传输损坏,不能认证发送者。
|
||||
|
||||
从此目录运行主机检查:
|
||||
|
||||
```sh
|
||||
clang -std=c99 -Wall -Wextra -Werror rmic_control.c test_receiver.c -o test_receiver
|
||||
./test_receiver
|
||||
```
|
||||
|
||||
Windows 下输出名可写 `test_receiver.exe`。不应把生成的可执行文件提交到仓库;可用 `-o` 输出到仓库 `target/` 下。主机测试覆盖启动杂字节、分段输入、坏帧重同步、乱序、重复、通道边界、超时及计时器回绕;实际 UART 电平和下位机停机行为仍需接线验证。
|
||||
@@ -0,0 +1,107 @@
|
||||
#include "rmic_control.h"
|
||||
#include <string.h>
|
||||
|
||||
static uint16_t read16(const uint8_t *p) {
|
||||
return (uint16_t)((uint16_t)p[0] | ((uint16_t)p[1] << 8));
|
||||
}
|
||||
|
||||
static uint32_t read32(const uint8_t *p) {
|
||||
return (uint32_t)p[0] | ((uint32_t)p[1] << 8) |
|
||||
((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
|
||||
}
|
||||
|
||||
static uint16_t crc16(const uint8_t *bytes, size_t count) {
|
||||
uint16_t crc = UINT16_C(0xffff);
|
||||
size_t i;
|
||||
for (i = 0; i < count; ++i) {
|
||||
unsigned int bit;
|
||||
crc ^= (uint16_t)((uint16_t)bytes[i] << 8);
|
||||
for (bit = 0; bit < 8; ++bit) {
|
||||
crc = (uint16_t)((crc & UINT16_C(0x8000)) ?
|
||||
((uint32_t)crc << 1) ^ UINT32_C(0x1021) : (uint32_t)crc << 1);
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
static void stop(rmic_receiver *receiver) {
|
||||
memset(&receiver->control, 0, sizeof(receiver->control));
|
||||
receiver->synchronized = false;
|
||||
}
|
||||
|
||||
void rmic_receiver_init(rmic_receiver *receiver) {
|
||||
memset(receiver, 0, sizeof(*receiver));
|
||||
}
|
||||
|
||||
rmic_control rmic_receiver_poll(rmic_receiver *receiver, uint32_t now_ms) {
|
||||
if (receiver->synchronized &&
|
||||
(uint32_t)(now_ms - receiver->last_valid_ms) >= RMIC_CONTROL_TIMEOUT_MS) {
|
||||
stop(receiver);
|
||||
}
|
||||
return receiver->control;
|
||||
}
|
||||
|
||||
static bool decode(const uint8_t *bytes, rmic_control *result, uint32_t *sequence) {
|
||||
unsigned int i;
|
||||
uint8_t flags = bytes[8];
|
||||
if (bytes[0] != 0xa5 || bytes[1] != 0x5a || bytes[2] != 1 ||
|
||||
bytes[3] != RMIC_CONTROL_FRAME_SIZE || bytes[9] != 0 || (flags & 0xf8) != 0 ||
|
||||
crc16(bytes, 18) != read16(bytes + 18)) {
|
||||
return false;
|
||||
}
|
||||
memset(result, 0, sizeof(*result));
|
||||
result->enabled = (flags & 1) != 0;
|
||||
result->debug = (flags & 2) != 0;
|
||||
result->fire = (flags & 4) != 0;
|
||||
*sequence = read32(bytes + 4);
|
||||
for (i = 0; i < 4; ++i) {
|
||||
uint16_t raw = read16(bytes + 10 + 2 * i);
|
||||
/* Avoid implementation-defined conversion of values above INT16_MAX. */
|
||||
int32_t value = raw <= INT16_MAX ? (int32_t)raw : (int32_t)raw - 65536;
|
||||
if (value < -1000 || value > 1000 || (!result->enabled && value != 0)) {
|
||||
return false;
|
||||
}
|
||||
result->channel[i] = (int16_t)value;
|
||||
}
|
||||
return result->enabled || (!result->debug && !result->fire);
|
||||
}
|
||||
|
||||
size_t rmic_receiver_feed(rmic_receiver *receiver, const uint8_t *data, size_t length,
|
||||
uint32_t now_ms) {
|
||||
size_t accepted = 0;
|
||||
size_t i;
|
||||
(void)rmic_receiver_poll(receiver, now_ms);
|
||||
for (i = 0; i < length; ++i) {
|
||||
rmic_control next;
|
||||
uint32_t sequence;
|
||||
receiver->buffer[receiver->used++] = data[i];
|
||||
if (receiver->used < RMIC_CONTROL_FRAME_SIZE) {
|
||||
continue;
|
||||
}
|
||||
if (!decode(receiver->buffer, &next, &sequence)) {
|
||||
/* One-byte rescan preserves an embedded prefix after noise or a truncated frame. */
|
||||
memmove(receiver->buffer, receiver->buffer + 1, RMIC_CONTROL_FRAME_SIZE - 1);
|
||||
receiver->used = RMIC_CONTROL_FRAME_SIZE - 1;
|
||||
continue;
|
||||
}
|
||||
receiver->used = 0;
|
||||
if (!receiver->synchronized) {
|
||||
/* Cold boot or timeout: first observe a complete neutral frame.
|
||||
* This also permits the C3 sequence to restart at zero after reboot. */
|
||||
if (next.enabled) {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
uint32_t delta = sequence - receiver->sequence;
|
||||
if (delta == 0 || delta >= UINT32_C(0x80000000)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
receiver->synchronized = true;
|
||||
receiver->sequence = sequence;
|
||||
receiver->last_valid_ms = now_ms;
|
||||
receiver->control = next;
|
||||
++accepted;
|
||||
}
|
||||
return accepted;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#ifndef RMIC_CONTROL_H
|
||||
#define RMIC_CONTROL_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define RMIC_CONTROL_FRAME_SIZE 20u
|
||||
#define RMIC_CONTROL_TIMEOUT_MS 100u
|
||||
|
||||
/* Channel order: forward, strafe-right, yaw-right, pitch-up. Speed: -1000..1000. */
|
||||
typedef struct {
|
||||
int16_t channel[4];
|
||||
bool enabled;
|
||||
bool debug;
|
||||
bool fire;
|
||||
} rmic_control;
|
||||
|
||||
typedef struct {
|
||||
uint8_t buffer[RMIC_CONTROL_FRAME_SIZE];
|
||||
size_t used;
|
||||
bool synchronized;
|
||||
uint32_t sequence;
|
||||
uint32_t last_valid_ms;
|
||||
rmic_control control;
|
||||
} rmic_receiver;
|
||||
|
||||
void rmic_receiver_init(rmic_receiver *receiver);
|
||||
|
||||
/* Call from one task/context only; feed arbitrary UART chunks, never assume frame alignment.
|
||||
* Returns number of accepted fresh frames. Noise/bad/duplicate frames do not renew timeout.
|
||||
* now_ms is a monotonic uint32 clock (wraparound is supported). */
|
||||
size_t rmic_receiver_feed(rmic_receiver *receiver, const uint8_t *data, size_t length,
|
||||
uint32_t now_ms);
|
||||
|
||||
/* Call at least once per control-loop iteration, including when UART is silent.
|
||||
* This function stops all channels/fire after 100ms and requires a neutral frame to resync.
|
||||
* Use only this returned state when applying motor/gimbal/launcher setpoints. */
|
||||
rmic_control rmic_receiver_poll(rmic_receiver *receiver, uint32_t now_ms);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,145 @@
|
||||
#include "rmic_control.h"
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Deliberately independent fixture encoder; compare the golden frame with Rust tests. */
|
||||
static void finish_crc(uint8_t bytes[20]) {
|
||||
unsigned int i, bit;
|
||||
uint32_t crc = 0xffff;
|
||||
for (i = 0; i < 18; ++i) {
|
||||
crc ^= (uint32_t)bytes[i] << 8;
|
||||
for (bit = 0; bit < 8; ++bit) {
|
||||
crc = ((crc << 1) ^ ((crc & 0x8000) ? 0x1021 : 0)) & 0xffff;
|
||||
}
|
||||
}
|
||||
bytes[18] = (uint8_t)crc;
|
||||
bytes[19] = (uint8_t)(crc >> 8);
|
||||
}
|
||||
|
||||
static void frame(uint8_t bytes[20], uint32_t sequence, bool enabled) {
|
||||
unsigned int i;
|
||||
const uint8_t channels[8] = {0xe8, 0x03, 0x18, 0xfc, 0xc8, 0, 0x38, 0xff};
|
||||
memset(bytes, 0, 20);
|
||||
bytes[0] = 0xa5; bytes[1] = 0x5a; bytes[2] = 1; bytes[3] = 20;
|
||||
for (i = 0; i < 4; ++i) bytes[4 + i] = (uint8_t)(sequence >> (8 * i));
|
||||
if (enabled) {
|
||||
bytes[8] = 5;
|
||||
memcpy(bytes + 10, channels, 8);
|
||||
}
|
||||
finish_crc(bytes);
|
||||
}
|
||||
|
||||
static void expect_stopped(rmic_receiver *receiver, uint32_t now) {
|
||||
unsigned int i;
|
||||
rmic_control state = rmic_receiver_poll(receiver, now);
|
||||
assert(!state.enabled && !state.debug && !state.fire);
|
||||
for (i = 0; i < 4; ++i) assert(state.channel[i] == 0);
|
||||
}
|
||||
|
||||
static void startup_noise_chunks_and_timeout(void) {
|
||||
rmic_receiver receiver;
|
||||
uint8_t bytes[20];
|
||||
const uint8_t noise[] = "ESP-ROM:esp32c3 startup\n\xa5\x5a\x00";
|
||||
unsigned int i;
|
||||
rmic_receiver_init(&receiver);
|
||||
frame(bytes, 1, true);
|
||||
assert(rmic_receiver_feed(&receiver, bytes, sizeof(bytes), 0) == 0);
|
||||
expect_stopped(&receiver, 0);
|
||||
assert(rmic_receiver_feed(&receiver, noise, sizeof(noise), 1) == 0);
|
||||
frame(bytes, 2, false);
|
||||
for (i = 0; i < 20; ++i) {
|
||||
assert(rmic_receiver_feed(&receiver, bytes + i, 1, 2) == (i == 19 ? 1u : 0u));
|
||||
}
|
||||
frame(bytes, 3, true);
|
||||
assert(rmic_receiver_feed(&receiver, bytes, 7, 10) == 0);
|
||||
assert(rmic_receiver_feed(&receiver, bytes + 7, 13, 10) == 1);
|
||||
assert(receiver.control.enabled && receiver.control.fire);
|
||||
assert(receiver.control.channel[0] == 1000 && receiver.control.channel[1] == -1000);
|
||||
assert(receiver.control.channel[2] == 200 && receiver.control.channel[3] == -200);
|
||||
assert(rmic_receiver_poll(&receiver, 109).enabled);
|
||||
assert(rmic_receiver_feed(&receiver, bytes, 20, 109) == 0); /* Duplicate cannot renew. */
|
||||
expect_stopped(&receiver, 110);
|
||||
frame(bytes, 4, true);
|
||||
assert(rmic_receiver_feed(&receiver, bytes, 20, 111) == 0);
|
||||
expect_stopped(&receiver, 111);
|
||||
frame(bytes, 0, false); /* Restarted C3. */
|
||||
assert(rmic_receiver_feed(&receiver, bytes, 20, 112) == 1);
|
||||
frame(bytes, 1, true);
|
||||
assert(rmic_receiver_feed(&receiver, bytes, 20, 113) == 1);
|
||||
assert(receiver.control.enabled);
|
||||
}
|
||||
|
||||
static void corrupt_truncated_and_invalid_frames(void) {
|
||||
rmic_receiver receiver;
|
||||
uint8_t bytes[20], good[20];
|
||||
size_t i;
|
||||
rmic_receiver_init(&receiver);
|
||||
frame(bytes, 1, false);
|
||||
assert(rmic_receiver_feed(&receiver, bytes, 20, 0) == 1);
|
||||
frame(good, 2, true);
|
||||
for (i = 0; i < 20; ++i) {
|
||||
memcpy(bytes, good, 20);
|
||||
bytes[i] ^= 1;
|
||||
assert(rmic_receiver_feed(&receiver, bytes, 20, 1) == 0);
|
||||
}
|
||||
memcpy(bytes, good, 20);
|
||||
bytes[8] = 0x85; finish_crc(bytes);
|
||||
assert(rmic_receiver_feed(&receiver, bytes, 20, 2) == 0);
|
||||
memcpy(bytes, good, 20);
|
||||
bytes[9] = 1; finish_crc(bytes);
|
||||
assert(rmic_receiver_feed(&receiver, bytes, 20, 3) == 0);
|
||||
memcpy(bytes, good, 20);
|
||||
bytes[10] = 0xe9; finish_crc(bytes); /* 1001 */
|
||||
assert(rmic_receiver_feed(&receiver, bytes, 20, 4) == 0);
|
||||
memcpy(bytes, good, 20);
|
||||
bytes[8] = 0; finish_crc(bytes); /* Disabled, nonzero channels. */
|
||||
assert(rmic_receiver_feed(&receiver, bytes, 20, 5) == 0);
|
||||
assert(rmic_receiver_feed(&receiver, good, 7, 6) == 0); /* Half frame followed by whole. */
|
||||
assert(rmic_receiver_feed(&receiver, good, 20, 7) == 1);
|
||||
assert(receiver.control.enabled);
|
||||
frame(bytes, 1, false); /* Old stop must not advance sequence or change output. */
|
||||
assert(rmic_receiver_feed(&receiver, bytes, 20, 8) == 0);
|
||||
assert(receiver.control.enabled);
|
||||
expect_stopped(&receiver, 107);
|
||||
}
|
||||
|
||||
static void wrapping_sequence_and_clock(void) {
|
||||
rmic_receiver receiver;
|
||||
uint8_t bytes[20];
|
||||
rmic_receiver_init(&receiver);
|
||||
frame(bytes, UINT32_MAX - 1, false);
|
||||
assert(rmic_receiver_feed(&receiver, bytes, 20, UINT32_MAX - 30) == 1);
|
||||
frame(bytes, UINT32_MAX, true);
|
||||
assert(rmic_receiver_feed(&receiver, bytes, 20, UINT32_MAX - 20) == 1);
|
||||
frame(bytes, 0, true);
|
||||
assert(rmic_receiver_feed(&receiver, bytes, 20, UINT32_MAX - 10) == 1);
|
||||
assert(rmic_receiver_poll(&receiver, 88).enabled);
|
||||
expect_stopped(&receiver, 89);
|
||||
}
|
||||
|
||||
static void rust_golden_frame(void) {
|
||||
const uint8_t golden[20] = {
|
||||
0xa5, 0x5a, 0x01, 0x14, 0x78, 0x56, 0x34, 0x12, 0x05, 0x00,
|
||||
0xe8, 0x03, 0x18, 0xfc, 0xc8, 0x00, 0x38, 0xff, 0x78, 0x60
|
||||
};
|
||||
uint8_t bytes[20];
|
||||
rmic_receiver receiver;
|
||||
rmic_receiver_init(&receiver);
|
||||
frame(bytes, UINT32_C(0x12345677), false);
|
||||
assert(rmic_receiver_feed(&receiver, bytes, 20, 0) == 1);
|
||||
assert(rmic_receiver_feed(&receiver, golden, 20, 1) == 1);
|
||||
assert(receiver.sequence == UINT32_C(0x12345678));
|
||||
assert(receiver.control.enabled && receiver.control.fire && !receiver.control.debug);
|
||||
assert(receiver.control.channel[0] == 1000 && receiver.control.channel[1] == -1000);
|
||||
assert(receiver.control.channel[2] == 200 && receiver.control.channel[3] == -200);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
startup_noise_chunks_and_timeout();
|
||||
corrupt_truncated_and_invalid_frames();
|
||||
wrapping_sequence_and_clock();
|
||||
rust_golden_frame();
|
||||
puts("RMIC UART receiver: all tests passed");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user