Problem with Ultrasonic sensor

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

Moderators: 99jonathan, roger, imaqine

Problem with Ultrasonic sensor

Postby quack » Tue Dec 25, 2007 7:23 pm

Hey! I'm doing a program for the NXT that makes it move when I send especific commands from my PC (with bluetooth).

The problem is that I wanted to use the ultrasonic sensor to make it avoid obstacles alone without intervention from the PC, but it doesn't work... the ultrasonic sensor keeps returning 255 of distance.

Here's the code so you can better understand the problem and help me:

Code: Select all
import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.io.*;

public class BTReceive
{
    private static UltrasonicSensor sonic = new UltrasonicSensor (SensorPort.S1);
   
    public static void main (String [] args) throws Exception
    {
        boolean connect = true;

        String connected = "Connected";
        String waiting = "Waiting...";
        String closing = "Closing...";
       
        String moveUp     = "Moving forward  ";
        String moveDown   = "Moving backwards";
        String moveRight  = "Turning right   ";
        String moveLeft   = "Turning left    ";
        String stop       = "Stop            ";
        String obstacle   = "Obstacle ahead  ";

        while (connect)
        {
            boolean run = true;
           
            LCD.drawString (waiting, 0, 0);
            LCD.refresh ();

            BTConnection btc = Bluetooth.waitForConnection ();

            LCD.clear ();
            LCD.drawString (connected, 0, 0);
            LCD.refresh ();   

            InputStream is = btc.openInputStream ();
            OutputStream os = btc.openOutputStream ();

            DataInputStream dis = new DataInputStream (is);
            DataOutputStream dos = new DataOutputStream (os);

            while (run)
            {
                int i = dis.readInt ();
   
                LCD.drawInt (i, 3, 0, 1);
                LCD.refresh ();
                LCD.drawInt (sonic.getDistance (), 3, 0, 4);
                LCD.refresh ();

                if (i == -1)
                {
                    run = false;
                    connect = false;

                    LCD.clear ();
                    LCD.drawString (closing, 0, 0);
                    LCD.refresh ();
                   
                    Motor.B.stop ();
                    Motor.C.stop ();

                    dos.writeInt (0);
                }
                else if (i == 1)
                {
                    if (sonic.getDistance () < 5)
                    {
                        LCD.drawString (obstacle, 0, 2);
                        LCD.refresh ();
                       
                        Motor.B.backward ();
                        Motor.C.backward ();
                       
                        Thread.sleep (10);
                       
                        Motor.B.stop ();
                        Motor.C.backward ();
                    }
                    else
                    {
                        LCD.drawString (moveUp, 0, 2);
                        LCD.refresh ();

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

                        dos.writeInt (1);
                    }
                }
                else if (i == 2)
                {
                    LCD.drawString (moveDown, 0, 2);
                    LCD.refresh ();

                    Motor.B.backward ();
                    Motor.C.backward ();

                    dos.writeInt (1);
                }
                else if (i == 3)
                {
                    if (sonic.getDistance () < 5)
                    {
                        LCD.drawString (obstacle, 0, 2);
                        LCD.refresh ();
                       
                        Motor.B.backward ();
                        Motor.C.backward ();
                       
                        Thread.sleep (10);
                       
                        Motor.B.stop ();
                        Motor.C.backward ();
                    }
                    else
                    {
                        LCD.drawString (moveRight, 0, 2);
                        LCD.refresh ();

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

                        dos.writeInt (1);
                    }
                }
                else if (i == 4)
                {
                    if (sonic.getDistance () < 5)
                    {
                        LCD.drawString (obstacle, 0, 2);
                        LCD.refresh ();
                       
                        Motor.B.backward ();
                        Motor.C.backward ();
                       
                        Thread.sleep (10);
                       
                        Motor.B.backward ();
                        Motor.C.stop ();
                    }
                    else
                    {
                        LCD.drawString (moveLeft, 0, 2);
                        LCD.refresh ();

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

                        dos.writeInt (1);
                    }
                }
                else if (i == 5)
                {
                    LCD.drawString (stop, 0, 2);
                    LCD.refresh ();

                    Motor.B.stop ();
                    Motor.C.stop ();

                    dos.writeInt (1);
                }

                dos.flush ();
            }

            dis.close ();
            dos.close ();
            btc.close ();
            LCD.clear ();
        }
    }
}


What should I do?

(sorry for my poor english)
quack
Novice
 
Posts: 26
Joined: Sun Jul 08, 2007 9:03 pm

Postby lawrie » Wed Dec 26, 2007 1:27 pm

Try putting dsome delays between the calls of getDistance().
lawrie
leJOS Team Member
 
Posts: 922
Joined: Mon Feb 05, 2007 1:27 pm

Postby bbagnall » Wed Jan 02, 2008 12:15 am

Further to what Lawrie said, the Ultrasonic sensor sends out a ping that travels at the speed of sound - pretty slow when compared to the speed of electricity. So you need to have a delay in there between readings because the ping takes a while to reflect off objects and return. I think our new code just returns the old value if you try to request this value too often.
User avatar
bbagnall
Site Admin
 
Posts: 392
Joined: Fri Aug 04, 2006 4:03 pm

Re: Problem with Ultrasonic sensor

Postby tc2357 » Mon Jan 07, 2008 8:31 pm

You also might also store the result of your first call to 'sonic.getDistance ()' in a local variable, then do your distance comparisons against the variable. i.e.- use just one call to getdistance on each iteration of your 'While(run)' event loop.
You might not need a delay in this case, as the 'motor' calls in your 'if .. then' statements could provide the needed delay.
tc2357
New User
 
Posts: 1
Joined: Fri Jul 06, 2007 4:35 am


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 3 guests

more stuff