mindstorm-lwt

Drive Lego Mindstorms bricks from OCaml (LWT version)
IN THIS PACKAGE
Module Mindstorm_lwt . NXT . Sensor
type t
type port = [
| `S1
| `S2
| `S3
| `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 = [
| `No_sensor
| `Switch
| `Temperature
| `Reflection
| `Angle
| `Light_active
| `Light_inactive
| `Sound_db
| `Sound_dba
| `Custom
| `Lowspeed
| `Lowspeed_9v
| `Highspeed
| `Color_full
| `Color_red
| `Color_green
| `Color_blue
| `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 = [
| `Raw
| `Bool
| `Transition_cnt
| `Period_counter
| `Pct_full_scale
| `Celsius
| `Fahrenheit
| `Angle_steps
| `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 Lwt.t

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

  • parameter check_status

    whether to check the status returned by the brick. Default: see Mindstorm_lwt.NXT.connect_bluetooth.

type data = Mindstorm.NXT.Sensor.data = {
sensor_type : sensor_type;
mode : mode;
valid : bool;(*

true if new data value should be seen as valid

*)
raw : int;(*

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

*)
normalized : int;(*

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

*)
scaled : int;(*

Scaled value. Its range depend on the Mindstorm_lwt.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 Lwt.t

get conn p returns the data read on port p. Before using this function, you must set the sensor type with Mindstorm_lwt.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 Lwt.t

reset_scaled conn port

  • parameter check_status

    whether to check the status returned by the brick. Default: see Mindstorm_lwt.NXT.connect_bluetooth.

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 Lwt.t

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 Lwt.t

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.

  • parameter check_status

    whether to check the status returned by the brick. Default: see Mindstorm_lwt.NXT.connect_bluetooth.

val read : 'a conn -> port -> string Lwt.t

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.