Repository navigation
Rover Software Setup
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:
The project keeps the original vendor code separate from our own experiments so that we can always return to a known working baseline.
The rover has two power systems:
- The rover battery pack powers the motors, servos, sensors, and LEDs.
- The Raspberry Pi Zero / Pi Zero 2 W is powered separately through its micro-USB PWR port.
To start the rover:
- Switch the rover battery pack ON.
- Check that the blue LED on the rover board is visible.
- Connect the micro-USB power cable or power bank to the Raspberry Pi Zero PWR port.
- Wait approximately 1β2 minutes for the Raspberry Pi to boot.
- Connect from a laptop using SSH:
ssh astrosheep@marsrover.localIf marsrover.local is not resolved, find the Raspberry Pi IP address from the router and connect with:
ssh astrosheep@<IP_ADDRESS>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 -yDownload and run the original 4tronix installation script:
wget https://4tronix.co.uk/rover.sh -O rover.sh
bash rover.shThe script creates the marsrover folder and downloads the original rover library and test programs.
After installation, go to the runtime folder:
cd ~/marsroverThe 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.
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.shThe 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:
~/marsroverAfter installation, go to the runtime folder:
cd ~/marsroverThe 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.
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-configThen open:
Interface Options β I2C β Enable
Interface Options β SPI β Enable
After changing these settings, reboot:
sudo rebootAfter the Raspberry Pi has restarted, reconnect through SSH and install smbus again if needed:
sudo apt install python3-smbus -yThe original 4tronix files are installed in:
cd ~/marsroverTypical 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.
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.
Navigate to the rover folder:
cd ~/marsroverCreate a new file:
nano myprogram.pySave in nano with:
Ctrl+O β Enter β Ctrl+X
Run the program:
python myprogram.pyIf the program uses LEDs with brightness greater than zero, run it with sudo:
sudo python myprogram.pyThe original 4tronix software includes several useful hardware tests. These should be run before developing custom autonomy or LLM-control logic.
Flashes all FireLEDs through red, green, blue, and white:
sudo python ledTest.pyUse this test to check that the FireLEDs work and that the rpi_ws281x package is installed correctly.
Runs the wheel motors using keyboard control:
sudo python motorTest.pyBefore 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
Tests individual servo channels:
sudo python servoTest.pyControls:
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.
Use the calibration program to straighten the steering servos and save offsets:
sudo python calibrateServos.pyControls:
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.
Reads the ultrasonic distance sensor on the mast:
sudo python sonarTest.pyThe program prints the measured distance in centimetres once per second.
Manual driving with steering servos:
sudo python driveRover.pyControls:
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
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.jpgOn 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.jpgThe 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
To turn off the rover safely:
- Shut down the Raspberry Pi:
sudo shutdown -h now- Wait until the Raspberry Pi green LED activity stops.
- Unplug the Raspberry Pi USB power cable.
- Switch the rover battery pack OFF.
Do not simply disconnect the Raspberry Pi power without shutting down, because this may corrupt the SD card.
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-configInstall the FireLED / NeoPixel package:
sudo python -m pip install rpi_ws281x --break-system-packagesInstall the I2C Python package:
sudo apt install python3-smbus -yCheck the router for the Raspberry Pi IP address and connect directly:
ssh astrosheep@<IP_ADDRESS>Servo jitter is often caused by weak batteries. Recharge or replace the AA batteries in the rover battery pack.
Remove the old SSH host key:
ssh-keygen -R marsrover.localThen reconnect:
ssh astrosheep@marsrover.local