Please note:
“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.
Prerequisites:
-
- 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.
See Installing Ubuntu’s Mate on Pi2
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.
VII. Code
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:
controltank.py_.gz (867 downloads)
This is the download for the joystick interface:
joytank.py_.gz (901 downloads)
Installation instructions for both software downloads follow:
1. controltank.py:
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.
2. joytank.py
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:
http://www.raspi’s_name.local:8081
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.
Hi Ron,
Very impressive!
Dave, Thanks. I am hoping to have a Playstation 3 game controller interface ready in a couple of more days.
very nice !!
I’d hate to ask, but could you perhaps make a script for an xbox 360 controller to function with it? I don’t have a ps3 controller :c
Alec,
Sorry I do not have an XBOX 360 controller so I am not familiar with how they function. Maybe you can find a used PS3 at a cheap price.
Its also worth noting again that I modified a code segment written for another motor controller (while honoring their license agreement) to work with an AdafruitMotor Hat so I did not write the original joystick/button logic in the code. That might have to be rewritten to accommodate an XBOX 360 controller.
Hi Ron,
I’m using the Adafruit Raspberry Pi HAT Product ID 2348 and I found your tank project.
I’m using a Raspberry Pi 3 with the HAT and stepper motors and all the tests scripts included in the Adafruit Library are running without problems, including the Threat tests.
I downloaded your program controltank.py and testet it with the my configuration but without camera.
Very nice project. Congratulation.
What I did changed :
1. line to import from Adafruit-MotorHat adding Adafruit_StepperMotor
2. Class VideoThread : in def __init commented the two lines with MYSTREAM_URL
The program is starting, with the Motor Control Buttons and the sliders and progress bars.
No error message !
But motors are not turning when pushing the Buttons !
Also there are the respective printouts on the screen.
Do you have an idea what is the reason ?
Could you please help ?
Thank you very much.
Johann
I moved your comment here because this is the program I believe you are trying to use.
OK. There are two computers needed here:
Computer A: lets say this is the PI on the tank and it has the program and drivers installed
Computer B: lets say this is a laptop that you will use to log into Computer A.
You must use an application on Computer B which allows X11 or the graphics will not work. To do this you would ssh to Computer A using Putty (Putty must be configured to allow X11). cd to where the controltank.py program is and sudo controltank.py .
You say that the test scripts from Adafruit work. the DCTest.py script expects to control a single motor on the M3 terminal block. If you can successfully run this script then I would think that my program would also be successful except that it is expecting the motors to be connected to the M1 and M2 terminal blocks. Make sure that your motor’s wiring is to M1 and M2 (or change my application’s code to the motor blocks for your setup) or my app will not have the desired effect
Hi Ron,
thank you for your replay.
I did installed Putty with x11 configuration. Started controltank.py from the PC with Putty connection.
Control window with camera image and buttons and sliders are there .
When I push the Forward button I can here a whistle which is stops when I push the Stop button.
Motors are not running.
I’m using STEPPER Motors and they are correctly connected because I did runned the Adafruit Threat Test with them.
Regards
Johann
I think your problem might be with “stepper motors”. I did not use stepper motors but 6 Volt DC brushed motors like this: https://www.pololu.com/product/1575 (without the encoder).
If your have a similar type of motor (does not need to have the gear box) wire it to the motor HAT and see if it can be controlled using my app. If I am correct stepper motors require more than two wires and while the HAT can drive stepper motors the code and drivers I am using are not designed for use with stepper motors but two wire DC motors w/gearbox. Again trying hooking up a DC motor like the type I reference above.
Hi Ron,
I just looked more in detail to the Adafruit HAT library and have seen that for the stepper motor there is no “run” instruction which is only for DC motors.
Sorry for the misunderstanding .
I will go forward also with DC motors.
Thank you again for your support.
Best regards
Johann
Johann,
ok. Maybe I should be more clear in the write up that this is for DC motors and not stepper motors. I will make such a correction. Hope it works out for you.
Ron
Hi ron
I want to make same project but I have l298n & l293d motor controller so I connect rpi to motor controller with jumper cable which pins should i use I don’t know can you help me for about pins ( my english is bad sorry about this :))
Said,
I think the L293D motor controller is for the Arduino. I do not believe it can be used with a Raspberry Pi. I am not familiar with Arduinos or their motor controllers but the following link might help you with the wiring if this is the same board. https://www.adafruit.com/product/1438 is a link for what I believe is the same board as you. On the right hand side of this page is a “LEARN” link which will be quite helpful with wiring if this is the board you have. Hope this helps you, Ron
Thanks for your positive comments. It is alway nice to receive positive comments. Hope you visit often.