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 move in the opposite direction. However, if we are moving forward when one of the sensors is triggered, we move backward, and then turn. This allows the robot to gradually turn away from the object that is blocking its path and continue its movement in another direction. Similarly, if we are moving backwards and a sensor is triggered, we move forward, and then turn. By combining simple object avoidance with directional information, the robot can be commanded to navigate around as desired.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.67.70