Raspberry Pi 3 Cookbook for Python Programmers - Third Edition
by Steven Lawrence Fernandes, Tim Cox
How it works...
We import rover_drive to control the robot (if we are using a Pi-Bug robot, bug_drive can be used) and wiringpi2 so that we can use the GPIO to read the sensors (defined as PINS). We define opCmds, which uses a Python dictionary to allocate new commands in response to the original command (using opCmds[char], where char is the original command).
We create a new class called sensor and set up each of the switches as GPIO input (each with an internal pull-ups set). Now, whenever we make a movement (as earlier, from the list of requested commands in the main() function), we check to see if any of the switches have been triggered (by calling mySensor.checkSensor()).
If a switch was tripped, we stop the current movement, and then ...
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.
Read now
Unlock full access