NQC Overview Sensors Contents | Master Index
 CyberMaster   RCX   RCX2   Scout 

FUNCTIONS / COMMANDS
 
          SetSensor()  Set the type and mode of the given sensor to the specified configuration.
          SetSensorType()  Set a sensor's type.
          SetSensorMode()  Set a sensor's mode.
          ClearSensor()  Clear the value of a sensor.
          SetSensorLowerLimit()  Set the light sensor's lower limit.
          SetSensorUpperLimit()  Set the light sensor's upper limit.
          SetSensorHysteresis()  Set the light sensor's hysteresis.
          CalibrateSensor()  Reads the current value of the light sensor.

VALUES / QUERIES
 
          SensorValue()  Returns the processed sensor reading for sensor n.
          SensorType()  Returns the configured type of sensor n.
          SensorMode()  Returns the current sensor mode for sensor n.
          SensorValueBool()  Returns the boolean value of sensor n.
          SensorValueRaw()  Returns the raw value of sensor n.

OVERVIEW
 
          There are three sensors, which internally are numbered 0, 1, and 2. This is potentially confusing since they are externally labeled as sensors 1, 2, and 3. To help mitigate this confusion, the sensor names SENSOR_1, SENSOR_2, and SENSOR_3 have been defined. These sensor names may be used in any function that requires a sensor as an argument. Furthermore, the names may also be used whenever a program wishes to read the current value of the sensor:
   x = SENSOR_1; // read sensor and store value in x 
