brian.sensors.EV3 package

brian.sensors.EV3.ColorSensorEV3 module

class brian.sensors.EV3.ColorSensorEV3.ColorSensorEV3(port: SensorPort)

Bases: Sensor

Class for interacting with EV3 color sensor.

Sensor is automatically registered in the constructor of the base class and un-registered in its destructor. It can also be unregistered with the ColorSensorEV3.close_sensor() function.

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

__init__(port: SensorPort)

Initialize a EV3 color sensor at the given port.

Parameters:

port – Sensor port to which the sensor is attached.

class Mode(*values)

Bases: Enum

REFLECT = 0
AMBIENT = 1
COLOR_DETECT = 2
REFLECT_RAW = 3
RGB_RAW = 4
class RawRGB(red: int, green: int, blue: int)

Bases: object

Class used to hold rgb values from ColorSensorEV3.rgb_values_raw() measurement. Each color channel holds values between 0-1023. Attributes can be accessed either directly or using an index (RawRGB[0] = RawRGB.red)

red: int
green: int
blue: int
__init__(red: int, green: int, blue: int)
Parameters:
  • red – red color component [0-1023].

  • green – green color component [0-1023].

  • blue – blue color component [0-1023].

class Color(*values)

Bases: Enum

Constants for ColorSensor.detected_color(self) function.

NO_COLOR = 0
BLACK = 1
BLUE = 2
GREEN = 3
YELLOW = 4
RED = 5
WHITE = 6
BROWN = 7
set_mode(mode: Mode) None

This function sets the sensor to the desired mode. While it’s not mandatory, it is recommended to call this function before accessing values from the sensor in a specific mode to prevent SensorIsNotReady exceptions.

Parameters:

mode – desired mode to be set

reflected_value() int

Sets the sensor mode to REFLECT and returns the last value.

Returns:

reflectivity in % (0-100).

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

REFLECT mode: Measure surface reflectivity for red light. The values are calibrated within the sensor itself. Returns the values in percent, in range 0-100. The measurement is corrected for the ambient light change.

reflected_value_raw() int

Sets the sensor mode to REFLECT_RAW and returns the last value.

Returns:

reflectivity raw values (0-1023).

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

REFLECT_RAW mode: Measure surface reflectivity for red light. The values are uncalibrated. Code using this function must thus perform range scaling/shifting manually. Measuring in this mode should provide more resolution over REFLECT mode. Returns values in range 0-1023. The measurement is corrected for the ambient light change.

ambient_value() int

Sets the sensor mode to AMBIENT and returns the last value.

Returns:

ambient in % (0-100).

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

AMBIENT mode: Measure ambient light intensity. The values are calibrated within the sensor itself. Returns the values in percent, in range 0-100.

detected_color() Color

Sets the sensor mode to COLOR_DETECT and returns the last value.

Returns:

color constants corresponding to Color.[COLOR].

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

COLOR_DETECT mode: Measure and discriminate the color. The discrimination is provided by the sensor itself. If this works poorly for your use case, you can use RGB_RAW instead.

static get_color_name(color: Color) str
Parameters:

color – Color to get the name of.

Returns:

English name of the provided color. This is a convenience function for UI or logging.

rgb_values_raw() RawRGB

Sets the sensor mode to RGB_RAW and return the last value.

Returns:

RGB raw values (0-1023 each channel). You can access them with RGB.red, RGB.green and RGB.blue, or by using subscription (RGB[0], RGB[1], …)

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

RGB_RAW mode: Measure surface reflectivity for red, green and blue light. The values are uncalibrated. Code using this function must thus perform range scaling/shifting manually. The measurement is corrected for the ambient light change.

brian.sensors.EV3.GyroSensorEV3 module

class brian.sensors.EV3.GyroSensorEV3.GyroSensorEV3(port: SensorPort)

Bases: Sensor

Class for interacting with EV3 gyro sensor.

Sensor is automatically registered in the constructor of the base class and un-registered in its destructor. It can also be unregistered with the GyroSensorEV3.close_sensor() function.

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

__init__(port: SensorPort)

Initialize a EV3 gyro sensor at the given port.

Parameters:

port – Sensor port to which the sensor is attached.

class Mode(*values)

Bases: Enum

ANGLE = 0
SPEED = 1
SPEED_COARSE = 2
ANGLE_AND_SPEED = 3
TILT_SPEED = 5
TILT_ANGLE = 6
set_mode(mode: Mode) None

This function sets the sensor to the desired mode. While it’s not mandatory, it is recommended to call this function before accessing values from the sensor in a specific mode to prevent SensorIsNotReady exceptions.

Parameters:

mode – desired mode to be set

angle() int

Sets the sensor mode to ANGLE and returns the last value.

Returns:

