You are on page 1of 15

ROBOT

“TORRETA DE CONTROL”
Barrientos Romero Juan Gabriel

Resumen.- En el presente proyecto se observará el prototipo de un torreta de control, su objetivo es el de


detectar objetivos y neutralizarlos.

Índice de Términos— torreta de contro .

1. Objetivo General 4. Diseño y materiales.

Realizar un prototipo de una torreta de control. Para desarrollar este robot se usó el prototipo rubber
band Centry Gum. El diseño de la torreta de control se
1.2 Objetivos Específicos muestra en la siguiente figura

 Realizar el diseño mecánico para la Torreta


de control.
 Controlar el robot con arduino y guiado por
una cámara.

2. Fundamento Teórico

Las guerras desde su antigüedad han involucionado FIGURA 1. Diseño de las piezas en el software
de manera exponencial desde su inicio, con la SolidWorks
finalidad de velar los intereses geopolíticos que se
suscitan. 4.1. Materiales

La tecnología militar ha sido desarrollada para Se utilizaron los siguientes materiales


revolucionar muchas ingenierías, que hoy en dia
aplicamos de manera cotidiana.
La tecnología nos permite preservar la integridad
del ser humano para tal efecto, ha sido diseñado este
proyecto donde las guerras han dejado de ser
directas, es decir no convencionales.
Para tal efecto es importante que nuestras fuerzas
armadas cuentes con este tipo de prototipo para su
ejecución.
3. Procedimiento
Para el desarrollo del proyecto se diseño en
solidworks.
Para la parte electrónica usamos servomotores de
360 grados para controlar el eje X y el eje Y un
nmotor de 180 grados.
Para el control usamos la placa arduino y el
software processing de inteligencia artificial.
- Arduino uno //
<=====================================
5. Código de Arduino (Sketch) ====================================>

