RobotCore
1.0
WPILib port to raspberry Pi
Toggle main menu visibility
Loading...
Searching...
No Matches
Gyro.java
1
/*
2
* Copyright (C) 2016 John H. Gaby
3
*
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, version 3 of the License.
7
*
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
12
*
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15
*
16
* Contact: robotics@gabysoft.com
17
*/
18
19
package
robotCore;
20
21
import
java.nio.ByteBuffer;
22
23
import
org.wpilib.math.geometry.Rotation2d;
24
// import frc.robot.subsystems.DriveSubsystem;
25
import
robotCore.Device.Processor;
26
import
robotCore.Device.RequestType;
27
35
public
class
Gyro {
36
private
static
final
int
k_maxRetry = 5;
37
38
private
double
m_yawZero = 0;
39
private
double
m_yaw = 0;
40
private
boolean
m_invert =
false
;
41
private
Device
m_device;
42
private
Object m_lock =
new
Object();
43
44
// Support for DummyGyro
45
public
Gyro(
int
d) {
46
47
}
48
49
// This function will be called from another thread
50
void
groupCallback(ByteBuffer data, Object
object
) {
51
// Logger.log("Gyro", 1, "groupCallback");
52
53
int
angle = data.getShort();
54
55
synchronized
(m_lock) {
56
m_yaw = angle / 100.0;
57
}
58
}
59
63
public
Gyro() {
64
Logger
.
log
(
"Gyro"
, 1,
"Wait for initialization..."
);
65
66
m_device =
Device
.getInstance();
67
68
for
(
int
i = 0; i < k_maxRetry; i++) {
69
int
state;
70
71
m_device.sendCommand(
Device
.k_i2cDefAddr,
TwoWire
.k_initGyro);
72
73
/*
74
* Wait for gyro to initialize
75
*/
76
do
{
77
RobotCore.sleep(1000);
78
79
state = m_device.getByte(
Device
.k_i2cDefAddr,
TwoWire
.k_getGyroState);
80
81
Logger
.
log
(
"Gyro"
, 1, String.format(
"Waiting for gyro: state = %d"
, state));
82
83
if
(state >= 1) {
84
Processor processor =
Device
.getInstance().getProcessor(
Device
.k_i2cDefAddr);
85
86
processor.addGroupRequest(RequestType.Gyro, 0, (d, o) -> groupCallback(d, o),
this
);
87
Device
.startGroupRequests();
88
return
;
// success
89
}
90
}
while
(state == 0);
91
92
Logger
.
log
(
"Gyro"
, 1,
"Init failed, retrying..."
);
93
}
94
95
throw
new
TwoWire
.
HardwareException
(
"Gyro: max retries exceeded"
);
96
}
97
104
public
static
double
normalize
(
double
angle,
double
range) {
105
angle = angle % range;
106
107
if
(angle > (range / 2)) {
108
angle -= range;
109
}
else
if
(angle < -(range / 2)) {
110
angle += range;
111
}
112
113
return
(angle);
114
}
115
122
public
static
double
normalizeAngle
(
double
angle) {
123
return
normalize
(angle, 360);
124
}
125
126
private
double
getRawYaw() {
127
synchronized
(m_lock) {
128
return
m_invert ? -m_yaw : m_yaw;
129
}
130
}
131
137
public
double
getYaw
() {
138
return
normalizeAngle
(getRawYaw() - m_yawZero);
139
}
140
147
public
Rotation2d
getRotation2d
() {
148
return
Rotation2d
.
fromDegrees
(
getYaw
());
149
}
150
157
public
void
reset
(
double
yaw) {
158
m_yawZero = getRawYaw();
159
// Logger.log("Gyro", 1, String.format("reset=%f", m_yawZero));
160
}
161
171
public
void
invert
(
boolean
invert
) {
172
m_invert =
invert
;
173
}
174
}
org.wpilib.math.geometry.Rotation2d
Definition
Rotation2d.java:30
org.wpilib.math.geometry.Rotation2d.fromDegrees
static Rotation2d fromDegrees(double degrees)
Definition
Rotation2d.java:175
robotCore.Device
Device.
Definition
Device.java:40
robotCore.Gyro.reset
void reset(double yaw)
Definition
Gyro.java:157
robotCore.Gyro.invert
void invert(boolean invert)
Definition
Gyro.java:171
robotCore.Gyro.getRotation2d
Rotation2d getRotation2d()
Definition
Gyro.java:147
robotCore.Gyro.getYaw
double getYaw()
Definition
Gyro.java:137
robotCore.Gyro.normalize
static double normalize(double angle, double range)
Definition
Gyro.java:104
robotCore.Gyro.normalizeAngle
static double normalizeAngle(double angle)
Definition
Gyro.java:122
robotCore.Logger
The Logger class provides a mechanism to log messages.
Definition
Logger.java:54
robotCore.Logger.log
static void log(String tag, int level, String message, boolean noLogToConsole)
Definition
Logger.java:161
robotCore.TwoWire.HardwareException
Definition
TwoWire.java:125
robotCore.TwoWire
Definition
TwoWire.java:25
robotCore
Gyro.java
Generated by
1.18.0