angle (int) in degrees (-32768 to 32768).

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

ANGLE mode: Measures the tilt angle and returns the value in degrees in range -32768 to 32767.

Clockwise is positive when looking at the side of the sensor with the arrows.

If you spin around too many times in ANGLE, ANGLE_AND_SPEED or TILT_ANGLE mode, it will get stuck at 32767 or overflow through -32768 depending on when the sensor was manufactured.

speed() int

Sets the sensor mode to SPEED and returns the last value.

Returns:

angular speed in degrees/s (-500 to 500).

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

SPEED mode: Measures angular speed and returns the value in degrees/second in range -500 to 500.

Clockwise is positive when looking at the side of the sensor with the arrows.

tilt_speed() int

Sets the sensor mode to Modes::TILT_SPEED and returns the last value.

Returns:

angular speed in degrees/s (-500 to 500).

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

TILT_SPEED mode: Measures angular speed around a second axis and returns the value in degrees/second in range -500 to 500. Clockwise is positive when looking at the side of the sensor opposite the cable jack.

tilt_angle() int

Sets the sensor mode to TILT_ANGLE and returns the last value.

Returns:

angle (int) in degrees (-32768 to 32768).

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

TILT_ANGLE mode: Measures the tilt angle around a second axis and returns the value in degrees in range -32768 to 32767.

This mode is not present in older sensors (date code ending with 2 or 3). Clockwise is positive when looking at the side of the sensor opposite the cable jack.

If you spin around too many times in “ANGLE”, “ANGLE_AND_SPEED” or “TILT_ANGLE” mode, it will get stuck at 32767 or overflow through -32768 depending on when the sensor was manufactured.

speed_coarse() int

Sets the sensor mode to SPEED_COARSE and returns the last value.

Returns:

angular speed in degrees/s (-1464 to 1535).

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

SPEED_COARSE mode: Measures angular speed and returns the value in degrees/second in range -1464 to 1535. Lower resolution, but wider range than the “SPEED” mode.

Clockwise is positive when looking at the side of the sensor with the arrows.

angle_and_speed() Tuple[int, int]

Sets the sensor mode to ANGLE_AND_SPEED and returns the last value.

Returns:

Tuple consisting of two integers: - The first integer represents the angle (deg) measurement. - The second integer represents the speed (deg/s) measurement.

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

ANGLE_AND_SPEED mode: Measures the tilt angle and angular speed and returns the value in degrees in range -32768 to 32767 for angle and -500 to 500 for speed.

Clockwise is positive when looking at the side of the sensor with the arrows. If you spin around too many times in “ANGLE”, “ANGLE_AND_SPEED” or “TILT_ANGLE” mode, it will get stuck at 32767 or overflow through -32768 depending on when the sensor was manufactured.

set_zero_point() None

Adjusts the angle readings to create new zero point angle. Adjusts angle only in “ANGLE”, “ANGLE_AND_SPEED” and “TILT_ANGLE” modes. Does not fix drift (for drift fixing, reboot the sensor using the reboot() function).

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

reboot() None

Reboots the sensor to force recalibration.

During the recalibration, keep the sensor steady to minimize drifting.

Reboot 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.EV3.TouchSensorEV3 module

class brian.sensors.EV3.TouchSensorEV3.TouchSensorEV3(port: SensorPort)

Bases: Sensor

Class for interacting with EV3 touch sensor.

Sensor is automatically registered in the constructor of the base class and un-registered in its destructor. It can also be unregistered with the TouchSensorEV3.close_sensor() function.

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

__init__(port: SensorPort)

Initialize a EV3 touch sensor at the given port.

Parameters:

port – Sensor port to which the sensor is attached.

is_pressed() bool

Measures the sensor state. Returns boolean of the last button pressed state. If the sensor is not ready, returns False.

Returns:

True if the sensor button is pressed, False otherwise. Or False if the sensor is not ready.

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

wait_for_press(timeout_ms: int = -1) bool

Waits for next button press event. This function is blocking.

Parameters:

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

Return success:
  • True: If the desired button event was caught.

  • False: If the timeout ran out.

wait_for_release(timeout_ms: int = -1) bool

Waits for next button release event. This function is blocking.

Parameters:

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

Return success:
  • True: If the desired button event was caught.

  • False: If the timeout ran out.

wait_for_press_and_release(timeout_ms: int = -1) bool

Waits for next button press and release event. This function is blocking.

Parameters:

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

Return success:
  • True: If the desired button event was caught.

  • False: If the timeout ran out.

brian.sensors.EV3.UltrasonicSensorEV3 module

class brian.sensors.EV3.UltrasonicSensorEV3.UltrasonicSensorEV3(port: SensorPort)

Bases: Sensor

