NXT-0.2.3: A Haskell interface to Lego Mindstorms NXT

Safe HaskellNone




This module defines an interface to a digital ultrasonic sensor of the NXT kit.

I2C communication with ultrasonics sensor is described in Lego Mindstorms NXT Hardware Developer Kit, Appendix 7 - Ultrasonic sensor I2C communication protocol.



usInit :: InputPort -> NXT ()Source

Initializes sensor on the given input port. Default is ContinuousMeasurement mode.


usGetMeasurement :: InputPort -> MeasurementNumber -> NXT (Maybe Measurement)Source

Gets last measurement for a given measurement number based on the current mode. To retrieve new SingleShot measurements first use usSetMode (with SingleShot as an argument) to send new ultrasonic ping and after approximately 20ms read the results. (Change of NXT Bluetooth communication direction takes around 30 ms.) In ContinuousMeasurement mode new measurements are made automatically based on the continuous measurement interval value.

usGetAllMeasurements :: InputPort -> NXT [Measurement]Source

Helper function which gets all measurements available in order (closer first).


usSetMode :: InputPort -> Mode -> NXT ()Source

Sets current mode of operation.

usGetMode :: InputPort -> NXT ModeSource

Gets current mode of operation.

data Mode Source



Turns sensor off.


In this mode ultrasonic sensor only makes a new measurement every time this mode is set. The sensor measures distances for up to 8 objects (8 first echoes) which can be retrieved with usGetMeasurement.


In this mode the sensor continuously makes new measurement with the specific interval. This is the default mode.


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.


Requests warm reset.

usSetContinuousInterval :: InputPort -> ContinuousInterval -> NXT ()Source

Sets current continuous measurement interval value.

usGetContinuousInterval :: InputPort -> NXT ContinuousIntervalSource

Gets current continuous measurement interval value.

usSetActualZero :: InputPort -> Zero -> NXT ()Source

Sets current (actual) zero value. This is used to calibrate sensor.

usGetActualZero :: InputPort -> NXT ZeroSource

Gets current (actual) zero value.

usSetActualScaleFactor :: InputPort -> ScaleFactor -> NXT ()Source

Sets current (actual) scale factor value. This is used to calibrate sensor.

usGetActualScaleFactor :: InputPort -> NXT ScaleFactorSource

Gets current (actual) scale factor value.

usSetActualScaleDivisor :: InputPort -> ScaleDivisor -> NXT ()Source

Sets current (actual) scale divisor value. This is used to calibrate sensor.

usGetActualScaleDivisor :: InputPort -> NXT ScaleDivisorSource

Gets current (actual) scale divisor value.


usGetVersion :: InputPort -> NXT StringSource

Reads software version string (V1.0).

usGetVendorID :: InputPort -> NXT StringSource

Reads vendor ID string (LEGO).

usGetDeviceID :: InputPort -> NXT StringSource

Reads device ID string (Sonar).

usGetFactoryZero :: InputPort -> NXT ZeroSource

Reads factory zero value.

usGetFactoryScaleFactor :: InputPort -> NXT ScaleFactorSource

Reads factory scale factor value.

usGetFactoryScaleDivisor :: InputPort -> NXT ScaleDivisorSource

Reads factory scale divisor value.

usGetMeasurementUnits :: InputPort -> NXT StringSource

Reads measurement units string (10E-2m, a centimeter).


type Zero = IntSource

Type of a zero value.

type ScaleFactor = IntSource

Type of a scale factor value.

type ScaleDivisor = IntSource

Type of a scale divisor value.

type ContinuousInterval = IntSource

Type of a continuous measurement interval value. This seems to be in the range 1-15.

type MeasurementNumber = IntSource

Type of a measurement number. Sensor stores measurements (distances) for the first 8 echoes (numbered 0-7) it receives in SingleShot mode. For ContinuousMeasurement and EventCapture modes use first (0) measurement (distance) number.