Go to Post you know you have overdosed to first when while you are reading all of these things, you can smile and say "oh yeah, i miss doing that" and then, you start to tear up and scream "I MISS FIRST!!!" - amanda547 [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #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
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 14:15.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi