Raspberry Pi robot with Python
This page describes my attempts to make a robot based on the Raspberry Pi 'brain' and using non-Lego parts.
The main parts of a simple Pi-based robot vehicle are:
- A Raspberry Pi computer. Note that the Pi versions B2 or later are much faster than earlier versions like the B+, and therefore much more fun to work with. The Pi should be equipped with a WiFi dongle so that it can communicate with a second computer - you don't want to have to plug a monitor, keyboard and mouse into the Pi every time you want to make a modification. The WiFi could also communicate with some kind of remote control, but my aim is to make an autonomous, somewhat intelligent robot, rather than a remotely controlled robot (though that can also be fun to play with). The Pi also needs a micro SD card with a special version of the Raspian operating system - I suggest you buy this with the robot (at extra cost) although it is also possible to download an image of the operating system free of charge and copy that onto your own card.
- A base or chassis or frame, typically made of perspex or ABS plastic. The DFRobots Pirate 4WD chassis is metal and looks more sturdy - it's used in this project.
- Wheels or caterpillar tracks.
- Caterpillar tracks are only really needed for use outdoors and are less common on simple robots, though an example is the DFRobot Rover 5.
- Some simple robot vehicles have four wheels, steering like a tank rather than like a car, normally with all four wheels being driven but often by only two motors.
- Some simple robot vehicles have two driven wheels and a castor wheel (using a ball so that it can turn freely in any direction)
- Motors to drive the wheels. There are three kinds of motors that can be used in simple robot vehicles and it is not entirely obvious which is the best type. What IS clear is that most simple robot kits use the first type, DC motors. Note that the Lego EV3 or NXT systems use servo motors, however:
- Direct current (DC) motors are the simplest kind, the kind usually used in motorised toys. Their movement is rather imprecise and unpredictable though it is possible to use encoders to provide feedback to the motor controller to improve the control of the motors.
- Stepper motors can be made to rotate in small, accurate steps provided the load on the motors is not too great for in this case the motor will fail to make the steps that it has been instructed to
- Servo motors receive electronic pulses from the motor controller circuit which are usually used to control the angle which the motor will turn to. This might be useful, for example, to rotate the camera on the front of a robot through a limited angle, but it won't work for turning the wheels. For that purpose, a special kind of servo motor called a continuous rotation servo motor must be used. They are more precise than DC motors but cannot be instructed to turn to a specific angular position - the pulses are used instead to control the speed of the motors.
- Motor controller circuit board to ... control the motors
- Sensors, such as ultrasound sensors or touch sensors or the Raspberry Pi camera (which does not need a sensor circuit board because it plugs straight into the Pi).
- Sensor circuit board (could be combined with the motor board) receives signals from sensors and sends them to the Pi
- Battery pack, typically 6 or 8 AA sized rechargeable batteries (NiMH, for example)
Instead of purchasing each part separately and hoping they will work well together I decided to buy one of the many kits that are available - after a few more hours of research I narrowed the choice down to three:
- GoPiGo seems to have nice electronics and software options, though the whole robot seems flimsy, mainly made from perspex. The GoPiGo is designed to work easily with the Raspberry Pi camera, which is a big plus, and it includes encoders to get feedback about the rotation of the wheels for more accurate steering, which is not the case in most kits.
- The Initio base seems more sturdy and is available from 4tronix in several versions with various levels of sophistication.
- The Dawn Robotics Raspberry Pi Camera Robot includes a pan and tilt mechanism to orient the Raspberry Pi camera. It's based on the Magician chassis. This robot is cheaper than the GoPiGo and I think the software and motor controller board is less sophisticated than for the GoPiGo.
I eventually chose the GoPiGo because it seems to have better electronics and software, has encoders for the wheels and compatibility with the Pi camera. It really is rather flimsy though - I cannot imagine using this robot in the classroom. My GoPiGo cost about 90 Euros from Generation Robots, not including the Pi, the Pi camera, the batteries, the battery charger, the micro SD card or the servo attachment. I regret not buying the servo attachment with the robot for it's important to learn how to work with this kind of motor as well as with DC motors like those included with the GoPiGo. The GoPiGo servo attachment, not shown in this photo but shown in the video below, is designed to rotate the camera, or the ultrasound sensor.
The above image shows where the battery pack would go (8 x AA batteries) but does not actually show the pack. Note the camera board standing vertically on the top level and the green Raspberry Pi board sitting above (and plugged into) the red motor controller board. These two boards are connected together via the General Purpose Input/Output (GPIO) pins of the Pi. The battery pack is supposed to be good for at least an hour of robot use. When working on the programming it's a good idea to power the Pi with a USB charger instead of the battery pack - this will power the Pi but not the motors or most sensors. Dexter Industries has a good set of instruction pages to get you started but be warned: it will probably take you several hours to figure everything out and set everything up, including perhaps one hour to physically assemble the robot.
Let's take a closer look at the motor controller board. Note that the ATMega microcontroller chip is the same one used in the Arduino microcontroller board.
WiFi communication via VNC
Earlier, I pointed out that you don't want to have to connect up a keyboard, mouse and monitor to the Raspberry Pi on the robot each time you want to modify its program. You need to be able to modify it from another computer, connected to the Pi via WiFi. The simplest way of dong that is (on a Windows computer) to install a free program called Putty on the Windows machine and then communicate with Pi via a Secure SHell (SSH). But this will only give you a command line interface, as if you were using LXterminal on the Pi, and that is very limiting. The ideal would be to be able to see the Pi's normal GUI (desktop) on the Windows (or Linux, or other) machine, and that's possible using a Virtual Network Computing (VNC)connection. I believe this is easier to set up than a Virtual Private Network (VPN), which is similar. The most popular free choice for VNC communication seems to beTightVNC, which can be downloaded HERE. You need to run TightVNC Server on the Pi and TightVNC Viewer on the client machine. See HERE or HERE orHERE for help setting up TightVNC. It took me about an hour to get a TightVNC connection going between my Pi (version 2) and my Windows machine. Or so I thought. In the end I had tens of hours of frustration trying to get a reliable, stable VNC connection between the Pi and a couple of different computers. I had particularly bad problems using a Pi B+ as opposed to a B2, presumably because the B+ is so much slower. I also had much more trouble getting VNC to work with the Dexter Industries robot-ready SD card image than I did with a standard Raspian SD card image. Part of the problem is that the Dexter Industries website does a poor job explaining how to set up the VNC connection - it doesn't even make it clear that their image already includes a VNC server. Also, I was never able to get the raspberrypi.local address to work, as suggested on the DexterIndustries/GoPiGo website. I could only make the connection via a normal IP address. I was able, however, to instruct my router to give the GoPiGo a static IP address.
The TightVNC website recommends that additional free software called DFMirage mirror display driver be installed on the Windows client (viewer) to reduce the network load caused by running VNC - see HERE. Note that a version of TightVNC is also available for Android devices, with the name Remote Ripple - I haven't tried it yet...
By the way, another option for working remotely with the Pi might be to use theAdafruit webide. Adafruit describes this as 'by far the easiest way to run code on your Raspberry Pi'. I started trying to install this but before I could get it going I decided to uninstall it because I thought it might be conflicting in some way with my TightVNC installation and might explain why I was having so much trouble getting TightVNC working.
Problem: I wanted to be able to demonstrate my GoPiGo robot at my local computer club, Pobot, but that would mean working outside my home WiFi network so my TightVNC setup would no longer work. Worse, they don't even have a WiFi network at my club. Solution: I set up my Galaxy S4 phone as a WiFi hotspot and connected both the GoPiGo and my Windows laptop to that - it worked. Even better: it seems the S4 always give the GoPiGo the same IP address, even though I have not specifically set up a static IP address for the GoPiGo on my phone.
Problem: I wanted a simpler solution, bypassing the need to use my phone as a hotspot. Solution: I used Connectify on my Windows laptop to make my laptop become a hotspot, then connected the GoPiGo directly to that. (Connectify will interrupt your connection regularly unless you pay...)
Although I was able to get a basic GoPiGo program going I was not able yet to run the setup that streams video from the robot to the remote computer. The server did not launch correctly only the Pi. I'm still trying...
Come back to this page to see what I was able to do with my GoPiGo. Actually, working with the GoPiGo makes me appreciate even more how robust and satisfying it is to work with the Lego EV3 robot - what a shame there is no easy way to program the Lego EV3 system in Python. I tried using EV3dev / EV3 Python for this on more than one occasion but each time found the experience to be so frustrating that I gave up. I also considered using BrickPi for this but decided against it - see this page.