Package lejos.hardware.sensor
Class NXTUltrasonicSensor
- java.lang.Object
-
- lejos.hardware.Device
-
- lejos.hardware.sensor.BaseSensor
-
- lejos.hardware.sensor.I2CSensor
-
- lejos.hardware.sensor.NXTUltrasonicSensor
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,SensorConstants,SensorMode,SensorModes,SampleProvider
public class NXTUltrasonicSensor extends I2CSensor
NXT Ultrasonic sensor
The NXT Ultrasonic sensor generates sound waves and reads their echoes to detect and measure distance from objects.Supported modes Mode name Description unit(s) Getter Distance Measures the distance to the nearest object in front of the sensor meter getDistanceMode()Ping Measures the distance to the nearest 8 objects in front of the sensor meter getPingMode()See Mindstorms NXT HDK/SDK See The leJOS sensor framework See
leJOS conventions for SampleProviders- Author:
- Aswin
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classNXTUltrasonicSensor.DistanceModeclassNXTUltrasonicSensor.PingMode
-
Field Summary
Fields Modifier and Type Field Description protected static byteMODE_CONTINUOUSprotected static byteMODE_OFFprotected static byteMODE_PING-
Fields inherited from class lejos.hardware.sensor.I2CSensor
address, DEFAULT_I2C_ADDRESS, port, REG_PRODUCT_ID, REG_VENDOR_ID, REG_VERSION, retryCount
-
Fields inherited from class lejos.hardware.sensor.BaseSensor
modes
-
Fields inherited from interface lejos.hardware.sensor.SensorConstants
BLACK, BLANK_INDEX, BLUE, BLUE_INDEX, BROWN, GREEN, GREEN_INDEX, MAX_TYPE, MIN_TYPE, MODE_RAW, NXT_ADC_RES, RED, RED_INDEX, TYPE_ANGLE, TYPE_COLORBLUE, TYPE_COLORFULL, TYPE_COLORGREEN, TYPE_COLORNONE, TYPE_COLORRED, TYPE_CUSTOM, TYPE_HIGHSPEED, TYPE_HIGHSPEED_9V, 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, WHITE, YELLOW
-
-
Constructor Summary
Constructors Constructor Description NXTUltrasonicSensor(I2CPort port)NXTUltrasonicSensor(Port port)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddisable()voidenable()voidgetData(int register, byte[] buf, int off, int len)Executes an I2C read transaction and waits for the result.SampleProvidergetDistanceMode()Gives a SampleProvider representing the sensor in distance mode.SampleProvidergetPingMode()Gives a SampleProvider representing the sensor in ping mode.booleanisEnabled()voidsendData(int register, byte[] buf, int off, int len)Executes an I2C write transaction.protected booleansetMode(byte mode)Sets the sensor to CONTINUOUS, PING or OFF-
Methods inherited from class lejos.hardware.sensor.I2CSensor
fetchString, getAddress, getData, getPort, getProductID, getRetryCount, getVendorID, getVersion, sendData, sendData, setAddress, setRetryCount
-
Methods inherited from class lejos.hardware.sensor.BaseSensor
fetchSample, getAvailableModes, getCurrentMode, getMode, getMode, getModeCount, getName, sampleSize, setCurrentMode, setCurrentMode, setModes
-
Methods inherited from class lejos.hardware.Device
close, releaseOnClose
-
-
-
-
Field Detail
-
MODE_OFF
protected static final byte MODE_OFF
- See Also:
- Constant Field Values
-
MODE_PING
protected static final byte MODE_PING
- See Also:
- Constant Field Values
-
MODE_CONTINUOUS
protected static final byte MODE_CONTINUOUS
- See Also:
- Constant Field Values
-
-
Method Detail
-
getData
public void getData(int register, byte[] buf, int off, int len)Description copied from class:I2CSensorExecutes an I2C read transaction and waits for the result.
-
sendData
public void sendData(int register, byte[] buf, int off, int len)Description copied from class:I2CSensorExecutes an I2C write transaction.
-
getDistanceMode
public SampleProvider getDistanceMode()
Gives a SampleProvider representing the sensor in distance mode. In this mode the sensor scans the surrounding continuously and reports the distance to the nearest object in sight. The sensor reports the distance to the nearest object in meters. The theoretical range of the sensor is 0,04 to 2.54 meter. If there is no object detected within this range the sensor reports Float.POSITIVE_INFINITY.
Samples can only be provided every 30 ms. If samples are fetched more often the SampleProvider will pause until the 30ms have passed.- Returns:
- A SamplePrivider
-
getPingMode
public SampleProvider getPingMode()
Gives a SampleProvider representing the sensor in ping mode. In this mode the sensor only scans the surrounding upon request, ie by calling fetchSample method. The sensor reports the distance, expressed in meters, to the eight nearest objects in sight. The theoretical range of the sensor is 0,04 to 2.54 meter. If there are less then eight objects within the sensors range the sensor will reports Float.POSITIVE_INFINITY. Fetching a sample in ping mode takes about 70 ms.- Returns:
- the sample provider
-
enable
public void enable()
-
disable
public void disable()
-
isEnabled
public boolean isEnabled()
-
setMode
protected boolean setMode(byte mode)
Sets the sensor to CONTINUOUS, PING or OFF- Parameters:
mode-- Returns:
- True, if the mode was changed.
False, if the mode was already in the requested state.
-
-