Advertisement
Guest User

Untitled

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