|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
How do I program a joystick deadband?
I'm using an Xbox controller, which are notorious for not returning to center properly. I want to use a threshold of +- .1 (on a scale of -1 to 1).
I assume it would be something like: Code:
if ((joystick < .1) && (joystick > -.1)) {
return 0;
}
Also: I'm planning on using one thumbstick to turn, and the right trigger as throttle, but I'm not sure how you would accomplish that and most tutorials are either for tank drive or in languages other than Java. Thanks! |
|
#2
|
||||
|
||||
|
Re: How do I program a joystick deadband?
About your first question:
Code:
public double getJoystickValue(Joystick joystick) {
if(Math.abs(joystick.getValue() < 0.1) return 0;
else return joystick.getValue();
}
As for your second question about using two joysticks, the RobotDrive classes have an arcadeDrive method that has two arguments: the speed you go forward (the value of the throttle joystick) and the speed you turn at (the value of the turning joystick). |
|
#3
|
||||
|
||||
|
Re: How do I program a joystick deadband?
Does this appear to be correct?
![]() |
|
#4
|
||||
|
||||
|
Re: How do I program a joystick deadband?
I'd just suggest changing the name of your "turn" method to something less confusing, such as "getCompensatedValue" or "getBetterValue" (or anything better you can think of). If you call "turn(Xbox)", then you may be confused why the robot's not turning
Other than that, you should be good. Good luck! |
|
#5
|
||||||
|
||||||
|
Re: How do I program a joystick deadband?
You've created a new function, but haven't called it. In order to do the equivalent of the commented out code, you would need to add another line similar to the following
Code:
double turn = turn(Xbox); Code:
double turn = applyDeadband(Xbox); |
|
#6
|
||||
|
||||
|
Re: How do I program a joystick deadband?
Allow me to show you a piece of code my team has used the last three years....
public double deadband(double JoystickValue, double DeadbandCutOff) { if (JoystickValue<DeadbandCutOff&&JoystickValue>(Dead bandCutOff*(-1))) { deadbandreturn=0; } else { deadbandreturn=(JoystickValue-(Math.abs(JoystickValue)/JoystickValue*DeadbandCutOff))/(1-DeadbandCutOff); } return deadbandreturn; } this not only cuts it off at a point, but prevents jumping up to 0.1 after you get past it. it basically smooths the dead band curve. How ever, i would recommend making a algorithm you understand. ((i am still working on understanding this one. lol)) Last edited by techkid86 : 08-10-2012 at 01:31. |
|
#7
|
||||
|
||||
|
Re: How do I program a joystick deadband?
Quote:
To anyone confused: The first if() checks if the joystick value is in the (-deadband,deadband) range (this could more succinctly be done with Math.abs(JoystickValue) < deadband), and if so, puts the output to 0. Otherwise, it scales the joystick value to [0,1] or [-1,0] depending on the sign by modifying the ranges this way: [deadband,1] -> [0,1-deadband] -> [0,1] [-1,-deadband] -> [-1+deadband,0] -> [-1,0] Code:
deadbandreturn=(JoystickValue- // initially in one of two ranges: [DeadbandCutOff,1] or [-1,-DeadBandCutOff]
(Math.abs(JoystickValue)/JoystickValue // 1 if JoystickValue > 0, -1 if JoystickValue < 0 (abs(x)/x); could use Math.signum(JoystickValue) instead
*DeadbandCutOff // multiply by the sign so that for >0, it comes out to - (DeadBandCutOff), and for <0 it comes to - (-DeadBandCutOff)
)
) // now in either [0,1-DeadBandCutOff] or [-1+DeadBandCutOff,0]
/(1-DeadbandCutOff); // scale to [0,1] or [-1,0]
Last edited by Ginto8 : 08-10-2012 at 12:13. |
|
#8
|
||||
|
||||
|
Re: How do I program a joystick deadband?
There's deadband in the motor controller too.
|
|
#9
|
||||
|
||||
|
Re: How do I program a joystick deadband?
Personally, the best way I can think of to go about this would be to extend the Joystick class, making a DeadbandJoystick class. I'll write up an example of what I'm talking about when I get home tonight.
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|