Skip to content

Commit 479eaf7

Browse files
caengineerscaengineers
authored andcommitted
Create Drive command (combines limelight drive and teleop drive)
1 parent 98c1bba commit 479eaf7

4 files changed

Lines changed: 73 additions & 88 deletions

File tree

Robot2019/src/main/java/frc/robot/OI.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import frc.robot.commands.ToggleClimberRails;
2727
import frc.robot.commands.ToggleHatchEject;
2828
import frc.robot.commands.ToggleLight;
29+
import frc.robot.commands.ToggleLimelight;
2930
import frc.robot.commands.WobbleDrive;
3031
import frc.robot.subsystems.Cargo;
3132
import frc.robot.subsystems.Climber;
@@ -44,6 +45,7 @@ public class OI {
4445
JoystickButton arcadeOrTankBtn;
4546
JoystickButton normDriveBtn;
4647
JoystickButton gradDriveBtn;
48+
JoystickButton limelightBtn;
4749
JoystickButton hatchIntakeBtn, hatchEjectBtn;
4850
JoystickButton cargoIntakeBtn, cargoEjectBtn;
4951
JoystickButton climberRailBtn;
@@ -74,6 +76,8 @@ public class OI {
7476
normDriveBtn.whenPressed(new NormalDrive());
7577
gradDriveBtn = new JoystickButton(leftJoy, 5);
7678
gradDriveBtn.whenPressed(new GradualDrive());
79+
limelightBtn = new JoystickButton(rightJoy, 2); // Figure out the correct button
80+
limelightBtn.whenPressed(new ToggleLimelight());
7781

7882
hatchIntakeBtn = new JoystickButton(manipulator, Manip.X);
7983
hatchIntakeBtn.whenPressed(new IntakeHatch(hp, dt));
@@ -94,7 +98,6 @@ public class OI {
9498
manualClimbBtn = new JoystickButton(manipulator, Manip.RT_rTrigger);
9599
manualClimbBtn.toggleWhenPressed(new ManualClimb(climber, manipulator, lights));
96100

97-
98101
slowClimbBtn = new JoystickButton(manipulator, Manip.LB_lShoulder);
99102
slowClimbBtn.whileHeld(new SlowClimb());
100103

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
import frc.robot.commands.DrivetrainAnalysis;
1414
import frc.robot.commands.IncreaseVoltageLinear;
1515
import frc.robot.commands.IncreaseVoltageStepwise;
16-
import frc.robot.commands.TeleopDrive;
16+
import frc.robot.commands.Drive;
17+
import frc.robot.lib.Limelight;
1718
import frc.robot.subsystems.Cargo;
1819
import frc.robot.subsystems.Climber;
1920
import frc.robot.subsystems.Drivetrain;
@@ -25,6 +26,7 @@ public class Robot extends TimedRobot {
2526
private static Drivetrain dt;
2627
private static HatchPanel hp;
2728
private static OI oi;
29+
private static Limelight lime;
2830
private static Cargo cargo;
2931
private static Climber climber;
3032
private static Lights lights;
@@ -40,6 +42,7 @@ public void robotInit() {
4042
climber = new Climber(RobotMap.climberMotor, RobotMap.climberEncoder, RobotMap.ahrs, RobotMap.climberPistons);
4143
lights = new Lights(RobotMap.lights);
4244
oi = new OI(dt, hp, cargo, climber, lights, RobotMap.driveCamera, RobotMap.hatchCamera, RobotMap.cameraServer);
45+
lime = new Limelight();
4346

4447
fname1 = "/home/lvuser/drive_char_linear_for.csv";
4548
fname2 = "/home/lvuser/drive_char_stepwise_for.csv";
@@ -56,7 +59,7 @@ public void robotInit() {
5659
SmartDashboard.putData("Increase Voltage Stepwise Backward", ivsb);
5760
SmartDashboard.putData("Drivetrain Characterization Analysis", dca);
5861

59-
dt.setDefaultCommand(new TeleopDrive(dt, oi.leftJoy, oi.rightJoy));
62+
dt.setDefaultCommand(new Drive(dt, lime, oi.leftJoy, oi.rightJoy));
6063
SmartDashboard.putNumber("Max Acceleration", dt.getMaxSpeed() / 1.0);
6164

6265
SmartDashboard.putBoolean("Outreach Mode", false);

Robot2019/src/main/java/frc/robot/commands/AllignAssist.java

Lines changed: 0 additions & 63 deletions
This file was deleted.

Robot2019/src/main/java/frc/robot/commands/TeleopDrive.java renamed to Robot2019/src/main/java/frc/robot/commands/Drive.java

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
/*----------------------------------------------------------------------------*/
2-
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
2+
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
33
/* Open Source Software - may be modified and shared by FRC teams. The code */
44
/* must be accompanied by the FIRST BSD license file in the root directory of */
55
/* the project. */
66
/*----------------------------------------------------------------------------*/
77

88
package frc.robot.commands;
99

10+
import frc.robot.lib.Limelight;
11+
import frc.robot.subsystems.Drivetrain;
1012
import edu.wpi.first.wpilibj.Joystick;
1113
import edu.wpi.first.wpilibj.command.Command;
1214
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
13-
import frc.robot.subsystems.Drivetrain;
14-
15-
public class TeleopDrive extends Command {
16-
Drivetrain dt;
17-
Joystick leftJoy, rightJoy;
18-
19-
double prevSpeed = 0, prevLeft = 0, prevRight = 0;
2015

21-
double outreachSpeed = 0.3;
22-
23-
/**
24-
* Handles all the teleoperated driving functionality
25-
*
26-
* @param dt the Drivetrain object to use, passing it in is useful for testing
27-
* purposes
28-
*/
29-
public TeleopDrive(Drivetrain dt, Joystick leftJoy, Joystick rightJoy) {
16+
public class Drive extends Command {
17+
private Drivetrain dt;
18+
private Joystick leftJoy, rightJoy;
19+
private Limelight lime;
20+
private Limelight.Mode limelightMode = Limelight.Mode.TARGET;
21+
private double adjustment = 0;
22+
private double minError = 0.05; // TODO: Test minimum values
23+
private double prevSpeed = 0, prevLeft = 0, prevRight = 0;
24+
private double outreachSpeed = 0.3;
25+
26+
public Drive(Drivetrain dt, Limelight lime, Joystick leftJoy, Joystick rightJoy) {
3027
requires(dt);
3128
this.dt = dt;
29+
this.lime = lime;
30+
this.dt = dt;
3231
this.leftJoy = leftJoy;
3332
this.rightJoy = rightJoy;
3433

@@ -45,12 +44,22 @@ public TeleopDrive(Drivetrain dt, Joystick leftJoy, Joystick rightJoy) {
4544
}
4645
}
4746

47+
// Called just before this Command runs the first time
48+
@Override
49+
protected void initialize() {
50+
}
51+
52+
// Called repeatedly when this Command is scheduled to run
4853
@Override
4954
protected void execute() {
50-
if (SmartDashboard.getBoolean("Arcade Drive", true)) {
51-
arcadeDrive();
55+
if (SmartDashboard.getBoolean("Using Limelight", false)) {
56+
autoAllign();
5257
} else {
53-
tankDrive();
58+
if (SmartDashboard.getBoolean("Arcade Drive", true)) {
59+
arcadeDrive();
60+
} else {
61+
tankDrive();
62+
}
5463
}
5564
}
5665

@@ -230,16 +239,49 @@ private void charDrive(double left, double right) {
230239
dt.disableVoltageCompensation();
231240
}
232241

242+
private void autoAllign() {
243+
if (limelightMode == Limelight.Mode.DIST) {
244+
adjustment = lime.distanceAssist();
245+
dt.drive(adjustment, adjustment);
246+
if (Math.abs(adjustment) < minError) {
247+
SmartDashboard.putBoolean("Finished Alligning", true);
248+
}
249+
}
250+
else if (limelightMode == Limelight.Mode.STEER) {
251+
adjustment = lime.distanceAssist();
252+
dt.drive(adjustment, -adjustment);
253+
if (Math.abs(adjustment) < minError) {
254+
SmartDashboard.putBoolean("Finished Alligning", true);
255+
}
256+
} else {
257+
double[] params = lime.autoTarget();
258+
dt.drive(params[0], params[1]);
259+
double maxInput = Math.max(Math.abs(params[0]), Math.abs(params[1]));
260+
if (maxInput < minError) {
261+
SmartDashboard.putBoolean("Finished Alligning", true);
262+
}
263+
}
264+
}
265+
266+
// Make this return true when this Command no longer needs to run execute()
233267
@Override
234268
protected boolean isFinished() {
235-
return false;
269+
if (!SmartDashboard.getBoolean("Use Limelight", false) || SmartDashboard.getBoolean("Finished Alligning", false)) {
270+
SmartDashboard.putBoolean("Use Limelight", false);
271+
SmartDashboard.putBoolean("Finished Alligning", false);
272+
return true;
273+
} else {
274+
return false;
275+
}
236276
}
237277

278+
// Called once after isFinished returns true
238279
@Override
239280
protected void end() {
240-
dt.stop();
241281
}
242282

283+
// Called when another command which requires one or more of the same
284+
// subsystems is scheduled to run
243285
@Override
244286
protected void interrupted() {
245287
end();

0 commit comments

Comments
 (0)