Lotus
Lotus

Reputation: 21

Synchronize accelerometer and Gyroscope / Android

I am developing a mobile application under android and I need to retrieve the data from the accelerometer and gyroscope at exactly the same time ie I need to synchronize the two sensors to simultaneously read their values . I need to use a Service (not an Activity) but the value of the two event.timestamp are completely different.

public void onSensorChanged(SensorEvent event) {


    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        xa =event.values[0] ;
        ya =event.values[1] ;
        za =event.values[2] ;
        ta= event.timestamp;

    }

    if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
        xg = event.values[0];
        yg = event.values[1];
        zg = event.values[2];
        tg = event.timestamp;
   }

Thank you.

Upvotes: 2

Views: 1573

Answers (1)

Thracian
Thracian

Reputation: 67443

Use 2 boolean flags, one for accelerometer and another for gyroscope, when both of them true it means you get at least one time from both of them, posibly one of them will have 3 or more times. Accelerometer sensor is quite fast. When both of them true set them to false again to get data from them again. You can't synchronize sensors with Sensor api.

boolean isAccelData = false;
boolean isGyroData = false;

public void onSensorChanged(SensorEvent event) {


    if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
        isAccelData = true;
    }
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        isGyroData = true;
    }
    if (isAccelData & isGyroData) {
        // DO something here
        isAccelData = false;
        isGyroData = false;
    }
}

Upvotes: 1

Related Questions