A TouchSensor
represents a sensor that can be touched on the robot.
See also API doc: 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 API doc: TouchState.
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"].
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")
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();
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.