↵to select↓↑to navigateescto close
AI SPIDER DOCSEARCH
05 Host AI gateway
// ON THIS PAGE
1. Cognitive pipeline and multimodal agent workflow2. Network topology and ingress routing3. MQTT communication topic taxonomy4. Binary audio stream protocol specificationPSRAM ring buffer and jitter management5. Canonical task decomposition JSON contract6. Cognitive tool registry and function calling7. Dynamic video stream relay and camera controlCamera tuning parameter matrix

Host gateway and voice AI engine

Raspberry Pi 4 network orchestrator, Faster-Whisper ASR, Piper TTS, multimodal agent contracts, and binary audio streaming.

The Pi-Hub executes on a Raspberry Pi 4B (or Linux host), coordinating local speech recognition, cloud vision-language models, dynamic camera relays, and low-latency binary audio streaming.

1. Cognitive pipeline and multimodal agent workflow

sequenceDiagram
    actor User
    participant UI as Web-UI Client
    participant MQTT as Mosquitto Broker (:1883 / :9001)
    participant AI as Hexapod AI Service
    participant CAM_RELAY as Camera Relay (:8088)
    participant OMNI as OmniRoute Gateway (Llama 3.3)
    participant S3 as ESP32-S3 Controller

    User->>UI: Speak Command ("Walk forward and wave")
    UI->>MQTT: Publish hexapod/{id}/ai (Audio/Text)
    MQTT->>AI: Dispatch Payload
    AI->>AI: Speech-to-Text (Faster-Whisper int8)

    opt Visual Scene Grounding
        AI->>CAM_RELAY: GET /snapshot
        CAM_RELAY-->>AI: Cached JPEG Frame
    end

    AI->>OMNI: POST /v1/chat/completions (Prompt + Frame + Memory)
    OMNI-->>AI: Tool Calls / JSON Kinematic Plan & Spoken Text

    par Concurrent Audio Streaming
        AI->>MQTT: Stream 22.05kHz Binary Frames (hexapod/{id}/audio)
        MQTT->>S3: Forward Raw PCM Chunks
        S3-->>User: Acoustic Speech Output (I2S DAC)
    and Concurrent Motion Execution
        AI->>MQTT: Stream 20Hz Motion Leases (hexapod/{id}/cmd)
        MQTT->>S3: Execute Inverse Kinematics Loop
        S3-->>User: Coordinated 18-DoF Movement
    end

2. Network topology and ingress routing

flowchart TD
    WAN["Internet / WAN Uplink (Wi-Fi or Ethernet)"] --> PI["Raspberry Pi 4B (Gateway Host)"]
    
    subgraph PiHubGateway ["Pi-Hub Networking & Ingress"]
        AP["NetworkManager Hotspot AP<br/>SSID: 'spiderlink' (192.168.4.1/24)"]
        NAT["IPv4 Dynamic NAT Masquerade<br/>iptables POSTROUTING"]
        NGINX["Nginx Reverse Proxy<br/>Port 80 / 443 / 9001 WS / 8088 HTTP"]
    end

    PI --> AP
    AP --> NAT
    NAT --> NGINX

    subgraph InternalNodes ["Connected Hardware Nodes"]
        S3_NODE["ESP32-S3 Controller (192.168.4.2)"]
        CAM_NODE["ESP32-CAM Node (192.168.4.3)"]
        CLIENT_NODE["Web Dashboard Client (192.168.4.x)"]
    end

    NGINX <--> S3_NODE
    NGINX <--> CAM_NODE
    NGINX <--> CLIENT_NODE

3. MQTT communication topic taxonomy

flowchart TD
    ROOT["hexapod/"]
    
    subgraph S3_Topics ["ESP32-S3 Controller ('hexapod-s3-01')"]
        S3_CMD["hexapod/{id}/cmd (Inbound: Velocity, Sequences, Leases)"]
        S3_TEL["hexapod/{id}/telemetry (Outbound: 10 Hz State)"]
        S3_CFG["hexapod/{id}/config (Retained: Channel Maps)"]
        S3_AUD["hexapod/{id}/audio (Inbound: 10-byte Framed PCM)"]
        S3_STA["hexapod/{id}/audio/status (Outbound: Audio Buffer State)"]
    end

    subgraph CAM_Topics ["ESP32-CAM Node ('hexapod-cam-01')"]
        CAM_CMD["hexapod/{cam_id}/cmd (Inbound: Presets, Flashlight, FPS)"]
        CAM_TEL["hexapod/{cam_id}/telemetry (Outbound: 1 Hz IP Discovery)"]
    end

    subgraph AI_Topics ["Cognitive Agent Pipeline"]
        AI_CMD["hexapod/{id}/ai/memory/cmd (Memory Updates)"]
        AI_STA["hexapod/{id}/ai/status (Heartbeat & Tool State)"]
    end

    ROOT --> S3_Topics
    ROOT --> CAM_Topics
    ROOT --> AI_Topics

