Sensor service

A sensor is a device that can detect one or more kinds of information from the outside world and can be transformed into electrical signals or other required forms of information output according to certain rules. The robot can sense the outside environment through the sensor. Because of the sensor, robots have human-like perception and response capabilities.

The sensor service provides a series of interfaces for controlling the robot's sensors. We can obtain SensorManager through [RobotContext[ to control the sensors.

SensorManager sensorManager = robotContext.getSystemService(SensorManager.SERVICE);

Obtain the sensor

Before using the sensor, you can get the list of sensors supported by the robot and view the sensor information.

  • Obtain the sensor of a specified ID

java SensorDevice sensorDevice = sensorManager.getDevice(sensorId);/* [1] */

[1] The SensorDevice object describes the information from the sensor as follows:``

Type Attributes Descriptions
String id Sensor ID
String type Sensor type
String name Sensor name
String description Description
List supportedCommandList The list of instructions supported
Map commandOptionClassMap Special instruction parameters
  • Obtain the list of sensors java List<SensorDevice> sensorDeviceList = sensorManager.getDeviceList();

  • Obtain the list of sensors of a specified type

java List<SensorDevice> sensorDeviceList = sensorManager.getDeviceList(type);/* [2] */

[2] The sensor type is defined by the implementation of specific sensor services and varies according to product design.

Turn the sensor on/off

You can turn the sensor on/off through ID

  • Turn the sensor on
Promise<Void, SensorException> result = sensorManager.enable(sensorId);/* [1] */

[1]The method that the returned result is Promise is an asynchronous method. You can obtain the execution result by registering a callback, or you can execute the get () method to convert it to a synchronous method. For details, see async.

  • Turn the sensor off
Promise<Void, SensorException> result = ssensorManager.disable(sensorId);

Obtain the status of the sensor

You can query the status of the sensor by ID

boolean sensorStatus = sensorManager.isEnabled(sensorId);

monitor

  • Register a sensor monitor
SensorListener sensorListener = new SensorListener() {
@Override
public void onSensorChanged(SensorDevice sensorDevice, SensorEvent sensorEvent/* [1] */) {
//view the event of sensor changes here
          }
};
sensorManager.registerListener(sensorId, sensorListener);

[1] The sensor value's changed values are stored in SensorEvent object. The specific information is as follows:

Type Attribute Description
String id Sensor ID
long timestamp Time stamp
float[] values Changed sensor value
  • Remove sensor monitor
sensorManager.unregisterListener(sensorListener);

Sensor list

Name Id
ultrasonic1 ultrasonic1
ultrasonic2 ultrasonic2
ultrasonic3 ultrasonic3
ultrasonic4 ultrasonic4
ultrasonic5 ultrasonic5
ultrasonic6 ultrasonic6
ultrasonic7 ultrasonic7
ultrasonic8 ultrasonic8
rgbd rgbd1
wallIR1 wallIR1
wallIR2 wallIR2
wallIR3 wallIR3
wallIR4 wallIR4
wallIR5 wallIR5
wallIR6 wallIR6
groundIR1 groundIR1
groundIR2 groundIR2
groundIR3 groundIR3
groundIR4 groundIR4
groundIR5 groundIR5
groundIR6 groundIR6
lidar lidar1
obstacle_detect obstacle_detect
human_detect human_detect
cliff_detect cliff_detect
humiture humiture
pyroelectric pyroelectric
TOF_infrared TOF_infrared
geomagnetism_detect geomagnetism_detect
LPalm_skin LPalm_skin
LPalm_switch LPalm_switch
LFArm_skin LFArm_skin
LBArm_skin LBArm_skin
RPalm_skin RPalm_skin
RPalm_switch RPalm_switch
RFArm_skin RFArm_skin
RBArm_skin RBArm_skin
emergency_stop emergency_stop
totalSwitch totalSwitch