:: AForge.NET Framework :: Articles :: Forums ::

Ultrasonic Sensor

The forum is to discuss topics related to robotics, like building robot, controlling it, its software and hardware, etc.

Ultrasonic Sensor

Postby adan0001 » Sat Feb 23, 2013 1:55 am

Hi !

I'm trying to use an Ultrasonic Sensor for NXT, unafortunally the only value that I receive is -1,

I initialize the sensor when connecting to NXT :
Code: Select all
nxt.SetSensorMode(NXTBrick.Sensor.First, NXTBrick.SensorType.LightActive, NXTBrick.SensorMode.Raw);
            nxt.SetSensorMode(NXTBrick.Sensor.Second, NXTBrick.SensorType.LightActive, NXTBrick.SensorMode.Raw);
            nxt.SetSensorMode(NXTBrick.Sensor.Third, NXTBrick.SensorType.Lowspeed9V, NXTBrick.SensorMode.Raw);

Then in order to get the sensor values I click on a button that executes the following code:
Code: Select all
private void button1_Click(object sender, EventArgs e)
            //output vars
            NXTBrick.SensorValues sensor1, sensor2;
            int ultrasound;

            nxt.GetSensorValue(NXTBrick.Sensor.First, out sensor1);
            nxt.GetSensorValue(NXTBrick.Sensor.Second, out sensor2);
            nxt.GetUltrasonicSensorsValue(NXTBrick.Sensor.Third, out ultrasound);

            MessageBox.Show(sensor1.Raw.ToString() + " " + sensor2.Raw.ToString()+" "+ultrasound.ToString());

What I get Is something like 560 540 -1, as you see the last number is the ultrasonic sensor value,

Using NXT Test, it says that my NXT firmware is 1.3, protocol 1.2, lejos 0.9.1,and using Port 3 with sensortype lowspeed9v and raw mode ,throws:

does someone know if my error is in my code, or in the library? does I need to use lswrite, lsread to get the real values from sensor?
Last edited by adan0001 on Thu Feb 28, 2013 10:25 pm, edited 1 time in total.
Posts: 3
Joined: Sat Feb 23, 2013 1:32 am

Re: Ultrasonic Sensor

Postby adan0001 » Thu Feb 28, 2013 9:44 pm

Well I reimplemented te GetUltrasonicvalues as follows:
Code: Select all
public bool GetUltrasonicSensorsValue2(NXTBrick.Sensor sensor, out int value)
            value = -1;

            // request distance value
            if (!nxt.LsWrite(sensor, new byte[] { 0x02, 0x42 }, 1))
                return false;

            int readyBytes = -1;

            for (int i = 0; i < 10; i++)
                if (!nxt.LsGetStatus(sensor, out readyBytes))   //<-------RETURNS 0 TO readyBytes
                    return false;

                out_box.Text += " "+readyBytes+" ";

                if (readyBytes >= 1)//<!----------------------
                    // read from I2C device
                    byte[] readValues = new byte[1];
                    int bytesRead;

                    if (!nxt.LsRead(sensor, readValues, out bytesRead))
                        return false;
                    value = readValues[0];

                    return true;
            }//FOR END
            return false;
        }//end getusvalues

for any reason nxt.LsGetStatus(sensor, out readyBytes) throws 0 .....
Posts: 3
Joined: Sat Feb 23, 2013 1:32 am

Re: Ultrasonic Sensor

Postby adan0001 » Mon Mar 04, 2013 5:07 am

Solved by reinstalling NXT firmware, unafortunly now I have to calibrate the motor powers for my functions up down left right...

For some reason if I set the motor powers to 20% nothing happens unti 60% of motor power,

Another issue is that if I send a command like motorpower to 60 not always work, I dont know if its a bluetooth interference or a error on the firmware.

Maybe I shoul set back the lejOS firmware...

Help is still needed :)
The first one is how to use the ultrasonic sensor using lejos,
the second one petition is how to send the commands to NXT without errors or have an stable communication...?
The third one Aforge was designed to use certain NXT firmware?
Posts: 3
Joined: Sat Feb 23, 2013 1:32 am

Return to Robotics