View Single Post
  #9   Spotlight this post!  
Unread 12-02-2013, 13:51
David++ David++ is offline
Registered User
no team
 
Join Date: Mar 2012
Rookie Year: 1900
Location: N/A
Posts: 8
David++ is an unknown quantity at this point
Re: Deadzone Programming

Quote:
Originally Posted by DjMaddius View Post
Try this, you need to set default values. Also I just cleaned it up a bit.

Code:
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();
	}
Instead of checking for both the positive and negative deadzone, you can just take the absolute value. In addition, you can reduce the number of function calls to GetX() and GetY() by assuming the value is valid and handling the exceptional situations.

Code:
#include <math.h>

...

void driving() {
    double y = driverStick.GetY();
    double x = driverStick.GetX();
    double turn = driverStick2.GetX();
    double deadzone = 0.3;

    if(fabs(y) < deadzone)
    {
        y = 0;
    }

    if(fabs(x) < deadzone)
    {
        x = 0;
    }

    if(fabs(turn) < deadzone)
    {
        turn = 0;
    }

...

}
Or, if you prefer the compact Arithmetic If operator:

Code:
#include <math.h>

...

void driving() {
    double y = driverStick.GetY();
    double x = driverStick.GetX();
    double turn = driverStick2.GetX();
    double deadzone = 0.3;

    y = ((fabs(y) < deadzone) ? (0) : (y));
    x = ((fabs(x) < deadzone) ? (0) : (x));
    turn = ((fabs(turn) < deadzone) ? (0) : (turn));

...

}

Last edited by David++ : 12-02-2013 at 13:54.
Reply With Quote