Implementation:78 Xiaozhi esp32 Robot Controllers
| Knowledge Sources | |
|---|---|
| Domains | Robotics, MCP_Tools, Embedded_Systems |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
Concrete robot controller implementations for XiaoZhi-compatible bipedal robot boards (Otto and Electron), providing servo-based movement libraries, MCP-protocol action dispatch, WebSocket control servers, and emoji display rendering.
Description
Two robot platforms are supported under their respective board directories:
| Component | Otto Robot | Electron Bot |
|---|---|---|
| Movement library | Otto (otto_movements.h/.cc, 966 lines) |
Movements (movements.h/.cc, 549 lines)
|
| Controller | OttoController (otto_controller.cc, 854 lines) |
ElectronBotController (electron_bot_controller.cc, 674 lines)
|
| Board impl | OttoRobot (otto_robot.cc, 420 lines) |
ElectronBot (electron_bot.cc, 147 lines)
|
| Emoji display | OttoEmojiDisplay |
ElectronEmojiDisplay
|
| WebSocket server | WebSocketControlServer |
WebSocketControlServer
|
| Power manager | board-specific | board-specific |
| Servo driver | Oscillator (oscillator.h/.cc) |
Oscillator (oscillator.h/.cc)
|
Otto class manages 6 servos (left/right leg, left/right foot, optional left/right hand) with predetermined gaits: Walk, Turn, Jump, Sit, UpDown, Swing, Moonwalker, Crusaito, Flapping, HandsUp/Down, Windmill, and more. Uses Oscillator for smooth servo control with trim, speed limits, and phase-difference oscillation.
OttoController registers MCP tools (self.otto.*) for AI-driven robot actions: walk, turn, jump, sit, wave, dance, etc. Dispatches actions via FreeRTOS queue to an action task. Also supports WebSocket control for direct remote commands.
Movements (Electron) manages 6 servos (right/left pitch/roll, body, head) with similar gait patterns. ElectronBotController provides MCP tools and WebSocket control.
Both controllers share the WebSocketControlServer pattern for browser/app-based remote control and the Oscillator servo driver.
Usage
Reference this implementation when:
- Working with Otto or Electron robot boards
- Understanding servo-based bipedal robot movement
- Implementing MCP tools for AI-controlled robot actions
- Adding WebSocket-based remote control for robots
Code Reference
Source Location
- Repository: XiaoZhi ESP32
- Otto Robot:
main/boards/otto-robot/— otto_movements.h/.cc, otto_controller.cc, otto_robot.cc, oscillator.h/.cc, otto_emoji_display.h/.cc, websocket_control_server.h/.cc, power_manager.h - Electron Bot:
main/boards/electron-bot/— movements.h/.cc, electron_bot_controller.cc, electron_bot.cc, oscillator.h/.cc, electron_emoji_display.h/.cc, websocket_control_server.h/.cc, power_manager.h
Signature
// Otto movement library
class Otto {
public:
void Init(int left_leg, int right_leg, int left_foot, int right_foot,
int left_hand = -1, int right_hand = -1);
void AttachServos();
void DetachServos();
void SetTrims(int left_leg, int right_leg, int left_foot, int right_foot,
int left_hand = 0, int right_hand = 0);
void Home(bool hands_down = true);
// Predetermined motions
void Walk(float steps = 4, int period = 1000, int dir = FORWARD, int amount = 0);
void Turn(float steps = 4, int period = 2000, int dir = LEFT, int amount = 0);
void Jump(float steps = 1, int period = 2000);
void Sit();
void Moonwalker(float steps = 1, int period = 900, int height = 20, int dir = LEFT);
void HandsUp(int period = 1000, int dir = 0);
void HandWave(int dir = LEFT);
void Windmill(float steps = 10, int period = 500, int amplitude = 90);
// ... many more gaits
void EnableServoLimit(int speed_limit_degree_per_sec = SERVO_LIMIT_DEFAULT);
private:
Oscillator servo_[SERVO_COUNT]; // SERVO_COUNT = 6
};
// Servo oscillator driver
class Oscillator {
public:
Oscillator(int pin, int trim = 0);
void Attach();
void Detach();
void SetPosition(int angle);
void Oscillate(int amplitude, int offset, int period, double phase, float steps);
};
// Otto MCP controller (registers self.otto.* tools)
class OttoController {
// Registers MCP tools: self.otto.walk, self.otto.turn, self.otto.jump, etc.
// Dispatches actions via FreeRTOS queue to action task
};
// Electron bot controller (registers self.electron.* tools)
class ElectronBotController {
// Similar MCP tool registration for Electron bot
};
Import
// Otto robot
#include "otto_movements.h" // Otto class
#include "oscillator.h" // Oscillator class
// Electron bot
#include "movements.h" // Movements class
#include "oscillator.h" // Oscillator class
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| servo pins | int | Yes | GPIO pins for each servo (left_leg, right_leg, etc.) |
| steps | float | Yes | Number of steps for walk/turn motions |
| period | int | Yes | Motion period in ms |
| dir | int | Yes | Direction (FORWARD/BACKWARD, LEFT/RIGHT) |
| height/amplitude | int | Yes | Motion height/amplitude in degrees |
| MCP commands | JSON | Yes | AI-issued MCP tool calls (self.otto.*, self.electron.*) |
Outputs
| Name | Type | Description |
|---|---|---|
| Servo positions | Physical | PWM signals to servo motors |
| MCP response | cJSON | Action result (success/failure) |
Usage Examples
Otto Robot Walk via MCP
#include "otto_movements.h"
#include "mcp_server.h"
// In board constructor:
Otto otto;
otto.Init(CONFIG_LEFT_LEG_PIN, CONFIG_RIGHT_LEG_PIN,
CONFIG_LEFT_FOOT_PIN, CONFIG_RIGHT_FOOT_PIN,
CONFIG_LEFT_HAND_PIN, CONFIG_RIGHT_HAND_PIN); // hands optional
otto.AttachServos();
otto.Home();
// MCP tools registered by OttoController allow AI to say:
// "Walk forward 4 steps" -> self.otto.walk(steps=4, dir=FORWARD)
otto.Walk(4, 1000, FORWARD, 0);
Electron Bot Head Movement
#include "movements.h"
Movements electron;
electron.Init(CONFIG_RIGHT_PITCH, CONFIG_RIGHT_ROLL,
CONFIG_LEFT_PITCH, CONFIG_LEFT_ROLL,
CONFIG_BODY, CONFIG_HEAD);
electron.AttachServos();
electron.Home();
// Wave head via MCP: self.electron.head(direction=LEFT)