// servo positions:
#define panServo_scanningMin 0 // how int panServoPin; // Arduino pin for pan
far side to side you want the servo
#define panServo_scanningMax 180 // gun int tiltServoPin; // Arduino pin for tilt
to turn while 'scanning' servo
#define scanningSpeed 2000 // total time int triggerServoPin; // Arduino pin for
for 1 sweep (in milliseconds) trigger servo, or output to trigger MOSFET
int firingIndicatorLEDPin; // Arduino pin for
#define panServo_HomePosition 97 // firing indicator LED
'centered' gun position int USBIndicatorLEDPin; // Arduino pin for
#define tiltServo_HomePosition 65 // USB indicator LED
int modeIndicatorLEDPin; // Arduino pin for
#define panServo_ReloadPosition 97 // Mode indicator LED
convenient position for reloading gun int reloadSwitchPin; // Arduino pin for input
#define tiltServo_ReloadPosition 150 // from RELOAD switch
int disablePlatePin; // Arduino pin for input
#define triggerServo_HomePosition 120 // from disable plate
trigger servo not-firing position int electricTriggerPin; // Arduino pin for
#define triggerServo_SqueezedPosition 90 // output to trigger MOSFET
trigger servo firing position boolean invertInputs; // TRUE turns on
internal pull-ups, use if closed switch connects arduino
// more trigger settings: pin to ground
#define triggerTravelMillis 1500 // how // pin assignments for each hardware setup are set in the
often should trigger be squeezed (in semi-auto function assignPins() at bottom of code
firing)
// higher value = slower firing, lower value = faster typedef struct config_t
firing {
// Booleans but int
// disable plate settings: int controlMode;
#define disablePlateDelay 5000 // how int safety;
long to disable sentry when plate is pressed (in int firingMode;
milliseconds) int scanWhenIdle;
int trackingMotion;
// ammunition magazine/clip settings: int trackingColor;
boolean useAmmoCounter = false; // if int leadTarget;
you want to use the shot counter / clip size feature, int safeColor;
set this to true int showRestrictedZones;
int clipSize = 5; // how many shots int showDifferentPixels;
before the gun will be empty and the gun will be int showTargetBox;
disabled (reload switch resets the ammo counter) int showCameraView;
int mirrorCam;
// int soundEffects;
<==================================
=================================== // Integers
====> int camWidth;
// End custom values int camHeight;
int nbDot; boolean idle = true;
int antSens; boolean scanning = false;
int minBlobArea; boolean scanDirection = true;
int tolerance; boolean disabled = false;
int effect; unsigned long int disableEndTime;
int trackColorTolerance; int scanXPosition = panServo_scanningMin;
int trackColorRed;
int trackColorGreen; int shotCounter = 0; // number of shots fires
int trackColorBlue; since last reload
int safeColorMinSize; boolean clipEmpty = false; // is the ammo
int safeColorTolerance; magazine empty?
int safeColorRed;
int safeColorGreen;
int safeColorBlue; byte indicator; // if 'a', continue, if 'z', idle
int idleTime; byte x100byte; // some bytes used during
serial communication
// Floats byte x010byte;
double propX; byte x001byte;
double propY; byte y100byte;
double xRatio; byte y010byte;
double yRatio; byte y001byte;
double xMin; byte fireByte;
double xMax; byte fireSelectorByte;
double yMin; byte scanningByte;
double yMax;
void setup(){
} assignPins();
configuration;
configuration configuration1; pan.attach(panServoPin); // set up the x axis
servo
#include <Servo.h> pan.write(panServo_HomePosition);
tilt.attach(tiltServoPin); // set up the y axis servo
Servo pan; // x axis servo tilt.write(tiltServo_HomePosition);
Servo tilt; // y axis servo pinMode(electricTriggerPin, OUTPUT); // electric
Servo trigger; // trigger servo trigger, set as output
digitalWrite(electricTriggerPin, LOW);
int xPosition; // pan position trigger.attach(triggerServoPin); // servo for trigger,
int yPosition; // tilt position set that servo up
int fire = 0; // if 1, fire; else, don't trigger.write(triggerServo_HomePosition);
fire
pinMode(USBIndicatorLEDPin, OUTPUT); // set
int fireTimer = 0; up USB indicator LED
int fireSelector = 1; // 1 - semi-automatic pinMode(modeIndicatorLEDPin, OUTPUT); // set
firing, auto/semi-auto gun up Mode indicator LED
pinMode(firingIndicatorLEDPin, OUTPUT); // set
// 3 - full automatic firing, full-auto gun up firing indicator LED
pinMode(reloadSwitchPin, INPUT); // set up
int idleCounter = 0; reload switch input
int watchdog = 0; pinMode(disablePlatePin, INPUT); // set up
int watchdogTimeout = 2000; disable plate input
if(invertInputs) {
digitalWrite(reloadSwitchPin, HIGH); // }
turn on internal pull-up else if(indicator == 'r'){ // start restore
digitalWrite(disablePlatePin, HIGH); //
turn on internal pull-up restore();
}
Serial.begin(4800); // start }
communication with computer }
else{
} watchdog++;
if(watchdog > watchdogTimeout) {
void loop() { idle = true;
if (Serial.available() >= 10) { // check to see }
if a new set of commands is available }
watchdog = 0; if(idle) { // when Arduino is not getting
indicator = Serial.read(); // read first byte in commands from computer...
buffer Serial.write('T'); // tell the computer that
if(indicator == 'a') { // check for 'a' Arduino is here
(indicates start of message) idleCounter++; // periodically
idle = false; blink the USB indicator LED
idleCounter = 0; if(idleCounter > 1000) { //
digitalWrite(USBIndicatorLEDPin, HIGH); // sequenceLEDs(1, 100);
light up the USB indicator LED delay(10);
x100byte = Serial.read(); // read the
message, byte by byte // digitalWrite(USBIndicatorLEDPin, HIGH);
x010byte = Serial.read(); // //
x001byte = Serial.read(); // // delay(250); //
y100byte = Serial.read(); // // digitalWrite(USBIndicatorLEDPin, LOW); //
y010byte = Serial.read(); // idleCounter = 0; //
y001byte = Serial.read(); // } //
fireByte = Serial.read(); // else{ //
fireSelectorByte = Serial.read(); // digitalWrite(USBIndicatorLEDPin, LOW); //
fireSelector = int(fireSelectorByte) - 48; // } //
convert byte to integer xPosition = panServo_HomePosition; // keep x axis
scanningByte = Serial.read(); servo in its home position
if((int(scanningByte) - 48) == 1) { yPosition = tiltServo_HomePosition; // keep y axis
scanning = true; servo in its home position
} fire = 0; // don't fire
else{ }
scanning = false; else{ // when Arduino is getting commands
} from the computer...
} xPosition = (100*(int(x100byte)-48)) +
else if(indicator == 'z'){ // check for command (10*(int(x010byte)-48)) + (int(x001byte)-48); //
to go idle (sent by computer when program is decode those message bytes into two 3-digit numbers
ended) yPosition = (100*(int(y100byte)-48)) +
idle = true; (10*(int(y010byte)-48)) + (int(y001byte)-48); //
} fire = int(fireByte) - 48; // convert byte to
else if(indicator == 'b'){ // start backup integer
}
backup();
if(scanning) { digitalWrite(modeIndicatorLEDPin, LOW);
digitalWrite(modeIndicatorLEDPin, HIGH); delay(100);
if(scanDirection) { }
scanXPosition += 1;
if(scanXPosition > panServo_scanningMax) { if(disabled) {
scanDirection = false; xPosition = panServo_ReloadPosition; // if it is
scanXPosition = panServo_scanningMax; flipped, override computer commands,
} yPosition = tiltServo_ReloadPosition; // and send
} the servos to their reload positions
else{ fire = 0; // don't fire while reloading
scanXPosition -= 1; digitalWrite(modeIndicatorLEDPin, HIGH);
if(scanXPosition < panServo_scanningMin) { delay(50);
scanDirection = true; digitalWrite(modeIndicatorLEDPin, LOW);
scanXPosition = panServo_scanningMin; delay(50);
} }
}
xPosition = scanXPosition; pan.write(xPosition); // send the servos to
yPosition = tiltServo_HomePosition; whatever position has been commanded
fire = 0; tilt.write(yPosition); //

delay(scanningSpeed/abs(panServo_scanningMax if(useAmmoCounter && shotCounter >= clipSize) {


-panServo_scanningMin)); clipEmpty = true;
} }
else{ else{
digitalWrite(modeIndicatorLEDPin, LOW); clipEmpty = false;
} }

if((digitalRead(disablePlatePin) == HIGH &&


!invertInputs) || (digitalRead(disablePlatePin) == if(fire == 1 && !clipEmpty) { // if firing...
LOW && invertInputs)) { // check the disable Fire(fireSelector); // fire the gun in whatever
plate to see if it is pressed firing mode is selected
disabled = true; }
disableEndTime = millis() + disablePlateDelay; else{ // if not firing...
} ceaseFire(fireSelector); // stop firing the gun
if(millis() > disableEndTime) { }
disabled = false; }
}
void Fire(int selector) { // function to fire the gun,
if((digitalRead(reloadSwitchPin) == HIGH && based on what firing mode is selected
!invertInputs) || (digitalRead(reloadSwitchPin) == if(selector == 1) {
LOW && invertInputs)) { // check the reload fireTimer++;
switch to see if it is flipped if(fireTimer >=0 && fireTimer <=
shotCounter = 0; triggerTravelMillis) {
xPosition = panServo_ReloadPosition; // if it digitalWrite(electricTriggerPin, HIGH);
is flipped, override computer commands, trigger.write(triggerServo_SqueezedPosition);
yPosition = tiltServo_ReloadPosition; // and digitalWrite(firingIndicatorLEDPin, HIGH);
send the servos to their reload positions }
fire = 0; // don't fire while reloading if(fireTimer > triggerTravelMillis && fireTimer <
digitalWrite(modeIndicatorLEDPin, HIGH); 1.5*triggerTravelMillis) {
delay(100); digitalWrite(electricTriggerPin, LOW);
trigger.write(triggerServo_HomePosition); digitalWrite(USBIndicatorLEDPin, HIGH);
digitalWrite(firingIndicatorLEDPin, LOW); }
} digitalWrite(USBIndicatorLEDPin, LOW);
if(fireTimer >= 1.5*triggerTravelMillis) {
fireTimer = 0; startDelay = millis();
if(useAmmoCounter) { while(millis()-startDelay < delayTime) {
shotCounter++; // increment the shot digitalWrite(modeIndicatorLEDPin, HIGH);
counter }
} digitalWrite(modeIndicatorLEDPin, LOW);
}
} startDelay = millis();
if(selector == 3) { while(millis()-startDelay < delayTime) {
digitalWrite(electricTriggerPin, HIGH); // chill
trigger.write(triggerServo_SqueezedPosition); }
digitalWrite(firingIndicatorLEDPin, HIGH); }
} }
}
void assignPins() {
void ceaseFire(int selector) { // function to stop if(type == "Arduino_bare" || type ==
firing the gun, based on what firing mode is "Arduino_Bare") {
selected // pin attachments:
if(selector == 1) { panServoPin = 8; // Arduino pin for
fireTimer = 0; pan servo
digitalWrite(electricTriggerPin, LOW); tiltServoPin = 9; // Arduino pin for tilt
trigger.write(triggerServo_HomePosition); servo
digitalWrite(firingIndicatorLEDPin, LOW); triggerServoPin = 10; // Arduino pin for
} trigger servo, or output to trigger MOSFET
if(selector == 3) { // for my gun, both firingIndicatorLEDPin = 12; // Arduino pin
firing modes cease firing by simply shutting off. for firing indicator LED
digitalWrite(electricTriggerPin, LOW); USBIndicatorLEDPin = 11; // Arduino pin
trigger.write(triggerServo_HomePosition); for USB indicator LED
digitalWrite(firingIndicatorLEDPin, LOW); modeIndicatorLEDPin = 13; // Arduino pin
} for Mode indicator LED
} reloadSwitchPin = 3; // Arduino pin for
input from RELOAD switch
void sequenceLEDs(int repeats, int delayTime) { disablePlatePin = 2; // Arduino pin for
int startDelay; input from disable plate
for(int i = 0; i < repeats; i++) { electricTriggerPin = 7; // Arduino pin for
output to trigger MOSFET
digitalWrite(USBIndicatorLEDPin, LOW); invertInputs = true; // TRUE turns on
digitalWrite(modeIndicatorLEDPin, LOW); internal pull-ups, use if closed switch connects arduino
pin to ground
startDelay = millis(); }
while(millis()-startDelay < delayTime) { else if(type == "Shield_v4" || type == "Shield_v6") {
digitalWrite(firingIndicatorLEDPin, HIGH); // pin attachments:
} panServoPin = 9; // Arduino pin for
digitalWrite(firingIndicatorLEDPin, LOW); pan servo
tiltServoPin = 8; // Arduino pin for tilt
startDelay = millis(); servo
while(millis()-startDelay < delayTime) {
triggerServoPin = 7; // Arduino pin electricTriggerPin = 7; // Arduino pin for
for trigger servo, or output to trigger MOSFET output to trigger MOSFET
electricTriggerPin = 6; // Arduino pin firingIndicatorLEDPin = 12; // Arduino pin
for output to trigger MOSFET for firing indicator LED
firingIndicatorLEDPin = 11; // Arduino USBIndicatorLEDPin = 14; // Arduino pin
pin for firing indicator LED for USB indicator LED
USBIndicatorLEDPin = 12; // Arduino modeIndicatorLEDPin = 13; // Arduino pin
pin for USB indicator LED for Mode indicator LED
modeIndicatorLEDPin = 13; // Arduino reloadSwitchPin = 11; // Arduino pin for
pin for Mode indicator LED input from RELOAD switch
reloadSwitchPin = 10; // Arduino pin disablePlatePin = 2; // Arduino pin for
for input from RELOAD switch input from disable plate
disablePlatePin = 2; // Arduino pin invertInputs = true; // TRUE turns on
for input from disable plate internal pull-ups, use if closed switch connects arduino
invertInputs = true; // TRUE turns on pin to ground
internal pull-ups, use if closed switch connects }
arduino pin to ground else if(type == "Standalone_v5") {
} // pin attachments:
else if(type == "Shield_v7") { panServoPin = 8; // Arduino pin for
// pin attachments: pan servo
panServoPin = 8; // Arduino pin tiltServoPin = 9; // Arduino pin for tilt
for pan servo servo
tiltServoPin = 9; // Arduino pin for triggerServoPin = 10; // Arduino pin for
tilt servo trigger servo, or output to trigger MOSFET
triggerServoPin = 10; // Arduino pin electricTriggerPin = 7; // Arduino pin for
for trigger servo, or output to trigger MOSFET output to trigger MOSFET
electricTriggerPin = 7; // Arduino pin firingIndicatorLEDPin = 12; // Arduino pin
for output to trigger MOSFET for firing indicator LED
firingIndicatorLEDPin = 12; // Arduino USBIndicatorLEDPin = 14; // Arduino pin
pin for firing indicator LED for USB indicator LED
USBIndicatorLEDPin = 6; // Arduino modeIndicatorLEDPin = 13; // Arduino pin
pin for USB indicator LED for Mode indicator LED
modeIndicatorLEDPin = 13; // Arduino reloadSwitchPin = 11; // Arduino pin for
pin for Mode indicator LED input from RELOAD switch
reloadSwitchPin = 11; // Arduino pin disablePlatePin = 2; // Arduino pin for
for input from RELOAD switch input from disable plate
disablePlatePin = 2; // Arduino pin invertInputs = true; // TRUE turns on
for input from disable plate internal pull-ups, use if closed switch connects arduino
invertInputs = true; // TRUE turns on pin to ground
internal pull-ups, use if closed switch connects }
arduino pin to ground else if(type == "Standalone_v7") {
} // pin attachments:
else if(type == "Standalone_v3") { panServoPin = 8; // Arduino pin for
// pin attachments: pan servo
panServoPin = 8; // Arduino pin tiltServoPin = 9; // Arduino pin for tilt
for pan servo servo
tiltServoPin = 9; // Arduino pin for triggerServoPin = 10; // Arduino pin for
tilt servo trigger servo, or output to trigger MOSFET
triggerServoPin = 10; // Arduino pin electricTriggerPin = 7; // Arduino pin for
for trigger servo, or output to trigger MOSFET output to trigger MOSFET
firingIndicatorLEDPin = 12; // Arduino // End custom values
pin for firing indicator LED //
boolean PRINT_FRAMERATE = false; // set to true to print the
USBIndicatorLEDPin = 14; // Arduino framerate at the bottom of the IDE window
pin for USB indicator LED
modeIndicatorLEDPin = 13; // Arduino int[] diffPixelsColor = {
pin for Mode indicator LED 255, 255, 0
reloadSwitchPin = 11; // Arduino pin }; // Red, green, blue values (0-255) to show pixel as marked as
target
for input from RELOAD switch public int effect = 0; // Effect
disablePlatePin = 2; // Arduino pin
for input from disable plate public boolean mirrorCam = false; // set true to mirror
invertInputs = true; // TRUE turns on camera image
internal pull-ups, use if closed switch connects
public float xMin = 0.0; // Actual calibration values are loaded
arduino pin to ground from "settings.txt".
} public float xMax = 180.0; // If "settings.txt" is borken /
else if(type == "Standalone_v8") { unavailable, these defaults are used instead -
// pin attachments: public float yMin = 0.0; // otherwise, changing these lines will
panServoPin = 8; // Arduino pin have no effect on your gun's calibration.
public float yMax = 180.0; //
for pan servo
tiltServoPin = 9; // Arduino pin for import JMyron.*;
tilt servo import blobDetection.*;
triggerServoPin = 10; // Arduino pin import processing.serial.*;
for trigger servo, or output to trigger MOSFET import ddf.minim.*;
import java.awt.Frame;
electricTriggerPin = 7; // Arduino pin import processing.opengl.*; // see note on OpenGL in
for output to trigger MOSFET void setup()
firingIndicatorLEDPin = 12; // Arduino import procontroll.*;
pin for firing indicator LED import net.java.games.input.*;
USBIndicatorLEDPin = 14; // Arduino
public int minBlobArea = 30; // minimum target size
pin for USB indicator LED (pixels)
modeIndicatorLEDPin = 13; // Arduino public int tolerance = 100; // sensitivity to motion
pin for Mode indicator LED
reloadSwitchPin = 11; // Arduino pin public boolean runWithoutArduino = false;
for input from RELOAD switch public boolean connecting = false;
disablePlatePin = 2; // Arduino pin
for input from disable plate public Serial arduinoPort;
invertInputs = true; // TRUE turns on JMyron camInput;
internal pull-ups, use if closed switch connects BlobDetection target;
arduino pin to ground Blob blob;
Blob biggestBlob;
}
}
int[] Background;
Código PROCESSING int[] rawImage;
int[] rawBackground;
// int[] currFrame;
// Begin custom values - change these camera dimensions int[] screenPixels;
to work with your turret public int targetX = camWidth/2;
public int targetY = camHeight/2;
public int camWidth = 320; // camera width int fire = 0;
(pixels), usually 160*n int[] prevFire = {
public int camHeight = 240; // camera height 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
(pixels), usually 120*n };

// float xRatio;
float yRatio;
int possibleX = camWidth/2; public boolean useInputDevice = false; // use a joystick or game
int possibleY = camHeight/2; controller as input (in manual mode)
public boolean inputDeviceIsSetup = false;
int displayX = camWidth/2;
int displayY = camHeight/2; public ControllIO controlIO; // more stuff for using a joystick
or game controller for input
int oldX = camWidth/2; // smoothing (contributed by Adam public ControllDevice inputDevice;
S.)
int oldY = camHeight/2; // smoothing public ControllButton[] buttons = new ControllButton[30];
int xdiff; // smoothing public ControllSlider[] sliders = new ControllSlider[10];
int ydiff; // smoothing
public float smoothingFactor = 0.8; // smoothing public ControllButton[] fire_buttons = new ControllButton[0];
public boolean activeSmoothing = true; public ControllButton[] preciseAim_buttons = new
ControllButton[0];
String strTargetx; public ControllButton[] centerGun_buttons = new
String strTargety; ControllButton[0];
String fireSelector; public ControllButton[] autoOn_buttons = new
String scanSelector; ControllButton[0];
public ControllButton[] autoOff_buttons = new
public boolean showDifferentPixels = false; ControllButton[0];
public boolean showTargetBox = true; public ControllButton[] inputToggle_buttons = new
public boolean showCameraView = true; ControllButton[0];
public boolean firingMode = true; // true = semi, public ControllButton[] randomSound_buttons = new
false = auto ControllButton[0];
public boolean safety = true;
public boolean controlMode = false; // true = public ControllSlider[] pan_sliders = new ControllSlider[0];
autonomous, false = manual public ControllSlider[] tilt_sliders = new ControllSlider[0];
public boolean soundEffects = false; // set to true to public ControllSlider[] panInvert_sliders = new ControllSlider[0];
enable sound effects by default public ControllSlider[] tiltInvert_sliders = new ControllSlider[0];
public boolean scanWhenIdle = true;
public boolean trackingMotion = true;
public float xPosition = camWidth/2;
int idleTime = 10000; // how many milliseconds to wait public float yPosition = camHeight/2;
until scanning (when in scan mode)
int idleBeginTime = 0;
boolean scan = false; String[] inStringSplit; // buffer for backup
int controlMode_i, safety_i, firingMode_i, scanWhenIdle_i,
public String serPortUsed; trackingMotion_i, trackingColor_i, leadTarget_i, safeColor_i,
showRestrictedZones_i, showDifferentPixels_i,
int[][] fireRestrictedZones = new int[30][4]; showTargetBox_i, showCameraView_i, mirrorCam_i,
int restrictedZone = 1; soundEffects_i;
boolean showRestrictedZones = false;

boolean selectingColor = false; void setup() {


boolean trackingColor = false;
int trackColorTolerance = 100; loadSettings();
int trackColorRed = 255;
int trackColorGreen = 255; size(camWidth, camHeight); // some users have
int trackColorBlue = 255; reported a faster framerate when the code utilizes OpenGL. To try
this, comment out this line and uncomment the line below.
boolean selectingSafeColor = false; // size(camWidth, camHeight, OPENGL);
boolean safeColor = false; minim = new Minim(this);
int safeColorMinSize = 500; loadSounds();
int safeColorTolerance = 100; playSound(18);
int safeColorRed = 0; camInput = new JMyron();
int safeColorGreen = 255; camInput.start(camWidth, camHeight);
int safeColorBlue = 0; camInput.findGlobs(0);
camInput.adaptivity(1.01);
boolean useArrowKeys = false; // use arrow keys to finely camInput.update();
adjust the aiming (in manual mode) currFrame = camInput.image();
rawImage = camInput.image(); scanSelector = str(1);
Background = camInput.image(); }
rawBackground = camInput.image(); else {
screenPixels = camInput.image(); scanSelector = str(0);
target = new BlobDetection(camWidth, camHeight); }
target.setThreshold(0.9); //println('a' + strTargetx + strTargety + str(fire) + fireSelector +
target.setPosDiscrimination(true); scanSelector);
if (!runWithoutArduino && !connecting) {
retryArduinoConnect(); arduinoPort.write('a' + strTargetx + strTargety + str(fire) +
fireSelector + scanSelector); // send to arduino
xRatio = (camWidth / (xMax - xMin)); // }
used to allign sights with crosshairs on PC
yRatio = (camHeight/ (yMax - yMin)); // if ((keyPressed && key == 't') || showRestrictedZones) {
drawControlPanel(); for (int col = 0; col <= restrictedZone; col++) {
} noStroke();
fill(0, 255, 0, 100);
rect(fireRestrictedZones[col][0], fireRestrictedZones[col][2],
fireRestrictedZones[col][1]-fireRestrictedZones[col][0],
void draw() { fireRestrictedZones[col][3]-fireRestrictedZones[col][2]);
if (PRINT_FRAMERATE) { }
println(frameRate); }
} if (selectingColor) {
stroke(190, 0, 190);
if (controlMode) { // autonomous mode strokeWeight(2);
autonomousMode(); // fill(red(currFrame[(mouseY*width)+mouseX]),
} green(currFrame[(mouseY*width)+mouseX]),
else if (!controlMode) { // manual mode blue(currFrame[(mouseY*width)+mouseX]));
manualMode(); // rect(mouseX+2, mouseY+2, 30, 30);
} }

