main
Drew Owens Publish Microgoose Rev A full-stack hardware, CAD, datasheet, and 7-stage RL stack e843249
/*
 * dxl_protocol2.h
 * Dynamixel Protocol 2.0 packet builder, parser, and CRC-16 (0x8005) engine
 * for 15x ROBOTIS XL330-M288-T smart actuators at 1 Mbps half-duplex TTL.
 */

#ifndef MICROGOOSE_DXL_PROTOCOL2_H
#define MICROGOOSE_DXL_PROTOCOL2_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "board_pins.h"

/* Protocol 2.0 Framing Constants */
#define DXL_HEADER_0                    0xFFU
#define DXL_HEADER_1                    0xFFU
#define DXL_HEADER_2                    0xFDU
#define DXL_RESERVED_BYTE               0x00U
#define DXL_BROADCAST_ID                0xFEU

/* Protocol 2.0 Instructions */
#define DXL_INST_PING                   0x01U
#define DXL_INST_READ                   0x02U
#define DXL_INST_WRITE                  0x03U
#define DXL_INST_REG_WRITE              0x04U
#define DXL_INST_ACTION                 0x05U
#define DXL_INST_REBOOT                 0x08U
#define DXL_INST_STATUS                 0x55U
#define DXL_INST_SYNC_READ              0x82U
#define DXL_INST_SYNC_WRITE             0x83U
#define DXL_INST_FAST_SYNC_READ         0x8AU

/* XL330-M288-T Control Table Addresses (Protocol 2.0) */
#define XL330_ADDR_RETURN_DELAY_TIME    9U    /* 1 byte,  2 us/LSB */
#define XL330_ADDR_DRIVE_MODE           10U   /* 1 byte */
#define XL330_ADDR_OPERATING_MODE       11U   /* 1 byte,  3 = Position Control */
#define XL330_ADDR_TORQUE_ENABLE        64U   /* 1 byte,  0/1 */
#define XL330_ADDR_LED                  65U   /* 1 byte,  0/1 */
#define XL330_ADDR_STATUS_RETURN_LEVEL  68U   /* 1 byte */
#define XL330_ADDR_HARDWARE_ERROR       70U   /* 1 byte */
#define XL330_ADDR_POSITION_D_GAIN      80U   /* 2 bytes */
#define XL330_ADDR_POSITION_I_GAIN      82U   /* 2 bytes */
#define XL330_ADDR_POSITION_P_GAIN      84U   /* 2 bytes */
#define XL330_ADDR_GOAL_CURRENT         102U  /* 2 bytes, 1.0 mA/LSB */
#define XL330_ADDR_GOAL_VELOCITY        104U  /* 4 bytes, 0.229 rpm/LSB */
#define XL330_ADDR_GOAL_POSITION        116U  /* 4 bytes, 0..4095 ticks (2048 = 0 rad) */
#define XL330_ADDR_PRESENT_CURRENT      126U  /* 2 bytes, int16, 1.0 mA/LSB (load) */
#define XL330_ADDR_PRESENT_VELOCITY     128U  /* 4 bytes, int32, 0.229 rpm/LSB */
#define XL330_ADDR_PRESENT_POSITION     132U  /* 4 bytes, int32, ticks */
#define XL330_ADDR_PRESENT_VOLTAGE      144U  /* 2 bytes, uint16, 0.1 V/LSB */
#define XL330_ADDR_PRESENT_TEMPERATURE  146U  /* 1 byte,  uint8, 1 degC/LSB */

/* Contiguous telemetry window: Current (2B) + Velocity (4B) + Position (4B) = 10B */
#define XL330_TELEM_START_ADDR          XL330_ADDR_PRESENT_CURRENT
#define XL330_TELEM_BYTE_LEN            10U

/* XL330-M288-T Encoder Conversion Constants */
#define XL330_TICKS_PER_REV             4096
#define XL330_CENTER_TICK               2048
#define XL330_MIN_TICK                  0
#define XL330_MAX_TICK                  4095
#define XL330_RAD_TO_TICKS              651.898646904f
#define XL330_TICKS_TO_RAD              0.00153398078789f
#define XL330_VEL_LSB_TO_RAD_S          0.02398082392f   /* 0.229 rpm * 2*pi / 60 */

/* Maximum Protocol 2.0 packet buffer size for 15 servos */
#define DXL_MAX_PACKET_BYTES            160U

typedef struct {
    uint8_t  id;
    uint8_t  err_flags;
    int16_t  present_load_ma;
    int32_t  present_vel_raw;
    int32_t  present_pos_ticks;
    float    present_pos_rad;
    float    present_vel_rad_s;
    bool     valid;
} dxl_servo_feedback_t;

/* Compute Dynamixel Protocol 2.0 CRC-16 (poly 0x8005, init 0x0000, non-reflected) */
uint16_t dxl_crc16(uint16_t crc_accum, const uint8_t *data, size_t len);
uint16_t dxl_crc16_bitwise_ref(uint16_t crc_accum, const uint8_t *data, size_t len);

/* Radian <-> XL330 tick conversions */
int32_t dxl_rad_to_ticks(float angle_rad);
float   dxl_ticks_to_rad(int32_t ticks);
float   dxl_vel_raw_to_rad_s(int32_t vel_raw);

/* Packet builders (return packet byte length, or 0 on buffer overflow / invalid args) */
size_t dxl_build_ping(uint8_t id, uint8_t *out_buf, size_t max_len);

size_t dxl_build_write_u8(
    uint8_t id,
    uint16_t addr,
    uint8_t value,
    uint8_t *out_buf,
    size_t max_len
);

size_t dxl_build_sync_write_torque(
    const uint8_t *ids,
    const uint8_t *torque_en,
    size_t count,
    uint8_t *out_buf,
    size_t max_len
);

size_t dxl_build_sync_write_goal_pos(
    const uint8_t *ids,
    const int32_t *goal_ticks,
    size_t count,
    uint8_t *out_buf,
    size_t max_len
);

size_t dxl_build_sync_write_goal_rad(
    const uint8_t *ids,
    const float *goal_rad,
    size_t count,
    uint8_t *out_buf,
    size_t max_len
);

size_t dxl_build_sync_read_telemetry(
    const uint8_t *ids,
    size_t count,
    uint8_t *out_buf,
    size_t max_len
);

/* Build a Protocol 2.0 Status (0x55) response frame (used in loopback/unit tests) */
size_t dxl_build_status_telemetry(
    uint8_t id,
    uint8_t err_flags,
    int16_t load_ma,
    int32_t vel_raw,
    int32_t pos_ticks,
    uint8_t *out_buf,
    size_t max_len
);

/*
 * Parse a single Status (0x55) telemetry packet (10-byte payload from addr 126..135).
 * Handles Protocol 2.0 byte unstuffing and CRC-16 verification.
 * Sets *consumed_bytes to number of wire bytes consumed when a frame is found.
 */
bool dxl_parse_status_telemetry(
    const uint8_t *rx_buf,
    size_t rx_len,
    dxl_servo_feedback_t *out_fb,
    size_t *consumed_bytes
);

#endif /* MICROGOOSE_DXL_PROTOCOL2_H */