Skip to content

Commit 48dc9f0

Browse files
add drive mode changing for testing purposes
1 parent 64154ad commit 48dc9f0

File tree

4 files changed

+122
-2
lines changed

4 files changed

+122
-2
lines changed

src/main/kotlin/frc/robot/Constants.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ class Constants {
120120
val kballHeightForAutoIntake = 0.33 // a third of the screen
121121
val kBallAreaThreshold = 0.1
122122

123+
val kDriveToggleHoldTime = 5 // seconds to hold button to change drive mode
124+
val kDriveModeChangeRumbleTime = 1
125+
val kDriveModeChangingOn = true // turn this off to prevent changing
126+
123127
val constants = mutableMapOf(
124128
"DrivetrainPID_P" to 0.035,
125129
"DrivetrainPID_I" to 0.0,

src/main/kotlin/frc/robot/RobotContainer.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ class RobotContainer {
7777
/*** --- commands --- ***/
7878
//drive by a joystick (controller1)
7979
val XboxDriveCommand = XboxDrive(drivetrain, controller1)
80-
val XboxDriveSpecial = XboxDrive(drivetrain, controller1)
80+
val XboxDriveSpecial = XboxDriveSpecial(drivetrain, controller1)
81+
val driveModeToggle = DriveModeToggleSubsystem(drivetrain, controller1,
82+
listOf(XboxDriveCommand, XboxDriveSpecial), 0
83+
)
8184

8285
/** -- 0 point autos -- **/
8386
val noAuto = DriveDoubleSupplier(drivetrain, { 0.0 }, { 0.0 })
@@ -227,7 +230,11 @@ class RobotContainer {
227230
/* setup default commands */
228231
drivetrain.defaultCommand = XboxDriveCommand
229232
//drivetrain.defaultCommand = XboxDriveSpecial
230-
233+
driveModeToggle.defaultCommand = ChangeDriveMode(driveModeToggle) {
234+
Constants.kDriveModeChangingOn && controller1.aButton
235+
}
236+
237+
231238
/* default gate - run forward on X, backwards on A
232239
* If left bumper held, run until a ball is seen by the sensor
233240
*/
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*----------------------------------------------------------------------------*/
2+
/* Copyright (c) 2018-2019 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.wpilibj2.command.CommandBase
11+
import frc.robot.Constants
12+
import frc.robot.subsystems.*
13+
import java.time.LocalTime
14+
15+
class ChangeDriveMode(val driveToggle: DriveModeToggleSubsystem,
16+
val activating: () -> Boolean) : CommandBase() {
17+
/**
18+
* Creates a new ExampleCommand.
19+
*
20+
* @param m_subsystem The subsystem used by this command.
21+
*/
22+
init {
23+
addRequirements(driveToggle)
24+
}
25+
26+
// Called when the command is initially scheduled.
27+
override fun initialize() {
28+
}
29+
30+
// Called every time the scheduler runs while the command is scheduled.
31+
override fun execute() {
32+
val now = LocalTime.now()
33+
34+
// handle input
35+
if (activating()) {
36+
if (driveToggle.holdStartTime == null) {
37+
driveToggle.holdStartTime = now
38+
} else if (now.minusSeconds(Constants.kDriveToggleHoldTime.toLong()).isAfter(driveToggle.holdStartTime)) {
39+
driveToggle.incrementMode()
40+
driveToggle.holdStartTime = null
41+
}
42+
} else driveToggle.holdStartTime = null
43+
44+
// update the drive mode
45+
driveToggle.applyDriveMode()
46+
47+
// stop rumble if time is up
48+
if (driveToggle.rumbleStartTime != null
49+
&& now.minusSeconds(Constants.kDriveModeChangeRumbleTime.toLong())
50+
.isAfter(driveToggle.rumbleStartTime)) {
51+
driveToggle.stopRumble()
52+
}
53+
}
54+
55+
// Called once the command ends or is interrupted.
56+
override fun end(interrupted: Boolean) {
57+
}
58+
59+
// Returns true when the command should end.
60+
override fun isFinished(): Boolean {
61+
return false
62+
}
63+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package frc.robot.subsystems
2+
3+
import edu.wpi.first.wpilibj.GenericHID
4+
import edu.wpi.first.wpilibj.XboxController
5+
import edu.wpi.first.wpilibj2.command.CommandBase
6+
import java.time.LocalTime
7+
import edu.wpi.first.wpilibj2.command.SubsystemBase
8+
import frc.robot.Constants
9+
import java.lang.IndexOutOfBoundsException
10+
import java.util.*
11+
import kotlin.concurrent.schedule
12+
13+
class DriveModeToggleSubsystem(private val drivetrain: DrivetrainSubsystem,
14+
private val controller1: XboxController,
15+
private val driveModes: List<CommandBase>,
16+
var currentMode: Int) : SubsystemBase() {
17+
18+
var holdStartTime: LocalTime? = null
19+
var rumbleStartTime: LocalTime? = null
20+
21+
override fun periodic() {
22+
}
23+
24+
fun incrementMode() {
25+
currentMode = (currentMode + 1) % driveModes.size
26+
27+
// rumble the controller
28+
controller1.setRumble(GenericHID.RumbleType.kLeftRumble, 1.0)
29+
controller1.setRumble(GenericHID.RumbleType.kRightRumble, 1.0)
30+
rumbleStartTime = LocalTime.now()
31+
}
32+
33+
fun stopRumble() {
34+
rumbleStartTime = null
35+
controller1.setRumble(GenericHID.RumbleType.kLeftRumble, 0.0)
36+
controller1.setRumble(GenericHID.RumbleType.kRightRumble, 0.0)
37+
}
38+
39+
fun applyDriveMode() {
40+
drivetrain.defaultCommand = try {
41+
driveModes[currentMode]
42+
} catch (e: IndexOutOfBoundsException) { // this shouldn't happen, but just in case
43+
driveModes[0]
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)