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

Commit f89470d

Browse files
Conflicts: Robot2018/src/org/usfirst/frc/team199/Robot2018/Robot.java Robot2018/src/org/usfirst/frc/team199/Robot2018/RobotMap.java merging, most of this stuff not working super well rn
2 parents be7362f + 5ea98aa commit f89470d

10 files changed

Lines changed: 301 additions & 96 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ public class OI {
3535
private JoystickButton updatePIDConstantsButton;
3636
private JoystickButton updateEncoderDPPButton;
3737
public Joystick manipulator;
38-
38+
3939
public int getButton(String key, int def) {
4040
if (!SmartDashboard.containsKey("Button/" + key)) {
41-
SmartDashboard.putNumber("Button/" + key, def);
42-
}
41+
if (!SmartDashboard.putNumber("Button/" + key, def)) {
42+
System.err.println("SmartDashboard Key" + "Button/" + key + "already taken by a different type");
43+
return def;
44+
}
45+
}
4346
return (int) SmartDashboard.getNumber("Button/" + key, def);
4447
}
4548

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

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/*----------------------------------------------------------------------------*/
2+
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
3+
/* Open Source Software - may be modified and shared by FRC teams. The code */
4+
/* must be accompanied by the FIRST BSD license file in the root directory of */
5+
/* the project. */
6+
/*----------------------------------------------------------------------------*/
17

28
package org.usfirst.frc.team199.Robot2018;
39

