Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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:

PID loop image from Wikipedia

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.

share|improve this question
    
You can link to a picture and someone will helpfully convert that link into an actual picture for you. –  Joachim Sauer Feb 6 '13 at 16:14
2  
The link you yourself posted provides a basic pseudocode for going about it. If you don't mind C#, here is an example of a pid loop in C#. –  Neil Feb 6 '13 at 16:14
1  
Neil is right. I've implemented almost exactly that loop in C on TI. One hint: use a constant time loop, and factor the fixed dt into the constants, instead of doing extra divides and multiplies in the loop. –  AShelly Feb 6 '13 at 16:25
1  
@Neil that was a link that I added in revision 2 because I didn't know what a PID loop was and I suspected many others didn't either. –  MichaelT Feb 6 '13 at 16:32
    
@MichaelT, ah my apologies then. –  Neil Feb 6 '13 at 16:40

1 Answer 1

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 type you did. I believe you meant RC series circuit in parallel instead of RE ( if it is, i dont 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.

share|improve this answer
3  
The Ascii Diagram blew my mind. +1 –  l46kok Feb 7 '13 at 5:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.