How do I get subsequent timestamps accurate upto 5 micro seconds in android
Asked Answered
T

2

6

Below is sample code I am using:

 private final SensorManager mSensorManager;
 private final Sensor mAccelerometer;
 private long prevTime=0;

 public SensorActivity() {
   mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
   mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
   mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_FASTEST);
 }

I require the difference of subsequent system time-stamp values in OnSensorChanged(SensorEvent event) function after the sensor started.

 public void onSensorChanged(SensorEvent event) {
    long systemTimeStamp= SystemClock.elapsedRealtimeNanos();
    long timeDifference = (systemTimeStamp - prevTime) / 1000000;    //msec        
    Log.e("PROBLEM",timeDifference);
    prevTime=systemTimeStamp;
 }

Above difference becomes 0 many times at SENSOR_DELAY_FASTEST //Why??

The difference of subsequent time-stamps reports 0,but according to the definition it should report time in nano sec ,so at 5 micro second the difference should not be Zero. How can I get a non-zero difference or is there any other way to get system time-stamp difference at 5 micro sec accuracy.

Note that,event.timestamp reports timestamps of sensor, but I need timestamps of system in the OnSensorChanged(SensorEvent event) to measure how accurate the sensor timestamps are by comparing with the actual system timestamps.

Tragacanth answered 20/11, 2015 at 7:36 Comment(5)
Are you saying systemTimeStamp for two consecutive callbacks are the same, sometimes? I am sure that is what you are saying since the timeDefference can become 0 when you divide by 1000,000 and the difference is less than 1 ms, but just want to make sure.Japha
yes,this is my issue..is there a solution to this problem..?Tragacanth
Let me try again as I am not sure which one is the problem. You are logging the value of timeDifference. Is that the one that is 0? If so, that is the expected result when the actual difference between the values in nano seconds is less than 1000000 nanoseconds.Japha
So if this is expected, is there any work-around solution.Tragacanth
I am really getting confused. What exactly do you expect the result to be? When you use "long" the result is a whole number like 0, 1, ... What would you expect the result of 1000 divided by 1000,000 to be other than 0? Or maybe you are trying to get a fraction? In that case use float or double.Japha
J
0

Based on the comments you might be asking for this:

public void onSensorChanged(SensorEvent event) {
  long systemTimeStamp= SystemClock.elapsedRealtimeNanos();
  double timeDifference = ((double) (systemTimeStamp - prevTime)) / 1000000d;    //msec        
  Log.e("No PROBLEM",timeDifference);
  prevTime=systemTimeStamp;
}
Japha answered 30/11, 2015 at 18:42 Comment(0)
W
0

Sensor events are batched. You may use event.timestamp instead of SystemClock.elapsedRealtimeNanos().

See: https://source.android.com/devices/sensors/batching.html

Whoops answered 25/11, 2015 at 11:0 Comment(1)
i require the system time-stamps rather than the sensor timestamps to check the accuracy of sensor reporting values through event.timestamp.Tragacanth
J
0

Based on the comments you might be asking for this:

public void onSensorChanged(SensorEvent event) {
  long systemTimeStamp= SystemClock.elapsedRealtimeNanos();
  double timeDifference = ((double) (systemTimeStamp - prevTime)) / 1000000d;    //msec        
  Log.e("No PROBLEM",timeDifference);
  prevTime=systemTimeStamp;
}
Japha answered 30/11, 2015 at 18:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.