13
13
import org .apache .commons .math3 .complex .Quaternion ;
14
14
15
15
import java .util .Arrays ;
16
+ import java .util .concurrent .atomic .AtomicBoolean ;
16
17
17
18
/*
18
19
* Copyright 2018, Kircher Electronics, LLC
@@ -60,38 +61,41 @@ public class OrientationFusedKalman extends OrientationFused {
60
61
61
62
private static final String TAG = OrientationFusedComplementary .class .getSimpleName ();
62
63
63
- private RotationKalmanFilter kalmanFilter ;
64
- private volatile boolean run ;
64
+ private final RotationKalmanFilter kalmanFilter ;
65
+ private final AtomicBoolean run ;
65
66
private volatile float dT ;
66
67
private volatile float [] output = new float [3 ];
67
68
private Thread thread ;
68
69
69
- private volatile Quaternion rotationOrientation ;
70
+ private volatile Quaternion rotationVectorAccelerationMagnetic ;
71
+ private final double [] vectorGyroscope = new double [4 ];
72
+ private final double [] vectorAccelerationMagnetic = new double [4 ];
70
73
71
74
public OrientationFusedKalman () {
72
75
this (DEFAULT_TIME_CONSTANT );
73
76
}
74
77
75
78
public OrientationFusedKalman (float timeConstant ) {
76
79
super (timeConstant );
80
+ run = new AtomicBoolean (false );
77
81
kalmanFilter = new RotationKalmanFilter (new RotationProcessModel (), new RotationMeasurementModel ());
78
82
}
79
83
80
84
public void startFusion () {
81
- if (!run && thread == null ) {
82
- run = true ;
85
+ if (!run . get () && thread == null ) {
86
+ run . set ( true ) ;
83
87
84
88
thread = new Thread (new Runnable () {
85
89
@ Override
86
90
public void run () {
87
- while (run && !Thread .interrupted ()) {
91
+ while (run . get () && !Thread .interrupted ()) {
88
92
89
- calculate ();
93
+ output = calculate ();
90
94
91
95
try {
92
96
Thread .sleep (20 );
93
97
} catch (InterruptedException e ) {
94
- Log .e (TAG , "Kalman Thread Run " , e );
98
+ Log .e (TAG , "Kalman Thread" , e );
95
99
Thread .currentThread ().interrupt ();
96
100
}
97
101
}
@@ -105,8 +109,8 @@ public void run() {
105
109
}
106
110
107
111
public void stopFusion () {
108
- if (run && thread != null ) {
109
- run = false ;
112
+ if (run . get () && thread != null ) {
113
+ run . set ( false ) ;
110
114
thread .interrupt ();
111
115
thread = null ;
112
116
}
@@ -133,21 +137,16 @@ public float[] getOutput() {
133
137
* @return An orientation vector -> @link SensorManager#getOrientation(float[], float[])}
134
138
*/
135
139
private float [] calculate () {
136
- if (rotationVectorGyroscope != null && rotationOrientation != null && dT != 0 ) {
140
+ if (rotationVector != null && rotationVectorAccelerationMagnetic != null && dT != 0 ) {
141
+ vectorGyroscope [0 ] = (float ) rotationVector .getVectorPart ()[0 ];
142
+ vectorGyroscope [1 ] = (float ) rotationVector .getVectorPart ()[1 ];
143
+ vectorGyroscope [2 ] = (float ) rotationVector .getVectorPart ()[2 ];
144
+ vectorGyroscope [3 ] = (float ) rotationVector .getScalarPart ();
137
145
138
- double [] vectorGyroscope = new double [4 ];
139
-
140
- vectorGyroscope [0 ] = (float ) rotationVectorGyroscope .getVectorPart ()[0 ];
141
- vectorGyroscope [1 ] = (float ) rotationVectorGyroscope .getVectorPart ()[1 ];
142
- vectorGyroscope [2 ] = (float ) rotationVectorGyroscope .getVectorPart ()[2 ];
143
- vectorGyroscope [3 ] = (float ) rotationVectorGyroscope .getScalarPart ();
144
-
145
- double [] vectorAccelerationMagnetic = new double [4 ];
146
-
147
- vectorAccelerationMagnetic [0 ] = (float ) rotationOrientation .getVectorPart ()[0 ];
148
- vectorAccelerationMagnetic [1 ] = (float ) rotationOrientation .getVectorPart ()[1 ];
149
- vectorAccelerationMagnetic [2 ] = (float ) rotationOrientation .getVectorPart ()[2 ];
150
- vectorAccelerationMagnetic [3 ] = (float ) rotationOrientation .getScalarPart ();
146
+ vectorAccelerationMagnetic [0 ] = (float ) rotationVectorAccelerationMagnetic .getVectorPart ()[0 ];
147
+ vectorAccelerationMagnetic [1 ] = (float ) rotationVectorAccelerationMagnetic .getVectorPart ()[1 ];
148
+ vectorAccelerationMagnetic [2 ] = (float ) rotationVectorAccelerationMagnetic .getVectorPart ()[2 ];
149
+ vectorAccelerationMagnetic [3 ] = (float ) rotationVectorAccelerationMagnetic .getScalarPart ();
151
150
152
151
// Apply the Kalman fusedOrientation... Note that the prediction and correction
153
152
// inputs could be swapped, but the fusedOrientation is much more stable in this
@@ -156,15 +155,12 @@ private float[] calculate() {
156
155
kalmanFilter .correct (vectorAccelerationMagnetic );
157
156
158
157
// rotation estimation.
159
- rotationVectorGyroscope = new Quaternion (kalmanFilter .getStateEstimation ()[3 ],
160
- Arrays .copyOfRange (kalmanFilter .getStateEstimation (), 0 , 3 ));
161
-
162
- output = AngleUtils .getAngles (rotationVectorGyroscope .getQ0 (), rotationVectorGyroscope .getQ1 (), rotationVectorGyroscope .getQ2 (), rotationVectorGyroscope .getQ3 ());
158
+ Quaternion result = new Quaternion (kalmanFilter .getStateEstimation ()[3 ], Arrays .copyOfRange (kalmanFilter .getStateEstimation (), 0 , 3 ));
163
159
164
- return output ;
160
+ output = AngleUtils . getAngles ( result . getQ0 (), result . getQ1 (), result . getQ2 (), result . getQ3 ()) ;
165
161
}
166
162
167
- return null ;
163
+ return output ;
168
164
}
169
165
170
166
/**
@@ -177,13 +173,12 @@ private float[] calculate() {
177
173
* @return the fused orientation estimation.
178
174
*/
179
175
public float [] calculateFusedOrientation (float [] gyroscope , long timestamp , float [] acceleration , float [] magnetic ) {
180
-
181
176
if (isBaseOrientationSet ()) {
182
177
if (this .timestamp != 0 ) {
183
178
dT = (timestamp - this .timestamp ) * NS2S ;
184
179
185
- rotationOrientation = RotationUtil .getOrientationVectorFromAccelerationMagnetic (acceleration , magnetic );
186
- rotationVectorGyroscope = RotationUtil .integrateGyroscopeRotation (rotationVectorGyroscope , gyroscope , dT , EPSILON );
180
+ rotationVectorAccelerationMagnetic = RotationUtil .getOrientationVectorFromAccelerationMagnetic (acceleration , magnetic );
181
+ rotationVector = RotationUtil .integrateGyroscopeRotation (rotationVector , gyroscope , dT , EPSILON );
187
182
}
188
183
this .timestamp = timestamp ;
189
184
0 commit comments