-
Notifications
You must be signed in to change notification settings - Fork 78
/
MotorEx.java
224 lines (188 loc) · 6.23 KB
/
MotorEx.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package com.arcrobotics.ftclib.hardware.motors;
import com.arcrobotics.ftclib.controller.PController;
import com.arcrobotics.ftclib.controller.PIDController;
import com.arcrobotics.ftclib.controller.PIDFController;
import com.arcrobotics.ftclib.controller.wpilibcontroller.SimpleMotorFeedforward;
/**
* An extended motor class that utilizes more features than the
* regular motor. This is basically a rewrite of DcMotor in the basic
* SDK, but has more features and a more powerful PIDF control.
*
* In order to create an extended motor, you will need to have already
* created either a custom motor class or use {@link SimpleMotor}.
*
* Please note that this is the <b>abstract class file</b>, so
* the purpose of this class is for creating your own custom motor class.
* If you want to use the simple one we have created for you, please use
* {@link SimpleMotorEx}.
*
* @author Jackson
*/
public abstract class MotorEx implements Motor {
/**
* The PID controller for the extended motor.
*/
protected PIDController velocityController;
/**
* The feed forward output for the motor.
*/
protected SimpleMotorFeedforward motorFeedforward;
/**
* The P controller for position.
*/
protected PController positionController;
/**
* The motor in question.
*/
protected Motor motor;
/**
* The counts per revolution of the output shaft, usually listed in the specs for
* the motor.
*/
public final double COUNTS_PER_REV;
public ZeroPowerBehavior zeroBehavior = ZeroPowerBehavior.UNKNOWN;
/**
* The behavior of the motor when a speed of 0 is passed.
*
* <p>
* BREAK causes the motor to run into itself and immediately sets the speed to 0.
* </p>
* <p>
* FLOAT causes the motor to continue to spin with slowly decreasing speed when 0 is passed.
* </p>
* <p>
* UNKNOWN is the default behavior, which causes the motor to decelerate quickly.
* </p>
*/
public enum ZeroPowerBehavior {
BREAK, FLOAT, UNKNOWN
}
/**
* The constructor for the motor without a PID controller.
*
* @param mot The motor in question.
* @param cpr The counts per revolution of said motor.
*/
public MotorEx(Motor mot, double cpr) {
this(mot, cpr, new PIDController(new double[]{1,0,0}));
}
/**
* The constructor for the extended motor that includes a {@link PIDFController}.
*
* @param mot The motor in question.
* @param cpr The counts per revolution of said motor.
* @param controller The PID controller.
*/
public MotorEx(Motor mot, double cpr, PIDController controller) {
this(mot, cpr, controller, new PController(1));
}
/**
* The constructor for the extended motor which includes a
* {@link PIDController} and a {@link PController}
*/
public MotorEx(Motor mot, double cpr,
PIDController veloController,
PController positionController) {
this(mot, cpr, veloController, positionController,
new SimpleMotorFeedforward(0,0));
}
/**
* The constructor for the extended motor which includes a
* {@link PIDController} and a {@link PController} along
* with {@link SimpleMotorFeedforward}.
*/
public MotorEx(Motor mot, double cpr,
PIDController veloController,
PController positionController,
SimpleMotorFeedforward feedforward) {
motor = mot;
COUNTS_PER_REV = cpr;
velocityController = veloController;
this.positionController = positionController;
motorFeedforward = feedforward;
}
/**
* The constructor for the extended motor which includes a
* {@link PIDController} and {@link SimpleMotorFeedforward}.
*/
public MotorEx(Motor mot, double cpr,
PIDController veloController,
SimpleMotorFeedforward feedforward) {
this(mot, cpr, veloController, new PController(1), feedforward);
}
/**
* Set the zero power behavior of the motor.
*
* @param behavior The MotorEx.ZeroPowerBehavior of the motor.
*/
public void setZeroPowerBehavior(ZeroPowerBehavior behavior) {
zeroBehavior = behavior;
}
/**
* @return the current position of the motor in ticks
*/
public abstract int getCurrentPosition();
/**
* Resets the controlelrs running the robot
*/
public void resetControllers() {
velocityController.reset();
positionController.reset();
}
/**
* Sets the speed of the motor. This method needs to be overridden in your
* {@link Motor} object.
*
* @param speed The speed to set. Value should be between -1.0 and 1.0.
*/
public void set(double speed) {
motor.set(speed);
if (Math.abs(speed) <= 10E-2) {
if (zeroBehavior == ZeroPowerBehavior.FLOAT) {
positionController.control(motor, 0, motor.get(), 0.5);
} else if (zeroBehavior == ZeroPowerBehavior.BREAK) {
motor.stopMotor();
} else positionController.control(motor, 0, motor.get());
}
}
/**
* Disables the motor.
*/
@Override
public void disable() {
motor.disable();
}
/**
* Stops the motor.
*/
@Override
public void stopMotor() {
motor.stopMotor();
}
@Override
public double get() {
return motor.get();
}
/**
* This is the method that does the magic for the extended motor.
* It uses a {@link PIDController} and {@link SimpleMotorFeedforward}.
*
* @param output the desired output from the motor, from -1.0 to 1.0
*/
@Override
public void pidWrite(double output) {
motor.set(velocityController.calculate(get(), output) + motorFeedforward.calculate(output));
}
@Override
public void setInverted(boolean isInverted) {
motor.setInverted(isInverted);
}
@Override
public boolean getInverted() {
return motor.getInverted();
}
@Override
public String getDeviceType() {
return "Extended " + motor.getDeviceType();
}
}