Page 115 - Programming the Raspberry Pi Getting Started with Python
P. 115
simpler old program. To do this, copy the file raspirobot_plus.desktop into the directory
/home/pi/.config/autostart and remove the raspirobot_basic.desktop file from that folder; otherwise,
both programs would start.
Note that because in this phase of the project, the Raspberry Pi has a screen and a keyboard (albeit a
very small one), it is possible to make the changes described here, but you’ll be using a tiny screen. If
this proves too difficult, then as before, disconnect the battery, detach the motors, and power the
Raspberry Pi from its USB power supply with its regular monitor, keyboard, and mouse.
Step 4: Run It
That’s it! The project is ready to run. As always, if the LEDs on the Raspberry Pi don’t come on
straight away, disconnect the batteries and look for the problem. The Raspberry Pi is pretty power
hungry for a battery-powered device. The screen also uses quite a lot of power. Therefore, to avoid too
much recharging of batteries, you should disconnect them when not in use.
Revised Software
The new program is bigger than the old one, so it is not listed here in full. You can open it up in IDLE
to take a look. The main differences are, as you would expect, the distance sensing and the display.
The function get_range is shown here:
This function is a very thin wrapper around a call to get_range_inch in the RaspiRobot module.
The exception handling is added because if the range finder does not work for any reason (say, it isn’t
plugged in), exceptions will be raised. This function just intercepts any such exceptions and returns a
distance of 0 if that happens.
The update_display function first gets the distance and then displays it along with a graphical
indication of the closeness of any obstacles, as shown in Figure 11-10.