Note that the sensor names can not be used for the sensor values, such as SensorValue. For all of these values the sensor must be specified by its sensor number (0, 1, or 2), and not a sensor name (e.g. SENSOR_1).
 
          The sensor ports on the RCX are capable of interfacing to a variety of different sensors (other targets don't support configurable sensor types). It is up to the program to tell the RCX what kind of sensor is attached to each port. A sensor's type may be configured by calling SetSensorType. There are four sensor types, each corresponding to a specific LEGO sensor. A fifth type (SENSOR_TYPE_NONE) can be used for reading the raw values of generic passive sensors. In general, a program should configure the type to match the actual sensor. If a sensor port is configured as the wrong type, the RCX may not be able to read it accurately.
 
Sensor Type Meaning
SENSOR_TYPE_NONE generic passive sensor
SENSOR_TYPE_TOUCH a touch sensor
SENSOR_TYPE_TEMPERATURE a temperature sensor
SENSOR_TYPE_LIGHT a light sensor
SENSOR_TYPE_ROTATION a rotation sensor

 
          Both the RCX and CyberMaster allow a sensor to be configured in different modes. The sensor mode determines how a sensor's raw value is processed. Some modes only make sense for certain types of sensors, for example SENSOR_MODE_ROTATION is useful only with rotation sensors. The sensor mode can be set by calling SetSensorMode. The possible modes are shown below. Note that since CyberMaster does not support temperature or rotation sensors, the last three modes are restricted to the RCX only.
 
Sensor ModeMeaning
SENSOR_MODE_RAW raw value from 0 to 1023
SENSOR_MODE_BOOL boolean value (0 or 1)
SENSOR_MODE_EDGE counts number of boolean transitions
SENSOR_MODE_PULSE counts number of boolean periods
SENSOR_MODE_PERCENT value from 0 to 100
SENSOR_MODE_FAHRENHEIT degrees F - RCX only
SENSOR_MODE_CELSIUS degrees C - RCX only
SENSOR_MODE_ROTATION rotation (16 ticks per revolution) - RCX only

 
          When using the RCX, it is common to set both the type and mode at the same time. The SetSensor function makes this process a little easier by providing a single function to call and a set of standard type/mode combinations.
 
Sensor Configuration Type Mode
SENSOR_TOUCH SENSOR_TYPE_TOUCH SENSOR_MODE_BOOL
SENSOR_LIGHT SENSOR_TYPE_LIGHT SENSOR_MODE_PERCENT
SENSOR_ROTATION SENSOR_TYPE_ROTATION SENSOR_MODE_ROTATION
SENSOR_CELSIUS SENSOR_TYPE_TEMPERATURE SENSOR_MODE_CELSIUS
SENSOR_FAHRENHEIT SENSOR_TYPE_TEMPERATURE SENSOR_MODE_FAHRENHEIT
SENSOR_PULSE SENSOR_TYPE_TOUCH SENSOR_MODE_PULSE
SENSOR_EDGE SENSOR_TYPE_TOUCH SENSOR_MODE_EDGE

 
          The RCX provides a boolean conversion for all sensors - not just touch sensors. This boolean conversion is normally based on preset thresholds for the raw value.
  • A "low" value (less than 460) is a boolean value of 1.  
  • A high value (greater than 562) is a boolean value of 0.  
This conversion can be modified: a slope value between 0 and 31 may be added to a sensor's mode when calling SetSensorMode. If the sensor's value changes more than the slope value during a certain time (3ms), then the sensor's boolean state will change.  This allows the boolean state to reflect rapid changes in the raw value.  
  • A rapid increase will result in a boolean value of 0.
  • A rapid decrease is a boolean value of 1.
Even when a sensor is configured for some other mode (i.e. SENSOR_MODE_PERCENT), the boolean conversion will still be carried out.
 
          On the Scout, SENSOR_3 refers to the built-in light sensor. Reading the light sensor's value (with SENSOR_3) will return one of three levels: 0 (dark), 1 (normal), or 2 (bright). The sensor's raw value can be read with SensorValueRaw(SENSOR_3), but bear in mind that brighter light will result in a lower raw value. The conversion of the sensor's raw value (between 0 and 1023) to one of the three levels depends on three parameters: lower limit, upper limit, and hysteresis. The lower limit is the smallest (brightest) raw value that is still considered normal. Values below the lower limit will be considered bright. The upper limit is the largest (darkest) raw value that is considered normal. Values about this limit are considered dark. Hysteresis can be used to prevent the level from changing when the raw value hovers near one of the limits. This is accomplished by making it a little harder to leave the dark and bright states than it is to enter them. Specifically, the limit for moving from normal to bright will be a little lower than the limit for moving from bright back to normal. The difference between these two limits is the amount of hysteresis. A symmetrical case holds for the transition between normal and dark.

 
FUNCTIONS / COMMANDS
 CyberMaster   RCX   RCX2   Scout 

         SetSensor(sensor, configuration) Overview | Top
         
Set the type and mode of the given sensor to the specified configuration, which must be a special constant containing both type and mode information.
SetSensor(SENSOR_1, SENSOR_TOUCH);


         SetSensorType(sensor, type) Overview | Top
         
Set a sensor's type, which must be one of the predefined sensor type constants.
SetSensorType(SENSOR_1, SENSOR_TYPE_TOUCH);


         SetSensorMode(sensor, mode) Overview | Top
         
Set a sensor's mode, which should be one of the predefined sensor mode constants. A slope parameter for boolean conversion, if desired, may be added to the mode (RCX only).
SetSensorMode(SENSOR_1, SENSOR_MODE_RAW); // raw mode

SetSensorMode(SENSOR_1, SENSOR_MODE_RAW + 10); // slope 10

         ClearSensor(sensor) Overview | Top
         
Clear the value of a sensor - only affects sensors that are configured to measure a cumulative quantity such as rotation or a pulse count.
ClearSensor(SENSOR_1);


         SetSensorLowerLimit(value) Overview | Top
         
Set the light sensor's lower limit. Value may be any expression.
SetSensorLowerLimit(100);


         SetSensorUpperLimit(value) Overview | Top
         
Set the light sensor's upper limit. Value may be any expression.
SetSensorUpperLimit(900);


         SetSensorHysteresis(value) Overview | Top
         
Set the light sensor's hysteresis. Value may be any expression.
SetSensorHysteresis(20);


         CalibrateSensor() Overview | Top
         
Reads the current value of the light sensor, then sets the upper and lower limits to 12.5% above and below the current reading, and sets the hysteresis to 3.12% of the reading.
CalibrateSensor();



 
VALUES / QUERIES
 CyberMaster   RCX   RCX2   Scout 

         SensorValue(n) Overview | Top
         
Returns the processed sensor reading for sensor n, where n is 0, 1, or 2. This is the same value that is returned by the sensor names (e.g. SENSOR_1).
x = SensorValue(0); // read sensor 1


         SensorType(n) Overview | Top
         
Returns the configured type of sensor n, which must be 0, 1, or 2. Only the RCX has configurable sensors types, other targets will always return the pre-configured type of the sensor.
x = SensorType(0);


         SensorMode(n) Overview | Top
         
Returns the current sensor mode for sensor n, which must be 0, 1, or 2.
x = SensorMode(0);


         SensorValueBool(n) Overview | Top
         
Returns the boolean value of sensor n, which must be 0, 1, or 2. Boolean conversion is either done based on preset cutoffs, or a slope parameter specified by calling SetSensorMode.
x = SensorValueBool(0);


         SensorValueRaw(n) Overview | Top
         
Returns the raw value of sensor n, which must be 0, 1, or 2. Raw values may range from 0 to 1023.
x = SensorValueRaw(0);


Comments