package mindstorm

  1. Overview
  2. Docs

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

type 'a t

Represent an initialized ultrasonic sensor connected to a given port.

val make : 'a conn -> port -> 'a t

make conn port initialize the sensor on port port as being an ultrasonic one.

val set : ?check_status:bool -> 'a t -> [ `Off | `Meas | `Meas_cont | `Event | `Reset | `Meas_interval of int | `Zero of int | `Scale_mul of int | `Scale_div of int ] -> unit

Ultrasonic.set us cmd set the state or parameters for the ultrasonic sensor us. cmd may be:

  • `Off: turns the ultrasonic sensor off.
  • `Meas: single shot command. In this mode, the ultrasonic sensor will only make a new measurement every time the command byte is send to the sensor. The sensor will measure distances for up to 8 objects and save the distances within the `Byte0 .. `Byte7 variables.
  • `Meas_cont Continuous measurement command. This is the default mode, where the sensor continuously makes new measurement with the specified interval (see `Meas_interval below).
  • `Event: Event capture command. 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.
  • `Meas_interval t: set continuous measurment interval.
  • `Zero z: set the actual zero.
  • `Scale_mul m: set the actual scale factor.
  • `Scale_div d: set the actual scale divisor.
  • `Reset: resets the ultrasonic sensor.
  • parameter check_status

    check the return status. Exceptionally, the default is true because the sensor needs to time to set itself up anyway and this avoids Mindstorm.Buffer_full errors if we try to get values from the sensor.

val get : 'a t -> [ `Byte0 | `Byte1 | `Byte2 | `Byte3 | `Byte4 | `Byte5 | `Byte6 | `Byte7 | `Meas_interval | `Zero | `Scale_mul | `Scale_div ] -> int

Ultrasonic.get us var returns the content of the variable var on the sensor (detailed underneath). All values are between 0 and 255. Communication errors will be reported by raising Error Bus_error; your application should be ready to handle such exceptions.

  • `Byte0 .. `Byte7 are the 8 variables containing the distances measured with the `Meas or `Meas_cont command (in centimeters).
  • `Meas_interval returns the interval (in 0.01 sec) between two consecutive measurments when the sensor is in continuous measurment (command `Meas_cont).
  • `Zero returns the value of the actual zero (settable with the `Zero command).
  • `Scale_mul (resp. `Scale_div) return the current scaling factor (resp. divisor). It is settable with the `Scale_mul (resp. `Scale_div) command.
val get_state : 'a t -> [ `Off | `Meas | `Meas_cont | `Event | `Reset ]

get_state us get the current state of the ultrasonic sensor us.

OCaml

Innovation. Community. Security.