Button Mapping

I can see that my gamepad is recognized by the diver station and I see that each button works. I am curious what do I put before the .getRawbutton method to use them. this is my gamepad.

What does your current code look like for the controller input? Luckily for you, if the driver station interprets all of the buttons and axes on that gamepad properly, you should be able to instantiate a GenericHID (GenericHID (WPILib API 2022.3.1)) on the port that the Driver Station recognizes it under.

Have a look at the official documentation (see the Joystick Class article).

You basically have to

  • declare a joystick in the file RobotContainer.java:
    • example: Joystick m_driverJoystick = new Joystick(0);
    • “0” is the port of the joystick (you can look on the driver station to get the port… if multiple joysticks are plugged-in the joysticks will have different ports)
  • declare the buttons for your specific joystick:
    • example: final JoystickButton myButton = new JoystickButton(driverJoystick , 9);
    • “9” is the button number, you can use the driver station to determine the numbers of the buttons
  • bind a button to a command
    • example: myButton.whenPressed(new PrintCommand("MyButton was pressed"));
    • a PrintCommand is a command that prints text in the console and ends instantly

So putting it all together it gives something like this.

package frc.robot;

import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj2.command.PrintCommand;
import edu.wpi.first.wpilibj2.command.button.JoystickButton;

public class RobotContainer {

    public Joystick m_driverJoystick = new Joystick(0);

    public RobotContainer() {
        JoystickButton myButton = new JoystickButton(m_driverJoystick, 9);
        myButton.whenPressed(new PrintCommand("MyButton was pressed"));
    }
}

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