Difference between revisions of "Vision-based Cannon"

From Mech
Jump to navigationJump to search
 
(49 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==Team Members==
==Team Members==

[[Image:SM_UsAndProject.jpg|right|Tony Franco and Scott Mueller|thumb|400px]]


* Scott Mueller (Mechanical Engineering, Class of 2010)
* Scott Mueller (Mechanical Engineering, Class of 2010)
* Tony Franco (Mechanical Engineering, Class of 2009)
* Tony Franco (Mechanical Engineering, Class of 2009)


<br clear=all>


==Overview==
==Overview==




The goal of this project was to use computer vision algorithms to identify targets and direct a Nerf gun to fire at them.
The goal of this project was to use computer vision algorithms to identify targets and direct a Nerf gun to fire at them. The vision portion of the project utilized a web cam and Matlab's image acquisition and processing toolboxes to find and classify objects of interest. The Nerf gun was actuated using a pan & tilt assembly made from modified hobby servos using the PIC prototyping board as a dedicated PID servo controller.
<br clear=all>
Watch A Video of the System in Action!
<br clear=all>
[http://www.youtube.com/watch?v=RsXYjYz_vM4| Servo Calibration Video]
<br clear=all>
[http://www.youtube.com/watch?v=LRAXirfj4Uk| Target Shooting Video]


==Vision Overview==
==Vision System==
The vision system was implemented in Matlab to take advantage of their image acquisition and image processing toolboxes.
The vision system was implemented in Matlab to take advantage of their image acquisition and image processing toolboxes.


==Target Extraction==
===Target Extraction===


Targets were cut from colored foam and attached to a black background. Raw images were captured in RGB format, which takes the form of an [m,n,3] matrix. [http://en.wikipedia.org/wiki/RGB_color_model]. While targets could be identified in such an image, it is much faster to work with binary matrices. [http://en.wikipedia.org/wiki/(0,1)-matrix] To do this some scalar threshold is needed, such that any pixel in the raw image having a value greater than the threshold becomes 1 in the binary image and anything less becomes 0. Rather than a static threshold which would be affected by room lighting, a dynamic threshold based on the median image brightness is used.
Targets were cut from colored foam and attached to a black background. Raw images were captured in RGB format, which takes the form of an [m,n,3] matrix. [http://en.wikipedia.org/wiki/RGB_color_model]. While targets could be identified in such an image, it is much faster to work with binary matrices. [http://en.wikipedia.org/wiki/(0,1)-matrix] To do this some scalar threshold is needed, such that any pixel in the raw image having a value greater than the threshold becomes 1 in the binary image and anything less becomes 0. Rather than a static threshold which would be affected by room lighting, a dynamic threshold based on the median image brightness is used.
Line 56: Line 67:
<br clear=all>
<br clear=all>


==Servo Calibration==
===Servo Calibration===


Since the camera and the Nerf gun are not coaxial a method of mapping servo angles to pixel positions in the image needs to be implemented. This is done by removing all targets from the background so that a laser on the pan/tilt assembly is easily visible. By moving the servos through various angles and finding the centroid of the laser on the background a lookup table can be created. Interpolation can then be used to determine the appropriate servo angles for a given pixel position of a target.
Since the camera and the Nerf gun are not coaxial a method of mapping servo angles to pixel positions in the image needs to be implemented. This is done by removing all targets from the background so that a laser on the pan/tilt assembly is easily visible. By moving the servos through various angles and finding the centroid of the laser on the background a lookup table can be created. Interpolation can then be used to determine the appropriate servo angles for a given pixel position of a target.
Line 83: Line 94:
<br clear=all>
<br clear=all>


===Matlab Code===


For brevity the full code is not shown here. For file descriptions and comments see files.
==Matlab Code==

[[media:SM_Main.m|Main Program]]
[[media:SM_ServoCalibration.m|Servo Calibration]]
*[[media:SM_Main.m|Main Program]]
[[media:SM_Classify.m|Shape Classification]]
*[[media:SM_ServoCalibration.m|Servo Calibration]]
[[media:SM_Fire.m|Fire The Nerf Gun]]
*[[media:SM_GetShape.m|Shape Classification]]
[[media:SM_PanTilt.m|Send Pan/Tilt Servo Positions]]
*[[media:SM_Fire.m|Fire The Nerf Gun]]
[[media:SM_Tx.m|Serial Ouput]]
*[[media:SM_PanTilt.m|Send Pan/Tilt Servo Positions]]
*[[media:SM_Tx.m|Serial Ouput]]


==Electronics==
==Electronics==
The electrical system for this project consisted of a custom developed master/slave servo controller. Each actuator is controlled directly by a slave system that monitors a feedback potentiometer and implements PID control. Each slave in turn receives a position command from a master PIC, which itself is controlled by Matlab running on a PC.
The electrical system for this project consisted of a custom developed master/slave servo controller. Each actuator is controlled directly by a slave system that monitors a feedback potentiometer and implements PID control. Each slave in turn receives a position command from a master PIC, which itself is controlled by Matlab running on a PC.
===Circuits===


[[Image:MasterSlave Motor Control.png|Servo Drive and Control Circuit|thumb|450px|right]]
==Master PIC==
*This is the main servo PWM generation and controller circuit with the Master and two Slaves


<br clear=all>
==Slave PIC==


[[Image:Fire Control.png|Nerf Gun Firing Circuit|thumb|450px|right]]
==Mechanics==
*This is the Nerf rocket Fire Control circuit


<br clear=all>
The nerf gun used was a [http://www.dreamcheeky.com/index.php?pagename=product&pid=1: Dream Cheeky USB Missile Launcher] which we modified to fit our own actuators into. The actuators were [http://www.futaba-rc.com/servos/specs-lineart/specs-futm0004.html: Futaba 3004 Servo]. We removed the control circuitry and wired the potentiometer and motor to the slave PIC controllers. Then we fitted the servos into the Missile Launcher housing. The gun firing mechanism consisted of a spring actuated piston which was driven by a sector gear. A microswitch was tripped after the gun fired and the piston returned to the relaxed position. We simply used a transistor to turn the motor on and off with a PIC and used the input from the microswitch to trigger an external interrupt to shut the motor off.


[[Image:SM_PIC_Boards.jpg|Master, 2 Slaves, Fire Control PICs|thumb|450px|right]]
*These are all four PICs used for controlling the project. One Master, two Slaves, and the Fire Control


[[Image:SM_Target.jpg|Targets]]
[[Image:SM_Gun_Camera_PIC_Setup.JPG]]




<br clear=all>

===Master PIC===

[[Image:Master PWM Generator.png|PWM Generator|thumb|400px|right]]

*[[Media:Master Servo PWM Generator.c|Code for Master servo PWM generator and interfacing with Matlab]]

The Master PIC is in charge of getting the servo positions via RS-232 communication with Matlab and converting them into PWM signals suitable for driving off the shelf servo motors. In the case of this project the servo pulse widths are fed into our custom PIC servo driver or "Slave" PIC's.

<br clear=all>



===Slave PIC===

[[Image:PIC Servo Controller.png|Servo Control Circuit|thumb|600px|right]]

*[[PIC Servo Controller|Main page about the PIC Servo controller]]

*[[Media:Slave Servo PID Controller.c|Code for Slave servo PID controller]]

*[[RC Servo Theory]]




The Slave PIC is meant to interpret servo PWM signals and implement a Proportional-Integral-Derivative (PID) algorithm to drive a servo motor to its intended position. The incoming PWM signal comes in on CCP1, and the outgoing PWM signal goes out on CCP2 and C0 to an L298 H-Bridge chip which gives full bidirectional motor control. Because there are only two CCP pins available on the PIC18F4520, each servo motor must have its own dedicated slave PIC for driving.


<br clear=all>


===Fire Control PIC===

*[[Media:SM_Fire.c|Fire Control PIC Code]]

The fire control PIC allows for firing the missile launcher from Matlab. A USB-FTDI cable is used to provide connectivity. The PIC will fire on any serial input.

==Mechanics==

The nerf gun used was a [http://www.dreamcheeky.com/index.php?pagename=product&pid=1: Dream Cheeky USB Missile Launcher] which we modified to fit our own actuators into. The actuators were [http://www.futaba-rc.com/servos/specs-lineart/specs-futm0004.html: Futaba 3004 Servo]. We removed the control circuitry and wired the potentiometer and motor to the slave PIC controllers. Then we fitted the servos into the Missile Launcher housing. The gun firing mechanism consisted of a spring actuated piston which was driven by a sector gear. A microswitch was tripped after the gun fired and the piston returned to the relaxed position. We simply used a transistor to turn the motor on and off with a PIC and used the input from the microswitch to trigger an external interrupt to shut the motor off.


[[Image:SM_Target.jpg|Targets|thumb|450px]]
<br clear=all>
[[Image:SM_Gun_Camera_PIC_Setup.JPG|Camera Gun and PIC Assembly|thumb|450px]]
<br clear=all>
[[Image:SM_PanTilt_Open.jpg|The pan/tilt assembly opened|thumb|450px]]
<br clear=all>
<br clear=all>


Line 118: Line 183:
==Future Work==
==Future Work==


A more precisely made pan/tilt mechanism with more precise potentiometers would have improved accuracy/repeatability of the system. Lens distortion could be compensated for by applying some sort of camera calibration routine to map pixels to a plane rather than the spherical surface of the lens.


==References==
==References==


*[http://en.wikipedia.org/wiki/Computer_vision/: Computer Vision]
[http://www.mathworks.com/products/image/: Matlab Image Processing Toolbox]
*[http://www.mathworks.com/products/image/: Matlab Image Processing Toolbox]
*[http://en.wikipedia.org/wiki/Servo_drive/: Servo Drive]
*[http://en.wikipedia.org/wiki/Pulse-width_modulation/: Pulse Width Modulation(PWM)]

Latest revision as of 05:05, 20 March 2009

Team Members

Tony Franco and Scott Mueller
  • Scott Mueller (Mechanical Engineering, Class of 2010)
  • Tony Franco (Mechanical Engineering, Class of 2009)



Overview

The goal of this project was to use computer vision algorithms to identify targets and direct a Nerf gun to fire at them. The vision portion of the project utilized a web cam and Matlab's image acquisition and processing toolboxes to find and classify objects of interest. The Nerf gun was actuated using a pan & tilt assembly made from modified hobby servos using the PIC prototyping board as a dedicated PID servo controller.
Watch A Video of the System in Action!
Servo Calibration Video
Target Shooting Video

Vision System

The vision system was implemented in Matlab to take advantage of their image acquisition and image processing toolboxes.

Target Extraction

Targets were cut from colored foam and attached to a black background. Raw images were captured in RGB format, which takes the form of an [m,n,3] matrix. [1]. While targets could be identified in such an image, it is much faster to work with binary matrices. [2] To do this some scalar threshold is needed, such that any pixel in the raw image having a value greater than the threshold becomes 1 in the binary image and anything less becomes 0. Rather than a static threshold which would be affected by room lighting, a dynamic threshold based on the median image brightness is used.

Raw Image


  • capture raw image


Selecting ROI


  • select an ROI (region of interest) and turn any pixels outside of it off


Binary Image


  • create binary image based on threshold


  • create a label matrix (identifies regions of interconnected pixels)
  • remove small objects (pixel threshold)


Binary Mask of Object In Image


  • create list of targets: fill data structure with pertinent target properties (area, centroid, perimeter,shape,color,binary mask)



Shape and Color Classified Objects


  • classify target shapes and colors


Servo Calibration

Since the camera and the Nerf gun are not coaxial a method of mapping servo angles to pixel positions in the image needs to be implemented. This is done by removing all targets from the background so that a laser on the pan/tilt assembly is easily visible. By moving the servos through various angles and finding the centroid of the laser on the background a lookup table can be created. Interpolation can then be used to determine the appropriate servo angles for a given pixel position of a target.

Servo Calibration: Raw Image


  • This image is the raw image of the black background with some extraneous background. The laser is the white dot in the lower left hand corner.



Servo Calibration: Selecting ROI


  • The ROI (region of interest) has been selected over the black background only.


Servo Calibration: Laser Identified


  • Beginning the servo calibration routine, the centroid of the laser is found and marked with a blue '*'.


Matlab Code

For brevity the full code is not shown here. For file descriptions and comments see files.

Electronics

The electrical system for this project consisted of a custom developed master/slave servo controller. Each actuator is controlled directly by a slave system that monitors a feedback potentiometer and implements PID control. Each slave in turn receives a position command from a master PIC, which itself is controlled by Matlab running on a PC.

Circuits

Servo Drive and Control Circuit
  • This is the main servo PWM generation and controller circuit with the Master and two Slaves


Nerf Gun Firing Circuit
  • This is the Nerf rocket Fire Control circuit


Master, 2 Slaves, Fire Control PICs
  • These are all four PICs used for controlling the project. One Master, two Slaves, and the Fire Control



Master PIC

PWM Generator

The Master PIC is in charge of getting the servo positions via RS-232 communication with Matlab and converting them into PWM signals suitable for driving off the shelf servo motors. In the case of this project the servo pulse widths are fed into our custom PIC servo driver or "Slave" PIC's.



Slave PIC

Servo Control Circuit



The Slave PIC is meant to interpret servo PWM signals and implement a Proportional-Integral-Derivative (PID) algorithm to drive a servo motor to its intended position. The incoming PWM signal comes in on CCP1, and the outgoing PWM signal goes out on CCP2 and C0 to an L298 H-Bridge chip which gives full bidirectional motor control. Because there are only two CCP pins available on the PIC18F4520, each servo motor must have its own dedicated slave PIC for driving.




Fire Control PIC

The fire control PIC allows for firing the missile launcher from Matlab. A USB-FTDI cable is used to provide connectivity. The PIC will fire on any serial input.

Mechanics

The nerf gun used was a Dream Cheeky USB Missile Launcher which we modified to fit our own actuators into. The actuators were Futaba 3004 Servo. We removed the control circuitry and wired the potentiometer and motor to the slave PIC controllers. Then we fitted the servos into the Missile Launcher housing. The gun firing mechanism consisted of a spring actuated piston which was driven by a sector gear. A microswitch was tripped after the gun fired and the piston returned to the relaxed position. We simply used a transistor to turn the motor on and off with a PIC and used the input from the microswitch to trigger an external interrupt to shut the motor off.


Targets


Camera Gun and PIC Assembly


The pan/tilt assembly opened


Results

The vision algorithms themselves worked very well. We were able to extract targets from the background and identify their centroid, shape and color and store them in a data structure as a list of targets. However as targets got farther away from the center of the image the shape got distorted by the curvature of the camera lens, throwing off the area/perimeter ratio used to determine shape. The servo calibration routine also worked well and the interpolated servo angles closely matched the actual pixel center of the targets. We had some problems getting the PID coefficients on the slave PICs adjusted properly which resulted in some error in the servo positioned which in turn was reflected in the servo calibration routine.

The master/slave PIC arrangement worked very well and the slave PICs were able to measure the incoming signal pulsewidth +- 1us. Communication between the master PIC and Matlab also worked well. Unfortunately the function to fire the nerf gun from Matlab did not work, for reasons unknown. The PIC function to fire the gun via a push button worked fine, but calling that routine when it received serial data did not. We tried different PICs, different baud rates (19200 and 9600 bps) and different USB-FTDI cables with no success. For our demonstration we manually fired the gun with the push button.

Future Work

A more precisely made pan/tilt mechanism with more precise potentiometers would have improved accuracy/repeatability of the system. Lens distortion could be compensated for by applying some sort of camera calibration routine to map pixels to a plane rather than the spherical surface of the lens.

References