Skip to content

brian.sensors.EV3.ColorSensorEV3

brian.sensors.EV3

ColorSensorEV3 class objects

class ColorSensorEV3(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__

def __init__(port: SensorPort)

Initialize a EV3 color sensor at the given port.

Arguments:

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

Mode class objects

class Mode(Enum)

REFLECT

AMBIENT

COLOR_DETECT

REFLECT_RAW

RGB_RAW

RGB class objects

class RGB()

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

red

green

blue

__init__

def __init__(red: int, green: int, blue: int)

Arguments:

  • red: red color component [0-1023].
  • green: green color component [0-1023].
  • blue: blue color component [0-1023].

__sub__

def __sub__(other)

Element wise subscription

If resulting element would be smaller than 0, it is set to 0

__getitem__

def __getitem__(item)

Color class objects

class Color(Enum)

Constants for ColorSensor.detected_color(self) function.

NO_COLOR

BLACK

BLUE

GREEN

YELLOW

RED

WHITE

BROWN

set_mode

def 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.

Arguments:

  • mode: desired mode to be set

reflected_value

def reflected_value() -> int

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

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.

Returns:

reflectivity in % (0-100).

reflected_value_raw

def reflected_value_raw() -> int

Sets the sensor mode toREFLECT_RAW and returns the last value.

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.

Returns:

reflectivity raw values (0-1023).

ambient_value

def ambient_value() -> int

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

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.

Returns:

ambient in % (0-100).

detected_color

def detected_color() -> 'Color'

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

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.

Returns:

color constants corresponding to Color.[COLOR].

get_color_name

@staticmethod
def get_color_name(color: Color) -> str

Arguments:

  • 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

def rgb_values_raw() -> RGB

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

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. rgb_values_raw

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], ...)