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. RobotC has a built-in wizard that can be used to configure the NXT sensors. The wizard contains a number of PC windows that allow you to set the following fields for the sensor:
· 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 enum 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 temperature sensors.
SensorType[]
This red/write variable is used to configure the type of a sensor (touch, light, sonar, etc)
SensorMode[]
This red/write variable is used to configure the mode of a sensor.
.
SensorValue[]
This variable contains the current normalized value of the sensor.
SensorRaw[]
This variable contains the current un-normalized value of the sensor.
The NXT firmware contains built-in device drivers for the LEGO supplied sensors. However, there are several suppliers of 3rd party developed sensors that are compatible with the NXT. Ideally these can be used in a user application program transparently to the user; the developer of the sensor will provide a “device driver” program for their sensor and RobotC will automatically recognize all the vendor supplied ‘device drivers” when it starts up and automatically extend the list of available sensor types. RobotC will support this ideal situation in an upgrade later this fall, but for now a little manual effort (i.e. a few lines of code) will be required by the end user. The basic concept is as follows:
· The device driver for the third party sensor will be a separate RobotC task (sometimes called a ‘thread) that executes in parallel with the end user’s program. The driver thread will provide all the support for the new sensor. Normally the driver thread will initialize the 3rd party sensor and then begin to continuously poll the sensor value.
· To facilitate implementation of 3rd party drivers the variables xx and xx are now read/write (vs read only) variables. This allows the driver task to update these fields with the results of the polling results.
· The end user application program can simply access the xx and xx variables as they would normally for a LEGO sensor whose drivers are integrated into the firmware. It will be nearly transparent to the end user program that it is using a 3rd party sensor rather than a LEGO sensor!
· A couple of new built-in variables have been added to the firmware to support 3rd party sensors. These are described below.
SensorSubType[]
This variable contains the ‘sub-type” for the indicated sensor. The main type may be assigned as “custom” which indicates that this is a 3rd party sensor with its own device driver. The custom type will tell the firmware to ignore the ‘built-in’ drivers for this sensor and that the fields xx and xx will be updated by the 3rd party driver task.
nPSControl[]
This variable is used by the wireless Playstation Controller Interface sensor developed by MindSensors. It is used to hold the values for the various joysticks and buttons on the PS controller.