Ultrasonic radar driver

The standard NXT kit comes with a set of sensors, divided in two categories : analog and digital sensors. As of February 2008, the only digital sensor official supported by Lego is the Ultrasonic radar. Apparently, another digital sensor, an RFID sensor, is currently being certified by Lego. Several other sensors made by Mindsensors are also digital (compass, radar, etc).

A digital sensor is a sensor that contains a integrated chip and that communicates with the NXT, as opposed to the analog sensors that just set the ANA line of the sensor plug to whatever 1 byte value they are currently perceiving. The communication with a digital sensors is based on an I2C bus.

Lego's radar is built as an I2C remote memory unit, and stores the measured values into this memory for the NXT to read. Several addresses in this memory are read/write and allows the driver to control the radar or change its settings. Note that we don't know yet if these areas are persistent across switch offs or not, but it's not really a big deal.

Radar hardware features

Radar's memory structure

How to use the radar driver

Using the radar driver in NxOS is very simple. High-level APIs are provided to interact with the radar, all I2C Memory and I2C logic behind handled automatically. First, you need to initialize the radar driver for the sensor port your radar's plugged on:

nx_radar_init(sensor);

If you want to make sure a radar is indeed plugged-in, and also check that this I2C peripheral is really a compatible radar, you can use the nx_radar_detect() function:

if (!nx_radar_detect(sensor)) {
  /* Error, ask the user, whatever. */
}

You can easily output to the screen the radar's current configuration (product ID, sensor type, measurement interval and units, etc) with nx_radar_info():

nx_display_clear();
nx_radar_info(sensor);

/* Wait until OK is pressed. */
while (nx_avr_get_button() != BUTTON_OK);

Finally, you can fetch the radar readings (that's all you really want from a radar I would say):

U8 readings[RADAR_MAX_OBJECT_NUMBER];
U8 i;

for (i=0; i<RADAR_MAX_OBJECT_NUMBER; i++)
  readings[i] = nx_radar_read_distance(sensor, i);

/* Do something clever (or not) with readings[]. */