Hello,

Has anyone been able to get the accelerometer on the G1 to sample
consistently for long periods of time?

We’ve been doing some testing of the G1 accelerometer on Android, and
wanted to see if others have gotten the same result.  Our test uses a
partial wake lock to keep the CPU alive.  It stores the xyz values and
time to local storage and then writes them to a file on the SD card
once a second.  The code below produces a csv file.

The xyz values we’re getting are within the expected range, but the
sample rate of the accelerometer varies greatly.  To determine that
using the file produced, for each sample, we found the difference
between the sample’s timestamp (taken from the SensorEvent) and the
timestamp from the previous sample.  The test results showed the time
between samples ranging from 30 milliseconds up to 4.6 second
spikes.

We’re interested to know if other people are seeing similar behavior
from their phones.  If you have the time and means, please run the
code below and post your results.

Thanks


package edu.acceltest;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.PowerManager;

public class AccelTest extends Activity {

        private SensorManager mSensorManager;
        private PowerManager.WakeLock mWakelock;
    private PowerManager mPowerMgr;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

                // Set up the accelerometer reading
      mSensorManager = (SensorManager)getSystemService
(Context.SENSOR_SERVICE);
      // Get the list of all sensors, and find the accelerometer
within
      List<Sensor> sensorList = mSensorManager.getSensorList
(Sensor.TYPE_ACCELEROMETER);

      mSensorManager.registerListener(mSensorListener,
                  sensorList.get(0),
                  SensorManager.SENSOR_DELAY_GAME);

      mPowerMgr = (PowerManager)getSystemService(POWER_SERVICE);
                if (mWakelock == null)
                {
                        mWakelock = 
mPowerMgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
                                                                          , 
"Test");
                        mWakelock.acquire();
                }

    }

    @Override
    public void onDestroy() {
        if (mWakelock.isHeld())
                mWakelock.release();

    }

  //Accelerometer
        private final SensorEventListener mSensorListener = new
SensorEventListener() {

                private PrintWriter mCurrentFile;
            private Timer second = null;
                boolean never = true;
                String comma = new String(",");
                volatile int index = 0;
                float zam[] = new float[2000];

                public void runOnce()
                {

                        //Creating a file to print the data into

                String nameStr = new String("/sdcard/Activity - game
cached.csv");
                        File outputFile = new File(nameStr);
                        mCurrentFile = null;
                        try {
                                mCurrentFile = new PrintWriter(new 
FileOutputStream(outputFile));
                        } catch (FileNotFoundException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }

                        // Create a 1-second timer to check the sample rate
                        second = new Timer();
                second.scheduleAtFixedRate(new TimerTask() {

                                @Override
                                public void run() {

                                                // dump to file and reset 
index, eventually overwriting all the
data
                                                System.out.println("Sending");
                                                int a = 0;
                                                while(true)
                                                {
                                                        if (zam[a*4] == 0)
                                                                break;
                                                        StringBuffer buff = new 
StringBuffer();
                                                        
buff.append(String.valueOf(zam[a * 4]));
                                                        buff.append(comma);
                                                        
buff.append(String.valueOf(zam[a * 4 + 1]));
                                                        buff.append(comma);
                                                        
buff.append(String.valueOf(zam[a * 4 + 2]));
                                                        buff.append(comma);
                                                        
buff.append(String.valueOf(zam[a * 4 + 3]));
                                                        
mCurrentFile.println(buff.toString());
                                                        a++;
                                                }
                                                mCurrentFile.flush();
                                                index = 0;
                                                Arrays.fill(zam, 0, 1999, 0);
                                }

                  }, 1000, 1000);

                }

                        @Override
                        public void onAccuracyChanged(Sensor sensor, int 
accuracy) {
                                // TODO Auto-generated method stub

                        }


                        @Override
                        public void onSensorChanged(SensorEvent event) {

                                if (never)
                        {
                                never = false;
                                runOnce();
                        }

                                zam[index] = event.timestamp;
                                zam[index + 1] = event.values[0];
                                zam[index + 2] = event.values[1];
                                zam[index + 3] = event.values[2];

                                index += 4;

                        }
          };
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to