From b3b3d321c9547e2dc60b674d2b064215872f836e Mon Sep 17 00:00:00 2001 From: Dawson Brown Date: Wed, 18 May 2011 11:25:16 +1000 Subject: [PATCH] refs #39 Changed SensorClient class to use SensorEventListener instead of the deprecated SensorListener. Changed G-force recording to be absolute, thereby removing the relevence of min, so removed min G-force display, and changed format of g-force display onscreen to g(maxg). Signed-off-by: Dawson Brown --- src/com/geeksville/info/InfoGMeter.java | 19 ++++------ .../location/AccelerometerClient.java | 37 +++++++++++++++---- .../geeksville/location/CompassClient.java | 9 ++++- .../location/GPSToPositionWriter.java | 2 +- src/com/geeksville/location/SensorClient.java | 24 ++++++------ 5 files changed, 57 insertions(+), 34 deletions(-) diff --git a/src/com/geeksville/info/InfoGMeter.java b/src/com/geeksville/info/InfoGMeter.java index 12ae076..5735629 100644 --- a/src/com/geeksville/info/InfoGMeter.java +++ b/src/com/geeksville/info/InfoGMeter.java @@ -34,7 +34,7 @@ */ public class InfoGMeter extends InfoField implements Observer { - private float g = 1.0f, gMin = 1.0f, gMax = 1.0f; + private float g = 0.0f, gMax = 0.0f; private AccelerometerClient accel; @@ -49,7 +49,7 @@ public String getLabel() { */ @Override public String getText() { - return String.format("%.1f/%.1f/%.1f", g, gMin, gMax); + return String.format("%.1f(%.1f)", g, gMax); } /** @@ -58,7 +58,7 @@ public String getText() { */ @Override public String getUnits() { - return "cur/min/max"; + return "cur(max)"; } /** @@ -96,23 +96,18 @@ void onShown() { if (accel != null) accel.addObserver(this); } - + + @Override public void update(Observable observable, Object data) { - + // convert from m/sec to g's float newg = ((Float) data) / 9.6f; - // if the phone is being pulled away from the pilot that reports as neg - // g's but in pilot speak that is really positive gs - newg = (newg + 1) + 1; - + newg = Math.abs(newg - g); if (newg != g) { g = newg; - - gMin = Math.min(g, gMin); gMax = Math.max(g, gMax); - onChanged(); } } diff --git a/src/com/geeksville/location/AccelerometerClient.java b/src/com/geeksville/location/AccelerometerClient.java index 7d634b0..6209c58 100644 --- a/src/com/geeksville/location/AccelerometerClient.java +++ b/src/com/geeksville/location/AccelerometerClient.java @@ -27,17 +27,38 @@ public class AccelerometerClient extends SensorClient { public AccelerometerClient(Context context) { - super(context, SensorManager.SENSOR_ACCELEROMETER); + super(context, Sensor.TYPE_ACCELEROMETER); } - + float[] gravity = {0,0,0}; + private boolean firstpass = true; @Override - public void onThrottledSensorChanged(float[] values) { - float zAccel = values[2]; // FIXME, correct this for phone - // orientation - - setChanged(); - notifyObservers(zAccel); + public void onThrottledSensorChanged(float[] values) { + // alpha is calculated as t / (t + dT) + // with t, the low-pass filter's time-constant + // and dT, the event delivery rate + final float alpha = 0.8f; + gravity[0] = alpha * gravity[0] + (1 - alpha) * values[0]; + gravity[1] = alpha * gravity[1] + (1 - alpha) * values[1]; + gravity[2] = alpha * gravity[2] + (1 - alpha) * values[2]; + float xAccel = values[0] - gravity[0]; + float yAccel = values[1] - gravity[1]; + float zAccel = values[2] - gravity[2]; + float force; + if(firstpass) + { + force = 0; + firstpass = false; + } + else + force = Math.abs(xAccel) + Math.abs(yAccel) + Math.abs(zAccel); + setChanged(); + notifyObservers(force); + } + @Override + public void onAccuracyChanged(Sensor sensor, int accuracy) { + // Auto-generated method stub + } } diff --git a/src/com/geeksville/location/CompassClient.java b/src/com/geeksville/location/CompassClient.java index 18aede0..bc1bf5d 100644 --- a/src/com/geeksville/location/CompassClient.java +++ b/src/com/geeksville/location/CompassClient.java @@ -21,6 +21,7 @@ package com.geeksville.location; import android.content.Context; +import android.hardware.Sensor; import android.hardware.SensorManager; public class CompassClient extends SensorClient { @@ -31,7 +32,7 @@ public class CompassClient extends SensorClient { public int bearing; public CompassClient(Context context) { - super(context, SensorManager.SENSOR_ORIENTATION); + super(context, Sensor.TYPE_ORIENTATION); } @Override @@ -42,4 +43,10 @@ public void onThrottledSensorChanged(float[] values) { notifyObservers(bearing); } + @Override + public void onAccuracyChanged(Sensor sensor, int accuracy) { + // Auto-generated method stub + + } + } diff --git a/src/com/geeksville/location/GPSToPositionWriter.java b/src/com/geeksville/location/GPSToPositionWriter.java index 350b85f..c5c03d1 100644 --- a/src/com/geeksville/location/GPSToPositionWriter.java +++ b/src/com/geeksville/location/GPSToPositionWriter.java @@ -156,7 +156,7 @@ public void startLogging(Context context, PositionWriter dest, int pollIntervalS if (!isLogging()) { accel = new AccelerometerClient(context); // FIXME - make this // optional - accel.startListening(); + //accel.startListening(); this.numPoints = 0; this.pollInterval = pollIntervalSecs; diff --git a/src/com/geeksville/location/SensorClient.java b/src/com/geeksville/location/SensorClient.java index cc958a5..a046e9e 100644 --- a/src/com/geeksville/location/SensorClient.java +++ b/src/com/geeksville/location/SensorClient.java @@ -20,14 +20,18 @@ ******************************************************************************/ package com.geeksville.location; +import java.util.List; import java.util.Observable; import java.util.Observer; import android.content.Context; +import android.hardware.Sensor; +import android.hardware.SensorEvent; +import android.hardware.SensorEventListener; import android.hardware.SensorListener; import android.hardware.SensorManager; -abstract class SensorClient extends Observable implements SensorListener { +abstract class SensorClient extends Observable implements SensorEventListener { private SensorManager sensorMan; @@ -54,10 +58,14 @@ public void stopListening() { * is here folks who want to poll instead */ public void startListening() { + List sensors = sensorMan.getSensorList(sensorType); + if (sensors.size() > 0){ + Sensor sensor = sensors.get(0); sensorMan.registerListener( this, - sensorType, + sensor, SensorManager.SENSOR_DELAY_NORMAL); + } } /* @@ -69,7 +77,6 @@ public void startListening() { public synchronized void addObserver(Observer observer) { if (countObservers() == 0) startListening(); - super.addObserver(observer); } @@ -86,11 +93,6 @@ public synchronized void deleteObserver(Observer observer) { stopListening(); } - @Override - public void onAccuracyChanged(int sensor, int accuracy) { - // Do nothing - } - float[] values; /** @@ -107,12 +109,10 @@ public float[] getValues() { long lastUpdate = System.currentTimeMillis(); @Override - public void onSensorChanged(int sensor, float[] values) { - this.values = values; // No need for a deep copy - + public void onSensorChanged(SensorEvent event) { + this.values = event.values; // No need for a deep copy // We limit updates to a slowish rate to avoid burning cycles elsewhere long nowMs = System.currentTimeMillis(); - long diff = nowMs - lastUpdate; if (diff >= timeSpanMs) { onThrottledSensorChanged(values);