Go to Post ...remember, this is coming from someone that spent the better part of an afternoon playing with an omniwheel--not assembling, not disassembling, just playing. - Billfred [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

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 08-02-2013, 20:12
Arrowhead Arrowhead is offline
Registered User
AKA: Daniel Brown
FRC #1369 (1369 Minotaur)
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2013
Location: Tampa, Florida
Posts: 49
Arrowhead is an unknown quantity at this point
Deadzone Programming

I looked around a bit but couldn't find any resources on this for c++
How do you program in a deadzone for a attack 3 joystick?
I wrote this code but it doesn't seem to be working.

Code:
	void driving(){
		double y;           //variable for forward/backward movement
		double x;           //variable for side to side movement
		double turn;        //variable for turning movement
		if(driverStick.GetY()>0.3||driverStick.GetY()<-0.3){
			y=driverStick.GetY();
		}
		if(driverStick.GetX()>0.3||driverStick.GetX()<-0.3){
			x=driverStick.GetX();
			}
		if(driverStick2.GetX()>0.3||driverStick2.GetX()<-0.3){
			turn=driverStick2.GetX();
			}
Reply With Quote
  #2   Spotlight this post!  
Unread 08-02-2013, 20:20
DjMaddius's Avatar
DjMaddius DjMaddius is offline
Registered User
AKA: Matt Smith
FRC #2620 (Southgate Titans)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2009
Location: Southgate, Mi
Posts: 161
DjMaddius is an unknown quantity at this point
Re: Deadzone Programming

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();
	}
Reply With Quote
  #3   Spotlight this post!  
Unread 08-02-2013, 20:22
Arrowhead Arrowhead is offline
Registered User
AKA: Daniel Brown
FRC #1369 (1369 Minotaur)
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2013
Location: Tampa, Florida
Posts: 49
Arrowhead is an unknown quantity at this point
Re: Deadzone Programming

Ok, thanks, I'll try that at practice on monday.
Also, how does setting default values for those variables change it?
Reply With Quote
  #4   Spotlight this post!  
Unread 08-02-2013, 20:24
DjMaddius's Avatar
DjMaddius DjMaddius is offline
Registered User
AKA: Matt Smith
FRC #2620 (Southgate Titans)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2009
Location: Southgate, Mi
Posts: 161
DjMaddius is an unknown quantity at this point
Re: Deadzone Programming

Quote:
Originally Posted by Arrowhead View Post
Ok, thanks, I'll try that at practice on monday.
Also, how does setting default values for those variables change it?
Well, if you don't and you try to use it later after the if statement failing the value is set to null and you'll get an error.
Reply With Quote
  #5   Spotlight this post!  
Unread 09-02-2013, 09:46
kylelanman's Avatar
kylelanman kylelanman is online now
Programming Mentor
AKA: Kyle
FRC #2481 (Roboteers)
Team Role: Mentor
 
Join Date: Feb 2008
Rookie Year: 2007
Location: Tremont Il
Posts: 191
kylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to all
Re: Deadzone Programming

We've found the following to work great for dead zones. The main advantage is you can still generate values between 0 and .3. With the previously posted solution once you get out of the dead zone you're instantly outputting .3 or 30%. If you notice we have a lot of 127s in the middle of the array. Those provide the dead zone but we shift everything else over so that once your out of the dead zone it doesn't go straight to .3.

This was written on the old control system. Technically the cRio has enough power to run the equation we are using to generate the array. We've just never taken the time to switch it over.

Code:
const char inputShape[255] = {0,1,3,4,5,6,7,9,10,11,12,13,15,16,17,18,19,21,22,23,24,25,27,28,29,30,31,
	        33,34,35,36,37,38,40,41,42,43,44,46,47,48,49,50,52,53,54,55,56,58,59,60,61,62,
	        64,65,66,67,68,70,71,72,73,74,76,77,78,79,80,82,83,84,85,86,88,89,90,91,92,94,
	        95,96,97,98,100,101,102,103,104,106,107,108,109,110,112,113,114,115,116,117,
	        118,119,120,121,121,122,123,123,124,124,125,125,125,126,126,126,126,126,127,
	        127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,
	        128,128,128,128,128,129,129,129,130,130,131,131,132,133,133,134,135,136,
	        137,138,139,140,141,142,144,145,146,147,148,150,151,152,153,154,156,157,158,
	        159,160,162,163,164,165,166,168,169,170,171,172,174,175,176,177,178,180,181,
	        182,183,184,186,187,188,189,190,192,193,194,195,196,198,199,200,201,202,204,
	        205,206,207,208,210,211,212,213,214,216,217,218,219,220,221,223,224,225,226,
	        227,229,230,231,232,233,235,236,237,238,239,241,242,243,244,245,247,248,249,
	        250,251,253,254,255};
*/
float InputShape(float userValue)
	{
		int iUserValue;

		iUserValue = (int)(userValue * 127);
		iUserValue += 127;
		iUserValue = inputShape[iUserValue];
		userValue = iUserValue - 127;
		userValue /= 127;
		return userValue;
	}

--SNIP--

ly = InputShape(stick->GetRawAxis(2)); 
ry = InputShape(stick->GetRawAxis(5));
__________________
"May the coms be with you"

Is this a "programming error" or a "programmer error"?

Reply With Quote
  #6   Spotlight this post!  
Unread 11-02-2013, 11:12
Arrowhead Arrowhead is offline
Registered User
AKA: Daniel Brown
FRC #1369 (1369 Minotaur)
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2013
Location: Tampa, Florida
Posts: 49
Arrowhead is an unknown quantity at this point
Re: Deadzone Programming

Quote:
Originally Posted by kylelanman View Post
We've found the following to work great for dead zones. The main advantage is you can still generate values between 0 and .3. With the previously posted solution once you get out of the dead zone you're instantly outputting .3 or 30%. If you notice we have a lot of 127s in the middle of the array. Those provide the dead zone but we shift everything else over so that once your out of the dead zone it doesn't go straight to .3.

This was written on the old control system. Technically the cRio has enough power to run the equation we are using to generate the array. We've just never taken the time to switch it over.

Code:
const char inputShape[255] = {0,1,3,4,5,6,7,9,10,11,12,13,15,16,17,18,19,21,22,23,24,25,27,28,29,30,31,
	        33,34,35,36,37,38,40,41,42,43,44,46,47,48,49,50,52,53,54,55,56,58,59,60,61,62,
	        64,65,66,67,68,70,71,72,73,74,76,77,78,79,80,82,83,84,85,86,88,89,90,91,92,94,
	        95,96,97,98,100,101,102,103,104,106,107,108,109,110,112,113,114,115,116,117,
	        118,119,120,121,121,122,123,123,124,124,125,125,125,126,126,126,126,126,127,
	        127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,
	        128,128,128,128,128,129,129,129,130,130,131,131,132,133,133,134,135,136,
	        137,138,139,140,141,142,144,145,146,147,148,150,151,152,153,154,156,157,158,
	        159,160,162,163,164,165,166,168,169,170,171,172,174,175,176,177,178,180,181,
	        182,183,184,186,187,188,189,190,192,193,194,195,196,198,199,200,201,202,204,
	        205,206,207,208,210,211,212,213,214,216,217,218,219,220,221,223,224,225,226,
	        227,229,230,231,232,233,235,236,237,238,239,241,242,243,244,245,247,248,249,
	        250,251,253,254,255};
*/
float InputShape(float userValue)
	{
		int iUserValue;

		iUserValue = (int)(userValue * 127);
		iUserValue += 127;
		iUserValue = inputShape[iUserValue];
		userValue = iUserValue - 127;
		userValue /= 127;
		return userValue;
	}

--SNIP--

ly = InputShape(stick->GetRawAxis(2)); 
ry = InputShape(stick->GetRawAxis(5));
Quick question abotu the code, where does the variable userValue come from?
Reply With Quote
  #7   Spotlight this post!  
Unread 11-02-2013, 11:35
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: Deadzone Programming

Quote:
Originally Posted by Arrowhead View Post
Quick question abotu the code, where does the variable userValue come from?
It comes from whatever line calls the InputShape() function.
Reply With Quote
  #8   Spotlight this post!  
Unread 11-02-2013, 13:27
Arrowhead Arrowhead is offline
Registered User
AKA: Daniel Brown
FRC #1369 (1369 Minotaur)
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2013
Location: Tampa, Florida
Posts: 49
Arrowhead is an unknown quantity at this point
Re: Deadzone Programming

Gah, wasnt paying attention lol
Reply With Quote
  #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
  #10   Spotlight this post!  
Unread 12-02-2013, 15:25
Arrowhead Arrowhead is offline
Registered User
AKA: Daniel Brown
FRC #1369 (1369 Minotaur)
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2013
Location: Tampa, Florida
Posts: 49
Arrowhead is an unknown quantity at this point
Re: Deadzone Programming

Thanks! Also, did you mean abs() instead of fabs()?
Reply With Quote
  #11   Spotlight this post!  
Unread 12-02-2013, 17:41
MathMaven MathMaven is offline
Registered User
AKA: Elisha Sword
FRC #3175 (Knight Vision)
Team Role: Programmer
 
Join Date: Jan 2011
Rookie Year: 2010
Location: Michigan
Posts: 50
MathMaven is on a distinguished road
Re: Deadzone Programming

abs() takes an int and returns an int. fabs() takes a float and returns a float.

Since Joystick::GetX() and GetY() return floats, he did mean fabs().
__________________
—Elisha Sword

2010–13: FRC Team 3175 (Knight Vision), student, lead programmer
2012–13: FRC Team 3175 (Knight Vision), lead website designer
2014: FRC Team 3175 (Knight Vision), programming mentor

“Who has not been amazed to learn that the function y = e^x, like a phoenix rising from its own ashes, is its own derivative?”
—Francois le Lionnais
Reply With Quote
  #12   Spotlight this post!  
Unread 12-02-2013, 18:01
Arrowhead Arrowhead is offline
Registered User
AKA: Daniel Brown
FRC #1369 (1369 Minotaur)
Team Role: Programmer
 
Join Date: Jan 2013
Rookie Year: 2013
Location: Tampa, Florida
Posts: 49
Arrowhead is an unknown quantity at this point
Re: Deadzone Programming

Ah, thanks for the correction.
Reply With Quote
Reply


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 13:34.

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