2121import edu .wpi .first .wpilibj .PIDSourceType ;
2222import edu .wpi .first .wpilibj .SerialPort ;
2323import edu .wpi .first .wpilibj .SpeedControllerGroup ;
24+ import edu .wpi .first .wpilibj .VictorSP ;
2425import edu .wpi .first .wpilibj .drive .DifferentialDrive ;
2526import edu .wpi .first .wpilibj .smartdashboard .SmartDashboard ;
2627
3233 */
3334public class RobotMap {
3435
35- public static WPI_TalonSRX intakeMotor ;
3636 public static WPI_TalonSRX liftMotor ;
3737 public static WPI_TalonSRX climberMotor ;
3838
39+ public static VictorSP leftIntakeMotor ;
40+ public static VictorSP rightIntakeMotor ;
41+
3942 public static DigitalSource leftEncPort1 ;
4043 public static DigitalSource leftEncPort2 ;
4144 public static Encoder leftEncDist ;
@@ -102,18 +105,6 @@ private void configSPX(WPI_VictorSPX mc) {
102105 mc .configPeakOutputReverse (-1 , kTimeout );
103106 }
104107
105- /**
106- * Uses SmartDashboard and math to calculate a *great* default kD
107- */
108- public double calcDefkD () {
109- double timeConstant = Robot .getConst ("Omega Max" , 5330 ) * Robot .getConst ("Mass of Robot" , 54.4311 )
110- * Robot .getConst ("Radius of Drivetrain Wheel" , 0.0635 )
111- * Robot .getConst ("Radius of Drivetrain Wheel" , 0.0635 ) / Robot .getConst ("Stall Torque" , 2.41 );
112- double cycleTime = Robot .getConst ("Code cycle time" , 0.1 );
113- double denominator = 1 - Math .pow (Math .E , -1 * cycleTime / timeConstant );
114- return 1 / denominator ;
115- }
116-
117108 public RobotMap () {
118109
119110 // intakeMotor = new WPI_TalonSRX(getPort("IntakeTalonSRX", 4));
@@ -123,14 +114,17 @@ public RobotMap() {
123114 // climberMotor = new WPI_TalonSRX(getPort("ClimberTalonSRX", 6));
124115 // configSRX(climberMotor);
125116
117+ leftIntakeMotor = new VictorSP (getPort ("IntakeLeftVictorSP" , 0 ));
118+ rightIntakeMotor = new VictorSP (getPort ("IntakeRightVictorSP" , 1 ));
119+
126120 leftEncPort1 = new DigitalInput (getPort ("1LeftEnc" , 2 ));
127121 leftEncPort2 = new DigitalInput (getPort ("2LeftEnc" , 3 ));
128122 leftEncDist = new Encoder (leftEncPort1 , leftEncPort2 );
129123 leftEncDist .setPIDSourceType (PIDSourceType .kDisplacement );
130124 leftEncRate = new Encoder (leftEncPort1 , leftEncPort2 );
131125 leftEncRate .setPIDSourceType (PIDSourceType .kRate );
132- leftEncDist .setDistancePerPulse (Robot .getConst ("DPP" , DIST_PER_PULSE_RATIO ));
133- leftEncRate .setDistancePerPulse (Robot .getConst ("DPP" , DIST_PER_PULSE_RATIO ));
126+ leftEncDist .setDistancePerPulse (Robot .getConst ("DPP" , 0.013908 ));
127+ leftEncRate .setDistancePerPulse (Robot .getConst ("DPP" , 0.013908 ));
134128
135129 dtLeftMaster = new WPI_TalonSRX (getPort ("LeftTalonSRXMaster" , 1 ));
136130 configSRX (dtLeftMaster );
@@ -140,9 +134,9 @@ public RobotMap() {
140134 // inverted bc gear boxes invert from input to output
141135 dtLeft .setInverted (true );
142136
143- leftVelocityController = new VelocityPIDController (Robot .getConst ("VelocityLeftkP " , 0 ),
144- Robot .getConst ("VelocityLeftkI " , 0 ), Robot .getConst ("VelocityLeftkD " , 0 ) ,
145- 1 / Robot . getConst ( "Max Low Speed" , 84 ), leftEncRate , dtLeft );
137+ leftVelocityController = new VelocityPIDController (Robot .getConst ("VelocityLeftkI " , 0 ), 0 ,
138+ Robot .getConst ("VelocityLeftkD " , calcDefkD ()), 1 / Robot .getConst ("Max Low Speed " , 84 ), leftEncRate ,
139+ dtLeft );
146140 leftVelocityController .setInputRange (-Robot .getConst ("Max High Speed" , 204 ),
147141 Robot .getConst ("Max High Speed" , 204 ));
148142 leftVelocityController .setOutputRange (-1.0 , 1.0 );
@@ -156,8 +150,8 @@ public RobotMap() {
156150 rightEncDist .setPIDSourceType (PIDSourceType .kDisplacement );
157151 rightEncRate = new Encoder (rightEncPort1 , rightEncPort2 );
158152 rightEncRate .setPIDSourceType (PIDSourceType .kRate );
159- rightEncDist .setDistancePerPulse (Robot .getConst ("DPP" , DIST_PER_PULSE_RATIO ));
160- rightEncRate .setDistancePerPulse (Robot .getConst ("DPP" , DIST_PER_PULSE_RATIO ));
153+ rightEncDist .setDistancePerPulse (Robot .getConst ("DPP" , 0.013908 ));
154+ rightEncRate .setDistancePerPulse (Robot .getConst ("DPP" , 0.013908 ));
161155
162156 dtRightMaster = new WPI_TalonSRX (getPort ("RightTalonSRXMaster" , 4 ));
163157 configSRX (dtRightMaster );
@@ -167,18 +161,19 @@ public RobotMap() {
167161 // inverted bc gear boxes invert from input to output
168162 dtRight .setInverted (true );
169163
170- rightVelocityController = new VelocityPIDController (Robot .getConst ("VelocityRightkP " , 0 ),
171- Robot .getConst ("VelocityRightkI " , 0 ), Robot .getConst ("VelocityRightkD " , 0 ) ,
172- 1 / Robot . getConst ( "Max Low Speed" , 84 ), rightEncRate , dtRight );
164+ rightVelocityController = new VelocityPIDController (Robot .getConst ("VelocityRightkI " , 0 ), 0 ,
165+ Robot .getConst ("VelocityRightkD " , calcDefkD ()), 1 / Robot .getConst ("Max Low Speed " , 84 ), rightEncRate ,
166+ dtRight );
173167 rightVelocityController .setInputRange (-Robot .getConst ("Max High Speed" , 204 ),
174168 Robot .getConst ("Max High Speed" , 204 ));
175169 rightVelocityController .setOutputRange (-1.0 , 1.0 );
176170 rightVelocityController .setContinuous (false );
177171 rightVelocityController .setAbsoluteTolerance (Robot .getConst ("VelocityToleranceRight" , 2 ));
178172
179- robotDrive = new DifferentialDrive (leftVelocityController , rightVelocityController );
180- robotDrive .setMaxOutput (Robot .getConst ("Max High Speed" , 204 ));
181- // robotDrive = new DifferentialDrive(dtLeft, dtRight);
173+ // robotDrive = new DifferentialDrive(leftVelocityController,
174+ // rightVelocityController);
175+ // robotDrive.setMaxOutput(Robot.getConst("Max High Speed", 204));
176+ robotDrive = new DifferentialDrive (dtLeft , dtRight );
182177
183178 distEncAvg = new PIDSourceAverage (leftEncDist , rightEncDist );
184179 fancyGyro = new AHRS (SerialPort .Port .kMXP );
@@ -197,9 +192,48 @@ public RobotMap() {
197192 */
198193 public int getPort (String key , int def ) {
199194 if (!SmartDashboard .containsKey ("Port/" + key )) {
200- SmartDashboard .putNumber ("Port/" + key , def );
195+ if (!SmartDashboard .putNumber ("Port/" + key , def )) {
196+ System .err .println ("SmartDashboard Key" + "Port/" + key + "already taken by a different type" );
197+ return def ;
198+ }
201199 }
202200 return (int ) SmartDashboard .getNumber ("Port/" + key , def );
203201 }
204202
203+ /**
204+ * Uses SmartDashboard and math to calculate a *great* default kD
205+ */
206+ public double calcDefkD () {
207+ /*
208+ * timeConstant is proportional to max speed of the shaft (which is the max
209+ * speed of the cim divided by the gear reduction), half the mass (because the
210+ * half of the drivetrain only has to support half of the robot), and radius of
211+ * the drivetrain wheels squared. It's inversely proportional to the stall
212+ * torque of the shaft, which is found by multiplying the stall torque of the
213+ * motor with the gear reduction by the amount of motors.
214+ */
215+ double gearReduction = Robot .getBool ("High Gear" , false ) ? Robot .getConst ("High Gear Gear Reduction" , 5.392 )
216+ : Robot .getConst ("Low Gear Gear Reduction" , 12.255 );
217+ double radius = Robot .getConst ("Radius of Drivetrain Wheel" , 0.0635 );
218+ double timeConstant = Robot .getConst ("Omega Max" , 5330 ) / gearReduction
219+ * convertNtokG (Robot .getConst ("Weight of Robot" , 342 )) / 2 * radius * radius
220+ / (Robot .getConst ("Stall Torque" , 2.41 ) * gearReduction * 2 );
221+ double cycleTime = Robot .getConst ("Code cycle time" , 0.05 );
222+ /*
223+ * The denominator of kD is 1-(e ^ -cycleTime / timeConstant). The numerator is
224+ * one.
225+ */
226+ double denominator = 1 - Math .pow (Math .E , -1 * cycleTime / timeConstant );
227+ return 1 / denominator ;
228+ }
229+
230+ private double convertLbsTokG (double lbs ) {
231+ // number from google ;)
232+ return lbs * 0.45359237 ;
233+ }
234+
235+ private double convertNtokG (double newtons ) {
236+ // weight / accel due to grav = kg
237+ return newtons / 9.81 ;
238+ }
205239}
0 commit comments