1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
package com.android2ee.android.tuto.sensor.gyroscope;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;
public class SensorGyroscopeTutoActivity extends Activity implements SensorEventListener, RadioGroup.OnCheckedChangeListener
{
private SensorManager mSensorManager = null;
//_________________________________Gyroscope
private float[] gyro = new float[3];// vitesses angulaires de gyroscope
private float[] gyroMatrix = new float[9];// matrice de rotation à partir de données gyro
private float[] gyroOrientation = new float[3];// angles d'orientation de matrice gyroscopique
//_________________________________Magnetic
private float[] magnet = new float[3];// vecteur champ magnétique
//_________________________________Accelerometer
private float[] accel = new float[3];// vecteur de l'accéléromètre
//_________________________________Accelerometer + Magnetic
private float[] accMagOrientation = new float[3];// angles d'orientation à partir Accelerometer + Magnetic
private float[] rotationMatrix = new float[9];// accéléromètre et magnétomètre basé matrice de rotation
//_________________________________Fusion
private float[] fusedOrientation = new float[3];// orientation finale Angles de fusion de capteurs
//_____________________________________________________________________________________________________
public static final float EPSILON = 0.000000001f;
private static final float NS2S = 1.0f / 1000000000.0f;
public static final int TIME_CONSTANT = 30;
public static final float FILTER_COEFFICIENT = 0.98f;
private float timestamp;
private boolean initState = true;
private Timer fuseTimer = new Timer();
//_______________________________l'affichage de la sortie du capteur
public Handler mHandler;
private RadioGroup mRadioGroup;
private TextView mAzimuthView,mPitchView,mRollView,tv;
private int radioSelection;
DecimalFormat d = new DecimalFormat("#.##");
float ang;
int i=0;
//__________________________________oncreate
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); setContentView(R.layout.main);
gyroOrientation[0] = 0.0f; gyroOrientation[1] = 0.0f; gyroOrientation[2] = 0.0f;
//__Initialiser gyroMatrix avec matrice d'identité
gyroMatrix[0] = 1.0f; gyroMatrix[1] = 0.0f; gyroMatrix[2] = 0.0f;
gyroMatrix[3] = 0.0f; gyroMatrix[4] = 1.0f; gyroMatrix[5] = 0.0f;
gyroMatrix[6] = 0.0f; gyroMatrix[7] = 0.0f; gyroMatrix[8] = 1.0f;
//__obtenir sensorManager et initialiser auditeurs de capteurs
mSensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
initListeners();
/* - attendez une seconde jusqu'à ce gyroscope et magnétomètre / accéléromètre
- données est initialisée alors scedule la tâche de filtre complémentaire */
fuseTimer.scheduleAtFixedRate(new calculateFusedOrientationTask(),1000, TIME_CONSTANT);
// GUI stuff
mHandler = new Handler();
radioSelection = 0;
d.setRoundingMode(RoundingMode.HALF_UP); d.setMaximumFractionDigits(3); d.setMinimumFractionDigits(3);
mRadioGroup = (RadioGroup)findViewById(R.id.radioGroup1);
mAzimuthView = (TextView)findViewById(R.id.textView4); mPitchView = (TextView)findViewById(R.id.textView5); mRollView = (TextView)findViewById(R.id.textView6);
tv = (TextView)findViewById(R.id.tv);
mRadioGroup.setOnCheckedChangeListener(this);
}
//__________________________________onStop
@Override
public void onStop()
{
super.onStop();
//__Auditeurs de capteurs de désinscription pour empêcher l'activité de épuiser la batterie de l'appareil.
mSensorManager.unregisterListener(this);
}
//__________________________________onPause
@Override
protected void onPause()
{
super.onPause();
//__Auditeurs de capteurs de désinscription pour empêcher l'activité de épuiser la batterie de l'appareil.
mSensorManager.unregisterListener(this);
}
//__________________________________onResume
@Override
public void onResume()
{
super.onResume();
//__Restaurer les auditeurs du capteur lorsque l'utilisateur reprend la demande .
initListeners();
}
//__Cette fonction enregistre auditeurs de capteurs pour l'accéléromètre , magnétomètre et du gyroscope .
public void initListeners()
{
mSensorManager.registerListener(this,mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this,mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE),SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this,mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event)
{
switch(event.sensor.getType())
{
case Sensor.TYPE_ACCELEROMETER:
//__Copie de nouvelles données de l'accéléromètre en accélération réseau et de calculer l'orientation
System.arraycopy(event.values, 0, accel, 0, 3);
//__Calcule les angles d'orientation de la sortie de l'accéléromètre et magnétomètre
if(SensorManager.getRotationMatrix(rotationMatrix, null, accel, magnet))
{
SensorManager.getOrientation(rotationMatrix, accMagOrientation);
}
break;
case Sensor.TYPE_GYROSCOPE:
//__données processus de gyro
gyroFunction(event);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
//__Copier les nouvelles données du magnétomètre dans un tableau magnétique
System.arraycopy(event.values, 0, magnet, 0, 3);
break;
}
}
// Cette fonction effectue l'intégration des données de gyroscope .
// Il écrit l'orientation à base de gyroscope dans gyroOrientation .
public void gyroFunction(SensorEvent event)
{
// Ne pas commencer jusqu'à ce que l'orientation premier accéléromètre / magnétomètre a été acquise
if (accMagOrientation == null)
return;
// Initialisation de la matrice de rotation du gyroscope à base
if(initState)
{
float[] initMatrix = new float[9];
initMatrix = getRotationMatrixFromOrientation(accMagOrientation);
float[] test = new float[3];
SensorManager.getOrientation(initMatrix, test);
gyroMatrix = matrixMultiplication(gyroMatrix, initMatrix);
initState = false;
}
// Copier les nouvelles valeurs gyroscopiques dans le tableau du gyroscope
// Convertir les données gyroscopiques premières dans un vecteur de rotation
float[] deltaVector = new float[4];
if(timestamp != 0)
{
final float dT = (event.timestamp - timestamp) * NS2S;
System.arraycopy(event.values, 0, gyro, 0, 3);
float[] normValues = new float[3];
// Calcule la vitesse angulaire de l'échantillon
float omegaMagnitude = (float)Math.sqrt(gyro[0] * gyro[0] + gyro[1] * gyro[1] + gyro[2] * gyro[2]);
// Normaliser le vecteur de rotation si elle est assez grande pour obtenir l'axe
if(omegaMagnitude > EPSILON)
{
normValues[0] = gyro[0] / omegaMagnitude;
normValues[1] = gyro[1] / omegaMagnitude;
normValues[2] = gyro[2] / omegaMagnitude;
}
float thetaOverTwo = omegaMagnitude * (dT / 2.0f);
float sinThetaOverTwo = (float)Math.sin(thetaOverTwo);
float cosThetaOverTwo = (float)Math.cos(thetaOverTwo);
deltaVector[0] = sinThetaOverTwo * normValues[0];
deltaVector[1] = sinThetaOverTwo * normValues[1];
deltaVector[2] = sinThetaOverTwo * normValues[2];
deltaVector[3] = cosThetaOverTwo;
}
// Mesure fait , gagner du temps actuel intervalle suivant
timestamp = event.timestamp;
// Convertir vecteur de rotation en matrice de rotation
float[] deltaMatrix = new float[9];
SensorManager.getRotationMatrixFromVector(deltaMatrix, deltaVector);
// Appliquer le nouvel intervalle de rotation de la matrice de rotation du gyroscope à base
gyroMatrix = matrixMultiplication(gyroMatrix, deltaMatrix);
// Obtenir l'orientation à base de gyroscope de la matrice de rotation
SensorManager.getOrientation(gyroMatrix, gyroOrientation);
}
private float[] getRotationMatrixFromOrientation(float[] o)
{
float[] xM = new float[9];
float[] yM = new float[9];
float[] zM = new float[9];
float sinX = (float)Math.sin(o[1]);
float cosX = (float)Math.cos(o[1]);
float sinY = (float)Math.sin(o[2]);
float cosY = (float)Math.cos(o[2]);
float sinZ = (float)Math.sin(o[0]);
float cosZ = (float)Math.cos(o[0]);
// rotation about x-axis (pitch)
xM[0] = 1.0f; xM[1] = 0.0f; xM[2] = 0.0f;
xM[3] = 0.0f; xM[4] = cosX; xM[5] = sinX;
xM[6] = 0.0f; xM[7] = -sinX; xM[8] = cosX;
// rotation about y-axis (roll)
yM[0] = cosY; yM[1] = 0.0f; yM[2] = sinY;
yM[3] = 0.0f; yM[4] = 1.0f; yM[5] = 0.0f;
yM[6] = -sinY; yM[7] = 0.0f; yM[8] = cosY;
// rotation about z-axis (azimuth)
zM[0] = cosZ; zM[1] = sinZ; zM[2] = 0.0f;
zM[3] = -sinZ; zM[4] = cosZ; zM[5] = 0.0f;
zM[6] = 0.0f; zM[7] = 0.0f; zM[8] = 1.0f;
// rotation order is y, x, z (roll, pitch, azimuth)
float[] resultMatrix = matrixMultiplication(xM, yM);
resultMatrix = matrixMultiplication(zM, resultMatrix);
return resultMatrix;
}
private float[] matrixMultiplication(float[] A, float[] B)
{
float[] result = new float[9];
result[0] = A[0] * B[0] + A[1] * B[3] + A[2] * B[6];
result[1] = A[0] * B[1] + A[1] * B[4] + A[2] * B[7];
result[2] = A[0] * B[2] + A[1] * B[5] + A[2] * B[8];
result[3] = A[3] * B[0] + A[4] * B[3] + A[5] * B[6];
result[4] = A[3] * B[1] + A[4] * B[4] + A[5] * B[7];
result[5] = A[3] * B[2] + A[4] * B[5] + A[5] * B[8];
result[6] = A[6] * B[0] + A[7] * B[3] + A[8] * B[6];
result[7] = A[6] * B[1] + A[7] * B[4] + A[8] * B[7];
result[8] = A[6] * B[2] + A[7] * B[5] + A[8] * B[8];
return result;
}
class calculateFusedOrientationTask extends TimerTask {
public void run() {
float oneMinusCoeff = 1.0f - FILTER_COEFFICIENT;
/*
/ *
* Correction de 179 ° < - > de problème -179 ° de transition :
* Vérifiez si l'un des deux angles d'orientation ( gyroscopiques ou accMag ) est négative tandis que l'autre est positive .
* Si oui , ajouter 360 ° ( 2 * Math.PI ) à la valeur négative , effectuer la fusion de capteurs , et de supprimer les 360 ° à partir du résultat
* Si elle est supérieure à 180 °. Cela stabilise la sortie dans les cas positifs à négative transition.
*/
// azimuth
if (gyroOrientation[0] < -0.5 * Math.PI && accMagOrientation[0] > 0.0)
{
float number = Float.valueOf(tv.getText().toString());
fusedOrientation[0] = (float) (FILTER_COEFFICIENT * (gyroOrientation[0] + 2.0 * Math.PI) + oneMinusCoeff * accMagOrientation[0]);
fusedOrientation[0] -= (fusedOrientation[0] > Math.PI) ? 2.0 * Math.PI : 0;
}
else if (accMagOrientation[0] < -0.5 * Math.PI && gyroOrientation[0] > 0.0)
{
fusedOrientation[0] = (float) (FILTER_COEFFICIENT * gyroOrientation[0] + oneMinusCoeff * (accMagOrientation[0] + 2.0 * Math.PI));
fusedOrientation[0] -= (fusedOrientation[0] > Math.PI)? 2.0 * Math.PI : 0;
}
else {
fusedOrientation[0] = FILTER_COEFFICIENT * gyroOrientation[0] + oneMinusCoeff * accMagOrientation[0];
}
// pitch
if (gyroOrientation[1] < -0.5 * Math.PI && accMagOrientation[1] > 0.0) {
fusedOrientation[1] = (float) (FILTER_COEFFICIENT * (gyroOrientation[1] + 2.0 * Math.PI) + oneMinusCoeff * accMagOrientation[1]);
fusedOrientation[1] -= (fusedOrientation[1] > Math.PI) ? 2.0 * Math.PI : 0;
}
else if (accMagOrientation[1] < -0.5 * Math.PI && gyroOrientation[1] > 0.0) {
fusedOrientation[1] = (float) (FILTER_COEFFICIENT * gyroOrientation[1] + oneMinusCoeff * (accMagOrientation[1] + 2.0 * Math.PI));
fusedOrientation[1] -= (fusedOrientation[1] > Math.PI)? 2.0 * Math.PI : 0;
}
else {
fusedOrientation[1] = FILTER_COEFFICIENT * gyroOrientation[1] + oneMinusCoeff * accMagOrientation[1];
}
// roll
if (gyroOrientation[2] < -0.5 * Math.PI && accMagOrientation[2] > 0.0) {
fusedOrientation[2] = (float) (FILTER_COEFFICIENT * (gyroOrientation[2] + 2.0 * Math.PI) + oneMinusCoeff * accMagOrientation[2]);
fusedOrientation[2] -= (fusedOrientation[2] > Math.PI) ? 2.0 * Math.PI : 0;
}
else if (accMagOrientation[2] < -0.5 * Math.PI && gyroOrientation[2] > 0.0) {
fusedOrientation[2] = (float) (FILTER_COEFFICIENT * gyroOrientation[2] + oneMinusCoeff * (accMagOrientation[2] + 2.0 * Math.PI));
fusedOrientation[2] -= (fusedOrientation[2] > Math.PI)? 2.0 * Math.PI : 0;
}
else {
fusedOrientation[2] = FILTER_COEFFICIENT * gyroOrientation[2] + oneMinusCoeff * accMagOrientation[2];
}
// Remplacer matrice gyroscopique et de l'orientation à l'orientation fondu
// Pour comensate dérive gyroscopique
gyroMatrix = getRotationMatrixFromOrientation(fusedOrientation);
System.arraycopy(fusedOrientation, 0, gyroOrientation, 0, 3);
// update sensor output in GUI
mHandler.post(updateOreintationDisplayTask);
}
}
// **************************** GUI FUNCTIONS *********************************
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId) {
case R.id.radio0:
radioSelection = 0;
ang=accMagOrientation[0];
break;
case R.id.radio1:
radioSelection = 1;
ang=gyroOrientation[0];
break;
case R.id.radio2:
radioSelection = 2;
ang=fusedOrientation[0];
break;
}
}
public void updateOreintationDisplay() {
switch(radioSelection) {
case 0:
mAzimuthView.setText(d.format(accMagOrientation[0] * 180/Math.PI) + '°');
tv.setText(d.format((accMagOrientation[0] -ang) * 180/Math.PI) + '°');
mPitchView.setText(d.format(accMagOrientation[1] * 180/Math.PI) + '°');
mRollView.setText(d.format(accMagOrientation[2] * 180/Math.PI) + '°');
break;
case 1:
tv.setText(d.format((gyroOrientation[0] -ang) * 180/Math.PI) + '°');
mAzimuthView.setText(d.format(gyroOrientation[0] * 180/Math.PI) + '°');
mPitchView.setText(d.format(gyroOrientation[1] * 180/Math.PI) + '°');
mRollView.setText(d.format(gyroOrientation[2] * 180/Math.PI) + '°');
break;
case 2:
tv.setText(d.format((fusedOrientation[0] -ang) * 180/Math.PI) + '°');
mAzimuthView.setText(d.format(fusedOrientation[0] * 180/Math.PI) + '°');
mPitchView.setText(d.format(fusedOrientation[1] * 180/Math.PI) + '°');
mRollView.setText(d.format(fusedOrientation[2] * 180/Math.PI) + '°');
break;
}
}
private Runnable updateOreintationDisplayTask = new Runnable() {
public void run() {
updateOreintationDisplay();
}
};
} |
Partager