Advertisement
Guest User

MyRobotClass

a guest
Feb 22nd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 KB | None | 0 0
  1. package nl.vu.cs.s2.simbadtest;
  2.  
  3. import java.awt.Color;
  4. import java.awt.image.BufferedImage;
  5. import java.util.ArrayList;
  6.  
  7. import javax.vecmath.Vector3d;
  8. import javax.vecmath.Point3d;
  9. import javax.vecmath.Color3f;
  10. import simbad.sim.Agent;
  11. import simbad.sim.CameraSensor;
  12. import simbad.sim.RangeSensorBelt;
  13. import simbad.sim.RobotFactory;
  14. import simbad.sim.LampActuator;
  15.  
  16.  
  17.  
  18. public class myRobot extends Agent{
  19.    
  20.     CameraSensor camera;
  21.     BufferedImage cameraImage;
  22.     RangeSensorBelt sonars;
  23.     RangeSensorBelt bumpers;
  24.     LampActuator lamp;
  25.     ArrayList<Point3d> posArray = new ArrayList<Point3d>();
  26.    
  27.     public myRobot(Vector3d position, String name, Color3f color) {
  28.         super(position, name);
  29.         this.setColor(color);
  30.         // Add bumpers
  31.         bumpers = RobotFactory.addBumperBeltSensor(this, 16);
  32.         // Add sonars
  33.         sonars = RobotFactory.addSonarBeltSensor(this, 8);  
  34.        
  35.         camera = RobotFactory.addCameraSensor(this);
  36.        
  37.         lamp = RobotFactory.addLamp(this);
  38.        
  39.     }
  40.    
  41.  
  42.     /** This method is called by the simulator engine on reset. */
  43.     public void initBehavior() {
  44.         System.out.println("I exist and my name is " + this.name);
  45.     }
  46.     public boolean boxDetected(){
  47.         cameraImage = camera.createCompatibleImage();
  48.         camera.copyVisionImage(cameraImage);
  49.         Color pixelRGB = new Color(cameraImage.getRGB(50,50));
  50.         int red = pixelRGB.getRed();
  51.         int green = pixelRGB.getGreen();
  52.         int blue = pixelRGB.getBlue();
  53.         if (red > 0 && green == 0 && blue == 0){
  54.             return true;
  55.         }
  56.         return false;
  57.        
  58.     }
  59.     //general strategy to avoid obstacles
  60.     public void avoidObstacle(double l, double fl, double f, double fr, double r){
  61. //      System.out.println("avoidObstacle");
  62.         if((fl < fr) && fl < 0.5){
  63.             rotateY(-5);
  64.         } else if ((fr < fl)  && fr < 0.5) {
  65.             rotateY(5);
  66.         } else if((fr > f) && f < 0.5 && (fl > f)){
  67.             rotateY(-5);
  68.         } else if (l < 0.3){
  69.             rotateY(-5);
  70.         } else if (r < 0.3){
  71.             rotateY(5);
  72.         }
  73.            
  74.     }
  75.            
  76.     // locate the box in the space
  77.     public void pinpoint(){
  78.         // TODO add error & classify with list
  79.         boolean contains = false;
  80.         Point3d coordinates = new Point3d();
  81.         getCoords(coordinates);
  82.         coordinates.x = Math.round(coordinates.x);
  83.         coordinates.y = Math.round(coordinates.y);
  84.         coordinates.z = Math.round(coordinates.z);
  85.         for(int i=0;i<posArray.size();i++){
  86.             if((coordinates.x == posArray.get(i).x) && (coordinates.z == posArray.get(i).z)){
  87.                 contains = true;
  88.             }
  89.         }
  90.         if(!contains){
  91.             posArray.add(coordinates);
  92.         }
  93.     }
  94.    
  95.     /** This method is call cyclically (20 times per second)
  96.      *  by the simulator engine. */
  97.     public void performBehavior() {
  98.                
  99.         setTranslationalVelocity(0.5);
  100.        
  101.        
  102.         if(getCounter() % 500 == 0){
  103. //          print the list of elements
  104.             System.out.println("-------------------begin of coords-----------------");
  105.             for(int i=0; i<posArray.size();i++){
  106.                 System.out.println(posArray.get(i));
  107.             }
  108.             System.out.println("-------------------end of coords-----------------");
  109.         }        
  110.         // perform the following actions every 5 virtual seconds
  111.         if(getCounter() % 5 == 0) {
  112.        
  113.             double frontSonar = sonars.getMeasurement(0);
  114.             double frontLeftSonar = sonars.getMeasurement(5);
  115.             double leftSonar = sonars.getMeasurement(4);
  116.             double rightSonar = sonars.getMeasurement(2);
  117.             double frontRightSonar = sonars.getMeasurement(1);
  118.            
  119.             if(bumpers.oneHasHit()){
  120.                 rotateY(-45);
  121.             }
  122.             if(boxDetected()){
  123.                 setRotationalVelocity(0);
  124.                 if(frontSonar < 0.3){
  125.                     pinpoint();
  126.                     avoidObstacle(leftSonar, frontLeftSonar, frontSonar, frontRightSonar, rightSonar);
  127.                 }
  128.             }
  129.             else if((sonars.getFrontQuadrantHits() > 0) || rightSonar < 0.5 || leftSonar < 0.5){
  130.                 avoidObstacle(leftSonar, frontLeftSonar, frontSonar, frontRightSonar, rightSonar);
  131.             }  
  132.         }
  133.         //frequently change orientation
  134.         if ((getCounter() % 50) == 0) {
  135.             if(!boxDetected()){
  136.                 setRotationalVelocity(Math.PI / 2 * (0.5 - Math.random()));
  137.             }
  138.         }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement