Skip to content
This repository was archived by the owner on Sep 14, 2019. It is now read-only.

Commit 5736362

Browse files
authored
Merge pull request #94 from lhmcgann/intake-changes
Intake changes: simplification
2 parents 2979aee + fcd4ae4 commit 5736362

File tree

7 files changed

+330
-115
lines changed

7 files changed

+330
-115
lines changed

Robot2018/src/org/usfirst/frc/team199/Robot2018/RobotMap.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ public RobotMap() {
127127

128128
leftIntakeMotor = new VictorSP(getPort("IntakeLeftVictorSP", 8));
129129
rightIntakeMotor = new VictorSP(getPort("IntakeRightVictorSP", 9));
130-
leftIntakeSolenoid = new DoubleSolenoid(getPort("IntakeLeftHorizontalSolenoidPort1", 4),
131-
getPort("IntakeLeftHorizontalSolenoidPort2", 5));
132-
rightIntakeSolenoid = new DoubleSolenoid(getPort("IntakeRightHorizontalSolenoidPort1", 2),
133-
getPort("IntakeRightHorizontalSolenoidPort2", 3));
130+
leftIntakeSolenoid = new DoubleSolenoid(getPort("IntakeLeftSolenoidForward", 4),
131+
getPort("IntakeLeftSolenoidReverse", 5));
132+
rightIntakeSolenoid = new DoubleSolenoid(getPort("IntakeRightSolenoidForward", 2),
133+
getPort("IntakeRightSolenoidReverse", 3));
134134

135135
leftEncPort1 = new DigitalInput(getPort("1LeftEnc", 2));
136136
leftEncPort2 = new DigitalInput(getPort("2LeftEnc", 3));
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.usfirst.frc.team199.Robot2018.autonomous;
2+
3+
import com.ctre.phoenix.motorcontrol.ControlMode;
4+
import com.ctre.phoenix.motorcontrol.can.WPI_TalonSRX;
5+
6+
import edu.wpi.first.wpilibj.PIDSource;
7+
import edu.wpi.first.wpilibj.PIDSourceType;
8+
9+
public class PIDSourceFromTalon implements PIDSource {
10+
11+
private PIDSourceType type;
12+
private WPI_TalonSRX talon;
13+
14+
public PIDSourceFromTalon(WPI_TalonSRX talon) {
15+
this.talon = talon;
16+
ControlMode mode = talon.getControlMode();
17+
switch (mode.toString()) {
18+
case "Position":
19+
type = PIDSourceType.kDisplacement;
20+
break;
21+
default:
22+
type = PIDSourceType.kRate;
23+
break;
24+
}
25+
}
26+
27+
@Override
28+
public void setPIDSourceType(PIDSourceType pidSource) {
29+
type = pidSource;
30+
}
31+
32+
@Override
33+
public PIDSourceType getPIDSourceType() {
34+
return type;
35+
}
36+
37+
@Override
38+
public double pidGet() {
39+
switch (type.toString()) {
40+
case "kDisplacement":
41+
return talon.getSelectedSensorPosition(0);
42+
default:
43+
return talon.getSelectedSensorVelocity(0);
44+
}
45+
}
46+
47+
}
Lines changed: 141 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,184 @@
11
package org.usfirst.frc.team199.Robot2018.autonomous;
22

3+
import com.ctre.phoenix.motorcontrol.ControlMode;
4+
import com.ctre.phoenix.motorcontrol.FeedbackDevice;
35
import com.ctre.phoenix.motorcontrol.can.WPI_TalonSRX;
46

7+
import edu.wpi.first.wpilibj.Encoder;
8+
import edu.wpi.first.wpilibj.PIDSource;
9+
import edu.wpi.first.wpilibj.PIDSourceType;
510
import edu.wpi.first.wpilibj.SpeedController;
611

712
public class TalonVelocityController implements SpeedController {
813

914
private WPI_TalonSRX talon;
1015

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) {
1241
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+
}
1351
}
1452

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+
*/
1560
@Override
1661
public void pidWrite(double output) {
17-
// TODO Auto-generated method stub
1862
set(output);
1963
}
2064

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+
*/
2172
@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);
2577
}
2678

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+
*/
2784
@Override
2885
public double get() {
29-
// TODO Auto-generated method stub
30-
return 0;
86+
return talon.get();
3187
}
3288

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+
*/
33135
@Override
34136
public void setInverted(boolean isInverted) {
35-
// TODO Auto-generated method stub
36-
137+
talon.setInverted(isInverted);
37138
}
38139

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+
*/
39145
@Override
40146
public boolean getInverted() {
41-
// TODO Auto-generated method stub
42-
return false;
147+
return talon.getInverted();
43148
}
44149

150+
/**
151+
* Disable this controller.
152+
*/
45153
@Override
46154
public void disable() {
47-
// TODO Auto-generated method stub
48-
155+
talon.disable();
49156
}
50157

158+
/**
159+
* Stop the motor until Set is called again.
160+
*/
51161
@Override
52162
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+
}
54174

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);
55182
}
56183

57184
}

Robot2018/src/org/usfirst/frc/team199/Robot2018/autonomous/VelocityPIDController.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ public class VelocityPIDController extends PIDController implements SpeedControl
1313

1414
/**
1515
* Constructs a VelocityPIDContoller and invokes the super constructor
16-
* (PIDController), setting the three PID constants and the source and output
17-
* for this VelocityPIDController.
16+
* (PIDController), setting the four PID constants and the source and output for
17+
* this VelocityPIDController.
1818
*
1919
* @param kp
20-
* the proportional PID constant
20+
* the proportional PID constant, multiplied by the total error (aka
21+
* I)
2122
* @param ki
22-
* the integral PID constant
23+
* the integral PID constant, not used in velocity control
2324
* @param kd
24-
* the derivative PID constant
25+
* the derivative PID constant, multiplied by the error (aka P)
2526
* @param kf
2627
* the feed forward value: should be 1/MaxSpeed
2728
* @param source

0 commit comments

Comments
 (0)