Avoiding walls – writing a script to avoid obstacles
Now that we have tested both sensors, we can integrate them with our robot class and make obstacle avoidance behavior for them. This behavior loop reads the sensors and then chooses behavior accordingly.
Adding the sensors to the robot class
So, before we can use the sensors in a behavior, we need to add them to the Robot
class, assigning the correct pin numbers for each side. This way, if pin numbers change or even the interface to a sensor changes, behaviors will not need to change:
- To use the
DistanceSensor
object, we need to import it fromgpiozero
; the new code is in bold:from Raspi_MotorHAT import Raspi_MotorHAT from gpiozero import DistanceSensor
- We create an instance of one of these
DistanceSensor
objects for each side in the robot class. We need to set these up in the constructor for our robot. We use the same pin numbers and queue length as in our test:class Robot: Â Â Â Â def __init__...