Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.31 KB | None | 0 0
  1. import lejos.nxt.*;
  2.  
  3.  
  4.  
  5. public class blockPickUp extends Thread {
  6.         public static final int ROTATE_SPEED = 150;
  7.         public static final int FORWARD_SPEED = 200;
  8.         public static final double forwardDistance = 12.0;
  9.         public static final double forwardThreshold = 22.0;
  10.         public final double leftRadius = 2.1;
  11.         public final double rightRadius = 2.15;
  12.         public final double width = 10.0;
  13.         private ClawDriver claw;
  14.         private UltrasonicSensor sensor;
  15.         private NXTRegulatedMotor leftMotor,rightMotor;
  16.        
  17. //        private static UltrasonicPoller poller;
  18. //        private  UltrasonicController usController;
  19.        
  20.         //initiating objects
  21.         WheelDriver wheels = new WheelDriver( leftMotor, rightMotor) ;
  22.         Odometer odo = new Odometer();
  23.         Navigation nav = new Navigation(leftRadius, rightRadius, odo, wheels);
  24.         UltrasonicSensor us = new UltrasonicSensor(SensorPort.S1);
  25.        
  26.         public blockPickUp (ClawDriver claw, NXTRegulatedMotor leftMotor, NXTRegulatedMotor rightMotor ) {
  27.             this.claw = claw;
  28.             this.leftMotor = leftMotor;
  29.             this.rightMotor= rightMotor;
  30.         }
  31.  
  32.         public void scanRange() {
  33.            
  34.                 int threshold = 25;    
  35.                 boolean travel = true;
  36.                 double turnAngle = -20;
  37.                 int storeI = 0;
  38.                 int storeW = 0;
  39.                 double backDistance = 0;
  40.                 int loopBreak = 0;
  41.                 while (travel){                
  42.                     if (us.getDistance() < threshold) {
  43.                         travelForward(forwardThreshold);
  44.                         claw.close();
  45.                         break;
  46.                     }
  47.                     rotateCCW(60);
  48.                    
  49.                     for (int i = 0 ; i < 6 ; i++){
  50.                         rotateCCW(turnAngle);
  51.                         if (us.getDistance() < threshold){
  52.                             travelForward(forwardThreshold);
  53.                             claw.close();
  54.                             loopBreak = 1;
  55.                             storeI = i;
  56.                             break;
  57.                         }
  58.                        
  59.                     }
  60.                     if (loopBreak != 1){
  61.                         rotateCCW(60);
  62.                         travelForward(forwardDistance);
  63.                         storeW++;
  64.                     }
  65.                     else {
  66.                         break;
  67.                     }
  68.                 }
  69.                
  70.                 Sound.beep();
  71.                
  72.                 rotateCCW(-180);
  73.                 travelForward(forwardThreshold);
  74.                
  75.                 if (storeI <= 2) {
  76.                     rotateCCW(storeI*turnAngle);
  77.                 }
  78.                 if (storeI >= 4){
  79.                     rotateCCW(-storeI*turnAngle);
  80.                 }
  81.                
  82.                 travelForward(storeW*forwardDistance);
  83.                
  84.         }
  85.        
  86.         public void travelForward(double forwardDistance){
  87.                 leftMotor.setSpeed(FORWARD_SPEED);
  88.                 rightMotor.setSpeed(FORWARD_SPEED);
  89.                 leftMotor.rotate(convertDistance(leftRadius, forwardDistance), true);
  90.                 rightMotor.rotate(convertDistance(rightRadius, forwardDistance), false);
  91.         }
  92.        
  93.         //rotate 45 degrees counterclockwise
  94.         public void rotateCCW(double angle) {
  95.             leftMotor.setSpeed(ROTATE_SPEED);
  96.             rightMotor.setSpeed(ROTATE_SPEED);
  97.             leftMotor.rotate(-convertAngle(leftRadius, width, angle), true);
  98.             rightMotor.rotate(convertAngle(rightRadius, width, angle), false);
  99.            
  100.         }
  101.        
  102.         private int getFilteredData() {
  103.             int distance;
  104.             // do a ping
  105.             us.ping();
  106.             // wait for the ping to complete
  107.             try { Thread.sleep(50); } catch (InterruptedException e) {}
  108.             // there will be a delay here
  109.             distance = us.getDistance();
  110.             return distance;
  111.     }
  112.        
  113.        
  114.         private static int convertDistance(double radius, double distance) {
  115.                 return (int) ((180.0 * distance) / (Math.PI * radius));
  116.         }
  117.  
  118.         private static int convertAngle(double radius, double width, double angle) {
  119.                 return convertDistance(radius, Math.PI * width * angle / 360.0);
  120.         }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement