November 2018
Beginner
472 pages
13h 5m
English
Our test code is simple. We will drive the robot until we meet the line and then stop. I put this in a file called stop_at_line.py:
from robot import Robotfrom time import sleepfrom gpiozero import LineSensorr = Robot()lsensor = LineSensor(23, pull_up=True)rsensor = LineSensor(16, pull_up=True)lsensor.when_line = r.stop_motorsrsensor.when_line = r.stop_motorsr.set_left(60)r.set_right(60)while True: sleep(0.02)
You should be able to upload this to the robot, place the robot a few centimeters away from hitting a black line, then run it with python stop_at_line.py. The robot should drive to the line and then stop.
Let's examine how this works. The first three lines are imports:
from robot import Robotfrom time import sleepfrom gpiozero ...