Motors problem

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

Moderators: 99jonathan, roger, imaqine

Motors problem

Postby aleXander » Tue Dec 13, 2011 11:26 pm

I have a problem with a motor. I'm using MotorPort class only. (coding in Java)
With these 3 methods:
controlMotor(power, mode);
resetTachoCount();
getTachoCount();



This code moves a motor connected to the B port with 90 degrees, then it stops the motor.

Code: Select all
MotorPort.B.controlMotor(70, 1);
while(MotorPort.B.getTachoCount() < 90)
{

}
MotorPort.B.controlMotor(70, 3);


After I run the previous code, if I try to find out MotorPort.B.getTachoCount() I get different values. Theoretically it should be 90.

How did I test it?
With this code:

Code: Select all
import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.MotorPort;


public class Test_motors
{
   public static void main(String[] args) throws Exception
   {
    int x = 10;
    MotorPort.B.controlMotor(70, 1);
    while(MotorPort.B.getTachoCount() < 90)
    {
       
    }
    MotorPort.B.controlMotor(70, 3);
    LCD.drawString("V: " + MotorPort.B.getTachoCount(), 0, 5);
    Thread.sleep(x);
    LCD.drawString("V2: " + MotorPort.B.getTachoCount(), 0, 6);
    while(!Button.ENTER.isPressed())
    {
 
    }
    Button.ENTER.waitForPressAndRelease();
   }

}


"V: " displays the value on the LCD immediately after the motor was stopped.
"V2: " displays the value on the LCD after a set time.

Look what happens for some values for x :

x = 10 -----------MotorPort.B.getTachoCount() = 93
x = 20 -----------MotorPort.B.getTachoCount() = 96
x = 30 -----------MotorPort.B.getTachoCount() = 99
x = 40 -----------MotorPort.B.getTachoCount() = 101
x = 50 -----------MotorPort.B.getTachoCount() = 102
x = 60 -----------MotorPort.B.getTachoCount() = 103
x = 70 -----------MotorPort.B.getTachoCount() = 104
x = 80 -----------MotorPort.B.getTachoCount() = 105
x = 90 -----------MotorPort.B.getTachoCount() = 105
x = 100 ----------MotorPort.B.getTachoCount() = 106
x = 150 ----------MotorPort.B.getTachoCount() = 106
x = 500 ----------MotorPort.B.getTachoCount() = 106
x = 2000 ---------MotorPort.B.getTachoCount() = 106
x = 10000 --------MotorPort.B.getTachoCount() = 106


Conclusion
My conclusion is that after the motor stops (meaning after the command MotorPort.B.controlMotor(70,3) )...it continues to move with approximately 16 degrees. Why is this happening?
It's very frustratring when I'm trying to switch between 3 recipients, and I'm always checking the getTachoCount() to know at which recipient I am right know. Everytime I lose 16 degrees.
Why why why? Please, help. :D

NOTE: No external force is messing with the motor. I'm not touching it.
aleXander
New User
 
Posts: 4
Joined: Wed Dec 07, 2011 6:50 pm

Re: Motors problem

Postby gloomyandy » Tue Dec 13, 2011 11:44 pm

Hi,
What you are seeing is expected. Motors like any other moving object do not stop instantly. You may want to take a look at the NXTRegulatedMotor class that will allow you to accurately control the movement of a motor. Even if for some reason you do not want to use that class take a look at the source code. That should give you an idea of how complex accurately controlling an motor can be.

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3966
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Motors problem

Postby aleXander » Wed Dec 14, 2011 5:28 am

gloomyandy wrote:Hi,
What you are seeing is expected. Motors like any other moving object do not stop instantly. You may want to take a look at the NXTRegulatedMotor class that will allow you to accurately control the movement of a motor. Even if for some reason you do not want to use that class take a look at the source code. That should give you an idea of how complex accurately controlling an motor can be.

Andy


Well, I was not usign NXTRegulatedMotor but MotorPort because my teacher said to use that in my assignment. Only basic classes.

I tried using Motor.B.rotateTo(angle) for rotations. No more problems. :D :D :D

I try to switch between 3 recipients (Blue[ID=1],Order[ID=2],Pink[ID=3])

Using MotorPort

I have 2 classes: RecipientChoser and Controller5 (has main method) .