Class for interacting with EV3 ultrasonic sensor.

Sensor is automatically registered in the constructor of the base class and un-registered in its destructor. It can also be unregistered with the UltrasonicSensorEV3.close_sensor() function.

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

__init__(port: SensorPort)

Initialize a EV3 ultrasonic sensor at the given port.

Parameters:

port – Sensor port to which the sensor is attached.

class Mode(*values)

Bases: Enum

DISTANCE_MM = 0
DISTANCE_TENTHS_OF_INCH = 1
DETECT_OTHER_US = 2
SINGLESHOT_MM = 3
SINGLESHOT_TENTHS_OF_INCH = 4
set_mode(mode: Mode) None

This function sets the sensor to the desired mode. While it’s not mandatory, it is recommended to call this function before accessing values from the sensor in a specific mode to prevent SensorIsNotReady exceptions.

Parameters:

mode – desired mode to be set

distance_mm() int

Sets the sensor mode to DISTANCE_MM and return the last value.

Returns:

distance in mm (0-2550). Or 2550 it reported a measurement error.

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

DISTANCE_MM mode: Continuously measures the distance and returns the value in mm. Returns the last measured distance in mm, in range 0-2550. If the measurement fails, the sensor reports a magic number (distance) of 2550.

distance_tenths_of_inch() int

Sets the sensor mode to DISTANCE_TENTHS_OF_INCH and return the last value.

Returns:

distance in tenths of an inch (0-1003).

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

DISTANCE_TENTHS_OF_INCH mode: Continuously measures the distance and returns the value in tenths of an inch. Returns the last measured distance in tenths of an inch, in range 0-1003. If the measurement fails, the sensor reports a magic number (distance) of 1003.

last_single_shot_mm() int

If the sensor is in SINGLESHOT_MM mode, returns the last value. If the sensor is in a different mode returns 2550. This method does not trigger the measurement. To trigger the measurement, use trigger_single_shot_measurement_mm().

Returns:

distance in mm (0-2550). Or 2550 if it reported a measurement error.

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

SINGLESHOT_MM mode: Activates once and measures the distance. After the measurement, the sensor goes to sleep and does not produce ultrasonic waves. This mode can be used when handling multiple ultrasonic sensors at the same time, to avoid interference between them. The single shot mode should not be called more often than about once in 250ms. Returns the last measured distance in mm, in range 0-2550. If the measurement fails, the sensor reports a magic number (distance) of 2550.

trigger_single_shot_measurement_mm() None

Sets the sensor mode to SINGLESHOT_MM and triggers the measurement. Does not wait for the result and does not return anything.

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

SINGLESHOT_MM mode: Activates once and measures the distance. After the measurement, the sensor goes to sleep and does not produce ultrasonic waves. This mode can be used when handling multiple ultrasonic sensors at the same time, to avoid interference between them. The single shot mode should not be called more often than about once in 250ms.

last_single_shot_tenths_of_inch() int

If the sensor is in SINGLESHOT_TENTHS_OF_INCH mode, returns the last value. If the sensor is in a different mode, returns 1003. This method does not trigger the measurement. To trigger the measurement, use trigger_single_shot_measurement_tenths_of_inch()

Returns:

distance in tenths of an inch (0-1003). Or 1003 if it reported a measurement error.

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

SINGLESHOT_TENTHS_OF_INCH mode: Activates once and measures the distance. After the measurement, the sensor goes to sleep and does not produce ultrasonic waves. This mode can be used when handling multiple ultrasonic sensors at the same time, to avoid interference between them. The single shot mode should not be called more often than about once in 250ms. Returns the last measured distance in tenths of an inch, in range 0-1003. If the measurement fails, the sensor reports a magic number (distance) of 1003. last_single_shot_tenths_of_inch

trigger_single_shot_measurement_tenths_of_inch() None

Sets the sensor mode to SINGLESHOT_TENTHS_OF_INCH and triggers the measurement. Does not wait for the result and does not return anything.

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

SINGLESHOT_TENTHS_OF_INCH mode: Activates once and measures the distance. After the measurement, the sensor goes to sleep and does not produce ultrasonic waves. This mode can be used when handling multiple ultrasonic sensors at the same time, to avoid interference between them. The single shot mode should not be called more often than about once in 250ms.

is_other_us_detected() bool

Sets the sensor mode to DETECT_OTHER_US and return the last value.

Returns:

True if other ultrasonic sensor is active and in range, False otherwise.

Raises:

brian.sensors.SensorIsNotReadyError – If the sensor is not ready.

DETECT_OTHER_US mode: Check if another ultrasonic sensor is active within the hearing distance of this sensor. Returns boolean value - True indicates that another ultrasonic sensor has been detected. A true value can also be triggered by a loud noise such as clapping.