Finding an object with Ultrasonic Sensor

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

Moderators: 99jonathan, roger, imaqine

Finding an object with Ultrasonic Sensor

Postby Aelskao » Sat Nov 24, 2012 9:20 pm

Hi !
I have some difficulties to find an approach an object with the ultrasonic sensor. I've made a little code to perform that, but it doesn't working. Here is it :

Code: Select all
import lejos.nxt.*;
import lejos.robotics.navigation.DifferentialPilot;

public class AlgoFindBall
{
    public static void main(String[] args){
        DifferentialPilot pilote = new DifferentialPilot(5.5, 17, Motor.A, Motor.B);
        UltrasonicSensor sonar = new UltrasonicSensor(SensorPort.S1);
        while(sonar.getDistance() > 50){
            pilote.travelArc(20,10,true);
            pilote.travelArc(-20,10,true);
        }
    }
}


It actually does nothing. I think it's coming from the immediatereturn. When I do a single
Code: Select all
DifferentialPilot pilote = new DifferentialPilot(5.5, 17, Motor.A, Motor.B);
pilote.travelArc(20,10,true);
,
nothing happens. But, if I remove the "true" (immediatereturn), Motor are well going forward().
Is there a way to keep the immediatereturn or to perform this task (Finding an object) ? Have I done something wrong ?
Thanks for your help !
(And sorry for my horrible english ! :mrgreen: )
Aelskao
New User
 
Posts: 2
Joined: Sat Nov 24, 2012 9:07 pm

Re: Finding an object with Ultrasonic Sensor

Postby Aswin » Sat Nov 24, 2012 11:37 pm

Hi,

The problem is with the while loop. This goes very fast, meaning your motors get mixed orders and don't have time to respond. I think you want the robot to complete a rotate before it goes back, and back, etc. Only when your sensor detects an object, the you want to stop the motors.

You use the immediate return. This is good as otherwise you would not be able to scan while turning. So you need a way to find out if a sweep is completed to go back. The pilot class has a method that tells you that. I think it is called isStopped(). You can then reverse direction and issue a new TravelArc method. In the mean time you can keep scanning the surrounding.

There is also a range finder that could help you. This one is documented.
My NXT blog: http://nxttime.wordpress.com/
Aswin
leJOS Team Member
 
Posts: 206
Joined: Tue Apr 26, 2011 9:18 pm
Location: Netherlands

Re: Finding an object with Ultrasonic Sensor

Postby Aelskao » Tue Nov 27, 2012 4:20 pm

Thank you very much for the answer.

I've tried an "if(!pilote.isMoving())" but this condition never applies. If I put a "pilote.stop()" before, the immediateReturn makes the robot always stopping. The only solution I've found was to put a "Thread.sleep(5000)", but it's not an elegant method ... Another suggestion ?
Aelskao
New User
 
Posts: 2
Joined: Sat Nov 24, 2012 9:07 pm

Re: Finding an object with Ultrasonic Sensor

Postby skoehler » Tue Nov 27, 2012 5:48 pm

This might work (untested)

Code: Select all
public class AlgoFindBall
{
    public static void main(String[] args){
        DifferentialPilot pilote = new DifferentialPilot(5.5, 17, Motor.A, Motor.B);
        UltrasonicSensor sonar = new UltrasonicSensor(SensorPort.S1);
        pilote.travelArc(20,10,true);
        while(pilote.isMoving()) {
            if (sonar.getDistance() <= 50) {
               pilote.stop();
               return;
            }
        }
        pilote.travelArc(-20,10,true);
        while(pilote.isMoving()) {
            if (sonar.getDistance() <= 50) {
               pilote.stop();
               return;
            }
        }
    }
}
skoehler
leJOS Team Member
 
Posts: 1446
Joined: Thu Oct 30, 2008 4:54 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff