各位大侠,
我在使用SDK中的gyroscope代码时,旋转机器,显示旋转的度数不正确。
在旋转机台的时候,还没用旋转到90°,代码显示的度数就已经到90°了。
希望高手能帮忙解决啊?
Sensor.TYPE_GYROSCOPE:private static final float NS2S = 1.0f / 1000000000.0f;
private float timestamp;
public void onSensorChanged(SensorEvent event){
if (timestamp != 0) {
final float dT = (event.timestamp - timestamp) * NS2S;
angle[0] += event.values[0] * dT;
angle[1] += event.values[1] * dT;
angle[2] += event.values[2] * dT;
}
timestamp = event.timestamp;
}
具体代码如下?package com.tware.gyrotest;import java.util.List;import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;public class GyroTestActivity extends Activity implements SensorEventListener { /** Called when the activity is first created. */
private static final float NS2S = 1.0f / 1000000000.0f;
private float timestamp;
private float[] angle = new float[3];
SensorManager mSensorManager;
private Sensor mGYROSCOPE;
TextView vX;
TextView vY;
TextView vZ;
public GyroTestActivity()
{
angle[0] = 0;
angle[1] = 0;
angle[2] = 0;
timestamp = 0;
} @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vX = (TextView)findViewById(R.id.v_x);
vY = (TextView)findViewById(R.id.v_y);
vZ = (TextView)findViewById(R.id.v_z);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mGYROSCOPE = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
}
public void onSensorChanged(SensorEvent event)
{
if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
{
return;
}
if (timestamp != 0) {
final float dT = (event.timestamp - timestamp) * NS2S;
angle[0] += event.values[0] * dT * 100;
angle[1] += event.values[1] * dT * 100;
angle[2] += event.values[2] * dT * 100;
}
timestamp = event.timestamp;
vX.setText("X: " + Float.toString(angle[0]));
vY.setText("Y: " + Float.toString(angle[1]));
vZ.setText("Z: " + Float.toString(angle[2]));
}
public void onResume() {
super.onResume();
mSensorManager.registerListener(this, mGYROSCOPE, SensorManager.SENSOR_DELAY_NORMAL);
}
public void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
} @Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
我在使用SDK中的gyroscope代码时,旋转机器,显示旋转的度数不正确。
在旋转机台的时候,还没用旋转到90°,代码显示的度数就已经到90°了。
希望高手能帮忙解决啊?
Sensor.TYPE_GYROSCOPE:private static final float NS2S = 1.0f / 1000000000.0f;
private float timestamp;
public void onSensorChanged(SensorEvent event){
if (timestamp != 0) {
final float dT = (event.timestamp - timestamp) * NS2S;
angle[0] += event.values[0] * dT;
angle[1] += event.values[1] * dT;
angle[2] += event.values[2] * dT;
}
timestamp = event.timestamp;
}
具体代码如下?package com.tware.gyrotest;import java.util.List;import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;public class GyroTestActivity extends Activity implements SensorEventListener { /** Called when the activity is first created. */
private static final float NS2S = 1.0f / 1000000000.0f;
private float timestamp;
private float[] angle = new float[3];
SensorManager mSensorManager;
private Sensor mGYROSCOPE;
TextView vX;
TextView vY;
TextView vZ;
public GyroTestActivity()
{
angle[0] = 0;
angle[1] = 0;
angle[2] = 0;
timestamp = 0;
} @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vX = (TextView)findViewById(R.id.v_x);
vY = (TextView)findViewById(R.id.v_y);
vZ = (TextView)findViewById(R.id.v_z);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mGYROSCOPE = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
}
public void onSensorChanged(SensorEvent event)
{
if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
{
return;
}
if (timestamp != 0) {
final float dT = (event.timestamp - timestamp) * NS2S;
angle[0] += event.values[0] * dT * 100;
angle[1] += event.values[1] * dT * 100;
angle[2] += event.values[2] * dT * 100;
}
timestamp = event.timestamp;
vX.setText("X: " + Float.toString(angle[0]));
vY.setText("Y: " + Float.toString(angle[1]));
vZ.setText("Z: " + Float.toString(angle[2]));
}
public void onResume() {
super.onResume();
mSensorManager.registerListener(this, mGYROSCOPE, SensorManager.SENSOR_DELAY_NORMAL);
}
public void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
} @Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货