AForge.NET

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

C# Lego NXT Ultrasonic Sensor with Aforge 2.2.5.0

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

C# Lego NXT Ultrasonic Sensor with Aforge 2.2.5.0

Postby fpisti35 » Thu May 15, 2014 8:48 am

Hi, I'm a beginner on Lego Robot but we have to program Lego NXT at university.

WE have to write a C# Form program which handing the Ultrasonic Sensor also.

My codes is up to now:

Code: Select all
        // sensor types
        private NXTBrick.SensorType[] sensorTypes = new NXTBrick.SensorType[] {
             NXTBrick.SensorType.Lowspeed9V };
       
        // sensor modes
        private NXTBrick.SensorMode[] sensorModes = new NXTBrick.SensorMode[] {
            NXTBrick.SensorMode.Raw };



and


Code: Select all
public bool GetUltrasonicSensorsValue(NXTBrick.Sensor sensor, out int value )
        {
            value = -1;

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

            int readyBytes = -1;

            for ( int i = 0; i < 10; i++ )
            {
                if (!nxt.LsGetStatus(NXTBrick.Sensor.First, out readyBytes))
                    return false;

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

                    if (!nxt.LsRead(NXTBrick.Sensor.First, readValues, out bytesRead))
                        return false;

                    value = readValues[0];

                    return true;
                }
            }
            return false;
        }


and I have a timer for handing

Code: Select all
        private void ultrasonicTimer_Tick(object sender, EventArgs e)
        {
            int iUltrasound;

            GetUltrasonicSensorsValue(NXTBrick.Sensor.First, out iUltrasound);

            lblDistance.Text = Convert.ToString(iUltrasound);

        }   



but they didn't work.

Anyone can help me why?

THX
Istvan
Last edited by fpisti35 on Thu May 15, 2014 11:13 am, edited 2 times in total.
fpisti35
 
Posts: 2
Joined: Thu May 15, 2014 8:41 am

Re: C# Ultrasonic Sensor 2.2.5.0

Postby fpisti35 » Thu May 15, 2014 11:10 am

I have solved :-)

The problem was, I had to setting SensorMode and Type after when I connected my brick.

Code: Select all
nxt.SetSensorMode(NXTBrick.Sensor.First, NXTBrick.SensorType.Lowspeed9V, NXTBrick.SensorMode.Raw);



My code is:

Code: Select all
  public bool GetUltrasonicSensorsValue(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))
                    return false;

                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];

                    iDistance = value; // the iDistance is a global variable

                    return true;
                }
            }
            return false;
        }


and timer with 500 interval

Code: Select all
        private void distanceTimer_Tick(object sender, EventArgs e)
        {
                //Distance check
                GetUltrasonicSensorsValue(NXTBrick.Sensor.First, out iDistance);
                lblDistance.Text = "Distance: " + Convert.ToString(iDistance);
        }   
fpisti35
 
Posts: 2
Joined: Thu May 15, 2014 8:41 am




Return to Robotics

cron