问题描述
在我的项目,我想控制通过使用Android设备的加速度计获得的数据一辆车。 (左,右,前,反向)。尽管我已成功地从加速计读取值读数变动频繁,即使该设备是在稳定的位置。谁能给我提供一个更好的方式来做到这一点?
以下是code,我是用
进口android.content.Context;
进口android.hardware.Sensor;
进口android.hardware.SensorEvent;
进口android.hardware.SensorEventListener;
进口android.hardware.SensorManager;
公共类AccelerometerService {
私有静态的SensorManager的SensorManager;
私有静态SensorEventListener sensorEventListener;
私有静态布尔开始= FALSE;
私有静态浮动[]加速度=新的浮动[3];
私有静态浮动[] magneticField =新的浮动[3];
私有静态浮动[] rotationMatrix =新的浮动[9];
私有静态浮动[] inclinationMatrix =新的浮动[9];
私有静态浮动[]的态度=新的浮动[3];
私人最终静态双RAD2DEG = 180 / Math.PI;
私有静态诠释initialAzimuth = 0;
私有静态诠释initialPitch = 0;
私有静态诠释initialRoll = 0;
私有静态诠释[] attitudeInDegrees =新INT [3];
公共静态无效的开始(最终上下文的applicationContext){
如果(开始){
返回;
}
的SensorManager =(的SensorManager)的applicationContext
.getSystemService(Context.SENSOR_SERVICE);
sensorEventListener =新SensorEventListener(){
@覆盖
公共无效onSensorChanged(SensorEvent事件){
整型= event.sensor.getType();
如果(类型== Sensor.TYPE_MAGNETIC_FIELD){
magneticField = event.values.clone();
}
如果(类型== Sensor.TYPE_ACCELEROMETER){
加速度= event.values.clone();
}
SensorManager.getRotationMatrix(rotationMatrix,inclinationMatrix,加速度计,magneticField);
SensorManager.getOrientation(rotationMatrix,态度);
attitudeInDegrees [0] =(int)的Math.round(姿态[0] * RAD2DEG); //方位
attitudeInDegrees [1] =(int)的Math.round(姿态[1] * RAD2DEG); //沥青
attitudeInDegrees [2] =(int)的Math.round(姿态[2] * RAD2DEG); //滚
}
@覆盖
公共无效onAccuracyChanged(传感器传感器,诠释精度){
}
};
sensorManager.registerListener(sensorEventListener,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
SensorManager.SENSOR_DELAY_UI);
sensorManager.registerListener(sensorEventListener,
sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD)
SensorManager.SENSOR_DELAY_UI);
开始= TRUE;
}
公共静态布尔getStarted(){
返回开始;
}
公共静态无效停止(){
如果(开始){
sensorManager.unregisterListener(sensorEventListener);
开始= FALSE;
}
}
}
老问题,但我在这里回答为他人着想是找到这个问题的。
首先,以下@安德鲁 - 莫顿链接正是我需要的,但在伪code,所以这里的Java为Android!
二:如果你能(你是最小的API拉特是9或更高版本),只需使用Sensor.TYPE_GRAVITY。因为它已经理顺。如果你需要支持旧的API Lvls(像我不得不),这code应该做的伎俩!
最后:这code段有所修改,从我使用它,如果你想用这个为自己,你需要创建一个getter为Vector3类型的重力是什么
。注意:我发现,5(MAX_SAMPLE_SIZE)的滚动平均值是比较顺利的。 10更是顺利,但你开始注意到滞后。
进口android.hardware.Sensor;
进口android.hardware.SensorEvent;
进口android.hardware.SensorEventListener;
进口android.hardware.SensorManager;
进口的java.util.ArrayList;
进口的java.util.List;
/ **
*在14年6月14日创建者安迪。
* /
公共类AndroidGravityUpdate实现SensorEventListener {
私人的SensorManager的SensorManager;
Vector3类型的重力;
名单<浮动> [] rollingAverage =新名单[3];
私有静态最终诠释MAX_SAMPLE_SIZE = 5;
AndroidGravityUpdate(的SensorManager的SensorManager){
this.gravity =新Vector3类型();
this.sensorManager =的SensorManager;
如果(sensorManager.getSensorList(Sensor.TYPE_GRAVITY).size()大于0){
sensorManager.registerListener(
本,
sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY)
SensorManager.SENSOR_DELAY_GAME
);
}否则如果(sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).size()大于0){
rollingAverage [0] =新的ArrayList<浮动>();
rollingAverage [1] =新的ArrayList&其中;浮法>();
rollingAverage [2] =新的ArrayList&其中;浮法>();
sensorManager.registerListener(
本,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
SensorManager.SENSOR_DELAY_GAME
);
}
}
@覆盖
公共无效onSensorChanged(SensorEvent事件){
如果(event.sensor.getType()== Sensor.TYPE_GRAVITY){
gravity.z = event.values [0];
gravity.x = event.values [1];
gravity.y = - event.values [2];
}
否则如果(event.sensor.getType()== Sensor.TYPE_ACCELEROMETER){
//无论出于何种原因,我的三星只有加速度
//但它是非常粗糙的,因此试图平稳
//它与滚动平均值。
rollingAverage [0] =辊(rollingAverage [0],event.values [0]);
rollingAverage [1] =辊(rollingAverage [1],event.values [1]);
rollingAverage [2] =辊(rollingAverage [2],-event.values [2]);
gravity.z = averageList(rollingAverage [0]);
gravity.x = averageList(rollingAverage [1]);
gravity.y = averageList(rollingAverage [2]);
}
}
公开名单<浮动>卷(册及LT;浮动>清单,浮动newMember){
如果(则为list.size()== MAX_SAMPLE_SIZE){
list.remove(0);
}
list.add(newMember);
返回列表;
}
公众持股量averageList(名单<浮动> tallyUp){
总浮= 0;
为(浮动项:tallyUp){
共有+ =项目;
}
总=总/ tallyUp.size();
总回报;
}
@覆盖
公共无效onAccuracyChanged(传感器传感器,诠释精度){
}
}
一流的Vector3类型{
浮动X;
浮动Ÿ;
浮动ž;
}
In a my project I am trying to control a car using data obtained through the accelerometer of an Android device. (Left, Right, Forward, Reverse). Even though I have managed to read values from the accelerometer the readings are subject to frequent changes even the device is in a stable position. Can someone provide me a better way to do this?
Following is the code that I have used
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class AccelerometerService {
private static SensorManager sensorManager;
private static SensorEventListener sensorEventListener;
private static boolean started = false;
private static float[] accelerometer = new float[3];
private static float[] magneticField = new float[3];
private static float[] rotationMatrix = new float[9];
private static float[] inclinationMatrix = new float[9];
private static float[] attitude = new float[3];
private final static double RAD2DEG = 180/Math.PI;
private static int initialAzimuth = 0;
private static int initialPitch = 0;
private static int initialRoll = 0;
private static int[] attitudeInDegrees = new int[3];
public static void start(final Context applicationContext) {
if(started) {
return;
}
sensorManager = (SensorManager) applicationContext
.getSystemService(Context.SENSOR_SERVICE);
sensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
int type = event.sensor.getType();
if(type == Sensor.TYPE_MAGNETIC_FIELD) {
magneticField = event.values.clone();
}
if(type == Sensor.TYPE_ACCELEROMETER) {
accelerometer = event.values.clone();
}
SensorManager.getRotationMatrix(rotationMatrix, inclinationMatrix, accelerometer, magneticField);
SensorManager.getOrientation(rotationMatrix, attitude);
attitudeInDegrees[0] = (int) Math.round(attitude[0] * RAD2DEG); //azimuth
attitudeInDegrees[1] = (int) Math.round(attitude[1] * RAD2DEG); //pitch
attitudeInDegrees[2] = (int) Math.round(attitude[2] * RAD2DEG); //roll
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
sensorManager.registerListener(sensorEventListener,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
sensorManager.registerListener(sensorEventListener,
sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_UI);
started = true;
}
public static boolean getStarted() {
return started;
}
public static void stop() {
if(started) {
sensorManager.unregisterListener(sensorEventListener);
started = false;
}
}
}
Old question, but I'm answering here for the sake of others that find this question.
First off, following @andrew-morton link is exactly what I needed, but was in pseudo code, so here is Java for Android!
Second: If you can (you're min API Lvl is 9 or higher), simply use the Sensor.TYPE_GRAVITY. As it is already smoothed out. If you need to support older API Lvls (like I had to), this code should do the trick!
Lastly: This code snippet is somewhat modified from what I am using it for, if you want to use this for yourself, you'll need to create a getter for the Vector3 gravity.
Notes:I've found that a rolling average of 5 (MAX_SAMPLE_SIZE) is quite smooth. 10 is even more smooth, but you start to notice lag.
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import java.util.ArrayList;
import java.util.List;
/**
* Created by andy on 6/14/14.
*/
public class AndroidGravityUpdate implements SensorEventListener {
private SensorManager sensorManager;
Vector3 gravity;
List<Float>[] rollingAverage = new List[3];
private static final int MAX_SAMPLE_SIZE = 5;
AndroidGravityUpdate( SensorManager sensorManager ) {
this.gravity = new Vector3();
this.sensorManager = sensorManager;
if(sensorManager.getSensorList(Sensor.TYPE_GRAVITY).size() > 0){
sensorManager.registerListener(
this,
sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY),
SensorManager.SENSOR_DELAY_GAME
);
} else if( sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() > 0) {
rollingAverage[0] = new ArrayList<Float>();
rollingAverage[1] = new ArrayList<Float>();
rollingAverage[2] = new ArrayList<Float>();
sensorManager.registerListener(
this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_GAME
);
}
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType()==Sensor.TYPE_GRAVITY){
gravity.z = event.values[0];
gravity.x = event.values[1];
gravity.y = - event.values[2];
}
else if ( event.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {
//For whatever reason, my Samsung only has "Accelerometer"
// But it is incredibly rough, so attempting to smooth
// it out with rolling averages.
rollingAverage[0] = roll(rollingAverage[0], event.values[0]);
rollingAverage[1] = roll(rollingAverage[1], event.values[1]);
rollingAverage[2] = roll(rollingAverage[2], -event.values[2]);
gravity.z = averageList(rollingAverage[0]);
gravity.x = averageList(rollingAverage[1]);
gravity.y = averageList(rollingAverage[2]);
}
}
public List<Float> roll(List<Float> list, float newMember){
if(list.size() == MAX_SAMPLE_SIZE){
list.remove(0);
}
list.add(newMember);
return list;
}
public float averageList(List<Float> tallyUp){
float total=0;
for(float item : tallyUp ){
total+=item;
}
total = total/tallyUp.size();
return total;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
class Vector3 {
float x;
float y;
float z;
}
这篇关于如何在稳定,准确地读取Android的加速度值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!