package mindstorm

  1. Overview
  2. Docs

Input ports.

The NXT brick also accepts the sensors for the previous version of the mindstorm brick, called RCX, so several options refer to RCX.

type t
type port = [
  1. | `S1
  2. | `S2
  3. | `S3
  4. | `S4
]

The four sensor ports, labeled 1 to 4 on the brick. It is recommended you give meaningful names to values of type port through let bindings.

type sensor_type = [
  1. | `No_sensor
  2. | `Switch
  3. | `Temperature
  4. | `Reflection
  5. | `Angle
  6. | `Light_active
  7. | `Light_inactive
  8. | `Sound_db
  9. | `Sound_dba
  10. | `Custom
  11. | `Lowspeed
  12. | `Lowspeed_9v
  13. | `Highspeed
  14. | `Color_full
  15. | `Color_red
  16. | `Color_green
  17. | `Color_blue
  18. | `Color_none
]

Sensor type for a port. The sensor type primarily affects scaling factors used to calculate the normalized sensor value `Raw, but some values have other side effects.

  • `No_sensor: No sensor configured
  • `Switch: NXT or RCX touch sensor
  • `Temperature: RCX temperature sensor
  • `Reflection: RCX light sensor
  • `Angle: RCX rotation sensor
  • `Light_active: NXT light sensor with floodlight enabled
  • `Light_inactive: NXT light sensor with floodlight disabled
  • `Sound_db: NXT sound sensor; includes sounds too high or too low for our ears.
  • `Sound_dba: NXT sound sensor; focuses on sounds within human hearing.
  • `Custom
  • `Lowspeed: I2C digital sensor
  • `Lowspeed_9v: I2C digital sensor, 9V power (e.g. ultrasonic).
  • `Highspeed: Set `S4 to highspeed mode. This is currently unused by LEGO® sensors. This targets the P-Net communication protocol (www.P-net.org).

The LEGO® NXT 2.0 (8547) includes a color sensor with a tri-color led. The following values allow to configure it. If you have an older brick, this may require that you update its firmware. To do it under Linux, you can issue the command ./fwflash path_to_firmware.rfw (as root) where fwflash comes from libnxt. The firmware 1.28 can be dowloaded from http://legoengineering.com/library/doc_details/250-nxt-firmware-v128.html

  • Color_full white floodlight (all 3 leds on).
  • Color_red red floodlight.
  • Color_green green floodlight.
  • Color_blue blue floodlight.
  • Color_none no floodlight (passive mode).
type mode = [
  1. | `Raw
  2. | `Bool
  3. | `Transition_cnt
  4. | `Period_counter
  5. | `Pct_full_scale
  6. | `Celsius
  7. | `Fahrenheit
  8. | `Angle_steps
  9. | `Slope_mask
]

Sensor mode.

  • `Raw: Report scaled value equal to raw value.
  • `Bool: Report scaled value as 1 (TRUE) or 0 (FALSE). Note that for the switch sensor, the value is 1 of the button is pressed at the moment the data is requested. Use `Transition_cnt if you want not to miss button presses between two requests. The firmware uses inverse Boolean logic to match the physical characteristics of NXT sensors. Readings are FALSE if raw value exceeds 55% of total range; readings are TRUE if raw value is less than 45% of total range.
  • `Transition_cnt: Report scaled value as number of transitions between TRUE and FALSE. May not be fully exact if transitions are fast.
  • `Period_counter: Report scaled value as number of transitions from FALSE to TRUE, then back to FALSE.
  • `Pct_full_scale: Report scaled value as percentage of full scale reading for configured sensor type.
  • `Celsius: Scale temperature reading to degrees Celsius.
  • `Fahrenheit: Scale temperature reading to degrees Fahrenheit.
  • `Angle_steps: Report scaled value as count of ticks on RCX-style rotation sensor.
val set : ?check_status:bool -> 'a conn -> port -> sensor_type -> mode -> unit

set conn p ty m set the sensor connected to port p to type ty and mode m.

type data = {
  1. sensor_type : sensor_type;
  2. mode : mode;
  3. valid : bool;
    (*

    true if new data value should be seen as valid

    *)
  4. raw : int;
    (*

    Raw A/D value. Device dependent. Range: 0 .. 1023

    *)
  5. normalized : int;
    (*

    Normalized A/D value. Range: 0 .. 1023

    *)
  6. scaled : int;
    (*

    Scaled value. Its range depend on the Mindstorm.NXT.Sensor.mode chosen:

    • `Raw: 0 .. 1023
    • `Boolean: 0 or 1
    • `Transition_cnt: 0 .. 65535
    • `Period_counter: 0 .. 65535
    • `Pct_full_scale: 0 .. 100
    • `Celsius: -200 .. 700 (10th of degree Celsius)
    • `Fahrenheit: -400 .. 1580 (10th of degree Fahrenheit)
    • `Angle_steps: 0 .. 65535
    *)
}

Data read from sensors.

val get : 'a conn -> port -> data

get conn p returns the data read on port p. Before using this function, you must set the sensor type with Mindstorm.NXT.Sensor.set.

val color_of_data : data -> [ `Black | `Blue | `Green | `Yellow | `Red | `White ]

Returns the color seen by the color sensor from its reading. This is usually only accurate at a distance of about 1cm.

  • raises Invalid_argument

    if not applied to a `Color_full sensor.

val reset_scaled : ?check_status:bool -> 'a conn -> port -> unit

reset_scaled conn port

Low speed

Commands dealing with the I2C bus available on every digital sensor. (The port number 4 may also be high speed.)

val get_status : 'a conn -> port -> int

get_status conn port returns the number of bytes ready to be read.

val write : ?check_status:bool -> 'a conn -> port -> ?rx_length:int -> string -> unit

write conn port yx_data writes tx_data to lowspeed I2C sensor connected to the port. This is the protocol (e.g. for talking to the ultrasonic sensor). Communication errors will be reported by raising Error Bus_error; your application should be ready to handle such exceptions.

  • parameter rx_length

    gives the number of bytes to receive. Default: 0 i.e. no answer expected.

val read : 'a conn -> port -> string

Read data from from lowspeed I2C port (e.g. for receiving data from the ultrasonic sensor). Communication errors will be reported by raising Error Bus_error; your application should be ready to handle such exceptions.

Convenience
module Ultrasonic : sig ... end

Ultrasonic sensor. Convenience functions to interact with the ultrasonic sensor through the I2C protocol.