![]() |
Sample PID code???
I have read the PID white paper on CD, and looked through the programming threads, but I can't seem to write a good PID function.
I was hoping someone would have a PID.c/.h file they could show me.I would appreciate any help. Thanks, Eric Haskins |
Re: Sample PID code???
Quote:
|
Re: Sample PID code???
I hate to sorta threadjack, but I'm also having problems with my PID code--I just can't get the thing to stop oscillating. I'll re-read over that whitepaper now that I have oodles of time, but I adjusted the terms according to the directions and got nowhere during the season. It works with minor disturbances, but seems to drift when it is sitting still, then detects the drift and reverses, into bigger and bigger oscillations.
I'll play with it probably next week, but right now I'm going to work on the camera code. JBot |
Re: Sample PID code???
I can write a simple PID, but I am having trouble when I try to write one to handle more then one output. I would like to be able to have the two/four drive outputs have one group of gains, and also have separate gains for a turret, thrower wheels... :ahh: .
Is this possible, and if anyone has done this can they show me how they did this. EDIT: My goal is to have 4-6 output channels. 1-2 would have the same gains. |
Re: Sample PID code???
Are you referring to my whitepaper? If so if you'd like to get in touch with me privately I'll try to help you out directly, or you can post your code here. I'm not quite clear on exactly what you're trying to accomplish, can you elaborate a bit more on it?
|
Re: Sample PID code???
Quote:
Or am I not understanding the question? |
Re: Sample PID code???
So this is what I have...
Code:
#include "pid.h"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. This function may not work properly I had written a single channel PID, but it was early this year and I can't find it. If you see any problems please tell me. Matt, I was referring to your white paper. Thanks, EHaskins |
Re: Sample PID code???
Quote:
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 structA 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. |
Re: Sample PID code???
Thanks I'll try that, but I have one more question. How do you send a pointer to a function? Do you place a & before the variable name like the code below?
Code:
Getdata(&rxdata); |
Re: Sample PID code???
Quote:
I recommend using one struct to hold the PID gains, static error variables, and output, and just pass a pointer to the structure when you call your function. This allows you to have multiple PID "objects" which you can then pull the data from it later on for whatever you need it for. Also, you might consider storing the gains in two unsigned chars each, one as a numerator and a denominator. In doing so you can store fractional math operations in 16 bits instead of 32 (or is it 64 on the PIC?) bit floating point math, and do it much faster An example: Code:
unsigned float p_gain = 0.5;Another example, with an error of 10: 10 * 0.5 = 5 10 * 1 / 2 = (10) / 2 = 5 See? Same thing! Using multiplication and division of integer numbers is easier on memory and processing time as the PIC doesn't have any hardware support for floating point math. This isn't perfect, any decimals on the final result will still be truncated, but in our situations this is hardly noticeable. If it's really that important, I suggest learning about fixed point math, its a compromise between my method and "true" floating point. I'll look over your function when I have some more time and see if I can recommend any improvements besides using a data structure. |
Re: Sample PID code???
Thanks for the help. If I have any other problems I'll put a post
here. |
Re: Sample PID code???
Quote:
make sure you remove your deadband around neutral on the victors, don't just rely on your accumulator to do it since it will be accumulating error that doesn't exist. |
Re: Sample PID code???
Quote:
Quote:
|
Re: Sample PID code???
Quote:
Code:
#define minimum 0 |
Re: Sample PID code???
Victor Deadband is 124-130, Inclusive. Other than that, power output is very linear. I've tested the victor all throughout its pwm range, and it has a nice linear output from 3-100% duty cycle. I like to run my servos (pid) on a 25ms interrupt timer, so i very rarely leave a deadband in my pid except for neutral, of course.
I'll be happy to answer any other questions you might have. -Q |
| All times are GMT -5. The time now is 18:12. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi