brian.sensors.Sensor
SensorWaitOptimismLevel class objects
Optimism level for wait_until_ready() calls.
-
WAIT_UNTIL_CORRECT_TYPE: Wait only until the sensor type is confirmed correct. Use this immediately after sensor construction to quickly verify the sensor is connected and is of the expected type, without waiting for full initialization.
-
WAIT_UNTIL_FULLY_READY: Wait until the sensor is fully initialized and ready for normal use. You may use this before reading sensor values to ensure all modes and data are properly initialized.
WAIT_UNTIL_CORRECT_TYPE
Wait only until the sensor type is confirmed correct
WAIT_UNTIL_FULLY_READY
Wait until the sensor is fully initialized and ready for normal use
Sensor class objects
Base Sensor class
__init__
Initialize a sensor class at the given port.
Note: The constructor does NOT wait for the sensor to be ready. After construction, you should call wait_until_ready() with the appropriate optimism level:
Example usage::
sensor = ColorSensorEV3(sensors.SensorPort.S1)
# Quickly verify correct sensor type is connected (good after constructor)
sensor.wait_until_ready(optimism_level=sensors.SensorWaitOptimismLevel.WAIT_UNTIL_CORRECT_TYPE)
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:
SensorPortAlreadyInUseError: When trying to create new sensor on port that is already in use.
__del__
close_sensor
is_connected
Returns:
True iff sensor is connected and not in the process of rebooting, False otherwise
is_ready
Ready-state indicates that the attempt to read values will give valid results.
Example reasons for invalid results:
- Sensor is not connected (
is_connectedreturns False) - 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)
When the sensor is not connected, this function returns False. When the wrong sensor type is connected, this function raises an exception.
Raises:
brian.sensors.SensorIncompatibleTypeError: If wrong sensor type is connected.
Returns:
True if the sensor meets the readiness criteria, False if not connected.
wait_until_ready
def wait_until_ready(
timeout_ms: Optional[int] = None,
optimism_level: Optional[SensorWaitOptimismLevel] = WAIT_UNTIL_FULLY_READY
) -> 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: Milliseconds to wait for readiness.- If None, there is no per-call wall-clock limit (wait until ready).
- If 0 or negative, return/raise immediately without waiting.
- If positive, wait at most that many milliseconds.
optimism_level: Level of readiness to wait for.- SensorWaitOptimismLevel.WAIT_UNTIL_CORRECT_TYPE: Wait only until the sensor type is confirmed. Use this right after sensor construction to quickly verify the sensor is connected and of the expected type. This is faster but doesn't guarantee full initialization.
- SensorWaitOptimismLevel.WAIT_UNTIL_FULLY_READY (default): Wait until sensor is fully ready for normal operation. Use this before reading sensor values. -> This is automatically called in all sensor read operations with default timeout values
Raises:
SensorNotConnectedException: When no sensor is connected.SensorIncompatibleTypeException: When wrong sensor type is connected.SensorNotReadyException: When sensor is not ready for other reasons.
Returns:
success: - True: The sensor is ready at the specified optimism level.