NXT-0.2.5: 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 Mode Source

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 ContinuousInterval Source

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 Zero Source

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 ScaleFactor Source

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 ScaleDivisor Source

Gets current (actual) scale divisor value.


usGetVersion :: InputPort -> NXT String Source

Reads software version string (V1.0).

usGetVendorID :: InputPort -> NXT String Source

Reads vendor ID string (LEGO).

usGetDeviceID :: InputPort -> NXT String Source

Reads device ID string (Sonar).

usGetFactoryZero :: InputPort -> NXT Zero Source

Reads factory zero value.

usGetFactoryScaleFactor :: InputPort -> NXT ScaleFactor Source

Reads factory scale factor value.

usGetFactoryScaleDivisor :: InputPort -> NXT ScaleDivisor Source

Reads factory scale divisor value.

usGetMeasurementUnits :: InputPort -> NXT String Source

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


type Zero = Int Source

Type of a zero value.

type ScaleFactor = Int Source

Type of a scale factor value.

type ScaleDivisor = Int Source

Type of a scale divisor value.

type ContinuousInterval = Int Source

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

type MeasurementNumber = Int Source

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.