Go to Post When a vomit shooter runs out of balls, is it dry heaving? :p - Lil' Lavery [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 Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Spotlight this post!  
Unread 10-02-2015, 13:32
Rakusan2 Rakusan2 is offline
Registered User
AKA: Tomas Rakusan
FRC #3571 (Milton Mustangs)
Team Role: Programmer
 
Join Date: May 2014
Rookie Year: 2011
Location: Milton, ON, Canada
Posts: 22
Rakusan2 is an unknown quantity at this point
Xbox Controller class

To make it easier for me and other programmers especially new ones, I have made this class that uses the Joystick class to get the state of the buttons and axis on an XboxController in a way that enables others to not have to remember the button and axis numbers. To that it is also able to tell if the button has just changed state that run making it easier to create programs that use toggle. The only dounside is that at the start of every run a refresh method has to be called.
We have been using this code with out problems and I would like to know what others think about it.
Thanks

Code:
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Joystick.RumbleType;

/**
 *
 * @author TomasR
 */
public class XboxController {
    private final Joystick joy;
    private Button[] button=new Button[10];
    
    /**
     * Axis state as of the last refresh
     */
    public Axis LeftStick=new Axis(0,0), RightStick=new Axis(0,0);
    /**
     * trigger states as of the last refresh
     */
    public triggers Triggers=new triggers(0,0);
    /**
     * Button states as of the last refresh
     */
    public buttons Buttons;
    
    public class triggers{
    	public double Right;
    	public double Left;
    	public double Combined;
    	public triggers(double r, double l){
    		Right=r;
    		Left=l;
    		combine();
    	}
    	private void combine(){
    		Combined=Right-Left;
    	}
    }
    class Axis{
        public double X,Y;
        public Axis(double x,double y){
            X=x;
            Y=y;
        }
    }
    class buttons{
        public Button A =button[0];
        public Button B =button[1];
        public Button X =button[2];
        public Button Y =button[3];
        public Button LB =button[4];
        public Button RB =button[5];
        public Button Back =button[6];
        public Button Start =button[7];
        public Button LeftStick =button[8];
        public Button RightStick =button[9];
    	
    }
    class Button{
        public boolean current=false , last=false,changedDown=false,changedUp=false;
        private void set(boolean c){
        	last=current;
        	current=c;
        	changedDown=!last && current;
        	changedUp=last && !current;
        }
    }
    
    /** 
     * @return the angle in degrees or -1 if not pressed
     */
    public int getDpad(){
    	return joy.getPOV(0);
    }
    private void leftStick(){
    	LeftStick.X=joy.getRawAxis(0);
    	LeftStick.Y=joy.getRawAxis(1);
    }
    private void rightStick(){
    	RightStick.X=joy.getRawAxis(4);
    	RightStick.Y=joy.getRawAxis(5);
    }
    private void trigger(){
        Triggers.Left = joy.getRawAxis(2);
        Triggers.Right = joy.getRawAxis(3);
        Triggers.combine();
    }
    
    /**
     * refreshes all button and axis values
     * Should be called only once per run
     */
    public void refresh(){
    	for(int i=0;i<10;i++){
    		button[i].set(joy.getRawButton(i));
    	}
        leftStick();
        rightStick();
        trigger();
    }
    
    /**
     * @param i The controller number
     */
    public XboxController(int i) {
        joy=new Joystick(i);
        refresh();
        Buttons=new buttons();
    }
    
    /**
     * 
     * @param type Left or Right rumble
     * @param value A value from 0 to 1 for the intensity
     */
    public void vibrate(RumbleType type,float value){
    	joy.setRumble(type, value);
    }
}

Last edited by Rakusan2 : 10-02-2015 at 19:12. Reason: new buttons() was being called too often
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 10:53.

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