View Single Post
  #6   Spotlight this post!  
Unread 02-17-2013, 10:21 PM
kylelanman's Avatar
kylelanman kylelanman is offline
Programming Mentor
AKA: Kyle
FRC #2481 (Roboteers)
Team Role: Mentor
 
Join Date: Feb 2008
Rookie Year: 2007
Location: Tremont Il
Posts: 185
kylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to all
Re: Launching a command with a second thumbstick

Quote:
Originally Posted by nickpaterni View Post
We are using an Xbox controller for robot control, and would like to use the right thumbstick to call a command - specifically to be able to use the value from GetRawAxis() to determine the speed at which we activate a Victor. I know how to get a button to launch a command in OI, but don't know how to make movement of the right thumbstick do the same. Any ideas?
We ran into the same problem. Our solution that I consider elegant was to make our own AnalogJoystickButton that inherits button.

You can simply create a Button like your would a JoystickButton but also pass in a threshold at which point you consider the joystick "pressed". This allows the assignment of multiple commands to different thresholds. Typically we use .5 and -.5 to get both directions from the axis.

Once you include the class below you can do the following. I'm assuming you know how to create a JoystickButton in OI. Have the member be an

Code:
AnalogJoystickButton* fireDiscButton;
Code:
fireDiscButton = new AnalogJoystickButton(shooterStick, XboxController::xbZAxis, -.5);
fireDiscButton->WhileHeld(new FireDiscCommand());
*Note: You need to provide the axis number. We have them defined as enums so we can just use XBoxController::xbZAxis to get at the trigger axis.

In your actual command you can get your joystick from OI and get the actual value of the axis if you need it for speed.

AnalogJoystickButton.h:

Code:
#ifndef _ANAOLG_JOYSTICK_BUTTON_H__
#define _ANOLOG_JOYSTICK_BUTTON_H__

#include "WPILib.h"

class AnalogJoystickButton : public Button
{
public:
	AnalogJoystickButton(GenericHID *joystick, int axisNumber, float threshold);
	virtual ~AnalogJoystickButton() {}

	virtual bool Get();
	
private:
	GenericHID *m_joystick;
	int m_axisNumber;
	float m_threshold;
};

#endif


AnalogJoystickButton.cpp:

Code:
#include "AnalogJoystickButton.h"

AnalogJoystickButton::AnalogJoystickButton(GenericHID *joystick, int axisNumber, float threshold ) {
        m_threshold = threshold;
        m_joystick = joystick;
        m_axisNumber = axisNumber;
}

bool AnalogJoystickButton::Get()
{
        if(m_threshold < 0)
                return m_joystick->GetRawAxis(m_axisNumber) < m_threshold;
        else if(m_threshold > 0)
                return m_joystick->GetRawAxis(m_axisNumber) > m_threshold;
        return false; 
                
}
Let me know if you have any questions or need help implementing it.

EDIT -- Just to make like easier.


XboxController.h:

Code:
#ifndef XBOXCONTROLLER_H_
#define XBOXCONTROLLER_H_

#include "WPILib.h"



class XboxController: public Joystick {
public:
        static const UINT32 XboxController::xbLeftXAxis;
        static const UINT32 XboxController::xbLeftYAxis;
        static const UINT32 XboxController::xbZAxis;
        static const UINT32 XboxController::xbRightXAxis;
        static const UINT32 XboxController::xbRightYAxis;
        static const UINT32 XboxController::xbAButton;
        static const UINT32 XboxController::xbBButton;
        static const UINT32 XboxController::xbXButton;
        static const UINT32 XboxController::xbYButton;
        static const UINT32 XboxController::xbStartButton;
        static const UINT32 XboxController::xbSelectButton;
        static const UINT32 XboxController::xbRightBumper;
        static const UINT32 XboxController::xbLeftBumper;
        static const UINT32 XboxController::xbRightStickCLick;
        static const UINT32 XboxController::xbLeftStickClick;
        
        XboxController(UINT32 port);
        virtual ~XboxController();
        
};

#endif /* XBOXCONTROLLER_H_ */

XboxController.cpp

Code:
#include "XboxController.h"

const UINT32 XboxController::xbLeftXAxis = 1;
const UINT32 XboxController::xbLeftYAxis = 2;
const UINT32 XboxController::xbZAxis = 3;
const UINT32 XboxController::xbRightXAxis = 4;
const UINT32 XboxController::xbRightYAxis = 5;
const UINT32 XboxController::xbAButton = 1;
const UINT32 XboxController::xbBButton = 2; 
const UINT32 XboxController::xbXButton = 3;
const UINT32 XboxController::xbYButton = 4;
const UINT32 XboxController::xbStartButton = 8;
const UINT32 XboxController::xbSelectButton = 7;
const UINT32 XboxController::xbRightBumper = 6;
const UINT32 XboxController::xbLeftBumper = 5;
const UINT32 XboxController::xbRightStickCLick = 10;
const UINT32 XboxController::xbLeftStickClick = 9;


XboxController::XboxController(UINT32 port) : Joystick(port){}

XboxController::~XboxController() {}
__________________
"May the coms be with you"

Is this a "programming error" or a "programmer error"?


Last edited by kylelanman : 02-17-2013 at 10:25 PM. Reason: Added XboxController Class for easier implementation.
Reply With Quote