Remote-Controlled Car Using Raspberry Pi

by riajain in Circuits > Raspberry Pi

20159 Views, 42 Favorites, 0 Comments

Remote-Controlled Car Using Raspberry Pi

IMG_7239.jpeg
IMG_7240.jpeg

In the golden age of artificial intelligence and technology, a new subfield is gaining traction: swarm robotics. Swarm robots have a great number of applications, like localized task completion in automation, autonomous navigation, or terrestrial mapping in areas that humans are unable to access. The basis of these robots is a radio-controlled (RC) car. Once multiple cars are built, swarm robotics algorithms can be implemented.

The brain of these cars is Raspberry Pi, a low entry point architecture for experimenting with hands-on robotics and programming. This tutorial walks through the steps to build such a car. It will be equipped with a camera module for remote driving and GPS and compass so that a car knows its own location and heading (this, once again, is extremely useful in swarm robotics algorithms -- especially for navigation). Then, with many of them, the cars can potentially communicate to execute swarm algorithms.

This project is a lot of fun to build! There are lots of little details (which can lead to a lot of detours), but isn't that the fun part? I really like this project because it encompasses a wide variety of concepts in both computer software (Python programming) and computer hardware through the Pi, and definitely the overlap of the two.

This project is also very versatile. You can use these cars for other applications beyond swarm robotics (although they are set up to work well for swarm projects too). It is definitely a great entry-level project that teaches you a lot! In addition, these skills are also useful for similar projects.

Supplies

For the car:

  • 1 Raspberry Pi board
  • I recommend using a Pi 4 with 16GB because that gives you the flexibility to use the OpenCV library for object recognition, which will give your car more autonomous capabilities in the future (and it will make it more versatile for whichever direction you choose to drive the project in later on)
  • You can use the Raspberry Pi 0w too
  • Make sure the board was a built-in wireless adapter, which is important for remote-control
  • GPIO pin header (40-pin)
  • 1 Raspberry Pi camera module
  • Link: https://www.amazon.com/Raspberry-Zero-Camera-1080...
  • 1 car chassis set (including wheels and motors)
  • Here's the one I used: https://www.amazon.com/gp/product/B07F759T89
  • 1 or 2 L298N Motor Drive Controller Board(s)
  • Each board controls 2 motors.
  • So, if you go with 1, you will drive 2 of the motors (which will allow the other two wheels to rotate as well). But, when the car turns, it will rotate in an arc about the inner back wheel, which can make future calculations more complicated.
  • If you go with 2, you will be able to drive all 4 motors, so the car will rotate about its center, so it just keeps things simple.
  • Link: https://smile.amazon.com/gp/product/B01M29YK5U/
  • 1 GPS module (with embedded compass)
  • Link: https://smile.amazon.com/gp/product/B078Y6323W/

General:

  • Soldering gun and wire
  • A lot of jumper wires (double male, male-female, double female)
  • Breadboard (not mandatory, but highly recommended for testing before soldering)
  • Mouse, keyboard, monitor (you will need at least once, then you can run the Pi headless if you want)
  • A laptop/computer
  • USB hub, power ports
  • A power source (it should be portable)
  • HDMI to mini HDMI adapter

A note: I recommend purchasing a Raspberry Pi starter kit, because that will usually include a heat sink, the GPIO pin header, the Raspberry Pi board, a microSD card, the adapters, many jumper wires, and a lot of the other small parts.

Install OS Images

Screen Shot 2021-03-14 at 1.33.29 PM.png

Materials: computer (Mac, Ubuntu 18.04, or Windows), microSD card, SD adapter

Basically, the Raspberry Pi runs the Raspbian OS, so we have to install the OS images on a microSD card. To do so, you need an SD adapter so a microSD card can be inserted into a regular laptop/computer's SD port (through which the images will be installed). Then, the microSD card is inserted into the Pi.

Follow the instructions here: https://www.raspberrypi.org/documentation/install...

Setting Up the Pi

Screen Shot 2021-03-14 at 1.56.28 PM.png
x.jpeg

We need to be able to work on the Raspberry Pi. Raspberry Pi is a single-board computer, so it can directly connect to a monitor. Be sure to connect the HDMI (using an adapter), USB (you’ll likely need a USB hub), and power ports. You’ll also need a keyboard and mouse (which you’ll connect using USB).

You can refer to the image to become familiar with the Raspberry Pi desktop. You'll see a manila folder icon, which is the equivalent of a Finder (Mac) or Windows Explorer. Essentially, it a visual of your files, downloads, directories, etc. For our purposes, it's how you'll manage your code. You'll also see a black screen icon, which is the terminal window. This is where you will write your code.

Open up a terminal window (there should be an icon in the upper left section of the screen).

From here, you have two options:

1. Continue using a monitor

  • Every time you want to work on the Pi, just do the same thing as above
  • Connect the peripherals and work directly on the pi

2. Run the pi headless (using ssh)

Create a repository for the project. Then, in the terminal window, make a new directory for the project on the pi. You can edit your files within the terminal window (vi) if more comfortable, or using a GUI if less comfortable.

If you are unfamiliar with the command line, refer to these links:

https://product.hubspot.com/blog/git-and-github-tu...

https://www.codecademy.com/learn/learn-the-command...

