Gravity sensing value increases
The
gravity sensor value is incremented after each call to the onSensorChanged(SensorEvent event) event
value. The value increases and reaches the NaN value after a little time (about 1 minute). Can someone say why this is so?
Code:
private void recordData(SensorEvent e) {
String label;
int currentSensorId = -1;
switch (e.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
currentSensorId = 0;
label = LOG_LABEL_ACCELEROMETER;
break;
case Sensor.TYPE_GYROSCOPE:
label = LOG_LABEL_GYROSCOPER;
currentSensorId = 1;
break;
case Sensor.TYPE_GRAVITY:
label = LOG_LABEL_GRAVITY;
currentSensorId = 2;
break;
case Sensor.TYPE_ROTATION_VECTOR:
label = LOG_LABEL_ROTATION_VECTOR;
currentSensorId = 3;
break;
default:
label = "UNKNOWN SENSOR TYPE!";
break;
}
if (currentSensorId == -1)
return;
Force set events frequency to value from config,
cause sensor events minimal frequency = 0.2 sec.
timePassed[currentSensorId] = System.currentTimeMillis()
- lastSensorEventTime[currentSensorId];
if (timePassed[currentSensorId] < Config.SENSOR_DELAY_MILLISEC) {
return;
} else {
lastSensorEventTime[currentSensorId] = System.currentTimeMillis();
}
if (this.pauseRecordingData || checkSDCardMemory())
return;
fileWriter.writeEvent(label, System.currentTimeMillis(), e.values);
}
public void writeEvent(String eventSource, long timestamp, float[] values) {
Log.d(TAG, "writeEvent(); eventSource = " + eventSource);
if ( !this.fileExists ) createNewFile();
try {
String valuesString = eventSource + "\t" + String.format( "%d\t", timestamp);
for (int i = 0; i < values.length; i++) {
valuesString += String.format( "%f\t",values[i]);
Log.d(TAG, "writeEvent(); values[" + i + "] = " + values[i]);
}
buffWriter.append(valuesString + "\n");
} catch (IOException e1) {
e1.printStackTrace();
Log.d(TAG,"RECORDING EVENT TO FILE FAILED!");
}
}
Solution
There are two possibilities:
Most likely: Your code has errors, such as you didn’t reset variables or add values (read for details). http://developer.android.com/guide/topics/sensors/sensors_motion.html)。
Your gravity sensor is broken (to check this, try another device).
For more help, post your code, How you can access and manage the values read from the gravity sensor.
Edit:
As I mentioned, there is an error in your code. Sensor read latency should be set to one of the possible options:
int SENSOR_DELAY_FASTEST get sensor data as fast as possible
int SENSOR_DELAY_GAME rate suitable for games
int SENSOR_DELAY_NORMAL rate (default) suitable for screen orientation changes
int SENSOR_DELAY_UI rate suitable for the user interface
!!! instead of custom values
Thanks for finding the solution.