@@ -9,6 +15,7 @@
915
import org.usfirst.frc.team199.Robot2018.commands.Autonomous;
1016
import org.usfirst.frc.team199.Robot2018.commands.Autonomous.Position;
1117
import org.usfirst.frc.team199.Robot2018.commands.Autonomous.Strategy;
18+
import org.usfirst.frc.team199.Robot2018.commands.ShiftLowGear;
1219
import org.usfirst.frc.team199.Robot2018.subsystems.Climber;
1320
import org.usfirst.frc.team199.Robot2018.subsystems.ClimberAssist;
1421
import org.usfirst.frc.team199.Robot2018.subsystems.Drivetrain;
@@ -51,14 +58,20 @@ public class Robot extends TimedRobot {
5158

5259
public static double getConst(String key, double def) {
5360
if (!SmartDashboard.containsKey("Const/" + key)) {
54-
SmartDashboard.putNumber("Const/" + key, def);
61+
if (!SmartDashboard.putNumber("Const/" + key, def)) {
62+
System.err.println("SmartDashboard Key" + "Const/" + key + "already taken by a different type");
63+
return def;
64+
}
5565
}
5666
return SmartDashboard.getNumber("Const/" + key, def);
5767
}
5868

5969
public static boolean getBool(String key, boolean def) {
6070
if (!SmartDashboard.containsKey("Bool/" + key)) {
61-
SmartDashboard.putBoolean("Bool/" + key, def);
71+
if (!SmartDashboard.putBoolean("Bool/" + key, def)) {
72+
System.err.println("SmartDashboard Key" + "Bool/" + key + "already taken by a different type");
73+
return def;
74+
}
6275
}
6376
return SmartDashboard.getBoolean("Bool/" + key, def);
6477
}
@@ -124,6 +137,7 @@ public void disabledPeriodic() {
124137
*/
125138
@Override
126139
public void autonomousInit() {
140+
Scheduler.getInstance().add(new ShiftLowGear());
127141
String fmsInput = DriverStation.getInstance().getGameSpecificMessage();
128142
Position startPos = posChooser.getSelected();
129143
double autoDelay = SmartDashboard.getNumber("Auto Delay", 0);
@@ -155,6 +169,7 @@ public void teleopInit() {
155169
// this line or comment it out.
156170
if (autonomousCommand != null)
157171
autonomousCommand.cancel();
172+
dt.putVelocityControllersToDashboard();
158173
}
159174

160175
/**
@@ -177,16 +192,14 @@ public void testPeriodic() {
177192
// firstTime = false;
178193
//// }
179194
// Robot.dt.setVPIDs(Robot.getConst("VPID Test Set", 0.5));
180-
// SmartDashboard.putNumber("Drivetrain/Left VPID Targ",
181-
// Robot.dt.getLeftVPIDSetpoint());
182-
// SmartDashboard.putNumber("Drivetrain/Right VPID Targ",
183-
// Robot.dt.getRightVPIDSetpoint());
184-
// SmartDashboard.putNumber("Left VPID Error", Robot.dt.getLeftVPIDerror());
185-
// SmartDashboard.putNumber("Right VPID Error", Robot.dt.getRightVPIDerror());
186-
// SmartDashboard.putNumber("Left Enc Rate", Robot.dt.getLeftEncRate());
187-
// SmartDashboard.putNumber("Right Enc Rate", Robot.dt.getRightEncRate());
188-
189-
dt.dtLeft.set(0.1);
195+
SmartDashboard.putNumber("Drivetrain/Left VPID Targ", Robot.dt.getLeftVPIDSetpoint());
196+
SmartDashboard.putNumber("Drivetrain/Right VPID Targ", Robot.dt.getRightVPIDSetpoint());
197+
SmartDashboard.putNumber("Left VPID Error", Robot.dt.getLeftVPIDerror());
198+
SmartDashboard.putNumber("Right VPID Error", Robot.dt.getRightVPIDerror());
199+
SmartDashboard.putNumber("Left Enc Rate", Robot.dt.getLeftEncRate());
200+
SmartDashboard.putNumber("Right Enc Rate", Robot.dt.getRightEncRate());
201+
202+
// dt.dtLeft.set(0.1);
190203
// dt.dtRight.set(-oi.rightJoy.getY());
191204
// dt.dtLeft.set(-oi.leftJoy.getY());
192205
// dt.dtRight.set(-oi.rightJoy.getY());

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

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import edu.wpi.first.wpilibj.PIDSourceType;
2222
import edu.wpi.first.wpilibj.SerialPort;
2323
import edu.wpi.first.wpilibj.SpeedControllerGroup;
24+
import edu.wpi.first.wpilibj.VictorSP;
2425
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
2526
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
2627

@@ -32,10 +33,12 @@
3233
*/
3334
public 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
}

Robot2018/src/org/usfirst/frc/team199/Robot2018/commands/PIDMove.java

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,43 @@
1010
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
1111

1212
/**
13-
*
13+
* Drives the robot a certain target distance using PID. Implements PIDOutput in
14+
* order to keep move PID centralized in this command.
1415
*/
1516
public class PIDMove extends Command implements PIDOutput {
1617

1718
private double target;
1819
private DrivetrainInterface dt;
1920
private PIDController moveController;
2021

22+
/**
23+
* Constructs this command with a new PIDController. Sets all of the
24+
* controller's PID constants based on SD prefs. Sets the controller's PIDSource
25+
* to the encoder average object and sets its PIDOutput to this command which
26+
* implements PIDOutput's pidWrite() method.
27+
*
28+
* @param targ
29+
* the target distance (in inches) to move to
30+
* @param dt
31+
* the Drivetrain (for actual code) or a DrivetrainInterface (for
32+
* testing)
33+
* @param avg
34+
* the PIDSourceAverage of the DT's two Encoders
35+
*/
2136
public PIDMove(double targ, DrivetrainInterface dt, PIDSourceAverage avg) {
2237
// Use requires() here to declare subsystem dependencies
23-
// eg. requires(chassis);
38+
requires(Robot.dt);
2439
target = targ;
2540
this.dt = dt;
26-
requires(Robot.dt);
2741
double kf = 1 / (dt.getCurrentMaxSpeed() * Robot.getConst("Default PID Update Time", 0.05));
2842
moveController = new PIDController(Robot.getConst("MovekP", 1), Robot.getConst("MovekI", 0),
2943
Robot.getConst("MovekD", 0), kf, avg, this);
3044
}
3145

32-
// Called just before this Command runs the first time
46+
/**
47+
* Called just before this Command runs the first time. Resets the distance
48+
* encoders, sets the moveController's settings, and then enables it.
49+
*/
3350
@Override
3451
public void initialize() {
3552
dt.resetDistEncs();
@@ -44,33 +61,57 @@ public void initialize() {
4461
SmartDashboard.putData(moveController);
4562
}
4663

47-
// Called repeatedly when this Command is scheduled to run
64+
/**
65+
* Called repeatedly when this Command is scheduled to run. This method is empty
66+
* bc the moveController runs on a different thread as soon as it is enabled in
67+
* initialize().
68+
*/
4869
@Override
4970
protected void execute() {
50-
// This method is empty bc the moveController runs on a different thread as soon
51-
// as it is enabled.
5271
}
5372

54-
// Make this return true when this Command no longer needs to run execute()
73+
/**
74+
* Tells this command to terminate when the moveController has reached its
75+
* target.
76+
*
77+
* @return true if the robot has moved the correct distance, false if not
78+
*/
5579
@Override
5680
protected boolean isFinished() {
5781
return moveController.onTarget();
5882
}
5983

60-
// Called once after isFinished returns true
84+
/**
85+
* Called once after isFinished returns true. Disables and frees the
86+
* moveController, essentially turning it "off" and "deleting" the thread it was
87+
* running on.
88+
*/
6189
@Override
6290
protected void end() {
6391
moveController.disable();
6492
moveController.free();
6593
}
6694

67-
// Called when another command which requires one or more of the same
68-
// subsystems is scheduled to run
95+
/**
96+
* Called when another command which requires one or more of the same subsystems
97+
* is scheduled to run. Disables and frees the moveController, essentially
98+
* turning it "off" and "deleting" the thread it was running on.
99+
*/
69100
@Override
70101
protected void interrupted() {
71102
end();
72103
}
73104

105+
/**
106+
* Implementation of PIDOutput's pidWrite method. Sends the moveController's
107+
* output speed to the motors as the drive/forward speed in DifferentialDrive's
108+
* arcadeDrive method.
109+
*
110+
* @param output
111+
* the output drive/forward speed [-1, 1], calculated by the move
112+
* PIDController, to pass in as the drive/forward speed in
113+
* arcadeDrive()
114+
*/
74115
@Override
75116
public void pidWrite(double output) {
76117
dt.arcadeDrive(output, 0);

0 commit comments

Comments
 (0)