17 package eu.intermodalics.tango_ros_streamer.nodes;
19 import android.app.Activity;
20 import android.content.Context;
21 import android.hardware.Sensor;
22 import android.hardware.SensorEvent;
23 import android.hardware.SensorEventListener;
24 import android.hardware.SensorManager;
26 import org.ros.namespace.GraphName;
27 import org.ros.node.AbstractNodeMain;
28 import org.ros.node.ConnectedNode;
29 import org.ros.node.NodeMain;
30 import org.ros.node.topic.Publisher;
38 public class ImuNode extends AbstractNodeMain implements NodeMain, SensorEventListener {
39 private static final String
TAG =
ImuNode.class.getSimpleName();
45 private ConnectedNodeLogger
mLog;
56 mSensorManager = (SensorManager) activity.getSystemService(Context.SENSOR_SERVICE);
57 mRotationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
58 mGyroscopeSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
59 mAccelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
64 public void onStart(ConnectedNode connectedNode) {
65 mConnectedNode = connectedNode;
66 mLog =
new ConnectedNodeLogger(connectedNode);
67 mImuPublisher = connectedNode.newPublisher(
"android/imu", Imu._TYPE);
68 mImuMessage = mConnectedNode.getTopicMessageFactory().newFromType(Imu._TYPE);
70 mSensorManager.registerListener(
this, mRotationSensor, SensorManager.SENSOR_DELAY_FASTEST);
71 mSensorManager.registerListener(
this, mGyroscopeSensor, SensorManager.SENSOR_DELAY_FASTEST);
72 mSensorManager.registerListener(
this, mAccelerometerSensor, SensorManager.SENSOR_DELAY_FASTEST);
82 switch (event.sensor.getType()) {
83 case Sensor.TYPE_ROTATION_VECTOR:
84 mNewRotationData =
true;
85 float[] quaternion =
new float[4];
86 SensorManager.getQuaternionFromVector(quaternion, event.values);
87 mImuMessage.getOrientation().setW(quaternion[0]);
88 mImuMessage.getOrientation().setX(quaternion[1]);
89 mImuMessage.getOrientation().setY(quaternion[2]);
90 mImuMessage.getOrientation().setZ(quaternion[3]);
91 mImuMessage.setOrientationCovariance(
new double[]{0, 0, 0, 0, 0, 0, 0, 0, 0});
93 case Sensor.TYPE_GYROSCOPE:
94 mNewGyroscopeData =
true;
95 mImuMessage.getAngularVelocity().setX(event.values[0]);
96 mImuMessage.getAngularVelocity().setY(event.values[1]);
97 mImuMessage.getAngularVelocity().setZ(event.values[2]);
98 mImuMessage.setAngularVelocityCovariance(
new double[]{0, 0, 0, 0, 0, 0, 0, 0, 0});
100 case Sensor.TYPE_ACCELEROMETER:
101 mNewAccelerometerData =
true;
102 mImuMessage.getLinearAcceleration().setX(event.values[0]);
103 mImuMessage.getLinearAcceleration().setY(event.values[1]);
104 mImuMessage.getLinearAcceleration().setZ(event.values[2]);
105 mImuMessage.setLinearAccelerationCovariance(
new double[]{0, 0, 0, 0, 0, 0, 0, 0, 0});
110 if (mNewRotationData && mNewGyroscopeData && mNewAccelerometerData) {
112 mImuMessage.getHeader().setStamp(mConnectedNode.getCurrentTime());
113 mImuMessage.getHeader().setFrameId(
"imu");
114 mImuPublisher.publish(mImuMessage);
115 }
catch (NullPointerException e) {
116 mLog.error(TAG,
"Could not get current time. If you are using simulated time, " +
117 "/clock might not be published yet.");
120 mNewRotationData =
false;
121 mNewGyroscopeData =
false;
122 mNewAccelerometerData =
false;
ConnectedNode mConnectedNode
ImuNode(Activity activity)
SensorManager mSensorManager
boolean mNewGyroscopeData
static final String NODE_NAME
boolean mNewAccelerometerData
void onAccuracyChanged(Sensor sensor, int accuracy)
void onSensorChanged(SensorEvent event)
Sensor mAccelerometerSensor
Publisher< Imu > mImuPublisher
GraphName getDefaultNodeName()
void onStart(ConnectedNode connectedNode)