Go to Post FIRST is privlidge that I am glad to be a part of and any award we recieve is a blessing to be cherish not an expectation to be agonized over. - Koko Ed [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

 
Reply
Thread Tools Rate Thread Display Modes
  #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
  #2   Spotlight this post!  
Unread 10-02-2015, 13:35
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is offline
Flywheel Police
AKA: Matthew Lythgoe
FRC #2363 (Triple Helix)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: Newport News, VA
Posts: 1,722
notmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond repute
Re: Xbox Controller class

Just a small nitpick from first glance, but is there a reason you didn't extend the Joystick class? This would allow you to use this class in any place a Joystick is used. We're currently working on one that overrides all of the joystick methods to map them to the buttons and sticks correctly.
Reply With Quote
  #3   Spotlight this post!  
Unread 10-02-2015, 13:42
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
Re: Xbox Controller class

That is because I have never worked with extend
Reply With Quote
  #4   Spotlight this post!  
Unread 10-02-2015, 13:44
GeeTwo's Avatar
GeeTwo GeeTwo is offline
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,654
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: Xbox Controller class

Quote:
Originally Posted by notmattlythgoe View Post
Just a small nitpick from first glance, but is there a reason you didn't extend the Joystick class? This would allow you to use this class in any place a Joystick is used. We're currently working on one that overrides all of the joystick methods to map them to the buttons and sticks correctly.
Actually, this makes sense to me. The x box controller has a joystick.
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.
Reply With Quote
  #5   Spotlight this post!  
Unread 10-02-2015, 13:45
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is offline
Flywheel Police
AKA: Matthew Lythgoe
FRC #2363 (Triple Helix)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: Newport News, VA
Posts: 1,722
notmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond repute
Re: Xbox Controller class

Quote:
Originally Posted by Rakusan2 View Post
That is because I have never worked with extend
Well, that's a valid reason

If you are interested you can look up information about Inheritance, Java is an Object Oriented Programming language, so it is a good topic to read up on.

If you have any questions let me know.
Reply With Quote
  #6   Spotlight this post!  
Unread 10-02-2015, 13:47
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is offline
Flywheel Police
AKA: Matthew Lythgoe
FRC #2363 (Triple Helix)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: Newport News, VA
Posts: 1,722
notmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond repute
Re: Xbox Controller class

Quote:
Originally Posted by GeeTwo View Post
Actually, this makes sense to me. The x box controller has a joystick.
In the loosest term of joystick. The OP's reasoning for not extending the Joystick class is a more valid reason not to.

Last edited by notmattlythgoe : 10-02-2015 at 14:09.
Reply With Quote
  #7   Spotlight this post!  
Unread 10-02-2015, 14:03
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
Re: Xbox Controller class

As I been looking, extending Joystick class might make the choices of methods which I want to keep as few as possible too crowded for the programmer that is using my class.
Reply With Quote
  #8   Spotlight this post!  
Unread 10-02-2015, 14:09
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is offline
Flywheel Police
AKA: Matthew Lythgoe
FRC #2363 (Triple Helix)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: Newport News, VA
Posts: 1,722
notmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond repute
Re: Xbox Controller class

Quote:
Originally Posted by Rakusan2 View Post
As I been looking, extending Joystick class might make the choices of methods which I want to keep as few as possible too crowded for the programmer that is using my class.
It definitely adds a bunch of new methods to the class, which can both be a good and bad thing. And there is nothing wrong with a simple class that sets all of the mappings to meaningful method names that aren't getRawAxis(2).

Overall, looks good. Well done.
Reply With Quote
Reply


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 12:35.

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