lejos.nxt
Class UltrasonicSensor

java.lang.Object
  extended by lejos.nxt.I2CSensor
      extended by lejos.nxt.UltrasonicSensor
All Implemented Interfaces:
SensorConstants, RangeFinder

public class UltrasonicSensor
extends I2CSensor
implements RangeFinder

Abstraction for a NXT Ultrasonic Sensor.


Field Summary
 
Fields inherited from class lejos.nxt.I2CSensor
PRODUCT_ID, SENSOR_TYPE, VERSION
 
Fields inherited from interface lejos.nxt.SensorConstants
MODE_ANGLESTEP, MODE_BOOLEAN, MODE_CELSIUS, MODE_FARENHEIT, MODE_PCTFULLSCALE, MODE_PERIODCOUNTER, MODE_RAW, MODE_TRANSITIONCNT, TYPE_ANGLE, TYPE_COLORBLUE, TYPE_COLORFULL, TYPE_COLORGREEN, TYPE_COLORNONE, TYPE_COLORRED, TYPE_CUSTOM, TYPE_HISPEED, TYPE_LIGHT_ACTIVE, TYPE_LIGHT_INACTIVE, TYPE_LOWSPEED, TYPE_LOWSPEED_9V, TYPE_NO_SENSOR, TYPE_REFLECTION, TYPE_SOUND_DB, TYPE_SOUND_DBA, TYPE_SWITCH, TYPE_TEMPERATURE
 
Constructor Summary
UltrasonicSensor(I2CPort port)
           
 
Method Summary
 int capture()
          Set capture mode Set the sensor into capture mode.
 int continuous()
          Switch to continuous ping mode.
 int getCalibrationData(byte[] data)
          Return 3 bytes of calibration data.
 byte getContinuousInterval()
          Return the interval used in continuous mode.
 int getData(int register, byte[] buf, int len)
          Executes an I2C read transaction and waits for the result.
 int getDistance()
          Return distance to an object.
 int getDistances(int[] dist)
          Return an array of 8 echo distances.
 int getFactoryData(byte[] data)
          Return 10 bytes of factory calibration data.
 byte getMode()
          Returns the current operating mode of the sensor.
 float getRange()
          Get the range to the nearest object
 String getUnits()
          Return a string indicating the type of units in use by the unit.
 int off()
          Turn off the sensor.
 int ping()
          Send a single ping.
 int reset()
          Reset the device Performs a "soft reset" of the device.
 int sendData(int register, byte[] buf, int len)
          Executes an I2C write transaction.
 int setCalibrationData(byte[] data)
          Set 3 bytes of calibration data.
 int setContinuousInterval(byte interval)
          Set the ping inetrval used when in continuous mode.
 
Methods inherited from class lejos.nxt.I2CSensor
fetchString, getPort, getProductID, getSensorType, getVersion, sendData, setAddress
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UltrasonicSensor

public UltrasonicSensor(I2CPort port)
Method Detail

getData

public int getData(int register,
                   byte[] buf,
                   int len)
Description copied from class: I2CSensor
Executes an I2C read transaction and waits for the result.

Overrides:
getData in class I2CSensor
Parameters:
register - I2C register, e.g 0x41
buf - Buffer to return data
len - Length of the return data
Returns:
status == 0 success, != 0 failure

sendData

public int sendData(int register,
                    byte[] buf,
                    int len)
Description copied from class: I2CSensor
Executes an I2C write transaction.

Overrides:
sendData in class I2CSensor
Parameters:
register - I2C register, e.g 0x42
buf - Buffer containing data to send
len - Length of data to send
Returns:
status zero=success, non-zero=failure

getDistance

public int getDistance()
Return distance to an object. To ensure that the data returned is valid this method may have to wait a short while for the distance data to become available.

Returns:
distance or 255 if no object in range

getRange

public float getRange()
Get the range to the nearest object

Specified by:
getRange in interface RangeFinder
Returns:
the distance to the nearest object

getDistances

public int getDistances(int[] dist)
Return an array of 8 echo distances. These are generated when using ping mode. A value of 255 indicates that no echo was obtained. The array must contain at least 8 elements, if not -1 is returned. If the distnace data is not yet available the method will wait until it is.

Returns:
0 if ok <> 0 otherwise

ping

public int ping()
Send a single ping. The sensor operates in two modes, continuous and ping. When in continuous mode the sensor sends out pings as often as it can and the most recently obtained result is available via a call to getDistance. When in ping mode a ping is only transmitted when a call is made to this method. This sends a single ping and up to 8 echoes are captured. These may be read by making a call to getDistance and passing a suitable array. A delay of approximately 20ms is required between the call to ping and obtaining the results. The getDistance call automatically takes care of this. The normal getDistance call may also be used with ping, returning information for the first echo. Calling this method will disable the default continuous mode, to switch back to continuous mode call continuous.

Returns:
0 if ok <> 0 otherwise

continuous

public int continuous()
Switch to continuous ping mode. This method enables continuous ping and capture mode. This is the default operating mode of the sensor. Please the notes for ping for more details.

Returns:
0 if ok <> 0 otherwise

off

public int off()
Turn off the sensor. This call disables the sensor. No pings will be issued after this call, until either ping, continuous or reset is called.

Returns:
0 if ok <> 0 otherwise

capture

public int capture()
Set capture mode Set the sensor into capture mode. The Lego documentation states: "Within this mode the sensor will measure whether any other ultrasonic sensors are within the vicinity. With this information a program can evaluate when it is best to make a new measurement which will not conflict with other ultrasonic sensors." I have no way of testing this. Perhaps someone with a second NXT could check it out!

Returns:
0 if ok <> 0 otherwise

reset

public int reset()
Reset the device Performs a "soft reset" of the device. Restores things to the default state. Following this call the sensor will be operating in continuous mode.

Returns:
0 if ok <> 0 otherwise

getFactoryData

public int getFactoryData(byte[] data)
Return 10 bytes of factory calibration data. The bytes are as follows data[0] : Factory zero (cal1) data[1] : Factory scale factor (cal2) data[2] : Factory scale divisor.

Returns:
0 if ok <> 0 otherwise

getUnits

public String getUnits()
Return a string indicating the type of units in use by the unit. The default response is 10E-2m indicating centimetres in use.

Returns:
7 byte string

getCalibrationData

public int getCalibrationData(byte[] data)
Return 3 bytes of calibration data. The bytes are as follows data[0] : zero (cal1) data[1] : scale factor (cal2) data[2] : scale divisor.

Returns:
0 if ok <> 0 otherwise

setCalibrationData

public int setCalibrationData(byte[] data)
Set 3 bytes of calibration data. The bytes are as follows data[0] : zero (cal1) data[1] : scale factor (cal2) data[2] : scale divisor. This does not currently seem to work.

Returns:
0 if ok <> 0 otherwise

getContinuousInterval

public byte getContinuousInterval()
Return the interval used in continuous mode. This seems to be in the range 1-15. It can be read and set. However tests seem to show it has no effect. Others have reported that this does vary the ping interval (when used in other implementations). Please report any new results.

Returns:
-1 if error otherwise the interval

setContinuousInterval

public int setContinuousInterval(byte interval)
Set the ping inetrval used when in continuous mode. See getContinuousInterval for more details.

Returns:
0 if 0k <> 0 otherwise.

getMode

public byte getMode()
Returns the current operating mode of the sensor. 0 : sensor is off 1 : Single shot ping mode 2 : continuous ping mode (default) 3 : Event capture mode

Returns:
-1 if error otherwise the operating mode