4. Binary audio stream protocol specification

Low-latency speech audio streams from the Raspberry Pi ai-service to the ESP32-S3 TaskAudio using framed raw PCM packets to eliminate JSON Base64 decode overhead:

Byte offsetField identifierTypeEndiannessValidation criteria
0x00MAGIC_BYTEuint8_t—Must equal 0xAA to distinguish from JSON payloads.
0x01ACTION_FLAGuint8_t—0x00 = Stream Audio Chunk; 0x01 = Abort Active Stream.
0x02..0x05FLOW_IDuint32_tLittleUnique pseudo-random stream identifier generated per utterance.
0x06..0x07SEQUENCE_IDXuint16_tLittle0-indexed packet sequence counter (0…Total−10 \dots \text{Total}-1).
0x08..0x09TOTAL_CHUNKSuint16_tLittleTotal packets in utterance (00 denotes unbounded media stream).
0x0A..NPCM_PAYLOADint16_t[]Little22,050 Hz Mono 16-bit PCM samples (max 4,096 bytes per chunk).

PSRAM ring buffer and jitter management

  1. Incoming frames on Core 0 extract the raw PCM slice and push to a 512KB PSRAM RingBuffer.
  2. TaskAudio enforces a 16,384-byte prebuffer threshold (≈370 ms\approx 370\text{ ms}) before triggering the I2S DMA controller, preventing buffer underruns during Wi-Fi latency spikes.
  3. Software volume is scaled sample-by-sample using Q15 fixed-point multiplication:
Sampleout=Samplein×volQ1532768,where volQ15∈[0,32767]\text{Sample}_{\text{out}} = \frac{\text{Sample}_{\text{in}} \times \text{volQ15}}{32768}, \quad \text{where } \text{volQ15} \in [0, 32767]

5. Canonical task decomposition JSON contract

{
  "task_title": "Inspect and Wave",
  "thought": "User requested visual inspection followed by greeting. Moving forward, turning flashlight on, capturing frame, and waving.",
  "speech": "Advancing to inspect the target area.",
  "order": "tts_first",
  "camera": {
    "preset": "inspection",
    "flash": 40
  },
  "audio": {
    "volume": 0.5,
    "alarm": "curious"
  },
  "timeline": [
    {
      "type": "gait",
      "id": "walk_forward",
      "duration_ms": 3000,
      "params": { "vx": 45.0, "vy": 0.0, "omega": 0.0, "gait": "tripod", "step_height": 40.0 }
    },
    {
      "type": "gesture",
      "id": "wave",
      "duration_ms": 2200
    }
  ]
}

6. Cognitive tool registry and function calling

Tool identifierParametersFunctional role
inspect_scenequery: strCaptures live frame from /snapshot to ground multimodal reasoning.
get_weatherlocation: strRetrieves live temperature, humidity, and forecast via Open-Meteo API.
web_searchquery: strPerforms instant web summaries via DuckDuckGo and Wikipedia APIs.
set_timerduration_seconds: int, label: strSets asynchronous countdown timer with audio alarms upon completion.
cancel_timerlabel: strCancels an active countdown timer.
play_musicquery: strStreams YouTube/local audio via yt-dlp and ffmpeg with auto-ducking.

7. Dynamic video stream relay and camera control

flowchart LR
    ESP_CAM["ESP32-CAM Node<br/>(OV2640 Sensor)"] -->|HTTP Pull :81/stream| RELAY["Camera Relay Service<br/>(Port 8088 Proxy)"]
    
    RELAY -->|1-to-N MJPEG Stream| WEB["Web-UI Dashboard"]
    RELAY -->|On-Demand /snapshot| VLM["VLM Perception Loop"]

Camera tuning parameter matrix

Parameter nameValue range / typeSensor hardware effect
presetnight_vision, inspection, stealth, low_power, defaultConfigures flashlight, exposure, gain ceiling, and resolution atomically.
flash0 to 100 (%)Modulates LEDC Channel 1 duty cycle driving the white LED via GPIO 4.
fps1 to 30 (Hz)Adjusts capture loop task delay (framePeriodUs).
framesize96X96, QVGA, VGA, HD, FHDReconfigures sensor active pixel array windowing.
quality0 to 63JPEG quantization matrix scaling (8=High Detail8 = \text{High Detail}, 12=Default12 = \text{Default}).
crop[startX, startY, width, height]Hardware sensor windowing for zero-latency digital zoom.
// FULLSCREEN_VIEWPORT
100%