Skip to content

Repository files navigation

Hexapod Robot - Go Implementation

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.

Features

  • 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

Hardware Requirements

  • 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

Software Requirements

  • Go 1.21 or later
  • Raspberry Pi OS with I2C enabled

Installation

  1. 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
  2. Enable I2C on Raspberry Pi:

    sudo raspi-config
    # Navigate to Interface Options > I2C > Enable
  3. Clone and build the project:

    cd /path/to/this/project
    go mod tidy
    go build -o hexapod

Hardware Connections

PCA9685 PWM Driver

  • VCC → 3.3V or 5V
  • GND → Ground
  • SDA → GPIO 2 (Pin 3)
  • SCL → GPIO 3 (Pin 5)

Bluetooth Module

  • VCC → 3.3V
  • GND → Ground
  • TX → GPIO 15 (Pin 10) - /dev/serial0
  • RX → GPIO 14 (Pin 8) - /dev/serial0

Servo Connections

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

Usage

Basic Operation

  1. Run the program:

    sudo ./hexapod
  2. With custom Bluetooth port:

    sudo ./hexapod -port /dev/ttyUSB0

Bluetooth Control Protocol

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).

Code Structure

  • 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

Configuration

Servo Calibration

Edit the servo parameters in servo.go:

{Channel: 0, MinAngle: 50, MaxAngle: 130, TargetAngle: 90, AngleOffset: 0}

Leg Dimensions

Modify the constants in ik.go:

const (
    LengthTrochanter = 50.0  // mm
    LengthFemur      = 80.0  // mm
    LengthTibia      = 120.0 // mm
)

Movement Parameters

Adjust walking parameters in walkgait.go:

var (
    legLiftDistance = 25.0  // How high legs lift during steps
    legLiftIncline  = 2.0   // Leg lift slope
)

Differences from Arduino Version

  1. Language: Go instead of C++
  2. Platform: Raspberry Pi instead of Arduino/STM32
  3. I2C Library: Gobot instead of Adafruit libraries
  4. Concurrency: Go's goroutines for better multi-tasking
  5. Error Handling: More robust error handling and logging
  6. Memory Management: Automatic garbage collection

Troubleshooting

I2C Issues

# Check if I2C is enabled
ls /dev/i2c-*

# Scan for I2C devices
i2cdetect -y 1

Servo Issues

  • Check power supply (servos need adequate current)
  • Verify PCA9685 connections
  • Adjust servo angle limits in code

Bluetooth Issues

  • Ensure correct baud rate (9600)
  • Check serial port permissions
  • Verify Bluetooth module connections

Performance Notes

  • The Go implementation runs at ~100Hz update rate
  • Memory usage is typically 10-20MB
  • CPU usage on Pi Zero is around 15-30%

Future Enhancements

  • Web interface for remote control
  • Camera integration
  • Sensor feedback (accelerometer, gyroscope)
  • Machine learning for adaptive gaits
  • ROS integration

Contributing

Pull requests are welcome! Please ensure code follows Go best practices and includes appropriate testing.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages