spikedev.sensor

A module to interact with the color, distance, and touch sensors

Example

from spikedev.sensor import ColorSensor, TouchSensor

cs = ColorSensor()
ts = TouchSensor()

# when the user presses the TouchSensor, read the RGB values from the ColorSensor
ts.wait_for_bump()
print("color sensor RGB values are {}".format(cs.rgb()))

Classes

class spikedev.sensor.Sensor(port, desc=None)

Bases: object

Parameters
  • port (hub.port) – the hub.port.X for this sensor

  • desc (str) – description, defaults to None

set_mode(mode)

Set the mode for the sensor to mode

value()

Return the current value(s) from the sensor

class spikedev.sensor.TouchSensorMode

Bases: object

  • FORCE value will be from 0 to 10

  • TOUCH value will be either 0 or 1

  • TAP value is one of None, 1, 2 or 3

  • FPEAK value will be from 0 to 10, remembers the highest value

  • FRAW value will be from 380 to 698

  • FPRAW TBD

  • CALIB TBD

FORCE = 0
TOUCH = 1
TAP = 2
FPEAK = 3
FRAW = 4
FPRAW = 5
CALIB = 6
class spikedev.sensor.TouchSensor(port, desc=None, mode=1)

Bases: spikedev.sensor.Sensor

_images/touch-sensor.jpg
value()

Return the current value of the TouchSensor

is_pressed()

Set the mode to TouchSensorMode.TOUCH. Return True if the TouchSensor is currently pressed, else return False

is_released()

Set the mode to TouchSensorMode.TOUCH. Return True if the TouchSensor is currently released, else return False

wait_for_pressed(timeout_ms=None)

Wait timeout_ms for the TouchSensor to be pressed. Return True if the button was pressed within timeout_ms, else return False. If timeout_ms is None this will wait for forever.

wait_for_released(timeout_ms=None)

Wait timeout_ms for the TouchSensor to be released. Return True if the button was released within timeout_ms, else return False. If timeout_ms is None this will wait for forever.

wait_for_bump(timeout_ms=None)

Wait timeout_ms for the TouchSensor to be bumped. Return True if the button was bumped within timeout_ms, else return False. If timeout_ms is None this will wait for forever.

spikedev.sensor.rgb2lab(red, green, blue)

Convert RGB (red, green, blue) to CIELAB

class spikedev.sensor.ColorSensorMode

Bases: object

  • COLOR single value, LED is on

  • REFLT single value, LED is on, 0 - 100

  • AMBI single value, LED is off, 0 - 100

  • LIGHT three values, LED is off, always reads [0, 0, 0], not sure?

  • RREFL two values, LED is on, not sure?

  • RGB_I four values, LED is on, (red, green, blue, intensity?)

  • HSV three values, LED is on, (hue, saturation, value)

  • SHSV four values, LED is off, not sure?

  • DEBUG TBD

  • CALIB TBD

COLOR = 0
REFLT = 1
AMBI = 2
LIGHT = 3
RREFL = 4
RGB_I = 5
HSV = 6
SHSV = 7
DEBUG = 8
CALIB = 9
class spikedev.sensor.ColorSensor(port, desc=None, mode=0)

Bases: spikedev.sensor.Sensor

_images/color-sensor.jpg
color()

The number of the color. The LED is on.

reflected_light_intensity()

Set the mode to ColorSensorMode.REFLT and return the reflected light intensity as a percentage (0 to 100). The LED is on.

ambient_light_intensity()

Set the mode to ColorSensorMode.AMBI and return the ambient light intensity as a percentage (0 to 100). The LED is off.

hsv()

Set the mode to ColorSensorMode.HSR and return the Hue, Saturation, Value values. The LED is on.

rgb(scale_by_intensity=True)

Set the mode to ColorSensorMode.RGB_I and return the Red, Green, Blue values. The LED is on.

lab(scale_by_intensity=True)

Set the mode to ColorSensorMode.RGB_I and return the CIELAB values. The LED is on.

class spikedev.sensor.DistanceSensorMode

Bases: object

  • DISTL returns a number from 4 to 49

  • DISTS returns a number from 4 to 24. Does not work from as far away as DISTL

  • SINGL always returns [18]

  • LISTN always returns [0]

  • TRAW returns a number between 80 and 1600

  • LIGHT always returns [0, 0, 0, 0]

  • PING always returns [None]

  • ADRAW always returns [20]

  • CALIB TBD

DISTL = 0
DISTS = 1
SINGL = 2
LISTN = 3
TRAW = 4
LIGHT = 5
PING = 6
ADRAW = 7
CALIB = 8
class spikedev.sensor.DistanceSensor(port, desc=None, mode=0)

Bases: spikedev.sensor.Sensor

_images/distance-sensor.jpg