【发布时间】:2014-04-11 10:18:25
【问题描述】:
我正在使用三星 Galaxy ace3 来采样加速度。 getMinDelay() 给了我 10 000 µs。当我将该时间段内的加速度记录到 csv 文件时,我会在正确的时间段内出现延迟,并且随机抖动有时会很大(超过 0.5 秒)。
有人知道如何改进吗?
这是我得到的样本之间的延迟。
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mSensor;
private int cpt = 0;
private boolean start;
private List<String> records = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
public final File dir = new File("/mnt/extSdCard/test/");
public FileOutputStream afos = null;
public OutputStream stream = null;
public void startButton(View view) {
start = true;
cpt = 0;
mSensorManager.registerListener((SensorEventListener) this, mSensor, 10000); // 10000 µs -> 10 ms
openCsvFiles(dir, "data.csv");
stream = new BufferedOutputStream(afos);
}
public void stopButton(View view) {
for (String record: records) {
try {
stream.write(record.getBytes());
} catch (IOException e) {
}
}
try {
stream.flush();
stream.close();
afos.close();
start = false;
} catch (IOException e) {
Toast.makeText(MainActivity.this, "exception: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
Toast.makeText(MainActivity.this, "Done", Toast.LENGTH_SHORT).show();
}
@Override
public final void onSensorChanged(SensorEvent event) {
float ax, ay, az;
String axs, ays, azs, ts, line;
if (start) {
cpt++;
ax = event.values[0];
ay = event.values[1];
az = event.values[2];
ts = String.valueOf(event.timestamp);
long ns = android.os.SystemClock.elapsedRealtimeNanos();
line = ns+", "+ cpt+", "+ ts + "," + ax + "," + ay + "," + az + "\n";
records.add(line);
}
}
}
【问题讨论】:
标签: android accelerometer android-sensors sampling