NXT Functions Sensors

From ROBOTC API Guide
Revision as of 20:25, 18 April 2012 by Bfeher (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


For more examples and explanations, head over to NXT Sensors Overview page!

For information and examples on using I2C sensors, head over to NXT I2C Digital Sensors page!


Color Key
Function:
Variable:


Information

The NXT is equipped with four sensor ports - S1, S2, S3, and S4. There are a variety of functions and variables used for configuring these ports and accessing their values. Configuring sensors can be complicated, to alleviate this ROBOTC has a built-in tool that can be used to configure the NXT sensors. Inside of this tool, you can do the following:
  • The variable name that you want to assign to the sensor. There are a number of “built-in” variable arrays that contain information about the sensors. The array index for these variable can be the sensor port values (i.e. S1, S2, S3, and S4) or even the numerical array index (i.e. 0 to 3). However, it is a good programming practice to assign a meaningful name (e.g. lightSensor, leftBump, rightBump, sonar, etc) to a sensor rather than one of these numerical indices.
  • The type of sensor that is involved. The NXT kit comes with several LEGO developed sensors – touch, sound, light, sonar – and there are many more 3rd party sensor types in development. The firmware must know the type of sensor so that it can properly configure the sensor – e.g. some sensors require optional 9V battery power and some do not – and know how to normalize the values received from the sensor.
  • The mode of the sensor. The most common mode is ‘percentage’ which will normalize the sensor value to a reading in the range 0 to 100. The next most common mode is ‘raw’ which simply returns the un-normalized value from the sensor. Other modes include “Celsius” and “Fahrenheit” which are used to specify the scale for RCX temperature sensors.


SensorRaw

word SensorRaw[tSensors sensor]
(word) This array value will return the "raw" (un-normalized) value of a sensor. Usually this is the raw A-D converted value, which is an analog value between 0 to 1023.


Parameter Explanation Data Type
sensor A sensor port or name tSensors


if(SensorRaw[Light2] > 512)  // if the Raw Value of the Light Sensor is greater than 512:
{
  nxtDisplayCenteredTextLine(3, "Light Sensor > 512");  // display text to the LCD screen
}


SensorType

TSensorTypes SensorType[tSensors sensor]
(TSensorTypes) The SensorType array is used to specify what type of sensor is connected to a certain port. Most users should not have to use this functionality and should use the Motors and Sensor Setup instead.


Parameter Explanation Data Type
sensor A sensor port or name tSensors


SensorType[sonarSensor] = sensorSonar;  // set 'SonarSensor' to be of type sensorSonar (the Motors and 
                                        // sensors Setup screen does this for you in the background)


SensorValue

word SensorValue[tSensors sensor]
(word) This array value returns the value of the sensor in a normalized fashion. Rather than returning a raw value of 0 to 1023, ROBOTC will interpret the data from the "SensorType" and return a more accurate representation of the sensor's data. An example of this is the Light Sensor, which will return a percentage value from 0 to 100.


Parameter Explanation Data Type
sensor A sensor port or name tSensors


while(true)  // infinite loop:
{
  nxtDisplayCenteredTextLine(3, "Sensor Value: %d", SensorValue[sonarSensor]);  // display "Sensor Value: ##"
  wait1Msec(100);  // Wait 100 milliseconds to help display correctly
}