# Sensors

Work in progress…

class nxt.sensor.Port(*values)

  Input port identifier.

  The prefix is needed because a Python identifier can not start with a digit.

  S1 = 0

    Sensor port 1.

  S2 = 1

    Sensor port 2.

  S3 = 2

    Sensor port 3.

  S4 = 3

    Sensor port 4.

class nxt.sensor.Type(*values)

  Sensor type.

  NO_SENSOR = 0

    No sensor is connected.

  SWITCH = 1

    Touch sensor.

  TEMPERATURE = 2

    RCX temperature sensor.

  REFLECTION = 3

    RCX light sensor.

  ANGLE = 4

    RCX rotation sensor.

  LIGHT_ACTIVE = 5

    Light sensor with light active.

  LIGHT_INACTIVE = 6

    Light sensor with light off.

  SOUND_DB = 7

    Sound sensor (unadjusted).

  SOUND_DBA = 8

    Sound sensor (adjusted).

  CUSTOM = 9

    Custom sensor (unused).

  LOW_SPEED = 10

    Low speed digital sensor.

  LOW_SPEED_9V = 11

    Low speed digital sensor with 9V supply voltage.

  HIGH_SPEED = 12

    High speed sensor.

  COLOR_FULL = 13

    NXT color sensor in full color mode (color sensor mode).

  COLOR_RED = 14

    NXT color sensor with red light on (light sensor mode).

  COLOR_GREEN = 15

    NXT color sensor with green light on (light sensor mode).

  COLOR_BLUE = 16

    NXT color sensor in with blue light on (light sensor mode).

  COLOR_NONE = 17

    NXT color sensor in with light off (light sensor mode).

  COLOR_EXIT = 18

    NXT color sensor internal state.

class nxt.sensor.Mode(*values)

  Sensor mode.

  RAW = 0

    Raw value, from 0 to 1023.

  BOOL = 32

    Boolean value, 0 or 1.

  EDGE = 64

    Count number of transitions.

  PULSE = 96

    Count number of pulse.

  PERCENT = 128

    Value from 0 to 100.

  CELSIUS = 160

    Temperature in degree Celsius.

  FAHRENHEIT = 192

    Temperature in degree Fahrenheit.

  ROTATION = 224

    RCX rotation sensor mode.

class nxt.sensor.Sensor(brick, port)

  Sensor base class.

  set_input_mode(sensor_type, sensor_mode)

    Set sensor input mode.

    Parameters:

      - sensor_type (Type) – Sensor type.

      - sensor_mode (Mode) – Sensor mode.

exception nxt.sensor.analog.InvalidReading

  Bases: `Exception`

  Exception raised on timeout trying to get valid readings.

class nxt.sensor.analog.RawReading(port, valid, calibrated, sensor_type, sensor_mode, raw_value, normalized_value, scaled_value, calibrated_value)

  Bases: `object`

  A object holding the raw sensor values for a sensor.

  Parameters:

    - port (Port)

    - valid (bool)

    - calibrated (bool)

    - sensor_type (Type)

    - sensor_mode (Mode)

    - raw_value (int)

    - normalized_value (int)

    - scaled_value (int)

    - calibrated_value (int)

  port: Port

    Input port identifier.

  valid: bool

    `True` if the value is valid, else `False`.

  calibrated: bool

    Always `False`, there is no calibration in NXT firmware.

  sensor_type: Type

    Sensor type.

  sensor_mode: Mode

    Sensor mode.

  raw_value: int

    Raw analog to digital converter value.

  normalized_value: int

    Normalized value.

  scaled_value: int

    Scaled value.

  calibrated_value: int

    Always normalized value, there is no calibration in NXT firmware.

class nxt.sensor.analog.BaseAnalogSensor(brick, port)

  Bases: `Sensor`

  Object for analog sensors.

  get_input_values()

    Get raw sensor readings.

    Returns:

      An object with the read values.

    Return type:

      RawReading

  get_valid_input_values()

    Wait until input is valid, then get raw sensor readings.

    Returns:

      An object with the read values.

    Return type:

      RawReading

    Raises:

      InvalidReading – On timeout trying to get valid readings.

  reset_input_scaled_value()

    Reset sensor scaled value.

    This can be used to reset accumulated value for some sensor modes.

class nxt.sensor.digital.SensorInfo(version, product_id, sensor_type)

  Bases: `object`

  clarifybinary(instr, label)

class nxt.sensor.digital.BaseDigitalSensor(brick, port, check_compatible=True)

  Bases: `Sensor`

  Object for digital sensors.

  Parameters:

    check_compatible (bool) – Check sensor class match the connected sensor.

  If check_compatible is `True`, queries the sensor for its name and print a warning if the wrong sensor class is used.

  I2C_ADDRESS is the dictionary storing name to I2C address mappings. It should be updated in every subclass. When subclassing this class, make sure to call `add_compatible_sensor()` to add compatible sensor data.

  I2C_DEV = 2
  I2C_ADDRESS = {'factory_scale_divisor': (19, 'B'), 'factory_scale_factor': (18, 'B'), 'product_id': (8, '8s'), 'sensor_type': (16, '8s'), 'version': (0, '8s')}
  read_value(name)

    Read one or several values from the sensor.

    Parameters:

      name (str) – Name of the values to read.

    Returns:

      Read values in a tuple.

    Return type:

      tuple

    The name parameter is an index inside I2C_ADDRESS dictionary, which gives the corresponding I2C register address and format string.

  write_value(name, value)

    Write one or several values to the sensor.

    Parameters:

      - name (str) – Name of the values to write.

      - value (tuple) – Tuple of values to write.

    The name parameter is an index inside I2C_ADDRESS dictionary, which gives the corresponding I2C register address and format string.

  get_sensor_info()
  classmethod add_compatible_sensor(version, product_id, sensor_type)

    Adds an entry in the compatibility table for the sensor.

    Parameters:

      - version (str or None) – Sensor version, or `None` for default class.

      - product_id (str or None) – Product identifier, or `None` for default class.

      - sensor_type (str) – Sensor type

exception nxt.sensor.digital.SearchError

  Bases: `Exception`

nxt.sensor.digital.find_class(info)

  Returns an appropriate class.

  Parameters:

    info (SensorInfo) – Information read from the sensor.

  Returns:

    Class corresponding to sensor.

  Return type:

    BaseDigitalSensor

  Raises:

    SearchError – When no class found.

- LEGO Mindstorms NXT Sensors
  - `Touch`
  - `Light`
  - `Sound`
  - `Ultrasonic`
  - `Color`
  - `Temperature`
- Mindsensors Sensors
  - `SumoEyes`
  - `Compassv2`
  - `DIST`
  - `RTC`
  - `ACCL`
  - `MTRMUX`
  - `LineLeader`
  - `Servo`
  - `MMX`
  - `HID`
  - `PS2`
- HiTechnic Sensors
  - `Compass`
  - `Accelerometer`
  - `IRReceiver`
  - `IRSeekerv2`
  - `EOPD`
  - `Colorv2`
  - `Gyro`
  - `Prototype`
  - `SuperPro`
  - `ServoCon`
  - `MotorCon`
  - `Angle`