Embedded firmware and motion core
FreeRTOS SMP dual-core task allocation, analytical 3-DoF IK derivations, gait generator, and safety watchdog.
1. FreeRTOS dual-core task partitioning
flowchart TD
subgraph ESP32S3 ["ESP32-S3 Dual-Core SMP Processor"]
subgraph Core0 ["Core 0: Network Ingress, Audio DMA & Host Comms"]
TASK_NET["TaskNetwork (Pri 2, 8KB Stack)<br/>• WiFiMulti Auto-Reconnect Engine<br/>• MQTT Ingress & 10 Hz Telemetry Loop<br/>• Non-blocking LogSink Drainer (UART)<br/>• Binary Audio 10-byte Frame Ingest"]
TASK_AUDIO["TaskAudio (Pri 1, 8KB Stack)<br/>• 512KB PSRAM RingBuffer Reader<br/>• 16,384-byte Prebuffer Threshold<br/>• Q15 Fixed-Point Volume Scaler<br/>• I2S DMA Direct Memory Writes"]
end
subgraph Core1 ["Core 1: Deterministic Real-Time Control"]
TASK_CTRL["TaskControl (Pri 3, 4KB Stack)<br/>• Hard RTOS Loop (100 Hz / 10ms)<br/>• Analytical 3-DoF Inverse Kinematics<br/>• 6-DoF Body Pose Transformation<br/>• Omnidirectional Gait Engine<br/>• SequencePoser Keyframe Engine<br/>• Two-Stage Safety Watchdog<br/>• Dual PCA9685 I2C Writes (400 kHz)"]
end
end
PSRAM["512KB PSRAM Audio RingBuffer"]
I2S_HW["MAX98357A I2S DMA Buffer"]
I2C_HW["Dual PCA9685 PWM Registers"]
TASK_NET -->|Pushes PCM Audio Chunks| PSRAM
PSRAM -->|Streams Buffered Audio| TASK_AUDIO
TASK_AUDIO -->|Writes DMA Registers| I2S_HW
TASK_CTRL -->|Executes 100 Hz Loop| I2C_HW
Core isolation and mutex guarantees
- Non-blocking control loop:
TaskControlon Core 1 executes deterministically every without preemption from Wi-Fi interrupts. Non-blocking logging pushes string pointers to a bounded FreeRTOS queue (xQueueSend(..., 0)). If the queue fills, logs drop silently without delaying the motion loop. - I2C bus mutex: All register writes to the dual PCA9685 controllers are protected by
m_i2cMutexwith a hardware timeout to prevent deadlocks from electrical noise.
2. Analytical 3-DoF inverse kinematics
Each leg operates as an open kinematic chain with three revolute joints: Coxa (, hip pan), Femur (, thigh lift), and Tibia (, knee reach).
flowchart LR
HIP["Body Center (Mount Point M_i)"] -->|L1: 52mm| COXA["Coxa Joint (α: Hip Pan)"]
COXA -->|L2: 66mm| FEMUR["Femur Joint (β: Thigh Lift)"]
FEMUR -->|L3: 132mm| TIBIA["Tibia Joint (γ: Knee Reach)"]
TIBIA --> FOOT["Target Foot Tip (X, Y, Z)"]
Mathematical kinematic derivation
Given a target Cartesian coordinate in the local frame of hip mounting point :
- Coxa Angle ():
- Planar projection and reach vector ():
- Reachability boundary clamping:
- Femur angle ():
- Tibia angle ():
3. 6-DoF rigid body pose transformation
To apply rigid body translations and Tait-Bryan Euler rotations relative to ground contact points, coordinates transform through forward and inverse mounting orientations.
For leg with physical mounting offset and mounting angle :
- Foot position in body coordinate frame:
- Inverse rigid body transformation:
Where .
- Local leg frame projection for inverse kinematics:
Where the principal rotation matrices are defined as:
4. Omnidirectional gait generator engine
The GaitGenerator modulates a normalized phase accumulator driven by cycle time :
flowchart TD
PHASE["Continuous Phase Clock: Φ(t + dt) = (Φ(t) + dt / T_cycle) mod 1.0"]
subgraph Gaits ["Gait Phase Offset Distributions"]
TRIPOD["Tripod Gait (2-Phase, σ = 0.5)<br/>Offsets: [0.0, 0.5, 0.5, 0.0, 0.0, 0.5]"]
RIPPLE["Ripple Gait (6-Phase, σ = 0.333)<br/>Offsets: [0.0, 0.667, 0.333, 0.5, 0.167, 0.833]"]
WAVE["Wave Gait (6-Phase, σ = 0.167)<br/>Offsets: [0.0, 0.167, 0.333, 0.5, 0.667, 0.833]"]
end
subgraph Trajectory ["Trajectory Synthesis for Leg Phase ϕ_i"]
SWING["Swing Phase (ϕ_i < σ, Foot in Flight)<br/>Z = Z_base + sin(π · τ) · H_step"]
STANCE["Stance Phase (ϕ_i ≥ σ, Ground Propulsion)<br/>Z = Z_base"]
end
PHASE --> Gaits
Gaits --> Trajectory
Foot trajectory formulations
For leg phase :
- Swing phase (, foot in flight):
- Stance phase (, ground propulsion):
Where ground stride vectors incorporate turning angular rate :
5. Trajectory interpolation and easing functions
The SequencePoser dynamically transitions between poses using analytical easing functions:
| Easing identifier | Mathematical formula | Dynamic motion characteristics |
|---|---|---|
LINEAR | Constant velocity; discontinuous acceleration. | |
EASE_IN_OUT_QUAD | Quadratic acceleration and deceleration. | |
EASE_IN_OUT_CUBIC | Standard: Smooth S-curve transition profile. | |
EASE_IN_OUT_SINE | Harmonic sinusoidal profile for periodic gestures. | |
MINIMUM_JERK | Quintic polynomial: Zero boundary jerk (), preventing chassis resonance. |
6. Multi-stage safety watchdog state machine
stateDiagram-v2
[*] --> BootReset : Power On / ESP32-S3 Reset
state BootReset {
OE_HIGH : Output Enable (OE) = HIGH (GPIO 13)
LIMP : Servos in Limp State (0 mA Draw)
}
BootReset --> SoftStartRamp : Network Connected & Motion Command Received
state SoftStartRamp {
RAMP : Slew Rate Ramps from 30°/s to 180°/s
DUR : Duration = 1.0 Second
}
SoftStartRamp --> NormalOperation : Ramp Complete
state NormalOperation {
IK_LOOP : Full-Speed 100 Hz IK Loop
BURST : Staggered PCA9685 PWM Writes
}
NormalOperation --> VelocityBrake : Inactivity > 3000ms
NormalOperation --> HardwareLimp : Inactivity > 15000ms
state VelocityBrake {
BRAKE : Velocity Clamped to 0
STANCE : Robot Holds Neutral Stance
FLAG : watchdog_braked = true
}
state HardwareLimp {
LIMP_OE : OE Pulled HIGH (GPIO 13)
POWERDOWN : Servos Powered Down (0 mA)
AUDIO_ACTIVE : Audio Subsystem Remains Awake
}
VelocityBrake --> NormalOperation : Motion Command Received
HardwareLimp --> SoftStartRamp : Motion Command Received
7. Toolchain compilation and firmware flashing
# Compile and flash ESP32-S3 Motion & Audio Controller
cd firmware/s3-main
pio run -e esp32s3 --target upload
pio device monitor -b 115200
# Compile and flash ESP32-CAM Vision & Sensor Node
cd firmware/cam-main
pio run -e esp32cam --target upload
pio device monitor -b 115200