android - Calculate difference between start angle and current measured angle/ Circle Calculation -
in application want calculate angle difference between start of application , current measured angle. therefore use sensor.type_accelerometer
, sensor.type_magnetic_field
current orientation. works quite well. here code:
@override public void onsensorchanged(sensorevent event) { if (event.accuracy == sensormanager.sensor_status_unreliable) return; switch (event.sensor.gettype()) { case sensor.type_accelerometer: valuesaccelerometer = lowpass(event.values.clone(), valuesaccelerometer); break; case sensor.type_magnetic_field: valuesmagneticfield = lowpass(event.values.clone(), valuesmagneticfield); } boolean success = sensormanager.getrotationmatrix(matrixr, matrixi, valuesaccelerometer, valuesmagneticfield); if (success) { sensormanager.getorientation(matrixr, matrixvalues); azimuth = (float) math.todegrees(matrixvalues[0]); if (azimuth < 0.0f) { azimuth += 360.0f; } if (!initaz) { startazimuth = azimuth; initaz = true; } else { float difaz = startazimuth-azimuth; log.i("test", "" +"dif: "+ difaz + "act_value " + azimuth + " start_value " + startazimuth); } } }
to describe problem here example logcat:
dif: -220.63545 act_value 358.24 start_value 137.61 dif: -220.7943 act_value 358.40 start_value 137.61 dif: -221.49236 act_value 359.10 start_value 137.61 dif: 137.07309 act_value 0.53 start_value 137.61 dif: 135.63617 act_value 1.97 start_value 137.61 dif: 135.16443 act_value 2.44 start_value 137.61 dif: 136.15468 act_value 1.457 start_value 137.61 dif: 136.72552 act_value 0.88 start_value 137.61
if current measurement turns 360° 0° difference angel turns wrong. how handle quite clean, works left , right rotation.
Comments
Post a Comment