|
|
@@ -19,33 +19,28 @@ import crown.android.CrownEnum;
|
|
|
*/
|
|
|
public class CrownAccelerometer
|
|
|
{
|
|
|
- private final float MIN_VALUE = -1.0f;
|
|
|
- private final float MAX_VALUE = 1.0f;
|
|
|
+ private static final float MIN_VALUE = 0.0f;
|
|
|
+ private static final float MAX_VALUE = 1.0f;
|
|
|
|
|
|
- private SensorManager sensorManager;
|
|
|
- private Sensor sensor;
|
|
|
- private SensorEventListener sensorEventListener;
|
|
|
+ private static SensorManager sensorManager;
|
|
|
+ private static Sensor sensor;
|
|
|
+ private static SensorEventListener sensorEventListener;
|
|
|
|
|
|
- private float x;
|
|
|
- private float y;
|
|
|
- private float z;
|
|
|
- private float lastX;
|
|
|
- private float lastY;
|
|
|
- private float lastZ;
|
|
|
+ private static float mX;
|
|
|
+ private static float mY;
|
|
|
+ private static float mZ;
|
|
|
+ private static float lastX;
|
|
|
+ private static float lastY;
|
|
|
+ private static float lastZ;
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
- public CrownAccelerometer()
|
|
|
- {
|
|
|
- }
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------------
|
|
|
- private boolean initAccelerometer(Context context)
|
|
|
+ private static boolean initAccelerometer(Context context)
|
|
|
{
|
|
|
// already initialized!
|
|
|
- if (sensorEventListener != null)
|
|
|
- {
|
|
|
- return true;
|
|
|
- }
|
|
|
+ // if (sensorEventListener != null)
|
|
|
+ // {
|
|
|
+ // return true;
|
|
|
+ // }
|
|
|
|
|
|
sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
|
|
|
|
|
|
@@ -61,10 +56,15 @@ public class CrownAccelerometer
|
|
|
@Override
|
|
|
public void onSensorChanged(SensorEvent event)
|
|
|
{
|
|
|
- lowPassFiltering(event.values[0], event.values[1], event.values[2]);
|
|
|
+ mX = event.values[0];
|
|
|
+ mY = event.values[1];
|
|
|
+ mZ = event.values[2];
|
|
|
|
|
|
- CrownLib.pushFloatEvent(CrownEnum.OSET_ACCELEROMETER, x, y, z, 0);
|
|
|
+ CrownLib.pushFloatEvent(CrownEnum.OSET_ACCELEROMETER, mX, mY, mZ, 0);
|
|
|
|
|
|
+ lastX = mX;
|
|
|
+ lastY = mY;
|
|
|
+ lastZ = mZ;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
@@ -73,97 +73,96 @@ public class CrownAccelerometer
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
- public boolean startListening(Context context)
|
|
|
+ public static boolean startListening(Context context)
|
|
|
{
|
|
|
return initAccelerometer(context);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
- public void stopListening()
|
|
|
+ public static void stopListening()
|
|
|
{
|
|
|
sensorManager.unregisterListener(sensorEventListener);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
- public float getX()
|
|
|
+ public static float getX()
|
|
|
{
|
|
|
- return x;
|
|
|
+ return mX;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
- public float getY()
|
|
|
+ public static float getY()
|
|
|
{
|
|
|
- return y;
|
|
|
+ return mY;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
- public float getZ()
|
|
|
+ public static float getZ()
|
|
|
{
|
|
|
- return z;
|
|
|
+ return mZ;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
- public float getLastX()
|
|
|
+ public static float getLastX()
|
|
|
{
|
|
|
return lastX;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
- public float getLastY()
|
|
|
+ public static float getLastY()
|
|
|
{
|
|
|
return lastY;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------------
|
|
|
- public float getLastZ()
|
|
|
+ public static float getLastZ()
|
|
|
{
|
|
|
return lastZ;
|
|
|
}
|
|
|
|
|
|
-//-----------------------------------------------------------------------------------
|
|
|
- private void lowPassFiltering(float uX, float uY, float uZ)
|
|
|
- {
|
|
|
- float updateFreq = 30; // match this to your update speed
|
|
|
- float cutOffFreq = 0.9f;
|
|
|
- float timeRC = 1.0f / cutOffFreq;
|
|
|
- float dt = 1.0f / updateFreq;
|
|
|
- float filterConstant = timeRC / (dt + timeRC);
|
|
|
- float alpha = filterConstant;
|
|
|
- float kAccelerometerMinStep = 0.033f;
|
|
|
- float kAccelerometerNoiseAttenuation = 3.0f;
|
|
|
-
|
|
|
- float d = clamp(Math.abs(norm(x, y, z) - norm(uX, uY, uZ)) / kAccelerometerMinStep - 1.0f, MIN_VALUE, MAX_VALUE);
|
|
|
- alpha = d * filterConstant / kAccelerometerNoiseAttenuation + (1.0f - d) * filterConstant;
|
|
|
-
|
|
|
- x = (float) (alpha * (x + uX - lastX));
|
|
|
- y = (float) (alpha * (y + uY - lastY));
|
|
|
- z = (float) (alpha * (z + uZ - lastZ));
|
|
|
-
|
|
|
-
|
|
|
- lastX = x;
|
|
|
- lastY = y;
|
|
|
- lastZ = z;
|
|
|
- }
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------------
|
|
|
- private float clamp(float v, float min, float max)
|
|
|
- {
|
|
|
- if (v < min)
|
|
|
- {
|
|
|
- v = min;
|
|
|
- }
|
|
|
- else if (v > max)
|
|
|
- {
|
|
|
- v = max;
|
|
|
- }
|
|
|
-
|
|
|
- return v;
|
|
|
- }
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------------
|
|
|
- private float norm(float x, float y, float z)
|
|
|
- {
|
|
|
- float v = (float)Math.pow(x, 2) + (float)Math.pow(y, 2) + (float)Math.pow(z, 2);
|
|
|
- return (float)Math.sqrt(v);
|
|
|
- }
|
|
|
+// //-----------------------------------------------------------------------------------
|
|
|
+// private void lowPassFiltering(float uX, float uY, float uZ)
|
|
|
+// {
|
|
|
+// float updateFreq = 30; // match this to your update speed
|
|
|
+// float cutOffFreq = 0.9f;
|
|
|
+// float timeRC = 1.0f / cutOffFreq;
|
|
|
+// float dt = 1.0f / updateFreq;
|
|
|
+// float filterConstant = timeRC / (dt + timeRC);
|
|
|
+// float alpha = filterConstant;
|
|
|
+// float kAccelerometerMinStep = 0.033f;
|
|
|
+// float kAccelerometerNoiseAttenuation = 3.0f;
|
|
|
+
|
|
|
+// float d = clamp((Math.abs(norm(mX, mY, mZ) - norm(uX, uY, uZ)) / kAccelerometerMinStep - 1.0f), MIN_VALUE, MAX_VALUE);
|
|
|
+// alpha = d * filterConstant / kAccelerometerNoiseAttenuation + (1.0f - d) * filterConstant;
|
|
|
+
|
|
|
+// mX = (float) (alpha * (mX + uX - lastX));
|
|
|
+// mY = (float) (alpha * (mY + uY - lastY));
|
|
|
+// mZ = (float) (alpha * (mZ + uZ - lastZ));
|
|
|
+
|
|
|
+// lastX = mX;
|
|
|
+// lastY = mY;
|
|
|
+// lastZ = mZ;
|
|
|
+// }
|
|
|
+
|
|
|
+// //-----------------------------------------------------------------------------------
|
|
|
+// private float clamp(float v, float min, float max)
|
|
|
+// {
|
|
|
+// if (v <= min)
|
|
|
+// {
|
|
|
+// v = min;
|
|
|
+// }
|
|
|
+// else if (v >= max)
|
|
|
+// {
|
|
|
+// v = max;
|
|
|
+// }
|
|
|
+
|
|
|
+// return v;
|
|
|
+// }
|
|
|
+
|
|
|
+// //-----------------------------------------------------------------------------------
|
|
|
+// private float norm(float x, float y, float z)
|
|
|
+// {
|
|
|
+// float v = (float)Math.pow(x, 2) + (float)Math.pow(y, 2) + (float)Math.pow(z, 2);
|
|
|
+// return (float)Math.sqrt(v);
|
|
|
+// }
|
|
|
}
|