In this tutorial, we will learn how to build a Self-Driving RC Car using Raspberry Pi and Machine Learning using Google Colab. Ever since the thought and discussion and hype about self-driving cars came into existence, I always wanted to build one on my own. This tutorial is a very baby step towards that reality and will also provide you with some in-depth analysis and knowledge into the basics of self-driving cars. This tutorial will also cover how to train your model using Google Colab
Firstly, after a few research, I came across Donkey Car. Donkey is a high-level self-driving library written in Python. Also, it was developed with a focus on enabling fast experimentation and easy contribution. Hence, this was essential for me to quickly get started.
We have 2 options for building our Donkey Car.
There are 2 official stores to buy your Donkey Care Starter Kit.
If you want to buy the parts yourself, want to customize your donkey or live out to of the US, you may want to choose the bottoms up build. Furthermore, instructions regarding the same are mentioned in the documentation.
The starter kit includes everything you need to get started. It includes:
In addition, refer to the Donkey Car Assembly video below to follow detailed instructions to assemble all the parts and build your car.
The installation of the software and setup consists of the following 2 simple steps. In addition, we will be creating our Donkey car application.
When controlling your Donkey via behavioral cloning, you will need to setup a host pc to train your machine learning model from the data collected on the robot. Choose a setup that matches your computer OS. In my case, it was a Mac.
To begin, we need to flash the micro SD card with an operating system.
Open a text editor or notepad and copy paste the following code into it.
Replace <your network name> with the ID of your network. Leave the quotes. Replace <your password> with your password, leaving it surrounded by quotes. If it bothers you to leave your password unencrypted, you may change the contents later once you've gotten the pi to boot and log-in.
Save this file to the root of boot partition with the filename wpa_supplicant.conf. On first boot, this file will be moved to /etc/wpa_supplicant/wpa_supplicant.conf where it may be edited later. If you are using Notepad on Windows, make sure it doesn't have a .txt at the end.
If yours is the only Pi in the network, you can find it with the following command.
Put a file named ssh in the root of your boot partition.
Now you're SD card is ready. Eject it from your computer, put it in the Pi and plug in the Pi.
Plug in your raspberry pi device to a monitor using HDMI cable and also connect a keyboard to the raspberry pi.
Once the initial boot is done, you will be prompted to enter the login credentials.
Then try the command below. This would show you the IP address of the raspberry pi.
Once we know the IP address and since we enabled SSH, we can directly remotely connect to the raspberry pi using our mac or computer. Open the Terminal and type in one of the following command options to connect.
Type in the following commands in the raspberry pi via ssh to update and upgrade the pi.
Make sure to reboot after making the configuration changes.
You can check this page to find the one you like. Or install this one:
This will pull the latest from Tawn's fork:
Start your car and its time to take it for a spin. if you are planning on using your mobile phone to control your car, you can configure it under the config.py file.
change the following line at the bottom of the script.
Open your car's folder and start your car.
This script will start the drive loop in your car which includes a part that is a web server for you to control your car. You can now control your car from a web browser at the URL: <your car's IP's address>:8887
You can load the URL on a mobile browser and control your vehicle remotely or alternatively use the desktop browser to control the device. The car will start recording automatically when you use it to move forward and it will capture the necessary sequence of images and JSON files by creating a tub folder inside the Raspberry Pi's project data folder.
Now that we are able to drive our car successfully, we need to start training our model so that we can have a self-driving car. In the quest towards the same, I started training it on my Mac and it took hours and I completely gave up on it. Then as an alternate, used my Gaming Rig at home with pretty high specs and it took around 25-30 mins to train the model. This was no good if we wanted to quickly train more models, so in my research wanted to use the Cloud to get the job done quickly but you need to pay for it. Finally came across Google Colab, which requires no setup and runs in the cloud and is completely free.
Colaboratory is a free Jupyter notebook environment that requires no setup and runs entirely in the cloud.
With Colaboratory you can write and execute code, save and share your analyses, and access powerful computing resources, all for free from your browser.
Using Google Colab's accelerated hardware, I was able to train our model multiple times within a couple of minutes or less.
Follow the instructions as shown in the Github repository.
Copy the generated mypilot.h5 file to your Raspberry Pi under the following location. /home/pi/d2/models/mypilot.h5
Execute the following command in your Raspberry Pi.
To conclude, we have successfully built a self-driving car using Raspberry Pi and Machine Learning models using Tensorflow and Google Colab. We can train more models using the same method and train faster using Google Colab for free.