我在加速度计和陀螺仪同时获取数据时遇到了一些问题。我需要使用这些传感器来获得加速度和方向,以便计算我正在进行的项目的相对位置。当两个传感器同时使用时,它会导致一些非常奇怪的数据输出,其中数据每秒只改变一次。当只有加速度计运行时,数据并不是更好,因为它在最快的设置上每秒仅改变大约10次。该数据打印到具有每个传感器读数的时间戳的文件中。
我无法在互联网上找到教程,特别是同时使用多个传感器。我需要将每个传感器放在不同的线程上吗?我没有太多使用线程的经验,我怎么能这样做。
答案 0 :(得分:4)
您无需将每个传感器放在不同的线程上。 Yo可以使用SensorManager类来注册和取消注册您需要的不同传感器
public class SensorActivity extends Activity, implements SensorEventListener {
private final SensorManager mSensorManager;
private final Sensor mAccelerometer;
private final Sensor mGyroscope;
public SensorActivity() {
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mGyroscope = mSensorManager.getDefaultSensor(TYPE_GYROSCOPE);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(this, mGyroscope, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
实现sensorEventList处理程序(特别是onSensorChanged方法)并检查谁属于收到的数据:
public void onSensorChanged(SensorEvent sensorEvent) {
sensorName = sensorEvent.sensor.getName();
Log.d(sensorName + ": X: " + sensorEvent.values[0] + "; Y: " + sensorEvent.values[1] + "; Z: " + sensorEvent.values[2] + ";");
}
答案 1 :(得分:1)
此Kotlin解决方案源自Kroka的解决方案:
class MainActivity : AppCompatActivity(), SensorEventListener {
private lateinit var mSensorManager:SensorManager
private lateinit var mAccelerometer :Sensor
private lateinit var mGyroscope :Sensor
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
mGyroscope = mSensorManager.getDefaultSensor(TYPE_GYROSCOPE)
}
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
}
override fun onSensorChanged(event: SensorEvent?) {
val sensorName: String = event?.sensor!!.getName();
Log.d("Sensor",sensorName + ": X: " + event.values[0] + "; Y: " + event.values[1] + "; Z: " + event.values[2] + ";")
}
override fun onResume() {
super.onResume()
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL)
mSensorManager.registerListener(this, mGyroscope, SensorManager.SENSOR_DELAY_NORMAL)
}
override fun onPause() {
super.onPause()
mSensorManager.unregisterListener(this)
}
}
答案 2 :(得分:0)
import android.content.pm.ActivityInfo;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textX, textY, textZ;
SensorManager sensorManager;
Sensor sensor;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
textX = findViewById(R.id.textX);
textY = findViewById(R.id.textY);
textZ = findViewById(R.id.textZ);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
public void onResume() {
super.onResume();
sensorManager.registerListener(gyroListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
public void onStop() {
super.onStop();
sensorManager.unregisterListener(gyroListener);
}
public SensorEventListener gyroListener = new SensorEventListener() {
public void onAccuracyChanged(Sensor sensor, int acc) {
}
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
textX.setText("X : " + (int) x + " rad/s");
textY.setText("Y : " + (int) y + " rad/s");
textZ.setText("Z : " + (int) z + " rad/s");
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textX"
android:layout_width="209dp"
android:layout_height="79dp"
android:layout_marginStart="91dp"
android:layout_marginLeft="91dp"
android:layout_marginEnd="111dp"
android:layout_marginRight="111dp"
android:gravity="center"
android:text=""
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@+id/textY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textY"
android:layout_width="209dp"
android:layout_height="79dp"
android:layout_marginStart="91dp"
android:layout_marginLeft="91dp"
android:layout_marginEnd="111dp"
android:layout_marginRight="111dp"
android:layout_marginBottom="8dp"
android:gravity="center"
android:text=""
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@+id/textZ"
app:layout_constraintEnd_toEndOf="@+id/textX"
app:layout_constraintHorizontal_bias="0.445"
app:layout_constraintStart_toStartOf="@+id/textX"
app:layout_constraintTop_toBottomOf="@+id/textX"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textZ"
android:layout_width="209dp"
android:layout_height="79dp"
android:layout_marginStart="153dp"
android:layout_marginLeft="153dp"
android:layout_marginEnd="49dp"
android:layout_marginRight="49dp"
android:layout_marginBottom="241dp"
android:gravity="center"
android:text=""
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/textY"
app:layout_constraintHorizontal_bias="0.757"
app:layout_constraintStart_toStartOf="@+id/textY"
app:layout_constraintTop_toBottomOf="@+id/textY"
tools:ignore="MissingConstraints" />
</android.support.constraint.ConstraintLayout>