Ohayo gozaimashita, Legoboto-san, NXT: come to your sensors (LEGO MINDSTORMS NXT)

In the previous post I offered some sample code for motor movement of the LEGO MINDSTORMS NXT product used with Bram Fokke's .NET API NXT#.

In this post I will give info about how to use the different types of sensors.

Out-of-the-box you get 4 different sensors.

  1. Pressure (button)
  2. Sound
  3. Light
  4. Sonar

Pressure, as the name says, measures how much pressure is applied. The API will pretty much treat it as a button.

Sound measures the level, the amplitude, of sound – or noise. It has two modes: a "human" mode, scaled to the human hearing, and a normal mode.

The Light sensor can find out for you if it is dark or not. It has an active (red) light you can turn on, with the reflections from that red light you can detect objects in darkness or you can tell the difference between the colors red or blue.

Sonar tells you the distance the sensor has to the next objects in the direction it is pointing. It operates with reflections from ultrasonic sound waves. Note that if you get too close you are actually blocking the sound getting out and it will not be reflected in a way that makes sense. So when you actually touch an object, it can jump to the maximum possible value suddenly.

But here goes the sample code:

using System;

using System.Collections.Generic;

using System.Text;

using Bram.NxtSharp;

 

namespace LegoNxtConsoleSample

{

    class Program

    {

        const string c_BluetoothComPort = "COM4";

 

        static void SensorDemo()

        {

            //sensors

            //the general use pattern of attaching sensors and

            //looping through sensors is similar and so not shown here

            int i = 0;

 

            NxtBrick brick;

            brick = new NxtBrick();

            brick.COMPortName = c_BluetoothComPort;

            brick.Connect();

 

 

            NxtPressureSensor pressureSensor = new NxtPressureSensor();

            pressureSensor.Brick = brick;

            brick.Sensor1 = pressureSensor;

            pressureSensor.InitSensor();

 

            NxtSoundSensor soundSensor = new NxtSoundSensor();

            soundSensor.Brick = brick;

            brick.Sensor2 = soundSensor;

            Console.WriteLine("sound sensor adjusted for human ear");

            soundSensor.AdjustForHumanEar = true;

            soundSensor.InitSensor();

            i = 0;

            while (i++ < 10)

            {

                soundSensor.Poll();

                Console.WriteLine("sound sensor raw value:" + soundSensor.Value);

                Console.WriteLine("sound sensor value:" + soundSensor.Value);

                System.Threading.Thread.Sleep(500);

            }

 

            Console.WriteLine("sound sensor not adjusted for human ear");

            soundSensor.AdjustForHumanEar = false;

            soundSensor.InitSensor();

            i = 0;

            while (i++ < 10)

            {

                soundSensor.Poll();

                Console.WriteLine("sound sensor raw value:" + soundSensor.RawValue);

                Console.WriteLine("sound sensor value:" + soundSensor.Value);

                System.Threading.Thread.Sleep(500);

            }

 

 

            //lightsensor used uninitialized

            NxtLightSensor lightSensor = new NxtLightSensor();

            lightSensor.Brick = brick;

            brick.Sensor3 = lightSensor;

 

 

            Console.WriteLine("raw lightsensor value before poll:" + lightSensor.RawValue);

            Console.WriteLine("lightsensor value before poll:" + lightSensor.Value);

            lightSensor.Poll();

            Console.WriteLine("raw lightsensor value:"+lightSensor.RawValue);

            //note that this behaves differently when "initialized"

            //but only when brick was "off" before or different sensor was

            //used

            Console.WriteLine("lightsensor value:" + lightSensor.Value);

 

            //initSensor is done at brick.Connect(), so usually attach sensors first,

            //then connect brick!

            lightSensor.InitSensor();

            Console.WriteLine("raw initialized lightsensor value before poll:" + lightSensor.RawValue);

            Console.WriteLine("initialized lightsensor value before poll:" + lightSensor.Value);

            lightSensor.Poll();

            Console.WriteLine("raw lightsensor value:" + lightSensor.RawValue);

            Console.WriteLine("initialized lightsensor value:" + lightSensor.Value);

 

 

            //turn on ligth on lightSensor, needs InitSensor for change of lightstatus

            lightSensor.Active = true;

            lightSensor.InitSensor();

            Console.WriteLine("raw active lightsensor value before poll:"

                              + lightSensor.RawValue);

            Console.WriteLine("lightsensor active value before poll:"

                + lightSensor.Value);

            lightSensor.Poll();

            Console.WriteLine("raw active lightsensor value:" + lightSensor.RawValue);

            Console.WriteLine("active lightsensor value:" + lightSensor.Value);

 

            //turnoff light

            lightSensor.Active = false;

            lightSensor.InitSensor();

 

            //autopolling lightsensor

            brick.AutoPoll = true;

            lightSensor.AutoPoll = true;

            lightSensor.AutoPollDelay = 1000;

            //note that usually you will set up

            //sensors and autopoll before first brick.Connect()

            //which will do StartPolling for you

            brick.StartPolling();

            while (i++ < 20)

            {

                //this should show that AutoPollDelay matters

                Console.WriteLine("Light sensor raw value:"+lightSensor.RawValue);

                System.Threading.Thread.Sleep(500);

            }

            brick.StopPolling();

 

 

            //sonar sensor measures distance

            NxtSonar sonarSensor = new NxtSonar();

            sonarSensor.Brick = brick;

            brick.Sensor4 = sonarSensor;

            sonarSensor.InitSensor();

            i = 0;

            while (i++ < 20)

            {

                sonarSensor.Poll();

                Console.WriteLine("Sonar sensor raw value:"+sonarSensor.RawValue);

                System.Threading.Thread.Sleep(500);

            }

 

 

            //disconnect Sensor2

            brick.Sensor2 = null;

            foreach(NxtSensorPort sensorPort in brick.SensorPorts())

            {

                Console.Write(sensorPort.ToString()+" ");

                if (brick.GetSensor(sensorPort) == null)

                    Console.WriteLine("is not connected");

                else

                    Console.WriteLine("is connected with a "

                        + brick.GetSensor(sensorPort).ToString());

            }

 

 

            foreach(NxtSensor sensor in brick.ConnectedSensors())

            {

                sensor.Poll();

                Console.WriteLine(sensor.GetType().Name + " is connected to port " + sensor.Port.ToString() + " and has value " + sensor.RawValue);

            }

            //of further interest could be:

            //sensor.LastResult of type NxtGetInputValues

 

 

            brick.Disconnect();

        }

 

        static void Main(string[] args)

        {

            SensorDemo();

        }

    }

}

 


Posted Apr 19 2007, 01:27 AM by Andreas Erben

Comments

Andreas Erben's posts wrote Legoboto: hajimemashou, LEGO MINDSTORMS NXT, sensor eventing
on 04-19-2007 1:50
In the previous posts I offered some sample code for motor movement and dealing with sensors of the LEGO
developers.de is a .Net Community Blog powered by daenet GmbH.