PID with UNO - Hardware simulated feedback - Solved. #6

Using the following code to understand the PID implementation using an Arduino. Using the library by Brettbuergard and also read the excellent discussion here : Improving the Beginner’s PID – Introduction « Project Blog

I originally wanted to apply this to a proportional pressure control with feedback closed by a signal from a pressure sensor. But since the setup was not available immediately, i tried to simulate the setup with RC network based delay.. see the circuit below :

And the code is this :

#include <PID_v1.h>

//Define Variables we'll be connecting to
double Setpoint, Feedback, Output;

unsigned long printMs, printMsInterval = 1000;

//Specify the links and initial tuning parameters
PID myPID(&Feedback, &Output, &Setpoint, 2, 5, 1, P_ON_E, DIRECT); //P_ON_M specifies that Proportional on Measurement be used
//P_ON_E (Proportional on Error) is the default behavior

void setup()
{
  Serial.begin(9600);

  //initialize the variables we're linked to
  Feedback = analogRead(A5);
  Setpoint = analogRead(A0);

  //turn the PID on
  myPID.SetMode(AUTOMATIC);
  printMs = millis();
}

void loop()
{
  Feedback = analogRead(A5);
  Setpoint = analogRead(A0);

  uint32_t before, after;
  before = micros();
  myPID.Compute();
  after = micros();

  analogWrite(3, Output);

  if ( millis() - printMs > printMsInterval ) {
    printMs = millis();
    Serial.print("runtime: ");
    Serial.print(after - before);
    Serial.print(" SP: ");
    Serial.print(Setpoint);
    Serial.print(" FB: ");
    Serial.print(Feedback);
    Serial.print(" OUT: ");
    Serial.println(Output);
  }
}

Is there any other method to carry out the verification of PID functionality other than above ?
Has anyone used this library in a regular industrial application ?

Thanks

presumably you have a variable output that affects a sensor with some delay and your using PID to anticipate the delay to avoid overshoot.

it seems like an RC circuit will work to simulate the delay

but i’ll suggest implementing it in SW which allows changing the delay w/o needing to replace hardware components

i’m fond of leaky integration which is a simple low-pass filter

A += (s - A) * K where K <1

in general, if K = 1/N, “A” will reach (~98%) the value of “s” in 3*N iterations; very much like an RC circuit. However, K can be different if s > or < A; it can have different attack and decay rates which may be useful

your Feedback value would be A"" and analogRead (5) “s”.

gcjr:
presumably you have a variable output that affects a sensor with some delay and your using PID to anticipate the delay to avoid overshoot.

it seems like an RC circuit will work to simulate the delay

but i’ll suggest implementing it in SW which allows changing the delay w/o needing to replace hardware components

i’m fond of leaky integration which is a simple low-pass filter

A += (s - A) * K where K <1

in general, if K = 1/N, “A” will reach (~98%) the value of “s” in 3*N iterations; very much like an RC circuit. However, K can be different if s > or < A; it can have different attack and decay rates which may be useful

your Feedback value would be A"" and analogRead (5) “s”.

Looks a good option to eliminate the hardware simulation altogether…

Sorry but I must admit I have little idea how to implement this in my code. Aspects like what value of N to start with and how to integrate the formula into my code are not clear.

A little elaboration will help. Thanks.

instead of

 Feedback = analogRead(A5);
#define K   0.25
Feedback += (analogRead(A5) - Feedback) * K;

gcjr:
instead of

 Feedback = analogRead(A5);
#define K   0.25

Feedback += (analogRead(A5) - Feedback) * K;

OK .. so I directly link D3 which is the output to A5 instead of the RC network ?

sorry. you're right. no I/O at all

#define K   0.25
Feedback += (Output - Feedback) * K;

gcjr:
sorry. you’re right. no I/O at all

#define K   0.25

Feedback += (Output - Feedback) * K;

Great … now its clear. Shall checkout tomorrow when I get back to my board.

Thanks

Mogaraghu:
I originally wanted to apply this to a proportional pressure control with feedback closed by a signal from a pressure sensor. But since the setup was not available immediately, i tried to simulate the setup with RC network based delay

Unless you have checked that your hardware simulation accurately mimics the responsiveness of the real pressure control I suspect you won't learn very much that you could not have learned by reading.

There is some simple PID code in this link which may make the workings more obvious.

...R

Unless you have checked that your hardware simulation accurately mimics the responsiveness of the real pressure control I suspect you won’t learn very much that you could not have learned by reading.

how accurate does it need to be to debug and verify that the code runs properly with PID?

software simulations are quite common and sometime the only option until the code has been shown to be stable. You wouldn’t want to debug code on a heavy or expensive apparatus that could be or cause damage.

i feel that many programmers don’t take advantage of developing and testing their code in simulation which can often accelerate development.

