Skip to content

brian.sensors.Sensor

brian.sensors

Sensor class objects

class Sensor()

Base Sensor class

__init__

def __init__(port: SensorPort, sensor_type: AutoDetect)

Initialize a EV3 sensor class at the given port.

Arguments:

  • port: Sensor port to which the sensor is attached (sensors.SensorPort.S1-sensors.SensorPort.S4).
  • sensor_type: Type of the sensor which is attached. sensor_port_probe.AutoDetect.ANALOG_P1 corresponds to LightSensorNXT and TouchSensorNXT sensor_port_probe.AutoDetect.ANALOG_P6 corresponds to TouchSensor sensor_port_probe.AutoDetect.PROTOCOL_UART_EV3 corresponds to ColorSensor, GyroSensor and UltrasonicSensor

Raises:

  • SensorPortAlreadyInUse: When trying to create new sensor on port that is already in use.

__del__

def __del__()

Deinitialize the sensor and free the port for other uses.

close_sensor

def close_sensor()

Deinitialize the sensor and free the port for other uses.

is_connected

def is_connected() -> bool

Returns:

True iff sensor is connected and not in the process of rebooting, False otherwise

is_ready

def is_ready() -> bool

Ready-state indicates that the attempt to read values will give valid results.

Example reasons for invalid results:

  • Sensor is rebooting or not initiated yet
  • Sensor is changing modes and the change is not finished yet
  • Connected sensor is incompatible with this handler (e.g. wrong type of sensor is connected)
In all of the above cases, this function will return False.

Returns:

True iff values are ready for the next read, False otherwise

wait_until_ready

def wait_until_ready(timeout_ms: int = -1) -> bool

Waits until the sensor is ready. This function is blocking.

When changing modes, the sensor enters a "not ready" state for a short period (until the mode change is propagated). Therefore, it is recommended to first set the correct mode using set_mode() before the calling this function. This only applies to sensors with modes.

Arguments:

  • timeout_ms: Maximum number of milliseconds to wait. If the timeout is negative, the function will wait indefinitely.

Returns:

success: - True: If the desired button event was caught. - False: If the timeout ran out.

reboot

def reboot() -> None

Turn off power to the port and turn it back on. This will forcibly reboot the sensor.

The powered-down state lasts about 100ms. In case of some (mostly digital) sensors, there can be some additional time (~1s or more) to boot up and process connection handshake with Brian.