2026 remote: tabbed Nextion UI, robot telemetry, servo centre calibration

Protocol v4: CmdPacket grows to 26 bytes (cal_cmd, cal_servo, cal_value);
new 30-byte Telemetry rides back on the nRF24 ack payload (robot state,
IMU, battery, 18 joint angles, packets/s, servo-cal echo). Each end drops
wrong-length packets, so version mismatches still fail closed.

Robot (owner-approved): SERVO_CAL state while disarmed, per-servo centre
trims (+-200 us) in EEPROM, telemetry, battery reading; the IMU estimate
now always runs (leveling still gated by ROBOT GYRO).

Remote: generated HMI (scripts/make_remote_hmi.py) with pages splash,
drive, status, servos, cal, setup, upload; page-aware NextionUI that only
writes the showing page and only what changed; servo calibration and
UPDATE DISPLAY on the touchscreen and serial console; battery module.
The display is on Serial2 (pins 7/8), found on the real remote; the old
sketch's Serial1 was never connected to it.

Sim: RF24 shim supports ack payloads, API version 8
(hxsim_get_ack_payload); sim_smoke checks telemetry and every CAL command.
Unity CmdPacket mirror updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mangavalkandClaude Opus 5.5 committed 2026-11-17 14:42:37 +01:00
1 parent 481610c9fd
commit 0b836af8de
40 files changed
+1463 -281

No files matched your search

