Advertisement
Technologyman00

Untitled

Aug 22nd, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.16 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class NoAngorTurrMain {
  3. public static void main(String[] args) {
  4.  
  5.     //Instances
  6.     Scanner scan=new Scanner(System.in);
  7.    
  8.     //Variables
  9.         //Robot Has
  10.         double targetnumb, x_axis, y_axis, height, width; //Output from Pixy
  11.         //View is 640 x 400
  12.         //Variables to Decide which Goal is Correct
  13.             double numdist;
  14.             int best = 0,bestx = 0,besty = 0,besth = 0,bestw = 0;
  15.             int done = 0;
  16.             int waitfortargets = 15;
  17.             double[] backtargetnumb = new double[waitfortargets];
  18.             double[] backx_axis = new double[waitfortargets];
  19.             double[] backy_axis = new double[waitfortargets];
  20.             double[] backheight = new double[waitfortargets];
  21.             double[] backwidth = new double[waitfortargets];
  22.         //Selected Target Variables
  23.             double xp = 0,yp = 0,hp = 0,wp = 0;
  24.         double wantx, wanty, wanth, wantw; //Target From Testing
  25.         boolean xgood, ygood, hgood, wgood; //Make Sure All are good
  26.         double oldwidth; //For Checking if Strafing is better or worse
  27.         double LeftMotor = 0, RightMotor = 0; //Robot Variables
  28.         boolean SeesTarget, fixed; //SeesTarget is for alerting Driver and Causes Experiment Code to Run //Fixed Causes the Correction to not Fight other Correction
  29.         //For Sake of Experiment
  30.         boolean strafe = false; //If Robot Can Strafe or Not.
  31.         boolean auto = false; //If in Autonomous
  32.    
  33.     while(true){
  34.     //User input
  35.         //What Robot Sees
  36.         System.out.println("Enter Target Number:");
  37.         targetnumb = scan.nextInt();
  38.         System.out.println("Enter X Axis:");
  39.         x_axis = scan.nextInt();
  40.         System.out.println("Enter Y Axis:");
  41.         y_axis = scan.nextInt();
  42.         System.out.println("Enter Height:");
  43.         height = scan.nextInt();
  44.         System.out.println("Enter Width:");
  45.         width = scan.nextInt();
  46.         //Correct Points
  47.         wantx = 200;
  48.         wanty = 320;
  49.         wanth = 80;
  50.         wantw = 40;
  51.        
  52.        
  53.    
  54.     //Start Tracking Code
  55.         if(targetnumb > 0){
  56.             SeesTarget = true;
  57.             //Collect waitfortargets amount of data
  58.             for(int i=0; i < waitfortargets; i++){
  59.                 backx_axis[i] = x_axis;
  60.                 backy_axis[i] = y_axis;
  61.                 backheight[i] = height;
  62.                 backwidth[i] = width;
  63.             }
  64.             //Find Which Data is the Closest to the Right.
  65.             //X-Axis
  66.                 numdist = Math.abs(backx_axis[0] - wantx);
  67.                 for(int i = 1; i < waitfortargets; i++){
  68.                     if(Math.abs(backx_axis[i] - wantx) < numdist){
  69.                         bestx = i;
  70.                         numdist = Math.abs(backx_axis[i] - wantx);
  71.                     }
  72.                 }
  73.             //Y-Axis  
  74.                 numdist = Math.abs(backy_axis[0] - wanty);
  75.                 for(int i = 1; i < waitfortargets; i++){
  76.                     if(Math.abs(backy_axis[i] - wanty) < numdist){
  77.                         besty = i;
  78.                         numdist = Math.abs(backy_axis[i] - wanty);
  79.                     }
  80.                 }
  81.             //Height
  82.                 numdist = Math.abs(backheight[0] - wanth);
  83.                 for(int i = 1; i < waitfortargets; i++){
  84.                     if(Math.abs(backheight[i] - wanth) < numdist){
  85.                         besth = i;
  86.                         numdist = Math.abs(backheight[i] - wanth);
  87.                     }
  88.                 }
  89.             //Width
  90.                 numdist = Math.abs(backwidth[0] - wantw);
  91.                 for(int i = 1; i < waitfortargets; i++){
  92.                     if(Math.abs(backwidth[i] - wantw) < numdist){
  93.                         bestw = i;
  94.                         numdist = Math.abs(backwidth[i] - wantw);
  95.                     }
  96.                 }
  97.             //Find which is Best
  98.                 best = (bestx + besty + besth + bestw)/4;
  99.         }
  100.     while(done == 0){
  101.         if(targetnumb == best){
  102.             xp = x_axis;
  103.             yp = y_axis;
  104.             hp = height;
  105.             wp = width;
  106.         }
  107.         fixed = false;
  108.         xgood = false;
  109.         ygood = false;
  110.         hgood = false;
  111.         wgood = false;
  112.         //Height of Target First
  113.             if(hp > wanth && fixed == false){
  114.                 LeftMotor = (1 - ((hp * -1)/wanth));
  115.                 RightMotor = (1 - ((hp * -1)/wanth));
  116.                 fixed = true;
  117.             }
  118.             else if(hp < wanth && fixed == false){
  119.                 LeftMotor = (hp/wanth);
  120.                 RightMotor = (hp/wanth);
  121.                 fixed = true;
  122.             }
  123.             else{
  124.                 //Size Good
  125.                 LeftMotor = 0;
  126.                 RightMotor = 0;
  127.                 hgood = true;
  128.             }
  129.            
  130.         //X-Axis
  131.             if(xp > wantx && fixed == false){
  132.                 //Turn Left
  133.                 System.out.println("More than X Fix");
  134.                 LeftMotor = (1 - (wantx/xp));
  135.                 RightMotor = ((1 - (wantx/xp)) * -1.0);
  136.                 fixed = true;
  137.             }
  138.             else if(xp < wantx && fixed == false){
  139.                 //Turn Right
  140.                 System.out.println("Less than X Fix");
  141.                 LeftMotor = ((1 - (xp/wantx)) * -1.0);
  142.                 RightMotor = (1 - (xp/wantx));
  143.                 fixed = true;
  144.             }
  145.             else if(fixed == false){
  146.                 //Placement Good
  147.                 LeftMotor = 0;
  148.                 RightMotor = 0;
  149.                 xgood = true;
  150.             }
  151.         //Y-Axis of the Target Third Can only change if Robot has Adjustable Angle //Works same as Height if No Adjustable Angle
  152.         //Angle Adjust
  153.             if(fixed == false && hgood == true && yp != wanty){
  154.                 System.out.println("ERROR Target Too High! UNABLE TO FIX!");
  155.             }
  156.             else if(yp == wanty){
  157.                 ygood = true;
  158.             }
  159.             else{
  160.                 System.out.println("ERROR Y-AXIS UNABLE TO FIX AND HEIGHT NO FIX");
  161.             }
  162.            
  163.         //Width of Target Last (HAS TO STRAFE SHOULD THROW ERROR IF ROBOT CANNOT STRAFE) //Shouldn't really matter with turning of robot But here anyways
  164.             if(wp > wantw && strafe == true && fixed == false){
  165.                 //Nothing to Do if Width too big Should Reverse because of Error
  166.                 System.out.println("TOO CLOSE CAMERA ERROR!");
  167.                 //LeftMotor = (width/wantw);
  168.                 //RightMotor = (width/wantw);
  169.                 //fixed = true;
  170.             }
  171.             else if(wp < wantw && strafe == true && fixed == false){
  172.                 oldwidth = wp;
  173.                 //Strafe Left
  174.                 if(oldwidth > wp){
  175.                     //Strafe Right
  176.                     System.out.println("Strafing Right was Better");
  177.                 }
  178.                 else if(oldwidth < wp){
  179.                     System.out.println("Strafing Left was Better");
  180.                 }
  181.                 else{
  182.                     System.out.println("STRAFING DIDNT WORK ERROR!");
  183.                 }
  184.                 fixed = true;
  185.             }
  186.             else if(strafe == false && fixed == false && wp != wantw){
  187.                 System.out.println("CANNOT STRAFE MAY NEED TO RECENTER ROBOT ON TARGET!");
  188.             }
  189.             else if(fixed == false){
  190.                 //Size Good
  191.                 LeftMotor = 0;
  192.                 RightMotor = 0;
  193.                 wgood = true;
  194.             }  
  195.         //Check if Shooting is Good! Absolutely
  196.             if(wgood && hgood && xgood && ygood && fixed == false){
  197.                 System.out.println("Everything Good!");
  198.                 if(auto){
  199.                     System.out.println("Shot Ball.");
  200.                     done = 1;
  201.                 }
  202.                 if(auto == false){
  203.                     System.out.println("Wait For Trigger.");
  204.                     done = 1;
  205.                 }
  206.             }
  207.     }      
  208.     //End Tracking Code
  209.            
  210.     System.out.println("Left Motor: " + LeftMotor);
  211.     System.out.println("Right Motor: " + RightMotor);
  212.            
  213.     }
  214.    
  215. }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement