This is a Go implementation of the hexapod robot controller using Gobot for Raspberry Pi Zero. It's a complete rewrite of the original Arduino C++ code found in the Hexapod_code directory.
- Inverse Kinematics: Full 3DOF inverse kinematics for each leg
- Walking Gait: Tripod gait walking pattern with smooth movement
- Bluetooth Control: Wireless control via Bluetooth serial communication
- Servo Control: PWM servo control via PCA9685 I2C driver
- Modular Design: Clean separation of concerns with dedicated modules
- Raspberry Pi Zero (or any Raspberry Pi)
- PCA9685 16-Channel PWM driver board
- 18 servo motors (3 per leg × 6 legs)
- Bluetooth module (HC-05/HC-06 or similar)
- Power supply suitable for servos
- Go 1.21 or later
- Raspberry Pi OS with I2C enabled
-
Install Go on your Raspberry Pi:
wget https://go.dev/dl/go1.21.linux-armv6l.tar.gz sudo tar -C /usr/local -xzf go1.21.linux-armv6l.tar.gz echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc source ~/.bashrc
-
Enable I2C on Raspberry Pi:
sudo raspi-config # Navigate to Interface Options > I2C > Enable -
Clone and build the project:
cd /path/to/this/project go mod tidy go build -o hexapod
- VCC → 3.3V or 5V
- GND → Ground
- SDA → GPIO 2 (Pin 3)
- SCL → GPIO 3 (Pin 5)
- VCC → 3.3V
- GND → Ground
- TX → GPIO 15 (Pin 10) - /dev/serial0
- RX → GPIO 14 (Pin 8) - /dev/serial0
Connect servos to PCA9685 channels as follows:
- Leg 0 (Front Right): Channels 0-2
- Leg 1 (Middle Right): Channels 3-5
- Leg 2 (Back Right): Channels 6-8
- Leg 3 (Back Left): Channels 9-11
- Leg 4 (Middle Left): Channels 12-14
- Leg 5 (Front Left): Channels 15-17
-
Run the program:
sudo ./hexapod
-
With custom Bluetooth port:
sudo ./hexapod -port /dev/ttyUSB0
The robot expects an 11-byte data packet via Bluetooth:
| Byte | Purpose | Values |
|---|---|---|
| 0 | Stand/Sit Command | 50=Sit, 100=Stand |
| 1 | Rotation Input | 0-200 (100=center) |
| 2 | Reserved | - |
| 3 | Y Direction | 0-200 (100=center) |
| 4 | X Direction | 0-200 (100=center) |
| 5 | Max Speed | 0-255 |
| 6 | Ground Clearance | 0-255 |
| 7 | Step Radius | 0-255 |
| 8-10 | Reserved/Future Use | - |
Each packet must be preceded by a sync byte (0x00).
- main.go: Main program entry point and loop
- math.go: Vector math utilities (Vector2, Vector3)
- servo.go: Servo control and hardware interface
- ik.go: Inverse kinematics calculations
- walkgait.go: Walking gait and movement logic
- bluetooth.go: Bluetooth communication handling
Edit the servo parameters in servo.go:
{Channel: 0, MinAngle: 50, MaxAngle: 130, TargetAngle: 90, AngleOffset: 0}Modify the constants in ik.go:
const (
LengthTrochanter = 50.0 // mm
LengthFemur = 80.0 // mm
LengthTibia = 120.0 // mm
)Adjust walking parameters in walkgait.go:
var (
legLiftDistance = 25.0 // How high legs lift during steps
legLiftIncline = 2.0 // Leg lift slope
)- Language: Go instead of C++
- Platform: Raspberry Pi instead of Arduino/STM32
- I2C Library: Gobot instead of Adafruit libraries
- Concurrency: Go's goroutines for better multi-tasking
- Error Handling: More robust error handling and logging
- Memory Management: Automatic garbage collection
# Check if I2C is enabled
ls /dev/i2c-*
# Scan for I2C devices
i2cdetect -y 1- Check power supply (servos need adequate current)
- Verify PCA9685 connections
- Adjust servo angle limits in code
- Ensure correct baud rate (9600)
- Check serial port permissions
- Verify Bluetooth module connections
- The Go implementation runs at ~100Hz update rate
- Memory usage is typically 10-20MB
- CPU usage on Pi Zero is around 15-30%
- Web interface for remote control
- Camera integration
- Sensor feedback (accelerometer, gyroscope)
- Machine learning for adaptive gaits
- ROS integration
Pull requests are welcome! Please ensure code follows Go best practices and includes appropriate testing.