Raspberry Pi Robotic Projects by Richard Grimmett

Raspberry Pi Robotic Projects by Richard Grimmett

Author:Richard Grimmett
Language: eng
Format: epub, pdf
Publisher: Packt Publishing


Now you'll create some Python code that will allow you to access both the DC motors on your tracked platform. Here is some basic code that allows you to do this:

#!/usr/bin/python import time from rrb3 import * rr = RRB3(9, 6) rr.set_motors(1, 0, 1, 0) time.sleep(1) rr.set_motors(0, 0, 0, 0) rr.sw1_closed()

The lines of the code import the time and rrb3 libraries. The time library will allow you to delay your program, and the rrb3 library allows you access to the RaspiRobotBoard3. The rr = RRB2(9,6) line allows you to access the functionality on the RaspiRobotBoard3. rr.set_motors(1, 0, 1, 0) turns on both motors at full speed. time.sleep(1) pauses the program for 1 second. The rr.set_motors(0, 0, 0, 0) function stops the motors.

When you have created the code, save it under the filename johnny.py. Now you can run the program by typing python johnny.py. The tracks should move in the forward direction. This confirms that you have connected everything correctly. As in Chapter 2, Building Your Own Futuristic Robot, you may want to add dynamic control of the motors. Here is the Python code:

#!/usr/bin/python import time from rrb3 import * import termios import sys import tty def getch(): fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) tty.setraw(sys.stdin.fileno()) ch = sys.stdin.read(1) termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) print '\n char is '' + ch + ''\n' return ch rr = RRB3(9, 6) var = 'n' print "starting up" while var != 'q': var = getch() if var == 'f': rr.set_motors(1, 0, 1, 0) if var == 'b': rr.set_motors(1, 1, 1, 1) if var == 's': rr.set_motors(0, 0, 0, 0) if var == ',': rr.set_motors(1, 1, 1, 0) if var == '.': rr.set_motors(1, 0, 1, 1) rr.sw1_closed()

In this code, you'll have some additional import statements, termios, sys, and tty; these will allow you to sense key presses from the keyboard without hitting the Enter key. This will make the real-time interface seem more real-time. The getch() function senses the actual key press.

The second part of the code is a while loop that takes the input and translates it into commands for your Wall-E, moving it forward and backward and turning right and left. This program is quite simple, you'll almost certainly want to add more commands that provide more ways to control the speed and direction.

You may also want to call the Wall-E functions from another program. Instead of processing key presses, you'll want to call the program with command line arguments. Here is the Python code for that:

#!/usr/bin/python import time from rrb3 import * import sys rr = RRB3(9, 6) if (sys.argv[1]) == "f": rr.set_motors(1, 0, 1, 0) time.sleep(1) rr.set_motors(0, 0, 0, 0) if (sys.argv[1]) == "b": rr.set_motors(1, 1, 1, 1) time.sleep(1) rr.set_motors(0, 0, 0, 0) if (sys.argv[1]) == "l": rr.set_motors(1, 1, 1, 0) time.sleep(1) rr.set_motors(0, 0, 0, 0) if (sys.argv[1]) == "r": rr.set_motors(1, 0, 1, 1) time.sleep(1) rr.set_motors(0, 0, 0, 0) if (sys.argv[1]) == "s": rr.set_motors(0, 0, 0, 0) rr.sw1_closed()

Again, you may want to start with the basic johnny.py program and then add the additional capability.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.