Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Implementing a PID

Status
Not open for further replies.

Naotake

Materials
Nov 29, 2012
2
Dear All,

I am a researcher scientist who knows nothing about PID except from what I did read on this forum and on a few sources from the web these past two weeks. For my research, I need to program a PID and I am seeking help to write the algorithm.

Here's what I am planning to do:

Variables
SP => requested output for A
PV => actual output of A measured by a sensor
MV => actual value applied to A's input
current_time => value of a timer at the start of the current loop
previous_time =>value of the timer a the start of the previous loop

Kp, Ki, Kd => pid configuration constants
integ => integration storage variable


Before starting the PID:
MV is set to 0 so the output of A is 0 and the measured value (PV) is also 0.
Kp, Ki, Kd are user-defined

Initialization
Code:
[b]SP[/b] is set to the desired value
[b]MV[/b] is set to a value that is expected to yield an a close result for [b]PV[/b]
[b]previous_time[/b] is set to the current time
Waiting for 1 second

LOOP
Code:
[b]current_time[/b] is set to the current time
[b]PV[/b] is set to the value read on the sensor
[b]Error[/b] is set to (SP - PBV)
[b]dt[/b] is set to ([b]current_time[/b] - [b]previous_time[/b])
[b]integ[/b] is set to ([b]integ[/b] + [b]Error[/b]*[b]dt[/b])

[b]MV[/b] is set to [b]Kp*Error[/b] + [b]Ki*integ[/b] + [b]Kd*(Error / dt)[/b]

[b]previous_time[/b] is set to [b]current_time[/b]
Waiting 1 second
Repeating the loop

According to you, could this be a functional implementation of a PID ?

Thanks for your replies,
Naotake

More informations on the process
I need to control a process (let's call it A) that has the usual characteristics. A takes an input (MV) and provides an output (PV) that should ideally be equal to MV. PV is measured by a sensor.
When changing MV, the response of A is immediate, but because of the sensor's characteristics, it can take from 1 to 30 seconds for PV to reach a stable value (30 seconds for really extreme changes on MV).
Unfortunately, thought the process's (A) response is immediate, there is a small decrease in time of its output after a change in MV (typically less than 10%). This evolution (drift) takes place over the course of 5-10 minutes and is proportional to the difference between the previous value of MV and the new one. So the moment this is the most noticeable is when the PID has just been started, and when the operator requests a new set point.

 
Replies continue below

Recommended for you

Welcome to eng-tips. Before getting too deep in your question, I would like to recommend the following article to you, titled: PID without a PhD It is one of the best practical descriptions of a PID and how to implement one in simple terms that I have seen.
 
This question about sensor response time has been asked rather coincidentally in the last month. Is this a school problem?

What is with this using crappy sensors that don't follow the process variable? And if your sensor is that crappy, how do you even know that the process response is "immediate," and why don't you use the sensor that tells you that the process is fast as the process sensor?

TTFN
faq731-376
7ofakss
 
Thank you very much for your replies.
Noway2, the document is really helpful, thank you very much. I still need to learn a lot more to fully understand it. My current concerns are about setting the PID right for my particular system, which is far from ideal.

IRstuff, I can assure you it's not a school project. As I mentioned, I am researcher scientist (Associate Professor to be more accurate). I need to develop this PID for my research.
The physical parameter that is tracked is not easy to follow… so, yes, I agree that the sensor's characteristics look crappy. The process's response time is near immediate, because physics dictate it. I really can't tell more whiteout disclosing too much. But I could tell you more by e-mail without the rest of the world reading it. But trust me, all the relevant details are exposed here.

I can however explain more about the drift in the process with an example.
For each MV, there is a PV which is reproducible and corresponds to an equilibrium of the process. However, changing MV changes this equilibrium and PV does drift during the time it takes for the system to reach an new equilibrium. That's the price to pay for having an immediate response.
Let's imagine that setting MV to 0 gives a PV of 0. Setting MV to 50 gives a PV of 40. Relationship is proportional. Then, we can guess that setting MV to 100 should give a PV of 80 (immediate). And that's what actually happens. However, during the next few minutes, there will be a drift occurring, because 80 is not the equilibrium value, and PV will eventually stabilize around 74.
Then, setting MV to 50 again, will give a PV of 37 (immediate) that will evolve to become 40 again.

I have also been considering removing P & I altogether because I can predict, with really decent accuracy which value of MV should be applied for a particular SP. So I could get there first, and then start regulating proportionally to compensate for any slow drift that may occur.

I am still experimenting with it and any thought is helpful to me.
Thank you again for your replies.


 
You might consider something like a Kalman Filter. Maximum likelihood vs. linear regression.

TTFN
faq731-376
7ofakss
 
In a system where the response is very well behaved with respect to the controlled variable (MV above) a feature called feed forward is often used. It will improve the overall response of the control loop. Feed forward applies a portion of the setpoint SP directly to the output.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor