Heya again folks. So, I have encountered another issue in my journey. I have no idea how to start on doing joysticks on a command base robot. I have searched far and wide for examples but alas, they are not enough. I would be eternally grateful if someone could provide a good example for how to map a joystick (at a certain angle) along to a connected function. Thank you very much
I get the feeling that, through your searching, you probably came across this page, which documents how to bind joystick or controller buttons to functions through commands, but here it is just incase.
As far as binding a function to the joystick achieving a certain angle, I’m not 100% percent sure what use case you need this for and it’d be quite useful to have some more information.
joystick = new CommandJoystick(joystickPort);
angleCommand = new CommandThatTakesJoystickAngleRadians(
() -> Math.atan2(
-joystick.getX(),
-joystick.getY()
)
);
// Schedule angleCommand to run when needed
This will give your command the CCW-positive angle of your joystick, in radians, where pushing the stick straight forward is 0 radians.
Explanation:
The first line creates the joystick object, using the usb id number.
The command declaration is for a command with a single argument, a DoubleSupplier.
Suppliers are functions that can be called to return values— instead of giving the command the actual angle, which would make it always use whatever the value was when the command was created (i.e. robot startup), we pass it a function that can be called repeatedly to return the current angle. This is what the () → does; it defines an unnamed function that takes no arguments and returns the value of the Math.atan2 function.
Math.atan2(y, x) takes a Cartesian coordinate and returns its polar coordinate value of θ, in radians, while also dealing with all the annoying special cases. Because we want joystick forwards to be an angle of zero, we give the atan2 function the joystick x value as it’s y parameter and vice versa, because the y-axis of the joystick is forward/backward. Additionally, we negate both axes, because most flight-stick type joysticks are right/back positive, and we want left/forward positive.
Sorry about this! Forgot to clarify that I am trying to use a CommandXboxController Joystick. For rotational motion on the operator controller. Thanks again.
Simple to swap over to— just use CommandXboxController instead of CommandJoystick, then .getLeftX() and .getLeftY() instead of .getX() and .getY()
Replace “Left” with “Right” if you want to use the other stick on the controller.
Sorry to respond so late, but thank you very much for the examples and solutions to this problem. Using the accumulated examples from Github and the helpful advice y’all have provided, I was able to create some proper code for the joystick functionality. Thank you again and I hope you have a nice rest of your day.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.