CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How to run a PID?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
BM92



Joined: 04 Oct 2013
Posts: 18

View user's profile Send private message

How to run a PID?
PostPosted: Tue Oct 22, 2013 9:44 am     Reply with quote

Ok so i have coded a PID using a very good tutorial online. but i am wondering what iis the best way for me to use it.

Am i able to leave the function outside of the main loop and call the function inside the main loop

eg.

Code:
main
{
  while(1)
    {
      PID();
    }
}



Or is this not the correct way to run it? Also would the PID code need to use and interrupt to run or will it just run when it has data available to use
Ttelmah



Joined: 11 Mar 2010
Posts: 19553

View user's profile Send private message

PostPosted: Tue Oct 22, 2013 11:41 am     Reply with quote

Key thing normally is that 'it depends'....

For a motor PID. you need responses to be quick, but you also need a finite number of updates to the position. You also need to ensure that the position does not update during the calculation.
So typically perhaps you would synchronise the PID to the PWM, perhaps after 8 cycles, and copy the new values into local variables with the interrupts disabled for brief moment, or 'double copy' if they change while being transferred, and then perform the maths. Ideally use scaled integer, rather than float.
I did a PID some time ago, that required quick updates, and used 24bit integers (my own library), scaled by even binary multiples, to get the maths speed fast.

However for something like a heater PID, the update speed can be much slower, so fp maths can be used. Much simpler, but you still need to ensure the values do not change during the calculations.

So, 'it depends'.....

Best Wishes
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Tue Oct 22, 2013 12:27 pm     Reply with quote

When I implemented the PID controller for my little heater (keeps the hummingbird feeder from freezing Laughing ), I used an "average" of the last 8 readings of both the ambient and heat plate temperatures to make sure one wrong reading would not drive the controller crazy. With something like a heater that has a relatively long time constant, you can update every second or more, but you do have to address the response time of your system - rudder position on a oil tanker is somewhat slower than the flight controls on a supersonic aircraft. The trick is to adjust the update rate as well as the constants in the control so it responds the way you want it to - typically comes go the value you want it to without either a long delay or overshoot etc. That is part of the tuning process. My preference for what I do is to have an interrupt running that triggers the updates although I keep the calculations outside the interrupt routine - it simply updates a counter that I use to determine when the next cycle should begin.

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
BM92



Joined: 04 Oct 2013
Posts: 18

View user's profile Send private message

PostPosted: Tue Oct 22, 2013 12:32 pm     Reply with quote

I currently have not been given the specific purpose of this code as i am new to the project and this is just the "training" if you like.

Its for a scientific project which will be receiving data to and ADC. This data will then be entered into a PID and be sent back out through a DAC. This is all i have been told to do so far.

If i want to just use the data as it arrives would the method i have stated work ok or is it simply too difficult to say until i am given more information?
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Tue Oct 22, 2013 3:22 pm     Reply with quote

Without knowing the data rate, type of data and response times needed, that is sort of like telling someone to "accurately predict the number of hours that will be needed to handle unforeseen problems during the program". If you at least have a clue as to the data rate/type and response times needed, you can start to think about the hardware and software you will need. Maybe part of the "training" is to see if you have enough experience to go back and push on them for some basic requirements???

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
BM92



Joined: 04 Oct 2013
Posts: 18

View user's profile Send private message

PostPosted: Tue Oct 22, 2013 3:44 pm     Reply with quote

Unfortunately the person for me to contact has not been available due to sickness. so was trying to get as far as possible with the little info i have. Thanks for the response though.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Wed Oct 23, 2013 1:41 am     Reply with quote

PID control has to be tuned as a system. There are something like "universal" PID controllers for real world applications, but they all need to be set up - update rate, P, I and D terms - and adjusted for the system in which they are used. PID control works best when the update rate is well-defined as some of the terms are time-related. Though slower systems might well tolerate quite a lot of sampling/computation jitter.

You could write a fairly generic controller which had settable update rate and the three terms, but it would have to use floating point and would necessarily be slow. Faster PID, at least on PICs, involves using carefully scaled fixed point (i.e. scaled integer) arithmetic. I doubt a usefully generic, fully adjustable and FAST PID controller is practical on most PICs; you need more processor power. So, is that what you're being expected to implement? Or is part of the "training" that you are expected to insist on clarification of the requirements?
Ttelmah



Joined: 11 Mar 2010
Posts: 19553

View user's profile Send private message

PostPosted: Wed Oct 23, 2013 3:08 am     Reply with quote

Yes.

I think some remarks:

As RF_Developer says 'generic' PID's, will still require tuning.

To do anything approaching a generic PID, you'd have to use fp maths. Now on fast processors with a hardware maths co-processor, this can be quick enough, but on the PIC, except for 'slow' systems, this is unlikely to be the case.

Once you switch to using integer, the limitations rise, with you having not only to scale the factors themselves, but the choice of sampling time etc..

Also, don't get hooked on 'PID'. Though this is the starting point for most control algorithms in practice it is much more rarely used than you may think!. 'Variants' become the order of the day once in the real world, with often secondary control loops outside the basic control core. I've done ones using PD as the core, followed by I in a slower external loop, and even P^2D, with two external loops outside this to allow the system to compensate for more factors. Unless you are very lucky, and responses are symmetrical in the system itself, at the very least you may well find yourself having to adjust the factors according to the direction of motion (common in systems where gravity or spring loading makes the motion 'dissimilar' in different directions...

I'm afraid that without significant data, the core PID is not going to be much use....

Best Wishes
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

Re: How to run a PID?
PostPosted: Wed Oct 23, 2013 4:24 am     Reply with quote

To refer back to the original question:
BM92 wrote:
Also would the PID code need to use and interrupt to run or will it just run when it has data available to use


PID needs to sample and compute at fixed intervals; "whenever data is available" won't cut it, generally. The P part is not a problem, but the I and D terms are time sensitive. Slow systems will only need infrequent updates and may tolerate quite a lot of timing error.

To get good timing doesn't necessarily mean having to use interrupts directly, or even at all. Even if interrupts are required to give known update intervals, its generally not a good idea to have all the processing of the loop itself in an interrupt routine. For many systems a clock tick interrupt with the main loop doing the computations when the tick reaches the next update point will be more than good enough.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Oct 23, 2013 4:48 am     Reply with quote

Why waste time waiting for your tutor.

Get a few cheap bits, PIC, resistor, thermistor, etc, and throw together a crude temperature control system.
Experiment to find out first hand what the real issues are.
Then, when you're given your task, you'll be well up the learning curve.

Mike
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed Oct 23, 2013 1:19 pm     Reply with quote

a quick comment about PID

not all servos require the I term, as for certain designs like narrow range
temperature controls and large rotor-mass, low speed motors -
the physical/load parameters of the system may provide all the I you can handle. ( and then some!)
the time constant for 'D' and
time between samples becomes a dominant factor

And in other sorts of systems, the D term may be counter productive.
an electrically regulated pendulum system comes to mind in the second case.

PID is not a magic bullet, unless shooting your foot is the target Very Happy Very Happy Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group