ch.aplu.nxt
Class UltrasonicSensor

java.lang.Object
  extended by ch.aplu.nxt.Part
      extended by ch.aplu.nxt.Sensor
          extended by ch.aplu.nxt.I2CSensor
              extended by ch.aplu.nxt.UltrasonicSensor
All Implemented Interfaces:
SharedConstants

public class UltrasonicSensor
extends I2CSensor

Class that represents an ultrasonic sensor. Most of the code and the documentation taken from the leJOS library (lejos.sourceforge.net, with thanks to the autor.


Field Summary
static byte CONTINUOUS_MEASUREMENT
          Sensor making continuous readings at set intervals.
static byte EVENT_CAPTURE
          Puts the sensor in passive mode to record pings that might come from other sonar sensors.
static byte OFF
          Turn off sensor.
static byte REQUEST_WARM_RESET
          Resets the sensor.
static byte SINGLE_SHOT
          Sensor sends out one ping and records result.
 
Fields inherited from class ch.aplu.nxt.Part
robot
 
Fields inherited from interface ch.aplu.nxt.SharedConstants
ABOUT, ANGLE, ANGLESTEPSMODE, BOOLEANMODE, BOOT, BRAKE, CELSIUSMODE, CLOSE, CUSTOM, DEBUG_LEVEL_HIGH, DEBUG_LEVEL_LOW, DEBUG_LEVEL_MEDIUM, DEBUG_LEVEL_OFF, DELETE, DELETE_USER_FLASH, DIRECT_COMMAND_NOREPLY, DIRECT_COMMAND_REPLY, FAHRENHEITMODE, FIND_FIRST, FIND_NEXT, GET_BATTERY_LEVEL, GET_CURRENT_PROGRAM_NAME, GET_DEVICE_INFO, GET_FIRMWARE_VERSION, GET_INPUT_VALUES, GET_OUTPUT_STATE, HIGH_SPEED_BUFFER, KEEP_ALIVE, LIGHT_ACTIVE, LIGHT_INACTIVE, LOWSPEED, LOWSPEED_9V, LS_GET_STATUS, LS_READ, LS_WRITE, MESSAGE_READ, MESSAGE_WRITE, MODEMASK, MOTOR_RUN_STATE_IDLE, MOTOR_RUN_STATE_RAMPDOWN, MOTOR_RUN_STATE_RAMPUP, MOTOR_RUN_STATE_RUNNING, MOTORON, NO_OF_SENSOR_TYPES, NO_SENSOR, NXJ_FIND_FIRST, NXJ_FIND_NEXT, OPEN_APPEND_DATA, OPEN_READ, OPEN_READ_LINEAR, OPEN_WRITE, OPEN_WRITE_DATA, OPEN_WRITE_LINEAR, PCTFULLSCALEMODE, PERIODCOUNTERMODE, PLAY_SOUND_FILE, PLAY_TONE, POLL, POLL_BUFFER, POLL_LENGTH, RAWMODE, READ, REFLECTION, REGULATED, REGULATION_MODE_IDLE, REGULATION_MODE_MOTOR_SPEED, REGULATION_MODE_MOTOR_SYNC, REPLY_COMMAND, RESET_MOTOR_POSITION, RESET_SCALED_INPUT_VALUE, SET_BRICK_NAME, SET_INPUT_MODE, SET_OUTPUT_STATE, SLOPEMASK, SOUND_DB, SOUND_DBA, START_PROGRAM, STOP_PROGRAM, STOP_SOUND_PLAYBACK, SWITCH, SYSTEM_COMMAND_NOREPLY, SYSTEM_COMMAND_REPLY, TEMPERATURE, TITLE, TITLEMP, TRANSITIONCNTMODE, VERSION, WRITE
 
Constructor Summary
UltrasonicSensor()
          Creates a sensor instance connected to port S1.
UltrasonicSensor(SensorPort port)
          Creates a sensor instance connected to the given port.
 
Method Summary
 void addUltrasonicListener(UltrasonicListener ultrasonicListener, int triggerLevel)
          Registers the given ultrasonic listener for the given trigger level.
 void calibrate(byte scaleFactor, byte scaleDivisor, byte zero)
          Calibrates the sensor.
protected  void cleanup()
           
 int getDistance()
          Polls the sensor.
 byte[] getDistances()
          The ultrasonic sensor is capable of sending a ping and recording up to 8 echoes.
 int getFactoryScaleDivisor()
          Gets the default scale divisor, which produces either cm's, inches, or some other unit.
 int getFactoryScaleFactor()
          Gets the default scale factor, which produces either cm's, inches, or some other unit.
 int getFactoryZero()
          Gets the default "zero" point, which should be 0.
 int getMeasurementInterval()
          Returns the time interval between measurements in continuous mode.
 int getScaleFactor()
          Gets the scale factor, which produces either cm's, inches, or some other unit.
 void setMeasurementInterval(byte interval)
          Sets the time interval between measurements in continuous mode.
 void setScaleFactor(byte scale)
          Sets the scale factor, producing either cm's, inches, or some other unit.
 void setSensorMode(byte modeEnumeration)
          Sets the state for the sensor. e.g. off, single pulse, continuous, passive, warm reset.
protected  void startUltrasonicThread()
           
protected  void stopUltrasonicThread()
           
 
Methods inherited from class ch.aplu.nxt.I2CSensor
fetchString, getData, init, sendData, sendData
 
Methods inherited from class ch.aplu.nxt.Sensor
getPort, getPortId, getPortLabel, LSRead, LSWrite, readBooleanValue, readNormalizedValue, readRawValue, readScaledValue, setTypeAndMode
 
Methods inherited from class ch.aplu.nxt.Part
setRobot
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OFF

public static byte OFF
Turn off sensor.


SINGLE_SHOT

public static byte SINGLE_SHOT
Sensor sends out one ping and records result.


CONTINUOUS_MEASUREMENT

public static byte CONTINUOUS_MEASUREMENT
Sensor making continuous readings at set intervals.


EVENT_CAPTURE

public static byte EVENT_CAPTURE
Puts the sensor in passive mode to record pings that might come from other sonar sensors.


REQUEST_WARM_RESET

public static byte REQUEST_WARM_RESET
Resets the sensor.

Constructor Detail

UltrasonicSensor

public UltrasonicSensor(SensorPort port)
Creates a sensor instance connected to the given port.

Parameters:
port - the port where the sensor is plugged-in

UltrasonicSensor

public UltrasonicSensor()
Creates a sensor instance connected to port S1.

Method Detail

setSensorMode

public void setSensorMode(byte modeEnumeration)
Sets the state for the sensor. e.g. off, single pulse, continuous, passive, warm reset.

Parameters:
modeEnumeration - e.g. OFF, SINGLE_SHOT, CONTINUOUS_MEASUREMENT, EVENT_CAPTURE, REQUEST_WARM_RESET

cleanup

protected void cleanup()
Overrides:
cleanup in class I2CSensor

addUltrasonicListener

public void addUltrasonicListener(UltrasonicListener ultrasonicListener,
                                  int triggerLevel)
Registers the given ultrasonic listener for the given trigger level. If the touch thread is not yet started, start it now.

Parameters:
ultrasonicListener - the UltrasonicListener to become registered.
triggerLevel - the trigger level where the callback is triggered

startUltrasonicThread

protected void startUltrasonicThread()

stopUltrasonicThread

protected void stopUltrasonicThread()

getDistance

public int getDistance()
Polls the sensor.

Returns:
the current distance to the closest object in cm

getDistances

public byte[] getDistances()
The ultrasonic sensor is capable of sending a ping and recording up to 8 echoes. A SINGLE_SHOT command is used, rather than continuous pings. This method returns an array of up to 8 distances picked up by the UltrasonicSensor sensor.

Returns:
eight distance values, mostly will be 0.

getMeasurementInterval

public int getMeasurementInterval()
Returns the time interval between measurements in continuous mode.

Returns:
the time 0 .. 255 in ms

setScaleFactor

public void setScaleFactor(byte scale)
Sets the scale factor, producing either cm's, inches, or some other unit.

Parameters:
scale - a number 0 .. 255

calibrate

public void calibrate(byte scaleFactor,
                      byte scaleDivisor,
                      byte zero)
Calibrates the sensor. If your sensor readings seem off, you can calibrate the sensor to make it different from factory settings.

Parameters:
scaleFactor - Raw reading is multiplied by this number. 1 = factory setting
scaleDivisor - Raw reading divides by this number. 14 = factory setting
zero - e.g. If you find the real distance is 10 cm and the sensor says 12 cm you can adjust the zero. 0 = factory setting

getScaleFactor

public int getScaleFactor()
Gets the scale factor, which produces either cm's, inches, or some other unit.

Returns:
the scale factor 0 .. 255

getFactoryScaleFactor

public int getFactoryScaleFactor()
Gets the default scale factor, which produces either cm's, inches, or some other unit.

Returns:
the scale factor 0 .. 255

getFactoryScaleDivisor

public int getFactoryScaleDivisor()
Gets the default scale divisor, which produces either cm's, inches, or some other unit.

Returns:
the scale divisor 0 .. 255

getFactoryZero

public int getFactoryZero()
Gets the default "zero" point, which should be 0.

Returns:
the zero point

setMeasurementInterval

public void setMeasurementInterval(byte interval)
Sets the time interval between measurements in continuous mode.

Parameters:
interval - the time interval in ms (0 .. 255)