RecipientChoser
Code: Select all
import lejos.nxt.MotorPort;

public class RecipientChoser
{
 private MotorPort motor = MotorPort.B;
 private int movementSpeed = 70;
 private int recipientID = 2;
 private int rotationValue;
 
 public RecipientChoser()
 {
   
 }
 
 public void resetTachoCount()
 {
    motor.resetTachoCount();
 }
 public int getTachoCount()
 {
    return motor.getTachoCount();
 }
 public int getRecipientID()
 {
    return recipientID;
 }
 
 public void calibrate() throws Exception
 {
    motor.controlMotor(movementSpeed, 1);
    while(motor.getTachoCount() < 11)
    {
   
    }
    motor.controlMotor(movementSpeed, 3);
    Thread.sleep(200);
    motor.resetTachoCount();
 }
 
 public void chooseBlue() throws Exception
 {
  if(recipientID == 2) rotationValue = 23;                   
   else if(recipientID == 3) rotationValue = 57;
    else rotationValue = 0;
 
   motor.controlMotor(movementSpeed, 1);
   while(motor.getTachoCount() < rotationValue)
   {
     
   }
   motor.controlMotor(movementSpeed, 3);
   Thread.sleep(200);
   motor.resetTachoCount();


  recipientID = 1;
 }
 
 public void chooseOrder() throws Exception
 {
    if(recipientID == 1) rotationValue = -23;
    else if(recipientID == 3) rotationValue = 34;
     else rotationValue = 0;
   
    if(rotationValue == -23) {
                              motor.controlMotor(movementSpeed, 2);
                              while(motor.getTachoCount() > rotationValue)
                              {
       
                              }
                              motor.controlMotor(movementSpeed, 3);
                             }
    else if(rotationValue == 34) {
                                  motor.controlMotor(movementSpeed, 1);
                                  while(motor.getTachoCount() < rotationValue)
                                  {
         
                                  }
                                  motor.controlMotor(movementSpeed, 3); 
                                 }
    Thread.sleep(200);
    motor.resetTachoCount();

   recipientID = 2;
 }
 
 public void choosePink() throws Exception
 {
    if(recipientID == 1) rotationValue = -57;         
    else if(recipientID == 2) rotationValue = -34;   
     else rotationValue = 0;
   
    motor.controlMotor(movementSpeed, 2);
    while(motor.getTachoCount() > rotationValue)
    {
       
    }
    motor.controlMotor(movementSpeed, 3);
    Thread.sleep(200);
    motor.resetTachoCount();

   recipientID = 3;
 }
}


Controller5

Code: Select all
import lejos.nxt.Button;
import lejos.nxt.LCD;

public class Controller5
{
   public static void main(String[] args) throws Exception
   {
    RecipientChoser recipientChoser = new RecipientChoser();
    recipientChoser.calibrate();
   
    while(true)
    {
    recipientChoser.choosePink();
    LCD.drawString("Pink chosen", 0, 0);
    while(!Button.ENTER.isPressed())
    {
           
    }
    Button.ENTER.waitForPressAndRelease();
         
       
    recipientChoser.chooseBlue();
    LCD.drawString("Blue chosen", 0, 2);
    while(!Button.ENTER.isPressed())
    {
       
    }
    Button.ENTER.waitForPressAndRelease();
   
    recipientChoser.chooseOrder();
    LCD.drawString("Order chosen", 0, 4);
    while(!Button.ENTER.isPressed())
    {
     
    }
    Button.ENTER.waitForPressAndRelease();
       
   
    LCD.clear();   
    }

   }

}

------------------------------------------------------------------------------------
Problem: If I only switch between 2....the rotations will be the same. But if I switch between 3....the rotations are not the same.....each turn (3 switches) I'm losing some degrees (about 10-15). And the problem is that the motor will miss the recipients.
------------------------------------------------------------------------------------

Using NXTRegulatedMotor

I have 2 classes: RecipientChoser4 and Controller5 (has main method) .

RecipientChoser4

Code: Select all
import lejos.nxt.NXTRegulatedMotor;
import lejos.nxt.Motor;

public class RecipientChoser4
{
 private NXTRegulatedMotor motor = Motor.B;
 private int recipientID = 2;
 private int rotationValue;
 
