Go to Post I didn't say this! - Max Lobovsky [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 10-01-2008, 15:27
bronxbomber92 bronxbomber92 is offline
Registered User
FRC #1551 (Grapes of Wrath)
Team Role: Programmer
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Naples
Posts: 75
bronxbomber92 is an unknown quantity at this point
PID question

I've seen several PID sources posted on the forum, and all of them divide the proportional output by 100, the integral output by 1000 and the derivative output by 10. Then the return value of the PID goes something like this:

Code:
return Limit_Mix(2000 + 127 + P + I - D);
Why do this? Why not just set the gains for P, I, and D to something that'll work within the range of 0 to 255 and the integral max to 255 and min to 0? I don't quite understand where the 100, 1000, and 10 came from either? Could someone please explain this?

Thanks,
Jedd

Edit - Another question? How do most people find reasonable constants for the gains of P, I and D?

Last edited by bronxbomber92 : 10-01-2008 at 15:31.
  #2   Spotlight this post!  
Unread 10-01-2008, 15:30
Jared Russell's Avatar
Jared Russell Jared Russell is offline
Taking a year (mostly) off
FRC #0254 (The Cheesy Poofs), FRC #0341 (Miss Daisy)
Team Role: Engineer
 
Join Date: Nov 2002
Rookie Year: 2001
Location: San Francisco, CA
Posts: 3,082
Jared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond repute
Re: PID question

We do this to gain precision and avoid overflow.

At least I do
  #3   Spotlight this post!  
Unread 10-01-2008, 16:19
lukevanoort lukevanoort is offline
in between teams
AKA: Luke Van Oort
no team
 
Join Date: Oct 2005
Rookie Year: 2005
Location: Waterloo, ON, Canada
Posts: 1,873
lukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond repute
Send a message via AIM to lukevanoort
Re: PID question

I don't think it is realistically possible to choose constants that keep the output between 0 and 254. As for the division thing, it lets you get decimal precision without making the uC do floating point operations, which it is very bad at. (ex 34/100 is the same as .34, but the robot executes it much faster)

To find gains, we just make the setpoint something near the middle of the mechanism's travel (to keep oscillations from damaging the mechanism). Then we link the P, I, and D constants to an analog input on the OI. After that we twiddle the knobs in this order: increase P until it gets near the setpoint, increase I until it reaches the setpoint, and finish by increasing D until any oscillations present are damped. We then use the numbers determined this way as our constants. It actually takes very little time to do it this way, I think it took us maybe 20 minutes to tune three loops last year.
__________________
Team 1219: 2009 - Mentor
Team 587: 2005 - Animator, 2006-2008 - Team Captain

Last edited by lukevanoort : 10-01-2008 at 16:41.
  #4   Spotlight this post!  
Unread 10-01-2008, 16:31
bronxbomber92 bronxbomber92 is offline
Registered User
FRC #1551 (Grapes of Wrath)
Team Role: Programmer
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Naples
Posts: 75
bronxbomber92 is an unknown quantity at this point
Re: PID question

Ah, gotcha. Thanks.

One more thing. The code posted in this thread: http://www.chiefdelphi.com/forums/sh...ad.php?t=56663 will only return a value between 0 and 255, right? It wouldn't be suitable for use with a potentiometer(?).
  #5   Spotlight this post!  
Unread 10-01-2008, 16:40
lukevanoort lukevanoort is offline
in between teams
AKA: Luke Van Oort
no team
 
Join Date: Oct 2005
Rookie Year: 2005
Location: Waterloo, ON, Canada
Posts: 1,873
lukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond repute
Send a message via AIM to lukevanoort
Re: PID question

