View Single Post
  #2   Spotlight this post!  
Unread 08-10-2012, 11:07
Ginto8's Avatar
Ginto8 Ginto8 is offline
Programming Lead
AKA: Joe Doyle
FRC #2729 (Storm)
Team Role: Programmer
 
Join Date: Oct 2010
Rookie Year: 2010
Location: Marlton, NJ
Posts: 174
Ginto8 is a glorious beacon of lightGinto8 is a glorious beacon of lightGinto8 is a glorious beacon of lightGinto8 is a glorious beacon of lightGinto8 is a glorious beacon of light
Re: How do I program a joystick deadband?

Quote:
Originally Posted by techkid86 View Post
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;
}
That's actually some pretty clever code... I'd like to put that on our robot this year, if you're okay with that.

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.
Reply With Quote