How to Make a Button That Toggles Break/Coast Mode

I think I saw this in a 2910 video, but how do you make a button that can switch falcons from break to coast mode? Thanks!

What programing language do you use?

Java

Write a method in the disabled periodic iirc.

Are you using command based? If so, you can bind a button to a command that sets the motor to the mode you want. Make sure you call the commands that allows it to run on disable tho. By default commands don’t run when disabled.

as for buttons on the robot, you just make them as DigitalInputs in the code, and plug buttons into the rio

You can assign a button on your controller to flip from brake to coast. All you need to do is have a if(controller.getAButton() == true){
motorConfig.MotorOutput.NeutralMode = NeutralModeValue.Coast;
}
You would put this in periotic.

You could also add a Boolean flag system to make it switch back to brake mode.

You should use talonfx.setNeutralMode(mode);
I would do something like this actually.

commandXboxController.a().onTrue(new InstantCommand(() -> talonfx.setNeutralMode(NeutralModeValue.Coast));

This should be called in the code once.
I would also add a trigger for enable and set the neutral mode to on.
If you’re using a spark you can use setIdleMode

Commands.runOnce() is more concise.

I never understood why people prefer that. What’s so bad with creating the class directly?

Less code == more fun (especially if you drop the Commands. part and import the factory directly). I also think that in some cases the factory’s name is clearer than the class.

1 Like

I wouldn’t necessarily agree with the statement that less code is better. IMO, most of the times adding more code (i.e. extracting into more functions) is better. Though less relevant in this case since it’s just creating a command in one line.
About the static import part. I don’t really like static imports. I think they just create more confusion in code. If you have static imports you can’t know from where you’re using functions and that’s pretty bad. When you see a staticly imported function in code that doesn’t reference it’s class, you’d assume it’s a function inside the class it’s referenced in, though it is not.
I think InstantCommand’s name is pretty understandable, it runs something instantly. But I suppose creating an InstantCommand through the Commands class could be somewhat more readable. Thanks!

With that, I agree. My statement was somewhat concise (:wink:). I meant that if I can write the same code shorter without hindering readability I usually go for it.

Most of the time yes. But in that case, the factories’ names are pretty unique so I think it’s not very confusing. Note that with new InstantCommand it still might not be obvious where this class is coming from.

When not using a star (*) static import, you can just head to the top of the file and find where a class is imported from. If it’s not there, then it’s in the same package.
When using the star static import, you lose that ability.
Still, if you decide on some key static imports to be used throughout the project I think that’s fine, as long as you make sure anyone working on the code is aware of their existence, and what methods they include, so they could know what to look for.

Yes, consistency is key. I’m against star imports too but to find where a class/function comes from I almost always use the IDE’s “Jump to decleration” feature instead of searching for it in the import lines.

image

Remember, you may sometimes wish to BRAKE your robot, but you never want to BREAK your robot.

(Except in practice.)

2 Likes

If I asked my wife (she’s not a computer programmer) what a new InstantCommand() does she’d mostly have a blank look then make up something not very descriptive of what was trying to be accomplished. If I asked her what runOnce() does she would tell me exactly the right answer.

Just because all Java, C++, and Python programmers had to learn early on what new ClassName() is and none of them learned what runOnce() does, doesn’t mean new ClassName() is the easiest way to accomplish robot actions even for Java, C++, and Python programmers.

A little more training on the use of Command-Based goes a long way toward making better code. It’s worth a few hours of struggle to understand it’s usage.

1 Like

You generally don’t want to set configurations periodically, which is why the Trigger approach with a runOnce command is cleaner.

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