Go to Post Funding and success is no accident. - waialua359 [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
Prev Previous Post   Next Post Next
  #1   Spotlight this post!  
Unread 19-04-2014, 18:32
dmaki dmaki is offline
First VISTA
AKA: Dustin Maki
no team
Team Role: Engineer
 
Join Date: Jan 2014
Rookie Year: 2010
Location: United States
Posts: 6
dmaki is a jewel in the roughdmaki is a jewel in the roughdmaki is a jewel in the roughdmaki is a jewel in the rough
Joystick POV Hat Switch code

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?

Code:
/*
 * 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
}
Code:
/* 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());
    
    }
}
Attached Files
File Type: zip TestPOVhat.zip (1.37 MB, 12 views)
Reply With Quote
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 08:47.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi