Sensors

Lego_nxt_ev3_sensors

Before compiling the following programs, readers must follow the steps shown in the “Hardware Interaction” section.

Sensors are acceded using file operations that operate on special device files. In order to access the sensor data, the user must perform a few steps that involve opening the device, mapping the device data in user space, and if needed, configuring the device.
Opening and mapping the device is done in pretty much the same way for all types of sensors. The following examples cover some typical cases:

Serial Interface
Analog Sensors
Encoder Sensors
Simple I2C Interface

At startup, a sensor will be configured to work using default configuration parameters, however users may want to change the sampling frequency, pick a different mode of operation, increase the number of bytes in the data packet, or send direct commands to the sensor. This is done using input/output control functions (ioctl). The use of this function requires some understanding of the sensor capabilities. The following examples show the use of configuration functions:

I2C Interface (including third party devices)
IR Remote Control
Color Sensor

Prev >> Motors
Next >> User Interface

4 Responses to Sensors
  1. Stephan Korsholm says:

    In the serial interface example, the line

    (unsigned char)pUart->Raw[PORT][pUart->Actual[PORT]][0]

    Only prints the least significant byte of the sensor value. The 3 least significant bits of (unsigned char)pUart->Raw[PORT][pUart->Actual[PORT]][1] contains the most significant bits of the reading.

    As a consequence the sensor value reported by the example is only valid for short distances (approx less than 20 cm).

  2. playest says:

    Using “pAnalog->Pin6[port][pAnalog->Actual[port]]” for the touch sensor seems to be kind of random. The range of value seems to be pretty random, do you know any reliable way of dealing with this sensor?

    • playest says:

      Ok, I found the solution don’t cast the value to (unsigned char) it’s typed as a DATA16 so cast it to (signed short) or something like that and it will be fine.