This Tutorial was created by Kevin Rockwell and Colin Wilkie
The Autonomous Turret
Objective: To create an autonomous turret that will fire an airsoft gun using an FPGA.
Our goal has been to create a robotic turret and we chose to design one that can eventually be autonomous. We are just finishing Phase 1, which is the first prototype of the robot and will be controlled manually. Once we finish this phase we will make the decision to either go fully autonomous or do something else, perhaps control through an iPhone with a live video feed.
Unlike most turret projects ours is based off an FPGA(Field Programmable Gate Array) rather than a microcontroller. The advantage of using this board is the ability to write code and have it synthesized into hardware. This approach requires a bit more coding and in the VHDL language rather than in C# or the other languages that many microcontrollers use.
Furthermore, we tried to keep the expense of the project to a minimum; so far it has only cost $100, making it considerably less expensive than other guides. The bulk of the costs came from the servos at $30 each, and the last bit came from purchasing P-Mods and the actual firing mechanism (more on these later). If you own an FPGA or are planning on purchasing one, you will find our guide is an easy and fun way to gain experience using the board and its many features.
This project consists of multiple steps. It will guide you through building the housing and which materials are best to use. It will include instructions on installing and controlling servos and which types are the best to buy/use. You will be able to create a variety of programs in VHDL and the tools to begin new projects featuring the FPGA. At the end of this guide you will have yourself a nice looking controllable robotic turret as well as the knowledge to use UART protocols, seven segment displays, LED’s, servos, basic VHDL, and power usage.
This project will require the following supplies, for more information continue to the next page:
An FPGA, we use the NEXYS board: http://www.digilentinc.com/Products/Detail.cfm?NavPath=2,400,797&Prod=NEXYS
The free software from the Xilinx website: Adept 2.0 and Xilinx ISE 11
USB cable: should come with the FPGA or you can buy one at any electronics store
Servos: for more information visit the Servo Page
Housing: for more information visit the Housing Page
Power: for more information visit the Power Page
Three Peripheral Modules: for more information visit the P-Mod Page
A PS/2 Keyboard for manual control: for more information visit the keyboard interface page
Before starting this project we suggest that you do a little research and give yourself a few goals you wish to accomplish. Read through our guide to get an idea of what it will take to complete it and visit some of the sites on our links page to get a some inspiration for your project. If you do not have enough money to complete the project, there is a great tutorial on funding projects on the Sons of Robotics website. If you do choose to use our guide, please leave us a comment and feel free to ask any questions you may have along the way.
What you will need:
1. An FPGA, we use the NEXYS board: http://www.digilentinc.com/Products/Detail.cfm?NavPath=2,400,797&Prod=NEXYS
. Most FPGA boards should work, however we suggest using the NEXYS, NEXYS2, or Spartan 3E
2. The free software from the Xilinx website: Adept 2.0 and Xilinx ISE 11
3. USB cable: should come with the FPGA or you can buy one at any electronics store
4. Servos: for more information visit the Servo Page
. R/C servos for planes and helicopters will work well
5. Housing: for more information visit the Housing Page
. For our project we use plastic simply because we got it for free from our high school plastics class, however wood or metal will also suffice
6. Power: for more information visit the Power Page
7. Three Peripheral Modules: for more information visit the P-Mod Page
8. A PS/2 Keyboard for manual control: for more information visit the Keyboard Interface Page
We use a few extra tools for building, they are listed below
Power tools: Dremmel, Drill, Saw
File, plastic glue, small screws and nuts, grip tape, velcro, and small gauge wire for the power
Where to begin:
Here is a great introduction to FPGA video we found on youtube:
The most important aspect of this project is the FPGA. Getting aquainted with it is an important first step. If you bought a Digilent board go and read through the reference manual and complete any sample programs they may have. If you are new you will be starting the same place we did, with lighting up LED's and making them blink. Check out the beginner projects below for a few examples.
Getting Started with VHDL and your FPGA
Start by opening the ISE you downloaded from the Xilinx website. Click 'File' then 'New Project' and a box should pop up. First you will need to title your project, we named it "Led_Control" (you can not have spaces in the project names, use underscore instead). Next you will have to input the device specifics, it will remember these values for future projects. Under 'family' and 'device' find the corresponding FPGA you will be using and select it. Under preferred language select VHDL, then press next. You can either add a new source on the next page or make it later, for simplicity we will add it now, click 'New Source' then select 'VHDL Module' and give it a name. Next it will ask to create the ports, you will need two. The first will be 'BTNS', 'in' and it will have a bus of 8. The second will be 'LEDS', 'out' and will also have a bus of 8. Once you are done click next and then finish. You should have a fresh new module in front of you.
So how do we make these LED's light up? We can do this a number of ways, in this case we will be assigning them to corresponding buttons, an alternative would be to assign them a value of '1'. We write the operation after the line 'begin' and the line 'end'. Begin by typing "LEDS(0) <= BTNS(0);" This will assign the LED to the corresponding switch. The numbers simply indicate which LED in the bus you are referring too. Once you have done this for all 8 LED's, it is time to make a contraint file. On the left side of the ISE should be a box, go to the 'Design' tab and then find at the top of the box where it has your file "Led_Control" and right click on the file. Select 'New Source' and this time choose the 'Implementation Constraints file', this should open a new module where you will assign the physical outputs and inputs on the board with those used in the program. Refer to your reference manual to find these values, the ones provided will only work on the NEXYS board.
library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; ---- Uncomment the following library declaration if instantiating ---- any Xilinx primitives in this code. --library UNISIM; --use UNISIM.VComponents.all; entity Led_Control is Port ( BTNS : in STD_LOGIC_VECTOR(7 downto 0); LEDS : out STD_LOGIC_VECTOR(7 downto 0) ); end Led_Control; architecture Behavioral of Led_Control is begin LEDS(0) <= BTNS(0); LEDS(1) <= BTNS(1); LEDS(2) <= BTNS(2); LEDS(3) <= BTNS(3); LEDS(4) <= BTNS(4); LEDS(5) <= BTNS(5); LEDS(6) <= BTNS(6); LEDS(7) <= BTNS(7); end Behavioral;
Contraints File: The values after "LOC" are found in the reference manual for your device.
NET "LEDS<0>" LOC="L14"; NET "LEDS<1>" LOC="L13"; NET "LEDS<2>" LOC="M14"; NET "LEDS<3>" LOC="L12"; NET "LEDS<4>" LOC="N14"; NET "LEDS<5>" LOC="M13"; NET "LEDS<6>" LOC="P14"; NET "LEDS<7>" LOC="R16"; NET "BTNS<0>" LOC="N15"; NET "BTNS<1>" LOC="J16"; NET "BTNS<2>" LOC="K16"; NET "BTNS<3>" LOC="K15"; NET "BTNS<4>" LOC="L15"; NET "BTNS<5>" LOC="M16"; NET "BTNS<6>" LOC="M15"; NET "BTNS<7>" LOC="N16";
After assigning all of the values go back to the main module, here you will click 'Synthesize" on the lower left box and the click "Check Syntax". If it is successfull double click "Generate Programming File" underneath it, if it fails to check syntax or create a file, find the error in the console at the bottom of the ISE and fix the problem and try again. Once it successfully generates the programming file connect your FPGA to the computer with the USB cable and turn it on. Open up the Adept 2.0 program you downloaded from Xilinx and find your device. There should be two places you can upload the program too, the flash memory and the FPGA, the flash memory will remember the program even after turning it off however the FPGA will not. Go ahead and just load it onto the FPGA and test it out. If you upload it to the Flash you will need to hit the reset button on the board after it uploads, this will erase previous memory and execute the new code.
Have fun playing with those LED's. There are many sample programs and tutorials out there for VHDL, we suggest you find a few and learn how to use clocks and program 7 segment displays.
We purchased a X47 Airsoft Rifle from Stunt Studios at Big 5. We brought the rifle home and weighed and tested it to see if it would work. We then carefully took the important pieces out (Firing mechanism, barrel and battery) and discarded with the rest of the plastic. Most of the plastic was heavy and only for show. This lightened up the load the servos will have to bear, allowing for a quicker response time on the servos. The only problem is we now lack structure for the gun as well as a loading mechanism, but these will be fixed with some metal pieces and a makeshift hopper using gravity as its acting force.
Our Servo Specs:
Speed: .18 sec/60degrees at 4.8V, .16 sec/60degree at 6.0V
Torque: 213 oz-in (15.5 kg-cm) at 4.8V, 231 oz-in(16.8 kg-cm) at 6.0V
Weight: 2.1 oz (60 grams)
Price: $24.95 per servo ($31.01 with S&H and Tax)
More Specs and measurements can be found at following link: http://www.dpcav.com/xcart/product.php?productid=16491&cat=255&page=1
|Clock: .05 ms
||Degrees from center
The clock on the FPGA is a signal that runs at 50 MHgz, we divided this down to make a slower clock that sends a signal at 0.05ms. The chart shows the "Time(ms)" which is the time period that a high signal is sent to the servo followed by a low signal for the rest of the 20ms refresh time. All servos use a 20ms time period, so a high signal lasting 1.5ms will be followed by 18.5ms of a low signal. 1.5ms is a standard that will bring the servo to the center, however not all servos follow this rule so we did a little bit of testing and confirmed the center mark manually using this tutorial.
The FPGA counts the length of high signals from the 0.05ms clock, so after a set amount of counts the signal will change from high to low and remain low for the 20ms refresh time. The column "Count" represents this amount, so 15 counts of 0.05ms is .75ms (15 X 0.05) and a high of .75ms moves the servo to 160 degrees. Note: The last column times 0.05ms equals the first column. The angles we found simply by testing different time periods and found corresponding angles using a protracter. The servo appears to be able to go around 170 degrees, we can not get it to go the full 180 degrees. The timing 1.50ms centers the servo so from the center we measured the degrees at each timing period.
Cutting the holes to fit the servos into was a bit of a challenge. We used a dremmel to cut the approximate width and lenth of the servos into the top of the housing and the arm then used a file to finish with a nice snug fit. We decided to keep the screw holes on the top rather than beneath the plastic (see pictures in housing). One of the screw holes on our servo broke so the bottom servo only has 3 screws keeping it in place.
A few tips:
1. When buying servos make sure that whichever ones you choose will be adequate for your project. Using servos that are do not have enough torque is a bad idea. To avoid this start by estimating how much the arm and gun will weigh and shoot for a servo that has torque about twice the weight you are trying to move.
2. The servos are sold by torque so if you are moving something heavy and want to do so quickly then those servos will be pretty expensive. To save money try to keep the arm and gun as light as possible, we did this by removing the excess plastic from the gun and using only the necessary parts.
3. Broken servos are not fun, keep your servos safe.
We bought a few P-Mods (Peripheral Modules) that will add quite a bit to the turret project. Peripheral Modules are extensions that can be added to the FPGA's 6 pin inputs along the side. They add more room and more capabilities to the board, for instance the servo connector allows for supplying external power to the servos as well as controlling up to four servos, freeing up the other 3 -6 pin inputs for other uses. Below are descriptions of the 3 P-Mods we will be using for this project.
The first is a R/C Servo Connector, capable of connecting 4 servos that can be powered by either an outside power source or by the Board. This addition will help free up some more pins for other uses as well as supply a steady amount of power to the servos.
(Note: the sample program for this p-mod is very helpful, especially if you are trying to figure out how to control both servos seperately or incorporating the 7-segment display)
The second is a an Open collector output board. We will be using this to control the relay that will fire the airsoft gun. It is a basic relay that will complete the circut for the firing mechanism (located in the gun, see "Airsoft Gun"age).
The third is a PS2 - Keyboard/mouse connector. This will allow us to connect a keyboard to the board and eventually control the turret using the arrowkeys and spacebar.
"Power is my mistress. I have worked too hard at her conquest to allow anyone to take her away from me." - Napoleon Bonaparte
The turret has two different power sources. The first is a pack of 4 rechargeable 1.2V batteries with a diode attached which results in 5.01V which is used to power the Nexys FPGA board. The board can take power from either a battery pack or from a USB connection. Each set of 6 pin expansion connectors receives 3.3V or 5V depending on the position of the jumper.
The second power source is used for our servos. This suply consists of a 4 pack of 1.5V batteries supplying 5.50V to the servos.
The R/C servo controller is supplied with 5.50V, in order to use this external power the jumper on the P-Mod must be changed from "VCC" to "VE", this link will show the schematics.
When buying and using batteries in your project keep in mind that new batteries will have slightly more voltage than they claim. We suggest using rechargeable batteries because they will save you money and are easier to control. Rechargeables have slightly less voltage than what they claim to have, so they are generally easier to use and pose a smaller chance of ruining your devices. Whichever type of batteries you use, make sure to use a voltmeter to check it before using it, keep those boards safe!
The Keyboard interface is arguably the toughest aspect of the project, however it is also a valuable tool for controlling robots. We use a PS/2 connection to communicate.
A PS/2 connection is an older way of communicating between computers and keyboards. Today most keyboards use USB but the PS/2 connection can still be found. It consists of 6 lines/wires, one for Data, one for Ground, one for power, and one for a clock, the other two are not used. Clock and data lines are bi-directional, both the keyboard and host can use them.
Using a keyboard with a PS/2 connection along with the PS/2 P-Mod, we are able to control the turret's movement. When a key is hit on a keyboard a "message" is sent to the computer or other host (in our case the FPGA). This "message" is known as a Scancode and consists of two letters or numbers, for instance, the letter "A" sends a scancode of "1C" to the computer. The scancode of "1C" is sent by a signal of 1's and 0's; 1 is considered "high" and a 0 is considered "low". The alternating high and low signal is read by the computer and translated into letters and numbers. Upon recieving the "1C" scancode from the keyboard, the computer will take that scancode and will print it to the screen or use it for whatever function is needed, in our case the letter "A" will move the turret down. This is all done extremely fast, and therefore timing will be extremely important.
PS/2 connections are bi-directional, meaning the keyboard can communicate to the host and the host can communicate with the keyboard. So when does the host device know when to recieve data or send data? This is done by the combination of a clock and an "initiation" bit. Because both need to communicate with each other, both need to be running off a clock that is going at the same speed (more on this later). The keyboard can only send data when the clock signal is "high". So if the host wants to send information it will pull the clock to a "low" position, inhibiting the ability of the keyboard to send data. The keyboard will write to the Data line when the clock signal is high and the host will read this data when the clock is low.
The host and keyboard must also be on the same page when it comes to receiving and transmitting information. The keyboard sends a total of 11 bits, 8 of those bits makes up the scancode. The first is the "initiation" bit, basically the keyboard sends a "low" signal to initiate the transfer of data, this is followed by the 8 bit scancode and 1 parity bit. The parity bit is a way to catch mistakes. What it does is makes the amount of 1's odd, so if there are an even amount of 1's in the scancode the parity bit will be a 1 to make an odd number of 1's. If the number in the scancode is odd, the parrity bit will be a 0, keeping the amount of 1's odd. As odd as this seems, the role of the parity bit is very important. If the host recieves an even number of 1's it will know that the code was a mistake, so the host will request for the scancode to be resent, this will ensure that no errors occur resulting in the loss of data. It will fix mistakes and make the communication run much smoother. The 11th bit that is sent to the host is the "end" bit, this is always a 1 and tells the host that it is done sending data. The host will then send one bit back to the keyboard acknowledging that it recieved the information.
For a more in depth look at the procedure take a look at this site: http://www.computer-engineering.org/ps2protocol/
This code is not mine, it is the code that comes from Digilent Inc. It serves as a great example of how the interfacing of a keyboard appears in VHDL. I suggest reading both the website and the VHDL to better understand this topic.
Society of Robots: http://www.societyofrobots.com/
Journals on how to build turrets:
Other turrets people have built:
"Never trust anything that can think for itself if you can't see where it keeps its brain." -J.K. Rowling
The housing was created using a thick high quality clear plastic, courtesy of the high school plastics class. The dimensions can be seen in the below picture. Our goal was to give enough room inside the housing to hold the servo, battery, FPGA, and possibly some LEDs for aesthetic appeal. Note that the lid of the box has a red rim around it, this is red grip tape that we used in order to give it a snug fit.
(Insert pictures here)