(Autonomous Vehicle using Raspberry Pi and Motor Pack )
Submitted by Ava L, East Midlands, UK
I always wanted to make some kind of "intelligent" robot type of device but considered it too big a task. Then the Raspberry Pi appeared. This clever little device offered a self contained credit card size computer that could be programmed and then "let loose" to carry out its programming untethered to monitor, keyboard, mouse or mains power. It isn't even that expensive (at time of writing its about £25) . Although the Raspberry Pi can provide all the computing power I need, it doesn't have any built in facilities for motor control and other such "real world" devices. It does have USB ports though ! I decided the best option would be to use a MotorBee from PC Control Ltd. This provides control of up to four DC motors, one servo, 4 switching outputs and 5 digital inputs and , most importantly, it simply plugs in to one of the Pi's USB ports (via small USB lead). This also meant that I didn't have to get into assembly code programming, boot sequences or minute details of chip firmware since control of the MotorBee can be done directly from the Python language. The code for doing this is published on PC Control's website ( link).
I knew I would need motor driven wheels and a method of steering as a minimum so I opted for the Motor Pack (link) which contains a MotorBee (link) and a DC geared motor assembly which was ideal for driving the two rear wheels. The battery packs included were also put to good use as you will see later.
The MotorBee includes an output and controls for a Servo, which made the choice of the steering mechanism quite easy. i.e. the servo would be used to turn the front steering wheel while the back ones did the driving. A servo is not included in the Motor Pack so I had to add that to my shopping list. The Raspberry Pi was obtained from RS Components who are one of the authorised distributors of the Pi. I decided to get the newer model B+ simply because it had the latest features and enhancements but the actual functionality required would have easily been provided by the original model B as well. I chose plastic model wheels from an ebay shop to match the spindle (axle) diameter of the geared motor which is 3mm. These wheels are available in a whole range of sizes and styles from a large number of ebay shops. These have a 2.6mm centre hole and simply push fit on and make a good tight fit.
I also needed a good way of mounting the Pi to give it some protection and allow connection of USB leads etc. I considered one of the many custom made cases available but decided I wanted full access and display of the board so I opted for a simple mounting board with standoff pillars. The one I chose ended up being the main structural component for my vehicle , which would be used to mount the motor, gearbox, wheels, servos, battery packs, motorbee and Pi. It was the "mounting board system" again from PC Control ( link ).
The first step was to drill the mounting board to mount the two circuit boards, the battery packs and the motor gearbox. The board itself is a rigid plastic type material 3mm thick. It's covered with peelable film so you can drill etc. and only remove the film when ready. Removing the film reveals a semi-glossy black surface which suited the overall appearance of the unit. Drilling was quite easy using my little 12v hobby drill. The holes were only 2.5mm for the board mounting and 3mm for the other bits. The mounting board came with nuts and bolts (2.5mm) and standoffs which I used to mount the Pi and MotorBee. I decided to use seperate battery packs to provide seperate power supplies for the Pi and the motors. This would avoid any possibility of the voltage spikes normally associated with all electric motors getting into the Pi and possibly causing damage. i.e. the Pi would have a smooth clean DC supply. The Pi actually needs 5v DC but most battery packs (including the ones I used ) provide 6v ( i.e. 4 x 1.5v AA batteries). This meant I had to add a small voltage regulator ( LM2940CT ) on a piece of stripboard to reduce it to 5v. (RS components part number 533-8164 (link).
The one slightly tricky bit was mounting a wheel on to the servo. This was going to be the front "steering" wheel. In the end I used a small metal "mending" plate from the local DIY shop and bent it into a U shape. This was then attached to the servo arm with wire used a bit like thread. The servo arm was one of a range supplied with the servo. A 3mm spindle was used to hold another wheel mounted through the existing holes in the mending plate and held in place with two little gear wheel "thingy's" left over from a previous project. I suppose these could have been proper nuts or something but they fitted the 3mm spindle with a good tight fit and did the job well.
The rear "driving" wheels were really very easy to fit since the whole motor gearbox (supplied with the motor pack ) was virtually ready to attach via the mounting holes in the motor support housing. The only thing I had to do was add a little wooden block spacer (painted black, of course) to provide clearance for the wheels fitted to the spindle. (click photo to enlarge)
The wiring was fairly straight forward being mostly the connection of battery power to the boards plus a couple of wires to the motor. The servo just plugged straight in to the connector on the motorbee. A standard A-B USB lead connected the Pi to the motor bee. I cut off a micro-usb connector from an old cable and used it to connect the battery power to the Pi (via the 5v regulator as already mentioned) and a 9v PP3 battery was used to supply motor power to the motorbee for use with driving the rear gearbox motor. It was simply connected to the GND and V+ terminals on the motor bee.
I normally program in C or C++, but, since Python seems to be the common natural language for the Pi, I decided to give it a try. The code examples published (link) gave me a good head start with controlling the Motorbee. It just needed an installation of Pyusb utility which I downloaded from the pyusb.org website (link). This provided the basic USB functionality needed to use any USB device (in this case the Motorbee). The rest of the code developed as I went along by increasing the complexity of movements little by little until I could see what was possible. i.e. the try, test and improve method.
Once I had a simple program running on the Pi controlling the pi-mobile, I had to figure out how to make this run automatically on startup without any keyboard, monitor or mouse present. It took quite a bit of digging around the web before I found a solution that suited me. I chose a solution where the program runs automatically on startup, but, if a keyboard, monitor and mouse were present it would still be useable. Some solutions involved changing the boot sequence and bypassed the normal terminal access modes. These were more optimised for faster operation but made it difficult to get back into edit mode especially if the program crashed in any way. My way was almost transparent and could easily switch. The way it was done involved the following two steps...
1. Edit the "inittab" startup file so that it automatically logged in the current user (i.e. me) using...
sudo nano /etc/inittab
This uses the "nano" text editor to edit the inittab file. The "sudo" is necessary to give you the permissions to edit this file. You then need to find the line ....
1:2345:respawn:/sbin/getty --noclear 38400 tty1
and replace it with..
1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1
This assumes the current user is the default "pi". Once this is edited, the user "pi" will be automatically logged in at startup.
2. The user "cron" table has to be edited so that it automatically starts your program at startup. This is done by simply typing the command...
The following line should then be added to the bottom of the file...
@reboot sudo python /home/pi/motorbee.py &
This will cause the program "motorbee.py" to be run each time the system is rebooted (started). Note that the full pathlist to the program needs to be included and that "sudo" is again required to give the necessary permissions.
The beauty of the above method is that, if no keyboard, mouse or monitor is present it will just run the program specified, but, if they are present, it will provide you with a terminal access screen exactly as you had before. The program "motorbee.py" will still run even if you have your terminal screen etc, but you can just "kill" the process, if need be, from your terminal.
Testing and Development
My initial tests of the "Pi Mobile" were simple sequences of automatic maneouveres around the floor of the room. Simple, but highly entertaining. The dog was convinced it was alive :-)
I hope to explore more elaborate uses by making use of the unused controls remaining on the motor bee. i.e. it has two more motor controls, 5 digital inputs and 4 switching outputs which are just screaming to be put to use. I'm considering adding a motorised "crane" or similar lifting device to the top of the unit. I don't know what it will be "picking up" yet but it I'll think of something. Id like to use the inputs for some type of sensor. Possibly an infrared type with a simple digital output. That way I could use them on two, or more, inputs and steer the mobile in the direction of the "body heat source". Maybe it could follow the dog around the room. He'd be convinced it was alive then :-).
I think the best use of the switching outputs would be to control lights, buzzers or possibly some solenoid type mechanism to go on the end of the crane for "picking up" those undefined objects ! In any case, I'll submit another article when complete.
ED: I look forward to it. Thanks Ava.
Click on any photo to enlarge (opens in new window)
PC Control Ltd: All articles are publish as submitted in good faith but we do not accept any responsibility for the authenticity or content of these articles. Any article breaching copyright will be removed from this website as soon as we are made aware.