ControlMotor alternative?

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

Moderators: 99jonathan, roger, imaqine

ControlMotor alternative?

Postby Ramacco » Sat Sep 28, 2013 8:39 pm

Hi all,

I have recently started using LeJOS for my NXT and EV3.
I went to a few example programs and found this:
Code: Select all
MotorPort.B.controlMotor(power, forward);

Nowadays, this function doesn't seem to be available anymore in LeJOS.I do know about the setSpeed method as shown below, but for some reason it does not work as I want to in my easy linefollowing program. The problem is that the robot stops very brutal always when it goes back in the loop. (as it sets the speed back to what it was supposed to be I guess).

The ControlMotor function would be a lot easier and be more efficient to use. Am I missing something here or is this the way it should be?

Code: Select all
package newNXTproject;

import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.Motor;
import lejos.nxt.LightSensor;
import lejos.nxt.SensorPort;

public class RobotDrive {
   public static void main (String args []){
      LightSensor light = new LightSensor(SensorPort.S3);

      // do nothing as long as the ENTER button is not pressed
      while(!Button.ENTER.isPressed()){
         LCD.clear(1);
         LCD.drawInt(light.getLightValue(), 1, 1);
      }
          
       // set startvalue
      int startValue = light.getLightValue();
      LCD.drawInt(startValue, 1, 1);
      
      // initialise motorspeed
      Motor.B.setSpeed(500);
      Motor.C.setSpeed(500);
      Motor.B.forward();
      Motor.C.forward();
      
      while (!Button.ESCAPE.isPressed()){
         LCD.clear(3);
         LCD.drawInt(light.getLightValue(), 1, 3);
         
         if(light.getLightValue() > startValue){
            Motor.B.setSpeed(20);
            Motor.C.setSpeed(200);
            Motor.B.forward();
            Motor.C.forward();
         }
         else {
            Motor.C.setSpeed(20);
            Motor.B.setSpeed(200);
            Motor.B.forward();
            Motor.C.forward();
         }
      }   
   }
}


Regards,
Ramacco
Ramacco
New User
 
Posts: 5
Joined: Wed Aug 28, 2013 8:54 am

Re: ControlMotor alternative?

Postby gloomyandy » Sat Sep 28, 2013 8:54 pm

You are mixing up two different ways to control a motor, one using the MotorPort allows you to set the power for the motor directly, the other using the Motor uses a much more sophisticated regulated motor interface. The second will constantly attempt to keep the speed at the value you have requested but your code will be making huge changes in the requested speed (from 20 degrees per second to 200 degrees per second). This will cause huge changes in the actual power being applied to the motor and you are also making these changes very rapidly. It looks like the control scheme you are using was intended to use the change the power mechanism which is available via the controlMotor method, what makes you think this is no longer available?
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3642
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: ControlMotor alternative?

Postby Ramacco » Sun Sep 29, 2013 10:59 am

Thank you Andy for your reply!

For some reason I got an error while compiling the controlMotor function, but it seems to be solved now.I can use it again after rebooting and it makes a lot more sense.

Thanks for the clear explanation about the difference between the 2 functions!
Ramacco
New User
 
Posts: 5
Joined: Wed Aug 28, 2013 8:54 am


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 3 guests

more stuff