![]() |
Sensors | Contents | Master Index |
CyberMaster | RCX | RCX2 | Scout |
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. |
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 xNote 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.
|
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.
|
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.
|
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.
|
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 |
|
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 | |||||||
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 |
|
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); | |||||||