Skip to content

Instantly share code, notes, and snippets.

@jcorcoran
Last active February 10, 2024 15:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jcorcoran/5743806 to your computer and use it in GitHub Desktop.
Save jcorcoran/5743806 to your computer and use it in GitHub Desktop.
This class turns an analog axis on a Joystick device, into a button which can be used to trigger commands.It is intended to be used with the WPILib Command Based robot project on FRC robots.
import edu.wpi.first.wpilibj.GenericHID;
import edu.wpi.first.wpilibj.buttons.Button;
/**
* This class allows you to trigger commands from an analog input on
* a joystick (sich as the triggers - Axis 3).
*
*
* The following example code placed in OI class turns axis 3 into two buttons:
* ----------------------------------------------------------------------------
* //Create an AnalogButton for each trigger
* int joystickChannel = 1;
* public JoystickAnalogButton TriggerR = new JoystickAnalogButton(joystickChannel, 3, -0.5),
* TriggerL = new JoystickAnalogButton(joystickChannel, 3, 0.5)
*
* //Link the buttons to commands
* TriggerR.whenPressed(new ExampleCommand1());
* TriggerL.whenPressed(new ExampleCommand2())
*
* Note that since both buttons are on the same Axis channel, they cannot be
* pressed simultaneously. One trigger will negate the other and neither will
* look pressed. So plan your controls accordingly.
*
* @author James@team2168.org
*
*/
public class JoystickAnalogButton extends Button {
GenericHID m_joystick;
int m_axisNumber;
private double THRESHOLD = 0.5;
/**
* Create a button for triggering commands off a joystick's analog axis
*
* @param joystick The GenericHID object that has the button (e.g. Joystick, KinectStick, etc)
* @param axisNumber The axis number
*/
public JoystickAnalogButton(GenericHID joystick, int axisNumber) {
m_joystick = joystick;
m_axisNumber = axisNumber;
}
/**
* Create a button for triggering commands off a joystick's analog axis
*
* @param joystick The GenericHID object that has the button (e.g. Joystick, KinectStick, etc)
* @param axisNumber The axis number
* @param threshold The threshold to trigger above (positive) or below (negative)
*/
public JoystickAnalogButton(GenericHID joystick, int axisNumber, double threshold) {
m_joystick = joystick;
m_axisNumber = axisNumber;
THRESHOLD = threshold;
}
/**
* Set the value above which triggers should occur (for positive thresholds)
* or below which triggers should occur (for negative thresholds)
* The default threshold value is 0.5
*
* @param threshold the threshold value (1 to -1)
*/
public void setThreshold(double threshold){
THRESHOLD = threshold;
}
/**
* Get the defined threshold value.
* @return the threshold value
*/
public double getThreshold(){
return THRESHOLD;
}
public boolean get() {
if(THRESHOLD < 0){
return m_joystick.getRawAxis(m_axisNumber) < THRESHOLD; //Return true if axis value is less than negative threshold
} else {
return m_joystick.getRawAxis(m_axisNumber) > THRESHOLD; //Return true if axis value is greater than positive threshold
}
}
}
@battery-staple
Copy link

Awesome, thanks! I'd love to use this myself in an FRC library our team is writing—do you mind if we do that?

@jcorcoran
Copy link
Author

Awesome, thanks! I'd love to use this myself in an FRC library our team is writing—do you mind if we do that?

@battery-staple please do.
There may be something similar you could use in newer versions of WPILib.

If you do end up using, please attribute to james@team2168.org

@M0000se
Copy link

M0000se commented Jan 3, 2024

this is great, exactly what I was about to do for FTClib in ftc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment