rotateTo() problem

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

rotateTo() problem

Postby hyderabat » Fri Nov 04, 2011 1:03 pm

Hi

I got a problem with the rotateTo()-method.

What I am doing:

Turning the motors.
Stopping the motors.
Resetting the tachometer with Motor.A.resetTachoMeter().
Turning the motors.
Stopping the motors.
Tell them to go to the ordered position (in my case 0).

The problem is that they just continue spinning around and around.
Exact code of the problem:

Code: Select all
for (int i = 0;i<3;i++) {m[i].stop();mR[i].stop();}
      Button.waitForPress(2000);
      if (error == 0) goZero();


Code: Select all
public static void goZero() {   //resets all legs to the original position, called after a shutdown-order
      
      LCD.clear();
      LCD.drawString("Starting zeroing!",0,2);
      for (int i = 0;i<3;i++){
         m[i].setSpeed(40);
         m[i].rotateTo(0,true);
         mR[i].setSpeed(40);
         mR[i].rotateTo(0,true);
      }
      for (int i = 0;i<3;i++){m[i].waitComplete();mR[i].waitComplete();}
      
      if (m[0].getPosition()== 0 && m[1].getPosition()== 0 && m[2].getPosition() == 0) LCD.drawString("Finished zeroing",0,5);
      else LCD.drawString("Couldn't zero!", 0, 5);
      Button.waitForPress(2000);


It is supposed to stop in my opinion (API: "if immediateReturn is true, method returns immediately and the motor stops by itself" )

Thanks for any help in advance!!!
hyderabat
New User
 
Posts: 14
Joined: Sun Oct 16, 2011 12:48 pm

Re: rotateTo() problem

Postby hyderabat » Fri Nov 04, 2011 1:19 pm

Ok, so I figured out with a simple test program that it just simply goes aaaaaaaaaaall the way back to the original position.

I just want to have it in the same position, but not after it turned back for ages. Isn't it possible to set 0 as a special point on the circle and then just go back to this point?
hyderabat
New User
 
Posts: 14
Joined: Sun Oct 16, 2011 12:48 pm

Re: rotateTo() problem

Postby hyderabat » Fri Nov 04, 2011 1:37 pm

Okay, so for everyone with the same Problem, this is what I did to make it work:

Code: Select all
int [] Number = new int[6];
      int [] Rest = new int[6];
      
      for (int i = 0;i<3;i++) {
      
      m[i].stop();
      Number[i] = m[i].getPosition()/360;
      Rest[i] = m[i].getPosition() - Number[i]*360;
      m[i].rotate(360 - Rest[i],true);
      }


If anyone has a better idea, please write it...
hyderabat
New User
 
Posts: 14
Joined: Sun Oct 16, 2011 12:48 pm

Re: rotateTo() problem

Postby nate » Fri Nov 04, 2011 8:59 pm

Try this out. [ Untested, not even compiled ]

Code: Select all
public static void goZero() {
    LCD.clear();
    LCD.drawString("Starting zeroing!", 0, 2);

    for (int i = 0;i < 3; i++) {
        m[i].rotateTo(calcPos(m[i], 0), true);
        m[i].setSpeed(40);
        m[i].rotateTo(calcPos(mR[i], 0), true);
        mR[i].setSpeed(40);
    }

    // Wait for the motors to find their mark.
    for (int i = 0;i < 3; i++) {
        m[i].waitComplete();
        mR[i].waitComplete();
    }

    // How close must we be.  +-1 degree is good enough!
    if (Math.abs(m[0].getPosition() % 360) < 2 &&
        Math.abs(m[1].getPosition() % 360) < 2 &&
        Math.abs(m[2].getPosition() % 360) < 2)
        LCD.drawString("Finished zeroing",0, 5);
    else
        LCD.drawString("Couldn't zero!", 0, 5);

    Button.waitForPress(2 * 1000);
}

private static int calcPos(Motor m, int desiredPos) {
    // We don't care how many full rotations, we just want the offset
    // from the zero position.
    int motorEncoderPos = m.getPosition();
    int absPos = Math.abs(motorEncoderPos);

    // Calculate how far away the motor is currently away from this
    // position.
    int offset = (absPos + desiredPos) % 360;

    // Determine which way we want to go based on how far away we are.
    // If we are less than 180 degrees away, then make the target
    // position the smaller number (reversed), otherwise go forward
    // since it's less to move.
    int target;
    if (offset <= 180)
        target = absPos - offset;
    else
        target = absPos + (360 - offset);

    // Finally, determine if the encoder position is positive or
    // negative, and return the position accordingly.
    if (motorEncoderPos > 0)
        return target;
    else
        return -target;
}
nate
New User
 
Posts: 16
Joined: Sat Oct 29, 2011 11:40 pm

Re: rotateTo() problem

Postby hyderabat » Sat Nov 05, 2011 12:17 pm

Thanks for the suggestion. By the way, won't run, as it needs NXTRegulatedMotor m instead of "Motor m"... but I am using RemoteMotor as well, so I can't use this method.

And isn't that just simply easier and faster:

Code: Select all
for (int i = 0;i<3;i++){   //lets put all the legs up (Position: 0)
         
         m[i].stop();
         m[i].setSpeed(50);
         RestUp[i] = m[i].getPosition() % 360;   //% = Rest of Division
         if (RestUp[i] >= 180) {m[i].rotate(360 - RestUp[i],true);}
         else m[i].rotate(-RestUp[i]);
         m[i].resetTachoCount();      //used after curve()


Works for positive and negative numbers, and you need a lot less integers...
hyderabat
New User
 
Posts: 14
Joined: Sun Oct 16, 2011 12:48 pm

Re: rotateTo() problem

Postby nate » Sun Nov 06, 2011 11:14 pm

Sure, I was trying to make it easier to read/understand, not shorter. Whatever works for you!
nate
New User
 
Posts: 16
Joined: Sat Oct 29, 2011 11:40 pm


Return to NXJ Software

Who is online

Users browsing this forum: Google [Bot] and 4 guests

more stuff