Skip to content

2 KB

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

Cerebellum Project (Arduino Mega 2560)

The cerebellum firmware for Cheers No. 1 — responsible for perception (pedals/buttons/sensors) and execution (motors/lights).

Architecture

src/main.cpp          Cerebellum main program (platform-independent logic)
src/protocol/         Protocol module (pure C++, no dependency on Arduino API)
  ├─ commands.h        Command constants (consistent with Python-side commands.py)
  ├─ crc8.h/.cpp       CRC8 checksum
  └─ frame.h/.cpp      Frame packaging / parsing
src/hal/              Hardware Abstraction Layer (HAL)
  ├─ hal.h             HAL interface
  ├─ hal_sim.cpp       Desktop simulation implementation (native environment)
  └─ hal_mega.cpp      Real pin implementation (mega2560 environment)
test/test_frame.cpp   Protocol unit test (Unity)

Layering principle: main.cpp and protocol/ only call interfaces defined in hal.h and do not directly touch hardware. This allows the same logic to run on both PC simulation and real boards.

Common Commands

# Compile and run protocol unit tests (works without a board)
pio test -e native

# Compile desktop simulation program (native exe, stdin/stdout as serial port)
pio run -e native
# Compilation output is at .pio/build/native/program.exe

# Compile real firmware (to be flashed to Arduino Mega 2560)
pio run -e mega2560

# Flash firmware
pio run -e mega2560 -t upload

Tip: After installing PlatformIO in VSCode, you can directly click the checkmark (build) and arrow (upload) in the IDE. Use ~/.platformio/penv/Scripts/pio.exe in the command line.

Desktop Simulation Demo (No Board Required)

After compiling the native program, it will treat stdin as serial reception and stdout as serial transmission:

# Run directly and observe button events/status reports (stderr displays actuator actions)
.pio/build/native/program.exe

A more complete demo: drive it with a Python brain (see software/raspberry_pi):

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

Wiring on Real Hardware

See docs/hardware_io_map.md for the pin assignment. The pins have already been mapped in hal_mega.cpp according to this document.

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.