Repository navigation
Using the Rover Library Module
The main Python interface for the M.A.R.S. Rover is the rover.py library module. It provides high-level functions for controlling the rover hardware: motors, servos, FireLEDs, ultrasonic distance sensor, keypad, and cleanup.
Marsy keeps a mirrored copy of the original 4tronix rover.py file in the repository:
https://github.com/astro-sheeps/marsy/tree/main/4tronix
When writing new Marsy programs, use rover.py as the hardware access layer. Do not control GPIO pins directly unless there is a specific reason.
Every rover program starts by importing the library:
import roverMost programs also need time:
import rover
import timeBefore using motors, servos, sensors, or LEDs, initialize the rover:
rover.init(0)The argument controls FireLED brightness.
Use:
rover.init(0)when the program does not need the FireLEDs.
Use:
rover.init(40)when the program needs FireLEDs with brightness 40.
Programs that initialize LEDs with brightness greater than zero usually need to be run with sudo:
sudo python myprogram.pyFor simple motor, servo, or sensor programs without LEDs, rover.init(0) is usually enough.
Every program should end with:
rover.cleanup()This stops the motors, clears the LEDs if they were initialized, and releases GPIO resources.
A safe program structure is:
import rover
import time
rover.init(0)
try:
# Your rover code goes here
pass
finally:
rover.cleanup()Using try and finally is recommended because rover.cleanup() will still run if the program is interrupted or if an error happens.
The motor functions use speed values from 0 to 100.
rover.stop()Stops both motors and lets the rover coast to a stop.
rover.brake()Stops both motors more quickly.
rover.forward(speed)Example:
rover.forward(60)
time.sleep(1)
rover.stop()rover.reverse(speed)Example:
rover.reverse(50)
time.sleep(1)
rover.stop()rover.spinLeft(speed)This turns the rover on the spot by driving the left and right motors in opposite directions.
rover.spinRight(speed)This turns the rover on the spot in the opposite direction.
rover.turnForward(leftSpeed, rightSpeed)This moves forward in an arc by using different speeds for the left and right sides.
Example:
rover.turnForward(30, 70)
time.sleep(1)
rover.stop()rover.turnReverse(leftSpeed, rightSpeed)This moves backward in an arc.
import rover
import time
rover.init(0)
try:
rover.forward(60)
time.sleep(1)
rover.stop()
time.sleep(0.5)
rover.reverse(60)
time.sleep(1)
rover.stop()
finally:
rover.cleanup()Save this as:
movement_test.py
Run it from the rover folder:
cd ~/marsrover
python movement_test.pyThe rover uses servos for steering and for the mast.
Set a servo angle with:
rover.setServo(servo, degrees)The angle is given in degrees, usually from -90 to +90.
Example:
rover.setServo(0, 0)This centres servo channel 0.
Stop all servos with:
rover.stopServos()Known Marsy 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
The mast servo is connected to channel 0.
On our Marsy rover, the mast direction is inverted compared with intuitive left/right naming:
negative angle = mast turns right
positive angle = mast turns left
The useful physical range is approximately:
-45Β° to +45Β°
For custom programs, it is better to create helper functions instead of using raw mast angles everywhere:
MAST_SERVO = 0
def look_center():
rover.setServo(MAST_SERVO, 0)
def look_left():
rover.setServo(MAST_SERVO, 30)
def look_right():
rover.setServo(MAST_SERVO, -30)This makes the code easier to read and hides the inverted direction convention.
The mast has an ultrasonic distance sensor.
Read the distance in centimetres with:
distance = rover.getDistance()Example:
import rover
import time
rover.init(0)
try:
while True:
distance = rover.getDistance()
print("Distance:", distance)
time.sleep(1)
finally:
rover.cleanup()A returned value close to 0 can mean that no valid object was detected.
The rover has four FireLEDs.
To use the LEDs, initialize the rover with brightness greater than zero:
rover.init(40)Create colours with:
red = rover.fromRGB(255, 0, 0)
green = rover.fromRGB(0, 255, 0)
blue = rover.fromRGB(0, 0, 255)
white = rover.fromRGB(255, 255, 255)
black = rover.fromRGB(0, 0, 0)Set all LEDs to one colour:
rover.setColor(red)
rover.show()Set one LED:
rover.setPixel(0, blue)
rover.show()Clear the LEDs:
rover.clear()
rover.show()Example:
import rover
import time
rover.init(40)
try:
red = rover.fromRGB(255, 0, 0)
green = rover.fromRGB(0, 255, 0)
rover.setColor(red)
rover.show()
time.sleep(1)
rover.setColor(green)
rover.show()
time.sleep(1)
finally:
rover.cleanup()Run LED programs with:
sudo python myprogram.pyThe rover library includes keypad support.
Read the keypad state with:
key = rover.getKey()Example:
import rover
rover.init(0)
try:
while True:
key = rover.getKey()
print("Key:", key)
finally:
rover.cleanup()The original 4tronix library documentation mentions battery reading support through:
rover.getBattery()If using this in Marsy code, test it on the current rover hardware before relying on it for autonomous behaviour.
For safety, weak batteries should still be diagnosed physically: servo jitter, unstable movement, or repeated hardware errors often indicate that the AA batteries need charging or replacement.
Use this template for new Marsy programs:
import rover
import time
LED_BRIGHTNESS = 0
rover.init(LED_BRIGHTNESS)
try:
# Marsy behaviour goes here
rover.forward(50)
time.sleep(1)
rover.stop()
finally:
rover.cleanup()For programs that use LEDs:
LED_BRIGHTNESS = 40and run with:
sudo python myprogram.pyFor Marsy-specific behaviour, create wrapper functions with meaningful names.
Example:
import rover
import time
MAST_SERVO = 0
def look_center():
rover.setServo(MAST_SERVO, 0)
def look_left():
rover.setServo(MAST_SERVO, 30)
def look_right():
rover.setServo(MAST_SERVO, -30)
def drive_forward(seconds=1, speed=50):
rover.forward(speed)
time.sleep(seconds)
rover.stop()
rover.init(0)
try:
look_center()
drive_forward(seconds=1, speed=50)
finally:
rover.cleanup()This style is easier to use later with autonomous navigation or LLM-generated commands, because the high-level functions describe rover behaviour instead of exposing low-level servo angles and motor calls everywhere.