TouchSensor & TouchState



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

See also javadoc: TouchSensor.


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

See also javadoc: TouchState.

How to use it

Listing the touch sensors

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

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


Retrieving a touch sensor

To retrieve a TouchSensor, use:

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

Reading a touch state

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

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:

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

See also javadoc: OnStateChangedListener and Timestamps.