3

I have a little problem with the "Compass".

I want to show on the screen in which direction I am looking with the camera. For that I have a CameraView and an Activity in running in landscape mode.

Now I try to get the direction with the sensors but something went wrong. When I start the app it shows some strange data but after shaking my phone it shows the correct data?!

Maybe you can help me. This is my onSensorChanged method:

public void onSensorChanged(SensorEvent event) {

    int type = event.sensor.getType();
    if (type == Sensor.TYPE_ACCELEROMETER) {
        mGrav = event.values.clone();
    } else if (type == Sensor.TYPE_MAGNETIC_FIELD) {
        mMag = event.values.clone();
    }

    if (SensorManager.getRotationMatrix(mRinn, null, mGrav, mMag)) {
        SensorManager.remapCoordinateSystem(mRinn, SensorManager.AXIS_X, SensorManager.AXIS_Z, mRout);

        SensorManager.getOrientation(mRout, mOrient);

        //Logger.d("direction?: " + Math.toDegrees(mOrient[0]));
        setOrientationText(Math.toDegrees(mOrient[0]));
    }
}

I am getting the data of the two sensors (magnet and accelerator) and then creating the RotationMatrix (like the documentation told me). In case it is landscape I put X -> X Axis and Y -> Z Axis for the RotationMatrix.

The screenOrientation for this Activity is landscape, too.

But I really don't understand why I am getting the correct data only after shaking the phone?!

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
Informatic0re
  • 6,300
  • 5
  • 41
  • 56
  • 1
    Very good question Mirko. The unhelpful answer is 'the compass needs calibration'. On my phone there is a phone keypad sequence which puts it into test mode and there it may tell me the compass needs calibrating. On what basis it makes this decision, I too would dearly like to know. – NickT Nov 25 '11 at 13:39
  • i thought only iPhone users are the guys who shaking his phones like a little toy car. It really needs calibration? isnt it "magnetic"? I think that, when i shake the phone it changed the orientation of the screen (from portrait to landscape), but i am not sure becouse i put anything to landscape mode... – Informatic0re Nov 25 '11 at 13:46
  • I found a propperty entry to calibrate the G-Sensor, but it doesnt help. The compass should always showing north :) – Informatic0re Nov 25 '11 at 13:59
  • This is an interesting question, Mirko. I'm not an Android expert. But I have played with 2-axiz chips. They could make correct bearing measurements without changes in orientation (shaking). I wonder of there are peculiarities in your compass' pronciple of operation. I'd like to invite your question to a group dedicated to sensors: http://tech.groups.yahoo.com/group/sensorforum – Nick Alexeev Nov 26 '11 at 02:51
  • a friend told me that you also need to calibrate the sensors in the Maps App (i never niticed that). Maybe i need to save the calibrated state of the compass? I dont know how but maybe this is needed, becouse i have to do it every time i start this app. – Informatic0re Nov 28 '11 at 12:54

1 Answers1

0

I think this is a problem related to initialization of sensors. You can see what I did answer to a similar question.

In short:

The readings are zero for some of the axis (either two or sometimes just one). This problem happens when you have just enabled the sensors (registerListener()) and I assume that it is related to some kind of sensor initialization. In the case of the acceleration sensor, I have observed that just a small shaking of the device makes it to start giving correct sensor readings.

The correct solution would be the method onAccuracyChanged() giving the correct information about the sensor state.

Community
  • 1
  • 1
jap1968
  • 7,747
  • 1
  • 30
  • 37