help with sensor

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

help with sensor

Postby roozis » Thu May 05, 2011 11:11 pm

im tryin to make my robot move and sense an object and then when it gets near the object, wall it avoids so it doesnt crash.


this is the code that i have done, can any one tell whats wrong with it?
Code: Select all



public void jStart() {
        float fMaxSpeed = pilot.getMoveMaxSpeed();
        pilot.setMoveSpeed(fMaxSpeed);
        pilot.backward();

while(!pilot.isMoving())
    {

    int distance = UltrasonicSensor.getDistance();
    if (distance > 20)
        {
        pilot.backward();
        pilot.stop();
        pilot.rotate(-180);
        pilot.stop();
        pilot.backward();
       
        }
    }

    }

roozis
New User
 
Posts: 15
Joined: Sun Apr 17, 2011 2:43 pm

Re: help with sensor

Postby gloomyandy » Fri May 06, 2011 10:26 am

Hi,
Your program will never execute the code in the while loop. When the robot is moving pilot.isMoving will be true, so the while loop condition will not be met, you probably need to remove the "!". Secondly you collision detection code is probably the wrong way round. don't you want to take the avoiding action when you get close to an object? If so then the condition should be distance < 20 not distance > 20...

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

Re: help with sensor

Postby roozis » Fri May 06, 2011 10:55 am

I did as you told and it works the robot starts turning away from the object, now the robot just continues and it doesn't stop, is that because of the while loop? what can be done to make the robot stop after the turn? and also how can I make the robot pause before making a turn away from the object so then it can continue moving?
roozis
New User
 
Posts: 15
Joined: Sun Apr 17, 2011 2:43 pm

Re: help with sensor

Postby gloomyandy » Fri May 06, 2011 11:04 am

I'm not going to write your program for you. Try thinking about what it is you are telling your robot to do. You have told it to move backwards, but not for how far or how long, similarly you have called stop but then immediately told it to start agin by calling backwards. If you want the robot actually sit still for a period of time then you have to tell it to do that, similarly if you want your robot to move backwards for a period of time you need to tell it to move backwards then after the period of time tell it to stop. You may want to investigate the sleep method to do this...
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4118
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: help with sensor

Postby roozis » Fri May 06, 2011 11:26 am

im not asking you to write the code for just, im just trying to have a good understanding, but thanks for the information
roozis
New User
 
Posts: 15
Joined: Sun Apr 17, 2011 2:43 pm


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 0 guests

more stuff