I am using Sensortag cc2541 as a sensor connected to NEXUX 5 via bluetooth. Thus, the code below is not typical onSensorChanged, since it does NOT use the sensors of the phone itself. Instead, he uses cc2541 sensor methods.
I want to make a simple compass and did some research and came up with the following.
From what I read on the Internet, Azimuth should be the direction (around Z) of the compass. However, I do not get the correct reading.
I also included images showing orientation accelerometerand magnetometersensor.
Accelerometer orientation

Magnetometer orientation

the code
public class ManagerListener extends SensorTagLoggerListener implements SensorTagListener {
private float[] gravityData = new float[3];
private float[] geomagneticData = new float[3];
private double azimuth,pitch,roll;
@Override
public void onUpdateAccelerometer(SensorTagManager mgr, Point3D acc) {
super.onUpdateAccelerometer(mgr, acc);
gravityData[0] = (float)acc.x;
gravityData[1] = (float)acc.y;
gravityData[2] = (float)acc.z;
}
@Override
public void onUpdateMagnetometer(SensorTagManager mgr, Point3D b) {
super.onUpdateMagnetometer(mgr, b);
geomagneticData[0] = (float) -b.y;
geomagneticData[1] = (float) b.x;
geomagneticData[2] = (float) -b.z;
float I[] = new float[9];
float R[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, gravityData, geomagneticData);
if (success)
{
float orientationMatrix[] = new float[3];
SensorManager.getOrientation(R, orientationMatrix);
azimuth = Math.toDegrees(orientationMatrix[0]);
pitch = Math.toDegrees(orientationMatrix[1]);
roll = Math.toDegrees(orientationMatrix[2]);
}
final float rotation = (float) azimuth;
final String xyz = "Azimuth:\t" + String.valueOf(Math.round(azimuth)) +
"\nPitch:\t" + String.valueOf(Math.round(pitch)) +
"\nRoll:\t" + String.valueOf(Math.round(roll));
runOnUiThread(new Runnable() {
@Override
public void run() {
degree.setText(xyz);
pointer.setRotation(rotation);
}
});
}
}
onUpdateAccelerometer onUpdateMagnetometer . . , .
, , 100 .
. , SensorTag . . , onUpdateAccelerometer ..... . Point3D x, y, z . - . , , https://www.sendspace.com/file/62ubk7 - , . Ive , , . - , , .