Building your own DeepRacer

So this has taken a lot longer to post mainly due to the fact that every time I add in the command to restart the RPi I get locked out of my account for an hour or so as the software my host has on the server thinks I’m trying to do something bad….

Also this post will be updated over the next week or so to add in the rest of the images and also to add clarity around the printing – since I originally wrote this draft changes have been made to some of the print files (and I’ve changed job and lost a few of the original images)

Today we’re going to walk through building our own DeepRacer vehicle to accelerate our machine learning journey thanks to the DeepRacer community. Pro racer Lars “Duckworth” Ludvigsen has created an open source build of the DeepRacer vehicle (GitHub), which makes it easy to get started using readily available parts and helping solve the problem of AWS DeepRacer vehicle availability in some locales.

Overview of solution

The community version of DeepRacer takes the original vehicle code that AWS open sourced in 2021 and re-packages it to run on a Raspberry Pi along the way updating both the operating system and software libraries to more recent versions. The GitHub repository (https://github.com/larsll/deepracer-pi/) lists all of the steps required to get racing at a high level, including all of the software steps, hardware required and 3D print files for mounting on a donor 1/18th scale RC car chassis.

Walkthrough

The purpose of this walkthrough is to detail the steps needed to print, install and build your own DeepRacer vehicle using a Raspberry Pi as the main computer. 

This walkthrough comprises the following steps

  • 3D printing the computer chassis components
    • (Aside from waiting for the purchased parts to arrive, printing the parts you need takes the longest time)
    • (Aside from waiting for the purchased parts to arrive, printing the parts you need takes the longest time)
  • Raspberry Pi setup
    • Initial setup of Raspberry Pi – Flashing the SD card with Ubuntu and updating the operating system
    • Community DeepRacer Pi on GitHub: https://github.com/larsll/deepracer-pi/
      • DeepRacer vehicle software install – Installing the required packages prior to installing the DeepRacer vehicle software
  • Car build
    • Raspberry Pi build – Adding the (optional) cooling fan, GPIO riser, servo hat and camera
    • Chassis and Raspberry Pi mounting – Bringing it all together

Prerequisites

For this walkthrough, you should have the following prerequisites:

  • Required hardware (see complete list at the end)
  • Access to a 3D printer
  • Donor 1/18th scale RC car (WL Toys 9×9 model) or AWS DeepRacer vehicle
  • An AWS Account to train your models using the DeepRacer console or DRfC

3D Printing the computer chassis components

The most time-consuming part is printing the components that are needed for the chassis to mount the Raspberry Pi and camera to the car. There are 4 components to print, front camera mount, main plate, rear mount and body mounts (2). Printing on a Prusa Mini took 12 hours 19 minutes and used a total of 26.87m of PLA (in my case) filament. For printing I have infill set to 25% and printing supports everywhere with a brim to help with bed adhesion (probably overkill but worked for me).

PiMount v1.2 Front – 1 hours 20 minutes – 9.31m of filament

PiMount v1.3 Plate – 2 hours 29 minutes (with legs for DeepRacer) – 13.64m of filament

PiMount v1.3 Plate (No legs) – 1 hours 10 minutes (without legs for WL Toys) – 10.89m of filament

PiMount v1.2 Back – 59 minutes – 3.11 m of filament

A979 Body mount (if building for a WL Toys car) – 28minutes (2 needed – be careful removing the supports) – 1.22m of filament for each support

So whilst everything is printing we can move on to the next step and get the Raspberry Pi ready

Raspberry Pi setup

Initial setup of the Raspberry Pi – To prepare the SD card you can either download the Ubuntu 22.04 Server ISO (64 Bit) and write it to an SD card (recommended size is 32Gb or larger) using your preferred tool, or use Raspberry Pi Imager and just select “Other general-purpose OS” → “Ubuntu” → “Ubuntu Server 22.04.4 LTS (64Bit)” and write to your SD card

When prompted if “Would you like to apply OS customisation settings”?” select “Edit Settings” so you are able to set the default username and password as well as the WiFi settings for your local network.

I’ve set the hostname, username and password and added in my WiFi settings, click “Save” which brings you back to “Use OS customisation” prompt

Clicking “Yes” prompts you that you are about to erase all content on the selected SD card, clicking “Yes” again starts the process, depending on the operating system you are using you may get prompted for your password at this point to allow access to removable storage.

Writing the SD card will take a number of minutes depending on the card size and also if you allow the verification of the image.

Now we have our initial image ready we can put the SD card into our Raspberry Pi (RPi) and power the RPi on, it should boot up in ~30 seconds (it might take longer on the first boot as updates are installed) and show up on your network with the hostname as per the configuration (in my case “dr-rpi.local”).

Once on the network we’re going to remote login to the RPi using SSH and install the DeepRacer software from GitHub. Using terminal on your computer, SSH into the RPi (connecting computer and Pi should be on the same network) alternatively you can connect a monitor and keyboard to the RPi and complete the following steps locally.

To remote log into the RPi using SSH:

ssh deepracer@dr-rpi.local

The username and password will be as you specified when flashing the SD card. Once connected to the RPi (either over SSH or locally using monitor and keyboard) we’re going to start by ensuring the operating system is up to date, run the command to get the list of required updates:

sudo apt update

Then we install the updates using:

sudo apt upgrade -y

Note: this command just updates the packages to their latest versions and doesn’t change the version of the operating system installed on the RPi

It takes ~15 minutes for the operating system to be updated depending on the number of updates and the speed of your connection. During the update process you might get a kernel version mismatch warning as well as being prompted to restart running services (which is fine to do). The network services also might be restarted as part of this process depending on the updates being applied in which case you’ll need to reconnect via SSH (or just watch the services restart if connected locally using a monitor and keyboard) to reboot the Pi before continuing with the install. To reboot the RPi:

sudo /sbin/shutdown -r now

It should take ~30 seconds for the RPi to reboot and we can then connect back in using SSH or login locally using the attached monitor and keyboard, now we’re ready to install the DeepRacer perquisites and vehicle software.

Clone the repository containing the installing scripts from GitHub on to the Pi

git clone https://github.com/larsll/deepracer-pi

Change to the cloned directory and start installing the packages needed for DeepRacer to run

cd deepracer-pi
sudo ./install-prerequisites.sh

Depending on the speed of your internet connection this will take ~15 minutes to run and install all of the required libraries. During this step you may receive the message below when some of the firewall rules are being updated:

Command may disrupt existing ssh connections. Proceed with operation (y|n)?

Press Y and the install process will continue without interruption to your SSH connection as the required rules are already in place (if you are connected locally you will still be prompted), once this step has completed reboot the RPi.

sudo /sbin/shutdown -r now

Again it should take ~30 seconds for the RPi to reboot, log back into the RPi (either over SSH or locally) and now we can finally install DeepRacer, we change directory back into the cloned repository and run the command to install DeepRacer

cd deepracer-pi
sudo ./install-deepracer.sh

This stage takes the longest (aside from the 3D printing) taking up to ~30 minutes.

Note: If you are using a Raspberry Pi v2 Camera module you need to enable the legacy camera setting using “raspi-config”

sudo raspi-config

To enable the camera: “Interface Options” -> “Legacy Camera” -> “Enable”

This finishes the setup of the RPi, time to power down and once the 3D printing has completed we can build out our car.

Car build

First off let’s build the Raspberry Pi, I’ve got the cooling fan on mine and added a riser to the GPIO, securing the servo driver hat using the M2.5 stand-offs to keep everything together. The cooling fan connects to pin 4 (5V – red) and pin 6 (ground – black) of the GPIO for power and I’ve added a 2S JST connector to the power the servo hat and Raspberry Pi (having removed the middle cable from the JST connector first)

Our RPi is now ready to be mounted on to the 3D printed chassis

The 3D printed parts are bolted together using the M2 nylon stand off kit, 3x 15mm bolts to join the front and rear sections to the main plate and then mount the DeepRacer shell brackets. I’ve also put in 3x brass M2.5 screws into the chassis ready to add the RPi

Mounting the camera uses 4x 15mm M2 nylon screws tightening the bottom screws first to get the lower part of the camera as close to the mount as possible.

Connect up 200mm camera cable to the camera module and the Pi and we have our compute unit ready for adding to the car

IMPORTANT – Before connecting up everything going racing – You need to remove the middle red wire from the speed controller lead (or you risk damaging your servo hat and Raspberry Pi) – The connector can be carefully removed from the plug and then the wire either cut or tucked out of the way.

Chassis and Raspberry Pi mounting

With both the car chassis and the RPi attached to the 3D printed parts we’re ready for the final assembly.

DeepRacer build (left) and WL Toys chase build (right)

We can attach the computer chassis and wire everything up Attach the Raspberry Pi to the car chassis and connect the speed controller wire to 0 on the servo hat (black on the connector to black on the hat and white to yellow) and the steering controller wire to 1 on the servo hat (brown on the connector to black on the hat and yellow to yellow) as below

Plug in your battery using both connectors (one for the car, one for the RPi) wait 30 seconds for the Pi to boot up and you should be able to access the car console and get started with the car calibration – remember there is a switch just behind the left front wheel to turn the car on.

DeepRacer build complete

With everything built and running we’re going to SSH into the RPi again and start the DeepRacer process manually to check everything is working correctly before enabling the DeepRacer service on the car so that it starts automatically in future.

sudo /opt/aws/deepracer/start_ros.sh

Open a browser and enter the address of the car https://dr-rpi.local you will get a certificate warning as a self-signed cert is used, but you can accept this and then you should be presented with the DeepRacer car console log in page

The default password for the car console is deepracer and once you have logged in you should get the home page

Enable the camera if it isn’t already, it might be upside down (after all the camera is mounted upside down) so we’re going to edit (or create) /etc/rc.local using nano to rotate the camera image. Ctrl+C out of the running process to stop the DeepRacer service

sudo nano /etc/rc.local

Add the line to solve the issue with the camera image being upside down

v4l2-ctl --set-ctrl=rotate=180

Whilst we’re here lets quickly enable DeepRacer as a service on the car, reboot and we’re done.

command to restart an RPi which I can't post....

Once the Pi has rebooted open the car console in a browser window again and this time after logging in the camera image should be the right way up.

Now it’s time to start our machine learning journey to train some models and get racing…. see you on the track and good luck.

Conclusion

Through completing this we’ve learnt about installing and updating linux on a Raspberry Pi, with thanks to the community we‘ve installed DeepRacer and 3D printed all of the parts needed to build our own DeepRacer vehicle. We can now test our our DeepRacer models in the real world (or start training models) and see how they compare from the simulator. The physical conditions are very different with light being a major influence in how the car & model performs (whilst the image feed in the car console is in colour, the images used by the model are converted to greyscale to reduce compute resource requirements)

For additional help and support be sure to join the DeepRacer community and get involved.

Parts list

Complete list of the parts needed to complete the build

** coming soon **

Test results

AWS SSM Agent on an RPi 4

So as part of the work I do with AWS DeepRacer I use SSM Agent on the cars and (finally) now also on the Raspberry Pi based timing system, to make things easier I thought I’d “quickly” install and activate SSM on the RPi so I can access them remotely and show the timer online as part of DeepRacer Event Manager (DREM – more on which in another blog post)

Installing SSM on a 32bit OS on an RPi Zero or 4 was easy, just works, however on a 64bit OS I was getting errors:

dpkg: dependency problems prevent configuration of amazon-ssm-agent:armhf:
 amazon-ssm-agent:armhf depends on libc6.

dpkg: error processing package amazon-ssm-agent:armhf (--install):
 dependency problems - leaving unconfigured
Errors were encountered while processing:
 amazon-ssm-agent:armhf

Took me a while to find the answer as I didn’t happen to have the time to get an uninterrupted run at fixing the issue and more importantly testing the fix (because I was activating SSM as part of a scripted process) each attempt meant I needed to re-install the OS on the RPi to ensure it was working correctly.

Anyway, the solution was to install libc6:armhf so now my code to install SSM on an RPi 4 running 64bit OS is as follows:

sudo dpkg --add-architecture armhf
sudo apt-get update
sudo apt-get install -y libc6:armhf

mkdir /tmp/ssm
sudo curl https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/debian_arm/amazon-ssm-agent.deb -o /tmp/ssm/amazon-ssm-agent.deb
dpkg -i /tmp/ssm/amazon-ssm-agent.deb
rm -rf /tmp/ssm

And once installed activate SSM as normal.

Hopefully this helps someone, and if not it will probably help future me.

Goodbye to all that

So as we come to the end of 2019 I found myself back here, mainly because I’m in the process of winding up Wirewool Limited (my freelancing company) and with that comes the fun and games of unpicking all of the hosting / websites / domain name registration / email that I’d been running for a number of customers for up to 8 years.

One of the sites was this one, and that meant I was shutting down where it had been hosted so it needed moving.

The moving part was easy enough, (seriously how hard can it be to move a WordPress site). but then I had a look at when I had last posted, and what I had in the last couple of years posted about. There is a lot missing and a lot has changed. Some better, some just different, here’s a vague recap of what our hero has been up to….

So starting with the obvious, why shut down Wirewool?

Well since April 2018 I’ve been gainfully employed by Amazon Web Services as a Solution Architect, sure I could earn more as a freelancer, but only when working, and only if I have work. One of the things that I found with freelancing was that it was very much feast and famine. Also the idea of freedom of work doesn’t apply when you have bills to pay, kids to feed and a house to pay for. I managed to keep it going for 8 years and when a decent offer came along (in fact I had a couple of offers at the same) I took the one that felt like it would be the best for me long term. So far it’s worked out well and now it’s time to shut down Wirewool and remove something that is costing me money to run having finally gone through and tracked down all of the services I’m / Wirewool is using. (Note: something I should’ve done a long time ago, 20:20 hindsight is a wonderful thing.)

Ooh working for Amazon, gosh!

Yep, gone from a one man band to the largest company I’ve ever worked for (and will probably ever work for). It’s certainly interesting, but it’s also pretty awesome. I mean there is the basic stuff you get when you go back to working for someone, like paid holiday and sickness which is cool, and monthly pay (still freak out towards the end of each month as the account drains down) but also private healthcare (more of why this is good later). Training and education are massively important, I work with loads of awesome people to help customers, I’m enjoying presenting, helping to educate groups of people (ex-forces transitioning into a career in IT and also Princes Trust “kids”) on cloud, playing with DeepRacer and showing how it can help with learning AI / ML, helping to organise Meetups, and just well everything. I even enjoyed going to Vegas in December 2019 for re:Invent (and I hate Las Vegas). I even get to do some code hacking every now and then and have access to the best cloud toy box on the planet.

Introducing Brian

Everyone gets baggage that they carry around after a time, just I managed to name mine.

So I’d been feeling rubbish for a number of years, couldn’t shift the weight that I had gained over time, more than just “getting old”, couldn’t really train without taking days to recover (even after a slow 5km run / walk I’d need 3-4 days for my legs to start working again). I’d been to the doctor 6 years or so ago and said “this isn’t right” and they said what do you think it might be “well worst case, a brain tumor” said I and oh how we laughed….

….right until the point where I tried again with the doctors early last year. This time around I was taken more seriously and sent off for a load of blood tests which revealed I had 0 testosterone. Back to the doctors to discuss the result and the possible causes and it was time to test out the private health care I now had access to (see earlier note on private health care and the joys of working for a large company) More bloods revealed I had a prolactin level of 116290 (normal is below 500) and this was high enough that the day after I’d given the blood to be tested the consultant was called by the vampires who were slightly alarmed by the results. So something was wrong. An Ultrasound revealed nothing out of the ordinary with my testicles (one of the possible causes of 0 testosterone) however an MRI scan revealed I had a benign brain tumor that a) I christened “Brian” and b) that it was a macroprolactinoma approx 28mm in size.

