Repository navigation
Obstacle Avoidance
This page documents the Marsy obstacle avoidance mission.
Run in simulator mode with:
MARSY_MODE=sim python -m missions.avoid_obstacleThis mission uses the Marsy backend layer, so the same mission can run either in the simulator or on the real rover. In simulator mode, movement commands go to the Marsy simulator. In real mode, they go to the Raspberry Pi rover hardware.
The obstacle avoidance mission is a simple reactive behavior.
Marsy repeatedly:
- looks forward with the mast-mounted ultrasonic sensor;
- drives forward while the path is clear;
- stops when an obstacle is detected;
- reverses briefly if the obstacle is dangerously close;
- scans left, center, and right with the mast;
- chooses the side with more free space;
- turns toward that side;
- continues the loop.
The mission is intentionally simple. It is meant to be a safe baseline behavior for testing sensors, steering, backend switching, and simulator integration.
Relevant files:
missions/avoid_obstacle.py # mission runner and command-line options
behaviors/avoid_obstacle.py # reusable obstacle avoidance behavior
marsy_core/motion.py # high-level movement and mast calibration
marsy_core/sensors.py # sensor access wrapper
marsy_backends/loader.py # selects simulator or real backend
marsy_backends/sim_backend.py # simulator backend
marsy_backends/real_backend.py # real rover backend
The mission runner creates:
rover backend
β
MarsyMotion
β
MarsySensors
β
AvoidObstacleBehavior
First start the Marsy simulator UI from the project root:
python simulator/marsy_sim_ui.pyKeep this terminal open.
Then open a second terminal from the same project root and run the mission:
MARSY_MODE=sim python -m missions.avoid_obstacleRun with default settings:
MARSY_MODE=sim python -m missions.avoid_obstacleThe mission runs until stopped with:
Ctrl+C
or until it is stopped from the simulator UI.
For a short test, use --run-seconds:
MARSY_MODE=sim python -m missions.avoid_obstacle --run-seconds 20This is useful when testing changes because the mission exits automatically after the selected duration.
The simulator backend can provide fake ultrasonic distance values. This is useful for testing behavior without placing real objects in front of the rover.
MARSY_MODE=sim MARSY_SIM_DISTANCE_CM=100 python -m missions.avoid_obstacle --run-seconds 15Expected behavior:
- Marsy sees the path as clear.
- Marsy drives forward.
- No avoidance turn should be triggered.
MARSY_MODE=sim MARSY_SIM_DISTANCE_CM=30 python -m missions.avoid_obstacle --run-seconds 15Expected behavior:
- Marsy detects an obstacle because
30 cmis below the default safe distance. - Marsy stops.
- Marsy scans left, center, and right.
- Marsy turns toward the selected side.
MARSY_MODE=sim MARSY_SIM_DISTANCE_CM=15 python -m missions.avoid_obstacle --run-seconds 15Expected behavior:
- Marsy detects a dangerously close obstacle.
- Marsy stops.
- Marsy reverses briefly.
- Marsy scans and turns.
MARSY_MODE=sim MARSY_SIM_DISTANCE_SEQUENCE=100,90,70,50,35,20,80,100 python -m missions.avoid_obstacle --run-seconds 25Expected behavior:
- Marsy starts by moving forward.
- As the simulated distance decreases, Marsy eventually detects an obstacle.
- When the distance becomes dangerous, Marsy reverses briefly.
- Marsy scans and turns.
- After the sequence ends, the last value is repeated.
The mission supports these options.
--run-seconds 20Run duration in seconds.
Default:
None
If not provided, the mission runs until Ctrl+C or until the simulator UI requests stop.
--safe-distance 45Distance below which Marsy starts avoiding an obstacle, in centimetres.
Default:
45 cm
If the forward distance is below this threshold, Marsy treats the path as blocked.
--danger-distance 25Distance below which Marsy reverses briefly before turning, in centimetres.
Default:
25 cm
This should be lower than --safe-distance.
--max-valid-distance 250Ultrasonic readings above this value are ignored.
Default:
250 cm
This helps filter unrealistic or unstable sensor values.
--forward-speed 35Forward speed from 0 to 100.
Default:
35
Keep this conservative on the real rover.
--turn-speed 30Speed used during avoidance turns.
Default:
30
--reverse-speed 25Speed used when reversing away from a dangerous obstacle.
Default:
25
--scan-angle 55Mast angle used when scanning left and right, in degrees.
Default:
55
--steer-angle 28Wheel steering angle used during avoidance turns, in degrees.
Default:
28
--reverse-time 0.55How long Marsy reverses when the obstacle is dangerously close, in seconds.
Default:
0.55 seconds
--turn-time 1.05How long Marsy performs one avoidance arc turn, in seconds.
Default:
1.05 seconds
--no-echo-blocksBy default, missing or zero ultrasonic readings are treated as clear path.
With --no-echo-blocks, missing or zero readings are treated as unsafe.
Use this option when you want the rover to stop if the sensor reading is missing.
--default-turn rightor:
--default-turn leftTie-break direction when left and right scan results look equal.
Default:
right
--quietReduces logging output.
Force reversed mast behavior:
--mast-reversedForce normal mast behavior:
--mast-normalUse only one of these options at a time.
The real rover may have the mast servo physically reversed. The simulator normally uses non-reversed mast logic.
MARSY_MODE=sim python -m missions.avoid_obstacle --run-seconds 20MARSY_MODE=sim MARSY_SIM_DISTANCE_CM=100 python -m missions.avoid_obstacle --run-seconds 15MARSY_MODE=sim MARSY_SIM_DISTANCE_CM=30 python -m missions.avoid_obstacle --run-seconds 15MARSY_MODE=sim MARSY_SIM_DISTANCE_CM=15 python -m missions.avoid_obstacle --run-seconds 15MARSY_MODE=sim MARSY_SIM_DISTANCE_SEQUENCE=100,80,60,40,20,80,100 python -m missions.avoid_obstacle --run-seconds 30MARSY_MODE=sim MARSY_SIM_DISTANCE_CM=30 python -m missions.avoid_obstacle --run-seconds 15 --default-turn leftWhile the mission is running, check that:
- the mode changes from manual to mission/autonomous state;
- the rover moves forward when distance is clear;
- the rover stops when the distance is below
safe_distance; - the rover reverses when the distance is below
danger_distance; - the mast scans left, center, and right;
- the wheels turn during avoidance;
- the mission timer stops when the mission is stopped;
- the top-level stop button stops the mission, not only the motors;
- emergency stop blocks further movement until reset.
In the terminal, stop with:
Ctrl+C
The mission should clean up:
- stop motors;
- straighten wheels;
- center mast;
- call rover cleanup.
If using the simulator UI, use the stop or emergency stop controls.
Expected stop behavior:
- motors stop;
- mission state changes to stopped;
- mission timer stops;
- Marsy is ready for manual mode or another mission.
First try:
Ctrl+C
If the simulator or mission is stuck, find the running processes:
ps aux | grep -E "marsy_sim_ui.py|roversimulator.py|missions" | grep -v grepStop the mission process:
pkill -f "python -m missions.avoid_obstacle"If needed, stop the simulator UI:
pkill -f marsy_sim_ui.pyUse force kill only if normal stop does not work:
pkill -9 -f "python -m missions.avoid_obstacle"
pkill -9 -f marsy_sim_ui.pyUse real mode only after simulator tests pass.
A conservative real-rover command is:
MARSY_MODE=real python -m missions.avoid_obstacle \
--forward-speed 25 \
--turn-speed 25 \
--reverse-speed 20 \
--safe-distance 55 \
--danger-distance 35Before running on the real rover:
- place the rover on the floor in a clear area;
- keep your hand near the power switch;
- use fresh or charged AA batteries;
- make sure the Raspberry Pi has stable power;
- test at low speed first;
- be ready to stop the mission immediately.
- Start the simulator UI.
- Run
avoid_obstaclewithMARSY_MODE=sim. - Test clear path with
MARSY_SIM_DISTANCE_CM=100. - Test obstacle detection with
MARSY_SIM_DISTANCE_CM=30. - Test danger behavior with
MARSY_SIM_DISTANCE_CM=15. - Test a distance sequence.
- Check stop and emergency stop behavior.
- Only then run on the real rover with conservative speeds.
This mission is backend-agnostic. The obstacle avoidance behavior talks to MarsyMotion and MarsySensors, not directly to the simulator or to the original 4tronix rover.py.
That keeps the behavior reusable:
MARSY_MODE=sim -> distance comes from simulator backend
MARSY_MODE=real -> distance comes from the real ultrasonic sensor
Mission-specific behavior can be tuned with command-line options instead of editing the source code.