Toggle Slow Mode- Command Robot

We are working on our code for FRC and we came to a problem. We want to make a slow mode for our robot so that if you click a button then it will turn it on (We were going to use a boolean) but we can’t figure out how to get an “If button pressed, then slowMode = true” to work. Is anyone able to help?

// Joystick controller;
// boolean slowMode;
if (controller.getRawButtonPressed(1))
    slowMode = !slowMode;
if (slowMode) {
    // Code to drive slow
} else {
    // Code to break the sound barrier
}

Based on TimedRobot, not Command based

We are using command robot, do you know how to do it in that instead?

You could write a short “instant command” that inverts the value of a boolean when run. Or you can use a lambda expression in place of a command.

How would I do that? I’m pretty new to java

myButton.whenPressed(() -> DriveSubsystem.isReverse = !DriveSubsystem.isReverse)

In your RobotContainer, I believe you put code like this in your configureButtonBindings():

new JoystickButton(...).whenPressed(new InstantCommand(() -> slowMode = !slowMode));

Here’s an example of a RobotContainer class. Does something completely different than what you’re doing, but it’s a good piece of code to look at: https://github.com/wpilibsuite/allwpilib/blob/master/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/gyrodrivecommands/RobotContainer.java

Ok, thanks. I will try it

The problem here is that getRawButtonPressed() will continue to return true as long as the button is pressed, toggling the mode an essentially arbitrary number of times. In timed, you’ll need some sort of lockout so that it won’t toggle again for a few tenths of a second, and preferably not until a release/re-press cycle.

2 Likes

Actually, getRawButtonPressed() is the correct method here. However, if you were to call getRawButton(), it would instead have the behavior you described.

3 Likes

maxOutput method

Came here to say this. If you’re using a DifferentialDrive object, then definitely make an InstantCommand that calls a method on your subsystem, then bind that command to the button you want. Then in your subsystem method, you just add something like m_differentialDrive.setMaxOutput(kSlowSpeed)

With Command-Based, what I would do is create two commands, one is the regular full-speed drive, and the other is the slow mode. Lets call these the JoystickDrive and SlowJoystickDrive commands respectively. The difference between these can be done however you want. Personally, I’d just multiply my joystick inputs by some scaling factor 0.8, 0.5, or whatever in the Slow command. Both should “never end” meaning isFinished() always returns false.

Of course, we want the JoystickDrive to be the default command for our subsystem, so we set it as such in our RobotContainer: drivetrain.setDefaultCommand(new JoystickDrive(drivetrain, joystickXLambda, joystickYLambda));

Then we can setup a button to toggle our SlowJoystickDrive Command. slowModeButton.toggleWhenPressed(new SlowJoystickDrive(drivetrain, joystickXLambda, joystickYLambda));.

With this, when you press the button, the SlowJoystickDrive Command will interrupt the JoystickDrive command, and continue to run until you press the button again, at which point the SlowJoystickDrive command will be interrupted, allowing the default JoystickDrive command to take back over.

1 Like

What do I replace controller with. I put XboxController and this is what it said “Cannot make a static reference to the non-static method getRawButton(int) from the type GenericHID)”

This means that you are trying to do XboxController.getRawButton(...). This is not a valid way to use the XboxController class. You have to create a new XboxController like this:

XboxController controller = new XboxController(0);
public void teleopPeriodic(){
    double value = controller.getRawButton(0);
}

The above code is using an iterative approach rather than a command approach.

1 Like

Thanks, there are no errors so I will try it at the next practice

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.