|
1 | 1 | package org.usfirst.frc.team199.Robot2018.autonomous; |
2 | 2 |
|
| 3 | +import com.ctre.phoenix.motorcontrol.ControlMode; |
| 4 | +import com.ctre.phoenix.motorcontrol.FeedbackDevice; |
3 | 5 | import com.ctre.phoenix.motorcontrol.can.WPI_TalonSRX; |
4 | 6 |
|
| 7 | +import edu.wpi.first.wpilibj.Encoder; |
| 8 | +import edu.wpi.first.wpilibj.PIDSource; |
| 9 | +import edu.wpi.first.wpilibj.PIDSourceType; |
5 | 10 | import edu.wpi.first.wpilibj.SpeedController; |
6 | 11 |
|
7 | 12 | public class TalonVelocityController implements SpeedController { |
8 | 13 |
|
9 | 14 | private WPI_TalonSRX talon; |
10 | 15 |
|
11 | | - public TalonVelocityController(WPI_TalonSRX output) { |
| 16 | + /** |
| 17 | + * Constructs a TalonVelocityContoller. Sets the four PID constants and the |
| 18 | + * FeedbackDevice of the output Talon. |
| 19 | + * |
| 20 | + * @param kp |
| 21 | + * the proportional PID constant, multiplied by the error |
| 22 | + * @param ki |
| 23 | + * the integral PID constant, multiplied by the total error |
| 24 | + * @param kd |
| 25 | + * the derivative PID constant, multiplied by the change in error |
| 26 | + * @param kf |
| 27 | + * the feed forward value: should be 1/MaxSpeed |
| 28 | + * @param source |
| 29 | + * the sensor (e.g. velocity encoder) you are reading from |
| 30 | + * @param output |
| 31 | + * the Talon SRX you are telling what to do |
| 32 | + * @param slotIdx |
| 33 | + * Parameter slot for the constant. |
| 34 | + * @param timeoutMs |
| 35 | + * Timeout value in ms. If nonzero, function will wait for config |
| 36 | + * success and report an error if it times out. If zero, no blocking |
| 37 | + * or checking is performed. |
| 38 | + */ |
| 39 | + public TalonVelocityController(double kp, double ki, double kd, double kf, PIDSource source, WPI_TalonSRX output, |
| 40 | + int slotIdx, int timeoutMs) { |
12 | 41 | talon = output; |
| 42 | + talon.config_kP(slotIdx, kp, timeoutMs); |
| 43 | + talon.config_kI(slotIdx, ki, timeoutMs); |
| 44 | + talon.config_kD(slotIdx, kd, timeoutMs); |
| 45 | + talon.config_kF(slotIdx, kf, timeoutMs); |
| 46 | + if (source instanceof Encoder && source.getPIDSourceType().equals(PIDSourceType.kRate)) { |
| 47 | + talon.configSelectedFeedbackSensor(FeedbackDevice.QuadEncoder, 0, timeoutMs); |
| 48 | + } else { |
| 49 | + throw new IllegalArgumentException(); |
| 50 | + } |
13 | 51 | } |
14 | 52 |
|
| 53 | + /** |
| 54 | + * Sets the output of the output Talon based on the specified Velocity |
| 55 | + * ControlMode. |
| 56 | + * |
| 57 | + * @param output |
| 58 | + * setpoint value in inches/second |
| 59 | + */ |
15 | 60 | @Override |
16 | 61 | public void pidWrite(double output) { |
17 | | - // TODO Auto-generated method stub |
18 | 62 | set(output); |
19 | 63 | } |
20 | 64 |
|
| 65 | + /** |
| 66 | + * Sets the output of the output Talon based on the specified Velocity |
| 67 | + * ControlMode. |
| 68 | + * |
| 69 | + * @param output |
| 70 | + * setpoint value in inches/second |
| 71 | + */ |
21 | 72 | @Override |
22 | | - public void set(double speed) { |
23 | | - // TODO Auto-generated method stub |
24 | | - |
| 73 | + public void set(double output) { |
| 74 | + // the div. by 10 is to translate from our desired param units (in/s) to the |
| 75 | + // Talon's set() method param units (in/100ms) |
| 76 | + talon.set(ControlMode.Velocity, output / 10); |
25 | 77 | } |
26 | 78 |
|
| 79 | + /** |
| 80 | + * Get the current set speed of this side of the DT. |
| 81 | + * |
| 82 | + * @return the current set speed of this side of the DT from [-1, 1] |
| 83 | + */ |
27 | 84 | @Override |
28 | 85 | public double get() { |
29 | | - // TODO Auto-generated method stub |
30 | | - return 0; |
| 86 | + return talon.get(); |
31 | 87 | } |
32 | 88 |
|
| 89 | + /** |
| 90 | + * Sets the kP constant. |
| 91 | + * |
| 92 | + * @param value |
| 93 | + * the value to set kP to |
| 94 | + */ |
| 95 | + public void setP(double value) { |
| 96 | + talon.config_kP(0, value, 0); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Sets the kI constant. |
| 101 | + * |
| 102 | + * @param value |
| 103 | + * the value to set kI to |
| 104 | + */ |
| 105 | + public void setI(double value) { |
| 106 | + talon.config_kI(0, value, 0); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Sets the kD constant. |
| 111 | + * |
| 112 | + * @param value |
| 113 | + * the value to set kD to |
| 114 | + */ |
| 115 | + public void setD(double value) { |
| 116 | + talon.config_kD(0, value, 0); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Sets the feed forward gain. |
| 121 | + * |
| 122 | + * @param value |
| 123 | + * the value to set kF to |
| 124 | + */ |
| 125 | + public void setF(double value) { |
| 126 | + talon.config_kF(0, value, 0); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Invert this side of the DT (flip forwards and backwards). |
| 131 | + * |
| 132 | + * @param isInverted |
| 133 | + * invert this side of the DT or not |
| 134 | + */ |
33 | 135 | @Override |
34 | 136 | public void setInverted(boolean isInverted) { |
35 | | - // TODO Auto-generated method stub |
36 | | - |
| 137 | + talon.setInverted(isInverted); |
37 | 138 | } |
38 | 139 |
|
| 140 | + /** |
| 141 | + * Get whether or not this side of the DT is inverted. |
| 142 | + * |
| 143 | + * @return is this side of the DT inverted or not |
| 144 | + */ |
39 | 145 | @Override |
40 | 146 | public boolean getInverted() { |
41 | | - // TODO Auto-generated method stub |
42 | | - return false; |
| 147 | + return talon.getInverted(); |
43 | 148 | } |
44 | 149 |
|
| 150 | + /** |
| 151 | + * Disable this controller. |
| 152 | + */ |
45 | 153 | @Override |
46 | 154 | public void disable() { |
47 | | - // TODO Auto-generated method stub |
48 | | - |
| 155 | + talon.disable(); |
49 | 156 | } |
50 | 157 |
|
| 158 | + /** |
| 159 | + * Stop the motor until Set is called again. |
| 160 | + */ |
51 | 161 | @Override |
52 | 162 | public void stopMotor() { |
53 | | - // TODO Auto-generated method stub |
| 163 | + talon.stopMotor(); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Gets the selected sensor position (in raw sensor units). |
| 168 | + * |
| 169 | + * @return the selected sensor position (in raw sensor units). |
| 170 | + */ |
| 171 | + public double getCurrentPosition() { |
| 172 | + return talon.getSelectedSensorPosition(0); |
| 173 | + } |
54 | 174 |
|
| 175 | + /** |
| 176 | + * Gets the selected sensor velocity (in raw sensor units per 100ms). |
| 177 | + * |
| 178 | + * @return the selected sensor velocity (in raw sensor units per 100ms). |
| 179 | + */ |
| 180 | + public double getCurrentVelocity() { |
| 181 | + return talon.getSelectedSensorVelocity(0); |
55 | 182 | } |
56 | 183 |
|
57 | 184 | } |
0 commit comments