Skip to content

Obstacle Avoidance

Margarita Matson edited this page Jul 8, 2026 · 1 revision

Mission: Obstacle Avoidance

This page documents the Marsy obstacle avoidance mission.

Run in simulator mode with:

MARSY_MODE=sim python -m missions.avoid_obstacle

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

What the Mission Does

The obstacle avoidance mission is a simple reactive behavior.

Marsy repeatedly:

  1. looks forward with the mast-mounted ultrasonic sensor;
  2. drives forward while the path is clear;
  3. stops when an obstacle is detected;
  4. reverses briefly if the obstacle is dangerously close;
  5. scans left, center, and right with the mast;
  6. chooses the side with more free space;
  7. turns toward that side;
  8. 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.

Project Files

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

Start the Simulator

First start the Marsy simulator UI from the project root:

python simulator/marsy_sim_ui.py

Keep this terminal open.

Then open a second terminal from the same project root and run the mission:

MARSY_MODE=sim python -m missions.avoid_obstacle

Basic Simulator Run

Run with default settings:

MARSY_MODE=sim python -m missions.avoid_obstacle

The mission runs until stopped with:

Ctrl+C

or until it is stopped from the simulator UI.

Limited-Time Test Run

For a short test, use --run-seconds:

MARSY_MODE=sim python -m missions.avoid_obstacle --run-seconds 20

This is useful when testing changes because the mission exits automatically after the selected duration.

Fake Distance Tests

The simulator backend can provide fake ultrasonic distance values. This is useful for testing behavior without placing real objects in front of the rover.

Constant Clear Path

MARSY_MODE=sim MARSY_SIM_DISTANCE_CM=100 python -m missions.avoid_obstacle --run-seconds 15

Expected behavior:

  • Marsy sees the path as clear.
  • Marsy drives forward.
  • No avoidance turn should be triggered.

Constant Obstacle

MARSY_MODE=sim MARSY_SIM_DISTANCE_CM=30 python -m missions.avoid_obstacle --run-seconds 15

Expected behavior:

  • Marsy detects an obstacle because 30 cm is below the default safe distance.
  • Marsy stops.
  • Marsy scans left, center, and right.
  • Marsy turns toward the selected side.

Dangerous Close Obstacle

MARSY_MODE=sim MARSY_SIM_DISTANCE_CM=15 python -m missions.avoid_obstacle --run-seconds 15

Expected behavior:

  • Marsy detects a dangerously close obstacle.
  • Marsy stops.
  • Marsy reverses briefly.
  • Marsy scans and turns.

Distance Sequence

MARSY_MODE=sim MARSY_SIM_DISTANCE_SEQUENCE=100,90,70,50,35,20,80,100 python -m missions.avoid_obstacle --run-seconds 25

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

Command-Line Options

The mission supports these options.

Runtime

--run-seconds 20

Run duration in seconds.

Default:

None

If not provided, the mission runs until Ctrl+C or until the simulator UI requests stop.

Safe Distance

--safe-distance 45

Distance 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

--danger-distance 25

Distance below which Marsy reverses briefly before turning, in centimetres.

Default:

25 cm

This should be lower than --safe-distance.

Maximum Valid Distance

--max-valid-distance 250

Ultrasonic readings above this value are ignored.

Default:

250 cm

This helps filter unrealistic or unstable sensor values.

Forward Speed

--forward-speed 35

Forward speed from 0 to 100.

Default:

35

Keep this conservative on the real rover.

Turn Speed

--turn-speed 30

Speed used during avoidance turns.

Default:

30

Reverse Speed

--reverse-speed 25

Speed used when reversing away from a dangerous obstacle.

Default:

25

Scan Angle

--scan-angle 55

Mast angle used when scanning left and right, in degrees.

Default:

55

Steering Angle

--steer-angle 28

Wheel steering angle used during avoidance turns, in degrees.

Default:

28

Reverse Time

--reverse-time 0.55

How long Marsy reverses when the obstacle is dangerously close, in seconds.

Default:

0.55 seconds

Turn Time

--turn-time 1.05

How long Marsy performs one avoidance arc turn, in seconds.

Default:

1.05 seconds

No-Echo Behavior

--no-echo-blocks

By 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 Direction

--default-turn right

or:

--default-turn left

Tie-break direction when left and right scan results look equal.

Default:

right

Quiet Mode

--quiet

Reduces logging output.

Mast Calibration Overrides

Force reversed mast behavior:

--mast-reversed

Force normal mast behavior:

--mast-normal

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

Example Simulator Commands

Basic 20-Second Test

MARSY_MODE=sim python -m missions.avoid_obstacle --run-seconds 20

Clear Path Test

MARSY_MODE=sim MARSY_SIM_DISTANCE_CM=100 python -m missions.avoid_obstacle --run-seconds 15

Obstacle Detection Test

MARSY_MODE=sim MARSY_SIM_DISTANCE_CM=30 python -m missions.avoid_obstacle --run-seconds 15

Dangerous Obstacle Test

MARSY_MODE=sim MARSY_SIM_DISTANCE_CM=15 python -m missions.avoid_obstacle --run-seconds 15

Sequence Test

MARSY_MODE=sim MARSY_SIM_DISTANCE_SEQUENCE=100,80,60,40,20,80,100 python -m missions.avoid_obstacle --run-seconds 30

Left-Turn Tie-Break Test

MARSY_MODE=sim MARSY_SIM_DISTANCE_CM=30 python -m missions.avoid_obstacle --run-seconds 15 --default-turn left

What to Check in the Simulator UI

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

Stopping the Mission

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.

If the Mission Does Not Stop

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 grep

Stop the mission process:

pkill -f "python -m missions.avoid_obstacle"

If needed, stop the simulator UI:

pkill -f marsy_sim_ui.py

Use force kill only if normal stop does not work:

pkill -9 -f "python -m missions.avoid_obstacle"
pkill -9 -f marsy_sim_ui.py

Conservative Real-Rover Test

Use 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 35

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

Recommended Development Workflow

  1. Start the simulator UI.
  2. Run avoid_obstacle with MARSY_MODE=sim.
  3. Test clear path with MARSY_SIM_DISTANCE_CM=100.
  4. Test obstacle detection with MARSY_SIM_DISTANCE_CM=30.
  5. Test danger behavior with MARSY_SIM_DISTANCE_CM=15.
  6. Test a distance sequence.
  7. Check stop and emergency stop behavior.
  8. Only then run on the real rover with conservative speeds.

Notes

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.

Clone this wiki locally