Motors at the same time

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

Moderators: 99jonathan, roger, imaqine

Motors at the same time

Postby ale252 » Mon Dec 05, 2011 12:07 am

Hello,

I'm very new in programmation, but I'm tying.

I have a problem, I can Start the two motors at the same time with a while. He can stop when he is 20 cm from anything, but it doesn't stop the 2 motors at the same time, I have one stop et another stop. The robot do a little rotate.

Anybody know whats the problem?

I have this class:

Code: Select all
public class Moteur {

    public Moteur() {
    }

    public void avancer() {
        Motor.A.forward();
        Motor.B.forward();
       
    }

    public void reculer() {
        Motor.A.backward();
        Motor.B.backward();
    }
    public void tournerGauche(){
           Motor.A.rotate(320);
    }
   
        public void tournerDroite(){
           Motor.B.rotate(320);
           Motor.A.stop();
    }
}




And this other:
Code: Select all
public class HelloWorld {

    private static int dist = 20;

    public static void main(String[] args) {
        Moteur m = new Moteur();


        MotorPort a = MotorPort.A;
        UltrasonicSensor u1 = new UltrasonicSensor(SensorPort.S2);
        while (u1.getDistance() != dist) {
            m.avancer();

        }

        Motor.A.stop();
        Motor.B.stop();
        }
   

    }
}



Thanks very much for the help!
ale252
New User
 
Posts: 3
Joined: Sun Dec 04, 2011 11:59 pm

Re: Motors at the same time

Postby roger » Mon Dec 05, 2011 7:21 am

The difficulty is that
Code: Select all
    Motor.A.stop();
does not return immediately, and Motor.B continues to run for a short time.
To make them stop simultaneously, you use
Code: Select all
Motor.A.stop()true;
     Motor.B.stop();
In your main() method, I do not think that
MotorPort a = MotorPort.A;
does anything useful. And the repeated call to
m.avancer();
is not necessary and should be removed. Every time a call is made to a motor method, the motor regulator thread, which is trying to control the motor speed, is reset which prevents it from doing its job.
Cheers,
Roger
roger
Moderator
 
Posts: 367
Joined: Fri Jun 01, 2007 4:31 am
Location: Berkeley, CA

Re: Motors at the same time

Postby ale252 » Mon Dec 05, 2011 12:18 pm

Thank you very much, that help me!

Now it work correctly!
ale252
New User
 
Posts: 3
Joined: Sun Dec 04, 2011 11:59 pm

Re: Motors at the same time

Postby ale252 » Mon Dec 05, 2011 3:51 pm

Do you know how to do to rotate exactly 90°? Because using the commande

motor.rotate(320);

It's not very exactly.

Thanks
ale252
New User
 
Posts: 3
Joined: Sun Dec 04, 2011 11:59 pm

Re: Motors at the same time

Postby roger » Mon Dec 05, 2011 5:52 pm

Hi
Yes, an accurate rotation of the robot is not so easy to achieve. You could try other values of the motor rotation to see what works best.
Now that you understand how the Motor class works, look at the DifferentialPilot class. You can use it to make your robot to rotate, travel forward and backward, and trace an arc of a circle. You can use the immediate return flag in these methods and use the ultrasonic sensor to stop the robot if it detects an obstacle while the robot is moving.
Good luck,
Roger
roger
Moderator
 
Posts: 367
Joined: Fri Jun 01, 2007 4:31 am
Location: Berkeley, CA


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 4 guests

more stuff