1

I developed a Data collector which collects data from Accelerometer, Gyroscope, Magnetometer and it worked fine for a while. Then I added Linear Acceleration to it as well (After 4 months, this week). Now both the version are behaving very strangely. Sometime they log the data perfectly when I do some physical activities like walking etc. However, sometimes it doesn't update sensors values and just repeat old values i.e each sensor value is updated lets after 5 seconds, 2 sec etc randomly and I need a sampling rate of 50 samples per second. I experimented with 10-15 participants and all my data was invalid because of this. The strange things is that the same app has worked perfectly before. I can't find any problem in it. I am placing some of the snapshots here. May be if someone can point to any bug or something ?

The buffered Writter:

FileWriter fow;
    BufferedWriter bow;

extfile = new File(extfilepath, message + ".csv");
                    fow = new FileWriter(extfile);
                    bow = new BufferedWriter(fow);  

This bow.writer is then being used in timertask thread to log data every 20 milliseconds.

Can anyone please comment or help me with this ? This weird behavior of this app is beyond my understanding.

utengr
  • 3,225
  • 3
  • 29
  • 68
  • When its working properly, then it updates the sensors values after each 20 milliseconds, but now after this weird behavior it updates the sensor values ranging from some milliseconds to 6 or 7 seconds. Sometime its few milliseconds, sometime its 2 sec or 3 sec etc. Which I have no clue why its happened. I shall also mention, we used this data collector for 8 hours data collection in one day in one go, and it worked fine (the older version). – utengr Jan 09 '14 at 16:23
  • It also logs GPS data but that's not relevant there I guess. – utengr Jan 09 '14 at 16:23
  • Moreover, my phone is Samsung Galaxy S2, with Android 4.1.2. – utengr Jan 09 '14 at 17:04

2 Answers2

2

Check that you have a wake lock acquired if your application goes to background. I've used PowerManager.PARTIAL_WAKE_LOCK successfully in a data collection application.

When your display turns off, your application is at least paused (and system might even stop it). The partial wake lock "Ensures that the CPU is running; the screen and keyboard backlight will be allowed to go off." So reading between the lines it means that otherwise your CPU might go to sleep for small periods of time in order to save power.

jmalmari
  • 139
  • 1
  • 4
  • I am using the Filewriter and bufferedwritter. You can see the code snapshot in the above code. The thing is that it worked fine so far and now suddenly behaved like this. – utengr Jan 09 '14 at 22:01
  • I'm collecting data at 50 Samples per second. The only weird thing is that sometime it works fine and sometime it doesn't update the values. The problem lies in sensoronchange method I guess because it should update values very quickly because I am using Sensor_Delay_Fastest rate. – utengr Jan 09 '14 at 22:15
  • In most cases, it starts okay and after couple of seconds, it goes into this weird behavior. – utengr Jan 09 '14 at 22:16
  • I lock the screen while recording the data I just noticed it which I didn't before. It might be a reason for taking the app to background ? – utengr Jan 09 '14 at 22:23
  • When your display turns off, your application is at least paused (and system might even stop it). The partial wake lock "Ensures that the CPU is running; the screen and keyboard backlight will be allowed to go off." So reading between the lines it could mean that otherwise your CPU might go to sleep for small periods of time in order to save power. – jmalmari Jan 10 '14 at 07:04
  • I think, that's the reason. I tried it today with two phones. One was with screen lock and one without. The one without worked fine but the other one had the same issue. I guess its go into Pause mode. To be on safe side, I will use the Partial wake lock. I am currently using the screen flag to keep it on always. – utengr Jan 10 '14 at 12:22
  • I have start and stop button. Currently turn this flag on in start button and turn it off in stop. Shall I replace it with acquire partial wake up lock in start button and release in stop button ? – utengr Jan 10 '14 at 12:28
  • Though it worked with screen_on_flag, I have changed it to wake up lock and its working fine now. Thank you :) – utengr Jan 10 '14 at 13:07
  • You're welcome. Sounds reasonable to release the wait lock as soon as you don't need it, on stop button event. (edited the post to better match what worked for you) – jmalmari Jan 13 '14 at 15:47
0

Did you forget to paste in: else if (event.sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION){} ? Are you using the accelerometer data, then subtracting gravity?

OK. What's your code look like to call the timer?? Something like this?

    Timer updateTimer = new Timer("linear accel");
    updateTimer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            updateGUI();
        }
    }, 0, 100);
}

private void updateGUI() {
    runOnUiThread(new Runnable() {
        public void run() {} } ?
a person
  • 986
  • 6
  • 13
  • I just don't see where you're referencing the linear acceleration sensor or storing it's data. – a person Jan 09 '14 at 15:50
  • I just forgot to put it here. I have two versions so I put here the old version. Now I have corrected it. – utengr Jan 09 '14 at 16:19
  • I figured that was the case. Stupid question: does your device have a linear accel sensor? A lot don't. You might want to calculate it yourself with the accelerometer output. – a person Jan 09 '14 at 16:40
  • Its not because of linear acceleration because I tried it without it as well and the same problem. And yes, my phone has the ability to read linear acceleration as it comes with acc, gyro and magnetometer. – utengr Jan 09 '14 at 17:04
  • OK. What's your code look like to call the timer?? Something like this? Timer updateTimer = new Timer("linear accel"); updateTimer.scheduleAtFixedRate(new TimerTask() { public void run() { updateGUI(); } }, 0, 100); } private void updateGUI() { runOnUiThread(new Runnable() { public void run() { } – a person Jan 09 '14 at 17:44
  • Not exactly like this but something similar. There is no issue with the time, it logs data correctly after each fixed interval. The problem is that sensor data is not getting updated regularly or shall I say sensoronchange function is not getting called regularly. – utengr Jan 09 '14 at 17:51
  • I don't know. Have you checked the range and things for the sensor? If it works for the others, maybe it's that. I don't think I can help anymore. I read a news article today about phones not functioning well in cold weather conditions... :) – a person Jan 09 '14 at 18:04
  • hahahaha it may be :P One reason could be that I am using synchronized. Lets see if someone else can comment on it. – utengr Jan 09 '14 at 18:08