Obstacle Avoidance
This is a small program that allows Ozobot Evo to avoid obstacles in front of it, using the two front facing proximity sensors.
import asyncio
import pyozo
FORWARD_SPEED = 0.05
TURN_SPEED = 0.03
TURN_DURATION = 200
MOVE_DURATION = 200
PROXIMITY_THRESHOLD = 80
async def main() -> None:
async with pyozo.get_robot() as robot:
while True:
proximity = await robot.get_ir_proximity()
if proximity.left_front > PROXIMITY_THRESHOLD or proximity.right_front > PROXIMITY_THRESHOLD:
if proximity.left_front > PROXIMITY_THRESHOLD and proximity.right_front <= PROXIMITY_THRESHOLD:
print("Turning right to avoid left obstacle.")
await robot.move_wheels(TURN_SPEED, -TURN_SPEED, duration_ms=TURN_DURATION)
elif proximity.left_front <= PROXIMITY_THRESHOLD and proximity.right_front > PROXIMITY_THRESHOLD:
print("Turning left to avoid right obstacle.")
await robot.move_wheels(-TURN_SPEED, TURN_SPEED, duration_ms=TURN_DURATION)
else:
print("Turning right (default) to avoid forward obstacle.")
await robot.move_wheels(-TURN_SPEED, TURN_SPEED, duration_ms=TURN_DURATION)
else:
print("No obstacles. Moving forward.")
await robot.move_wheels(FORWARD_SPEED, FORWARD_SPEED, duration_ms=MOVE_DURATION)
asyncio.run(main())Algorithm:
- Read proximity sensor values.
- Consider there is an obstacle ahead if one of the two front facing sensors has a reading above 80.
- If the obstacle is on the left side, turn a bit to the right
- If the obstacle is on the right side, turn a bit to the left
- If the obstacle is directly in front, still turn to the right as an obstacle avoidance strategy
- Otherwise, move forward a bit.
Main ideas:
- reading proximity sensors
- controlling wheel speed to move forward and turn
- control loop