Below is code I wrote to make the Joystick POV Hat Switch behave like 8 directional buttons for a command based java FRC robot. It consists of only 1 file and requires manually instantiating only one object in OI.java as below. The problem is we are a rookie team that made it to Championship, so I have no robot to test it on to see if it works. I would appreciate if someone could run the attached NetBeans project on their robot and tell me if it prints the debug messages as expected when operating the joystick hat switch. Thank you. If it works, this seems like a useful utility, feel free to use it. Does anyone know how to contribute code back to wpilibj?
/*
* POVhat.java
*/
package edu.wpi.first.wpilibj.frc5002;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.buttons.Trigger;
/**
* Allows the POV hat switch on the joystick to be represented like a group
* of 8 buttons. Attach commands as easily as with buttons.
* @author Dustin Maki
*/
public class POVhat // outer class
{
Joystick stick;
int hatXaxis;
int hatYaxis;
POVtrigger Right;
POVtrigger UpRight;
POVtrigger Up;
POVtrigger UpLeft;
POVtrigger Left;
POVtrigger DownLeft;
POVtrigger Down;
POVtrigger DownRight;
class POVtrigger extends Trigger // inner class
{
private final int Xval;
private final int Yval;
private boolean exclusive = true; // if set true(default), trigger is active only when it exactly matches position of hat switch.
// if set false, trigger is active in any position that shares trigger name.
// i.e. If Up is set not exclusive, Command associated with Up will trigger if hat switch is UpLeft, UpRight, or Up.
private static final int kRight_XVAL = 1;
private static final int kUpRight_XVAL = 1;
private static final int kUp_XVAL = 0;
private static final int kUpLeft_XVAL = -1;
private static final int kLeft_XVAL = -1;
private static final int kDownLeft_XVAL = -1;
private static final int kDown_XVAL = 0;
private static final int kDownRight_XVAL = 1;
private static final int kRight_YVAL = 0;
private static final int kUpRight_YVAL = -1;
private static final int kUp_YVAL = -1;
private static final int kUpLeft_YVAL = -1;
private static final int kLeft_YVAL = 0;
private static final int kDownLeft_YVAL = 1;
private static final int kDown_YVAL = 1;
private static final int kDownRight_YVAL = 1;
protected POVtrigger(int Xval, int Yval)// inner class constructor
{
this.Xval = Xval;
this.Yval = Yval;
}
public void setExclusive(boolean exclusive) //only useful on up, down, left, and right
{
this.exclusive = exclusive;
}
public boolean get() // mandatory override from Trigger class
{
if (exclusive)
return (stick.getRawAxis(hatXaxis) == this.Xval && stick.getRawAxis(hatYaxis) == this.Yval);
else
return ((0 == this.Xval || stick.getRawAxis(hatXaxis) == this.Xval) && (0 == this.Yval || stick.getRawAxis(hatYaxis) == this.Yval));
}
}
// begin outer class constructors
// public POVhat()
// {
// this(Robot.oi.driverJoystick, 5, 6);
// }
public POVhat(Joystick stick)
{
this(stick, 5, 6);
}
public POVhat(Joystick stick, int hatXaxis, int hatYaxis)
{
this.stick = stick;
this.hatXaxis = hatXaxis;
this.hatYaxis = hatYaxis;
this.Right = new POVtrigger(POVtrigger.kRight_XVAL, POVtrigger.kRight_YVAL);
this.UpRight = new POVtrigger(POVtrigger.kUpRight_XVAL, POVtrigger.kUpRight_YVAL);
this.Up = new POVtrigger(POVtrigger.kUp_XVAL, POVtrigger.kUp_YVAL);
this.UpLeft = new POVtrigger(POVtrigger.kUpLeft_XVAL, POVtrigger.kUpLeft_YVAL);
this.Left = new POVtrigger(POVtrigger.kLeft_XVAL, POVtrigger.kLeft_YVAL);
this.DownLeft = new POVtrigger(POVtrigger.kDownLeft_XVAL, POVtrigger.kDownLeft_YVAL);
this.Down = new POVtrigger(POVtrigger.kDown_XVAL, POVtrigger.kDown_YVAL);
this.DownRight = new POVtrigger(POVtrigger.kDownRight_XVAL, POVtrigger.kDownRight_YVAL);
}
// end outer class constructors
}
/* OI.java*/
package edu.wpi.first.wpilibj.frc5002;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.frc5002.commands.*;
public class OI {
public Joystick driverJoystick;
public POVhat hatSwitch;
public OI()
{
driverJoystick = new Joystick(1); // create driverJoystick object
hatSwitch = new POVhat(driverJoystick); // create hatSwitch object
// assign commands to the 8 individual directions of the switch
hatSwitch.Right.whenActive(new rotateRight90Command());
hatSwitch.Left.whenActive(new rotateLeft90Command());
hatSwitch.Up.whenInactive(new setDriveRegulatedSpeedCommand(true));
hatSwitch.Up.whileActive(new driveStraightForwardFastCommandGroup());
hatSwitch.Down.setExclusive(false);
hatSwitch.Down.whenActive(new updateRobotDisplayCommand());
hatSwitch.UpRight.cancelWhenActive(new driveStraightForwardFastCommandGroup());
hatSwitch.UpLeft.cancelWhenActive(new driveStraightForwardFastCommandGroup());
hatSwitch.DownRight.toggleWhenActive(new rightBlinkerCommand());
hatSwitch.DownLeft.toggleWhenActive(new leftBlinkerCommand());
}
}
```<br><br><a class='attachment' href='/uploads/default/original/3X/e/c/eceaaee8367c44eb4ff54c2d7a861e843e40bf26.zip'>TestPOVhat.zip</a> (1.37 MB)<br><br><br><a class='attachment' href='/uploads/default/original/3X/e/c/eceaaee8367c44eb4ff54c2d7a861e843e40bf26.zip'>TestPOVhat.zip</a> (1.37 MB)<br>