The introduction of powerful, inexpensive processors that also provide a wide range of functionality through free open-source software has caused the do-it-yourself electronic project work to expand far beyond the simple, less than inspiring projects of the past. Now the developers can, with very low cost, create amazingly complex projects that were almost unthinkable a few years ago.
Many in this community are using Raspberry Pi as the basis for this revolution. This book provides simple, easy-to-follow instructions on how to use the Raspberry Pi in some very complex and sophisticated projects. Now enough of the introduction, let's start building something.
In this chapter, you'll learn the following:
How to modify an Xmods RC car using Raspberry Pi
How to set break into the control circuitry of the car and use Raspberry Pi to control it
How to use wireless communication to add remote control to the car
A brief note before you start. In this book, you'll be using Raspberry Pi B2, a microprocessor that can run on the Linux operating system. The following is an image of the unit, with the different interconnectors labeled:
As this is an advanced projects book, you have already spent some time with Raspberry Pi and know how to write Raspbian/Wheezy on an SD card and boot your Raspberry Pi. If you don't, feel free to go to the Raspberry Pi website at https://www.raspberrypi.org/. Here you'll find all the instructions that you need to get your Raspberry Pi B 2 up and running.
Note that you may want to install your system on a microSD card that has at least 8 GB of memory. In some of the projects that you'll be building, you'll be installing some fairly significant pieces of open source software and you may not want to run out of memory.
Now you are ready to start with some simple product modification. Let's start with an RC car; you'll replace the transmitter and control the car with a wireless connection on Raspberry Pi.
This particular car is an Xmods car, sold by Radio Shack, also available at other retail and online outlets. You can certainly use other RC cars as well. The advantage of this particular set is that the inputs to the drivetrain and steering are very easy to access.
The following is the car, exposing the center control mechanism:
There are two connections that you will want direct access to. The first is the drive motor, and the second is the steering mechanism. For this particular model of RC car, the drive mechanism is in the rear. What you are normally looking for is two wires that will directly drive the DC motor of the car. On this system, there is a connector in the rear of the car, it looks as shown in the following image:
Remove this plug and these wires; you'll use Raspberry Pi and a motor controller to provide the voltage to the drive system of the car. The motor will run faster or slower based on the level of voltage that is applied to these wires and the polarity of the voltage will determine the direction. Raspberry Pi will need to provide a positive or negative 6 volt signal to control the speed and direction of the car.
You'll also need to replace the control signals that go to the front of the car for the steering. This is a bit more difficult. The following is the connector that goes to the front of the car:
As you can see in the previous image, the blue and yellow wires are attached to a DC motor and the orange, brown, and red wires are attached to another control circuit. The motor will drive the wheels left or right, the polarity of the voltage will determine the direction, and its magnitude will cause the wheels to turn more or less sharply. The orange, brown, and red wires are interesting as their purpose is a bit difficult to discover. To do this, you can hook up a voltmeter and an oscilloscope. The orange and brown wires are straightforward, they are 3.5 volt and GND, respectively. The red wire is a control wire, the signal is a Pulse Width Modulation (PWM) signal, a square wave at 330 Hz and 10 percent duty cycle, and it is an enable control signal. Without the signal, the turning mechanism is not engaged.
Now that you understand the signals that are used in the original system to control the car, you can replicate those with Raspberry Pi. To control the steering, Raspberry Pi needs to provide a 3.3 volt DC signal, a GND signal, a 330 Hz, a 3.3 volt PWM signal, and the +/- 6 volt drive signal to the turning mechanism. To make these available, you can use the existing cables, solder some additional cable length, and use some shrink-wrap tubing to create a new connector with the connector that is available in the car:
To control the car, you'll need to provide each of the control signals. The +/- 6 volt signals cannot be sourced directly by Raspberry Pi. You'll need some sort of motor controller to source the signal to control the rear wheel drive of the car and turning mechanism of the car. The simplest way to provide these signals is to use a motor shield, an additional piece of hardware that installs at the top of Raspberry Pi and can source the voltage and current to power both of these mechanisms. The RaspiRobot Board V2 is available online and can provide these signals. Here is a picture:
The specifics on the board can be found at http://www.monkmakes.com/?page_id=698. The board will provide two key signals to your RC car, the drive signal and the turn signal. You'll need one more additional signal, the PWM signal that enables the steering control. The following are the steps to connect Raspberry Pi to the board:
First, connect the battery power connector to the power connector on the board, as shown in the following:
You'll use Pin 1 3.3V for the 3.3 volt signal and Pin 9 GND for the ground signal. You'll connect one of the GPIO pins so that you can create the 320 Hz, 10 percent duty cycle signal to enable the steering. Connect Pin 12 GPIO18, as shown in the following:
The hardware is ready, now you can access all this functionality from Raspberry Pi. First, install the library associated with the control board, found at http://www.monkmakes.com/?page_id=698. Perform the following steps:
Run the command
wget https://github.com/simonmonk/raspirobotboard2/raw/master/python/dist/rrb2-1.1.tar.gz—this will retrieve the library.
tar -xzf rrb2-1.1.tar.gz—this will unarchive the files.
cd rrb2-1.1—this will change the directory to the location of the files.
sudo python setup.py install—this will install the files.
Now you'll create some Python code that will allow you to access both the drive motor and the steering motor. The code will look similar to the following:
import RPi.GPIO as GPIO: This will import the
RPi.GPIOlibrary, allowing you to send out a PWM signal to the front steering mechanism.
import time: This will import the
timelibrary, allowing you to use the
time.sleep(number_of_milliseconds), which causes a fixed delay.
from rrb2 import *: This will import the
rrb2library, allowing you to control the two DC motors. The
rrb2is the library you just downloaded from GitHub.
pwmPin = 18: This will set the PWM pin to GPIO Pin 18, which is physically Pin 12 on the Raspberry Pi.
dc = 10: This will set the duty cycle to 10 percent on the PWM signal.
GPIO.setmode(GPIO.BCM): This will set the definition mode in the
RPi.GPIOlibrary to the BCM mode, allowing you to specify the physical pin of the PWM signal.
GPIO.setup(pwmPin, GPIO.OUT): This will set the PWM pin to an output so that you can drive the control circuitry on the steering.
pwm = GPIO.PWM(pwmPin, 320): This will initialize the PWM signal on the proper pin and set the PWM signal to 320 Hz.
rr = RRB2(): This will instantiate an instance of the motor controller.
pwm.start(dc): This will start the PWM signal.
rr.set_led1(1): This will light LED 1 on the motor controller board.
rr.set_motors(1, 1, 1, 1): This will set both the motors to move so that the vehicle goes in the forward direction. This command will allow you to set the motors to forward or reverse and set it at a specific speed. The first number is the speed of motor one and it goes from 0 to 1. The second numbers is the direction of motor one,
1is forward and
0is reverse. The third number is the speed of motor two, which also goes from
1, and the fourth number is the reverse and forward setting of the second motor, either
print("Loop, press CTRL C to exit"): This will instruct the user how to stop the program.
while 1: This will keep looping until Ctrl + C is pressed.
time.sleep(0.075): Causes the program to wait 0.075 seconds.
pwm.stop(): This will stop the PWM signal.
GPIO.cleanup(): This will cleanup the GPIO driver and prepare for shutdown.
Now you can run the program by typing
sudo python xmod.py. LED 1 on the control board should turn on, the rear wheels should move in the forward direction, and the steering should turn. This confirms that you have connected everything correctly. To make this a bit more interesting, you can add more dynamic control of the motors by adding some control code. The following is the first part of the python code:
Before you start, you may want to copy your python code in a new file, you can call it
xmodControl.py. In this code you'll have some additional import statements, which 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 rest of this code will look similar to the previous program. Now the second part of this code is as follows:
The second part of the code is a
while loop that takes the input and translates it into commands for your RC car, going 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 can now control your RC Car, but you certainly want to do this without any connected cables. This section will show you how to add a wireless LAN device so that you can control your car remotely. The first step in doing this is to install a Wireless LAN device. There are several possible ways to do this; however, the one that works well, with full documentation, is described at https://learn.adafruit.com/setting-up-a-raspberry-pi-as-a-wifi-access-point/overview.
You should now be able to connect to your Raspberry Pi via the Wireless Access Point. Once you've created the wireless access point, you can login via a VNC connection, this way you can add a USB webcam to your car to make it even easier to control. To do this, first download an application that can support a VNC connection. You can get this on your Raspberry Pi using an application called vncserver. You'll need to install a version of this on your Raspberry Pi by typing
sudo apt-get install tightvncserver in a terminal window on your Raspberry Pi.
TightVNC Server is an application that will allow you to remotely view your complete graphical desktop. Once you have it installed, you can do the following:
You need to start the server by typing
vncserverin a terminal window on Raspberry Pi.
You will be prompted for a password and then asked to verify it, then you will be asked if you'd like to have a view-only password. Remember the password that you have entered, you'll need it to remotely login via a VNC Viewer.
You'll need a VNC Viewer application for your remote computer. One choice for Windows users is RealVNC, available at http://www.realvnc.com/download/viewer/. When you run it, you will see the following:
Enter the VNC Server address, which is the IP address of your Raspberry Pi, and click on Connect. You will get a warning about an unencrypted connection, select Continue and you will get the following pop-up window:
Type in the password that you entered while starting the vncserver, and you will then get a graphical view of your Raspberry Pi, which looks like the following screenshot:
You can now access all the capabilities of your system; however, they may be slower if you are doing a graphics-intense data transfer. To avoid having to type
vncserver each time you boot your Raspberry Pi, use the instructions given at http://www.havetheknowhow.com/Configure-the-server/Run-VNC-on-boot.html.
Vncserver is also available via Linux. You can use an application called Remote Desktop Viewer to view the remote Raspberry Pi GUI system. If you have not installed this application, install it using the update software application based on the type of Linux system you have. Once you have the software, perform the following steps:
Run the application and you will get the following result:
Make sure vncserver is running on Raspberry Pi; the easiest way to do this is to log in using SSH and run
vncserverat the prompt. Now click on Connect on Remote Desktop Viewer. Fill in the screen as follows, under the Protocol selection, choose VNC, and you will see the following screenshot:
Now enter the host IP address, make sure you include a
:1at the end and then click on Connect. You'll need to enter the vncserver password that you created when you first ran vncserver on Raspberry Pi, like this:
You can see the graphical screen of Raspberry Pi. Now you are ready to observe the output of a USB webcam connected to your car. This is quite straightforward, simply plug in a USB webcam and download a video viewer. One such video viewer that works well is luvcview. To install this, type
sudo apt-get install luvcview.
With all these tools installed, you can now run vncview, bring up a luvcview window so you can see what your RC Car is seeing and control it remotely by running the
xcmodControl.py program that you wrote earlier. The screen will look similar to the following: