Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. import edu.wpi.first.wpilibj.GenericHID;
  2. import edu.wpi.first.wpilibj.buttons.Button;
  3.  
  4. /**
  5. * This class allows you to trigger commands from an analog input on
  6. * a joystick (sich as the triggers - Axis 3).
  7. *
  8. *
  9. * The following example code placed in OI class turns axis 3 into two buttons:
  10. * ----------------------------------------------------------------------------
  11. * //Create an AnalogButton for each trigger
  12. * int joystickChannel = 1;
  13. * public JoystickAnalogButton TriggerR = new JoystickAnalogButton(joystickChannel, 3, -0.5),
  14. * TriggerL = new JoystickAnalogButton(joystickChannel, 3, 0.5)
  15. *
  16. * //Link the buttons to commands
  17. * TriggerR.whenPressed(new ExampleCommand1());
  18. * TriggerL.whenPressed(new ExampleCommand2())
  19. *
  20. * Note that since both buttons are on the same Axis channel, they cannot be
  21. * pressed simultaneously. One trigger will negate the other and neither will
  22. * look pressed. So plan your controls accordingly.
  23. *
  24. * @author James@team2168.org
  25. *
  26. */
  27. public class JoystickAnalogButton extends Button {
  28.  
  29. GenericHID m_joystick;
  30. int m_axisNumber;
  31. private double THRESHOLD = 0.5;
  32.  
  33. /**
  34. * Create a button for triggering commands off a joystick's analog axis
  35. *
  36. * @param joystick The GenericHID object that has the button (e.g. Joystick, KinectStick, etc)
  37. * @param axisNumber The axis number
  38. */
  39. public JoystickAnalogButton(GenericHID joystick, int axisNumber) {
  40. m_joystick = joystick;
  41. m_axisNumber = axisNumber;
  42. }
  43.  
  44. /**
  45. * Create a button for triggering commands off a joystick's analog axis
  46. *
  47. * @param joystick The GenericHID object that has the button (e.g. Joystick, KinectStick, etc)
  48. * @param axisNumber The axis number
  49. * @param threshold The threshold to trigger above (positive) or below (negative)
  50. */
  51. public JoystickAnalogButton(GenericHID joystick, int axisNumber, double threshold) {
  52. m_joystick = joystick;
  53. m_axisNumber = axisNumber;
  54. THRESHOLD = threshold;
  55. }
  56.  
  57. /**
  58. * Set the value above which triggers should occur (for positive thresholds)
  59. * or below which triggers should occur (for negative thresholds)
  60. * The default threshold value is 0.5
  61. *
  62. * @param threshold the threshold value (1 to -1)
  63. */
  64. public void setThreshold(double threshold){
  65. THRESHOLD = threshold;
  66. }
  67.  
  68. /**
  69. * Get the defined threshold value.
  70. * @return the threshold value
  71. */
  72. public double getThreshold(){
  73. return THRESHOLD;
  74. }
  75.  
  76.  
  77. public boolean get() {
  78. if(THRESHOLD < 0){
  79. return m_joystick.getRawAxis(m_axisNumber) < THRESHOLD; //Return true if axis value is less than negative threshold
  80. } else {
  81. return m_joystick.getRawAxis(m_axisNumber) > THRESHOLD; //Return true if axis value is greater than positive threshold
  82. }
  83. }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement