Implements a speed-dependent PID gain scheduler that interpolates Kp/Ki/Kd across a configurable table of velocity breakpoints, replacing the fixed single-gain PID used previously. Changes: - include/pid_flash.h: add pid_sched_entry_t (16-byte entry), pid_sched_flash_t (128-byte record at 0x0807FF40), pid_flash_load_schedule(), pid_flash_save_all() (atomic single-sector erase for both schedule and single-PID records) - src/pid_flash.c: implement load_schedule and save_all; single erase covers both records at 0x0807FF40 (schedule) and 0x0807FFC0 (single PID) - include/pid_schedule.h: API header -- init, get_gains, apply, set/get table, flash_save, active_band_idx, get_default_table - src/pid_schedule.c: linear interpolation between sorted speed-band entries; integrator reset on band transition; default 3-band table (0/0.3/0.8 m/s) - include/jlink.h: add SCHED_GET (0x0C), SCHED_SET (0x0D), SCHED_SAVE (0x0E) commands; TLM_SCHED (0x85); jlink_tlm_sched_t; JLinkSchedSetBuf; sched_get_req, sched_save_req fields in JLinkState; include pid_flash.h - src/jlink.c: dispatch SCHED_GET/SET/SAVE; implement jlink_send_sched_telemetry, jlink_get_sched_set; add JLinkSchedSetBuf static buffer - test/test_pid_schedule.c: 48 unit tests -- all passing (gcc host build) Flash layout (sector 7): 0x0807FF40 pid_sched_flash_t (128 bytes) -- schedule 0x0807FFC0 pid_flash_t ( 64 bytes) -- single PID (existing) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
209 lines
9.7 KiB
C
209 lines
9.7 KiB
C
#ifndef JLINK_H
|
|
#define JLINK_H
|
|
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include "pid_flash.h" /* pid_sched_entry_t, PID_SCHED_MAX_BANDS */
|
|
|
|
/*
|
|
* JLink -- Jetson serial binary protocol over USART1 (PB6=TX, PB7=RX).
|
|
*
|
|
* Issue #120: replaces jetson_cmd ASCII-over-USB-CDC with a dedicated
|
|
* hardware UART at 921600 baud using DMA circular RX and IDLE interrupt.
|
|
*
|
|
* Frame format (both directions):
|
|
* [STX=0x02][LEN][CMD][PAYLOAD...][CRC16_hi][CRC16_lo][ETX=0x03]
|
|
*
|
|
* STX : frame start sentinel (0x02)
|
|
* LEN : count of CMD + PAYLOAD bytes (1 + payload_len)
|
|
* CMD : command/telemetry type byte
|
|
* PAYLOAD: 0..N bytes depending on CMD
|
|
* CRC16 : CRC16-XModem over CMD+PAYLOAD (poly 0x1021, init 0), big-endian
|
|
* ETX : frame end sentinel (0x03)
|
|
*
|
|
* Jetson to STM32 commands:
|
|
* 0x01 HEARTBEAT - no payload; refreshes heartbeat timer
|
|
* 0x02 DRIVE - int16 speed (-1000..+1000), int16 steer (-1000..+1000)
|
|
* 0x03 ARM - no payload; request arm (same interlock as CDC 'A')
|
|
* 0x04 DISARM - no payload; disarm immediately
|
|
* 0x05 PID_SET - float kp, float ki, float kd (12 bytes, IEEE-754 LE)
|
|
* 0x06 DFU_ENTER - no payload; request OTA DFU reboot (denied while armed)
|
|
* 0x07 ESTOP - no payload; engage emergency stop
|
|
* 0x08 AUDIO - int16 PCM samples (up to 126 samples)
|
|
* 0x09 SLEEP - no payload; request STOP-mode sleep
|
|
* 0x0A PID_SAVE - no payload; save current Kp/Ki/Kd to flash (Issue #531)
|
|
* 0x0C SCHED_GET - no payload; reply with TLM_SCHED (Issue #550)
|
|
* 0x0D SCHED_SET - uint8 num_bands + N*16-byte pid_sched_entry_t (Issue #550)
|
|
* 0x0E SCHED_SAVE - float kp, ki, kd (12 bytes); save sched+single to flash (Issue #550)
|
|
*
|
|
* STM32 to Jetson telemetry:
|
|
* 0x80 STATUS - jlink_tlm_status_t (20 bytes), sent at JLINK_TLM_HZ
|
|
* 0x81 POWER - jlink_tlm_power_t (11 bytes), sent at PM_TLM_HZ
|
|
* 0x82 BATTERY - jlink_tlm_battery_t (10 bytes, Issue #533)
|
|
* 0x83 PID_RESULT - jlink_tlm_pid_result_t (13 bytes), sent after PID_SAVE (Issue #531)
|
|
* 0x85 SCHED - jlink_tlm_sched_t (1+N*16 bytes), sent on SCHED_GET (Issue #550)
|
|
*
|
|
* Priority: CRSF RC always takes precedence. Jetson steer/speed only applied
|
|
* when mode_manager_active() == MODE_AUTONOMOUS (CH6 high). In RC_MANUAL and
|
|
* RC_ASSISTED modes the Jetson speed offset and steer are injected via
|
|
* mode_manager_set_auto_cmd() and blended per the existing blend ramp.
|
|
*
|
|
* Heartbeat: if no valid frame arrives within JLINK_HB_TIMEOUT_MS (1000ms),
|
|
* jlink_is_active() returns false and the main loop clears the auto command.
|
|
*/
|
|
|
|
/* ---- Frame constants ---- */
|
|
#define JLINK_STX 0x02u
|
|
#define JLINK_ETX 0x03u
|
|
|
|
/* ---- Command IDs (Jetson to STM32) ---- */
|
|
#define JLINK_CMD_HEARTBEAT 0x01u
|
|
#define JLINK_CMD_DRIVE 0x02u
|
|
#define JLINK_CMD_ARM 0x03u
|
|
#define JLINK_CMD_DISARM 0x04u
|
|
#define JLINK_CMD_PID_SET 0x05u
|
|
#define JLINK_CMD_DFU_ENTER 0x06u
|
|
#define JLINK_CMD_ESTOP 0x07u
|
|
#define JLINK_CMD_AUDIO 0x08u /* PCM audio chunk: int16 samples, up to 126 */
|
|
#define JLINK_CMD_SLEEP 0x09u /* no payload; request STOP-mode sleep */
|
|
#define JLINK_CMD_PID_SAVE 0x0Au /* no payload; save Kp/Ki/Kd to flash (Issue #531) */
|
|
#define JLINK_CMD_SCHED_GET 0x0Cu /* no payload; reply TLM_SCHED (Issue #550) */
|
|
#define JLINK_CMD_SCHED_SET 0x0Du /* uint8 num_bands + N*16-byte entries (Issue #550) */
|
|
#define JLINK_CMD_SCHED_SAVE 0x0Eu /* float kp,ki,kd; save sched+single to flash (Issue #550) */
|
|
|
|
/* ---- Telemetry IDs (STM32 to Jetson) ---- */
|
|
#define JLINK_TLM_STATUS 0x80u
|
|
#define JLINK_TLM_POWER 0x81u /* jlink_tlm_power_t (11 bytes) */
|
|
#define JLINK_TLM_BATTERY 0x82u /* jlink_tlm_battery_t (10 bytes, Issue #533) */
|
|
#define JLINK_TLM_PID_RESULT 0x83u /* jlink_tlm_pid_result_t (13 bytes, Issue #531) */
|
|
#define JLINK_TLM_SCHED 0x85u /* jlink_tlm_sched_t (1+N*16 bytes, Issue #550) */
|
|
|
|
/* ---- Telemetry STATUS payload (20 bytes, packed) ---- */
|
|
typedef struct __attribute__((packed)) {
|
|
int16_t pitch_x10; /* pitch degrees x10 */
|
|
int16_t roll_x10; /* roll degrees x10 */
|
|
int16_t yaw_x10; /* yaw degrees x10 (gyro-integrated) */
|
|
int16_t motor_cmd; /* ESC output -1000..+1000 */
|
|
uint16_t vbat_mv; /* battery millivolts */
|
|
int8_t rssi_dbm; /* CRSF RSSI (dBm, negative) */
|
|
uint8_t link_quality; /* CRSF LQ 0-100 */
|
|
uint8_t balance_state; /* 0=DISARMED, 1=ARMED, 2=TILT_FAULT */
|
|
uint8_t rc_armed; /* crsf_state.armed (1=armed) */
|
|
uint8_t mode; /* robot_mode_t: 0=RC_MANUAL,1=ASSISTED,2=AUTONOMOUS */
|
|
uint8_t estop; /* EstopSource value */
|
|
uint8_t soc_pct; /* state-of-charge 0-100, 255=unknown */
|
|
uint8_t fw_major;
|
|
uint8_t fw_minor;
|
|
uint8_t fw_patch;
|
|
} jlink_tlm_status_t; /* 20 bytes */
|
|
|
|
/* ---- Telemetry POWER payload (11 bytes, packed) ---- */
|
|
typedef struct __attribute__((packed)) {
|
|
uint8_t power_state; /* PowerState: 0=ACTIVE,1=SLEEP_PENDING,2=SLEEPING,3=WAKING */
|
|
uint16_t est_total_ma; /* estimated total current draw (mA) */
|
|
uint16_t est_audio_ma; /* estimated I2S3+amp current (mA); 0 if gated */
|
|
uint16_t est_osd_ma; /* estimated OSD SPI2 current (mA); 0 if gated */
|
|
uint32_t idle_ms; /* ms since last cmd_vel activity */
|
|
} jlink_tlm_power_t; /* 11 bytes */
|
|
|
|
/* ---- Telemetry BATTERY payload (10 bytes, packed) Issue #533 ---- */
|
|
typedef struct __attribute__((packed)) {
|
|
uint16_t vbat_mv; /* DMA-sampled LPF-filtered Vbat (mV) */
|
|
int16_t ibat_ma; /* DMA-sampled LPF-filtered Ibat (mA, + = discharge) */
|
|
uint16_t vbat_raw_mv; /* unfiltered last-tick average (mV) */
|
|
uint8_t flags; /* bit0=low, bit1=critical, bit2=4S, bit3=adc_ready */
|
|
int8_t cal_offset; /* vbat_offset_mv / 4 (+-127 -> +-508 mV) */
|
|
uint8_t lpf_shift; /* IIR shift factor (alpha = 1/2^lpf_shift) */
|
|
uint8_t soc_pct; /* voltage-based SoC 0-100, 255 = unknown */
|
|
} jlink_tlm_battery_t; /* 10 bytes */
|
|
|
|
/* ---- Telemetry PID_RESULT payload (13 bytes, packed) Issue #531 ---- */
|
|
/* Sent after JLINK_CMD_PID_SAVE is processed; confirms gains written to flash. */
|
|
typedef struct __attribute__((packed)) {
|
|
float kp; /* Kp saved */
|
|
float ki; /* Ki saved */
|
|
float kd; /* Kd saved */
|
|
uint8_t saved_ok; /* 1 = flash write verified, 0 = write failed */
|
|
} jlink_tlm_pid_result_t; /* 13 bytes */
|
|
|
|
/* ---- Telemetry SCHED payload (1 + N*16 bytes, packed) Issue #550 ---- */
|
|
/* Sent in response to JLINK_CMD_SCHED_GET; N = num_bands (1..PID_SCHED_MAX_BANDS). */
|
|
typedef struct __attribute__((packed)) {
|
|
uint8_t num_bands; /* number of valid entries */
|
|
pid_sched_entry_t bands[PID_SCHED_MAX_BANDS]; /* up to 6 x 16 = 96 bytes */
|
|
} jlink_tlm_sched_t; /* 1 + 96 = 97 bytes max */
|
|
|
|
/* ---- Volatile state (read from main loop) ---- */
|
|
typedef struct {
|
|
/* Drive command - updated on JLINK_CMD_DRIVE */
|
|
volatile int16_t speed; /* -1000..+1000 */
|
|
volatile int16_t steer; /* -1000..+1000 */
|
|
|
|
/* Heartbeat timer - updated on any valid frame */
|
|
volatile uint32_t last_rx_ms; /* HAL_GetTick() of last valid frame; 0=none */
|
|
|
|
/* One-shot request flags - set by parser, cleared by main loop */
|
|
volatile uint8_t arm_req;
|
|
volatile uint8_t disarm_req;
|
|
volatile uint8_t estop_req;
|
|
|
|
/* PID update - set by parser, cleared by main loop */
|
|
volatile uint8_t pid_updated;
|
|
volatile float pid_kp;
|
|
volatile float pid_ki;
|
|
volatile float pid_kd;
|
|
|
|
/* DFU reboot request - set by parser, cleared by main loop */
|
|
volatile uint8_t dfu_req;
|
|
/* Sleep request - set by JLINK_CMD_SLEEP, cleared by main loop */
|
|
volatile uint8_t sleep_req;
|
|
/* PID save request - set by JLINK_CMD_PID_SAVE, cleared by main loop (Issue #531) */
|
|
volatile uint8_t pid_save_req;
|
|
|
|
/* PID schedule commands (Issue #550) - set by parser, cleared by main loop */
|
|
volatile uint8_t sched_get_req; /* SCHED_GET: main loop calls jlink_send_sched_telemetry() */
|
|
volatile uint8_t sched_save_req; /* SCHED_SAVE: main loop calls pid_schedule_flash_save() */
|
|
volatile float sched_save_kp; /* kp for single-PID record in SCHED_SAVE */
|
|
volatile float sched_save_ki;
|
|
volatile float sched_save_kd;
|
|
} JLinkState;
|
|
|
|
extern volatile JLinkState jlink_state;
|
|
|
|
/* ---- SCHED_SET receive buffer -- Issue #550 ---- */
|
|
/*
|
|
* Populated by the parser on JLINK_CMD_SCHED_SET. Main loop reads via
|
|
* jlink_get_sched_set() and calls pid_schedule_set_table() before clearing.
|
|
*/
|
|
typedef struct {
|
|
volatile uint8_t ready; /* set by parser, cleared by main loop */
|
|
volatile uint8_t num_bands;
|
|
pid_sched_entry_t bands[PID_SCHED_MAX_BANDS]; /* copied from frame */
|
|
} JLinkSchedSetBuf;
|
|
|
|
/* ---- API ---- */
|
|
|
|
void jlink_init(void);
|
|
bool jlink_is_active(uint32_t now_ms);
|
|
void jlink_process(void);
|
|
void jlink_send_telemetry(const jlink_tlm_status_t *status);
|
|
void jlink_send_power_telemetry(const jlink_tlm_power_t *power);
|
|
void jlink_send_battery_telemetry(const jlink_tlm_battery_t *batt);
|
|
void jlink_send_pid_result(const jlink_tlm_pid_result_t *result);
|
|
|
|
/*
|
|
* jlink_send_sched_telemetry(tlm) - transmit JLINK_TLM_SCHED (0x85) in
|
|
* response to SCHED_GET. tlm->num_bands determines actual frame size.
|
|
* Issue #550.
|
|
*/
|
|
void jlink_send_sched_telemetry(const jlink_tlm_sched_t *tlm);
|
|
|
|
/*
|
|
* jlink_get_sched_set() - return pointer to the most-recently received
|
|
* SCHED_SET payload buffer (static storage in jlink.c). Main loop calls
|
|
* pid_schedule_set_table() from this buffer, then clears ready. Issue #550.
|
|
*/
|
|
JLinkSchedSetBuf *jlink_get_sched_set(void);
|
|
|
|
#endif /* JLINK_H */
|