当您使用TYPE_MAGNETOMETER传感器时,您将获得相对于设备方向的磁场强度的X,Y,Z值。我想得到的是将这些值转换为全局参考框架,澄清:用户获取设备,测量这些值,而不是将设备旋转任何轴周围一定程度并获得相同的值。 请在下面找到类似的问题: Getting magnetic field values in global coordinates How can I get the magnetic field vector, independent of the device rotation? 在这个答案中描述了样本解决方案(它用于线性加速,但我认为无关紧要):https://stackoverflow.com/a/11614404/2152255 我使用它并且我得到3个值,X总是非常小(不要认为它是正确的),Y和Z都可以,但是当我旋转设备时它们仍然改变了一点。怎么可以调整?它可以全部解决吗?我使用简单的卡尔曼滤波器来近似测量值,因为即使设备根本没有移动/旋转,我也会得到不同的值。请在下面找到我的代码:
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.opengl.Matrix;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import com.test.statistics.filter.kalman.KalmanState;
import com.example.R;
/**
* Activity for gathering magnetic field statistics.
*/
public class MagneticFieldStatisticsGatheringActivity extends Activity implements SensorEventListener {
public static final int KALMAN_STATE_MAX_SIZE = 80;
public static final double MEASUREMENT_NOISE = 5;
/** Sensor manager. */
private SensorManager mSensorManager;
/** Magnetometer spec. */
private TextView vendor;
private TextView resolution;
private TextView maximumRange;
/** Magnetic field coordinates measurements. */
private TextView magneticXTextView;
private TextView magneticYTextView;
private TextView magneticZTextView;
/** Sensors. */
private Sensor mAccelerometer;
private Sensor mGeomagnetic;
private float[] accelerometerValues;
private float[] geomagneticValues;
/** Flags. */
private boolean specDefined = false;
private boolean kalmanFiletring = false;
/** Rates. */
private float nanoTtoGRate = 0.00001f;
private final int gToCountRate = 1000000;
/** Kalman vars. */
private KalmanState previousKalmanStateX;
private KalmanState previousKalmanStateY;
private KalmanState previousKalmanStateZ;
private int previousKalmanStateCounter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mGeomagnetic = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
vendor = (TextView) findViewById(R.id.vendor);
resolution = (TextView) findViewById(R.id.resolution);
maximumRange = (TextView) findViewById(R.id.maximumRange);
magneticXTextView = (TextView) findViewById(R.id.magneticX);
magneticYTextView = (TextView) findViewById(R.id.magneticY);
magneticZTextView = (TextView) findViewById(R.id.magneticZ);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this, mGeomagnetic, SensorManager.SENSOR_DELAY_FASTEST);
}
/**
* Refresh statistics.
*
* @param view - refresh button view.
*/
public void onClickRefreshMagneticButton(View view) {
resetKalmanFilter();
}
/**
* Switch Kalman filtering on/off
*
* @param view - Klaman filetring switcher (checkbox)
*/
public void onClickKalmanFilteringCheckBox(View view) {
CheckBox kalmanFiltering = (CheckBox) view;
this.kalmanFiletring = kalmanFiltering.isChecked();
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
return;
}
synchronized (this) {
switch(sensorEvent.sensor.getType()){
case Sensor.TYPE_ACCELEROMETER:
accelerometerValues = sensorEvent.values.clone();
break;
case Sensor.TYPE_MAGNETIC_FIELD:
if (!specDefined) {
vendor.setText("Vendor: " + sensorEvent.sensor.getVendor() + " " + sensorEvent.sensor.getName());
float resolutionValue = sensorEvent.sensor.getResolution() * nanoTtoGRate;
resolution.setText("Resolution: " + resolutionValue);
float maximumRangeValue = sensorEvent.sensor.getMaximumRange() * nanoTtoGRate;
maximumRange.setText("Maximum range: " + maximumRangeValue);
}
geomagneticValues = sensorEvent.values.clone();
break;
}
if (accelerometerValues != null && geomagneticValues != null) {
float[] Rs = new float[16];
float[] I = new float[16];
if (SensorManager.getRotationMatrix(Rs, I, accelerometerValues, geomagneticValues)) {
float[] RsInv = new float[16];
Matrix.invertM(RsInv, 0, Rs, 0);
float resultVec[] = new float[4];
float[] geomagneticValuesAdjusted = new float[4];
geomagneticValuesAdjusted[0] = geomagneticValues[0];
geomagneticValuesAdjusted[1] = geomagneticValues[1];
geomagneticValuesAdjusted[2] = geomagneticValues[2];
geomagneticValuesAdjusted[3] = 0;
Matrix.multiplyMV(resultVec, 0, RsInv, 0, geomagneticValuesAdjusted, 0);
for (int i = 0; i < resultVec.length; i++) {
resultVec[i] = resultVec[i] * nanoTtoGRate * gToCountRate;
}
if (kalmanFiletring) {
KalmanState currentKalmanStateX = new KalmanState(MEASUREMENT_NOISE, accelerometerValues[0], (double)resultVec[0], previousKalmanStateX);
previousKalmanStateX = currentKalmanStateX;
KalmanState currentKalmanStateY = new KalmanState(MEASUREMENT_NOISE, accelerometerValues[1], (double)resultVec[1], previousKalmanStateY);
previousKalmanStateY = currentKalmanStateY;
KalmanState currentKalmanStateZ = new KalmanState(MEASUREMENT_NOISE, accelerometerValues[2], (double)resultVec[2], previousKalmanStateZ);
previousKalmanStateZ = currentKalmanStateZ;
if (previousKalmanStateCounter == KALMAN_STATE_MAX_SIZE) {
magneticXTextView.setText("x: " + previousKalmanStateX.getX_estimate());
magneticYTextView.setText("y: " + previousKalmanStateY.getX_estimate());
magneticZTextView.setText("z: " + previousKalmanStateZ.getX_estimate());
resetKalmanFilter();
} else {
previousKalmanStateCounter++;
}
} else {
magneticXTextView.setText("x: " + resultVec[0]);
magneticYTextView.setText("y: " + resultVec[1]);
magneticZTextView.setText("z: " + resultVec[2]);
}
}
}
}
}
private void resetKalmanFilter() {
previousKalmanStateX = null;
previousKalmanStateY = null;
previousKalmanStateZ = null;
previousKalmanStateCounter = 0;
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
}
感谢所有阅读此帖的人,并提前发布了有关该问题的一些想法。
答案 0 :(得分:6)
根据上述说明,请执行此操作
private static final int TEST_GRAV = Sensor.TYPE_ACCELEROMETER;
private static final int TEST_MAG = Sensor.TYPE_MAGNETIC_FIELD;
private final float alpha = (float) 0.8;
private float gravity[] = new float[3];
private float magnetic[] = new float[3];
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
if (sensor.getType() == TEST_GRAV) {
// Isolate the force of gravity with the low-pass filter.
gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
} else if (sensor.getType() == TEST_MAG) {
magnetic[0] = event.values[0];
magnetic[1] = event.values[1];
magnetic[2] = event.values[2];
float[] R = new float[9];
float[] I = new float[9];
SensorManager.getRotationMatrix(R, I, gravity, magnetic);
float [] A_D = event.values.clone();
float [] A_W = new float[3];
A_W[0] = R[0] * A_D[0] + R[1] * A_D[1] + R[2] * A_D[2];
A_W[1] = R[3] * A_D[0] + R[4] * A_D[1] + R[5] * A_D[2];
A_W[2] = R[6] * A_D[0] + R[7] * A_D[1] + R[8] * A_D[2];
Log.d("Field","\nX :"+A_W[0]+"\nY :"+A_W[1]+"\nZ :"+A_W[2]);
}
}
答案 1 :(得分:0)
Stochastically给出了一个答案,我觉得比这里发布的要好。
https://stackoverflow.com/a/16418016/4033525
似乎
## Add an 'id' so we know which points go into which polygons
df1$id <- 1:nrow( df1 )
dt <- as.data.table( df1 )
dt <- melt(dt, id.vars = c("id"), measure.vars = names(dt)[grepl("\\.l", names( dt ) )])
## Because youre columns are named 'p1., p2., p3.' we can use the number to put the points
## in the correct order
dt[, point_sequence := gsub("[^0-9]","",variable)]
## and remove the p1. bits
dt[, lonlat := gsub("p[0-9]\\.", "", variable)]
setorder( dt, id, point_sequence )
## reshape again to have separate lon & lat columns
## I'm doing this using a 'join' in data.table
dt <- dt[ lonlat == "lon", .(id, point_sequence, value) ][
dt[ lonlat == "lat", .(id, point_sequence, value) ]
, on = .(id, point_sequence)
, nomatch = 0
]
setnames( dt, c("value", "i.value"), c("lon", "lat"))
sf_poly <- sfheaders::sf_polygon(
obj = dt
, x = "lon"
, y = "lat"
, polygon_id = "id"
)
sf::st_crs( sf_poly ) <- 4326
sf_poly
# Simple feature collection with 6 features and 1 field
# geometry type: POLYGON
# dimension: XY
# bbox: xmin: 0.8662118 ymin: 51.28801 xmax: 0.8662981 ymax: 51.28808
# geographic CRS: WGS 84
# id geometry
# 1 1 POLYGON ((0.8662675 51.2880...
# 2 2 POLYGON ((0.866261 51.28806...
# 3 3 POLYGON ((0.866255 51.28805...
# 4 4 POLYGON ((0.8662485 51.2880...
# 5 5 POLYGON ((0.866242 51.28803...
# 6 6 POLYGON ((0.866236 51.28802...
## If you want the 'Value' column put back on
sf::st_geometry( df1 ) <- NULL
sf_poly <- merge(
x = sf_poly
, y = as.data.frame( df1[, c("id", "Value")] )
, by = "id"
)
无法正确转换为世界框架。
正确的代码是:
SensorManager.getOrientation()