main
Drew Owens Publish Microgoose Rev A full-stack hardware, CAD, datasheet, and 7-stage RL stack e843249
/*
 * spine_controller.h
 * Deterministic 200 Hz Spine MCU state machine, watchdog, and fault supervisor.
 * Shared between bare-metal STM32G431CB main.c and native gcc host verification.
 */

#ifndef MICROGOOSE_SPINE_CONTROLLER_H
#define MICROGOOSE_SPINE_CONTROLLER_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "board_pins.h"
#include "bmi270_min.h"
#include "dxl_protocol2.h"
#include "host_frame.h"
#include "ina226.h"

typedef struct {
    /* Timing and watchdog state */
    uint32_t uptime_us;
    uint32_t last_host_cmd_us;
    uint32_t overcurrent_accum_us;
    bool     host_ever_connected;

    /* Operating and fault flags */
    uint16_t status_flags;
    bool     torque_enabled;
    bool     estop_latched;
    uint16_t tx_seq;

    /* Commanded servo goals (radians) and LED colors */
    float    goal_pos_rad[MG_TOTAL_SERVO_COUNT];
    uint8_t  led0_rgb[3];
    uint8_t  led1_rgb[3];

    /* Latest measured telemetry */
    uint16_t vbat_mv;
    uint16_t bus_5v_mv;
    int16_t  bus_current_ma;
    uint16_t bus_power_mw;
    bmi270_sample_t torso_imu;
    bmi270_sample_t head_imu;
    uint16_t tof_min_mm;
    uint8_t  tof_grid_4mm[HOST_TOF_ZONES];
    dxl_servo_feedback_t servos[MG_TOTAL_SERVO_COUNT];
} mg_spine_state_t;

/* Canonical DXL ID table [1..15] and default standing angles [rad] */
extern const uint8_t MG_SERVO_DXL_IDS[MG_TOTAL_SERVO_COUNT];
extern const float   MG_SERVO_DEFAULT_RAD[MG_TOTAL_SERVO_COUNT];
extern const float   MG_SERVO_MIN_RAD[MG_TOTAL_SERVO_COUNT];
extern const float   MG_SERVO_MAX_RAD[MG_TOTAL_SERVO_COUNT];

void mg_spine_init(mg_spine_state_t *st);

/* Process a decoded host frame at timestamp now_us */
void mg_spine_handle_host_frame(
    mg_spine_state_t *st,
    const mg_host_decoded_frame_t *frame,
    uint32_t now_us
);

/* Run 200 Hz safety supervisor (host watchdog, undervoltage, overcurrent) */
void mg_spine_supervise(mg_spine_state_t *st, uint32_t now_us, uint32_t dt_us);

/* Populate outgoing 194-byte telemetry struct from current Spine state */
void mg_spine_fill_telemetry(const mg_spine_state_t *st, mg_telemetry_payload_t *out_telem);

#endif /* MICROGOOSE_SPINE_CONTROLLER_H */