TouchSensor & TouchState


level_1


TouchSensor

A TouchSensor represents a sensor that can be touched on the robot.

See also API doc: TouchSensor.

TouchState

A TouchState is a structure grouping the touch state of a TouchSensor (touched or not) and the last time this state changed.

See also API doc: TouchState.

How to use it

Listing the touch sensors

To retrieve the list of TouchSensor available on the robot, use:

val touch: Touch = qiContext.touch
val sensorNames: List<String> = touch.sensorNames
// sensorNames (on Pepper): ["Head/Touch", "LHand/Touch", "RHand/Touch", "Bumper/FrontLeft", "Bumper/FrontRight", "Bumper/Back"].
Touch touch = qiContext.getTouch();
List<String> sensorNames = touch.getSensorNames();
// sensorNames (on Pepper): ["Head/Touch", "LHand/Touch", "RHand/Touch", "Bumper/FrontLeft", "Bumper/FrontRight", "Bumper/Back"].
../../../_images/pepper_touch_sensors.png

Retrieving a touch sensor

To retrieve a TouchSensor, use:

val touch: Touch = qiContext.touch
val touchSensor: TouchSensor = touch.getSensor("Head/Touch");
val touch = qiContext.touch
val touchSensor = touch.getSensor("Head/Touch")

Reading a touch state

To read the TouchState of a TouchSensor, use the getState() method:

val touchSensor: TouchSensor = ...
val touchState: TouchState = touchSensor.state
TouchSensor touchSensor = ...;
TouchState touchState = touchSensor.getState();

Listening to touch state changes

To be notified when a TouchSensor is touched/released, use the related TouchSensor.OnStateChangedListener:

val touchSensor: TouchSensor = ...
touchSensor.addOnStateChangedListener { touchState ->
    Log.i(TAG, "Sensor " + (if (touchState.touched ) "touched" else "released") + " at ${touchState.time}")
}
TouchSensor touchSensor = ...;
touchSensor.addOnStateChangedListener(touchState -> {
    Log.i(TAG, "Sensor " + (touchState.getTouched() ? "touched" : "released") + " at " + touchState.getTime());
});

See also API doc: OnStateChangedListener and Timestamps.