Target Shooting Robot ECE395 Spring 2008 Final Report Tristan Griffith - Matt Davis – Terry Chen Abstract Our goal this semester was to improve our previous design of a robot capable of competing in the 2008 Defconbot competition held in Las Vegas as part of Defcon 16: August 8-10th. Visit www.defconbots.org for contest information. Essentially, the goal is to shoot down some number of round targets of a specific color, ranging in diameter from 1” to 3” in a 5’x5’ arena from 10’ away in under 2 minutes. Other than that, there are only a few other stipulations: your design must be completely autonomous, it must shoot something safe (like airsoft bbs), and it must have a physical “kill switch” to stop it in case it malfunctions. Basic Design Our design that consists of a gun mount to move the gun around, a circuit board to control the movement and firing of the gun, and a PC with webcam to do image processing on the target arena. The following diagram illustrates the dataflow: US RS 23 2 CPU (Image Processing) B Control Circuit Tilt Stepper Motor Camera Pan Stepper Motor oto ns Robotic Gun Assembly Ph Trigger Servo BB s Target Arena Robot Control The portion of the project that controls the aim and firing of the gun consists of a gun mount capable of rotating around two axes allowing the gun to pan (left and right) and tilt (up and down). This motion is generated via two high-torque hobby servos controlled by a microcontroller. There is a third servo that controls the firing of the gun. There is also a software component on the PC that communicates with the microcontroller and allows the image processing portion of the software to control the aim and firing of the gun. Hardware The hardware consists of a servo controller circuit, the servos themselves, and a gun mount. The servo controller and servos were all run off +5V from a Rosewill RV350 power supply. We decided to use a PSU because it’s more portable than the any of the lab power supplies. Servo Controller Parts List Part Microchip PIC18F458 Maxim MAX232N Quantity 1 1 The PIC hardware is fairly straight-forward. The MAX232 chip allows the PC and PIC to communicate via serial port. The PIC simply outputs the proper PWM signals for the three servos. Servos Parts List Part Hitec HS-805BB Servo (180 degree rotation) Quantity 3 These servos (and all analog Hitec servos, as far as I know) take a PWM signal with a period of 20ms. The width of the pulse itself ranges from 0.9ms to 2.1ms, where each pulse width corresponds to the two most extreme positions of the servo and 1.5ms corresponds to the center position (see below). 0ms 0.9ms 2.1ms 20ms Note that these are theoretical values. The 20ms period is ok, but we found that our servos had slightly different pulse width values that corresponded to their extreme positions. One servo worked from 0.6ms to 2.3ms, one servo worked from 1ms to 19ms, and the third from 0.9ms to 19ms. It’s best to generate a PWM signal on a waveform generator, output it to the servo, then adjust the pulse width to determine which pulse width values work best for that particular servo. The PWM signal is output on the yellow (sometimes white) lead of the servo. Red and black correspond to V+ and ground, respectively. The servos we used could handle a V+ from 4.8V to 6V. Note that these servos are high-torque and can pull a fair amount of current so they need to be isolated from your logic circuitry. Software The software behind the robot control consists of PIC software that generates PWM signals for the servos and communicates with the PC plus PC software that communicates with the PIC and provides an API allowing any c/c++ programmer to control the servos. A general overview of the software is given here, for more details take a look at the actual source code. PIC In order to generate the PWM signals, the PIC code uses 3 timers, two of which are setup to run the CCPs in compare mode—one for the pan servo and the other for the tilt servo. By running the two CCP timers in 16-bit mode with a pre-scalar of 1/1 we are able to have a pulse width resolution of around 0.0001ms. In other words, we are able to change the pulse width of the PWM signal by only 0.0001ms. This allows for very fine control of the servos and ultimately very precise aim of the gun. The non-CCP timer is used to enforce the 20ms period as well as control the position of the trigger servo. The trigger servo only requires two positions, so a fancy CCP is not needed for precision. Communication with the PC is achieved with a protocol defined in the code. Basically, a byte header is sent from the PC to the PIC that describes the type of request (tilt, get the position of the pan servo, trigger on, etc.), which is acknowledged by the PIC by sending a byte (0xAC) back. Depending on the request, there might be more information exchanged, but regardless, the PIC always acknowledges any communication from the PC. PC The PC portion of the code is encapsulated in a class called RoboControl. It opens up a communications port (most commonly “com1”, but basically any sort of serial port) and allows the user to control the servos by specifying angles in degrees. The PC calculates raw servo positions from the specified angles and sends it to the PIC. By off-loading the angle to position calculations on the PC, we are able to keep our PIC code simple. The class also has the ability to trigger the gun, read the current position of the tilt and pan servos, and trim the tilt and pan servos (basically allow the user to line up 90 degrees with their definition of “center”). Image Processing The image processing component of this project uses a computer with webcam. Specifically we wrote the software in c++ because of speed and existence of an image processing library OpenCV (http://sourceforge.net/projects/opencvlibrary/) which allows easy interfacing with webcams. We transitioned from using an older Creative webcam to using a Logitech QuickCam Pro 9000. The major advantages of this camera are the zoom lens, high resolution, and sophistication of camera configuration via the packaged Logitech software. The general idea is we identify the target locations from the webcam image and determine the guns position based on the location of a laser beam mounted as a gun scope. Software Usage Once the entire system is assembled and powered on, launching the software will connect to the PIC circuit via serial port as well as acquire image data from the web cam. Image data will be displayed in a window and the cursor will resemble a cross hair. Clicking on any region of the image will store that pixels color data to a buffer. The following keystrokes are controls: T(t) – Save buffered RGB value as the color of targets. L(l) – Save buffered RGB value as the color of the laser. A(a) – Begin automation of target shooting. S(s) – Stop automation of target shooting. Esc – Exit software Algorithm TargetBot.cpp is the entry point for the console application and is what sets up the global event loop. This file defines two constant values (how many frames to skip between processing and the number of pixels to skip over in scanning a single frame) which can be adjusted for changes in requirements. The main event loop passes frames into FindTarget which is the class responsible for locating targets, locating gun positioning, and moving/firing the gun via the libroboctrl discussed earlier in this document. FindTargets also has constants defined at the top for fine tuning algorithm operation. The FindTarget event loop (defined in executeAI) operates as follows: 1. _scanTargetSpace produces a reduced 2 dimensional array where 0 represents background and 1 represents target color (based on a threshold calculation). 2. The previous target list is cleared, and the reduced array is scanned (via _findTargets) populating a list of target locations. 3. Once target locations exist in the list, the event loop populates a list of possible laser pointer locations via _findLaserPtr and then determines the laser pointer coordinate via _getLaserCoord which computes percent error between the anticipated coordinate (based on tracking gun movements) and the list populated by _findLaserPtr. 4. Finally _performAction is called which will calculates how to move the gun, when to fire and sends the approprated commands. Target Arena The target arena is built out of 2’x4’s and is painted black. It is roughly 5’x5’ and has 3 shelves for targets, spaced roughly 1’ apart vertically. The targets are made out of poster board, and taped to hinges so that they can fall backwards. Results & Future The gun turret is completely rigid and offers a fine level of precision. The control circuit board provides perfect reliability in controlling the turret. The software is more dynamic than the past with some level of dynamic color selection available and a webcam that offers fine tuning of image lighting and zoom. Next steps before the competition are completing an auto calibration routine to reduce the time required to set up a working demo.