Forward command not working

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

Moderators: roger, gloomyandy, skoehler

poulabildgaard
New User
Posts: 15
Joined: Thu Jun 15, 2017 1:52 pm

Forward command not working

Postby poulabildgaard » Tue Oct 24, 2017 11:42 am

Hi
I'm using this code:

...
pilot.setLinearSpeed(80);

while(true){
pilot.forward();
distance.fetchSample(sample, 0);
if (sample[0]<50){
pilot.stop();
pilot.rotate(90);
}
}
...
but the robot makes a leap, then stops making it's way bit by bit very slowly, a couple of milimeters every second. When it detects an object it turns quickly and then slips back into crawling - mode. The robot works fine with travel - command.
I am using jdk 7 and jre 7 after having read some of the post concerning forward, but it does not work.
Thanks for any help.

Poul

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

Re: Forward command not working

Postby gloomyandy » Tue Oct 24, 2017 2:04 pm

What sort of pilot are you using?

What have you done to debug your problem?

Have you tried a very simple program that sets the speed and then uses forward followed by say a sleep of 20 seconds? Does your robot move as expected? Is the speed correct, what happens if you try different speeds?

It is never a good idea to constantly call something like forward (or travel) inside a loop when nothing is really changing. There is no need and doing so may have all sorts of side effects. Just call it once to start the move operation. So for instance your code would be better like this...

Code: Select all

pilot.setLinearSpeed(80);
pilot.forward();
while(true){
  distance.fetchSample(sample, 0);
  if (sample[0]<50){
    pilot.stop();
    pilot.rotate(90);
    pilot.forward();   
  }
}
leJOS news https://lejosnews.wordpress.com/

poulabildgaard
New User
Posts: 15
Joined: Thu Jun 15, 2017 1:52 pm

Re: Forward command not working

Postby poulabildgaard » Tue Oct 24, 2017 3:20 pm

Thanks a lot. It works now. But I am still confused of why it works. You only moved the forward-command out of the loop so what is it in the loop that confuses the proces? You need not answer, I am just happy you solved my problem.

Best regards Poul

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

Re: Forward command not working

Postby gloomyandy » Tue Oct 24, 2017 3:42 pm

Your code was calling forward many thousands of times a second, every time you do that it basically stops the current command (which was forward) and starts a new command (which will be forward!), this in turn generates new move events and will reset motor control parameters this is not a good idea. There is no simple test that says "am I already moving forward, if so do nothing", instead the code assumes that you are being sensible and will not be asking the pilot to do the same thing you have already asked it to do. Why not add that test? Well the Pilot API is not designed for continuous smooth movement, instead it uses a series of discrete move commands, most commands if you execute them multiple times do something different to if you execute them just the once (think of a rotate for example), so we basically take the same approach with every command, stop the old one, start the new one. This mechanism allows us to easily model the robot movement, to allow things likes the position and the orientation (the Pose) of the robot to be tracked. It works well for simple movements of LEGO robots.
leJOS news https://lejosnews.wordpress.com/


Return to “EV3 Software”

Who is online

Users browsing this forum: No registered users and 1 guest