No one wants to hear the phrase “brain tumor” when talking about their health but some reassurance from my consultant and some digging around showed it was easily sorted (well probably) and here take these drugs which have some “interesting” potential side effects (never read the side effects leaflets or google for “side effects of drug I am taking”)

So in the first three months of taking the drugs the prolactin levels were back down near normal, I was losing weight and able to train, aside from a few incidents with a black dog (which I haven’t named) I’d say it’s all pretty good, some members of the family may have a different view, and a couple of work colleagues thought I was suffering from a terrible hangover a few times as my body adjusted to the drugs. Anyhew since starting on the drugs I’ve lost 18 kilos in weight, taken 10 minutes or so off my half marathon PB and have managed to get my Parkrun PB to 23 minutes 13 seconds.

Over the years

So I get to this point every year, look back on my fitness activity and think “should’ve done better.” Except for 2019 I’m feeling pretty good about it, but to make sure that I’m not just kidding myself I needed some data and that meant I first needed to import 7 years of data from Garmin into Strava so all of my activity data was in one place. I actually have data going back to 2008 but it’s a bit sparse so for the sake of the table below I’ve dropped it.

Importing all the things from Garmin into Strava was made easy thanks to garminexport (well I had a local archive of 1300+ activities that I had to manually import, 25 at a time, and check were assigned to the correct activity type.)

