Skip to content

README.md

Machine-translated by Robotheca from the original in Chinese (opens in a new tab). License: MIT (opens in a new tab).

Brain Project (Raspberry Pi / Desktop)

The brain of Ganbei No. 1 β€” responsible for AI interaction, decision-making, and communication with the cerebellum.

Cross-platform: Python code runs the same set on Windows development and Raspberry Pi (Linux) deployment.

Architecture

car_brain/
β”œβ”€β”€ protocol/          Protocol module
β”‚   β”œβ”€β”€ crc8.py        CRC8 checksum (consistent with Arduino side)
β”‚   β”œβ”€β”€ commands.py    Command constants (consistent with Arduino side commands.h)
β”‚   └── frame.py       Frame packaging / parsing
β”œβ”€β”€ serial_link.py     Serial communication layer (frame transmission and reception)
β”œβ”€β”€ brain.py           Brain state machine (state view / heartbeat / button decision)
└── app.py             CLI entry point
tests/
β”œβ”€β”€ test_protocol.py   Protocol unit tests
└── test_brain_loop.py Brain↔Cerebellum memory joint debugging tests

Installation

pip install -r requirements.txt   # Requires pyserial

Running Tests

# Protocol unit tests + brain↔cerebellum joint debugging tests
python -m unittest discover -s tests -v

test_brain_loop.py uses two threads and memory pipes to simulate the complete "brain ↔ cerebellum" conversation, allowing protocol loopback verification without any hardware.

Running the Demo

Method 1: Connect to a Real/Virtual Serial Port
# Windows real serial port
python -m car_brain.app --port COM3 --demo

# Linux / Raspberry Pi
python -m car_brain.app --port /dev/ttyAMA0 --demo
Method 2: Directly drive the native cerebellum (no serial port required, recommended for joint debugging)

First, compile the native cerebellum program (see firmware/arduino/README.md), then let the brain drive it via stdin/stdout pipes:

python -m car_brain.app --native ../firmware/arduino/.pio/build/native/program.exe --demo

This way, you don't need to install com0com virtual serial ports on Windows to see the complete two-way conversation.

πŸŽ™οΈ Voice Control (Offline Recognition with VOSK)

Wake up by saying "Ganbei chulai" (cheers) into the microphone, then give a command; or say everything in one go (e.g., "Ganbei chulai turn on the light").

Supported commands: turn on the headlights / turn off the lights / mute / unmute / level one to four / emergency stop.

Preparation (one-time)
# 1. Create a Python 3.13 virtual environment (system 3.14 may not fully support speech libraries)
py -3.13 -m venv .venv

# 2. Install dependencies
.venv\Scripts\python.exe -m pip install -r requirements.txt

# 3. Download the VOSK Chinese model (~42MB) and extract it to the user directory:
#    https://alphacephei.com/vosk/models/vosk-model-small-cn-0.22.zip
#    The extracted directory should be ~/vosk-model-small-cn-0.22
#    Alternatively, set the environment variable VOSK_MODEL to point to the model directory
Running Voice Control
.venv\Scripts\python.exe -m car_brain.app --native <cerebellum_exe_path> --voice --seconds 120

A microphone is required. Voice recognition is offline and does not require an internet connection. On Windows, it will say "I'm listening, please give a command" as voice feedback.

How to Add / Remove Commands

The "list" of commands is in the COMMAND_RULES table in car_brain/voice.py (each line = one group of synonyms + command):

COMMAND_RULES = (
    (("turn on the headlights", "turn on the lights", "turn on the light", "lights on"), ("light", "on")),  # synonyms β†’ command
    (("brake", "emergency brake", "stop"), ("ebrk", None)),
)
  • Add synonyms: Simply add words to the keyword tuple in the corresponding line
  • Add new commands, reuse existing actions: Add a new line and use an existing command name (e.g., strip)
  • Add completely new actions: Add a new line + add a processing branch in on_voice_command in car_brain/app.py; if a new protocol command is needed, also add a method in car_brain/brain.py
  • Delete commands: Simply delete the corresponding line
Changing the TTS Voice

Set the environment variable TTS_VOICE (default: zh-CN-XiaoxiaoNeural ζ™“ζ™“):

set TTS_VOICE=zh-CN-YunxiNeural   # Yunxi (male voice)
set TTS_VOICE=zh-CN-XiaoyiNeural  # Xiaoyi (female voice)

Options: XiaoxiaoNeural (ζ™“ζ™“) / YunxiNeural (δΊ‘εΈŒ) / YunyangNeural (云扬) / XiaoyiNeural (ζ™“δΌŠ).

Directory Planning (Future Iterations)

  • audio/ β€”β€” Engine sound synthesis + audio management
  • cloud/ β€”β€” 4G communication / MQTT / app integration
  • gps/ β€”β€” Location reporting

Files by @RevolutionLA, at commit a7b0c9d, read from GitHub in your browser. Hardware license: MIT; software license: MIT. Robotheca doesn't review code: read it before running it.