Skip to content

Commit

Permalink
LowPassFilter added to dashboard 2
Browse files Browse the repository at this point in the history
  • Loading branch information
eziosoft committed Mar 26, 2013
1 parent 295a29d commit 96bc433
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 49 deletions.
1 change: 1 addition & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
eclipse.preferences.version=1
encoding//src/com/ezio/multiwii/helpers/Functions.java=UTF-8
encoding//src/com/ezio/multiwii/helpers/LowPassFilter.java=UTF-8
encoding//src/com/ezio/multiwii/helpers/Sensors.java=UTF-8
2 changes: 1 addition & 1 deletion AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"/>
android:targetSdkVersion="17" />

<application
android:name=".app.App"
Expand Down
11 changes: 9 additions & 2 deletions src/com/ezio/multiwii/dashboard/Dashboard2View.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import com.ezio.multiwii.R;
import com.ezio.multiwii.helpers.Functions;
import com.ezio.multiwii.helpers.LowPassFilter;

public class Dashboard2View extends View {

Expand Down Expand Up @@ -91,6 +92,9 @@ public class Dashboard2View extends View {
// scientific
// notation

LowPassFilter lowPassFilterRoll;
LowPassFilter lowPassFilterPitch;

public void Set(int satNum, float distanceToHome, float directionToHome, float speed, float gpsAltitude, float altitude, float lat, float lon, float pitch, float roll, float azimuth, float verticalSpeed, String state, int vbat, int powerSum, int powerTrigger, int txRSSI, int rxRSSI) {
SatNum = satNum;
DistanceToHome = distanceToHome;
Expand All @@ -110,7 +114,7 @@ public void Set(int satNum, float distanceToHome, float directionToHome, float s
PowerTrigger = powerTrigger;
TXRSSI = txRSSI;
RXRSSI = rxRSSI;
horizon.Set((int) pitch, (int) roll);
horizon.Set(lowPassFilterPitch.lowPass(pitch), lowPassFilterRoll.lowPass(roll));
this.invalidate();

}
Expand Down Expand Up @@ -189,6 +193,9 @@ private void setColorsAndFonts() {

horizon = new HorizonClass(context, null);
horizon.onSizeChanged(hh, hh);

lowPassFilterPitch = new LowPassFilter(0.2f);
lowPassFilterRoll = new LowPassFilter(0.2f);
}

void drawCompass(Canvas c, float x, float y, float wight, int step, int range, int value) {
Expand Down Expand Up @@ -237,7 +244,7 @@ protected void onDraw(Canvas c) {
}

// debug
if (true) {
if (false) {
SatNum = 5;
DistanceToHome = 254;
DirectionToHome = 45;
Expand Down
29 changes: 9 additions & 20 deletions src/com/ezio/multiwii/dashboard/HorizonClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ public class HorizonClass {
Paint mPaint;
Rect DrawingRec;
int ww = 0, hh = 0;
int tmp = 0;
float tmp = 0;

Bitmap[] bmp = new Bitmap[4];

Matrix matrix = new Matrix();

Context context;

public int roll = 15;
public int pitch = 20;
public float roll = 15;
public float pitch = 20;

public HorizonClass(Context context, AttributeSet attrs) {
// super(context, attrs);
Expand All @@ -51,8 +51,8 @@ public void init() {
DrawingRec = new Rect();
}

public void Set(int pitch, int roll) {
this.roll = roll;
public void Set(float pitch, float roll) {
this.roll = -roll;
this.pitch = pitch;
}

Expand All @@ -67,6 +67,10 @@ public void Draw(Canvas c, int x, int y) {
c.drawBitmap(bmp[0], matrix, null);

matrix.reset();
if (pitch > 90)
pitch = 90;
if (pitch < -90)
pitch = -90;
tmp = Functions.map(pitch, -90, 90, -(bmp[1].getHeight() / 2), bmp[1].getHeight() / 2);
matrix.postRotate(roll, bmp[1].getWidth() / 2, bmp[1].getHeight() / 2 - tmp);
matrix.postTranslate(x + (ww - bmp[1].getWidth()) / 2, y + ((hh - bmp[1].getHeight()) / 2) + tmp);
Expand All @@ -86,14 +90,6 @@ public void Draw(Canvas c, int x, int y) {
}

public void onSizeChanged(int w, int h) {
// super.onSizeChanged(w, h, oldw, oldh);
// Account for padding
// float xpad = (float) (getPaddingLeft() + getPaddingRight());
// float ypad = (float) (getPaddingTop() + getPaddingBottom());

// ww = (int) (w - xpad);
// hh = (int) (h - ypad);

ww = (int) (w);
hh = (int) (h);

Expand All @@ -110,13 +106,6 @@ public void onSizeChanged(int w, int h) {

}

// @Override
// protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
// int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
// this.setMeasuredDimension(parentWidth, parentWidth);
// }

// Scale and keep aspect ratio
private Bitmap scaleToFill(Bitmap b, int width, int height) {
float factorH = height / (float) b.getWidth();
Expand Down
24 changes: 24 additions & 0 deletions src/com/ezio/multiwii/helpers/LowPassFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.ezio.multiwii.helpers;

public class LowPassFilter {
/*
* time smoothing constant for low-pass filter 0 ≤ alpha ≤ 1 ; a smaller
* value basically means more smoothing See: http://en.wikipedia.org/wiki
* /Low-pass_filter#Discrete-time_realization
*/
float ALPHA = 0f;
float lastOutput = 0;

public LowPassFilter(float ALPHA) {
this.ALPHA = ALPHA;
}

public float lowPass(float input) {
if (Math.abs(input - lastOutput) > 170) {
lastOutput = input;
return lastOutput;
}
lastOutput = lastOutput + ALPHA * (input - lastOutput);
return lastOutput;
}
}
30 changes: 4 additions & 26 deletions src/com/ezio/multiwii/helpers/Sensors.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,30 +190,6 @@ private void computeOrientation() {
}
}

public class LowPassFilter {
/*
* time smoothing constant for low-pass filter 0 ≤ alpha ≤ 1 ; a smaller
* value basically means more smoothing See:
* http://en.wikipedia.org/wiki
* /Low-pass_filter#Discrete-time_realization
*/
float ALPHA = 0f;
float lastOutput = 0;

public LowPassFilter(float ALPHA) {
this.ALPHA = ALPHA;
}

protected float lowPass(float input) {
if (Math.abs(input - lastOutput) > 170) {
lastOutput = input;
return lastOutput;
}
lastOutput = lastOutput + ALPHA * (input - lastOutput);
return lastOutput;
}
}

@Override
public void onLocationChanged(Location location) {

Expand Down Expand Up @@ -257,8 +233,10 @@ public void onStatusChanged(String provider, int status, Bundle extras) {

public org.osmdroid.util.GeoPoint getNextPredictedLocationOfflineMap() {
if (location != null) {
// double lat = (location.getLatitude() + (location.getLatitude() - oldLocation.getLatitude()));
// double lon = (location.getLongitude() + (location.getLongitude() - oldLocation.getLongitude()));
// double lat = (location.getLatitude() + (location.getLatitude() -
// oldLocation.getLatitude()));
// double lon = (location.getLongitude() + (location.getLongitude()
// - oldLocation.getLongitude()));

return new org.osmdroid.util.GeoPoint(location.getLatitude() + (location.getLatitude() - oldLocation.getLatitude()), location.getLongitude() + (location.getLongitude() - oldLocation.getLongitude()));
} else
Expand Down

0 comments on commit 96bc433

Please sign in to comment.