-
Notifications
You must be signed in to change notification settings - Fork 0
/
PIDController.java
202 lines (177 loc) · 4.37 KB
/
PIDController.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package frc.System;
public class NavX
{
public static void Navx(){
private static AHRS navx = new AHRS(SPI.Port.kMXP);
}
public static void ReNavx(){
navx.reset();
}
/**
* Gets the current pitch angle of the navX.
*
* @return the current pitch angle of the navX.
*/
public static double getPitch() {
return navx.getRoll();
}
/**
* Gets the current rate of change in pitch angle of the navX.
*
* @return the current rate of change in pitch angle of the navX.
*/
public static double getRatePitch() {
return navx.getRawGyroY();
}
/**
* Gets the current roll angle of the navX.
*
* @return the current roll angle of the navX.
*/
public static double getRoll() {
return navx.getPitch();
}
/**
* Gets the current rate of change in roll angle of the navX.
*
* @return the current rate of change in roll angle of the navX.
*/
public static double getRateRoll() {
return navx.getRawGyroX();
}
/**
* Gets the current yaw angle of the navX.
*
* @return the current yaw angle of the navX.
*/
public static double getYaw() {
return navx.getYaw();
}
/**
* Gets the current rate of change in yaw angle of the navX.
*
* @return the current rate of change in yaw angle of the navX.
*/
public static double getRateYaw() {
return navx.getRawGyroZ();
}
/**
* Gets the current forward acceleration of the navX.
*
* @return the current forward acceleration of the navX.
*/
public static double getAccelForward() {
return navx.getWorldLinearAccelX();
}
/**
* Gets the current acceleration across the navX to the right.
*
* @return the current acceleration across the navX to the right.
*/
public static double getAccelRight() {
return -navx.getWorldLinearAccelY();
}
/**
* Gets the current upwards acceleration of the navX.
*
* @return the current upwards acceleration of the navX.
*/
public static double getAccelUp() {
return navx.getWorldLinearAccelZ();
}
/**
* Gets the current forward speed of the navX.
*
* @return the current forward speed of the navX.
*/
public static double getSpeedForward() {
return navx.getVelocityX();
}
/**
* Gets the current speed across the navX to the right.
*
* @return the current speed across the navX to the right.
*/
public static double getSpeedRight() {
return -navx.getVelocityY();
}
/**
* Gets the current upwards speed of the navX.
*
* @return the current upwards speed of the navX.
*/
public static double getSpeedUp() {
return navx.getVelocityZ();
}
/**
* A sensor representation of a navX reading, so it can be used as a {@link NumericSensor}.
* To create a sensor for the yaw angle, use {@link NavX.Yaw}.
*
* @author Sean Zammit
*/
public static class Sensor implements GettableNumber
{
/** The type of reading you want to create a sensor for. */
public NavXReading type;
/**
* Constructor for a sensor representation of a navX reading, so it can be used as a {@link NumericSensor}.
*
* @param type The type of reading you want to create a sensor for. See {@link NavXReading}.
*/
public Sensor(NavXReading type) {
this.type = type;
}
public double get() {
switch(type) {
case ACCEL_FORWARD:
return getAccelForward();
case ACCEL_RIGHT:
return getAccelRight();
case ACCEL_UP:
return getAccelUp();
case ANGLE_PITCH:
return getPitch();
case ANGLE_ROLL:
return getRoll();
case ANGLE_YAW:
return getYaw();
case RATE_PITCH:
return getRatePitch();
case RATE_ROLL:
return getRateRoll();
case RATE_YAW:
return getRateYaw();
case SPEED_FORWARD:
return getSpeedForward();
case SPEED_RIGHT:
return getSpeedRight();
case SPEED_UP:
return getSpeedUp();
default:
return getYaw();
}
}
}
/**
* A sensor representation of a navX yaw reading, so it can be used as a {@link NumericSensor}.
* To create a sensor for any other angle, use {@link NavX.Sensor}.
*
* @author Sean Zammit
*/
public static class Yaw extends NumericSensor
{
double curSet = 0;
protected double getSenVal() {
float yaw = (float) (getYaw() + curSet);
if(yaw < -180) yaw += 360;
else if(yaw > 180) yaw -= 360;
return yaw;
}
public void set(double value) {
curSet = value % 360;
if(curSet < -180) curSet -= 360;
else if(curSet > 180) curSet -= 360;
navx.reset();
}
}
}