TouchSensor & TouchState


level_1


TouchSensor

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

See also javadoc: 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 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"].

https://developer.softbankrobotics.com/sites/default/files/repository/60_rst_pepper/public/source/_build/html/_images/pepper_touch_sensors.png

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.