Pi2 Tank with Adafruit MotorHat
“PS3, Playstation” is a registered trademark of Sony Computer Entertainment Inc.
PiBorg is a registered trademark of Freeburn Robotics Limited
Adafruit is a registered trademark of Adafruit Industries, LLC
LEGO®is a trademark of the LEGO Group of companies
Dremel is a trademark of Dremel, a division of Robert Bosch Tool Corporation,
None of the above companies sponsor, authorize or endorse this site.
Images may be “double clicked” for a larger size.
Disclaimer: Let me state that while I have made every effort to be accurate there could be errors so if I cause anyone additional expen$e/lost time let me apologize now. Proceed at your own discretion and read twice or post questions.
- Raspberry Pi 2
- Ubuntu’s Mate OS – for Pi2
- PS3 Game Controller – optional
- Bluetooth USB dongle – optional
- Adafruit DC & Stepper Motor Hat – Stepper Motor Hat here
- Micro SD Card – 16 Gig or greater rated as Class 10 (a measurement of card’s performance)
- a Tank chassis with motors (there are many other chassis at this site) – motors must be rated between 6~12 volts (my tank chassis came w/2 motors). Always inquire into the motor specs when ordering to be sure they are compatible with the HAT. Some chassis come with 24 volt motors. Please note that this applications code and drivers do not allow the use of stepper motors.
- 8 X AA – battery holder – this will provide power to the chassis motors because the PI2 cannot do this
- wall plug power for Pi2 – used to provide power during static testing
- wall plug power for motors – used to provide power during static testing
- various tools/pieces like a soldering iron (Motor Hat requires some soldering)
- PC Mother Board standoffs (M3 for tank chassis and M2.5 for Pi2 and Hat) (standoffs are optional),
- Plexiglas – optional
- Lego bricks/blocks/baseplate/plates
- USB camera – I used a Logitech – NOTE: the small Pi camera is not compatible with the Ubuntu Mate OS
- Hex Key set – metric
- hobby file set
- small wrench set – got one at Sears
- Dremel drill press – for drilling into Plexiglas – optional
- Pi2 templates for drilling holes
- lock washers – optional if you dare
My tank chassis arrived from the above provided link today. It took about two weeks from China. Not bad. Here is how it looked (some assembly required):
I. Assembling the tank chassis.
In my case the vendor I purchased the tank chassis from allows so many days in which to file a dispute. Therefore it becomes imperative that you assemble the crawler as soon as possible to ensure that all the components are there: screws, wheels, motors etc. I have assembled this crawler per the steps below and all parts where present. I did “add” components because I wanted some extra wiggle room to expand the “crawler”. Most noticeably I added brass M4 standoffs and used them in place of 4 of their nuts. I will also be adding a Plexiglas platform to give me extra storage on the platform.
Here we go:
II. Configuring the SD card.
III. Soldering and mounting the Stepper Motor Hat
How to solder video. Very instructive if you have not soldered before. Its actually fairly easy.
IV. Mounting the Pi2 and MotorHat on the tank chassis
V. Software/applications prerequisites on the Pi2
I have removed this section of instructions from this page and replaced it with this software procedure. It is a copy of what was here but modified to streamline the installation procedure. Also it applies to any build utilizing the PS3 Game Controller. Support for both Adafruit and PiBorg hardware is/will be provided. Currently PiBorg build procedure is not done but can be acquired from the PiBorg site.
VI. Static Testing
7/12/15 Completed the control interface/logic using PygTK (python TK). It looks like the following (double click on the image for greater detail):
This robotic control interface is written in pygtk. The window shown above gives the Title: “Tank Control” and is an 8X8 table with cells being assigned/filled buttons, sliders, progress bars and video. While the two progress bars in the lower right indicate “RPM” they actually represent a “value” being issued to both electric motors between 0 (no power) and 255. This value is then scaled on the slider as a percentage, i.e., ground_speed (set by slider) divided by 255.
Button logic is like this:
Forward: applies equal power to both motors, so if the slider is set for 50 then both motors operate at 50.
Left: press and hold: this causes the right track operating at 50 to have the “rate of turn” slider value of 5 added to itself so that the right track is turning at 55, causing the crawler to turn left. Until I can test this off of the PVC jacks I won’t know how effective this will be. It might result in an extremely wide turn. When the button is released the right motor will return to its original set speed of 50 as per this example. Because the “turn buttons” are press and hold the rate of turn cannot be adjusted while in a turn. Based upon testing this logic had to be re-done. Here is how it works now: If the forward speed is 50 then a left turn will add the rate of turn value to the right track AND and subtract it from the left track. This makes for a more responsive turn. Before the turn radius was wide and a turn command resulted in the tank accelerating in the turn only to slow when the turn button was release. Addition code was required so that the reduction on the “inside track” did not go negative. You could have a forward speed of 50 with a rate of turn at 80. The would reduce the inside track rate to –30. I have no idea what effect this would have on the motor.
Right: Press and hold: Same logic as for Left
Reverse: Same logic as for Forward
This crawler as configured in the following video is heavy ~ 3.5lbs. now that two batteries and Lego bricks have been added. In the video I have attempted to capture two perspectives: one from the control side and the other from the operators side – what the operator sees. The video presents a control sequence followed by the equivalent of actual tank movement from the operator’s perspective. I am not sure how successful the video is but the crawler seems to be very much in command. Be sure to watch in “full screen” mode or the video will be unreadable. Here goes:
And a better way to control a robotic tank/crawler using a PS3 GameController with the Adafruit MotorHat as seen in the two videos below. Some observations: 1. the video from the tank’s “eye” is actually smooth but I was forced to do some editing (its amazing what the camera “sees” at ground level), 2. the PS3 GameControllers range is between -1 and +1 on both the forward/backward and left/right axis. The range for the Adafruit MotorHat is 0-255. I need to polish the algorithm for motor control. At current weight a motor value of 30 is not sufficient to move/break inertia of the tank. I might look at adjusting the range to 35~190 and making appropriate adjustments before handoff to the motors through the MotorHat.
8/8/15 I have completed work on modifying code from the PiBorg website under their license agreement to allow control of the tank with a PS3 gamecontroller and a USB Bluetooth dongle. I am testing and I will post here when completed. I would like to point out that PiBorg also makes motor controllers and that they have a new one capable of supporting higher power levels than does the one I used here from Adafruit. You can see PiBorg’s big tank here – scroll down for videos.
The following downloads of code are provided as is without warranties of any kind. Please read the header of the downloaded file(s) for more specifics. You only need one file: the pygtk interface or the joystick interface (of course you may download both).
This is the download for the pygtk interface:
This is the download for the joystick interface:
Installation instructions for both software downloads follow:
This is a compressed file using gzip. Uncompress it (see below) and place the file in the $HOME/Adafruit-Motor-HAT-Python-Library/examples/tank directory. Make sure that it is executable:
-rwxrwxr-x 1 user user 16443 Jul 28 13:44 controltank.py
execute the script:
to execute the script you need to remotely log into the Pi2 from another PC. This PC must let you login using X11. I suggest that you use the free product Putty (make sure it is configured for X11).
once logged into the Pi2 remoteley, cd to the directory above in step 1
sudo controltank.py &
this should launch the graphical pygtk interface and allow you to control the tank. Remember that motion must be running or this will fail.
This is a compressed file using gzip. Uncompress it (see below) and place the file in the $HOME/Adafruit-Motor-HAT-Python-Library/joyborg directory. Make sure it is executable.
execute the script:
here is the logic for a game controller:
a. it must first be bound to the Pi2 – so bind it now if you have not already done so. Remember that a PS3 can only be bound when connected to he Pi2 with a USB calbe. You cannot bind wirelessly. Period. Manually bind like this: sudo ./sixpair
Now remove the usb cable from Pi2 and PS3 GameControllerj
b. sixad must be running. It should have started on boot but if it did not then manually start it: sudo sixad –start &
c. Press the PS3 GameController “PS” button. It should vibrate and you are ready to go.
login to the Pi2 (plain ssh is fine you don’t need X11) and cd to: $HOME/Adafruit-Motor-HAT-Python-Library/joyborg
sudo joytank.py &
Pickup your PS3 Game controller. Drive tank.
NOTE: By using the PS3 GameController you might think that you loose the “tank’s eye” – video cameras. Because motion supplies its own mini http server you can launch any browser (IE might be an issue) and type in the following:
this will display the Pi2’s eye on a webpage. Not the url. It uses a “.local” which is a way to address a PC on your network that is receiving a DHCP address. So if your Pi2’s name is dogbone then http://dogbone.local:8081 should work.
Points of interest:
- this is a compressed file using gzip, so to uncompress do: gzip -d controltank.py.gz
- there is absolutely no error checking done in controltank.py so if a piece is not loaded/operational then the app will fail to run. For example, the Linux app “motion” is required. If it is not running then the script will fail. Make sure to configure “motion” to run a boot. I have detailed how to do this above.
- With respect to both programs I have modified the code to accommodate my tank with respect to its weight, types of motors etc., so when using for the first time start at a slow speeds and workup. It would be a shame to set the highest speed and have you tank run into the wall and disintegrate. You may have to change some settings in either of the files to accommodate your tank’s behavior with respect to straight line/turning speed. It is easy to do. The code is fairly well documented.