简体   繁体   中英

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.

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;
    }
}

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