+1
View File
@@ -83,6 +83,7 @@ HXSIM_API void hxsim_step(uint32_t dt_us) {
}
HXSIM_API void hxsim_radio_send(const uint8_t* bytes, int32_t len) { simhw::radioSend(bytes, len); }
HXSIM_API int32_t hxsim_get_ack_payload(uint8_t* out, int32_t cap) { return simhw::radioLastAckPayload(out, cap); }
HXSIM_API void hxsim_set_imu(float ax, float ay, float az, float gx, float gy, float gz) {
g_manual_imu = true;
+5 -1
View File
@@ -18,7 +18,7 @@
#endif
// Bump whenever a signature or struct below changes; the C# side checks it.
#define HXSIM_API_VERSION 7
#define HXSIM_API_VERSION 8
#define HXSIM_NUM_LEGS 6
@@ -83,6 +83,10 @@ HXSIM_API void hxsim_get_servos(int32_t* us_18, int32_t* attached_18, uint32_
HXSIM_API void hxsim_get_joints_deg(float* deg_18); // servo pulses decoded through Config.h's per-servo SERVOS table
HXSIM_API void hxsim_get_feet_mm(float* xyz_18); // forward kinematics of those angles, body frame
HXSIM_API void hxsim_get_status(HxStatus* out);
// The robot's telemetry (shared/HexapodProtocol Telemetry) as it came back in
// the ack payload of the last packet received. Returns its length in bytes
// (sizeof(Telemetry)), 0 before the first one.
HXSIM_API int32_t hxsim_get_ack_payload(uint8_t* out, int32_t cap);
// Kinematic body (SimBody.cpp): the body rests on the support plane of its
// feet and grounded feet don't slide. World frame = robot frame at power-on,
// ground at z = 0. Any pointer may be null.
+1 -1
View File
@@ -19,7 +19,7 @@ robot/ (unchanged) ──► PlatformIO + Teensy core ──► Teensy 3.6 firmw
|---|---|
| `analogWrite()` hardware PWM (how `ServoDriver` drives the servos) | the pulse width per pin, worked out from duty, resolution and frequency, then decoded back to angles through `Config.h`'s per-servo `SERVOS` table |
| `Wire` + MPU6050 registers | a register-level fake MPU6050 at 0x68, fed from the simulated body's tilt and rotation |
| `RF24` | a simulated radio: packets are received only if channel, address, data rate and **payload size** match, and the RX FIFO holds 3 |
| `RF24` | a simulated radio: packets are received only if channel, address, data rate and **payload size** match (any length up to 32 once `enableAckPayload()` turns on dynamic payloads, as on the chip), and the RX FIFO holds 3. `writeAckPayload()` goes back with the next packet received: `hxsim_get_ack_payload` returns it (the robot's `Telemetry`) |
| `millis()` / `micros()` / `Serial` | a simulated clock that Unity advances, and captured serial I/O |
`robot_ino.cpp` compiles `robot.ino` itself and exposes read-only views of
+26 -1
View File
@@ -213,6 +213,9 @@ struct AirPacket {
static bool g_radio_present = true;
static std::deque<AirPacket> g_air; // packets waiting at the receiver
static constexpr size_t RX_FIFO_DEPTH = 3;
static std::vector<uint8_t> g_ack_queued; // robot's writeAckPayload(), not sent yet
static bool g_ack_has_queued = false;
static std::vector<uint8_t> g_ack_delivered; // came back with the last packet received
namespace simhw {
void radioSetPresent(bool present) { g_radio_present = present; }
@@ -225,6 +228,16 @@ void radioSend(const uint8_t* bytes, int len) {
memcpy(p.address, HXP_RADIO_ADDRESS, 5);
p.payload.assign(bytes, bytes + len);
g_air.push_back(p);
// The chip acks on reception, carrying whatever ack payload is queued.
if (g_ack_has_queued) {
g_ack_delivered = g_ack_queued;
g_ack_has_queued = false;
}
}
int radioLastAckPayload(uint8_t* out, int cap) {
int n = (int)g_ack_delivered.size() < cap ? (int)g_ack_delivered.size() : cap;
if (n > 0) memcpy(out, g_ack_delivered.data(), n);
return n;
}
} // namespace simhw
@@ -240,7 +253,7 @@ bool RF24::available() {
while (!g_air.empty()) {
const AirPacket& p = g_air.front();
bool match = p.channel == channel_ && p.rate == data_rate_ &&
memcmp(p.address, address_, 5) == 0 && p.payload.size() == payload_size_;
memcmp(p.address, address_, 5) == 0 && (dynamic_ || p.payload.size() == payload_size_);
if (match) return true;
g_air.pop_front();
}
@@ -252,3 +265,15 @@ void RF24::read(void* buf, uint8_t len) {
memcpy(buf, p.payload.data(), len < p.payload.size() ? len : p.payload.size());
g_air.pop_front();
}
uint8_t RF24::getDynamicPayloadSize() { return g_air.empty() ? 0 : (uint8_t)g_air.front().payload.size(); }
bool RF24::writeAckPayload(uint8_t, const void* buf, uint8_t len) {
if (!dynamic_ || len > 32) return false;
const uint8_t* b = static_cast<const uint8_t*>(buf);
g_ack_queued.assign(b, b + len);
g_ack_has_queued = true;
return true;
}
uint8_t RF24::flush_tx() {
g_ack_has_queued = false;
return 0;
}
+3
View File
@@ -32,5 +32,8 @@ void imuSet(float ax, float ay, float az, float gx, float gy, float gz);
void radioSetPresent(bool present);
// Puts one packet "on air" using the protocol's channel/address/data rate.
void radioSend(const uint8_t* bytes, int len);
// The ack payload that came back with the last packet received (what the
// remote would read after write()). Returns its length, 0 if none yet.
int radioLastAckPayload(uint8_t* out, int cap);
} // namespace simhw
+1
View File
@@ -37,6 +37,7 @@ inline double degrees(double rad) { return rad * (180.0 / PI); }
void pinMode(uint8_t pin, uint8_t mode);
void digitalWrite(uint8_t pin, uint8_t value);
void analogWrite(uint8_t pin, int value);
inline int analogRead(uint8_t) { return 0; } // nothing analog is simulated
uint32_t analogWriteResolution(uint32_t bits); // returns the previous resolution, like Teensyduino
void analogWriteFrequency(uint8_t pin, float hz);
+2 -2
View File
@@ -1,6 +1,6 @@
#pragma once
// Not used by robot/ today; here so a future robot-side EEPROM use doesn't
// break the simulation build silently. Backed by RAM, starts erased (0xFF).
// ServoDriver keeps its servo centre trims here. Backed by RAM, starts erased (0xFF),
// so every simulated power-on starts with no trims stored.
#include <Arduino.h>
class SimEEPROM {
+11 -2
View File
@@ -8,8 +8,11 @@ enum rf24_datarate_e { RF24_1MBPS = 0, RF24_2MBPS, RF24_250KBPS };
// A simulated radio. Packets the simulation puts "on air" (hxsim_radio_send)
// are only received if this radio's channel, data rate, address and static
// payload size match the sender's, just like the real nRF24L01. The RX FIFO
// holds 3 packets; extra packets are dropped.
// payload size match the sender's, just like the real nRF24L01 (with dynamic
// payloads - enableAckPayload() - any length up to 32 is received). The RX
// FIFO holds 3 packets; extra packets are dropped. An ack payload queued with
// writeAckPayload() goes back with the next packet received, like the chip's
// auto-ack (hxsim_get_ack_payload shows it).
class RF24 {
public:
RF24(uint16_t ce_pin, uint16_t csn_pin) { (void)ce_pin; (void)csn_pin; }
@@ -27,6 +30,11 @@ public:
bool available();
void read(void* buf, uint8_t len);
bool write(const void*, uint8_t) { return false; } // the robot never transmits
void enableDynamicPayloads() { dynamic_ = true; }
void enableAckPayload() { dynamic_ = true; }
uint8_t getDynamicPayloadSize();
bool writeAckPayload(uint8_t pipe, const void* buf, uint8_t len);
uint8_t flush_tx();
private:
uint8_t payload_size_ = 32;
@@ -35,4 +43,5 @@ private:
uint8_t address_[5] = {};
bool pipe_open_ = false;
bool listening_ = false;
bool dynamic_ = false;
};
+77
View File
@@ -227,6 +227,21 @@ int main() {
snprintf(buf, sizeof(buf), "%u servo writes in 1.5 s (0 = every IK solution was rejected as invalid)", writes);
check(writes > 0, "IK produces valid joint solutions at the stance pose", buf);
printLegs("standing:");
{
Telemetry t;
int len = hxsim_get_ack_payload(reinterpret_cast<uint8_t*>(&t), sizeof(t));
float deg[18]; hxsim_get_joints_deg(deg);
float worst = 0;
for (int i = 0; i < 18; i++) {
float tj = t.joint_deg[i] - (i % 3 == 2 ? TELEMETRY_TIBIA_OFFSET_DEG : 0);
worst = fmaxf(worst, fabsf(tj - deg[i]));
}
snprintf(buf, sizeof(buf), "%d bytes, state %u, status %02X, joints off by up to %.1f deg, %u packets/s",
len, t.state, t.status, worst, t.rx_per_sec);
check(len == (int)sizeof(Telemetry) && t.state == TSTATE_STAND && (t.status & TSTAT_SERVOS_ON) &&
worst < 1.5f && t.rx_per_sec >= 90,
"telemetry comes back in the ack payload (state, servo angles, packet rate)", buf);
}
float feet[18]; hxsim_get_feet_mm(feet);
float worst = 0;
@@ -553,6 +568,68 @@ int main() {
s = status();
check(!s.link_ok && s.robot_state == 0, "link loss (>0.3 s silence) disarms the robot");
// ---- Servo centre calibration (CAL_* commands, only while disarmed) ----
{
auto tele = []() { Telemetry t; hxsim_get_ack_payload(reinterpret_cast<uint8_t*>(&t), sizeof(t)); return t; };
int us[18], att[18]; uint32_t wr[18];
CmdPacket c; // disarmed
c.cal_cmd = CAL_HOLD;
c.cal_servo = 4; // leg 1 (mid-right) femur
run(c, 0.3f);
float deg[18]; hxsim_get_joints_deg(deg);
hxsim_get_servos(us, att, wr);
float worst = 0; int relaxed = 0;
for (int i = 0; i < 18; i++) {
worst = fmaxf(worst, fabsf(deg[i] - (i % 3 == 2 ? -90.0f : 0.0f)));
if (!att[i]) relaxed++;
}
Telemetry t = tele();
snprintf(buf, sizeof(buf), "state %u, %d relaxed, pose off by up to %.1f deg", t.state, relaxed, worst);
check(t.state == TSTATE_SERVO_CAL && relaxed == 0 && worst < 1.0f,
"CAL_HOLD holds every servo at the centre pose (coxa 0, femur 0, tibia -90)", buf);
int us0 = us[4];
c.cal_cmd = CAL_SET; c.cal_value = 50;
run(c, 0.2f);
hxsim_get_servos(us, att, wr);
t = tele();
snprintf(buf, sizeof(buf), "pulse %d -> %d us, echo servo %u trim %d, status %02X", us0, us[4], t.cal_servo, t.cal_trim_us, t.status);
check(abs(us[4] - us0 - 50) <= 1 && t.cal_servo == 4 && t.cal_trim_us == 50 && (t.status & TSTAT_CAL_DIRTY),
"CAL_SET trims the selected servo and the telemetry echoes it", buf);
c.cal_cmd = CAL_SAVE;
run(c, 0.2f);
t = tele();
check((t.status & TSTAT_TRIMS_STORED) && !(t.status & TSTAT_CAL_DIRTY), "CAL_SAVE stores the trims");
c.cal_cmd = CAL_SET; c.cal_value = -120;
run(c, 0.2f);
c.cal_cmd = CAL_REVERT;
run(c, 0.2f);
t = tele();
snprintf(buf, sizeof(buf), "trim %d after revert", t.cal_trim_us);
check(t.cal_trim_us == 50 && !(t.status & TSTAT_CAL_DIRTY), "CAL_REVERT drops unsaved trims", buf);
c.flags = FLAG_ARMED; // arming wins over calibration
run(c, 0.3f);
t = tele();
check(t.state == TSTATE_SIT, "arming while CAL_* is sent leaves SERVO_CAL (robot arms into SIT)");
c.flags = 0; c.cal_cmd = CAL_SET; c.cal_value = 0; // put the trim back for the checks below
run(c, 0.2f);
c.cal_cmd = CAL_SAVE;
run(c, 0.2f);
c.cal_cmd = CAL_NONE;
run(c, 0.2f);
hxsim_get_servos(us, att, wr);
relaxed = 0;
for (int i = 0; i < 18; i++) if (!att[i]) relaxed++;
t = tele();
snprintf(buf, sizeof(buf), "state %u, %d of 18 relaxed, trim %d", t.state, relaxed, t.cal_trim_us);
check(t.state == TSTATE_DISARMED && relaxed == 18 && t.cal_trim_us == 0, "CAL_NONE relaxes the servos again", buf);
runSilent(0.5f);
}
// ---- Protocol mismatch fails closed ----
uint8_t short_payload[16] = {};
short_payload[10] = FLAG_ARMED;