Deadband Joystick Algorithm

This season I have decided to step-up the programming on our robot and create code that will benefit us in driver control and autonomous. This deadband algorithm will specifically help in driver control. Here is a link to deadband on Wikipedia Basic joystick programming sets motor power equal to the joystick value. Unfortunately the joysticks don’t return to zero all the time, so when you think your joysticks are at 0, they may actually be hovering around 20 (I think), which will send 20 power to the motor. This is insufficient to move the robot, so the motors stall and create that horrible sound. Stalling the motor with this little power may not be too harmful, but it is drawing power from the battery, which is never good.

Unfortunately, I don’t have my robot with me, so I can’t test it out, but if anyone see any errors feel free to share. :slight_smile: Also, do you think the deadband range is too small? Too large?


void leftDriveForward(int driveSpeed)
{
motor[port1]= -driveSpeed; //negative values are to simulate a reversed motor
motor[port10] = -driveSpeed; 
}

while(true){

int y_posLJoyStk = vexRT[Ch3];
bool deadband = true;
int DB_range= 20; //range of values that will be registered as neutral

if(abs(vexRT[Ch3]< DB_range)//  if the joystick is within the deadband range
{
y_posLJoyStk = 0;                //store 0 in the  y_posLJoystk integer
deadband = true;               //set deadband to true
} else {              //Joystick is out of deadband range
y_posLJoyStk= vexRT[Ch3];               //store actual value of the joystick in the y_posLJoystk integer
deadband = false;                //set deadband to false
}


while(deadband = false)
{
leftDriveForward(y_posLJoyStk);
}
}

You don’t need a bool for anything. Just set an int equal to either 0 (inside the deadband) or the joystick value. It works quite well.



#define DB_THRESH 15

task main {
  int rx;
  while(1) {
  if(abs(vexRT[Ch3]) >= DB_THRESH)
    ly = vexRT[Ch3];
  else
    ly = 0;
  leftDriveForward(ly);
  }
}

I use 7 for my deadband range usually, but sometimes I use 15. Those are 1/16 and 1/8 of 127 (Cortex math is fun) respectively.

You could use ternary operators to make it even simpler:


#define DB_THRESH 15

task main {
  int rx;
  while(1) {
    ly = (abs(vexRT[Ch3]) >= DB_THRESH) ? vexRT[Ch3] : 0;
    leftDriveForward(ly);
  }
}

Thank you @lpieroni and @Bobtheblob - 5327B. If I could, I would have marked both your responses as the answer. :slight_smile: @Ipieroni, I like your idea of 1/8 of 127 as the Deadband threshold.

Just to add. Looking at your code you seem to set the motor to a negative value rather than invert the motor in the motor sensor setup. I would suggest using the reverse flag in motor sensor setup so your code is more portable and is less likely to have errors.

Yes, thank you. The computer I’m writing this on doesn’t have robot C installed. :stuck_out_tongue: I’ll make sure invert it in the motor and sensor setup.