Build a safe robot behavior by separating sensing, decision-making, action, and stopping.
Sense, plan, and act
Prerequisites
- Robotics foundations
- Python conditions, loops, and functions
- Basic units, graphs, and debugging
Learning objectives
- separate sensing, planning, and action in a robot program;
- represent behavior with explicit states;
- handle missing or unsafe sensor readings; and
- test a robot in simulation before hardware.
The control loop
A simple robot repeatedly:
- Senses the environment.
- Plans what the reading means and selects a safe action.
- Acts through a motor, light, or message.
- Checks the result and repeats or stops.
Keep the emergency stop outside the normal decision path when possible. A safety limit must not depend only on a language model or an untested prediction.
A simulation-friendly state machine
def next_state(distance_cm, battery_percent):
if battery_percent < 15:
return "STOP_LOW_BATTERY"
if distance_cm is None:
return "STOP_SENSOR_FAILURE"
if distance_cm < 20:
return "STOP_OBSTACLE"
return "MOVE_FORWARD"
for reading in [45, 12, None]:
print(reading, next_state(reading, battery_percent=80))
Explicit states are easier to test than scattered motor commands. In hardware, each state should map to a bounded action, such as stopping motors rather than silently continuing.
Practice
Add a TURN state when an obstacle is near but the battery and sensor are healthy. Test:
- a clear path;
- an obstacle;
- a missing reading;
- low battery; and
- two failures at the same time.
Write down which condition wins and why.
Safety checklist
- Begin with simulation or an unpowered test.
- Use a physical or software emergency stop.
- Limit speed, force, and operating area.
- Keep fingers, hair, and loose clothing away from moving parts.
- Never test autonomous behavior near people or stairs without supervision.
- Disconnect power before changing wiring.