Help coding deadzones

So I found this code for adding a joystick deadzone:

void driving(){
double y = 0;           //variable for forward/backward movement
double x = 0;           //variable for side to side movement
double turn = 0;        //variable for turning movement
double deadzone = 0.3;	//variable for amount of deadzone

if(driverStick.GetY() > deadzone || driverStick.GetY() < -deadzone) {
	y = driverStick.GetY();
}

if(driverStick.GetX() > deadzone || driverStick.GetX() < -deadzone) {
	x = driverStick.GetX();
}

if(driverStick2.GetX() > deadzone || driverStick2.GetX() < -deadzone){
	turn = driverStick2.GetX();
}

but I’m not sure where to add them in or if this is best choice. We are using a meccanum drive and the logitech joystick I can post our full code if it would be useful

This is one way of implementing a dead zone; you then can use the x, y, and turn variables in your mechanum method. But, you’ll get a discontinuity when the joystick values cross above or below 0.3 in your example (the values will jump from 0.0 to 0.3, or 0.3 to 0.0.)

A technique a team (thanks, 1868!) told me about long ago in one of my programming classes, is to square the joystick parms, but also preserve the sign. You’ll get a parabolic response this way, flatter at low joystick positions but still get full-speed (1.0 or -1.0) values at full joystick.

Not enough of a low-speed behavior? Try cubing the JS values!

See https://wpilib.screenstepslive.com/s/currentCS/m/java/l/914252-wpilib-drive-classes-conventions-and-defaults for more on squaring.

1 Like

ok I’ll a look a that link thanks

ok so I tried looking into what you suggested but I’m not the actual coder on my team so don’t really know how to do something like this could you give me an example?

Here is the deadband function I used this year:

private static double linearDeadband(double raw, double deadband)
{

    if (Math.abs(raw)<deadband) return 0;

   return Math.signum(raw)*(Math.abs(raw)-deadband)/(1-deadband);
}

So, within the deadband, the value is 0. At the deadband limit, the value is 0, then rises linearly through the value 1. The point of it is to eliminate the discontinuity mentioned at the deadband limit.

I think I will probably square the fractional term next year, in order to achieve the fine control benefits at low ends mentioned by others.

“raw” is the raw value read from the joystick.
“deadband” is the deadband limit. i.e. if deadband=0.1, then any joystick value less than 0.1 will return 0.

For the visually inclined, this article was shared a few weeks back:

http://www.third-helix.com/2013/04/12/doing-thumbstick-dead-zones-right.html

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.