Quote:
Originally Posted by DjMaddius
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));
...
}