Fully autonomous cars are the upcoming holy grail of mobility, with giants from the IT and automotive industries alike racing to deliver a fully functional device. But why wait? Follow Adam Słucki’s instructions and build one for less than $500!
There are numerous exciting applications for computer vision and machine learning and autonomous driving is definitely near the top of my list. Building a real car is too problematic due to the cost and limitations on where you can drive it, but there are other options. You can use a simulated environment like CARLA or Gazebo or you can even build a programmable toy car. But most likely you’ll want to do both… Well, at least I did.
In this series, I’ll present the process of building an AI toy car. At the time of writing this, the project is not yet finished and the series is not meant to be a complete tutorial but rather a kind of a guide. I’ll share all the insights I’ve gained but don’t come here expecting instructions telling you to connect a green wire to pin 12, copy and paste this snippet, execute this script and watch the wheels spin. You should expect this project to require a lot of tinkering and self-discovery.
You can follow the progress of the project on our GitHub. You can find there the self-driving car python code and more.
How to build an autonomous car
The main idea is to build a realistic DIY autonomous RC car that can be controlled programmatically. How realistic should it be? To me, the Ackerman steering, suspension, and differentials are a must.
You can buy premade robots but they might be unrealistic and difficult to upgrade or modify. Building the thing yourself can be time-consuming and frustrating but definitely is more satisfying and gives you more freedom.
The goal is to build a realistic model of an autonomous vehicle that can be used for the development and testing of ML and CV solutions for autonomous driving.
Components of the RC autonomous car
Below is the list of necessary parts. Most of them can be bought, if not locally then you can always try ordering them in from China. There are some parts that I designed and 3D printed or fabricated using raw materials like aluminum bars. I’ll elaborate on a few selected components later.
I tried building my own chassis but the premade one has a lot of benefits:
- You save time that you can use on implementing algorithms.
- It’s durable and looks realistic.
- It’s quite big.
- There are spare parts and upgrades available on the market.
- You can buy a cool body and paint it so your model looks like a real car.
I chose the basic and probably the most popular version – Tamiya TT02. Note that the basic chassis has some obvious flaws and upgrading it is very addictive and may become costly ;)
2. Jetson Nano
You can use the latest Raspberry (4) or another Jetson as well. Keep in mind that Raspberry and Jetson are not completely interchangeable. The Raspberry is better supported and on Jetson you may encounter problems that don’t exist on Raspberry. For example, PWM pins don’t work out of the box on Jetson, while it’s not trivial to access Raspberry’s camera from OpenCV, etc.
Jetson Nano is quite limited in it’s capabilities. It’s easily switchable with more powerful Jetson boards and it can be done later but if you’re planning on using 3D mapping with RealSense camera and such you should consider Jetson Xavier as Nano won’t handle it with reasonable speed or at all.
The TT02 chassis set comes with a brushed motor. I decided to try a brushless one for two reasons:
- It’s easier to find a brushless motor with an encoder than for a brushed one. And I wanted the encoder to measure RPM.
- Out of curiosity.
Choosing a motor is not an easy task, especially when you’re new to the field. I bought a 7800 Kv one which turned out to be inconveniently fast (to my defense, it was the only one available in the store). I’ve already ordered a 2300Kv one from China. I also plan to replace the pinion gear with one that has fewer teeth.
A very nice thing about the motor I chose is the double sensor port. Notice that it’s quite a small port. You should buy a spare sensor cable for easy access to those pins (recommended for the Hall sensor output and possibly the temperature sensor).
It’s necessary for steering. It seems that there are commercially specific terms referring to torque. So, as strange as it sounds, you’re looking for a 9 or more kilogram servo. It’s worth buying a quality servo for the ease and precision of steering.
Together with the servo, you should also buy a so-called servo saver. It’ll literally save your servo in the case of some accidents. After all, your robot will weigh about 3kg and be able to move quite fast. Bumping the front wheels into something can easily damage the bearings of the servo when there is no servo saver.
5. Electronic Speed Controller (ESC)
I use a black box 120A ESC. I bought the high Kv motor first, so I didn’t have too many options here. With the lower Kv motor I recently ordered I can try a cheap version of a premade VESC, which is an open-source controller. It supports Field Oriented Control (although many people state this is risky with those cheap controllers) so I hope it’ll let me operate the motor with greater precision.
Update: it turns out PWM signal from Jetson board can’t be precisely controlled. Although it’s still usable I recommend buying a separate 12-bit PWM board.
Note that many controllers also provide BEC (battery eliminator circuit). This is something you should look for as it allows for the use of a single battery to power all the motors, the Jetson, and sensors. Pay attention to the max current that the BEC provides. Most of them are rated up to 2A which is too low to meet the Jetson’s needs (3A). As for me, I use a separate battery for the Jetson.
Besides the hard requirements that the ESC must meet (must be able to withstand the current your motor may draw, match the type of the motor, etc.) you may consider other properties as well. If I was buying a new ESC, assuming that I don’t want to use the VESC, I’d look for an ESC that has as many programmable options as possible, preferably using desktop software. It seems that the Xerun series provides such an option.
There are many choices but probably the most popular are LiPoly batteries. But the type is not everything. You still have to select the voltage, discharge current, capacity, etc. Naturally, it will all depend on your motor and ESC.
Although the BEC of my ESC is rated up to 3A and I could use it with the Jetson, I decided to power the board with a separate source. I use a 10000mAh power bank which provides up to 3A. proving suitable for the Jetson. It hasn’t failed me yet.
Remember, be careful with LiPoly batteries, they can easily catch fire!
- Camera: you can’t do any sort of computer vision without a camera. Currently, I’m using a Pi NoIR camera as this is what I had at hand.
- Ultrasonic sensors: an easy way of reading distance from objects. HC-SR04 sensors are probably the most popular. You’ll need a voltage divider as they output 5V signals and they also cover a rather narrow range so you should have a bunch of them. It’s safe to assume that they will detect only those objects that are directly in front of them. I created a frame for this sensor if you’re interested.
- Magnetometer: it’s nice to know the heading of the car.
- Motor encoder (usually a Hall sensor): it makes estimating the velocity much easier. To me, it was one of the main reasons for replacing the brushed motor with a sensored brushless one.
- Accelerometer and gyroscope
- Lidar: the lidar will be helpful in mapping and localization. I ordered the YDLIDAR X2 Lidar but it hasn’t arrived yet. Unfortunately, it’s a 2D lidar but still should be very useful.
8. A floor or platform
You’ll need to mount your sensors to something and the chassis isn’t the best choice to do it. One reason is that there isn’t enough space and the other is that, at this stage of prototyping, you should avoid permanent modifications to the base of your project as there may be no way back aside from buying a new chassis.
I 3D printed a deck that will be supported by two aluminum bars mounted to columns that are normally used for a body shell. You can download the STL file here.
9. Wires, connectors, cable sleeves, etc.
ESCs and motors usually come without any connectors and you’ll have to solder them yourself. Also, the connector on your battery may be different from the connector on your ESC and you’ll have to replace it.
As for wiring, I think the dupont connectors are the best choice as they allow for temporary connections and I can guarantee you’ll be reassembling your robot many times during the process.
10. Unexpected parts and tools
The chances are that you’ll want or need something else along the way. Changing one part often requires replacing some other components (like switching from brushed to a brushless motor usually requires a new ESC).
If you want to use dupont connectors you’ll need a crimper. Naturally, you’ll need a soldering iron and it should be quite powerful when it comes to making connections between the ESC and the motor. Also, a multimeter is a must-have.
The most frustrating aspect is that usually you can’t buy the required stuff in a local store. Sometimes Aliexpress will be your only choice and, as you know, that means a lot of waiting.
Common questions at this point
- How much does it cost in total?
You should prepare for about $400 if you want to follow the above guideline.
You can buy a ready kit like this (this is a randomly selected offer, it’s not my recommendation by any means). It may be a little bit cheaper, but it’s not that realistic and won’t be as extendable. You’ll also miss out on a lot of the excitement and satisfaction that comes from building something yourself.
Finally, you can start with a set like this, but it won’t be a realistic car at all. But it may be a good idea to try such a set first if you haven’t done anything with electronics before.
- Why do I need the ESC, how about a motor driver like L293D?
Those drivers are good for low current motors. The motors you’ll be using in this project can easily draw more than 30A which is definitely too much for an L293D. Of course, you could create your own circuit, maybe even based on the L293D and opto-isolators, especially if you plan to use a brushed motor which is relatively easy to control. I’d suggest this route only if you’re particularly interested in making it yourself. As for me, I wanted to have a working model sooner rather than later.
If you don’t want to build your own ESC but still want to have full control over how it works, take a look at the VESC project.
- Do I have to rewrite my code after changing the ESC or switching from a brushed to a brushless motor?
No, communication with the motor is abstracted by the ESC and every ESC should be controllable in a similar fashion. You might need to change some parameters though.
- How long does it take to make a working model?
If you have everything prepared you should be able to assemble everything over a single weekend. But in general, it’s a long-term project, I guess you should prepare for at least a year of fun with that.
- What upgrades did you make to your chassis?
First of all, I trimmed the limiters on the front knuckle arms to extend the steering angle.
Then I replaced all the plastic rings with actual bearings, and I changed the steering parts and front suspension arms to adjustable, aluminum versions. This gives me control over the geometry of the wheels, I can change the camber and toe angles. I also bought all-metal front wheel shafts to further increase the steering range.
I also ordered oil dampers as the standard ones are too weak.
Naturally, none of those upgrades, maybe except for the trimming of the limiters, are essential.
When you collect the necessary parts, from which the chassis, the motors, the ESC, the battery(ies), and the Jetson are essential, we can move on to the next part of our self driving car project using machine learning:
Pulse-width Modulation (PWM) – setting up the Jetson’s GPIO and controlling the servomechanism and motor.
This is the first part of a series that will guide you through the process of building your own AI powered model car from scratch.
In the next part we will deliver a guide to controlling the servomechanism and motor with PWM.
Final note: this project is still evolving and there is a lot to learn along the way. The presented solutions may be suboptimal. I’ll be more than happy to hear your advice and criticisms or answer your questions.