I’m an electrical engineer that was kind of thrust into the digital world and learning as I go. I’m programming a TI processor to do a PID (proportional-integral-derivative) loop, illustrated by this diagram:
I’ll also describe it:
Negative feedback op-amp, with non-inverting terminal grounded. Input through negative terminal. The feedback loop is an RE series circuit in parallel with a resistor and all that in parallel with a cap.
Anyone have any idea how to convert this circuit to C code? I’m a bit out of my element on this and could use the help.
7
The Circuit
Ok i just created an account in here when i saw this question. Am not able to edit your question so that i could correct the typo you did. I believe you meant RC series circuit in parallel instead of RE ( if it is, I don’t have a single clue what it means)
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 id 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 couldn’t resist my urge to draw this since i wanted to tell you how electrical/electronics engineers used to communicate in forums and emails without images … and why we just love courier, fixed width font)
I must confess that the circuit you are using is simple to setup but is very complex mathematically, when it comes to tune the Propotional, Integral and Derivative constants of the system to a desired value individually its not possible.
I strongly suggest you use the circuit from this source for studying.
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,
Assuming the the loop running time is small enough (a slow process), we can use the following function for calculating output,
output = 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.
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.
4