diff options
| author | BEAST <macbook@Macbooks-MacBook-Pro-2.local> | 2026-11-16 19:23:09 +0530 |
|---|---|---|
| committer | BEAST <macbook@Macbooks-MacBook-Pro-2.local> | 2026-11-16 19:23:09 +0530 |
| commit | 7764125350e66e464dded24ca8cf69a7e1ecef8d (patch) | |
| tree | e01f8b3a15fe905d6e5ccc3da6822df238b49030 /lib | |
Add local project files
Diffstat (limited to 'lib')
32 files changed, 1741 insertions, 0 deletions
diff --git a/lib/HexaHAL/build-manifest.json b/lib/HexaHAL/build-manifest.json new file mode 100644 index 0000000..c7c0a85 --- /dev/null +++ b/lib/HexaHAL/build-manifest.json @@ -0,0 +1,72 @@ +{ + "source_lib": "lib\\HexaHAL", + "sdk_version": "1.4.1", + "built_from_commit": "036661d7fa9340e198bf3f6d376121ae8c214f59", + "built_at": "2026-10-10T14:37:17.742212Z", + "harvested_from_project": "RobotFirmware", + "harvested_env": "robot", + "cxx_path": "C:\\Users\\thein\\.platformio\\packages\\toolchain-xtensa-esp32s3\\bin\\xtensa-esp32s3-elf-g++.exe", + "cxx_flags": [ + "-std=gnu++17", + "-fexceptions", + "-fno-rtti", + "-Os", + "-mlongcalls", + "-ffunction-sections", + "-fdata-sections", + "-Wno-error=unused-function", + "-Wno-error=unused-variable", + "-Wno-error=deprecated-declarations", + "-Wno-unused-parameter", + "-Wno-sign-compare", + "-ggdb", + "-freorder-blocks", + "-Wwrite-strings", + "-fstack-protector", + "-fstrict-volatile-bitfields", + "-Wno-error=unused-but-set-variable", + "-fno-jump-tables", + "-fno-tree-switch-conversion", + "-MMD", + "-g0", + "-fno-rtti", + "-fno-exceptions", + "-fno-asynchronous-unwind-tables" + ], + "defines": [ + "PLATFORMIO=60200", + "ARDUINO_ESP32S3_DEV", + "ARDUINO_USB_MODE=1", + "ARDUINO_RUNNING_CORE=1", + "ARDUINO_EVENT_RUNNING_CORE=1", + "CONFIG_FREERTOS_HZ=1000", + "CORE_DEBUG_LEVEL=3", + "ARDUINO_USB_MODE=1", + "ARDUINO_USB_CDC_ON_BOOT=1", + "HAVE_CONFIG_H", + "MBEDTLS_CONFIG_FILE=\"mbedtls/esp_config.h\"", + "UNITY_INCLUDE_CONFIG_H", + "WITH_POSIX", + "_GNU_SOURCE", + "IDF_VER=\"v4.4.7-dirty\"", + "ESP_PLATFORM", + "_POSIX_READER_WRITER_LOCKS", + "ARDUINO_ARCH_ESP32", + "ESP32", + "F_CPU=240000000L", + "ARDUINO=10812", + "ARDUINO_VARIANT=\"esp32s3\"", + "ARDUINO_BOARD=\"Espressif ESP32-S3-DevKitC-1-N8 (8 MB QD, No PSRAM)\"", + "ARDUINO_PARTITION_partitions" + ], + "hardening": { + "extra_cxx_flags": [ + "-g0", + "-fno-rtti", + "-fno-exceptions", + "-fno-asynchronous-unwind-tables" + ], + "symbols_stripped": true, + "strings_encrypted": true + } +}
\ No newline at end of file diff --git a/lib/HexaHAL/library.json b/lib/HexaHAL/library.json new file mode 100644 index 0000000..a9340c2 --- /dev/null +++ b/lib/HexaHAL/library.json @@ -0,0 +1,15 @@ +{ + "name": "HexaHAL", + "version": "1.0.0", + "description": "Hardware abstraction interfaces for HEXA COMMAND HUB", + "keywords": ["hal", "abstraction", "interface", "esp32", "hexa"], + "authors": [ + { + "name": "Rush", + "maintainer": true + } + ], + "license": "MIT", + "platforms": "*", + "frameworks": "arduino" +} diff --git a/lib/HexaHAL/library.properties b/lib/HexaHAL/library.properties new file mode 100644 index 0000000..d7f65ec --- /dev/null +++ b/lib/HexaHAL/library.properties @@ -0,0 +1,9 @@ +name=HexaHAL +version=1.0.0 +author=Rush +maintainer=Rush +sentence=Hardware abstraction interfaces for HEXA COMMAND HUB +paragraph=Pure abstract interfaces (IServo, IMotor, ISensor) that define the contract between application code and hardware drivers. Contains zero implementation code. +category=Device Control +url= +architectures=* diff --git a/lib/HexaIMU/build-manifest.json b/lib/HexaIMU/build-manifest.json new file mode 100644 index 0000000..a1ca16b --- /dev/null +++ b/lib/HexaIMU/build-manifest.json @@ -0,0 +1,72 @@ +{ + "source_lib": "lib\\HexaIMU", + "sdk_version": "1.4.1", + "built_from_commit": "036661d7fa9340e198bf3f6d376121ae8c214f59", + "built_at": "2026-10-10T14:37:09.526196Z", + "harvested_from_project": "RobotFirmware", + "harvested_env": "robot", + "cxx_path": "C:\\Users\\thein\\.platformio\\packages\\toolchain-xtensa-esp32s3\\bin\\xtensa-esp32s3-elf-g++.exe", + "cxx_flags": [ + "-std=gnu++17", + "-fexceptions", + "-fno-rtti", + "-Os", + "-mlongcalls", + "-ffunction-sections", + "-fdata-sections", + "-Wno-error=unused-function", + "-Wno-error=unused-variable", + "-Wno-error=deprecated-declarations", + "-Wno-unused-parameter", + "-Wno-sign-compare", + "-ggdb", + "-freorder-blocks", + "-Wwrite-strings", + "-fstack-protector", + "-fstrict-volatile-bitfields", + "-Wno-error=unused-but-set-variable", + "-fno-jump-tables", + "-fno-tree-switch-conversion", + "-MMD", + "-g0", + "-fno-rtti", + "-fno-exceptions", + "-fno-asynchronous-unwind-tables" + ], + "defines": [ + "PLATFORMIO=60200", + "ARDUINO_ESP32S3_DEV", + "ARDUINO_USB_MODE=1", + "ARDUINO_RUNNING_CORE=1", + "ARDUINO_EVENT_RUNNING_CORE=1", + "CONFIG_FREERTOS_HZ=1000", + "CORE_DEBUG_LEVEL=3", + "ARDUINO_USB_MODE=1", + "ARDUINO_USB_CDC_ON_BOOT=1", + "HAVE_CONFIG_H", + "MBEDTLS_CONFIG_FILE=\"mbedtls/esp_config.h\"", + "UNITY_INCLUDE_CONFIG_H", + "WITH_POSIX", + "_GNU_SOURCE", + "IDF_VER=\"v4.4.7-dirty\"", + "ESP_PLATFORM", + "_POSIX_READER_WRITER_LOCKS", + "ARDUINO_ARCH_ESP32", + "ESP32", + "F_CPU=240000000L", + "ARDUINO=10812", + "ARDUINO_VARIANT=\"esp32s3\"", + "ARDUINO_BOARD=\"Espressif ESP32-S3-DevKitC-1-N8 (8 MB QD, No PSRAM)\"", + "ARDUINO_PARTITION_partitions" + ], + "hardening": { + "extra_cxx_flags": [ + "-g0", + "-fno-rtti", + "-fno-exceptions", + "-fno-asynchronous-unwind-tables" + ], + "symbols_stripped": true, + "strings_encrypted": true + } +}
\ No newline at end of file diff --git a/lib/HexaIMU/keywords.txt b/lib/HexaIMU/keywords.txt new file mode 100644 index 0000000..9fa0d38 --- /dev/null +++ b/lib/HexaIMU/keywords.txt @@ -0,0 +1,43 @@ +# ═══════════════════════════════════════════════ +# Syntax Highlighting for HexaIMU Library +# ═══════════════════════════════════════════════ +# KEYWORD1 = Classes (highlighted as types) +# KEYWORD2 = Methods (highlighted as functions) +# LITERAL1 = Constants / enum values + +# ── Classes ── +HexaIMU KEYWORD1 +HexaIMUConfig KEYWORD1 +HexaIMUData KEYWORD1 + +# ── Methods ── +begin KEYWORD2 +isReady KEYWORD2 +read KEYWORD2 +getData KEYWORD2 +getAccelX KEYWORD2 +getAccelY KEYWORD2 +getAccelZ KEYWORD2 +getGyroX KEYWORD2 +getGyroY KEYWORD2 +getGyroZ KEYWORD2 +getTemp KEYWORD2 +setAccelRange KEYWORD2 +getAccelRange KEYWORD2 +setGyroRange KEYWORD2 +getGyroRange KEYWORD2 +tick KEYWORD2 +getHeading KEYWORD2 +isHeadingReady KEYWORD2 +zeroHeading KEYWORD2 +resetHeading KEYWORD2 + +# ── Enum Constants ── +HEXA_ACCEL_RANGE_2G LITERAL1 +HEXA_ACCEL_RANGE_4G LITERAL1 +HEXA_ACCEL_RANGE_8G LITERAL1 +HEXA_ACCEL_RANGE_16G LITERAL1 +HEXA_GYRO_RANGE_250_DPS LITERAL1 +HEXA_GYRO_RANGE_500_DPS LITERAL1 +HEXA_GYRO_RANGE_1000_DPS LITERAL1 +HEXA_GYRO_RANGE_2000_DPS LITERAL1 diff --git a/lib/HexaIMU/lib/xtensa-esp32s3/libHexaIMU_static.a b/lib/HexaIMU/lib/xtensa-esp32s3/libHexaIMU_static.a Binary files differnew file mode 100644 index 0000000..9cf8556 --- /dev/null +++ b/lib/HexaIMU/lib/xtensa-esp32s3/libHexaIMU_static.a diff --git a/lib/HexaIMU/library.json b/lib/HexaIMU/library.json new file mode 100644 index 0000000..b675b51 --- /dev/null +++ b/lib/HexaIMU/library.json @@ -0,0 +1,15 @@ +{ + "name": "HexaIMU", + "version": "2.0.0", + "description": "LSM6DSOX IMU driver for HEXA COMMAND HUB", + "keywords": ["imu", "lsm6dsox", "accelerometer", "gyroscope", "heading", "esp32", "hexa"], + "authors": [ + { + "name": "Rush", + "maintainer": true + } + ], + "license": "MIT", + "platforms": "espressif32", + "frameworks": "arduino" +} diff --git a/lib/HexaIMU/library.properties b/lib/HexaIMU/library.properties new file mode 100644 index 0000000..7e44d97 --- /dev/null +++ b/lib/HexaIMU/library.properties @@ -0,0 +1,10 @@ +name=HexaIMU +version=2.0.0 +author=Rush +maintainer=Rush +sentence=LSM6DSOX IMU driver for HEXA COMMAND HUB +paragraph=Thin wrapper around Adafruit_LSM6DSOX matching the HexaDCMotor/HexaServo/HexaOLED object idiom. Reads accelerometer (m/s2), gyroscope (rad/s), and temperature (degC), plus a non-blocking tick()-driven heading API (gyro-Z bias calibration + integration) so students never need a blocking calibration loop in init(). +category=Sensors +url= +architectures=esp32 +depends=Adafruit LSM6DS, Adafruit Unified Sensor diff --git a/lib/HexaIMU/src/HexaIMU.h b/lib/HexaIMU/src/HexaIMU.h new file mode 100644 index 0000000..7be5527 --- /dev/null +++ b/lib/HexaIMU/src/HexaIMU.h @@ -0,0 +1,164 @@ +/** + * HexaIMU.h — LSM6DSOX IMU Driver for HEXA COMMAND HUB + * + * Thin wrapper around Adafruit_LSM6DSOX — the LSM6DSOX is the IMU actually + * fitted on the robot board (I2C 0x6B), and Adafruit_LSM6DSOX is already a + * required dependency of this firmware (see NRLComms.cpp), so wrapping it + * here adds no new dependency. The point of this class is the same simple + * "declare an object, call its methods" idiom used by HexaDCMotor/HexaServo/ + * HexaOLED, plus a non-blocking heading API students can't get wrong. + * + * Chip: ST LSM6DSOX + * Part of: HexaIMU library + * Author: Rush / HEXA Robotics + */ + +#pragma once + +#include <Arduino.h> +#include <Wire.h> +#include <Adafruit_LSM6DSOX.h> + + +// Enumerations — simplified names over the underlying Adafruit_LSM6DSOX enums. + +typedef enum { + HEXA_ACCEL_RANGE_2G = 0, // ±2g (highest precision) + HEXA_ACCEL_RANGE_4G = 1, // ±4g (default) + HEXA_ACCEL_RANGE_8G = 2, // ±8g + HEXA_ACCEL_RANGE_16G = 3, // ±16g +} hexa_accel_range_t; + +typedef enum { + HEXA_GYRO_RANGE_250_DPS = 0, // ±250 deg/s (default) + HEXA_GYRO_RANGE_500_DPS = 1, // ±500 deg/s + HEXA_GYRO_RANGE_1000_DPS = 2, // ±1000 deg/s + HEXA_GYRO_RANGE_2000_DPS = 3, // ±2000 deg/s +} hexa_gyro_range_t; + + +// Data struct + +/** + * All IMU readings from one read() call. + */ +struct HexaIMUData { + float accelX; // Acceleration X (m/s²) + float accelY; // Acceleration Y (m/s²) + float accelZ; // Acceleration Z (m/s², ≈9.81 when flat on a table) + float gyroX; // Angular rate X (rad/s) + float gyroY; // Angular rate Y (rad/s) + float gyroZ; // Angular rate Z (rad/s) + float tempC; // Die temperature (°C, not ambient) +}; + + +/** + * Configuration for one LSM6DSOX. + * Defaults to 0x6B (the robot board's onboard IMU address). + */ +struct HexaIMUConfig { + uint8_t i2cAddr = 0x6B; // LSM6DSOX default (SDO high) + TwoWire* wire = &Wire; // Wire instance (Wire.h is Arduino built-in) + int8_t sdaPin = -1; // SDA pin (-1 = use board default) + int8_t sclPin = -1; // SCL pin (-1 = use board default) +}; + + +class HexaIMU { +public: + /** + * Constructor — stores config, does NOT touch hardware. + * Call begin() in setup()/init() to initialize. + */ + HexaIMU() : HexaIMU(HexaIMUConfig{}) {} + explicit HexaIMU(const HexaIMUConfig& config); + + /** + * Initialize the IMU. Returns true on success. + * Sets defaults: ±4g accel, ±250°/s gyro, 104 Hz data rate. + * Safe to call from an OpMode's init() — does not block longer + * than Adafruit_LSM6DSOX's own begin_I2C() already does. + */ + bool begin(); + + /** Returns true if begin() succeeded. */ + bool isReady() const; + + + // Core Data Acquisition + + /** + * Read all sensor data in one call. Caches the result for the get*() + * functions below. Call this in loop() before reading any values. + * Returns false if not ready (begin() was not called/failed). + */ + bool read(); + + /** Return all last readings as a single struct. */ + HexaIMUData getData() const; + + float getAccelX() const; // Acceleration X (m/s²) + float getAccelY() const; // Acceleration Y (m/s²) + float getAccelZ() const; // Acceleration Z (m/s²) + float getGyroX() const; // Angular rate X (rad/s) + float getGyroY() const; // Angular rate Y (rad/s) + float getGyroZ() const; // Angular rate Z (rad/s) + float getTemp() const; // Die temperature (°C) + + + // Range Configuration + + /** Set accelerometer full-scale range. Default: HEXA_ACCEL_RANGE_4G */ + void setAccelRange(hexa_accel_range_t range); + hexa_accel_range_t getAccelRange() const; + + /** Set gyroscope full-scale range. Default: HEXA_GYRO_RANGE_250_DPS */ + void setGyroRange(hexa_gyro_range_t range); + hexa_gyro_range_t getGyroRange() const; + + + // Heading (gyro-Z integration, non-blocking calibration) + + /** + * Call every loop() tick with the current millis() (or micros()/1000). + * For the first ~50 calls, accumulates a gyro-Z bias estimate while + * the robot should be held still (no delay() — calibration is simply + * spread across however many loop iterations happen in ~0.5s). + * After that, integrates heading from bias-corrected, deadbanded gz. + */ + void tick(uint32_t nowMs); + + /** Current integrated heading, in degrees. */ + float getHeading() const; + + /** True once the initial gyro bias calibration has completed. */ + bool isHeadingReady() const; + + /** Reset the accumulated angle only — keeps the existing bias estimate. */ + void zeroHeading(); + + /** Full reset — re-run bias calibration AND zero the angle. Hold the robot still for ~0.5s after calling this. */ + void resetHeading(); + + +private: + HexaIMUConfig m_cfg; + Adafruit_LSM6DSOX m_lsm; + HexaIMUData m_data = {}; + bool m_ready = false; + + hexa_accel_range_t m_accelRange = HEXA_ACCEL_RANGE_4G; + hexa_gyro_range_t m_gyroRange = HEXA_GYRO_RANGE_250_DPS; + + // Heading state — owned per-instance, independent of any other HexaIMU + // or framework-internal IMU instance that may also be polling the bus. + static constexpr uint8_t HDG_CALIB_N = 50; // samples for bias estimate (~0.5s at ~100Hz tick()) + static constexpr float HDG_DEADBAND = 0.003f; // rad/s — suppress residual noise (~0.17 deg/s) + + float m_headingDeg = 0.0f; + float m_gzBias = 0.0f; + uint8_t m_calibCount = 0; + bool m_calibDone = false; + uint32_t m_lastTickMs = 0; +}; diff --git a/lib/HexaLED/build-manifest.json b/lib/HexaLED/build-manifest.json new file mode 100644 index 0000000..5ca9e3b --- /dev/null +++ b/lib/HexaLED/build-manifest.json @@ -0,0 +1,72 @@ +{ + "source_lib": "lib\\HexaLED", + "sdk_version": "1.4.1", + "built_from_commit": "036661d7fa9340e198bf3f6d376121ae8c214f59", + "built_at": "2026-10-10T14:37:40.848498Z", + "harvested_from_project": "RobotFirmware", + "harvested_env": "robot", + "cxx_path": "C:\\Users\\thein\\.platformio\\packages\\toolchain-xtensa-esp32s3\\bin\\xtensa-esp32s3-elf-g++.exe", + "cxx_flags": [ + "-std=gnu++17", + "-fexceptions", + "-fno-rtti", + "-Os", + "-mlongcalls", + "-ffunction-sections", + "-fdata-sections", + "-Wno-error=unused-function", + "-Wno-error=unused-variable", + "-Wno-error=deprecated-declarations", + "-Wno-unused-parameter", + "-Wno-sign-compare", + "-ggdb", + "-freorder-blocks", + "-Wwrite-strings", + "-fstack-protector", + "-fstrict-volatile-bitfields", + "-Wno-error=unused-but-set-variable", + "-fno-jump-tables", + "-fno-tree-switch-conversion", + "-MMD", + "-g0", + "-fno-rtti", + "-fno-exceptions", + "-fno-asynchronous-unwind-tables" + ], + "defines": [ + "PLATFORMIO=60200", + "ARDUINO_ESP32S3_DEV", + "ARDUINO_USB_MODE=1", + "ARDUINO_RUNNING_CORE=1", + "ARDUINO_EVENT_RUNNING_CORE=1", + "CONFIG_FREERTOS_HZ=1000", + "CORE_DEBUG_LEVEL=3", + "ARDUINO_USB_MODE=1", + "ARDUINO_USB_CDC_ON_BOOT=1", + "HAVE_CONFIG_H", + "MBEDTLS_CONFIG_FILE=\"mbedtls/esp_config.h\"", + "UNITY_INCLUDE_CONFIG_H", + "WITH_POSIX", + "_GNU_SOURCE", + "IDF_VER=\"v4.4.7-dirty\"", + "ESP_PLATFORM", + "_POSIX_READER_WRITER_LOCKS", + "ARDUINO_ARCH_ESP32", + "ESP32", + "F_CPU=240000000L", + "ARDUINO=10812", + "ARDUINO_VARIANT=\"esp32s3\"", + "ARDUINO_BOARD=\"Espressif ESP32-S3-DevKitC-1-N8 (8 MB QD, No PSRAM)\"", + "ARDUINO_PARTITION_partitions" + ], + "hardening": { + "extra_cxx_flags": [ + "-g0", + "-fno-rtti", + "-fno-exceptions", + "-fno-asynchronous-unwind-tables" + ], + "symbols_stripped": true, + "strings_encrypted": true + } +}
\ No newline at end of file diff --git a/lib/HexaLED/lib/xtensa-esp32s3/libHexaLED_static.a b/lib/HexaLED/lib/xtensa-esp32s3/libHexaLED_static.a Binary files differnew file mode 100644 index 0000000..1ddfd77 --- /dev/null +++ b/lib/HexaLED/lib/xtensa-esp32s3/libHexaLED_static.a diff --git a/lib/HexaLED/library.json b/lib/HexaLED/library.json new file mode 100644 index 0000000..14f2eab --- /dev/null +++ b/lib/HexaLED/library.json @@ -0,0 +1,15 @@ +{ + "name": "HexaLED", + "version": "1.0.0", + "description": "NeoPixel status and user LED driver for HEXA COMMAND HUB", + "keywords": ["led", "neopixel", "ws2812b", "rgb", "status", "esp32", "hexa"], + "authors": [ + { + "name": "Rush", + "maintainer": true + } + ], + "license": "MIT", + "platforms": "espressif32", + "frameworks": "arduino" +} diff --git a/lib/HexaLED/library.properties b/lib/HexaLED/library.properties new file mode 100644 index 0000000..b43aa73 --- /dev/null +++ b/lib/HexaLED/library.properties @@ -0,0 +1,9 @@ +name=HexaLED +version=1.0.0 +author=Rush +maintainer=Rush +sentence=NeoPixel status and user LED driver for HEXA COMMAND HUB +paragraph=Drives WS2812B NeoPixels with solid, blink, and pulse patterns. Supports standalone and shared-strip modes for chained pixels. Framework status LED and student-controlled user LED on the same data line. +category=Display +url= +architectures=esp32 diff --git a/lib/HexaLED/src/HexaLED.h b/lib/HexaLED/src/HexaLED.h new file mode 100644 index 0000000..67714a9 --- /dev/null +++ b/lib/HexaLED/src/HexaLED.h @@ -0,0 +1,70 @@ +#pragma once +#include <Adafruit_NeoPixel.h> + +// ============================================================ +// HexaLED — NeoPixel status/user indicator for NRL +// ============================================================ +// +// Two usage modes: +// +// 1. Standalone — HexaLED owns its own strip (controller ctrlLed): +// HexaLED ctrlLed(38, 1); +// +// 2. Shared — HexaLED controls one pixel inside an external strip +// (robot botLed + userLed share the same GPIO 38 data line): +// Adafruit_NeoPixel nrlLedStrip(2, 38, NEO_GRB + NEO_KHZ800); +// HexaLED botLed(nrlLedStrip, 0); // pixel 0 — framework status +// HexaLED userLed(nrlLedStrip, 1); // pixel 1 — student use +// +// Call begin() once, then tick(millis()) every loop iteration. +// Call the stateXxx() / setSolid() / setBlink() / setPulse() methods +// to change the LED; tick() handles all animation without blocking. + +class HexaLED { +public: + // Standalone mode — owns a dedicated strip + HexaLED(uint8_t pin, uint8_t count = 1); + + // Shared mode — controls one pixel inside an externally-owned strip + HexaLED(Adafruit_NeoPixel& strip, uint8_t pixelIndex); + + void begin(); + void tick(uint32_t nowMs); + + // ── Color / pattern methods (student-facing API) ────────── + void setOff(); + void setSolid(uint8_t r, uint8_t g, uint8_t b); + void setBlink(uint8_t r, uint8_t g, uint8_t b, uint16_t periodMs = 500); + void setPulse(uint8_t r, uint8_t g, uint8_t b, uint16_t periodMs = 1000); + + // ── Named NRL framework states (framework use only) ─────── + void stateUnpaired(); + void statePairing(); + void stateReconnecting(); + void stateIdle(); + void stateInit(); + void stateRunningTeleop(); + void stateRunningAuto(); + void stateLoopOverrun(); + +private: + enum class Pattern : uint8_t { SOLID, BLINK, PULSE }; + + void _set(uint8_t r, uint8_t g, uint8_t b, Pattern p, uint16_t periodMs); + void _write(uint8_t r, uint8_t g, uint8_t b); + + Adafruit_NeoPixel _ownStrip; // used in standalone mode + Adafruit_NeoPixel* _strip; // points to _ownStrip (standalone) or external (shared) + uint8_t _pixelIndex; // which pixel in the strip this instance controls + uint8_t _count; // number of pixels controlled (always 1 in shared mode) + + uint8_t _r, _g, _b; + Pattern _pattern; + uint16_t _period; + uint32_t _phaseStart; + bool _on; + + // Last time _write() actually pushed bytes to the strip. SOLID uses this to + // periodically re-assert its colour — see tick(). + uint32_t _lastWriteMs = 0; +}; diff --git a/lib/HexaOLED/build-manifest.json b/lib/HexaOLED/build-manifest.json new file mode 100644 index 0000000..34ff1f6 --- /dev/null +++ b/lib/HexaOLED/build-manifest.json @@ -0,0 +1,72 @@ +{ + "source_lib": "lib\\HexaOLED", + "sdk_version": "1.4.1", + "built_from_commit": "036661d7fa9340e198bf3f6d376121ae8c214f59", + "built_at": "2026-10-10T14:36:58.768212Z", + "harvested_from_project": "RobotFirmware", + "harvested_env": "robot", + "cxx_path": "C:\\Users\\thein\\.platformio\\packages\\toolchain-xtensa-esp32s3\\bin\\xtensa-esp32s3-elf-g++.exe", + "cxx_flags": [ + "-std=gnu++17", + "-fexceptions", + "-fno-rtti", + "-Os", + "-mlongcalls", + "-ffunction-sections", + "-fdata-sections", + "-Wno-error=unused-function", + "-Wno-error=unused-variable", + "-Wno-error=deprecated-declarations", + "-Wno-unused-parameter", + "-Wno-sign-compare", + "-ggdb", + "-freorder-blocks", + "-Wwrite-strings", + "-fstack-protector", + "-fstrict-volatile-bitfields", + "-Wno-error=unused-but-set-variable", + "-fno-jump-tables", + "-fno-tree-switch-conversion", + "-MMD", + "-g0", + "-fno-rtti", + "-fno-exceptions", + "-fno-asynchronous-unwind-tables" + ], + "defines": [ + "PLATFORMIO=60200", + "ARDUINO_ESP32S3_DEV", + "ARDUINO_USB_MODE=1", + "ARDUINO_RUNNING_CORE=1", + "ARDUINO_EVENT_RUNNING_CORE=1", + "CONFIG_FREERTOS_HZ=1000", + "CORE_DEBUG_LEVEL=3", + "ARDUINO_USB_MODE=1", + "ARDUINO_USB_CDC_ON_BOOT=1", + "HAVE_CONFIG_H", + "MBEDTLS_CONFIG_FILE=\"mbedtls/esp_config.h\"", + "UNITY_INCLUDE_CONFIG_H", + "WITH_POSIX", + "_GNU_SOURCE", + "IDF_VER=\"v4.4.7-dirty\"", + "ESP_PLATFORM", + "_POSIX_READER_WRITER_LOCKS", + "ARDUINO_ARCH_ESP32", + "ESP32", + "F_CPU=240000000L", + "ARDUINO=10812", + "ARDUINO_VARIANT=\"esp32s3\"", + "ARDUINO_BOARD=\"Espressif ESP32-S3-DevKitC-1-N8 (8 MB QD, No PSRAM)\"", + "ARDUINO_PARTITION_partitions" + ], + "hardening": { + "extra_cxx_flags": [ + "-g0", + "-fno-rtti", + "-fno-exceptions", + "-fno-asynchronous-unwind-tables" + ], + "symbols_stripped": true, + "strings_encrypted": true + } +}
\ No newline at end of file diff --git a/lib/HexaOLED/keywords.txt b/lib/HexaOLED/keywords.txt new file mode 100644 index 0000000..2849081 --- /dev/null +++ b/lib/HexaOLED/keywords.txt @@ -0,0 +1,30 @@ +# ═══════════════════════════════════════════════ +# Syntax Highlighting for HexaOLED Library +# ═══════════════════════════════════════════════ +# KEYWORD1 = Classes (highlighted as types) +# KEYWORD2 = Methods (highlighted as functions) +# LITERAL1 = Constants + +# ── Classes ── +HexaOLED KEYWORD1 +HexaOLEDConfig KEYWORD1 + +# ── Methods ── +begin KEYWORD2 +isReady KEYWORD2 +clearDisplay KEYWORD2 +display KEYWORD2 +drawPixel KEYWORD2 +drawLine KEYWORD2 +drawRect KEYWORD2 +fillRect KEYWORD2 +setCursor KEYWORD2 +setTextColor KEYWORD2 +setTextSize KEYWORD2 +print KEYWORD2 +println KEYWORD2 + +# ── Constants ── +WHITE LITERAL1 +BLACK LITERAL1 +INVERSE LITERAL1 diff --git a/lib/HexaOLED/lib/xtensa-esp32s3/libHexaOLED_static.a b/lib/HexaOLED/lib/xtensa-esp32s3/libHexaOLED_static.a Binary files differnew file mode 100644 index 0000000..03a6bf8 --- /dev/null +++ b/lib/HexaOLED/lib/xtensa-esp32s3/libHexaOLED_static.a diff --git a/lib/HexaOLED/library.json b/lib/HexaOLED/library.json new file mode 100644 index 0000000..8843a0b --- /dev/null +++ b/lib/HexaOLED/library.json @@ -0,0 +1,15 @@ +{ + "name": "HexaOLED", + "version": "1.0.0", + "description": "Standalone SSD1306 OLED driver for HEXA COMMAND HUB", + "keywords": ["oled", "ssd1306", "display", "i2c", "esp32", "hexa"], + "authors": [ + { + "name": "Rush", + "maintainer": true + } + ], + "license": "MIT", + "platforms": "espressif32", + "frameworks": "arduino" +} diff --git a/lib/HexaOLED/library.properties b/lib/HexaOLED/library.properties new file mode 100644 index 0000000..feaa6d9 --- /dev/null +++ b/lib/HexaOLED/library.properties @@ -0,0 +1,9 @@ +name=HexaOLED +version=1.0.0 +author=Rush +maintainer=Rush +sentence=Standalone SSD1306 OLED driver for HEXA COMMAND HUB +paragraph=Zero Adafruit dependency. Uses only Wire.h (Arduino built-in). Built-in 5x7 font, frame buffer, and drawing primitives (pixel, line, rect, text). +category=Display +url= +architectures=esp32 diff --git a/lib/HexaOLED/src/HexaFont.h b/lib/HexaOLED/src/HexaFont.h new file mode 100644 index 0000000..e07366e --- /dev/null +++ b/lib/HexaOLED/src/HexaFont.h @@ -0,0 +1,135 @@ +/** + * ============================================================ + * HexaFont.h — Built-in 5×7 ASCII Font + * ============================================================ + * + * Standard 5×7 pixel monospace font covering ASCII 32–126. + * Stored in PROGMEM (flash memory) so it costs zero RAM. + * + * Layout of each glyph (5 bytes = 5 columns): + * + * Byte index → column (left to right) + * Bit index → row (bit 0 = top row, bit 6 = bottom row) + * + * Example: 'A' (0x7E, 0x11, 0x11, 0x11, 0x7E) + * + * col: 0 1 2 3 4 + * 0x7E 0x11 0x11 0x11 0x7E + * row0: 0 1 1 1 0 ← bit 0 + * row1: 1 0 0 0 1 ← bit 1 + * row2: 1 0 0 0 1 ← bit 2 + * row3: 1 1 1 1 1 ← bit 3 + * row4: 1 0 0 0 1 ← bit 4 + * row5: 1 0 0 0 1 ← bit 5 + * row6: 0 0 0 0 0 ← bit 6 + * + * Each character cell is 6×8 px (5 glyph + 1 gap column, + * 7 glyph + 1 gap row) giving clean spacing between chars. + * + * Part of: HexaOLED library + * Author: Rush / HEXA Robotics + * ============================================================ + */ + +#pragma once + +#include <Arduino.h> // for PROGMEM, pgm_read_byte + +// ASCII 32 (space) through 126 (~) = 95 characters × 5 bytes +static const uint8_t HEXA_FONT[][5] PROGMEM = { + { 0x00, 0x00, 0x00, 0x00, 0x00 }, // 32 (space) + { 0x00, 0x00, 0x5F, 0x00, 0x00 }, // 33 ! + { 0x00, 0x07, 0x00, 0x07, 0x00 }, // 34 " + { 0x14, 0x7F, 0x14, 0x7F, 0x14 }, // 35 # + { 0x24, 0x2A, 0x7F, 0x2A, 0x12 }, // 36 $ + { 0x23, 0x13, 0x08, 0x64, 0x62 }, // 37 % + { 0x36, 0x49, 0x55, 0x22, 0x50 }, // 38 & + { 0x00, 0x05, 0x03, 0x00, 0x00 }, // 39 ' + { 0x00, 0x1C, 0x22, 0x41, 0x00 }, // 40 ( + { 0x00, 0x41, 0x22, 0x1C, 0x00 }, // 41 ) + { 0x14, 0x08, 0x3E, 0x08, 0x14 }, // 42 * + { 0x08, 0x08, 0x3E, 0x08, 0x08 }, // 43 + + { 0x00, 0x50, 0x30, 0x00, 0x00 }, // 44 , + { 0x08, 0x08, 0x08, 0x08, 0x08 }, // 45 - + { 0x00, 0x60, 0x60, 0x00, 0x00 }, // 46 . + { 0x20, 0x10, 0x08, 0x04, 0x02 }, // 47 / + { 0x3E, 0x51, 0x49, 0x45, 0x3E }, // 48 0 + { 0x00, 0x42, 0x7F, 0x40, 0x00 }, // 49 1 + { 0x42, 0x61, 0x51, 0x49, 0x46 }, // 50 2 + { 0x21, 0x41, 0x45, 0x4B, 0x31 }, // 51 3 + { 0x18, 0x14, 0x12, 0x7F, 0x10 }, // 52 4 + { 0x27, 0x45, 0x45, 0x45, 0x39 }, // 53 5 + { 0x3C, 0x4A, 0x49, 0x49, 0x30 }, // 54 6 + { 0x01, 0x71, 0x09, 0x05, 0x03 }, // 55 7 + { 0x36, 0x49, 0x49, 0x49, 0x36 }, // 56 8 + { 0x06, 0x49, 0x49, 0x29, 0x1E }, // 57 9 + { 0x00, 0x36, 0x36, 0x00, 0x00 }, // 58 : + { 0x00, 0x56, 0x36, 0x00, 0x00 }, // 59 ; + { 0x08, 0x14, 0x22, 0x41, 0x00 }, // 60 < + { 0x14, 0x14, 0x14, 0x14, 0x14 }, // 61 = + { 0x00, 0x41, 0x22, 0x14, 0x08 }, // 62 > + { 0x02, 0x01, 0x51, 0x09, 0x06 }, // 63 ? + { 0x32, 0x49, 0x79, 0x41, 0x3E }, // 64 @ + { 0x7E, 0x11, 0x11, 0x11, 0x7E }, // 65 A + { 0x7F, 0x49, 0x49, 0x49, 0x36 }, // 66 B + { 0x3E, 0x41, 0x41, 0x41, 0x22 }, // 67 C + { 0x7F, 0x41, 0x41, 0x22, 0x1C }, // 68 D + { 0x7F, 0x49, 0x49, 0x49, 0x41 }, // 69 E + { 0x7F, 0x09, 0x09, 0x09, 0x01 }, // 70 F + { 0x3E, 0x41, 0x49, 0x49, 0x7A }, // 71 G + { 0x7F, 0x08, 0x08, 0x08, 0x7F }, // 72 H + { 0x00, 0x41, 0x7F, 0x41, 0x00 }, // 73 I + { 0x20, 0x40, 0x41, 0x3F, 0x01 }, // 74 J + { 0x7F, 0x08, 0x14, 0x22, 0x41 }, // 75 K + { 0x7F, 0x40, 0x40, 0x40, 0x40 }, // 76 L + { 0x7F, 0x02, 0x0C, 0x02, 0x7F }, // 77 M + { 0x7F, 0x04, 0x08, 0x10, 0x7F }, // 78 N + { 0x3E, 0x41, 0x41, 0x41, 0x3E }, // 79 O + { 0x7F, 0x09, 0x09, 0x09, 0x06 }, // 80 P + { 0x3E, 0x41, 0x51, 0x21, 0x5E }, // 81 Q + { 0x7F, 0x09, 0x19, 0x29, 0x46 }, // 82 R + { 0x46, 0x49, 0x49, 0x49, 0x31 }, // 83 S + { 0x01, 0x01, 0x7F, 0x01, 0x01 }, // 84 T + { 0x3F, 0x40, 0x40, 0x40, 0x3F }, // 85 U + { 0x1F, 0x20, 0x40, 0x20, 0x1F }, // 86 V + { 0x3F, 0x40, 0x38, 0x40, 0x3F }, // 87 W + { 0x63, 0x14, 0x08, 0x14, 0x63 }, // 88 X + { 0x07, 0x08, 0x70, 0x08, 0x07 }, // 89 Y + { 0x61, 0x51, 0x49, 0x45, 0x43 }, // 90 Z + { 0x00, 0x7F, 0x41, 0x41, 0x00 }, // 91 [ + { 0x02, 0x04, 0x08, 0x10, 0x20 }, // 92 backslash + { 0x00, 0x41, 0x41, 0x7F, 0x00 }, // 93 ] + { 0x04, 0x02, 0x01, 0x02, 0x04 }, // 94 ^ + { 0x40, 0x40, 0x40, 0x40, 0x40 }, // 95 _ + { 0x00, 0x01, 0x02, 0x04, 0x00 }, // 96 ` + { 0x20, 0x54, 0x54, 0x54, 0x78 }, // 97 a + { 0x7F, 0x48, 0x44, 0x44, 0x38 }, // 98 b + { 0x38, 0x44, 0x44, 0x44, 0x20 }, // 99 c + { 0x38, 0x44, 0x44, 0x48, 0x7F }, // 100 d + { 0x38, 0x54, 0x54, 0x54, 0x18 }, // 101 e + { 0x08, 0x7E, 0x09, 0x01, 0x02 }, // 102 f + { 0x0C, 0x52, 0x52, 0x52, 0x3E }, // 103 g + { 0x7F, 0x08, 0x04, 0x04, 0x78 }, // 104 h + { 0x00, 0x44, 0x7D, 0x40, 0x00 }, // 105 i + { 0x20, 0x40, 0x44, 0x3D, 0x00 }, // 106 j + { 0x7F, 0x10, 0x28, 0x44, 0x00 }, // 107 k + { 0x00, 0x41, 0x7F, 0x40, 0x00 }, // 108 l + { 0x7C, 0x04, 0x18, 0x04, 0x78 }, // 109 m + { 0x7C, 0x08, 0x04, 0x04, 0x78 }, // 110 n + { 0x38, 0x44, 0x44, 0x44, 0x38 }, // 111 o + { 0x7C, 0x14, 0x14, 0x14, 0x08 }, // 112 p + { 0x08, 0x14, 0x14, 0x18, 0x7C }, // 113 q + { 0x7C, 0x08, 0x04, 0x04, 0x08 }, // 114 r + { 0x48, 0x54, 0x54, 0x54, 0x20 }, // 115 s + { 0x04, 0x3F, 0x44, 0x40, 0x20 }, // 116 t + { 0x3C, 0x40, 0x40, 0x20, 0x7C }, // 117 u + { 0x1C, 0x20, 0x40, 0x20, 0x1C }, // 118 v + { 0x3C, 0x40, 0x30, 0x40, 0x3C }, // 119 w + { 0x44, 0x28, 0x10, 0x28, 0x44 }, // 120 x + { 0x0C, 0x50, 0x50, 0x50, 0x3C }, // 121 y + { 0x44, 0x64, 0x54, 0x4C, 0x44 }, // 122 z + { 0x00, 0x08, 0x36, 0x41, 0x00 }, // 123 { + { 0x00, 0x00, 0x7F, 0x00, 0x00 }, // 124 | + { 0x00, 0x41, 0x36, 0x08, 0x00 }, // 125 } + { 0x10, 0x08, 0x08, 0x10, 0x08 }, // 126 ~ +}; diff --git a/lib/HexaOLED/src/HexaOLED.h b/lib/HexaOLED/src/HexaOLED.h new file mode 100644 index 0000000..e16aa97 --- /dev/null +++ b/lib/HexaOLED/src/HexaOLED.h @@ -0,0 +1,214 @@ +/** + * HexaOLED.h — SH1106G OLED Driver for HEXA COMMAND HUB + * + * Standalone SH1106G driver — no Adafruit dependency, only Wire.h. + * Maintains a 1024-byte local frame buffer (SH1106G cannot be read back over I2C). + * All drawing writes to the buffer; call display() to push the buffer to hardware. + * + * Font: HexaFont.h (5x7 ASCII bitmap, stored in flash) + * Part of: HexaOLED library + * Author: Rush / HEXA Robotics + */ + +#pragma once + +#include <Arduino.h> +#include <Wire.h> +#include "HexaFont.h" + + +/** + * Configuration for one OLED display. + * Example: HexaOLEDConfig cfg = { .i2cAddr = 0x3C, .sdaPin = 21, .sclPin = 22 }; + */ +struct HexaOLEDConfig { + uint8_t i2cAddr = 0x3C; // SH1106G default I2C address (0x3C or 0x3D) + uint8_t width = 128; // Display width in pixels + uint8_t height = 64; // Display height in pixels (32 or 64) + TwoWire* wire = &Wire; // Wire instance (Wire.h is Arduino built-in) + int8_t sdaPin = -1; // SDA pin (-1 = use board default) + int8_t sclPin = -1; // SCL pin (-1 = use board default) +}; + + +class HexaOLED { +public: + // Color constants + static constexpr uint8_t WHITE = 1; // Turn pixel on + static constexpr uint8_t BLACK = 0; // Turn pixel off + static constexpr uint8_t INVERSE = 2; // Flip pixel state + + /** + * Constructor — stores config, does NOT touch hardware. + * Call begin() in setup() to initialize. + */ + HexaOLED(const HexaOLEDConfig& config); + + /** + * Initialize the display. Call in setup(). + * Returns true on success, false if display not found on I2C bus. + */ + bool begin(); + + /** + * Returns true if begin() succeeded. + */ + bool isReady() const; + + // Non-blocking self-heal after a transient I2C/power fault. No-op while + // healthy. Call every loop from main-loop context (never the ESP-NOW callback). + void tickRecovery(uint32_t nowMs); + +#ifdef NRL_LINK_FAULT_INJECTION + // Bench only: trip the m_ready latch exactly as 5 consecutive I2C failures + // would, so tickRecovery() can be verified without physically interrupting + // the bus. Compiled out of the shipping build. + void debugForceFail() { m_ready = false; m_i2cFailCount = 255; } +#endif + + // Buffer operations + + /** + * Fill the entire frame buffer with BLACK (all pixels off). + * Call before redrawing the screen each frame. + */ + void clearDisplay(); + + /** + * Push the local frame buffer to the SH1106G hardware. + * Nothing you draw appears on screen until you call this. + */ + void display(); + + /** + * Push only the pages that changed since the last displayDirty() call. + * Use this in tight loops instead of display() to minimize I2C transfer + * time and eliminate visible screen-sweep flicker. + */ + void displayDirty(); + + // Drawing primitives + + /** + * Set a single pixel. color = WHITE | BLACK | INVERSE. + * Out-of-bounds coordinates are silently ignored. + */ + void drawPixel(int16_t x, int16_t y, uint8_t color); + + /** + * Draw a straight line between two points. + * Uses Bresenham's algorithm — no floating point. + */ + void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint8_t color); + + /** + * Draw an unfilled rectangle outline. + */ + void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint8_t color); + + /** + * Draw a solid filled rectangle. + */ + void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint8_t color); + + /** + * Draw an unfilled circle outline. Uses midpoint circle algorithm — no floating point. + */ + void drawCircle(int16_t x0, int16_t y0, int16_t r, uint8_t color); + + /** + * Draw a solid filled circle. + */ + void fillCircle(int16_t x0, int16_t y0, int16_t r, uint8_t color); + + // Text + + /** + * Set cursor position for the next print() call. + * (0, 0) is the top-left corner of the display. + */ + void setCursor(int16_t x, int16_t y); + + /** + * Set text color. WHITE = visible, BLACK = background, + * INVERSE = flip each pixel as it is drawn. + */ + void setTextColor(uint8_t color); + + /** + * Set text scale factor. + * size=1 → 6×8 px per character (default) + * size=2 → 12×16 px per character + */ + void setTextSize(uint8_t size); + + /** Print a null-terminated string at the current cursor. */ + void print(const char* str); + + /** Print a string followed by a newline. */ + void println(const char* str); + + /** Print a signed integer. */ + void print(int32_t value); + + /** Print a float with a given number of decimal places (0-2). */ + void print(float value, uint8_t decimals = 2); + +private: + HexaOLEDConfig m_cfg; + + // Frame buffer: 128×64 / 8 = 1024 bytes max + // Indexed as: buf[x + (y/8)*width], bit = y%8 + uint8_t m_buf[128 * 64 / 8]; + + int16_t m_cursorX = 0; + int16_t m_cursorY = 0; + uint8_t m_textColor = WHITE; + uint8_t m_textSize = 1; + bool m_ready = false; + uint8_t m_dirty = 0xFF; // bitmask: bit N = page N needs sending + uint8_t m_i2cFailCount = 0; // consecutive endTransmission() failures since last success + + // SH1106G power-on init sequence. Defined at the top of HexaOLED.cpp, shared by + // begin() and tickRecovery() so the two cannot drift apart. Class-scope because + // the CMD_* values it uses are class members. + static const uint8_t SH1106_INIT_CMDS[]; + + // tickRecovery() state — see HexaOLED.cpp. + enum class Recovery : uint8_t { Idle, AwaitDisplayOn }; + Recovery m_recovery = Recovery::Idle; + uint32_t m_recoveryMs = 0; // last probe, or when init finished + + // Internal helpers + + // Send one page of pixel data (used by display() and displayDirty()) + void _sendPage(uint8_t page); + + // Send a single command byte to the SH1106G + void sh1106_cmd(uint8_t c); + + // Send a list of command bytes + void sh1106_cmdList(const uint8_t* list, uint8_t len); + + // Draw one character glyph from HexaFont at (x, y) + void drawChar(int16_t x, int16_t y, char c, uint8_t color, uint8_t size); + + // SH1106G command constants + static constexpr uint8_t CMD_DISPLAYOFF = 0xAE; + static constexpr uint8_t CMD_DISPLAYON = 0xAF; + static constexpr uint8_t CMD_SETDISPLAYCLOCKDIV = 0xD5; + static constexpr uint8_t CMD_SETMULTIPLEX = 0xA8; + static constexpr uint8_t CMD_SETDISPLAYOFFSET = 0xD3; + static constexpr uint8_t CMD_SETSTARTLINE = 0x40; + static constexpr uint8_t CMD_DCDC = 0xAD; // internal DC/DC control + static constexpr uint8_t CMD_MEMORYMODE = 0x20; + static constexpr uint8_t CMD_SEGREMAP = 0xA1; + static constexpr uint8_t CMD_COMSCANDEC = 0xC8; + static constexpr uint8_t CMD_SETCOMPINS = 0xDA; + static constexpr uint8_t CMD_SETCONTRAST = 0x81; + static constexpr uint8_t CMD_SETPRECHARGE = 0xD9; + static constexpr uint8_t CMD_SETVCOMDETECT = 0xDB; + static constexpr uint8_t CMD_DISPLAYALLON_RESUME= 0xA4; + static constexpr uint8_t CMD_NORMALDISPLAY = 0xA6; + static constexpr uint8_t CMD_SETPAGEADDR = 0xB0; // page addressing base +}; diff --git a/lib/HexaPower/build-manifest.json b/lib/HexaPower/build-manifest.json new file mode 100644 index 0000000..6828a85 --- /dev/null +++ b/lib/HexaPower/build-manifest.json @@ -0,0 +1,72 @@ +{ + "source_lib": "lib\\HexaPower", + "sdk_version": "1.4.1", + "built_from_commit": "036661d7fa9340e198bf3f6d376121ae8c214f59", + "built_at": "2026-10-10T14:38:01.873467Z", + "harvested_from_project": "RobotFirmware", + "harvested_env": "robot", + "cxx_path": "C:\\Users\\thein\\.platformio\\packages\\toolchain-xtensa-esp32s3\\bin\\xtensa-esp32s3-elf-g++.exe", + "cxx_flags": [ + "-std=gnu++17", + "-fexceptions", + "-fno-rtti", + "-Os", + "-mlongcalls", + "-ffunction-sections", + "-fdata-sections", + "-Wno-error=unused-function", + "-Wno-error=unused-variable", + "-Wno-error=deprecated-declarations", + "-Wno-unused-parameter", + "-Wno-sign-compare", + "-ggdb", + "-freorder-blocks", + "-Wwrite-strings", + "-fstack-protector", + "-fstrict-volatile-bitfields", + "-Wno-error=unused-but-set-variable", + "-fno-jump-tables", + "-fno-tree-switch-conversion", + "-MMD", + "-g0", + "-fno-rtti", + "-fno-exceptions", + "-fno-asynchronous-unwind-tables" + ], + "defines": [ + "PLATFORMIO=60200", + "ARDUINO_ESP32S3_DEV", + "ARDUINO_USB_MODE=1", + "ARDUINO_RUNNING_CORE=1", + "ARDUINO_EVENT_RUNNING_CORE=1", + "CONFIG_FREERTOS_HZ=1000", + "CORE_DEBUG_LEVEL=3", + "ARDUINO_USB_MODE=1", + "ARDUINO_USB_CDC_ON_BOOT=1", + "HAVE_CONFIG_H", + "MBEDTLS_CONFIG_FILE=\"mbedtls/esp_config.h\"", + "UNITY_INCLUDE_CONFIG_H", + "WITH_POSIX", + "_GNU_SOURCE", + "IDF_VER=\"v4.4.7-dirty\"", + "ESP_PLATFORM", + "_POSIX_READER_WRITER_LOCKS", + "ARDUINO_ARCH_ESP32", + "ESP32", + "F_CPU=240000000L", + "ARDUINO=10812", + "ARDUINO_VARIANT=\"esp32s3\"", + "ARDUINO_BOARD=\"Espressif ESP32-S3-DevKitC-1-N8 (8 MB QD, No PSRAM)\"", + "ARDUINO_PARTITION_partitions" + ], + "hardening": { + "extra_cxx_flags": [ + "-g0", + "-fno-rtti", + "-fno-exceptions", + "-fno-asynchronous-unwind-tables" + ], + "symbols_stripped": true, + "strings_encrypted": true + } +}
\ No newline at end of file diff --git a/lib/HexaPower/lib/xtensa-esp32s3/libHexaPower_static.a b/lib/HexaPower/lib/xtensa-esp32s3/libHexaPower_static.a Binary files differnew file mode 100644 index 0000000..1884764 --- /dev/null +++ b/lib/HexaPower/lib/xtensa-esp32s3/libHexaPower_static.a diff --git a/lib/HexaPower/library.json b/lib/HexaPower/library.json new file mode 100644 index 0000000..da3d5c2 --- /dev/null +++ b/lib/HexaPower/library.json @@ -0,0 +1,15 @@ +{ + "name": "HexaPower", + "version": "1.0.0", + "description": "MCP3008 ADC battery-voltage and ACS712 current sensing for HEXA COMMAND HUB", + "keywords": ["mcp3008", "acs712", "adc", "current", "voltage", "battery", "esp32", "hexa"], + "authors": [ + { + "name": "Rush", + "maintainer": true + } + ], + "license": "MIT", + "platforms": "espressif32", + "frameworks": "arduino" +} diff --git a/lib/HexaPower/src/HexaPower.h b/lib/HexaPower/src/HexaPower.h new file mode 100644 index 0000000..9a2d5d1 --- /dev/null +++ b/lib/HexaPower/src/HexaPower.h @@ -0,0 +1,171 @@ +/** + * HexaPower.h — Battery-voltage & current sensing for HEXA COMMAND HUB + * + * Wraps the onboard MCP3008 (8-channel, 10-bit SPI ADC) that feeds the + * battery voltage sensor and two ACS712 Hall-effect current sensors. Gives + * the same "declare an object, call its methods" idiom used by HexaDCMotor/ + * HexaServo/HexaOLED/HexaIMU. + * + * Board wiring (verified against the manufacturer Master Protocol + * HEXA-CH-VVR-001 §TC006–TC008 and the batch serial logs): + * + * MCP3008 channel map (as ACTUALLY wired — verified by the 3-phase PowerTest + * isolation, which differs from the manufacturer's rail labels): + * CH0 → battery voltage sensor (through a resistive divider) + * CH1 → ACS712 #1 — MAIN / total battery current (upstream: motor + servo) + * CH2 → ACS712 #2 — DC-motor branch current + * CH3..CH7 → external ADC breakout connectors (readRaw/readChannelVoltage) + * There is NO dedicated servo-rail sensor, so servo current is DERIVED as + * (main − motor) = CH1 − CH2. See getServoCurrent(). + * + * SPI pins (dedicated bus, free on the robot board): + * CS=8 SCK=14 MISO=9 MOSI=13 + * + * NOTE ON CALIBRATION: the MCP3008 reference (VREF) on this board is 5.0 V, + * not 3.3 V. The bring-up sketches that shipped with the prototype used 3.3 V + * and were explicitly flagged as "the WRONG protocol" — do not copy those + * constants. The defaults below come from the verified batch logs + * (CH0 941 counts → 11.35 V ⇒ scale ≈ 2.468). ACS712 sensitivity and the + * voltage divider still want a per-board recalibration against a multimeter. + * + * Part of: HexaPower library + * Author: Rush / HEXA Robotics + */ + +#pragma once + +#include <Arduino.h> +#include <SPI.h> + + +// Data struct + +/** One snapshot from read(): the onboard rails. */ +struct HexaPowerData { + float batteryV; // battery pack voltage (V) + float mainA; // main/total battery current (A) + float motorA; // DC-motor rail current (A) + float servoA; // servo rail current (A) +}; + + +/** + * Configuration for the MCP3008 power-sensing front end. + * Defaults match the robot board — most students never change these. + */ +struct HexaPowerConfig { + // SPI pins (robot board defaults) + int8_t csPin = 8; + int8_t sckPin = 14; + int8_t misoPin = 9; + int8_t mosiPin = 13; + + // ADC reference (MCP3008 VREF). 5.0 V on this board — NOT 3.3 V. + float vref = 5.0f; + + // Channel assignments (see the wiring note at the top of this file). + uint8_t vbattChannel = 0; // CH0 → voltage sensor + uint8_t mainCurrChannel = 1; // CH1 → ACS712 #1 (MAIN / total battery current) + uint8_t motorCurrChannel = 2; // CH2 → ACS712 #2 (DC-motor branch) + // No servoCurrChannel: servo current is derived as (main − motor). + + // Battery divider scale: multiply the CH0 ADC voltage to get pack volts. + // Verified from batch log (941 counts → 11.35 V). Recalibrate per board. + float vbattScale = 2.468f; + + // ACS712 sensitivity (V per Amp). ACS712-20A ≈ 0.100 V/A. The batch log + // implies ~0.088 V/A for the fitted part — RECALIBRATE against a DMM in + // series before trusting the amps reading. + float acsSensitivity = 0.100f; + + // Exponential-moving-average factor for current smoothing (0..1, higher = + // snappier / noisier). Matches the prototype current sketch. + float emaAlpha = 0.25f; + + // Low-battery warning threshold (V). Default sized for a 3S LiPo. + float lowBatteryV = 10.5f; + + // SPI clock for the MCP3008 (1 MHz is comfortably within spec at 5 V). + uint32_t spiHz = 1000000; +}; + + +class HexaPower { +public: + /** + * Constructor — stores config, does NOT touch hardware. + * Call begin() in setup()/init() to initialize. + */ + HexaPower() : HexaPower(HexaPowerConfig{}) {} + explicit HexaPower(const HexaPowerConfig& config); + + /** + * Initialize the SPI bus and the MCP3008, then capture the ACS712 + * zero-current offsets (a short averaged read — safe to call from an + * OpMode init(); it does NOT block for hundreds of milliseconds). + * Returns true (the MCP3008 has no ID register to probe). + */ + bool begin(); + + /** True once begin() has run. */ + bool isReady() const; + + /** + * Re-capture the ACS712 zero-current offsets. Call with both the motor + * and servo rails idle. begin() already does this once. + */ + void calibrateCurrentZero(); + + + // Core Data Acquisition + + /** + * Sample CH1/CH2 (and the battery divider) exactly ONCE, advance the EMA + * current filters from those single samples, and cache everything for + * the get*() functions below. Called once per tick by the framework + * (NRLRunner::tick()) — students never need to call this themselves. + * Returns false if begin() has not run. + */ + bool read(); + + /** All cached rail readings as one struct. */ + HexaPowerData getData() const; + + // Pure cache reads — no SPI, no side effects. Safe to call any number of + // times per tick (e.g. once per motor/servo object) and always returns + // the same value for a given rail, because it IS the same rail: there is + // one ACS712 per branch, not one per motor/servo. Values refresh once per + // tick via read(). + float getBatteryVoltage(); // battery pack voltage (V) + float getMainCurrent(); // MAIN/total battery current (A), CH1, EMA-smoothed + float getMotorCurrent(); // DC-motor branch current (A), CH2, EMA-smoothed + float getServoCurrent(); // servo current (A) = main − motor (CH1 − CH2), EMA-smoothed + + /** True while the last-read battery voltage is below lowBatteryV. */ + bool isBatteryLow() const; + + + // Generic MCP3008 access (also serves external ADC ports CH3..CH7) + + /** Raw 10-bit reading (0..1023) for any channel 0..7. */ + uint16_t readRaw(uint8_t channel); + + /** Channel voltage at the ADC pin (0..vref), before any scaling. */ + float readChannelVoltage(uint8_t channel); + + +private: + HexaPowerConfig m_cfg; + SPIClass m_spi{HSPI}; + HexaPowerData m_data = {}; + bool m_ready = false; + + float m_mainZeroV = 0.0f; // CH1 (main/total) ACS712 quiescent output (V) + float m_motorZeroV = 0.0f; // CH2 (motor branch) ACS712 quiescent output (V) + float m_mainEMA = 0.0f; // smoothed main/total current (A) + float m_motorEMA = 0.0f; // smoothed motor current (A) + float m_servoEMA = 0.0f; // smoothed servo current (A) = main − motor + + // Raw (unsmoothed, zero-subtracted) amps for one ACS712 channel. + float rawAmps(uint8_t channel, float zeroV); +}; diff --git a/lib/HexaServos/build-manifest.json b/lib/HexaServos/build-manifest.json new file mode 100644 index 0000000..809b3a0 --- /dev/null +++ b/lib/HexaServos/build-manifest.json @@ -0,0 +1,72 @@ +{ + "source_lib": "lib\\HexaServos", + "sdk_version": "1.4.1", + "built_from_commit": "036661d7fa9340e198bf3f6d376121ae8c214f59", + "built_at": "2026-10-10T14:36:47.795153Z", + "harvested_from_project": "RobotFirmware", + "harvested_env": "robot", + "cxx_path": "C:\\Users\\thein\\.platformio\\packages\\toolchain-xtensa-esp32s3\\bin\\xtensa-esp32s3-elf-g++.exe", + "cxx_flags": [ + "-std=gnu++17", + "-fexceptions", + "-fno-rtti", + "-Os", + "-mlongcalls", + "-ffunction-sections", + "-fdata-sections", + "-Wno-error=unused-function", + "-Wno-error=unused-variable", + "-Wno-error=deprecated-declarations", + "-Wno-unused-parameter", + "-Wno-sign-compare", + "-ggdb", + "-freorder-blocks", + "-Wwrite-strings", + "-fstack-protector", + "-fstrict-volatile-bitfields", + "-Wno-error=unused-but-set-variable", + "-fno-jump-tables", + "-fno-tree-switch-conversion", + "-MMD", + "-g0", + "-fno-rtti", + "-fno-exceptions", + "-fno-asynchronous-unwind-tables" + ], + "defines": [ + "PLATFORMIO=60200", + "ARDUINO_ESP32S3_DEV", + "ARDUINO_USB_MODE=1", + "ARDUINO_RUNNING_CORE=1", + "ARDUINO_EVENT_RUNNING_CORE=1", + "CONFIG_FREERTOS_HZ=1000", + "CORE_DEBUG_LEVEL=3", + "ARDUINO_USB_MODE=1", + "ARDUINO_USB_CDC_ON_BOOT=1", + "HAVE_CONFIG_H", + "MBEDTLS_CONFIG_FILE=\"mbedtls/esp_config.h\"", + "UNITY_INCLUDE_CONFIG_H", + "WITH_POSIX", + "_GNU_SOURCE", + "IDF_VER=\"v4.4.7-dirty\"", + "ESP_PLATFORM", + "_POSIX_READER_WRITER_LOCKS", + "ARDUINO_ARCH_ESP32", + "ESP32", + "F_CPU=240000000L", + "ARDUINO=10812", + "ARDUINO_VARIANT=\"esp32s3\"", + "ARDUINO_BOARD=\"Espressif ESP32-S3-DevKitC-1-N8 (8 MB QD, No PSRAM)\"", + "ARDUINO_PARTITION_partitions" + ], + "hardening": { + "extra_cxx_flags": [ + "-g0", + "-fno-rtti", + "-fno-exceptions", + "-fno-asynchronous-unwind-tables" + ], + "symbols_stripped": true, + "strings_encrypted": true + } +}
\ No newline at end of file diff --git a/lib/HexaServos/keywords.txt b/lib/HexaServos/keywords.txt new file mode 100644 index 0000000..9260f27 --- /dev/null +++ b/lib/HexaServos/keywords.txt @@ -0,0 +1,30 @@ +# ═══════════════════════════════════════════════ +# Syntax Highlighting for HexaServos Library +# ═══════════════════════════════════════════════ +# KEYWORD1 = Classes (highlighted as types) +# KEYWORD2 = Methods (highlighted as functions) +# LITERAL1 = Constants + +# ── Classes ── +HexaServo KEYWORD1 +HexaServoConfig KEYWORD1 +HexaDCMotor KEYWORD1 +HexaDCMotorConfig KEYWORD1 + +# ── Servo Methods ── +begin KEYWORD2 +setPosition KEYWORD2 +moveBy KEYWORD2 +getPosition KEYWORD2 +getMinAngle KEYWORD2 +getMaxAngle KEYWORD2 +isReady KEYWORD2 +detach KEYWORD2 +attach KEYWORD2 +writeMicroseconds KEYWORD2 + +# ── Motor Methods ── +setSpeed KEYWORD2 +getSpeed KEYWORD2 +stop KEYWORD2 +setFlipped KEYWORD2 diff --git a/lib/HexaServos/lib/xtensa-esp32s3/libHexaServos_static.a b/lib/HexaServos/lib/xtensa-esp32s3/libHexaServos_static.a Binary files differnew file mode 100644 index 0000000..12ba0e1 --- /dev/null +++ b/lib/HexaServos/lib/xtensa-esp32s3/libHexaServos_static.a diff --git a/lib/HexaServos/library.json b/lib/HexaServos/library.json new file mode 100644 index 0000000..de9dc16 --- /dev/null +++ b/lib/HexaServos/library.json @@ -0,0 +1,15 @@ +{ + "name": "HexaServos", + "version": "1.0.0", + "description": "Servo and DC motor driver for HEXA COMMAND HUB using ESP32-S3 LEDC peripheral", + "keywords": ["servo", "motor", "ledc", "pwm", "esp32", "hexa"], + "authors": [ + { + "name": "Rush", + "maintainer": true + } + ], + "license": "MIT", + "platforms": "espressif32", + "frameworks": "arduino" +} diff --git a/lib/HexaServos/library.properties b/lib/HexaServos/library.properties new file mode 100644 index 0000000..c5982a1 --- /dev/null +++ b/lib/HexaServos/library.properties @@ -0,0 +1,9 @@ +name=HexaServos +version=1.0.0 +author=Rush +maintainer=Rush +sentence=Servo and DC motor driver for HEXA COMMAND HUB (ESP32-S3 LEDC) +paragraph=Standalone servo and DC motor control using ESP32-S3 LEDC peripheral. Servos at 50Hz/16-bit, DC motors at 20kHz/8-bit. No external dependencies. +category=Device Control +url= +architectures=esp32 diff --git a/lib/HexaServos/src/HexaDCMotor.h b/lib/HexaServos/src/HexaDCMotor.h new file mode 100644 index 0000000..41cf274 --- /dev/null +++ b/lib/HexaServos/src/HexaDCMotor.h @@ -0,0 +1,130 @@ +/** + * HexaDCMotor.h — DC Motor Driver for HEXA COMMAND HUB + * + * Controls a brushed DC motor via DIR + PWM pins using ESP32 LEDC at 20kHz. + * Speed range: -255 (full reverse) to +255 (full forward), 0 = stopped. + * 20kHz is above human hearing so there's no audible whine at low speeds. + * + * PWM: 20kHz, 8-bit (0-255) + * Part of: HexaServos library + * Author: Rush / HEXA Robotics + */ + +#pragma once + +#include <Arduino.h> + + +/** + * Configuration struct for one DC motor channel. + * Example: HexaDCMotorConfig cfg = { .dirPin = 32, .pwmPin = 33 }; + */ +struct HexaDCMotorConfig { + uint8_t dirPin; // GPIO pin for direction (HIGH = forward, LOW = reverse or coast) + uint8_t pwmPin; // GPIO pin for speed (LEDC PWM output) + bool flipped = false; // Set true to software-reverse this motor's direction. + // Useful when a motor is wired backwards on the frame — + // flip it here instead of rewiring. + + // Stiction floor. 0 (default) = off, behaviour unchanged. + // + // Nonzero: a nonzero command has its MAGNITUDE remapped into [minDrive, 255], + // so a small command still makes the motor actually turn instead of just + // buzzing. Below the floor a geared DC motor draws current and produces no + // rotation, which a closed loop reads as "no response" and answers by + // winding the command up further. + // + // Opposite of MOTOR_DEADBAND, and the two coexist on purpose: the deadband + // is a noise gate that collapses near-zero commands TO zero; minDrive lifts + // the surviving commands UP to something the motor can act on. + // + // MEASURE it, do not tune it — it is a property of this motor and gearbox. + // Ramp the command up from 0 and note where the wheels first turn. + uint8_t minDrive = 0; +}; + + +class HexaDCMotor { +public: + /** + * Constructor — stores config, does NOT touch hardware. + * Call begin() in setup() to initialize. + */ + HexaDCMotor(const HexaDCMotorConfig& config); + + /** + * Initialize the motor driver. Call in setup(). Returns true on success. + */ + bool begin(); + + /** + * Set motor speed. Range: -255 (full reverse) to +255 (full forward), 0 = coast stop. + * Values outside range are clamped. If flipped=true in config, directions are swapped. + */ + void setSpeed(int speed); + + /** + * Get the last commanded speed (-255 to +255). + */ + int getSpeed() const; + + /** + * Stop the motor (equivalent to setSpeed(0)). + */ + void stop(); + + /** + * Reverse direction in software without rewiring. + * Takes effect immediately — reapplies current speed + * with the new direction. + */ + void setFlipped(bool flipped); + + /** + * Returns true if begin() has been called successfully. + */ + bool isReady() const; + + // NOTE: there is no per-motor getCurrent() here. The board has ONE ACS712 + // sensor on the shared DC-motor rail — every motor would report the exact + // same total, which was a source of confusion (looked like each motor had + // its own reading, but it was one shared value sampled inconsistently). + // Read the actual rail current via `power.getMotorCurrent()` instead + // (see RobotFirmware/lib/HexaNRL/src/RobotConfig.h). + + /** + * Stop every motor that has been initialised via begin(). + * Called automatically by NRLRunner when an OpMode ends — students do not + * need to call this directly, but may call it from stop() if they wish. + */ + static void stopAll(); + + /** + * Clear the internal motor registry. + * Called by NRLRunner after the OpMode is deleted so stale pointers + * from destroyed class-member motors don't linger between runs. + * Motors re-register automatically on the next begin() call. + */ + static void clearRegistry(); + +private: + HexaDCMotorConfig m_cfg; + int m_currentSpeed = 0; + bool m_ready = false; + bool m_registered = false; // true once registered in s_registry + uint8_t m_channel = 0; // LEDC channel (Core 2.x only; unused on Core 3.x) + + // 20kHz is above human hearing — no motor whine at low PWM duty. + // 8-bit gives 256 speed steps (0-255), matching analogWrite() range. + static constexpr uint32_t MOTOR_FREQ_HZ = 20000; + static constexpr uint8_t MOTOR_RES_BITS = 8; + static constexpr int MAX_SPEED = 255; + + // Deadband: commands within ±MOTOR_DEADBAND of zero are rounded to zero. + // Prevents rapid direction toggling (and the resulting stall/jitter) when + // the computed drive signal oscillates near zero — most common on the inner + // motor during tank-drive turns where (forward − turn) ≈ 0. + // Value is ~4 % of full scale; imperceptible to the driver but eliminates + // the 25 Hz direction-flip that causes visible motor stall. + static constexpr int MOTOR_DEADBAND = 10; +}; diff --git a/lib/HexaServos/src/HexaServo.h b/lib/HexaServos/src/HexaServo.h new file mode 100644 index 0000000..9ba109d --- /dev/null +++ b/lib/HexaServos/src/HexaServo.h @@ -0,0 +1,186 @@ +/** + * HexaServo.h — Servo Driver for HEXA COMMAND HUB + * + * Drives the RKI-1248 servo using the ESP32-S3 LEDC peripheral. + * Pulse range: 500µs=0°, 1500µs=90°, 2500µs=180°, period=20ms (50Hz). + * Uses LEDC (not analogWrite) for 14-bit resolution and independent channel control. + * At 14-bit / 50Hz: one tick ~1.22µs — well within the servo's 3µs dead band. + * + * Tuned for: RKI-1248 (500-2500µs, 180°, 50-330Hz) + * Part of: HexaServos library + * Author: Rush / HEXA Robotics + */ + +#pragma once + +#include <Arduino.h> + + +/** + * Configuration struct for a single servo. + * Struct keeps the constructor clean and lets you define named configs as constants. + */ +struct HexaServoConfig { + + uint8_t signalPin; // ESP32-S3 GPIO pin routed through HEXA HUB + // to the servo's signal wire (typically orange/white) + + // Pulse width range (from the RKI-1248 datasheet) + uint16_t minPulseUs = 500; // Pulse width in microseconds at 0° + uint16_t maxPulseUs = 2500; // Pulse width in microseconds at 180° + + // Mechanical angle range + float minAngle = 0.0f; // Minimum angle in degrees + float maxAngle = 180.0f; // Maximum angle in degrees + + // Calibration offset + // If your servo's "0°" position is physically off by a few + // degrees (e.g., the horn isn't perfectly aligned), you can + // compensate here instead of hacking your application code. + // A positive offset shifts the output clockwise. + float offsetDeg = 0.0f; + + // Initial position + // Where the servo moves to when begin() is called. + // Default is center (90°). Set to your preferred safe position. + float startAngle = 90.0f; + + // Auto-settle: detach LEDC after this many ms of no setPosition() calls. + // 0 = DISABLED (default) — the servo keeps its 50Hz signal and HOLDS its + // commanded angle, including the INIT pose, instead of going limp/drooping. + // Holding is the right default: a servo that goes limp drops whatever the + // arm was carrying. + // + // ABOUT THIS BOARD: the servos share the battery rail. There is NO BEC and + // no separate servo regulator. A held servo can therefore buzz audibly on + // battery power alone. That buzz is the rail, not a firmware fault, and the + // commanded angle is still correct. + // + // Set to a positive value (e.g. 300) to trade holding for silence: the + // servo detaches once idle, stops buzzing, and goes limp until the next + // setPosition(). + uint32_t settleMs = 0; +}; + + +class HexaServo { +public: + /** + * Constructor — stores config, does NOT touch hardware. + * Global objects are constructed before hardware is ready, so all + * hardware init is deferred to begin(). Call begin() in setup(). + */ + HexaServo(const HexaServoConfig& config); + + /** + * Initialize the hardware. Call in setup(). + * Sets up the LEDC channel and moves servo to start position. + * Returns true on success. + */ + bool begin(); + + // Servo control + + void setPosition(float angleDeg); // Move to an absolute angle (0–180°) + void moveBy(float deltaDeg); // Move relative to current angle + float getPosition() const; // Read back the current angle + float getMinAngle() const; + float getMaxAngle() const; + bool isReady() const; + + // NOTE: there is no per-servo getCurrent() here. The board has no dedicated + // servo-current sensor — this value is DERIVED as (main total − motor + // branch) = CH1 − CH2 on the MCP3008, so it's the TOTAL current drawn by + // all servos together, not any individual servo's current. Every HexaServo + // would report the exact same value, which was a source of confusion. Read + // the actual rail current via `power.getServoCurrent()` instead + // (see RobotFirmware/lib/HexaNRL/src/RobotConfig.h). + + // Ramp rate: limit how fast the servo output moves (degrees per second). + // Default 0 = instant (no ramp). Set e.g. 90.0f to limit to 90°/s. + // Must be called before the first setPosition() to take effect from the start. + void setRampRate(float degsPerSec); + + // Advanced / power management + + /** + * Detach the LEDC channel from the pin. + * After detaching, the servo receives no signal and will + * go limp (no holding torque). Useful for saving power or + * when you want the servo to be manually movable. + */ + void detach(); + + /** + * Re-attach after a detach(). Moves to the last known angle. + */ + void attach(); + + /** + * Write a raw pulse width in microseconds. + * For advanced use — bypasses the angle-to-pulse mapping. + * Use this if you need to fine-tune beyond what angles give you. + */ + void writeMicroseconds(uint16_t pulseUs); + + /** + * Call every loop iteration — handles auto-detach after settleMs of idle. + * Called automatically by NRLRunner via tickAll(); student code does not need this. + */ + void tick(uint32_t nowMs); + + /** + * Tick all registered servos. Called by NRLRunner after loop(). + */ + static void tickAll(uint32_t nowMs); + + /** + * Clear the servo registry. Called by NRLRunner after the OpMode is deleted + * so stale pointers from destroyed servos don't linger between runs. + * Servos re-register on the next begin() call. + */ + static void clearRegistry(); + + /** + * Detach every registered servo (release LEDC + drive pin LOW so it goes limp). + * Called by NRLRunner on OpMode stop so servos don't keep emitting PWM after exit. + */ + static void detachAll(); + +private: + HexaServoConfig m_config; // Stored configuration (immutable after construction) + float m_currentAngle; // Last written angle in degrees (tracks hardware output) + bool m_ready; // Has begin() been called successfully? + bool m_attached; // Is the LEDC channel currently active? + uint8_t m_channel; // LEDC channel (Core 2.x only; unused on Core 3.x) + float m_degsPerSec; // Ramp rate limit; 0 = instant + uint32_t m_lastTickMs; // millis() at last setPosition() call (for ramp dt) + uint32_t m_lastWriteMs; // millis() at last setPosition() call (for auto-settle) + bool m_registered; // true once registered in s_registry + + static HexaServo* s_registry[8]; + static uint8_t s_count; + + // Internal helpers + // Converts an angle in degrees to a pulse width in microseconds, + // applying the calibration offset. This is the core math of + // the entire driver. + uint16_t angleToPulseUs(float angleDeg) const; + + // Converts a pulse width in microseconds to an LEDC duty value + // (the raw number we write to the hardware register). + uint32_t pulseUsToDuty(uint16_t pulseUs) const; + + // Reverse mapping: pulse width back to angle (for writeMicroseconds) + float pulseUsToAngle(uint16_t pulseUs) const; + + // Constants + static constexpr uint32_t SERVO_FREQ_HZ = 50; // 50Hz = standard servo frequency + static constexpr uint8_t SERVO_RESOLUTION = 14; // 14-bit = max for ESP32-S3 (16384 steps) + static constexpr uint32_t CYCLE_US = 20000; // 1/50Hz = 20ms = 20000µs + static constexpr uint32_t MAX_DUTY = 16384; // 2^14 + static constexpr uint32_t HEARTBEAT_MS = 250; // held-servo duty re-assert interval (see tick()) + + // 14-bit: SOC_LEDC_TIMER_BIT_WIDTH on ESP32-S3 is 14 — 16-bit causes ledcAttach() to fail. + // At 14-bit / 50Hz each tick ~1.22µs, still well within the RKI-1248's 3µs dead band. +}; |
