dimanche 18 mai 2014

simulateur - circuits de conversion de C - Stack Overflow


Does anyone have an idea on how to convert a feedforeward opamp PID loop to C code? I am trying to do such a conversion and honestly, have no idea where to start. I can get all input values through an ADC, voltage, current, whatever, but coding a feedforeward PID is a little new to me. Any ideas?




You are talking about replacing hardware with software for a non-linear control system. I think your only hope is to write a simulation of the hardware.


I don't know anything about PIDs, but a quick Google search found this:


http://www.cds.caltech.edu/~murray/books/AM05/pdf/am06-pid_16Sep06.pdf


It has equations and graphs that appear to describe an ideal PID control system. You might start by writing code that implements those equations.


After I thought about your question a bit, it seemed to me that this might be a common problem. I did a Google search for "discrete PID controller simulation" and found Simulink, Matlab, and Python answers, as well as more references to books.


You might want to start with the Python recipe. Python is easier and faster to play around with than C, and if you use SciPy you can plot your results and make sure you are getting the numbers you want. Once you have it working in Python, then port to C if needed.


http://code.activestate.com/recipes/577231-discrete-pid-controller/




The Circuit


Seems like the analog circuit you want to simulate using C looks something like this


                         Ci
|------| |--------------|
| Rp |
|----/\/\/\/\-----------|
| Rd Cd |
Rf |----/\/\/\---| |-------|
Vin o----/\/\/\---| |
| |\ |
| | \ |
|----|- \ |
| \ |
| \-------------|---------o Vout
| /
| /
|+ /
----| /
| |/
|
|
___|___ GND
_____
___
_

LEGEND:
Vin is the input signal.
Vout is the Output.
Rp controls the propotional term ( P in PID)
Ci controls the Integral term ( I in PID)
Rd and Cd controls the differential term ( D in PID)
Rf is the gain control, which is common to all of the above controllers.

I strongly suggest you use the circuit from this source for studying. : Complete circuit diagram for PID controller


Even though a little tedious to set up, mathematically its much simpler to analyse as you can directly relate it to the standard mathematical form instead of ideal one.


Lastly the Vout goes to control a motor or whatever needs to be controlled. And Vin is the Process variable voltage.


Before getting your feet wet in C (sea?)


I assume you are reading the signals from some kind of analogue to digital converter. If not then you would have to simulate the signal as an input.


If using Standard form we have,


enter image description here


Assuming the the loop running time is small enough (a slow process), we can use the following function for calculating output,


PIDoutput = Kp * err + (Ki * int * dt) + (Kd * der /dt);

where


Kp = Proptional Constant.
Ki = Integral Constant.
Kd = Derivative Constant.
err = Expected Output - Actual Output ie. error;
int = int from previous loop + err; ( i.e. integral error )
der = err - err from previous loop; ( i.e. differential error)
dt = execution time of loop.

where initially 'der' and 'int' would be zero. If you use a delay function in code to tune the loop frequency to say 1 KHz then your dt would be 0.001 seconds.


Now the output of a feedforward system would be,


FeedForwardOutput = Kf * Vin;

where Kf = Propotional constant for feedforward system.


Thus the total output of our feedforward system with PID controller would be,


Output = FeedForwardOutput + PIDoutput;

Check this link for further reading about feedfoward system with PID controller.


Drawning in C


I found this excellent code for PID in C, though it doesn't cover every aspect of it, its a good one nonetheless.


//get value of setpoint from user
while(1){
// reset Timer
// write code to escape loop on receiving a keyboard interrupt.
// read the value of Vin from ADC ( Analogue to digital converter).
// Calculate the output using the formula discussed previously.
// Apply the calculated outpout to DAC ( digital to analogue converter).
// wait till the Timer reach 'dt' seconds.
}

If we take a slow process, then we can use lower frequency such that dt >>> code execution time for single loop ( far far greater than ). In such cases we can do away with timer and use a delay function instead.



