Scan objects while moving? I'm using leJOS with an NXT robot. I want to scan the distances of objects from my robot. While rotating the Ultrasonic sensor from right to left, I would like to find out which object is closest to me.

Motor.A.isRotating() seemed to me as a good idea, but later I found out that leJOS hasn't implemented it yet. They are suggesting using isMoving() instead. So I tried this:

 while(!Button.ESCAPE.isDown())
   {
    Motor.A.rotateTo( 40);
    while(Motor.A.isMoving())
    {
     value = sonic.getDistance();
     LCD.clear();
     LCD.drawString("Distance: ", 0, 2);
     LCD.drawInt(value, 0, 3);
     LCD.refresh();
     Thread.sleep(100);   
    }
    Thread.sleep(100);   

    Motor.A.rotateTo( -40);
    while(Motor.A.isMoving())
     {
      value = sonic.getDistance();
      LCD.clear();
      LCD.drawString("Distance: ", 0, 2);
      LCD.drawInt(value, 0, 3);
      LCD.refresh();
      Thread.sleep(100);   
     }
      Thread.sleep(100);   
   }


I'm open to suggestions and tips! Thank you!
I'm not a leJOS expert, but from looking at the docs, I don't think that the rotateTo method works the way that you are expecting:


  public void rotateTo(int limitAngle)
  
  Rotate to the target angle. Do not return until the move is complete.


So here's what your code is actually doing:

Motor.A.rotateTo(40); // Rotate to 40 and return when rotation is finished
while(Motor.A.isMoving()) { // The motor won't be moving
    // Execution never gets here
}


The rotateTo method takes an optional immediateReturn parameter that allows it to return without waiting for movement to complete:


  immediateReturn - if true do not wait for the move to complete


Replace your calls to rotateTo(angle) with rotateTo(angle, true), and you should be on the right track. Something like this should be more effective:

Motor.A.rotateTo(40, true); // Begin rotating to 40 and return immediately
while(Motor.A.isMoving()) { // The motor will be rotating for a time
    // Execution should loop here until the motor stops
}