create_threshold_listener()
  • 2 Minutes to read

create_threshold_listener()


Article Summary

Create a listener triggered when the value of one (or multiple) continuous sensor(s) go(es) inside/outside a specific numeric range .

Note: This method only works with continuous sensors. Remember the types of sensors in the Sensors page.

Reference

Arguments

ArgumentsTypeDefault value
listener_namestringUnique listener identifier
callbackCallable objectCallback
sensor_pathslist of stringsList of sensors paths, see the list of sensors
lower_bound-inffloatLower limit of the window
higher_boundinffloatHigher limit of the window
inside_rangeboolTrueTriggers when sensor value goes inside (True) / outside (False) the range
abs_valboolFalseIf true, uses the absolute value of the sensor value

At least one of both lower_bound or higher_bound must be speficied.

Note that if abs_val is True, lower_bound or/and higher_bound must be positive to have effect.

Return

None

Exceptions

ExceptionCondition
RayaListenerAlreadyCreatedProvided listener name already used to create another listener.
RayaInvalidCallbackInvalid or not callable callback.
RayaSensorsUnknownPathUnknown sensor path.
RayaSensorsIncompatiblePathSensor path not compatible with current robot.
RayaSensorsInvalidPathIncorrect sensor path.
RayaInvalidNumericRangeInvalid numeric range: low value irthreshold greater than high value, or one of the range limits is not a number.

See the Full List of Exceptions

Callback Arguments

Callback does not receive any arguments.

Examples

1. Detecting fast rotation

The following code detects when the robot rotates faster than 0.7rad/s (in any direction), and executes the listener_rotation_callback function asynchronously.

async def setup(self):
    ...
    self.sensors = await self.enable_controller('sensors')
    self.sensors.create_threshold_listener(listener_name='listener_rotation',
                                           callback = self.listener_rotation_callback,
                                           sensor_paths = ['/imu/rot_vel/z'],
                                           lower_bound = 0.7,
                                           abs_val=True)
    ...
    
async def loop(self):
    # Doing other stuff
    ...
    
def listener_rotation_callback(self):
        print('rotating too fast!!!')

2. Obstacles from multiple ultrasonic sensors

The following code detects obstacles between 0.5m and 1.5m, in any of the three ultrasonic sensors.

async def setup(self):
    ...
    self.sensors = await self.enable_controller('sensors')
    self.sensors.create_threshold_listener(listener_name='obstacles',
                                           callback = self.obstacles_callback,
                                           sensor_paths = ['/sonar/1', '/sonar/2', '/sonar/3'],
                                           lower_bound = 0.5,
                                           lower_bound = 1.5)
    ...
    
async def loop(self):
    # Doing other stuff
    ...
    
def obstacles_callback(self):
        print('obstacle detected!')

3. External temperature outside specified range

Detects when ambient temperature is not comfortable (between 20 and 25 degrees).

Chek if ambient temperature is not between 20 and 25 degrees.

...
self.sensors = await self.enable_controller('sensors')
...
if self.sensors.check_sensor_in_range("/temperature/ambient", lower_bound=20.0, higher_bound=25.0, inside_range=False):
    print('Not good temperature!!')
...
async def setup(self):
    ...
    self.sensors = self.enable_controller('sensors')
    self.sensors.create_threshold_listener(listener_name='bad_temperature',
                                           callback = self.bad_tempetature_callback,
                                           sensor_paths = ['/temperature/ambient'],
                                           lower_bound = 20.0,
                                           lower_bound = 25.0,
                                           inside_range = False)
    ...
    
async def loop(self):
    # Doing other stuff
    ...
    
def bad_tempetature_callback(self):
        print('Temperature not comfortable!')

Was this article helpful?