Amusing to note that as I was adding the entries I spotted a 3km run in 2015 that took me 22 minutes. In 2019 I managed to get my Parkrun PB down to 22:13 ;-)

So with the data all in one place, time to look at it (a bit)…. using this handy site I was able to quickly see annual summary data which is below, the biggest surprise for me is the lack of swimming, oh and a lot of the riding is made up of bike commuting. The interesting part (for me) is the improvement in my running pace over the last few years.

2012 2013 2014 2015 2016 2017 2018 2019
Ride
Count 10 12 79 151 163 53 84 74
Distance
(km)
413 262 423 399 672 250 394 350
Time
(hh:mm:ss)
21:53:51 10:50:22 23:56:42 21:09:31 34:24:52 13:34:27 21:50:58 18:00:36
Run
Count 58 37 33 31 85 89 71 131
Distance
(km)
358 247 201 211 238 457 270 847
Time
(hh:mm:ss)
35:49:27 26:55:34 23:54:00 23:58:01 30:28:49 52:12:56 27:29:12 78:02:32
Avg / Speed
min/km
6:00 6:32 7:08 6:50 7:42 6:51 6:07 5:32
Swim
Count 5 1 23 44
Distance
(km)
8 1 14 55
Time
(hh:mm:ss)
02:34:34 00:46:34 03:57:34 15:40:27

This is due to my “if you want to run faster, run faster” brain wave I had in the middle of 2018, where I suddenly worked out what I needed to do to stop my parkrun taking so long (and getting longer) and to just generally improve my running, going from a tedious and sometime boring plod, into something that almost feels like running. Just need to apply it to my swimming now ;-)