First attempt at obstacle avoidance

Here, we will need a loop that does the following:

  • Gets the distances from each sensor
  • If a sensor reads less than 20 cm (a reasonable threshold), then set the opposite motor in reverse to turn the robot away from the obstacle
  • Wait a small time and loop round again

We'll put this loop in a run method. There's a small bit of setup regarding this. We need to set the pan and tilt to 0 so that it won't obstruct the sensors. I've put this code in simple_avoid_behavior.py. Start with importing the robot, the exception, and the sleep for timing:

from robot import Robot, NoDistanceReadfrom time import sleep...

The following class will be the basis of our behavior:

...class ObstacleAvoidingBehavior(object): """Simple ...

Get Learn Robotics Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.