Robots that don’t move around by themselves aren’t much fun. Because you’d have to tie them to the back of a pterodactyl to fly them around. Or attach them to the tail of a unicorn to make them hunt for pots of gold.
But don’t worry. Humans have invented the wheel. So let’s not reinvent it.
We’ll simply attach the wheels to Rosie’s plastic body and send instructions to the motor controller board that controls their motors using the Raspberry Pi. Bit like how brains send signals to our legs through our nerves.
But let’s not get too scientific. We just want to make some plastic boxes move around the floor to scare the cat - and the grandparents. Or preferably both. Now how hard could that be?
You will need to have these:
- Raspberry Pi 3, and Raspbian running on SDHC card
- A computer from which you are connecting to the Raspberry Pi remotely
- A robot / rover kit (which includes at a minimum: motors, motor controller board, wheels and some form of additional power supply, like a battery pack). Exact contents will differ depending on the kit. In this example, we have used a rover kit made by Monk Makes (https://www.monkmakes.com/pi-rover/). Other varieties are available.
- A 'body' to attach your wheels to, if you are not planning to use what's included with the kit. We used a cheap plastic storage box for Rosie's chassis, the type sold in high-street discount stores (you know the ones!).
- Electric drill and screw driver (and suitable screws) if you intend to go off-piste with your robot chassis
You will need to have done these things:
You will need to do this:
- Assemble the kit and prepare the chassis. This will basically involve attaching the wheels to the motors, attaching the motors to the motor controller board, and attaching the motor controller to the Pi. An additional battery pack will be connected to the motor controller board as well. Amidst the excitement, don't forget to attach the motors / wheels to the chassis.
- Download (clone) the Python files from GitHub to your Pi that will allow you to control the motors using the motor controller board supplied with the kit
- We like to move it, move it!
What do you get after all this?
After our frantic foray into Raspbian OS and Python programming, we finally get Rosie to do what robots are meant to do. Yes, we'll get to move her around using all the things we've learnt so far. Silly robot dancing, here we come!But before that, time for another introduction. Budding roboticist - meet your new friend, GPIO, which stands for General Purpose Input / Output. It's the bank of 40 pins mysteriously sticking out from one corner of your Pi 3. And as its name suggests, it is how we get the Pi to talk to other bits of useful hardware. And if you want to move a robot around, what could possibly be more useful than a motor, with a wheel attached to it. In fact, two of those please so that we can make it turn around as well.
And in order to claim swift victory, we will simply use whatever Python Libraries (a collection of software) that was provided with the kit to make the robot glide (un)gracefully across the floor. There's plenty of time later to personalise our Rosie. And to understand what's actually going on behind the scenes. But for now, we just want to see a plastic storage box powered by a Raspberry Pi move across a garage floor.
This is simply too much detail:
Let's begin with the chassis.How you get your motors (and therefore your wheels) working will be different depending on the kit you have. So it's best to be sensible for a change, and first read the manual that came with the kit. But there's also nothing wrong with using only the bits from the kit that look useful, and improvising the rest. And where better to start to ignore the instructions than when it comes to the robot's looks?
Enter the... (audience pauses with excitement) plastic box. It's extremely cheap. It's lightweight. And with a few unsightly holes drilled, we can attach anything to it (like sensors, cup holders, mosquito swatters, a spatula, etc, etc). Let's then call this our prototype, as this makes it sound more acceptable.
Next, let's attach the motors with the wheels in whatever position that will give our robotic friend the best balance. I'm sure we could make use of some clever maths and science to advise us here, but where's the fun in that? Eventually, we opted for two rear wheels, and a swivelling castor wheel at the front. There's no right or wrong answer here. Just trial and error. Let's pray that Rosie will glide elegantly across the dance floor like a disco queen, and not stomp across like an angry teen.
We also drilled holes by the wheels so that wires from the motors could be pulled up through and into the box... or the compartment (as this sounds more professional). Direct Current (DC) Motors work by converting electrical current from a power source into rotational movement using a magnetic field, and these cables help carry this current. But current from where? A strange new device called the Motor Controller is introduced. Not only does it provide power to the motors from a separate AA battery pack, but it helps the Pi communicate with the motors, almost like a little translator from the sophisticated and elegant language of Pi and Python, to the primitive signals understood by the motors. The motor controller included in our kit was a RasPiRobot v3.
Hey, look. We now have a plastic box full of circuit boards and cables. And some motors and wheels. Now let's try to move it - not with the power of our mind, but with the power of our Pi.
Now, we could just sit down for the next
For our Monk Makes' Pi Rover Kit, the instructions inform us of some Python libraries (collection of Python software) that we can download. These will allow our Python programs to interact with the motor controller in a way that we can easily understand. Let's navigate to our 'rosie' folder and download (clone) the libraries from GitHub.
pwd...checks that we are still in the pi user's home folder (/home/pi)
cd rosie...let's continue to store all our files in the 'rosie' directory, including the files we are downloading
pwd...confirm that we are now in the rosie directory
git clone https://github.com/simonmonk/raspirobotboard3.git...this funny looking command allows us to clone (or download to you and me) the Python libraries for our motor controller (RasPiRobot v3) to the folder that we are currently in
Providing that your home network has an Internet connection (which it should if you are reading this!), after a few seconds, it should tell you that it has completed.
The installation instructions tell us that we should install the downloaded libraries so let's - for once - follow the instructions.
cd raspirobotboard3/python...Navigate to the 'python' folder in the 'raspirobotboard3' directory. Why? Because it tells us to.
sudo python setup.py install...Run setup.py using Python to install the motor controller files. Why? Because (again) it tells us to.
It looks like the installation script has completed what it was doing. Successfully.
Finally, the instructions tell us that there are also some example programs available on GitHub that show us how to move a rover around. Yes please!
cd /home/pi/rosie...goes back to the directory where we are storing our files
git clone https://github.com/teknoteacher/raspirobot3...download the example programs for our motor controller
After all this, we should be all set to go.
What were we doing? Oh yes - we remember - we want to move a plastic box around the garage floor. We want to be in total control when we do it, like a rock star. We want to tell it where to go, and it needs to listen to us. Thankfully, there is a sample program for that. It's called '08_manual_robot_continuous.py' so let's copy it to our 'rosie' folder.
cd raspirobot3...changes directory to 'raspirobot3' folder containing all the sample Python programs
cp 08_manual_robot_continuous.py ../rosie_wheels.py...copies the '08_manual_robot_continuous.py' program up one folder above (/home/pi/rosie). We are also renaming it to 'rosie_wheels.py', because we can! We can safely edit it in the future now without affecting the original.
cd .....moves back up one folder to '/home/pi/rosie'
Now, are you ready to see a plastic box move? Do you have your popcorn ready? Do you have your sledgehammer beside you just in case your robot goes out of control and needs to be terminated?
python rosie_wheels.py...runs the 'rosie_wheels.py' Python program.
The controls are very simple. Use the 'up', 'down', 'left' or 'right' arrows to move the robot around. Pressing any other key (we prefer the 'space' key) will stop the last movement. If it all gets too much, you can exit the program by pressing 'control' and 'c' keys together (no sledgehammer required).
For now enjoy the small victory. Feel proud of yourself that you are now a proud owner of a non-autonomous robot, which we will attempt to turn semi-autonomous.
Even more reading:
Raspberry Pi's official GPIO documentation can be found here:For this post, great deal of credit goes to Monk Makes (Simon Monk) and Teknoteacher (Alan O'Donohoe) whose excellent work we have referenced throughout:
Thanks for this fun "drive my bot around" test program. I learned a new way to access the key presses.
ReplyDeleteComment about rosie_wheels.py (08_manual_robot_continuous.py):
1) there is a harmless duplicate elif keyp == LEFT: stanza
2) for some reason the ord(keyp) == 3 did not catch cntl-c and quit. I had to kill the process.
(Results from Rosie the Rug Warrior Pi https://flic.kr/p/Ur7JzW )
Hello! We can't take credit for the RasPiRobot3 Python libraries as they have been created by Monk Makes (Simon Monk) and Teknoteacher (Alan O'Donohoe) especially for the board. Next step is to control your robot using a HTTP web interface perhaps? We do this later in Web of Pies and it makes a nice change to using your keyboard.
DeleteYour robot looks cool in the picture!