Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
DawsonLeBrown committed May 18, 2011
1 parent 2cde4bf commit b3b3d32
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 34 deletions.
19 changes: 7 additions & 12 deletions src/com/geeksville/info/InfoGMeter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}

/**
Expand All @@ -58,7 +58,7 @@ public String getText() {
*/
@Override
public String getUnits() {
return "cur/min/max";
return "cur(max)";
}

/**
Expand Down Expand Up @@ -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();
}
}
Expand Down
37 changes: 29 additions & 8 deletions src/com/geeksville/location/AccelerometerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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

}

}
9 changes: 8 additions & 1 deletion src/com/geeksville/location/CompassClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -42,4 +43,10 @@ public void onThrottledSensorChanged(float[] values) {
notifyObservers(bearing);
}

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

}

}
2 changes: 1 addition & 1 deletion src/com/geeksville/location/GPSToPositionWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 12 additions & 12 deletions src/com/geeksville/location/SensorClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -54,10 +58,14 @@ public void stopListening() {
* is here folks who want to poll instead
*/
public void startListening() {
List<Sensor> sensors = sensorMan.getSensorList(sensorType);
if (sensors.size() > 0){
Sensor sensor = sensors.get(0);
sensorMan.registerListener(
this,
sensorType,
sensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
}

/*
Expand All @@ -69,7 +77,6 @@ public void startListening() {
public synchronized void addObserver(Observer observer) {
if (countObservers() == 0)
startListening();

super.addObserver(observer);
}

Expand All @@ -86,11 +93,6 @@ public synchronized void deleteObserver(Observer observer) {
stopListening();
}

@Override
public void onAccuracyChanged(int sensor, int accuracy) {
// Do nothing
}

float[] values;

/**
Expand All @@ -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);
Expand Down

0 comments on commit b3b3d32

Please sign in to comment.