1
1
package com .kircherelectronics .fsensor .filter .gyroscope ;
2
2
3
- import android .hardware . SensorManager ;
3
+ import android .util . Log ;
4
4
5
5
import com .kircherelectronics .fsensor .BaseFilter ;
6
6
import com .kircherelectronics .fsensor .util .rotation .RotationUtil ;
7
7
8
8
import org .apache .commons .math3 .complex .Quaternion ;
9
+ import org .apache .commons .math3 .geometry .euclidean .threed .Rotation ;
10
+ import org .apache .commons .math3 .geometry .euclidean .threed .RotationConvention ;
11
+ import org .apache .commons .math3 .geometry .euclidean .threed .RotationOrder ;
12
+
13
+ import java .util .Arrays ;
9
14
10
15
/*
11
- * Copyright 2017 , Kircher Electronics, LLC
16
+ * Copyright 2018 , Kircher Electronics, LLC
12
17
*
13
18
* Licensed under the Apache License, Version 2.0 (the "License");
14
19
* you may not use this file except in compliance with the License.
79
84
*/
80
85
public class OrientationGyroscope extends BaseFilter {
81
86
82
- private static final String tag = OrientationGyroscope .class .getSimpleName ();
87
+ private static final String TAG = OrientationGyroscope .class .getSimpleName ();
83
88
private static final float NS2S = 1.0f / 1000000000.0f ;
84
89
private static final float EPSILON = 0.000000001f ;
85
90
private Quaternion rotationVectorGyroscope ;
@@ -105,34 +110,23 @@ public float[] getOutput() {
105
110
* @return An orientation vector -> @link SensorManager#getOrientation(float[], float[])}
106
111
*/
107
112
public float [] calculateOrientation (float [] gyroscope , long timestamp ) {
108
-
109
- if (rotationVectorGyroscope != null ) {
113
+ if (isBaseOrientationSet ()) {
110
114
111
115
if (this .timestamp != 0 ) {
112
116
final float dT = (timestamp - this .timestamp ) * NS2S ;
113
117
rotationVectorGyroscope = RotationUtil .integrateGyroscopeRotation (rotationVectorGyroscope , gyroscope , dT , EPSILON );
114
- }
115
-
116
- this .timestamp = timestamp ;
117
-
118
- // Now we get a structure we can pass to get a rotation matrix, and then
119
- // an orientation vector from Android.
120
-
121
- float [] fusedVector = new float [4 ];
122
-
123
- fusedVector [0 ] = (float ) rotationVectorGyroscope .getVectorPart ()[0 ];
124
- fusedVector [1 ] = (float ) rotationVectorGyroscope .getVectorPart ()[1 ];
125
- fusedVector [2 ] = (float ) rotationVectorGyroscope .getVectorPart ()[2 ];
126
- fusedVector [3 ] = (float ) rotationVectorGyroscope .getScalarPart ();
127
118
128
- // rotation matrix from gyro data
129
- float [] fusedMatrix = new float [ 9 ] ;
119
+ Rotation rotation = new Rotation ( rotationVectorGyroscope . getQ0 (), rotationVectorGyroscope . getQ1 (), rotationVectorGyroscope . getQ2 (),
120
+ rotationVectorGyroscope . getQ3 (), true ) ;
130
121
131
- // We need a rotation matrix so we can get the orientation vector
132
- SensorManager .getRotationMatrixFromVector (fusedMatrix , fusedVector );
122
+ try {
123
+ output = doubleToFloat (rotation .getAngles (RotationOrder .XYZ , RotationConvention .VECTOR_OPERATOR ));
124
+ } catch (Exception e ) {
125
+ Log .d (TAG , "" , e );
126
+ }
127
+ }
133
128
134
- // Get the OrientationFused
135
- SensorManager .getOrientation (fusedMatrix , output );
129
+ this .timestamp = timestamp ;
136
130
137
131
return output ;
138
132
} else {
@@ -160,6 +154,16 @@ public void reset() {
160
154
}
161
155
162
156
public boolean isBaseOrientationSet () {
163
- return !(rotationVectorGyroscope == null );
157
+ return rotationVectorGyroscope != null ;
158
+ }
159
+
160
+ private static float [] doubleToFloat (double [] values ) {
161
+ float [] f = new float [values .length ];
162
+
163
+ for (int i = 0 ; i < f .length ; i ++){
164
+ f [i ] = (float ) values [i ];
165
+ }
166
+
167
+ return f ;
164
168
}
165
169
}
0 commit comments