-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from KalebKE/v1.2.2
v1.2.2
- Loading branch information
Showing
4 changed files
with
106 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
fsensor/src/main/java/com/kircherelectronics/fsensor/util/angle/AngleUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.kircherelectronics.fsensor.util.angle; | ||
|
||
import android.util.Log; | ||
|
||
public class AngleUtils { | ||
private static final String TAG = AngleUtils.class.getSimpleName(); | ||
|
||
/** | ||
* https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm | ||
* | ||
* +X to the right | ||
* +Y straight up | ||
* +Z axis toward viewer | ||
* | ||
* Heading = rotation about y axis | ||
* Attitude = rotation about z axis | ||
* Bank = rotation about x axis | ||
* | ||
* Heading applied first | ||
* Attitude applied second | ||
* Bank applied last | ||
* | ||
* @param w | ||
* @param z | ||
* @param x | ||
* @param y | ||
* @return | ||
*/ | ||
public static float[] getAngles(double w, double z, double x, double y) { | ||
double heading; | ||
double pitch; | ||
double roll; | ||
|
||
double test = x*y + z*w; | ||
if (test > 0.499) { // singularity at north pole | ||
heading = 2 * Math.atan2(x,w); | ||
pitch = -Math.PI/2; | ||
roll = 0; | ||
Log.e(TAG, "singularity at north pole"); | ||
return new float[]{(float)heading, (float)pitch, (float)roll}; | ||
} | ||
if (test < -0.499) { // singularity at south pole | ||
heading = -2 * Math.atan2(x,w); | ||
pitch = Math.PI/2; | ||
roll = 0; | ||
Log.e(TAG, "singularity at south pole"); | ||
return new float[]{(float)heading, (float)pitch, (float)roll}; | ||
} | ||
double sqx = x*x; | ||
double sqy = y*y; | ||
double sqz = z*z; | ||
heading = Math.atan2(2*y*w-2*x*z , 1 - 2*sqy - 2*sqz); | ||
pitch = -Math.asin(2*test); | ||
roll = -Math.atan2(2*x*w-2*y*z , 1 - 2*sqx - 2*sqz); | ||
|
||
return new float[]{(float)pitch,(float)roll, (float)heading}; | ||
} | ||
} |