I don't see any complete PID implementations in that thread... I would recommend reading these two documents if you intend to code a PID loop: Matt Krass's whitepaper, and PID without a PhD. If you want to get a little more advanced you can search around the forum to find out how to go about writing generalized PID functions. (PID without a PhD actually uses a generalized function, but I don't think it talks about it specifically)
__________________
Team 1219: 2009 - Mentor
Team 587: 2005 - Animator, 2006-2008 - Team Captain
  #6   Spotlight this post!  
Unread 10-01-2008, 17:01
bronxbomber92 bronxbomber92 is offline
Registered User
FRC #1551 (Grapes of Wrath)
Team Role: Programmer
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Naples
Posts: 75
bronxbomber92 is an unknown quantity at this point
Re: PID question

Quote:
Originally Posted by lukevanoort View Post
I don't see any complete PID implementations in that thread... I would recommend reading these two documents if you intend to code a PID loop: Matt Krass's whitepaper, and PID without a PhD. If you want to get a little more advanced you can search around the forum to find out how to go about writing generalized PID functions. (PID without a PhD actually uses a generalized function, but I don't think it talks about it specifically)
Sorry about that, wrong thread. Here's the correct one: http://www.chiefdelphi.com/forums/sh...ad.php?t=55025

I have written an attempt at a generic PID function (rough draft of it at least).
Code:
typedef char bool;
#define false 0
#define true !false

/////////////////////////////
// PID info struct - state //
/////////////////////////////
typedef struct
{
	
	int kpGain; /* Propotional gain */
	int kiGain; /* Integral gain */
	int kdGain; /* Derivative gain */
	int previousError; // Previous cycles error */
	int sumError; /* Sum of each cycles' error */
	int errorTolerance; /* How small can the error be until we're done */
	int iMax; /* Maximum integral output */
	int iMin; /* Minimum integral output */
	bool finished; /* Signifies if the desired target has been reached */
} PID;

void InitPID( PID* pid, int pGain, int iGain, int dGain, int imin, int imax, int et );
int PID( PID* pid, int error );

void InitPID( PID* pid, int pGain, int iGain, int dGain, int imax, int imin, int et )
{
	pid->kpGain = pGain;
	pid->kiGain = iGain;
	pid->kdGain = dGain;
	pid->iMin = imin;
	pid->iMax = imax;
	pid->errorTolerance = et;
	pid->finished = false;
}

int PID( PID* pid, int error )
{
	int P, I, D, errorDelta;
	
	P = pid->kpGain * error;
	pid->sumError += error;
	
	if( pid->sumError > pid->iMax )
		pid->sumError = pid->iMax;
	else if( pid->sumError < pid->iMin )
		pid->sumError = pid->iMin;
		
	I = pid->kiGain * pid->sumError;
	
	errorDelta = error - pid->previousError;
	D = pid->kdGain * errorDelta;
	
	if( errorDelta == 0 && error < pid->errorTolerance && error > -pid->errorTolerance )
		pid->finished = true;
	else
		pid->finished = false;
	
	pid->previousError = error;
	
	return P + I - D;
}
My version would allow me to use a potentiometer with it, wouldn't? While the other one in the above thread could be used with motors and such?

Edit - I'm pretty confident it works, but I don't know for sure. I need to make a test case for it (I don't have access to the robot), but I don't know how to really. Does anyone know how to setup an example?

Last edited by bronxbomber92 : 10-01-2008 at 19:28.
  #7   Spotlight this post!  
Unread 10-01-2008, 20:34
lukevanoort lukevanoort is offline
in between teams
AKA: Luke Van Oort
no team
 
Join Date: Oct 2005
Rookie Year: 2005
Location: Waterloo, ON, Canada
Posts: 1,873
lukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond reputelukevanoort has a reputation beyond repute
Send a message via AIM to lukevanoort
Re: PID question

It looks good at first glance, but you may want to do the dividing trick (especially on I, that usually ends up as a quite small constant) to improve accuracy/speed. I would also make sure to limit the output to 0-254... I can see no way that you can be certain not to exceed the 0-254 range. If you do, the consequences can be dire for both your robot's mechanical well-being, and bystanders' (ask Billfred what a robot arm can do if you're too close... you really don't want to get an uppercut from your robot). A quick Limit_Mix() call is cheap insurance against accidentally causing major robot damage and/or bodily harm.
__________________
Team 1219: 2009 - Mentor
Team 587: 2005 - Animator, 2006-2008 - Team Captain
  #8   Spotlight this post!  
Unread 11-01-2008, 00:23
Uberbots's Avatar
Uberbots Uberbots is offline
Mad Programmer
AKA: Billy Sisson
FRC #1124 (ÜberBots)
Team Role: College Student
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Avon
Posts: 739
Uberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond reputeUberbots has a reputation beyond repute
Re: PID question

I added the divide by x's in there to increase precision... basically this robot controller cannot do floating point calculations, so i had to tell the controller to divide by something to get some amount of precision.

when i divide by ten, you are essentially getting precision of control to 1 decimal place, 100 is two, and 1000 is three. kI is divided by 1000 because typically very small changes in that constant can have a very large impact on the system. likewise, larger changes on kD wont have such a great impact.

and yes, the function is limited to 0-255.
__________________
A few of my favorite numbers:
175 176 177 195 230 558 716 1024 1071 1592 1784 1816
RPI 2012
BREAKAWAY
  #9   Spotlight this post!  
Unread 12-01-2008, 00:07
bronxbomber92 bronxbomber92 is offline
Registered User
FRC #1551 (Grapes of Wrath)
Team Role: Programmer
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Naples
Posts: 75
bronxbomber92 is an unknown quantity at this point
Re: PID question

Thanks a lot! Totally clear on PID now. Hopefully finding the magic gain constants won't be too difficult
Closed Thread


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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Potentiometer PID DustinB_3 Programming 28 15-02-2008 23:18
PID Control John Gutmann Programming 12 15-04-2007 18:52
PID Troubleshooting Disar Programming 4 13-09-2006 15:27
PID shadowops06 Programming 5 16-06-2006 22:48
PID control loop/Encoder question Zee Programming 18 30-01-2004 23:14


All times are GMT -5. The time now is 00:55.

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