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
Raspberry Pi Robotic Projects by Richard Grimmett.pdf
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.
Linux Device Driver Development Cookbook by Rodolfo Giometti(3932)
Embedded Programming with Modern C++ Cookbook by Igor Viarheichyk(3603)
Implementing Cellular IoT Solutions for Digital Transformation by Dennis McCain(3540)
Embedded Linux Development Using Yocto Project - Third Edition by Otavio Salvador & Daiane Angolini(3369)
TinyML Cookbook by Gian Marco Iodice(3308)
Simplifying 3D Printing with OpenSCAD by Colin Dow(2839)
TinyML Cookbook by Gian Marco Iodice & Ronan Naughton(2602)
Fusion 360 for Makers by Lydia Sloan Cline(2219)
Networking A Beginner's Guide by Bruce Hallberg(2209)
Hands-On Linux for Architects by Denis Salamanca(2051)
But How Do It Know? by J. Clark Scott(2030)
Computers For Seniors For Dummies by Nancy C. Muir(1995)
Raspberry Pi and MQTT Essentials by Dhairya Parikh(1951)
Arduino Project Handbook, Volume 2: 25 Simple Electronics Projects for Beginners by Geddes Mark(1947)
9781803246888-ENHANCING DEEP LEARNING WITH BAYESIAN INFERENCE by Unknown(1899)
Hack and HHVM by Owen Yamauchi(1884)
31 Days Before Your CompTIA A+ Exams (Shanette Luellen's Library) by Benjamin Patrick Conry(1860)
MicroPython Projects by Jacob Beningo(1732)
Hands-On Internet of Things with MQTT by Tim Pulver(1706)
