Magnetic Sensor Droid X development issues
Hello all,
I have been trying to create a simple compass functionality in an application. I created the simple application doing testing on my Motorola droid 1 (2.22) and nexus S(2.34) and they seemed to work correctly (accuracy was not the best how-ever they work similar to other apps on the same phone so any differences i assumed to be hardware error).
However i then tested the app on a droid X (2.33) and the results were completely different (and completely wrong). When the phone is rolled around 45 degrees (rotated around z axis a computer coordinate system) the compass heading returned can change over 180 degrees. The heading value even goes in the opposite direction of the other phones (nexus s and droid 1) when spun in the same direction. This is the only phone that produces this result even after calibrating in the settings and doing the normal figure 8 motion. Also when i test against another compass app on the droid x it doesnt seem to have the same issue, but my compass app on the other phones lines up with the 3rd party compass app. So I would assume there would be something i can do in software to avoid this but there is nothing i can find and most people say my code looks correct. Also what would be the difference between these phones to cause this do i have to have special cases for phones that have issues like this?
Here is my code to perform the orientation calculations
Code:
public void onSensorChanged(SensorEvent event)
{
// If the sensor data is unreliable return
if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
{
Toast.makeText(main.this, "Sensor Status Unreliable",Toast.LENGTH_SHORT).show();
}
// Gets the value of the sensor that has been changed
switch (event.sensor.getType())
{
case Sensor.TYPE_ACCELEROMETER:
m_vfgravity = event.values.clone();
break;
case Sensor.TYPE_MAGNETIC_FIELD:
m_vfgeomag = event.values.clone();
break;
}
if (m_vfgravity != null && m_vfgeomag != null)
{
if(SensorManager.getRotationMatrix(m_vfinR, m_vfI, m_vfgravity, m_vfgeomag))
{
SensorManager.getOrientation(m_vfinR, m_vforientVals);
m_fCompBearing = (float) Math.round((Math.toDegrees(m_vforientVals[0])) *2)/2;
//convert to 0-360 from -180-180
if(m_fCompBearing < 0.0)
{
m_fCompBearing = 360 + m_fCompBearing;
}
mCompHead.setText("" + (int)m_fCompBearing);
}
calcOffset();
rotateCmp();
}
}
and my initialization code
Code:
mSMngr = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSListener = new cSensorListener();
mSMngr.registerListener(mSListener,
mSMngr.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_UI);
mSMngr.registerListener(mSListener,
mSMngr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
Any insight and or advice would be greatly appreciated. This bug has been haunting me for almost a week now with no solution in sight. I still cant even identify the root of the problem.
Thanks in advance !!