Skip to content

Commit d5b7abd

Browse files
kevinzwangbrettle
authored andcommitted
Gradual drive (#103)
* add gradual drive * change characterization to a toggle * confirmed gradual drive button
1 parent 2c1a5d9 commit d5b7abd

4 files changed

Lines changed: 78 additions & 16 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import frc.robot.commands.ToggleClimberRails;
1515
import frc.robot.commands.Climb;
1616
import frc.robot.commands.EjectCargo;
17+
import frc.robot.commands.GradualDrive;
1718
import frc.robot.commands.ToggleHatchEject;
1819
import frc.robot.commands.IntakeCargo;
1920
import frc.robot.commands.ToggleHatchIntake;
@@ -41,6 +42,7 @@ public class OI {
4142
JoystickButton leftSlowBtn, rightSlowBtn;
4243
JoystickButton arcadeOrTankBtn;
4344
JoystickButton normDriveBtn;
45+
JoystickButton gradDriveBtn;
4446
JoystickButton hatchIntakeBtn, hatchEjectBtn;
4547
JoystickButton cargoIntakeBtn, cargoEjectBtn;
4648
JoystickButton climberRailBtn;
@@ -67,7 +69,9 @@ public class OI {
6769
arcadeOrTankBtn = new JoystickButton(leftJoy, 4);
6870
arcadeOrTankBtn.whenPressed(new SetArcadeOrTank());
6971
normDriveBtn = new JoystickButton(leftJoy, 3);
70-
normDriveBtn.whileHeld(new NormalDrive());
72+
normDriveBtn.whenPressed(new NormalDrive());
73+
gradDriveBtn = new JoystickButton(leftJoy, 5);
74+
gradDriveBtn.whenPressed(new GradualDrive());
7175

7276
hatchIntakeBtn = new JoystickButton(manipulator, Manip.X);
7377
hatchIntakeBtn.whenPressed(new ToggleHatchIntake(hp));
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*----------------------------------------------------------------------------*/
2+
/* Copyright (c) 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+
/*----------------------------------------------------------------------------*/
7+
8+
package frc.robot.commands;
9+
10+
import edu.wpi.first.wpilibj.command.InstantCommand;
11+
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
12+
13+
/**
14+
* Add your docs here.
15+
*/
16+
public class GradualDrive extends InstantCommand {
17+
/**
18+
* Add your docs here.
19+
*/
20+
public GradualDrive() {
21+
super();
22+
// Use requires() here to declare subsystem dependencies
23+
// eg. requires(chassis);
24+
}
25+
26+
// Called once when the command executes
27+
@Override
28+
protected void initialize() {
29+
SmartDashboard.putBoolean("Gradual Drive", !SmartDashboard.getBoolean("Gradual Drive", false));
30+
}
31+
32+
}

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@
77

88
package frc.robot.commands;
99

10-
import edu.wpi.first.wpilibj.command.Command;
10+
import edu.wpi.first.wpilibj.command.InstantCommand;
1111
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
1212

13-
public class NormalDrive extends Command {
14-
protected void initialize() {
15-
SmartDashboard.putBoolean("Characterized Drive", false);
16-
}
17-
18-
@Override
19-
protected boolean isFinished() {
20-
return false;
13+
/**
14+
* Add your docs here.
15+
*/
16+
public class NormalDrive extends InstantCommand {
17+
/**
18+
* Add your docs here.
19+
*/
20+
public NormalDrive() {
21+
super();
22+
// Use requires() here to declare subsystem dependencies
23+
// eg. requires(chassis);
2124
}
2225

26+
// Called once when the command executes
2327
@Override
24-
protected void end() {
25-
SmartDashboard.putBoolean("Characterized Drive", true);
28+
protected void initialize() {
29+
SmartDashboard.putBoolean("Characterized Drive", !SmartDashboard.getBoolean("Characterized Drive", false));
2630
}
2731

28-
@Override
29-
protected void interrupted() {
30-
end();
31-
}
3232
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public class TeleopDrive extends Command {
1616
Drivetrain dt;
1717
Joystick leftJoy, rightJoy;
1818

19+
double prevSpeed = 0, prevLeft = 0, prevRight = 0;
20+
1921
/**
2022
* Handles all the teleoperated driving functionality
2123
*
@@ -27,6 +29,10 @@ public TeleopDrive(Drivetrain dt, Joystick leftJoy, Joystick rightJoy) {
2729
this.dt = dt;
2830
this.leftJoy = leftJoy;
2931
this.rightJoy = rightJoy;
32+
33+
if (!SmartDashboard.containsKey("Gradual Drive Max dV")) {
34+
SmartDashboard.putNumber("Gradual Drive Max dV", 0.5); // between zero (no movement) to 2 (any movement)
35+
}
3036
}
3137

3238
@Override
@@ -56,6 +62,14 @@ private void arcadeDrive() {
5662
rot *= SmartDashboard.getNumber("Rotation Slow Ratio", 0.35);
5763
}
5864

65+
if (SmartDashboard.getBoolean("Gradual Drive", false)) {
66+
double dV = SmartDashboard.getNumber("Gradual Drive Max dV", 0.5);
67+
if (Math.abs(speed - prevSpeed) > dV) {
68+
speed = prevSpeed + dV * Math.signum(speed - prevSpeed);
69+
}
70+
}
71+
prevSpeed = speed;
72+
5973
double left, right;
6074

6175
// double maxInput = Math.copySign(Math.max(Math.abs(speed), Math.abs(rot)),
@@ -107,6 +121,18 @@ private void tankDrive() {
107121
right *= SmartDashboard.getNumber("Speed Slow Ratio", 0.5);
108122
}
109123

124+
if (SmartDashboard.getBoolean("Gradual Drive", false)) {
125+
double dV = SmartDashboard.getNumber("Gradual Drive Max dV", 0.5);
126+
if (Math.abs(left - prevLeft) > dV) {
127+
left = prevLeft + dV * Math.signum(left - prevLeft);
128+
}
129+
if (Math.abs(right - prevRight) > dV) {
130+
right = prevRight + dV * Math.signum(right - prevRight);
131+
}
132+
}
133+
prevLeft = left;
134+
prevRight = right;
135+
110136
if (SmartDashboard.getBoolean("Characterized Drive", true)) {
111137
charDrive(left, right);
112138
} else {

0 commit comments

Comments
 (0)