if (fire == 1) { if (selectingSafeColor) {
idleBeginTime = millis(); stroke(0, 255, 0);
scan = false; strokeWeight(2);
} fill(red(currFrame[(mouseY*width)+mouseX]),
else { green(currFrame[(mouseY*width)+mouseX]),
if (millis() > idleBeginTime + idleTime && controlMode blue(currFrame[(mouseY*width)+mouseX]));
&& scanWhenIdle) { rect(mouseX+2, mouseY+2, 30, 30);
scan = true; }
}
else { soundTimer++;
scan = false; if (soundTimer == soundInterval) {
} randomIdleSound();
} soundTimer = 0;
}
if (!safety) {
fire = 0; for (int i = 9; i >= 1; i--) {
} prevFire[i] = prevFire[i-1];
}
strTargetx = "000" + str(targetX); // make into 3- prevFire[0] = fire;
digit numbers int sumNewFire = prevFire[0] + prevFire[1] + prevFire[2] +
strTargetx = strTargetx.substring(strTargetx.length()-3); prevFire[3] + prevFire[4];
strTargety = "000" + str(targetY); int sumPrevFire = prevFire[5] + prevFire[6] + prevFire[7] +
strTargety = strTargety.substring(strTargety.length()-3); prevFire[8] + prevFire[9];
fireSelector = str(0);
if (firingMode) { if (sumNewFire == 0 && sumPrevFire == 5) { // target
fireSelector = str(1); departed screen
} int s = int(random(0, 6));
else { if (s == 0)
fireSelector = str(3); playSound(1);
} if (s == 1)
if (scan) { playSound(5);
if (s == 2) }
playSound(9); else {
if (s == 3) pixels[i] = color(0, 0, 0);
playSound(12); }
if (s == 4)
playSound(13); boolean motion = (((abs(red(currFrame[i])-
if (s == 5) red(Background[i])) + abs(green(currFrame[i])-
playSound(20); green(Background[i])) + abs(blue(currFrame[i])-
} blue(Background[i]))) > (200-tolerance)) && trackingMotion);
boolean isTrackedColor = (((abs(red(currFrame[i])-
if (fire == 1) trackColorRed) + abs(green(currFrame[i])-trackColorGreen) +
strokeWeight(3); abs(blue(currFrame[i])-trackColorBlue)) < trackColorTolerance)
if (fire == 0) && trackingColor);
strokeWeight(1);
stroke(255, 0, 0); //draw crosshairs boolean isSafeColor = (((abs(red(currFrame[i])-safeColorRed)
noFill(); // + abs(green(currFrame[i])-safeColorGreen) +
line(displayX, 0, displayX, camHeight); // abs(blue(currFrame[i])-safeColorBlue)) < safeColorTolerance)
line(0, displayY, camWidth, displayY); // && safeColor);
ellipse(displayX, displayY, 20, 20); //
ellipse(displayX, displayY, 28, 22); // if (motion || isTrackedColor) {
ellipse(displayX, displayY, 36, 24); // screenPixels[i] = color(255, 255, 255);
if (showDifferentPixels) {
updateControlPanels(); if (effect == 0) {
prevTargetX = targetX; pixels[i] = color(diffPixelsColor[0], diffPixelsColor[1],
prevTargetY = targetY; diffPixelsColor[2]);
} }
else if (effect == 1) {
void autonomousMode() { pixels[i] = color((diffPixelsColor[0] + red(currFrame[i]))/2,
if(inputDeviceIsSetup) { (diffPixelsColor[1] + green(currFrame[i]))/2, (diffPixelsColor[2]
checkInputDevice(); + blue(currFrame[i]))/2);
} }
else if (effect == 2) {
if (selectingColor || selectingSafeColor) { pixels[i] = color(255-red(currFrame[i]), 255-
cursor(1); green(currFrame[i]), 255-blue(currFrame[i]));
} }
else { else if (effect == 3) {
cursor(0); pixels[i] = color((diffPixelsColor[0] + (255-
} red(currFrame[i])))/2, (diffPixelsColor[1] + (255-
camInput.update(); green(currFrame[i])))/2, (diffPixelsColor[2] + (255-
rawBackground = camInput.retinaImage(); blue(currFrame[i])))/2);
rawImage = camInput.image(); }
if (mirrorCam) { }
for (int i = 0; i < camWidth*camHeight; i++) { }
int y = floor(i/camWidth); else {
int x = i - (y*camWidth); screenPixels[i] = color(0, 0, 0);
x = camWidth-x; }
currFrame[i] = rawImage[(y*camWidth) + x-1];
Background[i] = rawBackground[(y*camWidth) + x-1]; if (isSafeColor) {
} safeColorPixelsCounter++;
} pixels[i] = color(0, 255, 0);
else { screenPixels[i] = color(0, 0, 0);
currFrame = rawImage; }
Background = rawBackground; }
}

