brian.sensors package

Subpackages

brian.sensors.BrianBrianComm module

class brian.sensors.BrianBrianComm.BrianBrianComm(port: SensorPort, is_master: bool)

Bases: Sensor

Class exposing the communication between two Brians connected via a sensor port.

Sensor (other Brian) is automatically registered in the constructor of the base class and un-registered in its destructor.

There can be at most one instance at any given time, of any sensor class per port in the entire program.

__init__(port: SensorPort, is_master: bool) None

Initialize a new Brian-Brian communication handler at the given port. The communication can be initialized in either of two modes - master and slave. One of the ends must be master, the other must be slave.

Parameters:
  • port – Sensor port to which the sensor is attached.

  • is_master – true if this should be used in master mode, false for slave mode

available() int
Returns:

bytes available to be read

read(size: int | None = None) bytes

reads data incoming from the other Brian.

Parameters:

size – size limit, how many bytes to copy. If not provided, all available data will be read.

Returns:

read bytes

Raises:

OSError – If an error occurs during the write operation.

Error Codes: MP_EIO: BrianBrianComm is not ready or is disconnected.

write(message: bytearray | str | bytes) int

Sends the given buffer to the other Brian.

Parameters:

message – str, bytearray or bytes buffer containing the actual message.

Returns:

number of bytes written

Raises:

OSError – If an error occurs during the write operation.

Error Codes: MP_EIO: BrianBrianComm is not ready or is disconnected.

brian.sensors.Sensor module

class brian.sensors.Sensor.Sensor(port: SensorPort, sensor_type: AutoDetect)

Bases: object

Base Sensor class

__init__(port: SensorPort, sensor_type: AutoDetect)

Initialize a EV3 sensor class at the given port.

Parameters:
  • 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__()

Deinitialize the sensor and free the port for other uses.

close_sensor()

Deinitialize the sensor and free the port for other uses.

is_connected() bool
Returns:

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

is_ready() bool

Ready-state indicates that the attempt to read values will give valid results. Example reasons for invalid results:

  • Sensor is not connected (is_connected returns 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)

In all of the above cases, this function will return False. :return: True iff values are ready for the next read, False otherwise

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.

Parameters:

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

Return success:
  • True: The sensor is ready.

  • False: The sensor is not ready and time ran out.

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.

brian.sensors.SensorPort module

class brian.sensors.SensorPort.SensorPort(*values)

Bases: Enum

Sensor port constants

S1 = 0

Sensor port 1

S2 = 1

Sensor port 2

S3 = 2

Sensor port 3

S4 = 3

Sensor port 4

Module contents

exception brian.sensors.SensorException

Bases: Exception

Default sensor Exception

exception brian.sensors.SensorAlreadyClosedError

Bases: SensorException

Thrown when trying to access closed Sensor

exception brian.sensors.SensorIsNotReadyError

Bases: SensorException

Thrown when trying to read values from a sensor that is not ready

exception brian.sensors.SensorPortAlreadyInUse

Bases: SensorException

Thrown when trying to register sensor or sensor probe with autodetect to already used port