Wednesday, March 18, 2015

Raspberry Pi controlled RC car


This blog post will lead you through the process of building a remote controlled toy car which you will be able to operate from anywhere in the world without being restricted to being on the same wireless network as the RC car. The initial idea was to build a video spy toy car which I could operate from my phone and see where it's going but after researching the video streaming options I decided to postpone this feature at least until some improvements will be made in the WebRTC stack for the Raspberry Pi. So what this post will show you is how to control an RC car from any web browser.


What you will need for this project:

     Hardware   
  • Raspberry Pi (I used model B+)
  • RC toy car
  • 5V battery pack (I have a 10400mAh ADATA Power Bank)
  • Pololu DRV8835 2 channel motor driver
  • 3G or WiFi dongle (installation guides for both are available online)
     Software
  • Free account on pusher.com. This is a backend API service which allows sending events to Internet connected devices through WebSockets. For this application it’s the essential point in making the toy car work from anywhere in the world not just the local Wifi network. Pusher.com will be the middleware which will send the control messages triggered by the user interface to the Raspberry Pi which in turn will control the car. 
    • After you create your account, proceed to creating a new app. What you need are the App Credentials which are shown on the right side of that page (more precisely: app_id, key and secret)
  • Free account on Google Apps. By using Google's App Engine you will be able to host the web interface that will control the car from a <your_app_name>.appspot.com web address. Besides that it will also allow you to authenticate the communication channel that sends the messages to the RC car.

Step 1: Wiring the toy car

This is very specific to the toy car I've used but it will give you an idea of what to look for at your toy car. Below you can see my car's controller board being powered by the RX-2B chip which is specifically designed for remote controlled car applications and quite common. Therefore, the schematics for it are available on the web.










What you need to do is to identify the exact pins and connect wires to each of the 4 directions you want to control (the interest pins are 6, 7, 10 and 11).









Notice on the backside of the board the green, yellow, white and grey wires being connected to the direction pins on the RX-2B chip. Once you have them soldered in you will need to setup the Pololu motor driver. 
 I decided to use a motor driver instead of connecting these wires directly to the GPIOs because I was afraid I was going to fry the RPi. That is because from the GPIO pins you can draw a maximum current of 16mA. Through the driver motor board the maximum current is 1.2 Amps.





























The basic schematics of the Pololu DRV8835 motor driver board looks like this:























Notice the inputs on the left side (from the RPi’s GPIOs in our case) and the outputs on the right (the wires connected earlier that go to the RX-2B chip).

I used the Pololu DRV8835 Dual Motor Driver Kit for Raspberry Pi B+ which made installation a lot easier because it connects directly to the GPIO header. The board contained in the kit is laid out like this:
















You can see below the other ends of the direction wires being connected to the output pins on the motor driver. The red and black wires are VIN and GROUND which were also connected to the toy car’s board to power the motor driver’s output. Take note that the Pololu motor driver is by default in PWM mode which means that one GPIO pin dictates the speed and another direction. For my configuration, PWM was not something I wanted so I disabled it by connecting the MODE pin on the Pololu board to ground. 


     



Step 2: Running the software

There are three major software components involved in this project. First, there is the Python code that runs on the Raspberry Pi which converts the control messages the user sends in logical signals for the GPIO pins. Secondly, there is the JavaScript web interface hosted online which provides the user interface for controlling the car and lastly there is a Google App Engine app that authenticates the communication channel used for sending the control messages.

All the software bits can be found on the project's Github page which you will need to clone on your computer by running:

$ git clone https://github.com/24rush/raspeyecar.git

You can see that the repository contains two folders: pi and siteFirst, copy the pi folder on the Raspberry Pi then go to Pololu's Github page and follow the instructions for installing the motor driver dependency library.

Once you have the dependency library installed, go to the pi folder and run:


$ sudo python controller.py
This command will start the script that waits for messages from the user interface.

Next, use the site folder as your App Engine application. In order to deploy the app you need to install Google App Launcher on your computer and use the contents of the site folder as the code for your application. Using the Google App Engine Launcher go to File > Add Existing Application and select the site folder. Make sure that you change the application’s name in app.yaml under the ‘application’ section. After that, click Deploy. You should now be able to see the UI running at <your_application_name>.appspot.com

thumb


Finally, by dragging the black arrow you should have the RC car moving like in the video below: