GetUltrasonic

Reads the current value of the NXT ultrasonic sensor

Contents

Syntax

distance = GetUltrasonic(port)

distance = GetUltrasonic(port, handle)

Description

distance = GetUltraSonic(port) returns the current measurement value distance of the NXT ultrasonic sensor. distance represents the measured distance in cm. If no echo can be detected (which could indicate that either there is no obstacle in the way, or the ultrasound does not get reflected, e.g. by fur-like surfaces), the reading will be 255. If no measurement can be made (defect sensor, cable disconnected, etc.), a value of -1 will be returned.

The given port number specifies the connection port. The value port can be addressed by the symbolic constants SENSOR_1 , SENSOR_2, SENSOR_3 and SENSOR_4 analog to the labeling on the NXT Brick.

The last optional argument can be a valid NXT handle. If none is specified, the default handle will be used (call COM_SetDefaultNXT to set one).

Note:

This function only works when the sensor was correctly opened with OpenUltrasonic(port). If the sensor is being used in snapshot mode, GetUltrasonic will not work correctly!

For different uses, see also OpenUltrasonic(port, 'snapshot') and the functions USMakeSnapshot and USGetSnapshotResults.

Limitations

Since the Ultrasonic sensors all operate at the same frequency, multiple US sensors will interfere with each other! If multiple US sensors can "see each other" (or their echos and reflections), results will be unpredictable (and probably also unusable). You can avoid this problem by turning off US sensors, or operating them in snapshot mode (see also USMakeSnapshot and USGetSnapshotResults).

Due to the speed of sound in air, the ultrasonic sensor needs a certain amount of time to complete a successful measurement. This is why the maximum polling rate has been limited to 50 Hz (i.e. a call will take 20ms if called too often). This is only relevant for fast USB connections.

Example

   OpenUltrasonic(SENSOR_4);
   distance = GetUltrasonic(SENSOR_4);
   CloseSensor(SENSOR_4);

See also

OpenUltrasonic, USMakeSnapshot, USGetSnapshotResults, CloseSensor, NXT_LSRead, NXT_LSWrite,

Signature