View Single Post
  #8   Spotlight this post!  
Unread 24-07-2006, 22:02
Noah Kleinberg Noah Kleinberg is offline
Registered User
FRC #0395 (2TrainRobotics)
Team Role: Driver
 
Join Date: Jan 2006
Rookie Year: 2006
Location: New York
Posts: 196
Noah Kleinberg is a splendid one to beholdNoah Kleinberg is a splendid one to beholdNoah Kleinberg is a splendid one to beholdNoah Kleinberg is a splendid one to beholdNoah Kleinberg is a splendid one to beholdNoah Kleinberg is a splendid one to behold
Send a message via AIM to Noah Kleinberg
Re: Sample PID code???

Quote:
Originally Posted by EHaskins
1. I don't know how to retrieve the static variables after the function has processed them.
2. I would also like to create a structure to hold all the info passed to this function so if anyone has any advice on doing that I would appreciate it.
For your first question:

There are a few ways to do this (if you're referring to the local variables in the function).

One would be to make those variables global. This is the simplest way, but has the disadvantage of needing to copy the data from one channel before calculating data for a second channel.

A better way to do this would be, as you mentioned in your second question, to use a structure, and pass a pointer to it to the PID function.

Here's some code I wrote for this as an example (haven't tested it):

Code:
typedef struct
{
	int cOutput = 0;
	int cValue = 0; //current value
	int tValue = 0; //target value
	
	int cError = 0; //current error
	int lError = 0; //last error
	int sError = 0; //sum of the errors

	double pGain = 0;
	double iGain = 0;
	double dGain = 0;

	unsigned char flags = P|I|D;
} PID_Type;
The flags variable is used to determine which type of loop to use, and is not necessary(P I and D are globabl unsigned chars defined as 1, 2, and 4, respectively, the logical or '|' combines the bits); likewise, other variables like i_max that you used in your code could be added.

A pointer to a struct of this type is then passed to a PID function, like yours, and you would then do the same thing that you did but with member variables of the struct instead of local variables in the function and parameters.

The prototype for this function would look like:

void PID_Loop(PID_Type* object).

You could also pass the structure without using pointers (void PID_Loop(PID_Type object) ), but this would be less efficient because a second copy of the object would be made in memory, and also not as flexible because then the original structure cannot be modified by the function.

If you've never used structures before, they're a collection of variables; basically an array with named elements which can be off different sizes. They are defined with the keyword 'struct' before a list of variables in curly braces. The 'typedef' makes the struct a variable type, like any other type such as an int or a char, and then you can use 'PID_Type', for instance, in place of any other variable type.

Hope this was helpful, tell me if anything needs clarification.

Last edited by Noah Kleinberg : 24-07-2006 at 22:06.