Wednesday, 28 August 2013

Android Accelerometer data recording unusual gap between samples

Android Accelerometer data recording unusual gap between samples

I am trying to record Acceleration data in an Android Service and store it
in the SD card along with its unix time, sampling frequency is 50Hz but
when I run the service for more than few minutes stored file shows unusual
gaps between two samples sometimes up to few hundred seconds. Can anyone
tell me why this is happening?
p.s.: I even have used wake lock and the API to put the service in a
foreground state to make sure it won't get any interference from the OS.
p.s.2: I use GalaxyS3
public class Acc extends Service {
private final String TAG = "Acc";
private FileWriter rawAccelerometerWriter;
Sensor sensorAccelerometer;
SensorManager sensorManager;
SensorEventListener accelerometerListener;
public static boolean serviceRunning;
public PowerManager pm;
public PowerManager.WakeLock wl;
@SuppressWarnings("deprecation")
public void onCreate(){
super.onCreate();
Log.i(TAG,"started");
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"");
//make sure service will not get killed
Intent i=new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi=PendingIntent.getActivity(this, 0, i, 0);
Notification note = new Notification(R.drawable.ic_launcher, "AccRec
Service is Running", System.currentTimeMillis());
note.setLatestEventInfo(this, "AccRec","Runnig", pi);
startForeground(1, note);
setupAccelerometerPhone();
}
private void setupAccelerometerPhone(){
wl.acquire();
serviceRunning = true;
rawAccelerometerWriter =
FileToWrite.createLogFileWriter("raw_acc_cont.csv");
if(rawAccelerometerWriter==null){
Log.v(TAG, "Failed to open file for raw accelerometer log");
}
//Now request the system to provide accelerometer updates
sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
sensorAccelerometer =
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
accelerometerListener = new SensorEventListener() {
public void onAccuracyChanged(Sensor sensor, int accuracy){}
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
logRawAccelerometerReading(System.currentTimeMillis(),
event.values[0], event.values[1], event.values[2]);
}
}
};
sensorManager.registerListener(accelerometerListener,
sensorAccelerometer, SensorManager.SENSOR_DELAY_GAME);
wl.release();
}
private void stopAccelerometerPhone(){
sensorManager.unregisterListener(accelerometerListener); //Remove
the accelerometer listener from the sensor manager
try { //Close the file, and catch an IOException if it occurs
rawAccelerometerWriter.close();
}
catch (IOException e){
Log.e(TAG, "Error closing phone accelerometer log file:
"+e.toString());
}
}
private boolean logRawAccelerometerReading(long l, double x, double y,
double z) {
String fileText = l+ "," +x+ "," +y + "," +z+ "\n";
try {
rawAccelerometerWriter.append(fileText);
rawAccelerometerWriter.flush();
return true;
}
catch (IOException e) {
Log.e(TAG, "Could not write to phone accelerometer log file:
"+e.toString());
return false;
}
}
public static class FileToWrite {
//Returns a FileWriter object for the specified file that other
methods can use
public static FileWriter createLogFileWriter(String logFileName){
FileWriter fileWriter = null;
File root = new File(Environment.getExternalStorageDirectory(),
"AccRecordings");
if (!root.exists()) { //Create the subfolder if it does not exist
root.mkdirs();
}
File file = new File(root, logFileName); //Create the file
object using the provided file name
//Open the writer
try{
fileWriter = new FileWriter(file, true);
}
catch (IOException e){
Log.v("LOG FILE", "Failed to open file for '"+logFileName+"'");
}
return fileWriter;
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy()
{
super.onDestroy();
Log.i(TAG,"stopped");
stopAccelerometerPhone();
stopForeground(true);
serviceRunning = false;
}
}

No comments:

Post a Comment