Skip to Content
Learn Robotics Programming
book

Learn Robotics Programming

by Danny Staple
November 2018
Beginner
472 pages
13h 5m
English
Packt Publishing
Content preview from Learn Robotics Programming

Test code

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learn Robotics Programming - Second Edition

Learn Robotics Programming - Second Edition

Danny Staple

Publisher Resources

ISBN: 9781789340747Supplemental Content