or do a Google search.

Prereading

Screen Shot 2021-03-14 at 2.00.01 PM.png

If you want to learn more about how all this stuff works (which I recommend), I would do some reading (there are plenty of resources and videos online!). But, here's a brief overview of what to know if you are just looking to get started.

  • The Raspberry Pi board - a single-board computer; know where these things are (see image above):
    • MicroSD card slot
    • Mini HDMI port
    • Micro USB port
    • Micropower port & LED that indicates that the Pi is being powered
    • Camera connector
    • Processor (and heat sink if you have one)
    • 40-pin header & GPIO pins
      • Know the SDA/SCL pins, MOSI/MISO pins, and TX/RX pins
  • Communication protocols
    • UART: universal asynchronous reception and transmission
    • I2C: inter-integrated-circuit
    • SPI: serial peripheral interface

Assembling the Chassis

Screen Shot 2021-03-14 at 2.04.45 PM.png

You should have a chassis. This is the base frame of the car. Assemble the chassis the way the kit specifies.

If you bought the one I linked:

To start, break off the eight rectangles along the dotted indents (be careful!).

Then, using the nuts and bolts included in the kit, and a screwdriver, assemble the pieces, motors, and wheels until they resemble the image above (which is of the bottom of the car, the top should be clear):

Note: be sure that the orientation of the motors is correct!

Now, take jumper wires and solder the wire

Once you have completed the assembly, the base of the car is assembled. Now, do a quick check to ensure that all of the motors are working properly by directly powering the motors. Check to see that the white rods turn. If you switch the black and red wires, the rod should turn in the opposite direction. Check all four wheels.

Motor Controllers

Screen Shot 2021-03-14 at 2.09.08 PM.png

Each one motor controller can control two wheels. Depending on whether you bought one or two drivers, there a difference in the way the vehicle turns: whereas controlling all four wheels enables the car to turn about its center, only controlling the rear wheels will cause the car to turn in an arch.

For now, we will focus on using one controller. If you bought two, repeat these steps for the second driver. Note: you can run them in parallel by making one driver power the other.

Here is the reading to understand how these modules work: https://components101.com/modules/l293n-motor-dri... I'd recommend reading the specifications.

Use the jumper wires provided to get the motor controllers working. Again, using a breadboard first might be more efficient. Here is where you have options: I recommend using a 12V power source for the driver(s) and a 5V power source for the pi. But, you can use the pi to power the motor driver(s), too. There is a jumper that differentiates between 12V and 5V -- you should read the specs of the module when doing the wiring.

Camera Module

Screen Shot 2021-03-14 at 2.12.05 PM.png

If you use a pi board with limited RAM, then it will only support the basic Pi Camera module. If you purchased a Pi 4 with more RAM (around 8 GB), you can try and implement the OpenCV camera. There are resources online to do so.

For now, though, you can work with the camera module, which will suffice.

Readings:

For the code, you can run the following command:

git clone https://github.com/miguelgrinberg/flask-video-str...

NOTE: Credit goes to Miguel Grinberg for the camera code.

GPS and Compass

Screen Shot 2021-03-14 at 2.14.39 PM.png

Use the following resources to get the GPS and compass working. The wiring is also in the diagram above.

Readings for GPS:

https://maker.pro/raspberry-pi/tutorial/how-to-use...

https://maker.pro/raspberry-pi/tutorial/how-to-use...

The specific GPS module that I linked also has an embedded compass.

Sample code to read compass data: https://github.com/ozzmaker/BerryIMU.git

Another example NMEA format (parse string for coordinate data): https://github.com/ozzmaker/BerryIMU.git

Again, credit for the GPS code goes to ozzmaker.

Displaying Data on the Web Interface Using Flask

Screen Shot 2021-03-14 at 2.24.40 PM.png

This allows you to stream the GPS and compass data, as well as video, to a web server.

We are going to use Flask, a web framework that helps you build a web application.


Information for implementation:

https://stackoverflow.com/questions/13386681/stre...

https://stackoverflow.com/questions/13386681/stre...

Demonstration and Next Steps

RC Car Basic Demo

Congratulations! You have now built a car that can be the basis of any number of projects.

There's a video of the full working prototype above. What I did here is I programmed the car to move in a box (I am starting to explore the swarm aspect of the project some more). You can see the car start to turn at a 90-degree angle, too. I had to edit out some parts because it did take a while to complete (~5 minutes). I anticipate that this is because the motors are not super precise, and until the car's heading is exactly in a range of degrees, it will keep turning before it drives forward. Regardless, the car is fun to drive around, especially with video!

Like I mentioned in the introduction, a lot of the capabilities of this car is useful for swarm robotics (especially when you build multiple cars). To expand in that direction, I'd recommend reading this review: https://www.hindawi.com/journals/isrn/2013/608164/... (mostly read the relevant parts introducing the concept and explaining the purpose).

These instructions were pretty high-level because a lot of the fun is the building, trial-and-error, and eventual success! Hopefully, you enjoyed the building process. To work the car, you can use the four arrow keys to get the car to move around. Open up a new tab and go to the IP address to see the GPS and compass data as well as the camera footage.

For any information or suggestions, feel free to reach out to me at riajain3878@gmail.com.