spike.distancesensor

Distance sensor mock API

Classes

DistanceSensor(port)

Distance sensor mocking class

class spike.distancesensor.DistanceSensor(port)

Distance sensor mocking class

Class is accessed simultaneously by the user side thread and the ground truth update thread. It is therefore protected by a mutex

__init__(port)

Contructor

Parameters:

port (string ('A','B','C','D','E' or 'F')) – the hub port to which the sensor is connected

get_distance_cm(short_range=False)

Retrieves the measured distance in centimeters.

Parameters:

short_range (boolean) – whether or not to use short range mode. Short range mode increases accuracy, but it can only detect nearby objects.

Raises:

TypeError – short_range is not a boolean

Returns:

The measured distance or “none” if the distance can’t be measured.

Return type:

float

get_distance_inches(short_range=False)

Retrieves the measured distance in inches.

Parameters:

short_range (boolean) – whether or not to use short range mode. Short range mode increases accuracy, but it can only detect nearby objects.

Raises:

TypeError – short_range is not a boolean

Returns:

The measured distance or “none” if the distance can’t be measured.

Return type:

float

get_distance_percentage(short_range=False)

Retrieves the measured distance as a percentage.

Parameters:

short_range (boolean) – whether or not to use short range mode. Short range mode increases accuracy, but it can only detect nearby objects.

Raises:

TypeError – short_range is not a boolean

Returns:

The measured distance or “none” if the distance can’t be measured.

Return type:

integer [0,100]

wait_for_distance_farther_than(distance, unit='cm', short_range=False)

Waits until the measured distance is greater than the specified distance.

Parameters:
  • distance (float) – the target distance to be detected from the sensor to an object.

  • unit (string (cm, in, %)) – the unit in which the distance is measured.

  • short_range (boolean) – whether or not to use short range mode. Short range mode increases accuracy, but it can only detect nearby objects.

Raises:
  • TypeError – distance is not a number or unit is not a string or short_range is not a boolean.

  • ValueError – unit is not one of the allowed values.

wait_for_distance_closer_than(distance, unit='cm', short_range=False)

Waits until the measured distance is less than the specified distance.

Parameters:
  • distance (float) – the target distance to be detected from the sensor to an object.

  • unit (string (cm, in, %)) – the unit in which the distance is measured.

  • short_range (boolean) – whether or not to use short range mode. Short range mode increases accuracy, but it can only detect nearby objects.

Raises:
  • TypeError – distance is not a number or unit is not a string or short_range is not a boolean.

  • ValueError – unit is not one of the allowed values.

light_up_all(brightness=100)

Lights up all of the lights on the Distance Sensor at the specified brightness.

Parameters:

brightness (integer [0,100]) – the specified brightness of all of the lights.

Raises:

TypeError – brightness is not an integer

light_up(right_top, left_top, right_bottom, left_bottom)

Sets the brightness of the individual lights on the Distance Sensor.

Parameters:
  • right_top (integer [0,100]) – the brightness of the light that’s above the right part of the Distance Sensor.

  • left_top (integer [0,100]) – the brightness of the light that’s above the left part of the Distance Sensor.

  • right_bottom (integer [0,100]) – the brightness of the light that’s below the right part of the Distance Sensor.

  • left_bottom (integer [0,100]) – the brightness of the light that’s below the left part of the Distance Sensor.

Raises:

TypeError – brightness is not an integer

__get_distance()

Returns current distance in cm

Returns:

distance in centimeters

Return type:

float

__process_command(command)

Process command until over

Parameters:

command (generator function) – command to process

c_reset()

Reset function

Warning

This function is not part of the spike API. It is provided to update the component from scenario data and shall not be used by the end-user.

c_read(distance)

Update the distance from scenario data

Warning

This function is not part of the spike API. It is provided to update the component from scenario data and shall not be used by the end-user.

Parameters:

distance (float) – distance in centimeters

c_set_lights(right_top, left_top, right_bottom, left_bottom)

Light distance sensor LEDs

Warning

This function is not part of the spike API. It is provided to update the component from scenario data and shall not be used by the end-user.

Parameters:
  • right_top (integer [0,100]) – the brightness of the light that’s above the right part of the Distance Sensor.

  • left_top (integer [0,100]) – the brightness of the light that’s above the left part of the Distance Sensor.

  • right_bottom (integer [0,100]) – the brightness of the light that’s below the right part of the Distance Sensor.

  • left_bottom (integer [0,100]) – the brightness of the light that’s below the left part of the Distance Sensor.

property port

Sets the component connection port

Warning

This function is not part of the spike API. It is provided to update the component from scenario data and shall not be used by the end-user.

Returns:

the component port

Return type:

string