-
Notifications
You must be signed in to change notification settings - Fork 0
/
bno055driver.java
58 lines (43 loc) · 2 KB
/
bno055driver.java
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
package org.firstinspires.ftc.teamcode;
// This code comes from https://github.com/FROGbots-4634/Velocity_Vortex_SDK_v2.62/blob/master/TeamCode/src/main/java/org/firstinspires/ftc/teamcode/bno055driver.java
import com.qualcomm.hardware.bosch.BNO055IMU;
import com.qualcomm.robotcore.hardware.HardwareMap;
import org.firstinspires.ftc.robotcore.external.navigation.Quaternion;
import java.util.Locale;
public class bno055driver {
private final BNO055IMU imu;
private final String name;
public bno055driver(String name, HardwareMap hwmap) {
this.name = name;
imu = hwmap.get(BNO055IMU.class, name);
setParameters();
}
private void setParameters() {
BNO055IMU.Parameters parameters = new BNO055IMU.Parameters();
parameters.mode = BNO055IMU.SensorMode.IMU;
parameters.useExternalCrystal = true;
parameters.angleUnit = BNO055IMU.AngleUnit.RADIANS;
parameters.pitchMode = BNO055IMU.PitchMode.WINDOWS;
parameters.loggingEnabled = false;
parameters.loggingTag = "IMU";
imu.initialize(parameters);
}
public double[] getAngles() {
Quaternion quatAngles = imu.getQuaternionOrientation();
double w = quatAngles.w;
double x = quatAngles.x;
double y = quatAngles.y;
double z = quatAngles.z;
// for the Adafruit IMU, yaw and roll are switched
double roll = Math.atan2( 2*(w*x + y*z) , 1 - 2*(x*x + y*y) ) * 180.0 / Math.PI;
double pitch = Math.asin( 2*(w*y - x*z) ) * 180.0 / Math.PI;
double yaw = Math.atan2( 2*(w*z + x*y), 1 - 2*(y*y + z*z) ) * 180.0 / Math.PI;
return new double[]{yaw, pitch, roll};
}
public String getName() {return name;}
// This method returns a string that can be used to output telemetry data easily in other classes.
public String telemetrize() {
double[] angles = getAngles();
return String.format(Locale.US, "Yaw: %.3f Pitch: %.3f Roll: %.3f", angles[0], angles[1], angles[2]);
}
}