Skip to content

Commit

Permalink
gps added to sensors
Browse files Browse the repository at this point in the history
may be not API8 ready (Android 2.2)
  • Loading branch information
eziosoft committed Feb 7, 2013
1 parent 7608d37 commit a51f384
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 72 deletions.
Binary file added Frskycapture.txt
Binary file not shown.
1 change: 0 additions & 1 deletion src/com/ezio/multiwii/GPSActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

Expand Down
70 changes: 8 additions & 62 deletions src/com/ezio/multiwii/dashboard/Dashboard1Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@
package com.ezio.multiwii.dashboard;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.hardware.GeomagneticField;
import android.hardware.SensorManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
Expand All @@ -34,7 +27,7 @@
import com.ezio.multiwii.R;
import com.ezio.multiwii.helpers.Sensors;

public class Dashboard1Activity extends Activity implements LocationListener {
public class Dashboard1Activity extends Activity implements Sensors.Listener {

private boolean killme = false;

Expand All @@ -51,31 +44,16 @@ public class Dashboard1Activity extends Activity implements LocationListener {

Handler mHandler = new Handler();

private SensorManager sensorManager;

GeomagneticField geoField;
double declination = 0;
private LocationManager locationManager;
private String provider;

// //////////////////////
Sensors sensors;
float myAzimuth = 0;

private Runnable update = new Runnable() {
@Override
public void run() {

myAzimuth = (float) (sensors.GetYaw + declination);

app.mw.ProcessSerialData(app.loggingON);
app.frsky.ProcessSerialData(false);

if (app.D) {
app.mw.angy = sensors.GetPitch;
app.mw.angx = (sensors.GetRoll);
}

PRVp.SetAngle(app.mw.angy);
PRVr.SetAngle(app.mw.angx);

Expand Down Expand Up @@ -135,23 +113,6 @@ protected void onCreate(Bundle savedInstanceState) {
BattVoltageTV = (TextView) findViewById(R.id.TextViewBattVoltage);
PowerSumTV = (TextView) findViewById(R.id.TextViewPowerSum);

sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
// criteria.setAccuracy(Criteria.ACCURACY_COARSE);
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
Log.d("aaa", String.valueOf(location.getLatitude()));
Log.d("aaa", String.valueOf(location.getLongitude()));
geoField = new GeomagneticField(Double.valueOf(location.getLatitude()).floatValue(), Double.valueOf(location.getLongitude()).floatValue(), Double.valueOf(location.getAltitude()).floatValue(), System.currentTimeMillis());
declination = geoField.getDeclination();
} else {
Log.d("aaa", "Provider not available");
// PhoneLongtitudeTV.setText("Provider not available");
}

sensors = new Sensors(getApplicationContext());

}
Expand All @@ -167,9 +128,7 @@ protected void onPause() {
super.onPause();
killme = true;
mHandler.removeCallbacks(update);

sensors.stop();
locationManager.removeUpdates(this);

}

Expand All @@ -181,31 +140,18 @@ protected void onResume() {
mHandler.postDelayed(update, app.RefreshRate);

sensors.start();
locationManager.requestLocationUpdates(provider, 400, 1, this);
sensors.registerListener(this);
app.Say(getString(R.string.PitchRoll));

}

@Override
public void onLocationChanged(Location location) {
geoField = new GeomagneticField(Double.valueOf(location.getLatitude()).floatValue(), Double.valueOf(location.getLongitude()).floatValue(), Double.valueOf(location.getAltitude()).floatValue(), System.currentTimeMillis());
declination = geoField.getDeclination();
if (app.D)
Log.d("aaa", "geoField.getDeclination())=" + String.valueOf(geoField.getDeclination()));
}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
public void onSensorsStateChange() {
myAzimuth = (float) (sensors.GetHeading);
if (app.D) {
app.mw.angy = sensors.GetPitch;
app.mw.angx = sensors.GetRoll;
}

}

Expand Down
92 changes: 84 additions & 8 deletions src/com/ezio/multiwii/helpers/Sensors.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
package com.ezio.multiwii.helpers;

import java.util.Iterator;

import android.content.Context;
import android.hardware.GeomagneticField;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Criteria;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

public class Sensors implements SensorEventListener, LocationListener {
/* sensor data */

private Listener mListener = null;

public interface Listener {
public void onSensorsStateChange();
}

public void registerListener(Listener listener) {
mListener = listener;
}

private LocationManager locationManager;
private String provider;
GeomagneticField geoField;

int PhoneNumSat = 0;
double PhoneLatitude = 0;
double PhoneLongitude = 0;
double PhoneAltitude = 0;
double PhoneSpeed = 0;
int PhoneFix = 0;
float PhoneAccuracy = 0;
float Declination = 0;

SensorManager m_sensorManager;
float[] m_lastMagFields = new float[3];;
float[] m_lastAccels = new float[3];;
private float[] m_rotationMatrix = new float[16];
private float[] m_orientation = new float[4];

public float GetPitch = 0.f;
public float GetYaw = 0.f;
public float GetHeading = 0.f;
public float GetRoll = 0.f;

private Context context;
Expand All @@ -31,16 +60,53 @@ public Sensors(Context context) {

public void start() {
m_sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

Criteria criteria = new Criteria();
// if (!app.D)
criteria.setAccuracy(Criteria.ACCURACY_FINE);
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
geoField = new GeomagneticField(Double.valueOf(location.getLatitude()).floatValue(), Double.valueOf(location.getLongitude()).floatValue(), Double.valueOf(location.getAltitude()).floatValue(), System.currentTimeMillis());
}

locationManager.addGpsStatusListener(new GpsStatus.Listener() {

@Override
public void onGpsStatusChanged(int event) {
if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
GpsStatus status = locationManager.getGpsStatus(null);
Iterable<GpsSatellite> sats = status.getSatellites();
Iterator<GpsSatellite> it = sats.iterator();

PhoneNumSat = 0;
while (it.hasNext()) {

GpsSatellite oSat = (GpsSatellite) it.next();
if (oSat.usedInFix())
PhoneNumSat++;
}

}
if (event == GpsStatus.GPS_EVENT_FIRST_FIX)
PhoneFix = 1;
}
});

registerListeners();

}

private void registerListeners() {
m_sensorManager.registerListener(this, m_sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_GAME);
m_sensorManager.registerListener(this, m_sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
locationManager.requestLocationUpdates(provider, 0, 0, this);
}

private void unregisterListeners() {
m_sensorManager.unregisterListener(this);
locationManager.removeUpdates(this);
}

public void stop() {
Expand Down Expand Up @@ -75,14 +141,17 @@ private void computeOrientation() {
if (SensorManager.getRotationMatrix(m_rotationMatrix, null, m_lastAccels, m_lastMagFields)) {
SensorManager.getOrientation(m_rotationMatrix, m_orientation);

float yaw = (float) Math.toDegrees(m_orientation[0]);
float yaw = (float) (Math.toDegrees(m_orientation[0]) + Declination);
float pitch = (float) Math.toDegrees(m_orientation[1]);
float roll = (float) Math.toDegrees(m_orientation[2]);

GetYaw = filterYaw.lowPass(yaw);
GetHeading = filterYaw.lowPass(yaw);
GetPitch = filterPitch.lowPass(pitch);
GetRoll = filterRoll.lowPass(roll);

if (mListener != null)
mListener.onSensorsStateChange();

}
}

Expand All @@ -108,8 +177,14 @@ protected float lowPass(float input) {

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub

PhoneLatitude = location.getLatitude();
PhoneLongitude = location.getLongitude();
PhoneAltitude = location.getAltitude();
PhoneSpeed = location.getSpeed() * 100f;
PhoneAccuracy = location.getAccuracy() * 100f;

geoField = new GeomagneticField(Double.valueOf(location.getLatitude()).floatValue(), Double.valueOf(location.getLongitude()).floatValue(), Double.valueOf(location.getAltitude()).floatValue(), System.currentTimeMillis());
Declination = geoField.getDeclination();
}

@Override
Expand All @@ -129,4 +204,5 @@ public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}
}

}
1 change: 0 additions & 1 deletion src/com/ezio/multiwii/mw/MultirotorData.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import com.ezio.multiwii.helpers.FileAccess;
import com.ezio.multiwii.waypoints.Waypoint;
import com.google.android.maps.GeoPoint;

public abstract class MultirotorData {

Expand Down

0 comments on commit a51f384

Please sign in to comment.