Joystick Deadzones

D R I F T

Introduction

Sometimes, controller joysticks may have a slight non-zero reading when in a neutral position. This may cause robot motors to drift during operator control when motors are directly controlled by the joysticks.

To mitigate this effect, some teams implement a filter where if a joystick's value is low, the joystick's value will be set to zero. This effectively creates a "deadzone" near the center position of the joystick where any small joystick inputs will not be read.

Example Code

Below is example code for joystick deadzones applied to a tank drive.

#include "main.h"

// Motor Group Declarations for Left and Right
pros::Motor_Group motors_left(1);
pros::Motor_Group motors_right(2, true);

// Deadzone of 5
const int deadzone = 5;

void opcontrol() {
    while(1) {
        // Retrieve the joystick values for left and right Y.
        int left = master.get_analog(pros::E_CONTROLLER_ANALOG_LEFT_Y);
        int right = master.get_analog(pros::E_CONTROLLER_ANALOG_RIGHT_Y);
        
        // std::abs takes the absolute value of whatever it is called on.
        // Thus, any values in range (-5,5) are discarded as 0.
        if(std::abs(left) < deadzone)) {
            left = 0;
        }
        if(std::abs(right) < deadzone)) {
            right = 0;
        }
        
        // Drive the left side of the robot forward at joystick left Y speed
        motors_left.move(left);
        
        // Drive the right side of the robot forward at joystick right Y speed
        motors_right.move(right);
        delay(20);
    }
}

Summary

Joystick dead zones while removing some degree of controllability, are sometimes necessary to prevent motor drift with a defective controller. Most of the time, this occurs with older controllers with worn-down joysticks.

Contributing Teams to this Article:

  • BLRS (Purdue SIGBots)

Last updated

Logo

This work is licensed under a Attribution-ShareAlike 2.0 Generic License