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)

Sensor modes for set_mode().

REFLECT

Reflected light intensity, used by reflected_value()

AMBIENT

Ambient light intensity, used by ambient_value()

COLOR_DETECT

Detected color, used by detected_color()

REFLECT_RAW

Raw reflected light intensity, used by reflected_value_raw()

RGB_RAW

Raw RGB values, used by rgb_values_raw()

RawRGB class objects

class RawRGB()

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

Red component (0-1023)

green

Green component (0-1023)

blue

Blue component (0-1023)

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

str(color) (and therefore print(color)) gives the English name of the color, e.g. "Red".

NO_COLOR

No color detected

BLACK

Black

BLUE

Blue

GREEN

Green

YELLOW

Yellow

RED

Red

WHITE

White

BROWN

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 SensorNotReady 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.SensorNotReadyError: 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.SensorNotReadyError: 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.SensorNotReadyError: 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.SensorNotReadyError: 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].

rgb_values_raw

def rgb_values_raw() -> RawRGB

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

Raises:

  • brian.sensors.SensorNotReadyError: 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], ...)