of course there is a learning curve, which is often used as an excuse for not doing so, but the return on investment can be enormous

gcjr:
how accurate does it need to be to debug and verify that the code runs properly with PID?

software simulations are quite common and sometime the only option until the code has been shown to be stable.

My sense of things is along these lines ...

Why wouldn't the PID code work (in a very general sense of work)?

The complexity with PID code is choosing the factors to make it stable and that can only be established with the real apparatus or something that is known to provide a very accurate equivalent.

To my mind experimenting with PID on "the wrong thing" is like practising horse surgery on a dog. The bleeding is probably all they have in common.

But I may be wrong :slight_smile:

...R

of course the PID library code works, but his code may not be using it properly as well as there being other problems with his code and figuring out what information is needed to debug it

it certainly saves time to work out any problems in simulation before working with any apparatus, especially if it's unavailable.

and if you don't have much experience with PID, figuring out how the 3 coefficients affect the response for a variety of models is useful for the developers understanding.

and then after working with the apparatus and gaining a better understanding of the system, you can change your model to be representative of system and again work in simulation to tune parameters.

working in simulation is always so much faster. I often have one window repeatedly running make and executing code many cycles while modifying the code in another

i understand it's hard to appreciate this approach until you done it successfully.

gcjr:
and if you don't have much experience with PID, figuring out how the 3 coefficients affect the response for a variety of models is useful for the developers understanding.

I can see some value in that (though I am not sure how one would create the model that produces the simulation) but it is my understanding that the OP is proposing to simulate the real thing with some sort of electronic circuit and that is what my comments refer to.

...R

signal processing theory was developed to simulate the behavior of analog circuits. It was a lunch time conversation where someone wondered if the theory might ever be used in real-time to emulate the behaviors of hardware circuits in software. of course it became true.

i suggested he use an simple equation the effectively models the RC circuit he proposed. So it's a direct application of signal processing. and it can be expanded to account for more than 1st order effects

but of course, other types of hardware and software systems can also be modeled and simulated to speed development

Going by the last few interactions I thought it is better to clarify some basics :slight_smile:

When you work on hydraulic servo and proportional valves, you will stumble upon an aggressive PID implementation sooner than later. And so the subject is quite familiar and so far have been using the PID tool kit of LabVIEW which is quite a powerful one. And of course old systems ( circa 1970 ) used to have full analog implementation with the classic Setpoint , Process Variable summing junction followed by three op-amps handling the PID terms and finally a power stage to drive the valve.

So I was just wanting to checkout this implementation on a Arduino with its limited number crunching capabilities ( as compared to a 64 bit i5 CPU) and hence the trial. Sure, a simple RC network or a Software simulation will be some simplification. But then a hydraulic relief valve used to limit pressure based on a command signal can be very closely modeled just by varying the response delay. So this trial of mine will help to ,

  1. Check how fast or efficiently the UNO handles the PID loop.
  2. With a Setpoint in the form of a square wave of around 2Hz ( thats all the bandwidth I need ) i can tune the PID and observe the effect on a scope by matching the Setpoint Feedback.

I know PID will work. Just wanting to check how good it works with the library that i chose to use.

Thanks

Mogaraghu:
Going by the last few interactions I thought it is better to clarify some basics

If all that had been in your Original Post I would not have made any contribution to this Thread.

I got the distinct impression from your Original Post that you were just beginning to explore PID

...R

Robin2:
If all that had been in your Original Post I would not have made any contribution to this Thread.

I got the distinct impression from your Original Post that you were just beginning to explore PID

...R

Oh .. I am sorry if my post had conveyed a meaning as that.

I always value your responses and in particular your tutorials / support on using Serial and the RF24 modules.

Thanks.

I got the distinct impression from your Original Post that you were just beginning to explore PID

ironically, it's more experienced developers that appreciate and practice development using simulations because it's another skill that needs to be developed

gcjr:
ironically, it's more experienced developers that appreciate and practice development using simulations because it's another skill that needs to be developed

I do understand that, but as mentioned, my comments were based on the assumption that the OP was beginner.

...R

Unless you have checked that your hardware simulation accurately mimics the responsiveness of the real pressure control I suspect you won't learn very much that you could not have learned by reading.

so you think reading is more useful that attempting to simulate for a beginner?

i don't understand the point you're trying to make

gcjr:
so you think reading is more useful that attempting to simulate for a beginner?

i don't understand the point you're trying to make

In my head the point was / is very simple.

Mostly what confuses beginners to PID is choosing the correct values for the factors and they depend entirely on the characteristics of the device that is being controlled.

I cannot conceive of a situation in which a beginner might cobble together some electronic circuit that provides any useful basis for learning to control some device that is yet to be acquired.

The situation may be entirely different for an expert.

...R