Answers.Body: <p>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:</p>
  
  <blockquote>
    <p>public void rotateTo(int limitAngle)</p>
    
    <p>Rotate to the target angle. <strong>Do not return until the move is complete.</strong></p>
  </blockquote>
  
  <p>So here's what your code is actually doing:</p>
  
  <pre><code>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
  }
  </code></pre>
  
  <p>The rotateTo method takes an optional <code>immediateReturn</code> parameter that allows it to return without waiting for movement to complete:</p>
  
  <blockquote>
    <p>immediateReturn - if true do not wait for the move to complete</p>
  </blockquote>
  
  <p>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:</p>
  
  <pre><code>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
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-05-30T11:30:41.160Z
Answers.Text: Thank you very much, makes sense! I will test it tomorrow in the lab!
Answers.UserDisplayName: Anarkie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-05-31T09:17:03.690Z
Answers.Text: When I look here http://www.lejos.org/p_technologies/nxt/nxj/api/lejos/nxt/Motor.html rotateTo() also has boolean immediate return. Will try both versions in few minutes.
Answers.UserDisplayName: Anarkie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-05-31T09:35:28.017Z
Answers.Text: So `Motor.A.rotateTo(40 , true);` works but I couldn't find it without you, thanks!
Answers.UserDisplayName: Anarkie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-05-31T13:02:10.863Z
Answers.Text: I'm glad this worked for you! I've updated the answer to include the info from your comments in case someone else runs into this problem.
Answers.UserDisplayName: jncraton
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-05-29T18:45:16.587Z
Answers.LastActivityDate: 2014-05-31T13:00:42.757Z
Answers.LastEditDate: 2014-05-31T13:00:42.757Z
Answers.UserDisplayName: jncraton
Body: <p>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.</p>
  
  <p><code>Motor.A.isRotating()</code> seemed to me as a good idea, but later I found out that leJOS hasn't implemented it yet. They are suggesting using <code>isMoving()</code> instead. So I tried this:</p>
  
  <pre><code> 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);   
     }
  </code></pre>
  
  <p>I'm open to suggestions and tips! Thank you!</p>
  
Tags: <nxt><mindstorms><motor><sensor-ultrasonic>
Title: Scan objects while moving?
UserDisplayName: Anarkie
ThreadText: # Scan objects while moving?
  
  (Asked by: username_0 on 2014-05-29)
  
  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!
  
  **Tags:** nxt, mindstorms, motor, sensor-ultrasonic
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-05-29. Score: 0 | Accepted answer
  
  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
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you very much, makes sense! I will test it tomorrow in the lab!  
  > **username_0**: When I look here http://www.lejos.org/p_technologies/nxt/nxj/api/lejos/nxt/Motor.html rotateTo() also has boolean immediate return. Will try both versions in few minutes.  
  > **username_0**: So `Motor.A.rotateTo(40 , true);` works but I couldn't find it without you, thanks!  
  > **username_1**: I'm glad this worked for you! I've updated the answer to include the info from your comments in case someone else runs into this problem.  
  
  ---
  
  