简体   繁体   中英

Accelerometer values to degrees

I wrote the following code,where the values of the accelerometer are shown in x,y,z during rotation.

public class MainActivity extends AppCompatActivity implements SensorEventListener {

private TextView xText,yText,zText;
private Sensor mySensor;
private SensorManager SM;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Creating the Sensor Manager
    SM = (SensorManager)getSystemService(SENSOR_SERVICE);

    // Accelerometer Sensor
    mySensor = SM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    // Register sensor Listener
    SM.registerListener(this, mySensor, SensorManager.SENSOR_DELAY_NORMAL);

    // Assign TextView
    xText = (TextView)findViewById(R.id.xText);
    yText = (TextView)findViewById(R.id.yText);
    zText = (TextView)findViewById(R.id.zText);


}

@Override
public void onSensorChanged(SensorEvent sensorEvent) {



    xText.setText("X: " + sensorEvent.values[0]);
    yText.setText("Y: " + sensorEvent.values[1]);
    zText.setText("Z: " + sensorEvent.values[2]);
}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {

  }
}

Now I want to convert the values I get from the SensorEvents to degrees. I looked at various questions here,but I got confused.

    double x = sensorEvent.values[0];
    double y = sensorEvent.values[1];
    double z = sensorEvent.values[2];

There should be a formula that takes the above values and convert them in degrees.

Any ideas?

Thanks,

Theo.

EDIT

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
    //xText.setText("X: " + sensorEvent.values[0]);
    //yText.setText("Y: " + sensorEvent.values[1]);
    //zText.setText("Z: " + sensorEvent.values[2]);

    double x = sensorEvent.values[0];
    double y = sensorEvent.values[1];
    double z = sensorEvent.values[2];

    double pitch = Math.atan(x/Math.sqrt(Math.pow(y,2) + Math.pow(z,2)));
    double roll = Math.atan(y/Math.sqrt(Math.pow(x,2) + Math.pow(z,2)));
    //convert radians into degrees
    pitch = pitch * (180.0/3.14);
    roll = roll * (180.0/3.14) ;

    yText.setText(String.valueOf(pitch));
    zText.setText(String.valueOf(roll));
}

Now I want to convert the values I get from the SensorEvents to degrees

The unit of the value you get from TYPE_ACCELEROMETER is m/s^2, thus trying to convert to degree does not make sense.

Your pitch and roll calculations do not seem right. For the correct calculation see the method processSensorData(DProcessedSensorEvent.DProcessedSensorEventBuilder builder) in the DSensorEventProcessor class at https://github.com/hoananguyen/dsensor/blob/master/dsensor/src/main/java/com/hoan/dsensor_master/DSensorEventProcessor.java

To convert pitch and roll to degrees use Math.toDegrees(valueToConvert)

Youll need to register for the TYPE_ACCELEROMETER , but also for TYPE_MAGNETIC_FIELD and than you can leverage SensorManager built-in method for your help:

public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
      gravity = event.values;
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
      geomagnetic = event.values;
    if (mGravity != null && geomagnetic != null) {
      float R[] = new float[9];
      float I[] = new float[9];
      boolean success = SensorManager.getRotationMatrix(R, I, gravity, geomagnetic);
      if (success) {
        float orientation[] = new float[3];
        SensorManager.getOrientation(R, orientation);
        myAzimut = orientation[0]; // myAzimut is The geomagnetic inclination angle in radians.
      }
   }
}

You can learn all additional information by reading SensorManager source code comments.

your code seem right. use Math.PI in (180.0/3.14) to get more accurate results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM