Stephen Smith's Blog

Musings on Machine Learning…

My Raspberry Pi Learns to Drive

with 6 comments


Introduction

I received an early XMas present of a SunFounder PiCar-X. This is a car that is controlled via a Raspberry Pi, it contains a number of sensors, including a pan-tilt camera, ultrasonic module and a line tracking module, so you can experiment with self-driving programs. It is capable of avoiding obstacles, following objects, tracking lines, and recognizing objects. You can program it using either Python or EzBlocks (a Scratch-like language).  It costs under $100 and includes a battery that powers the whole thing, including the on-board Pi. This article is an overview of the assembly and setup process.

Which Pi to Use?

Since I received my Raspberry Pi 5, I figured that I’d use my 8Gig Raspberry Pi 4 for the robot car. However this Pi 4 has an active cooler attached, so won’t fit. So I switched this with the 4Gig Pi 4 I’ve been using to track aircraft. I still had to pry off one heat sink to make it fit under the robot controller hat.

The PiCar-X supports the Raspberry Pi 4B, 3B, 3B+, and 2B. Note that there isn’t support for the Pi 5 yet. Also for all the software to work, you have to use the older Raspberry Pi OS Bullseye. The claim is that some Python components don’t work with Bookworm yet.

Assembly

When you open the box, there is a four page spread with the assembly instructions. I followed these instructions and was able to assemble the car fairly easily. There were a couple of hard to tighten screws to hold one of the stepper motors in place inside an aluminum holder, but as these things go it wasn’t bad and didn’t take too long. One nice thing is that the kit includes lots of extra screws, ratchets and washers. Anywhere where four screws are required, the kit provides six. So I never ran out of screws and have lots of extras for the future. It also included screwdrivers, wrenches and electrical tape.

All assembled, but now what? The instructions showed how to assemble it, but not what to do next. So I went to the web and found the SunFounder docs online here. I followed along installing the right version of Raspberry Pi OS to an SD card and then booting up the Raspberry Pi. I connected the Pi to a monitor, keyboard and mouse to do the initial setup, though doing this via secure remote signon would have been fine as well. Installing the various Python components and libraries was easy enough. Might be nice if they provided a script to do all the steps for you, rather than a lot of copy/paste of various Linux commands.

Turning On?

Next up, turn the robot on and play with the sample Python programs. Ok, how do you turn the robot on? Supposedly there is a power switch on the robot hat, so which yes there is, but in a different place than the documentation. Turning it on, did nothing. Hmmm. Fortunately, I thought to try plugging the Raspberry Pi’s power adapter into the USB-C port on the robot hat rather than the USB-C port on the Pi. When I did this the board came to life. Apparently the battery it came with was completely dead and it is charged via the USB-C port on the robot hat. Reviewing the documentation, this is mentioned nowhere. Generally using the PiCar-X, you may as well always use the robot hat’s USB-C port rather than Pi. I think they should mention this. The robot hat’s USB-C port isn’t even mentioned in the documentation.

Calibration?

Once I had it powered up, I could try the sample Python programs. The first ones they want you to run are meant to calibrate the stepper motors. Ok, fair enough, however the diagrams show doing this before assembling the robot? Funny this has not been mentioned anywhere until now. Anyway I did the best I could with the robot already assembled and it seems to work fine. I don’t know how important this step is, but if it is important and should be done during assembly, then it should be highlighted in the assembly instructions.

Running

Next there are some simple Python programs where you press a key to move forward, turn right, etc. At this point you want to untether the robot from any monitor, keyboard, mouse and external power. Put it on the floor and experiment a bit. Once untethered you need to use ssh to remotely sign in to the onboard Pi and run the various Python samples that way. Fiddly and rather primitive way to control the robot, but these samples show the basic building block you can use in your own programs to control the car. These are nice and simple programs, so it’s easy to follow how they work.

Phone App

There is an iOS/Android app to control the car from your phone/tablet. You have to have a specific program running on the Pi to receive and execute commands from the phone.

The phone app is a bit of a toolbox where you select various components on the screen to control the robot. There is a tutorial that guides you through creating a working control panel for your device. Note this app can control a variety of SunFounder robot products.

Clicking on edit for a control or the plus sign if there isn’t a control lets you add something:

I didn’t follow the instructions very carefully and received Python errors when I tried to control my car. It was a simple matter to see that the Python program is quite picky that you put the correct controls in the correct places. If you want to customize the app controller, then you have to make matching changes to both the control layout in the app as well as how the Python program running on the Pi reacts to the data it receives. The different controls send data in different formats and you get Python data mismatch errors if they don’t match.

Once I set the app up correctly, then I could control the robot from the app and use it to chase my dogs around the house.

Summary

This robot is intended to allow you to play with programming a robot car and to this end it provides all the tools you need to do this. The documentation is a bit lacking in places and it appears the robot hat has been upgraded without the documentation being updated to match, but this shouldn’t stump any true DIYer. If you are looking for a remote control car to control from your phone, there are simpler, easier to use products out there. The idea here is that you have a fairly powerful computer, in this case a Raspberry Pi 4, that can run various machine learning algorithms, run complicated Python programs and allow you to write sophisticated programs to control the car. For under $100, this is a great platform to play with all these technologies.

Written by smist08

December 15, 2023 at 1:46 pm

6 Responses

Subscribe to comments with RSS.

  1. […] time, I introduced SunFounder’s PiCar-X, a robot car controlled by a Raspberry Pi. The previous article covered assembly and initial setup. […]

  2. […] time, we programmed a SunFounder PiCar-X to behave similar to a Roomba, to basically move around a room following an algorithm to go all […]

  3. […] a separate note, AI is making great strides in robotics. Smarter robots are appearing all the time and the costs have been coming down quickly. Now you can build a robot […]

  4. […] recently received a SunFounder PiDog robot dog kit. I reviewed the SunFounder PiCar here along with software projects here and here. There are a lot of similarities between the PiCar and […]

  5. […] introduce how to program the PiDog to do our bidding. Previously, we looked at the SunFounder PiCar and how to program it. Both robots share the same RobotHat to interface the Raspberry Pi with the […]

  6. […] of a SunFounder robot, this time the SunFounder GalaxyRVR Mars Rover. I previously reviewed their PiCar and PiDog which both used a Raspberry Pi as their brains. In contrast the Mars Rover uses an Arduino […]


Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.