Does anyone have an idea on how to convert a feedforeward opamp PID loop to C code? I am trying to do such a conversion and honestly, have no idea where to start. I can get all input values through an ADC, voltage, current, whatever, but coding a feedforeward PID is a little new to me. Any ideas?



You are talking about replacing hardware with software for a non-linear control system. I think your only hope is to write a simulation of the hardware.


I don't know anything about PIDs, but a quick Google search found this:


http://www.cds.caltech.edu/~murray/books/AM05/pdf/am06-pid_16Sep06.pdf


It has equations and graphs that appear to describe an ideal PID control system. You might start by writing code that implements those equations.


After I thought about your question a bit, it seemed to me that this might be a common problem. I did a Google search for "discrete PID controller simulation" and found Simulink, Matlab, and Python answers, as well as more references to books.


You might want to start with the Python recipe. Python is easier and faster to play around with than C, and if you use SciPy you can plot your results and make sure you are getting the numbers you want. Once you have it working in Python, then port to C if needed.


http://code.activestate.com/recipes/577231-discrete-pid-controller/



The Circuit


Seems like the analog circuit you want to simulate using C looks something like this


                         Ci
|------| |--------------|
| Rp |
|----/\/\/\/\-----------|
| Rd Cd |
Rf |----/\/\/\---| |-------|
Vin o----/\/\/\---| |
| |\ |
| | \ |
|----|- \ |
| \ |
| \-------------|---------o Vout
| /
| /
|+ /
----| /
| |/
|
|
___|___ GND
_____
___
_

LEGEND:
Vin is the input signal.
Vout is the Output.
Rp controls the propotional term ( P in PID)
Ci controls the Integral term ( I in PID)
Rd and Cd controls the differential term ( D in PID)
Rf is the gain control, which is common to all of the above controllers.

I strongly suggest you use the circuit from this source for studying. : Complete circuit diagram for PID controller


Even though a little tedious to set up, mathematically its much simpler to analyse as you can directly relate it to the standard mathematical form instead of ideal one.


Lastly the Vout goes to control a motor or whatever needs to be controlled. And Vin is the Process variable voltage.


Before getting your feet wet in C (sea?)


I assume you are reading the signals from some kind of analogue to digital converter. If not then you would have to simulate the signal as an input.


If using Standard form we have,


enter image description here


Assuming the the loop running time is small enough (a slow process), we can use the following function for calculating output,


PIDoutput = Kp * err + (Ki * int * dt) + (Kd * der /dt);

where


Kp = Proptional Constant.
Ki = Integral Constant.
Kd = Derivative Constant.
err = Expected Output - Actual Output ie. error;
int = int from previous loop + err; ( i.e. integral error )
der = err - err from previous loop; ( i.e. differential error)
dt = execution time of loop.

where initially 'der' and 'int' would be zero. If you use a delay function in code to tune the loop frequency to say 1 KHz then your dt would be 0.001 seconds.


Now the output of a feedforward system would be,


FeedForwardOutput = Kf * Vin;

where Kf = Propotional constant for feedforward system.


Thus the total output of our feedforward system with PID controller would be,


Output = FeedForwardOutput + PIDoutput;

Check this link for further reading about feedfoward system with PID controller.


Drawning in C


I found this excellent code for PID in C, though it doesn't cover every aspect of it, its a good one nonetheless.


//get value of setpoint from user
while(1){
// reset Timer
// write code to escape loop on receiving a keyboard interrupt.
// read the value of Vin from ADC ( Analogue to digital converter).
// Calculate the output using the formula discussed previously.
// Apply the calculated outpout to DAC ( digital to analogue converter).
// wait till the Timer reach 'dt' seconds.
}

If we take a slow process, then we can use lower frequency such that dt >>> code execution time for single loop ( far far greater than ). In such cases we can do away with timer and use a delay function instead.


0 commentaires:

Enregistrer un commentaire