 public RecipientChoser4()
 {
  Motor.B.setSpeed(200); 
 }
 
 public void resetTachoCount()
 {
    motor.resetTachoCount();
 }
 public int getTachoCount()
 {
    return motor.getTachoCount();
 }
 
 public void calibrate() throws Exception
 {
    motor.rotateTo(20);
    Thread.sleep(200);
    motor.resetTachoCount();
 }
 
 public void chooseBlue() throws Exception
 {
  if(recipientID == 2) rotationValue = 23;                   
   else if(recipientID == 3) rotationValue = 66;
    else rotationValue = 0;
 
   motor.rotateTo(rotationValue);
   Thread.sleep(200);
   motor.resetTachoCount();


  recipientID = 1;
 }
 
 public void chooseOrder() throws Exception
 {
    if(recipientID == 1) rotationValue = -23;
    else if(recipientID == 3) rotationValue = 43;
     else rotationValue = 0;
   
    if(rotationValue == -23) {
                              motor.rotateTo(rotationValue);
                             }
    else if(rotationValue == 43) {
                                  motor.rotateTo(rotationValue);
                                 }
    Thread.sleep(200);
    motor.resetTachoCount();

   recipientID = 2;
 }
 
 public void choosePink() throws Exception
 {
    if(recipientID == 1) rotationValue = -66;         
    else if(recipientID == 2) rotationValue = -43;   
     else rotationValue = 0;
   
   motor.rotateTo(rotationValue);
   Thread.sleep(200);
   motor.resetTachoCount();

   recipientID = 3;
 }
}




Controller5

Code: Select all
import lejos.nxt.Button;
import lejos.nxt.LCD;

public class Controller5
{
   public static void main(String[] args) throws Exception
   {
    RecipientChoser4 recipientChoser = new RecipientChoser4();
    recipientChoser.calibrate();
   
    while(true)
    {
    recipientChoser.choosePink();
    LCD.drawString("Pink chosen", 0, 0);
    while(!Button.ENTER.isPressed())
    {
           
    }
    Button.ENTER.waitForPressAndRelease();
         
       
    recipientChoser.chooseBlue();
    LCD.drawString("Blue chosen", 0, 2);
    while(!Button.ENTER.isPressed())
    {
       
    }
    Button.ENTER.waitForPressAndRelease();
   
    recipientChoser.chooseOrder();
    LCD.drawString("Order chosen", 0, 4);
    while(!Button.ENTER.isPressed())
    {
     
    }
    Button.ENTER.waitForPressAndRelease();
       
   
    LCD.clear();   
    }

   }

}

------------------------------------------------------------------------------------
Problem: None.
------------------------------------------------------------------------------------

What to do?

I don't think it's possible to use MotorPort, so I'll use NXTRegulatedMotor instead. Thank you!
aleXander
New User
 
Posts: 4
Joined: Wed Dec 07, 2011 6:50 pm

Re: Motors problem

Postby gloomyandy » Wed Dec 14, 2011 11:24 am

Well perhaps you should talk to your teacher about this. It may be that by telling you not to use the NXTRegulatedMotor class she wants you to understand the problems associated with motor control and spend some time learning how to control the motor accurately...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3966
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Motors problem

Postby aleXander » Wed Dec 14, 2011 5:18 pm

gloomyandy wrote:Well perhaps you should talk to your teacher about this. It may be that by telling you not to use the NXTRegulatedMotor class she wants you to understand the problems associated with motor control and spend some time learning how to control the motor accurately...

Andy


I think you're right. But it already took me 2 days to figure out where I'm losing degrees and why. Tried to fix it, doing some corrections with the code. Won't work. Stayed 3 days on this problem. NXTRegulatedMotor is accurate and won't miss my recipients. I'm using it.

Thank you for the replies! :D
aleXander
New User
 
Posts: 4
Joined: Wed Dec 07, 2011 6:50 pm

Re: Motors problem

Postby gloomyandy » Wed Dec 14, 2011 5:37 pm

Well it is your choice, but I would still talk to your teacher about this... If the assignment told you not to use things like NXTRegulatedMotor then you may lose marks if you do use it...
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3966
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK


Return to NXJ Software

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 6 guests

more stuff