Motor, setSpeed and constructor

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

Moderators: 99jonathan, roger, imaqine

Motor, setSpeed and constructor

Postby Ricoco » Thu Jan 19, 2012 4:29 pm

Hi !

I want to set the speed (max) of my motor once and for all my program.

The following code is not working :

import lejos.nxt.*;

public class MonRobot implements ButtonListener {

NXTRegulatedMotor motorB = Motor.B;
NXTRegulatedMotor motorC = Motor.C;


public void MonRobot() {
motorB.setSpeed(900);
motorC.setSpeed(900);
}


public void avance() {
/*Motor.B.setSpeed(900);
Motor.C.setSpeed(900);*/

Motor.B.forward();
Motor.C.forward();
}

public void recule() {
Motor.B.backward();
Motor.C.backward();
}

public void tourne() {
Motor.B.forward();
}

public void buttonPressed(Button b) {
}

public void buttonReleased(Button b) {
System.exit(0);
}

public static void main(String[] args) throws Exception {

MonRobot robot = new MonRobot();

Button.ESCAPE.addButtonListener(robot);

TouchSensor capteur = new TouchSensor(SensorPort.S1);

robot.avance();

while(true) {
if (capteur.isPressed()) {
robot.recule();
Thread.sleep(1000);
robot.tourne();
Thread.sleep(500);
robot.avance();
System.out.println("Sortie de boucle");
}
}

}

}

Sorry for the french in the code...

The red part doesn't give the behavior expected.

If i suppress comments, the green code is giving me the behavior i want.

Why ? Thx in advance.
Ricoco
New User
 
Posts: 2
Joined: Thu Jan 19, 2012 4:22 pm

Re: Motor, setSpeed and constructor

Postby Rickz2020 » Thu Jan 19, 2012 6:30 pm

Have you tried:

Code: Select all
public void MonRobot() {
motor.B.setSpeed(900);
motor.C.setSpeed(900);
}


?

You declare MotorB and MotorC but never use it, so setting the speed will have no effect. I think it's just a typo you've made :D
Last edited by Rickz2020 on Fri Jan 20, 2012 12:50 am, edited 2 times in total.
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: Motor, setSpeed and constructor

Postby gloomyandy » Thu Jan 19, 2012 7:22 pm

Hi,
Your code does not have a constructor it has a void method called MonRobot. Constructors should not have a type associated with them. Change the code to:
Code: Select all
public class MonRobot implements ButtonListener {

NXTRegulatedMotor motorB = Motor.B;
NXTRegulatedMotor motorC = Motor.C;

public MonRobot() {
motorB.setSpeed(900);
motorC.setSpeed(900);
}


Note the lack of void. The above works fine for me.... As the previous poster pointed out you would probably be better off using motorA and motorB in your code rather than Motor.A and Motor.B

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

Re: Motor, setSpeed and constructor

Postby Ricoco » Fri Jan 20, 2012 12:34 am

Thx you very much !

As you've seen, i've to improve my Java programming level (i haven't code since years).
Ricoco
New User
 
Posts: 2
Joined: Thu Jan 19, 2012 4:22 pm

Re: Motor, setSpeed and constructor

Postby gloomyandy » Fri Jan 20, 2012 12:50 am

Hi,
No problem, to be honest I'm a little surprised that this is actually allowed (though it does generate a warning in Eclipse)...

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


Return to NXJ Software

Who is online

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

cron
more stuff