×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Are you an
Engineering professional?
Join Eng-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Implementing a PID

## Implementing a PID

(OP)
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 -->

SP is set to the desired value
MV is set to a value that is expected to yield an a close result for PV
previous_time is set to the current time
Waiting for 1 second 

LOOP

#### CODE -->

current_time is set to the current time
PV is set to the value read on the sensor
Error is set to (SP - PBV)
dt is set to (current_time - previous_time)
integ is set to (integ + Error*dt)

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

previous_time is set to current_time
Waiting 1 second
Repeating the loop 

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

Naotake

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.

### RE: Implementing a PID

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.

### RE: Implementing a PID

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?

### RE: Implementing a PID

(OP)
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.

### RE: Implementing a PID

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

### RE: Implementing a PID

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.

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

#### Resources

White Paper - Considerations for choosing a 3D printing technology
The adoption of 3D printing into major companies’ product development life cycles is a testament to the technology’s incredible benefits to consumers, designers, engineers and manufacturers. While traditional production methods have limitations in manufacturability, 3D printing provides unparalleled design freedom due to the additive method of building parts layer by layer. Download Now
White Paper - Are all 3D printed metals the same?
With so many different 3D printing metal systems and processes today, how can you know exactly what you’re getting? Today, there are several different methods for manufacturing 3D printed metal components with their own strengths and limits. Download Now

Close Box

# Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!