Help with Java programming motor control

Hello, I am new to Java FRC programming this year. Looking for a little help on the basics of command based programming. I’ve been trying to turn on a motor at the touch of a button when pressed. Currently using an Xbox Controller to control a Talon motor controller to a CIM. I would like to know the basics of where to declare things like the existence of the controller, the Xbox Controller itself, the certain button I want to use, and how to make the command. The stuff on the FRC website is okay, but too difficult to follow most of the time for me. Just wanting to know what other teams do and how I can format things easier.

I recommend that you first, create a subsystem file where you create a motor controller object and write a couple of functions to run or stop the motor. Then, you have to create an instance of that subsystem in RobotContainer.java. Also, the Xbox controller should be declared here so that the button bindings can be declared inside the configureButtonBindings function. The “Button” class is a way to create a button and then write your command as an “InstantCommand” parameter, besides writing the button number you want to assign. You may want to see the methods for a Button object here.

Further information about Command-based Programming can be found here.

So I’ve made a subsystem file for my motor and declared it there. How can I make functions for it? I would assume it needs input from the Xbox controller, so I declared the controller in the RobotContainer.java. Now, how do you get the value from the Xbox Controller and, using that value, tell the Talon to move?

Welcome to FRC. If you haven’t I’d recommend you create a Command Based robot using the “Command Based” project template in either VS Code (docs) or in IntelliJ IDEA with the FRC plugin installed via File > New > Project > FRC. (Disclaimer: I write//maintain the unofficial FRC IntelliJ IDEA plugin.) The project created form that template has an ExampleSubsystem, along with the wiring up of commands to buttons in the RobotContainer class. Also take a look at the RapidReactCommandBot example template. It is a full robot using Command based programming. Keep them open in a second instance of the IDE for reference. I hope that helps.

3 Likes

I would recommend reviewing the help page on commands as well as the help page on binding commands to buttons.

In general, the WPILib docs have lots of useful information about CommandBased, as well as all other things robot programming. Would recommend starting your search there if you have any questions.

Zero to Robot is a great place to start if your a new team and want to get your robot up and running.

1 Like

I feel like I am super close to the answer. After reviewing the RapidReactCommandBot code, I found lots of helpful info. I ended up being able to put all of the pieces of code together except for how to execute setting the motor speed to 1. Here is the code:

public CommandBase MoveMotorCommand() {
set(arm_motor.set(speed: 1));
}
I made this in the subsystem for that motor. The problem with this code is that set (the first one, italicized) isn’t the right method, I just can’t find the right word to use to activate the arm_motor to 1. Anyone know the right word?

Can you send the entire file?

Happy to do that. Thanks for the help. How can I do that the easiest?

A screenshot will do as long as it’s clear

I will re write it and add comments

You have to return a command that calls arm_motor.set from your MoveMotorCommand method. You can build a command to do that with the run or runOnce factories. You’ll have to use a lambda expression to call those - that’s just a funny way of writing a function inline when you’re passing it to another function. The example robot code does this - it’s the bit that looks like () -> { }.

1 Like

https://store.ctr-electronics.com/software/

Did this solve your question?

This compiles, but i do not think it does what the OP wants - it doesn’t return a command.


These two screenshots are screenshots of the code I modified to make the motor run. What ended up happening is when i pressed the button on the XboxController, it started. Then, when I pressed it again, it stopped. How can I change my Controller input from a button to a joystick? (Because I am new, it only lets me put in one screenshot in a post, one second)

1 Like

Now you’ve got the hang of it! To pass joystick input into a command, have the command factory take a DoubleSupplier as a parameter and pass the code that fetches the joystick value as a lambda to the command factory. You can then “capture” it in the lambdas in your command body by calling accept on the DoubleSupplier - this will return your joystick value.

This sounds complicated when written out, but once you “get” it the code is really simple. You can see an example of this pattern in the default drive command for the RapidReactCommandBot.

2 Likes