Skip to content

Rover Software Setup

Margarita Matson edited this page Jul 7, 2026 · 4 revisions

This section describes the software setup used for the AstroSheep M.A.R.S. Rover / Marsy project. The setup is based on the original 4tronix M.A.R.S. Rover Python software for Raspberry Pi Zero and Raspberry Pi Zero 2 W.

The original 4tronix installation guide is available here:

A copy of the original 4tronix rover code used as the starting point for this project is stored in the Marsy repository:

4tronix mirror

The project keeps the original vendor code separate from our own experiments so that we can always return to a known working baseline.

Powering and Starting the Rover

The rover has two power systems:

  1. The rover battery pack powers the motors, servos, sensors, and LEDs.
  2. The Raspberry Pi Zero / Pi Zero 2 W is powered separately through its micro-USB PWR port.

To start the rover:

  1. Switch the rover battery pack ON.
  2. Check that the blue LED on the rover board is visible.
  3. Connect the micro-USB power cable or power bank to the Raspberry Pi Zero PWR port.
  4. Wait approximately 1–2 minutes for the Raspberry Pi to boot.
  5. Connect from a laptop using SSH:
ssh astrosheep@marsrover.local

If marsrover.local is not resolved, find the Raspberry Pi IP address from the router and connect with:

ssh astrosheep@<IP_ADDRESS>

Installing the Rover Software

The required Python packages and hardware interfaces must be installed and enabled before running the rover programs.

Install the LED and I2C dependencies:

sudo pip3 install rpi_ws281x --break-system-packages
sudo apt install python3-smbus -y

Download and run the original 4tronix installation script:

wget https://4tronix.co.uk/rover.sh -O rover.sh
bash rover.sh

The script creates the marsrover folder and downloads the original rover library and test programs.

After installation, go to the runtime folder:

cd ~/marsrover

The folder should contain the original 4tronix files, for example:

rover.py
pca9685.py
ledTest.py
motorTest.py
servoTest.py
sonarTest.py
keypad.py
driveRover.py
calibrateServos.py

Use this folder for low-level hardware tests and as a known working baseline.

Marsy-specific code should live in the main Marsy project structure, not inside the mirrored 4tronix baseline folder.

Installing 4tronix Baseline Code from the AstroSheep Mirror

Marsy keeps a mirrored copy of the original 4tronix M.A.R.S. Rover Python files inside the project repository.

The mirrored original files are stored here:

https://github.com/astro-sheep/marsy/tree/main/4tronix/original

The installer script is stored separately here:

https://github.com/astro-sheep/marsy/blob/main/4tronix/scripts/install_4tronix_mirror.sh

This script installs only the original 4tronix baseline files. It does not install the full Marsy project structure, simulator, web interface, or higher-level Marsy modules.

To install the mirrored 4tronix rover software on the Raspberry Pi, SSH into the rover and run:

wget https://raw.githubusercontent.com/astro-sheep/marsy/main/4tronix/scripts/install_4tronix_mirror.sh -O install_4tronix_mirror.sh
bash install_4tronix_mirror.sh

The script downloads the mirrored original files from:

https://raw.githubusercontent.com/astro-sheep/marsy/main/4tronix/original

and installs them into the standard 4tronix runtime folder on the Raspberry Pi:

~/marsrover

After installation, go to the runtime folder:

cd ~/marsrover

The folder should contain the original 4tronix files, for example:

rover.py
pca9685.py
ledTest.py
motorTest.py
servoTest.py
sonarTest.py
keypad.py
driveRover.py
calibrateServos.py

Use this folder for low-level hardware tests and as a known working baseline.

Marsy-specific code should live in the main Marsy project structure, not inside the mirrored 4tronix baseline folder.

Enabling I2C and SPI

The rover uses I2C for the PCA9685 servo controller and other board-level communication. I2C and SPI should be enabled through Raspberry Pi configuration:

sudo raspi-config

Then open:

Interface Options β†’ I2C β†’ Enable
Interface Options β†’ SPI β†’ Enable

After changing these settings, reboot:

sudo reboot

After the Raspberry Pi has restarted, reconnect through SSH and install smbus again if needed:

sudo apt install python3-smbus -y

Rover Software Folder

The original 4tronix files are installed in:

cd ~/marsrover

Typical files in this folder include:

rover.py
pca9685.py
ledTest.py
motorTest.py
servoTest.py
sonarTest.py
keypad.py
driveRover.py
calibrateServos.py

The main hardware abstraction module is rover.py. User programs should import this module rather than directly controlling GPIO pins.

Basic Program Structure

Every custom rover program follows the same basic structure:

import rover
import time

# Initialize the rover.
# Use rover.init(40) to enable LEDs with brightness 40.
# Use rover.init(0) to skip LED initialization and avoid sudo for simple motor/sensor tests.
rover.init(0)

try:
    # Your rover code goes here.
    pass

finally:
    rover.cleanup()

Always call rover.cleanup() at the end of the program. This stops the motors, clears LEDs if they were initialized, and releases GPIO resources.

