Skip to content

Commit 12b5884

Browse files
add vision mode toggling
1 parent 922b05a commit 12b5884

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import frc.robot.triggers.EndgameTrigger
3232
* periodic methods (other than the scheduler calls). Instead, the structure of the robot
3333
* (including subsystems, commands, and button mappings) should be declared here.
3434
*/
35+
36+
3537
class RobotContainer {
3638
val sch = CommandScheduler.getInstance()
3739
var m_autoCommandChooser: SendableChooser<Command> = SendableChooser()
@@ -69,6 +71,8 @@ class RobotContainer {
6971
val gate = GateSubsystem(WPI_TalonSRX(Constants.kGatePort), ColorSensorV3(I2C.Port.kOnboard))
7072
val lights = LEDSubsystem(AddressableLED(Constants.kLED0Port), 60, DriverStation.getInstance())
7173

74+
val visionToggle = VisionToggleSubsystem()
75+
7276
val climber = ClimbSubsystem(WPI_TalonSRX(Constants.kClimberPort))
7377
val winch = WinchSubsystem(WPI_TalonSRX(Constants.kWinchPort))
7478
/*** --- commands --- ***/
@@ -130,6 +134,20 @@ class RobotContainer {
130134
)
131135
}
132136

137+
val visionBallLineup = {
138+
SequentialCommandGroup(
139+
BallVision(drivetrain, 0.0),
140+
DriveDoubleSupplier(drivetrain, { 0.3 }, { 0.0 }).withTimeout(0.5)
141+
)
142+
}
143+
144+
val visionLineup: () -> SequentialCommandGroup = {
145+
when (visionToggle.visionMode) {
146+
VisionModes.HIGHGOAL -> visionHighGoalLineUp()
147+
VisionModes.BALL -> visionBallLineup()
148+
}
149+
}
150+
133151
val forwardShootAutoNoIntake = SequentialCommandGroup(
134152
ParallelCommandGroup(
135153
SequentialCommandGroup(
@@ -181,7 +199,7 @@ class RobotContainer {
181199
)
182200

183201
JoystickButton(controller1, kBumperLeft.value).whenHeld(
184-
visionHighGoalLineUp()
202+
visionLineup()
185203
)
186204

187205
/* Climbing, including winch */
@@ -242,6 +260,18 @@ class RobotContainer {
242260
})
243261
lights.defaultCommand = setAlliance
244262

263+
/* toggle vision mode when start is pressed on controller0 */
264+
visionToggle.defaultCommand = VisionModeChange(visionToggle, {
265+
if (controller0.getStartButtonPressed()) {
266+
when (visionToggle.visionMode) {
267+
VisionModes.BALL -> VisionModes.HIGHGOAL
268+
VisionModes.HIGHGOAL -> VisionModes.BALL
269+
}
270+
} else {
271+
visionToggle.visionMode
272+
}
273+
})
274+
245275
/* set options for autonomous */
246276
m_autoCommandChooser.setDefaultOption("Power Port Vision Autonomous", visionAuto())
247277
m_autoCommandChooser.addOption("Backup 2s Autonomous", backupAuto)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.subsystems.ExampleSubsystem
12+
import frc.robot.subsystems.VisionModes
13+
import frc.robot.subsystems.VisionToggleSubsystem
14+
15+
class VisionModeChange(val visionToggle: VisionToggleSubsystem,
16+
val mode: () -> VisionModes) : CommandBase() {
17+
/**
18+
* Creates a new ExampleCommand.
19+
*
20+
* @param m_subsystem The subsystem used by this command.
21+
*/
22+
init {
23+
addRequirements(visionToggle)
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+
visionToggle.visionMode = mode()
33+
}
34+
35+
// Called once the command ends or is interrupted.
36+
override fun end(interrupted: Boolean) {
37+
}
38+
39+
// Returns true when the command should end.
40+
override fun isFinished(): Boolean {
41+
return false
42+
}
43+
}

src/main/kotlin/frc/robot/subsystems/GateSubsystem.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import kotlin.math.abs
1717
class GateSubsystem(val motor: SpeedController, val colorSensor: ColorSensorV3) : SubsystemBase() {
1818

1919
override fun periodic() {
20-
debugMeasurment()
20+
debugMeasurement()
2121
}
2222

2323
/* set a motor speed, no matter if a ball is in the gate or not */
@@ -46,7 +46,7 @@ class GateSubsystem(val motor: SpeedController, val colorSensor: ColorSensorV3)
4646
}
4747

4848
/* print status of sensor + its reading */
49-
fun debugMeasurment() {
49+
fun debugMeasurement() {
5050
SmartDashboard.putBoolean("Gate Ball Sensor Status", colorSensor.getIR() != 0)
5151
SmartDashboard.putBoolean("Ball Present in Gate", isBallTriggered())
5252
SmartDashboard.putNumber("Gate Ball Sensor Reading", colorSensor.getIR().toDouble())
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package frc.robot.subsystems
2+
3+
import edu.wpi.first.wpilibj2.command.SubsystemBase
4+
5+
enum class VisionModes { BALL, HIGHGOAL }
6+
7+
class VisionToggleSubsystem() : SubsystemBase() {
8+
var visionMode = VisionModes.BALL
9+
10+
override fun periodic() {
11+
}
12+
}

0 commit comments

Comments
 (0)