loadPixels();
int safeColorPixelsCounter = 0; updatePixels();

for (int i = 0; i < camWidth*camHeight; i++) { int biggestBlobArea = 0;


if (showCameraView) { target.computeBlobs(screenPixels);
pixels[i] = currFrame[i]; for (int i = 0; i < target.getBlobNb()-1; i++) {
blob = target.getBlob(i); boolean clearOfZones = true;
int blobWidth = int(blob.w*camWidth); for (int col = 0; col <= restrictedZone; col++) {
int blobHeight = int(blob.h*camHeight); if (possibleX > fireRestrictedZones[col][0] && possibleX <
if (blobWidth*blobHeight >= biggestBlobArea) { fireRestrictedZones[col][1] && possibleY >
biggestBlob = target.getBlob(i); fireRestrictedZones[col][2] && possibleY <
biggestBlobArea = fireRestrictedZones[col][3]) {
int(biggestBlob.w*camWidth)*int(biggestBlob.h*camHeig clearOfZones = false;
ht); fire = 0;
} }
} }
possibleX = 0;
possibleY = 0;
if (safeColorPixelsCounter > safeColorMinSize && safeColor)
if (biggestBlobArea >= minBlobArea) { {
possibleX = int(biggestBlob.x * camWidth); noStroke();
possibleY = int(biggestBlob.y * camHeight); fill(0, 255, 0, 150);
} rect(0, 0, width, height);
fire = 0;
targetX = int((xMin+xMax)/2.0);
if ((biggestBlobArea >= minBlobArea)) { targetY = int(yMin);
fire = 1; displayX = camWidth/2;
if (showTargetBox) { displayY = camHeight;
stroke(255, 50, 50); }
strokeWeight(3); }
fill(255, 50, 50, 150);
rect(int(biggestBlob.xMin*camWidth), void manualMode() {
int(biggestBlob.yMin*camHeight), int((biggestBlob.xMax- // cursor(1);
biggestBlob.xMin)*camWidth), int((biggestBlob.yMax- camInput.update();
biggestBlob.yMin)*camHeight)); rawBackground = camInput.retinaImage();
} rawImage = camInput.image();
if (mirrorCam) {
anticipation(); for (int i = 0; i < camWidth*camHeight; i++) {
int y = floor(i/camWidth);
if (activeSmoothing) { int x = i - (y*camWidth);
xdiff = possibleX - oldX; // smoothing x = camWidth-x;
ydiff = possibleY - oldY; // smoothing currFrame[i] = rawImage[(y*camWidth) + x-1];
possibleX = int(oldX + xdiff*(1.0-smoothingFactor)); // Background[i] = rawBackground[(y*camWidth) + x-1];
smoothing }
possibleY = int(oldY + ydiff*(1.0-smoothingFactor)); // }
smoothing else {
} currFrame = rawImage;
Background = rawBackground;
displayX = possibleX; }
displayY = possibleY;
if (displayX < 0) loadPixels(); //draw camera view to screen
displayX = 0; for (int i = 0; i < camWidth*camHeight; i++) { //
if (displayX > camWidth) pixels[i] = currFrame[i]; //
displayX = camWidth; } //
if (displayY < 0) updatePixels(); //
displayY = 0;
if (displayY > camHeight) if(inputDeviceIsSetup) {
displayY = 0; checkInputDevice();
targetX = int((possibleX/xRatio)+xMin); }
targetY = int(((camHeight-possibleY)/yRatio)+yMin); if(useInputDevice) {
oldX = possibleX; // smoothing updateInputDevice(); // determine control values
oldY = possibleY; // smoothing using the input device (see declaration in Input_Device tab)
} if(useArrowKeys) { // use the arrow keys to aim one pixel at a
else { time
fire = 0; // use arrow keys to aim - see keyReleased() below
} if(keyPressed) {
if (keyCode == 37) { // left arrow
xPosition -= 1; trackColorRed =
} int(red(currFrame[(mouseY*width)+mouseX]));
if (keyCode == 38) { // up arrow trackColorBlue =
yPosition -= 1; int(blue(currFrame[(mouseY*width)+mouseX]));
} trackColorGreen =
if (keyCode == 39) { // right arrow int(green(currFrame[(mouseY*width)+mouseX]));
xPosition += 1; selectingColor = false;
} }
if (keyCode == 40) { // down arrow
yPosition += 1; if (selectingSafeColor) {
} safeColorRed =
fire = 0; int(red(currFrame[(mouseY*width)+mouseX]));
safeColorBlue =
} int(blue(currFrame[(mouseY*width)+mouseX]));
} safeColorGreen =
}else{ int(green(currFrame[(mouseY*width)+mouseX]));
if(useArrowKeys) { // use the arrow keys to aim one pixel selectingSafeColor = false;
at a time }
// use arrow keys to aim - see keyReleased() below }
if(keyPressed) {
if (keyCode == 37) { // left arrow void mouseReleased() {
displayX -= 1; if (keyPressed && key == 'r') {
} println(" ... " + mouseX + ", " + mouseY);
if (keyCode == 38) { // up arrow fireRestrictedZones[restrictedZone][1] = mouseX;
displayY -= 1; fireRestrictedZones[restrictedZone][3] = mouseY;
} if
if (keyCode == 39) { // right arrow (fireRestrictedZones[restrictedZone][1]>fireRestrictedZones[rest
displayX += 1; rictedZone][0] &&
} fireRestrictedZones[restrictedZone][1]>fireRestrictedZones[restr
if (keyCode == 40) { // down arrow ictedZone][2]) {
displayY += 1; restrictedZone++;
} }
fire = 0; }
} }
}else{
displayX = mouseX; void keyReleased() {
displayY = mouseY; if ( key == 'p') {
if (mousePressed) { randomIdleSound();
fire = 1; }
}
else { if (key == ' ') {
fire = 0; controlMode = !controlMode;
} }
}
targetX = constrain(int((displayX/xRatio)+xMin), 0, 180); if (key == 'b') {
// calculate position to go to based on mouse position camInput.adapt();
playSound(15);
targetY = constrain(int(((camHeight- }
displayY)/yRatio)+yMin), 0, 180); // if (key == 'a') {
xMin = float(targetX);
} xRatio = (camWidth / (xMax - xMin)); // used to
} allign sights with crosshairs on PC
}
if (key == 'd') {
void mousePressed() { xMax = float(targetX);
if (keyPressed && key == 'r') { xRatio = (camWidth / (xMax - xMin)); // used to
print("constraints:" + mouseX + ", " + mouseY); allign sights with crosshairs on PC
fireRestrictedZones[restrictedZone][0] = mouseX; }
fireRestrictedZones[restrictedZone][2] = mouseY; if (key == 's') {
} yMin = float(targetY);
if (selectingColor) { yRatio = (camHeight/ (yMax - yMin)); //
} s11.close();
if (key == 'w') { s12.close();
yMax = float(targetY); s13.close();
yRatio = (camHeight/ (yMax - yMin)); // s14.close();
} s15.close();
if(key == CODED && keyCode == 16) { // shift key was s16.close();
pressed, toggle aim with arrow keys s17.close();
useArrowKeys = !useArrowKeys; s18.close();
} s19.close();
s20.close();
} s21.close();
minim.stop();
}
if (!runWithoutArduino) {
public void viewCameraSettings() { arduinoPort.write("z0000000");
camInput.settings(); delay(500);
playSound(21); arduinoPort.stop();
} }
camInput.stop();
public void openWebsite() { super.stop();
link("http://psg.rudolphlabs.com/"); }
playSound(15); 6. Conclusiones y Recomendaciones
}

public void setBackground() { Se logró desarrollar satisfactoriamente la elaboración


camInput.adapt(); del prototipos torre de control.
playSound(11);
}
7. Anexos
public void playRandomSound() {
randomIdleSound();
}

public void selectColor() {


selectingColor = true;
}

public void selectSafeColor() {


selectingSafeColor = true;
}

public void radioEffect(int ID) {


effect = ID + 1;
}

public void stop() {


if(soundEffects) {
s1.rewind();
s1.play();
delay(2500);
s1.close();
s2.close();
s3.close();
s4.close();
s5.close();
s7.close();
s6.close();
s8.close();
s9.close();
s10.close();
7. Referencia Bibliográfica

(1)
http://projectsentrygun.rudolphlabs.com/successful-
projects

You might also like