Creating a New Program

Navigate to the rover folder:

cd ~/marsrover

Create a new file:

nano myprogram.py

Save in nano with:

Ctrl+O β†’ Enter β†’ Ctrl+X

Run the program:

python myprogram.py

If the program uses LEDs with brightness greater than zero, run it with sudo:

sudo python myprogram.py

Original Test Programs

The original 4tronix software includes several useful hardware tests. These should be run before developing custom autonomy or LLM-control logic.

LED Test

Flashes all FireLEDs through red, green, blue, and white:

sudo python ledTest.py

Use this test to check that the FireLEDs work and that the rpi_ws281x package is installed correctly.

Motor Test

Runs the wheel motors using keyboard control:

sudo python motorTest.py

Before running this test, lift or prop up the rover so the wheels can spin freely.

Controls:

Arrow Up / W     Forward
Arrow Down / Z   Reverse
Arrow Right / S  Spin right
Arrow Left / A   Spin left
Space            Coast stop
B                Brake
, or <           Decrease speed
. or >           Increase speed
Ctrl+C           Exit

Servo Test

Tests individual servo channels:

sudo python servoTest.py

Controls:

W / Arrow Up       Select next servo
Z / Arrow Down     Select previous servo
A / Arrow Left     Move servo left
S / Arrow Right    Move servo right
Space              Centre selected servo
X or .             Stop servo
Ctrl+C             Exit

The mast servo is channel 0.

Servo Calibration

Use the calibration program to straighten the steering servos and save offsets:

sudo python calibrateServos.py

Controls:

1    Front-left steering servo
2    Rear-left steering servo
3    Front-right steering servo
4    Rear-right steering servo
5    Mast servo
Left / Right arrows    Adjust offset
S                      Save calibration
Ctrl+C                 Exit without saving

Servo calibration is important before driving, because even small servo offsets can make the rover drift or steer asymmetrically.

Sonar Test

Reads the ultrasonic distance sensor on the mast:

sudo python sonarTest.py

The program prints the measured distance in centimetres once per second.

Keyboard Driving

Manual driving with steering servos:

sudo python driveRover.py

Controls:

Arrow Up / W     Drive forward
Arrow Down / Z   Drive reverse
Arrow Right / S  Steer right
Arrow Left / A   Steer left
Space            Coast stop
B                Brake and centre steering
, or <           Decrease speed
. or >           Increase speed
Ctrl+C           Exit

Camera Test

If the Raspberry Pi camera is installed and configured, capture a test photo with:

python -c "from picamera2 import Picamera2; import time; cam = Picamera2(); cam.start(); time.sleep(2); cam.capture_file('/home/astrosheep/photo.jpg'); cam.stop(); print('Done')"

To copy the photo from the rover to a laptop, run this command from the laptop terminal, not from the SSH session:

scp astrosheep@marsrover.local:/home/astrosheep/photo.jpg ./photo.jpg

On Windows PowerShell, the destination can be changed to a desktop path, for example:

scp astrosheep@marsrover.local:/home/astrosheep/photo.jpg C:\Users\username\Desktop\photo.jpg

Important Hardware Notes for our setup

The mast servo has a limited physical range. In our rover, the useful mast movement is approximately Β±45 degrees.

The mast servo direction is inverted compared with the intuitive left/right command direction:

negative angle = mast turns right
positive angle = mast turns left

This is not necessarily a physical assembly error. It is a convention mismatch between the servo orientation and the software coordinate system. Custom programs should account for this by wrapping mast movement in helper functions such as look_left() and look_right() instead of using raw servo angles everywhere.

Known steering servo channels:

Front-left steering servo   9
Rear-left steering servo    11
Front-right steering servo  15
Rear-right steering servo   13
Mast servo                  0

Shutdown Procedure

To turn off the rover safely:

  1. Shut down the Raspberry Pi:
sudo shutdown -h now
  1. Wait until the Raspberry Pi green LED activity stops.
  2. Unplug the Raspberry Pi USB power cable.
  3. Switch the rover battery pack OFF.

Do not simply disconnect the Raspberry Pi power without shutting down, because this may corrupt the SD card.

Troubleshooting

No I2C Detected or I/O Error

Most likely the rover battery switch is OFF. Turn the rover battery pack ON and check that the blue LED is visible.

Also check that I2C is enabled:

sudo raspi-config

ModuleNotFoundError: rpi_ws281x

Install the FireLED / NeoPixel package:

sudo python -m pip install rpi_ws281x --break-system-packages

ModuleNotFoundError: smbus

Install the I2C Python package:

sudo apt install python3-smbus -y

Cannot Resolve marsrover.local

Check the router for the Raspberry Pi IP address and connect directly:

ssh astrosheep@<IP_ADDRESS>

Servo Jitters

Servo jitter is often caused by weak batteries. Recharge or replace the AA batteries in the rover battery pack.

WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED

Remove the old SSH host key:

ssh-keygen -R marsrover.local

Then reconnect:

ssh astrosheep@marsrover.local

Clone this wiki locally