Package

Source Code of mandafruta

import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

public class mandafruta {
  static int MAXIMO_DERECHA_ANCHO = 108;
  static int MINIMO_IZQUIERDA_ANCHO = 68;

  static int MAXIMO_DERECHA_ANGOSTO = 98;
  static int MINIMO_IZQUIERDA_ANGOSTO = 78;

  static int MINIMO_AREA_FIN = 8000;
  static int MAXIMO_AREA_FIN = 22000;
  static int MAXIMO_SUPER_AREA_FIN = 40000;
//  static int MINIMO_MASA_FIN = 40;

  static int MINIMO_MASA = 20;
  static int MINIMO_MASA_CAMBIO_MODO_COLOR = 50;

  static int CANTIDAD_VECES_PROMEDIO = 5;
  //static SerialCommunicationChannel sc;
  static SerialComm sc;
  static RobotCamColorTrack prueba;

  static OutputStream out;

  /**
   * @param args
   * @throws IOException
   * @throws NoSuchPortException
   * @throws PortInUseException
   */
  public static void main(String[] args) throws Exception {
//    sc = new SerialCommunicationChannel();
//    sc.open("banana, COM5");

//    sc.send("advance");

    CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier( "COM6" );
    SerialPort sp = (SerialPort)portId.open("SerialCommunicationChannel", 2000);

    sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
    out  = sp.getOutputStream();
//    sc = new SerialComm("6",3);*/

    prueba = new RobotCamColorTrack("5");
    if (!prueba.setupCamera())
    {
      out.close();
      sp.close();
      return;
    }

    boolean continuar = true;

    int maxX=0,maxY=0,minX = 5000, minY = 5000;
   
    int remainingMovements = 200;
   
    int direccion = RobotCamColorTrack.NO_MOVE;
    int modo;
   
    int[] rgbMinRail = {43,52,120};
    int[] rgbMaxRail = {93,102,170};
    int[] rgbMinRed = {120,0,0};
    int[] rgbMaxRed = {180,51,46};
    int[] rgbMinViolet = {16,5,54};
    int[] rgbMaxViolet= {66,55,104};

    int areamax = 0;
    int antmove = RobotCamColorTrack.MOVER_ADELANTE;
    int meanxcolor = 0;
    TrackingInformation tiRail;
    TrackingInformation tiRed;
    TrackingInformation tiViolet;

    boolean redPlace = false;

    modo = 1;

    while(remainingMovements > 0 && !redPlace) {
   
      //TrackingInformation tiRail = prueba.addTrackingInformation(RobotCamColorTrack.TRACK_RAIL, rgbMinRail, rgbMaxRail);     
      //TrackingInformation tiRed = prueba.addTrackingInformation(RobotCamColorTrack.TRACK_RED, rgbMinRed, rgbMaxRed);
      int samples = 1;

      prueba.grabFrame();
      tiRail = prueba.addTrackingSamples(RobotCamColorTrack.TRACK_RAIL, rgbMinRail, rgbMaxRail, samples);
      tiViolet = prueba.addTrackingSamples(RobotCamColorTrack.TRACK_RED, rgbMinRed, rgbMaxRed, samples);
      tiRed = prueba.addTrackingSamples(RobotCamColorTrack.TRACK_RED, rgbMinRed, rgbMaxRed, samples);
     

      //boolean colisionRail = prueba.isCollision(RobotCamColorTrack.TRACK_RAIL,400,80);
      //boolean colisionRed = prueba.isCollision(RobotCamColorTrack.TRACK_RED,30,20);
     
      System.out.println("Movimiento: "+(200-remainingMovements));

      System.out.println("===========================");
      System.out.println("RAIL");
      System.out.println(tiRail);

      System.out.println("===========================");
      System.out.println("RED");
      System.out.println(tiRed);
     
      //redPlace = (prueba.isInPlace(RobotCamColorTrack.TRACK_RED, 4500, 255,5,1) && tiRail.isCollision());

      int railMass = tiRail.getColorMass();
      int colorMass = tiRed.getColorMass();
     
      if (modo==1)
      {
        direccion = prueba.getTurningDirection(tiRail.getMeanX(),railMass,15,antmove);
        antmove = direccion;

/*        if (tiRail.getColorMass()<MINIMO_MASA)
        {
          if (tiRed.getColorMass()>MINIMO_MASA_CAMBIO_MODO_COLOR)
          {
            System.out.println("CAMBIO MODO 2");
            areamax=tiRed.getArea();
            modo = 2;
          }
        }*/
/*        if (tiRed.getColorMass()<MINIMO_MASA)
        {
          if (direccion==RobotCamColorTrack.MOVER_ADELANTE)
          {
            direccion = prueba.getTurningDirection(meanxcolor);         
          }
        }
        else
        {
          meanxcolor = tiRed.getMeanX();
        }*/
      }
      if (modo==2)
      {
        direccion = prueba.getTurningDirection(tiRed.getMeanX(),colorMass,15,antmove);
        antmove = direccion;

        if (((tiRed.getArea()<MINIMO_AREA_FIN) && (areamax>MAXIMO_AREA_FIN)) || (areamax>MAXIMO_SUPER_AREA_FIN))// || (tiRed.getColorMass()<MINIMO_MASA_FIN))
        {
          System.out.println("LLEGUE!! AREA MAX "+areamax);
          modo=3;
          remainingMovements=0;
        }
        if (areamax<tiRed.getArea())
          areamax=tiRed.getArea();
      }
      if (modo==3)
      {
        direccion = RobotCamColorTrack.MOVER_DERECHA;
        int massviolet = tiViolet.getColorMass();
        int massred = tiRed.getColorMass();
       
      }
      if (remainingMovements>0)
        moverRobot(direccion,(direccion != RobotCamColorTrack.MOVER_ADELANTE) ? '2' : '5');

      remainingMovements--;
    }
   
    out.close();
    sp.close();
  }
 
/*  private static void printStatistics(String title,ArrayList<Integer> measures) {
    float mean = 0, desv = 0;
   
    for (Integer value : measures) {
      mean += value;
    }
   
    mean/=measures.size();
   
    for (Integer value : measures) {
      desv += (value-mean)*(value-mean);
    }
   
    desv/=(measures.size()-1);
    desv = (float)Math.sqrt(desv);
   
    System.out.println(title+" - Media: "+mean+" Desvio: "+desv);
  }*/

  private static void moverRobot(int direccion, char intensidad) throws IOException {
    byte salida[] = new byte[3];

    switch(direccion) {
      case RobotCamColorTrack.MOVER_IZQUIERDA: salida[0] = 'L'; break;
      case RobotCamColorTrack.MOVER_DERECHA: salida[0] = 'R'; break;
      case RobotCamColorTrack.MOVER_ATRAS: salida[0] = 'B'; break;
      case RobotCamColorTrack.MOVER_ADELANTE: salida[0] = 'F'; break;
      case RobotCamColorTrack.NO_MOVE: return;
      default: return;
    }
    salida[1] = (byte) intensidad;
    salida[2] = '\r';

    out.write(salida);
    try {
      Thread.sleep(300);
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

}
TOP

Related Classes of mandafruta

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.