Skip to content

Commit 1664ca7

Browse files
committed
testing for 14.0- NEED TO CHANGE ELECTRICAL PORTS BASED ON FALCON IDs AND CONNECTIONS TO ROBORIO
1 parent 9aa6ab6 commit 1664ca7

File tree

1 file changed

+128
-132
lines changed

1 file changed

+128
-132
lines changed

src/main/java/frc/robot/Robot.java

+128-132
Original file line numberDiff line numberDiff line change
@@ -26,117 +26,119 @@
2626

2727

2828
public class Robot extends TimedRobot {
29-
private int counter = 0;
29+
// AUTONOMOUS VARIABLES
30+
private int counter = 0;// TODO: Simple initialization for autonomous duration it takes to kickup counter-> can be adjusted on line 176
31+
private boolean ready; // Simple flag used for autonomous staging
32+
private static final String kDefaultAuto = "Default"; //types of autos- not used
33+
private static final String kCustomAuto = "My Auto"; //types of autos- not currently used
34+
private String m_autoSelected;
35+
private final SendableChooser<String> m_chooser = new SendableChooser<>();
36+
private boolean stage1; // auto staging
37+
38+
// CONSTANTS
3039
private static final double WHEEL_DIAMETER = 6; // inches
31-
//NEED TO FIGURE OUT ENCODER MODEL
40+
//NEED TO FIGURE OUT ENCODER MODEL- changes CPR for drive
3241
private static final double cpr = 360; //if am-3132
3342
//private static final double cpr = 7/4; //if am-2861a
3443
// private static final double cpr = 5; //if am-3314a
3544
// private static final double cpr = 1024; //if am-3445
3645
// private static final double cpr = 64; //if am-4027
46+
private static final double drive_dpp = (Math.PI*WHEEL_DIAMETER/cpr); // Gives in inches per rev FOR DRIVETRAIN WHEELS ONLY
3747

38-
private static final double drive_dpp = (Math.PI*WHEEL_DIAMETER/cpr); // FOR DRIVETRAIN WHEELS ONLY
39-
40-
private boolean ready;
41-
42-
private static final String kDefaultAuto = "Default";
43-
private static final String kCustomAuto = "My Auto";
44-
45-
private String m_autoSelected;
46-
private final SendableChooser<String> m_chooser = new SendableChooser<>();
48+
// TODO: Check what motors should be inverted (.setInverted(false))
49+
// MOTOR VARIABLES
50+
private WPI_TalonFX leftSlave, rightSlave, leftMaster, rightMaster, climbRotation, climbExtension, shooterFly, wrist; //Falcon 500s
51+
private TalonSRX rollers, hood; // 775s or BAG motors
4752

48-
private WPI_TalonFX leftSlave, rightSlave, leftMaster, rightMaster, climbRotation, climbExtension, shooterFly, wrist;
49-
private TalonSRX rollers, hood;
50-
private Joystick leftJoy, rightJoy;
51-
private XboxController controller = new XboxController(2);
53+
//CONTROLLERS
54+
private Joystick leftJoy, rightJoy; //Used for tank drive
55+
private XboxController controller; // Used for button man mechanism controls
5256

53-
private AHRS gyro;
54-
55-
private DifferentialDrive drive;
56-
private double nonZeroLimelightHorAng;
57+
// SENSORS
58+
private AHRS gyro; //9-axis-> used mainly to orient shooter hood using roll
5759

58-
private Encoder rightEncoder, leftEncoder;
59-
private DigitalInput shooterLimit;
60-
private DigitalInput intakeLimit;
61-
62-
//auto variables
63-
private boolean stage1;
60+
private Encoder rightEncoder, leftEncoder; // Drivetrain encoders (might just use integrated Falcon stuff who knows)
61+
private DigitalInput lowerShooterLimit, upperShooterLimit; // Limit switch to reset hood to its default position
62+
private DigitalInput intakeLimit; // Limit switch to know if intake is ready to kickup
6463

65-
//private ADXRS450_Gyro gyro;
66-
//private LiDARSensor liDAR;
64+
// MISCELLANEOUS
65+
private double nonZeroLimelightHorAng; // Used to orient bot
66+
private DifferentialDrive drive; //Used for monitoring tank drive motion
6767

6868
/**
6969
* This function is run when the robot is first started up and should be
7070
* used for any initialization code.
7171
*/
7272
@Override
7373
public void robotInit() {
74+
// STAGING FLAGS
75+
stage1 = true; // Auto starts in stage 1 and turns into stage 2
76+
ready = false; // Not ready to shoot by default
77+
78+
// CONTROLLER PORTS
79+
controller = new XboxController(2);
80+
leftJoy = new Joystick(0);
81+
rightJoy = new Joystick(1);
82+
83+
// Used to select autonomous mode
7484
m_chooser.setDefaultOption("Default Auto", kDefaultAuto);
7585
m_chooser.addOption("My Auto", kCustomAuto);
7686
SmartDashboard.putData("Auto choices", m_chooser);
77-
stage1 = true;
78-
ready = false;
79-
87+
88+
// SENSOR INITIALIZATIONS
8089
gyro = new AHRS(SerialPort.Port.kMXP); //SUBJECT TO CHANGE FROM ELECTRICAL COULD BE SOURCE OF ERROR
90+
// ALL Ports subject to change from Electrical
91+
leftEncoder = new Encoder(0, 1);
92+
leftEncoder.setDistancePerPulse(drive_dpp);
93+
rightEncoder = new Encoder(2, 3);
94+
rightEncoder.setDistancePerPulse(drive_dpp);
95+
// Limit switches- CHANGE PORTS BASED ON ELECTRICAL
96+
lowerShooterLimit = new DigitalInput(4);
97+
upperShooterLimit = new DigitalInput(5);
98+
intakeLimit = new DigitalInput(6);
8199

100+
// MOTORS
82101
//Drivetrain motors and configuration
83102
rightMaster = new WPI_TalonFX(RobotMap.DRIVETRAIN_FRONTRIGHT);
84103
rightSlave = new WPI_TalonFX(RobotMap.DRIVETRAIN_BACKRIGHT);
85104
rightSlave.follow(rightMaster);
86105
leftMaster = new WPI_TalonFX(RobotMap.DRIVETRAIN_FRONTLEFT);
87106
leftSlave = new WPI_TalonFX(RobotMap.DRIVETRAIN_BACKLEFT);
88107
leftSlave.follow(leftMaster);
89-
90-
// Used for tank and arcade drive respectively
91-
drive = new DifferentialDrive(leftMaster, rightMaster);
92-
93-
// Default integrated sensors
108+
// Set integrated sensor position to 0 for encoder use
94109
leftMaster.getSensorCollection().setIntegratedSensorPosition(0, 10);
95110
rightMaster.getSensorCollection().setIntegratedSensorPosition(0, 10);
96111

97-
98-
// ALL Ports subject to change from Electrical
99-
leftEncoder = new Encoder(0, 1);
100-
leftEncoder.setDistancePerPulse(drive_dpp);
101-
rightEncoder = new Encoder(2, 3);
102-
rightEncoder.setDistancePerPulse(drive_dpp);
103-
104-
// Control Initializations
105-
leftJoy = new Joystick(0);
106-
rightJoy = new Joystick(1);
107-
108112
//Climb Motors
109113
climbRotation = new WPI_TalonFX(RobotMap.CLIMBROTATION);
110114
climbExtension = new WPI_TalonFX(RobotMap.CLIMBEXTENSION);
111115

112-
//Shooter Motors
113-
shooterFly = new WPI_TalonFX(RobotMap.SHOOTER_FLY);
114-
shooterFly.configSelectedFeedbackSensor(TalonFXFeedbackDevice.IntegratedSensor, 0, 1);
115-
shooterFly.configNominalOutputForward(0, 1);
116-
shooterFly.configNominalOutputReverse(0, 1);
117-
shooterFly.configPeakOutputForward(1, 1);
118-
shooterFly.configPeakOutputReverse(-1, 1);
119-
shooterFly.setInverted(false);
120-
shooterFly.setSensorPhase(false);
121-
// config P,I,D,F values- start by doubling F, then P, then D, then I (middle values) then increase over time
122-
shooterFly.config_kF(0, 0.007, 1);
123-
shooterFly.config_kP(0, 0.8192, 1);
124-
shooterFly.config_kI(0, 0.0008, 1);
125-
shooterFly.config_kD(0, 0.0256, 1);
126-
127116
//Intake Motors
128117
wrist = new WPI_TalonFX(RobotMap.INTAKE_WRIST);
129118
rollers = new TalonSRX(RobotMap.INTAKE_ROLLERS);
130119

131120
//Hood Motor
132121
hood = new TalonSRX(RobotMap.SHOOTER_HOOD);
133122

134-
if (!shooterLimit.get()) {
135-
hood.set(ControlMode.PercentOutput, -0.1);
136-
}
137-
gyro.reset();
123+
//Shooter Motors
124+
shooterFly = new WPI_TalonFX(RobotMap.SHOOTER_FLY);
125+
shooterFly.configSelectedFeedbackSensor(TalonFXFeedbackDevice.IntegratedSensor, 0, 1); // Tells to use in-built falcon 500 sensor
126+
shooterFly.configNominalOutputForward(0, 1);
127+
shooterFly.configNominalOutputReverse(0, 1);
128+
shooterFly.configPeakOutputForward(1, 1);
129+
shooterFly.configPeakOutputReverse(-1, 1);
130+
shooterFly.setInverted(false);
131+
shooterFly.setSensorPhase(false);
132+
// config P,I,D,F values- start by doubling F, then P, then D, then I (middle values) then increase/decrease over time
133+
shooterFly.config_kF(0, 0.007, 1); // (F) Feed Forward Term
134+
shooterFly.config_kP(0, 0.8192, 1); // (P) Proportional Term
135+
shooterFly.config_kI(0, 0.0008, 1); // (I) Integral term
136+
shooterFly.config_kD(0, 0.0256, 1); // (D) Differentiable Term
137+
138+
// Used for tank and arcade drive respectively
139+
drive = new DifferentialDrive(leftMaster, rightMaster);
138140

139-
// Display information
141+
// TODO- make this choose from sendable chooser to set alliance color
140142
NetworkTableInstance.getDefault().getTable("limelight").getEntry("pipeline").setNumber(0); //Sets the pipeline to 0
141143
NetworkTableInstance.getDefault().getTable("limelight").getEntry("camMode").setNumber(0); //Sets the Limelight as a Vision Proccesor
142144
NetworkTableInstance.getDefault().getTable("TalonPi").getEntry("alliance").setString("red");
@@ -154,26 +156,34 @@ public void autonomousInit() {
154156
*/
155157
@Override
156158
public void autonomousPeriodic() {
159+
// Display information relayed by Limelight and RPM information for testing
157160
double[][] shooterCalculations = getLimelightData();
158161
if(shooterCalculations != null) {
159162
SmartDashboard.putNumber("Distance: ",shooterCalculations[0][0]);
160-
SmartDashboard.putNumber("Shooter Angle: ",shooterCalculations[0][1]);
163+
SmartDashboard.putNumber("Needed Shooter Angle: ",shooterCalculations[0][1]);
161164
SmartDashboard.putNumber("Ideal Ball Velocity :",shooterCalculations[0][2]);
162165
SmartDashboard.putNumber("Limelight H-Angle: ",shooterCalculations[1][0]);
163166
SmartDashboard.putNumber("Limelight V-Angle: ",shooterCalculations[1][1]);
164167
SmartDashboard.putNumber("Limelight Latency: ",shooterCalculations[1][2]);
165168
SmartDashboard.putNumber("Flywheel RPM: ", shooterFly.getSelectedSensorVelocity()/4 * 2048);
169+
SmartDashboard.putNumber("Current Angle", gyro.getRoll());
166170
}
167171

172+
// Resets hood to position 0 and uses that as the 0-angle.
173+
if (!lowerShooterLimit.get()) {
174+
hood.set(ControlMode.PercentOutput, -0.1);
175+
}
176+
gyro.reset(); //sets gyro value to 0
177+
168178
if ((Math.abs(shooterCalculations[0][0]) < 1.5)) {
169179
drive.tankDrive(-0.8, -0.8); // backs up until 1.5 meters away
170180
}
171-
if ((Math.abs(shooterCalculations[0][0]) >= 1.5) && (Math.abs(shooterCalculations[1][0]) > 1)) { //adjust to reduce bouncing
181+
if ((Math.abs(shooterCalculations[0][0]) >= 1.5) && (Math.abs(shooterCalculations[1][0]) > 1)) { // TODO: adjust to reduce bouncing
172182
centerAim("top_hub"); // fires a shot into the hub
173183
}
174184
if (Math.abs(shooterCalculations[1][0]) < 1 && (Math.abs(shooterCalculations[0][0]) >= 1.5) && stage1) {
175-
if (counter < 1000) { // adjust based on how long it takes to shoot a ball
176-
autoFire(); // shoots ball-> changes stage1 to false after having shot a ball
185+
if (counter < 1000) { // TODO: adjust based on how long it takes to shoot a ball
186+
fire(); // shoots ball-> changes stage1 to false after having shot a ball
177187
counter++;
178188
}
179189
else if (counter >= 1000) {
@@ -190,29 +200,11 @@ else if (counter >= 1000) {
190200
}
191201
}
192202

193-
/**
194-
* The shooter method for auto mode
195-
*/
196-
197203
/**
198204
* This function is called periodically during operator control.
199205
*/
200206
@Override
201207
public void teleopPeriodic() {
202-
203-
204-
//xbox.setRumble(RumbleType.kLeftRumble, 0.5);
205-
//xbox.setRumble(RumbleType.kRightRumble, 0.5);
206-
207-
// reset button
208-
if (leftJoy.getTrigger()) {
209-
leftMaster.getSensorCollection().setIntegratedSensorPosition(0, 10);
210-
rightMaster.getSensorCollection().setIntegratedSensorPosition(0, 10);
211-
}
212-
213-
if ((Math.abs(rightJoy.getY()) > 0.2) || Math.abs(leftJoy.getY()) > 0.2) drive.tankDrive(-rightJoy.getY(), -leftJoy.getY());// deadzones
214-
215-
216208
double[][] shooterCalculations = getLimelightData();
217209
if(shooterCalculations != null) {
218210
SmartDashboard.putNumber("Distance: ",shooterCalculations[0][0]);
@@ -221,14 +213,34 @@ public void teleopPeriodic() {
221213
SmartDashboard.putNumber("Limelight H-Angle: ",shooterCalculations[1][0]);
222214
SmartDashboard.putNumber("Limelight V-Angle: ",shooterCalculations[1][1]);
223215
SmartDashboard.putNumber("Limelight Latency: ",shooterCalculations[1][2]);
216+
SmartDashboard.putNumber("Flywheel RPM: ", shooterFly.getSelectedSensorVelocity()/4 * 2048);
217+
SmartDashboard.putNumber("Current Angle", gyro.getRoll());
224218
}
225-
if(leftJoy.getRawButton(1)) { //center robot on top hub (retro reflector)
219+
220+
// Driver aims to top hub or to balls
221+
if(leftJoy.getTrigger()) { //center robot on top hub (retro reflector)
226222
centerAim("top_hub");
227223
}
228-
if(rightJoy.getRawButton(2)) { //center robot on ball from Raspberry Pi Data
224+
if(rightJoy.getTrigger()) { //center robot on ball from Raspberry Pi Data
229225
centerAim("ball_tracking");
230226
}
231-
227+
228+
// DRIVE CALL
229+
if ((Math.abs(rightJoy.getY()) > 0.2) || Math.abs(leftJoy.getY()) > 0.2) drive.tankDrive(-rightJoy.getY(), -leftJoy.getY());// TODO: adjust deadzones
230+
231+
climbrotation();
232+
climb();
233+
if (controller.getAButton()) fire();
234+
else ready = false;
235+
shooter();
236+
hood();
237+
wrist();
238+
intake();
239+
// Reset function (deprecated)
240+
// if (leftJoy.getTrigger()) {
241+
// leftMaster.getSensorCollection().setIntegratedSensorPosition(0, 10);
242+
// rightMaster.getSensorCollection().setIntegratedSensorPosition(0, 10);
243+
// }
232244
}
233245

234246
public double[][] getLimelightData() {
@@ -294,28 +306,26 @@ public void centerAim(String target) {
294306
}
295307
}
296308

297-
private boolean ballSeen() {
309+
private boolean ballSeen() {// TODO: Sriman/Ayush code this so we know if there's a ball
298310
return true;
299-
} // Sriman/Ayush code this so we know if there's a ball
300-
311+
}
301312

302-
303-
//Shooter Code
304313
public void fire() {
305314
double transferPercent = 0.35;
306315
double[][] limelightData = getLimelightData();
307316
double idealAngle = limelightData[0][1];
308-
double idealVelocity = limelightData[0][2];
309-
double idealRPM = (((idealVelocity*(1/transferPercent))/(Math.PI*0.1016))*60); // flywheel diameter in meters
317+
double idealVelocity = limelightData[0][2]; // flywheel diameter in meters
318+
319+
double rpm = getRPM(idealVelocity, transferPercent);
310320
if (!intakeLimit.get()) {
311321
wrist.set(ControlMode.PercentOutput, 0.75);
312322
}
313-
shooterFly.set(ControlMode.Velocity, 4*idealRPM); //Gear ratio multiplier MIGHT HAVE TO MULTIPLY BY 2048- will test later
314-
if (gyro.getRoll() > idealAngle + 5) { // degrees of freedom +- 5
323+
shooterFly.set(ControlMode.Velocity, 4*rpm); //TODO: Gear ratio multiplier MIGHT HAVE TO MULTIPLY BY 2048- will test later
324+
if ((gyro.getRoll() > idealAngle + 5) && !upperShooterLimit.get()) { // TODO: Adjust degrees of freedom +- 5
315325
hood.set(ControlMode.PercentOutput, 0.1);
316326
ready = false;
317327
}
318-
else if (gyro.getRoll() < idealAngle-5) {
328+
else if ((gyro.getRoll() < idealAngle-5) && !lowerShooterLimit.get()) {
319329
hood.set(ControlMode.PercentOutput, -0.1);
320330
ready = false;
321331
}
@@ -331,41 +341,14 @@ else if (gyro.getRoll() < idealAngle-5) {
331341
// double flywheelMotor = actualRPM/the maximum rpm of the motor; //The motor output in percentage of the flywheel motor
332342
}
333343

334-
public boolean autoFire() {
335-
double transferPercent = 0.35;
336-
double[][] limelightData = getLimelightData();
337-
double idealAngle = limelightData[0][1];
338-
double idealVelocity = limelightData[0][2];
339-
double idealRPM = (((idealVelocity*(1/transferPercent))/(Math.PI*0.1016))*60); // flywheel diameter in meters
340-
if (!intakeLimit.get()) {
341-
wrist.set(ControlMode.PercentOutput, 0.75);
342-
}
343-
shooterFly.set(ControlMode.Velocity, 4*idealRPM); //Gear ratio multiplier MIGHT HAVE TO MULTIPLY BY 2048- will test later
344-
if (gyro.getRoll() > idealAngle + 5) { // degrees of freedom +- 5
345-
hood.set(ControlMode.PercentOutput, 0.1);
346-
ready = false;
347-
}
348-
else if (gyro.getRoll() < idealAngle-5) {
349-
hood.set(ControlMode.PercentOutput, -0.1);
350-
ready = false;
351-
}
352-
else {
353-
ready = true;
354-
}
355-
if (ready) {
356-
rollers.set(ControlMode.PercentOutput, 1);
357-
}
358-
return false;
359-
//adjust to fine tuning
360-
// double OneRPM = (60*idealVelocity)/(8*Math.PI); //RPM required if 100% of speed from flywheel is transferred to the ball
361-
// double actualRPM = 10*OneRPM; //RPM needed bearing 10% of velocity is transferred from flywheel to ball
362-
// double flywheelMotor = actualRPM/the maximum rpm of the motor; //The motor output in percentage of the flywheel motor
344+
//TODO: Determines the RPM to set flywheel based on what RPM corresponds to what ball velocity
345+
public double getRPM(double idealVelocity, double transferPercent) {
346+
return (((idealVelocity*(1/transferPercent))/(Math.PI*0.1016))*60); // rudimentary calculation that's 70% wrong
363347
}
364348

365-
366349
//Spinny part of intake, no encoder
367350
public void intake() {
368-
if(Math.abs(controller.getRightY()) > 0.2) { //Right trigger spins intake from field to robot
351+
if (Math.abs(controller.getRightY()) > 0.2) { //Right trigger spins intake from field to robot
369352
rollers.set(ControlMode.PercentOutput, controller.getRightTriggerAxis());
370353
}
371354
else if(Math.abs(controller.getRightY()) < -0.2 ) { //Left trigger spins intake from entrance of robot to flywheel
@@ -376,6 +359,19 @@ else if(Math.abs(controller.getRightY()) < -0.2 ) { //Left trigger spins intake
376359
}
377360
}
378361

362+
// Manual hood actuation
363+
public void hood() {
364+
if (controller.getXButton()) {
365+
hood.set(ControlMode.PercentOutput, 0.1);
366+
}
367+
else if (controller.getYButton()) {
368+
hood.set(ControlMode.PercentOutput, -0.1);
369+
}
370+
else {
371+
hood.set(ControlMode.PercentOutput, 0);
372+
}
373+
}
374+
379375
//Wrist for intake, no encoder (for teleop)
380376
public void wrist() {
381377
if (controller.getBButton()) { //sets intake in position to feed ball into flywheel

0 commit comments

Comments
 (0)