DSPRelated.com
Forums

PID Controller

Started by Unknown September 20, 2007
Hi

I have read some article and book on control design and PID. But I
never found how to implement it in software or how to derive an
algorithm for it.  Can someone help me at this? I just want to code an
algorithm from a block diagram of a controller.

Thanks!

You mean proportional integral differential?

 From analog to digital, you have to go to discrete time values. For eac 
sample, add a bit of the last sample for the integrator part, substract 
a bit of the last sample for the differentiator, and use a factor for 
the current sample for the p part. That should make you a simple control 
filter with 3 parameters (p, i, d) to play with.

Best regards,

Andre


reginald.louis@gmail.com wrote:
> Hi > > I have read some article and book on control design and PID. But I > never found how to implement it in software or how to derive an > algorithm for it. Can someone help me at this? I just want to code an > algorithm from a block diagram of a controller. > > Thanks! >

reginald.louis@gmail.com wrote:

> Hi > > I have read some article and book on control design and PID. But I > never found how to implement it in software or how to derive an > algorithm for it. Can someone help me at this? I just want to code an > algorithm from a block diagram of a controller.
f32 PID(f32 x) { const f32 P = FOO, I = BAR, D = BLABLABLA; static f32 i, d; i += x; f32 dx = x - d; d = x; return x*P + i*I + dx*D; } Incredible, isn't it? Vladimir Vassilevsky DSP and Mixed Signal Design Consultant http://www.abvolt.com
Vladimir Vassilevsky <antispam_bogus@hotmail.com> writes:

> reginald.louis@gmail.com wrote: > >> Hi >> I have read some article and book on control design and PID. But I >> never found how to implement it in software or how to derive an >> algorithm for it. Can someone help me at this? I just want to code an >> algorithm from a block diagram of a controller. > > f32 PID(f32 x) > { > const f32 P = FOO, I = BAR, D = BLABLABLA; > static f32 i, d; > > i += x; > f32 dx = x - d; > d = x; > > return x*P + i*I + dx*D; > }
What's an "f32"? -- % Randy Yates % "So now it's getting late, %% Fuquay-Varina, NC % and those who hesitate %%% 919-577-9882 % got no one..." %%%% <yates@ieee.org> % 'Waterfall', *Face The Music*, ELO http://home.earthlink.net/~yatescr
On Sep 20, 12:23 pm, Randy Yates <ya...@ieee.org> wrote:
> Vladimir Vassilevsky <antispam_bo...@hotmail.com> writes: > > reginald.lo...@gmail.com wrote: > > >> Hi > >> I have read some article and book on control design and PID. But I > >> never found how to implement it in software or how to derive an > >> algorithm for it. Can someone help me at this? I just want to code an > >> algorithm from a block diagram of a controller. > > > f32 PID(f32 x) > > { > > const f32 P = FOO, I = BAR, D = BLABLABLA; > > static f32 i, d; > > > i += x; > > f32 dx = x - d; > > d = x; > > > return x*P + i*I + dx*D; > > } > > What's an "f32"? > -- > % Randy Yates % "So now it's getting late, > %% Fuquay-Varina, NC % and those who hesitate > %%% 919-577-9882 % got no one..." > %%%% <ya...@ieee.org> % 'Waterfall', *Face The Music*, ELOhttp://home.earthlink.net/~yatescr
I would guess a "32-bit float." Jason

cincydsp@gmail.com wrote:


>>>>I have read some article and book on control design and PID. But I >>>>never found how to implement it in software or how to derive an >>>>algorithm for it. Can someone help me at this? I just want to code an >>>>algorithm from a block diagram of a controller. >> >>>f32 PID(f32 x) >> >>What's an "f32"? >>-- > > I would guess a "32-bit float."
Yes. And PID is a short for "pidor", which is the Russuan bad word for "pederast". Now you know all about PIDs; use them with care. VLV
On Sep 20, 10:26 am, Vladimir Vassilevsky <antispam_bo...@hotmail.com>
wrote:
> cincy...@gmail.com wrote: > >>>>I have read some article and book on control design and PID. But I > >>>>never found how to implement it in software or how to derive an > >>>>algorithm for it. Can someone help me at this? I just want to code an > >>>>algorithm from a block diagram of a controller. > > >>>f32 PID(f32 x) > > >>What's an "f32"? > >>-- > > > I would guess a "32-bit float." > > Yes. And PID is a short for "pidor", which is the Russuan bad word for > "pederast".
This reminds of Chekhov in Star Trek where Chekhov thinks everything was 'inwented' in Russia.
> Now you know all about PIDs; use them with care.
So far this thread looks like a case of the blind leading the blind. The OP should have done a search for PID code. He would have found John Shaw's and Tim Wescott's sites with PID examples. What is wrong with people that they can't do a simple search? Randy, remember this thread? http://groups.google.com/group/comp.dsp/browse_thread/thread/e34f1fded5a20817/1ee5fcb07221c9ec#1ee5fcb07221c9ec If one can't get or give decent answers then why bother? Here is a simple PID for a DSP. I first saw this MANY years ago in a TI DSP book. I think the example this code was written TMS320C14 appnote. e(n)=Target(n)-Actual(n) u(n) = u(n-1)+K0*e(n)+K1*e(n-1)+K2*e(n-2) K0 = Ki*T+Kp+Kd/T K1=-Kp-2*Kd/2 K2=Kd/T There are implementation problems. If the sample period T is very short then Kd/T can be a very big number and Ki*T very small so that the range of numbers required to accurately represent K0,K1 and K2 exceeds 16 bits. I recommend that people read the appnotes in the TI, Freescale and AD libraries. The DSP chip maker are trying to make using their chips as easy possible. Get the hint? I know I am a fuddy duddy, stick and the mud etc. Peter Nachtwey
On Sep 21, 6:24 am, pnacht...@gmail.com wrote:
> On Sep 20, 10:26 am, Vladimir Vassilevsky <antispam_bo...@hotmail.com> > wrote:> cincy...@gmail.com wrote: > > >>>>I have read some article and book on control design and PID. But I > > >>>>never found how to implement it in software or how to derive an > > >>>>algorithm for it. Can someone help me at this? I just want to code an > > >>>>algorithm from a block diagram of a controller. > > > >>>f32 PID(f32 x) > > > >>What's an "f32"? > > >>-- > > > > I would guess a "32-bit float." > > > Yes. And PID is a short for "pidor", which is the Russuan bad word for > > "pederast". > > This reminds of Chekhov in Star Trek where Chekhov thinks everything > was 'inwented' in Russia. > > > Now you know all about PIDs; use them with care. > > So far this thread looks like a case of the blind leading the blind. > > The OP should have done a search for PID code. He would have found > John Shaw's and Tim Wescott's sites with PID examples. What is wrong > with people that they can't do a simple search? > > Randy, remember this thread?http://groups.google.com/group/comp.dsp/browse_thread/thread/e34f1fde... > If one can't get or give decent answers then why bother? > > Here is a simple PID for a DSP. I first saw this MANY years ago in a > TI DSP book. I think the example this code was written TMS320C14 > appnote. > e(n)=Target(n)-Actual(n) > u(n) = u(n-1)+K0*e(n)+K1*e(n-1)+K2*e(n-2) > K0 = Ki*T+Kp+Kd/T > K1=-Kp-2*Kd/2 > K2=Kd/T > > There are implementation problems. If the sample period T is very > short then Kd/T can be a very big number and Ki*T very small so that > the range of numbers required to accurately represent K0,K1 and K2 > exceeds 16 bits. > > I recommend that people read the appnotes in the TI, Freescale and AD > libraries. The DSP chip maker are trying to make using their chips as > easy possible. Get the hint? > > I know I am a fuddy duddy, stick and the mud etc. > > Peter Nachtwey
A pure differentiator is never good - you need a filter on it or a phase-lead. Often you can use more than one integrator provided you stabilise the loop properly. PID is just a quick fix but not the best solution. For the best solution you need a Bode plot and design lag- lead compensators along with integrators.
On Thu, 20 Sep 2007 13:19:15 -0000, reginald.louis@gmail.com wrote in
comp.dsp:

> Hi > > I have read some article and book on control design and PID. But I > never found how to implement it in software or how to derive an > algorithm for it. Can someone help me at this? I just want to code an > algorithm from a block diagram of a controller. > > Thanks!
If you type "PID algorithm source code" into Google, it will come up with over 2,000,000 hits. Something I did not know before, honest, is that the very first one is a reference to PID source code on my website from the chapter I wrote in the book "C Unleashed" in 2000. The code is available on my site because it is all GPL. The text of the chapter is not there, that belongs to the publisher. -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://c-faq.com/ comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
<shiitshite@yahoo.co.uk> wrote in message 
news:1190320399.608982.305930@v23g2000prn.googlegroups.com...
> On Sep 21, 6:24 am, pnacht...@gmail.com wrote: >> On Sep 20, 10:26 am, Vladimir Vassilevsky <antispam_bo...@hotmail.com> >> wrote:> cincy...@gmail.com wrote: >> > >>>>I have read some article and book on control design and PID. But I >> > >>>>never found how to implement it in software or how to derive an >> > >>>>algorithm for it. Can someone help me at this? I just want to code >> > >>>>an >> > >>>>algorithm from a block diagram of a controller. >> >> > >>>f32 PID(f32 x) >> >> > >>What's an "f32"? >> > >>-- >> >> > > I would guess a "32-bit float." >> >> > Yes. And PID is a short for "pidor", which is the Russuan bad word for >> > "pederast". >> >> This reminds of Chekhov in Star Trek where Chekhov thinks everything >> was 'inwented' in Russia. >> >> > Now you know all about PIDs; use them with care. >> >> So far this thread looks like a case of the blind leading the blind. >> >> The OP should have done a search for PID code. He would have found >> John Shaw's and Tim Wescott's sites with PID examples. What is wrong >> with people that they can't do a simple search? >> >> Randy, remember this >> thread?http://groups.google.com/group/comp.dsp/browse_thread/thread/e34f1fde... >> If one can't get or give decent answers then why bother? >> >> Here is a simple PID for a DSP. I first saw this MANY years ago in a >> TI DSP book. I think the example this code was written TMS320C14 >> appnote. >> e(n)=Target(n)-Actual(n) >> u(n) = u(n-1)+K0*e(n)+K1*e(n-1)+K2*e(n-2) >> K0 = Ki*T+Kp+Kd/T >> K1=-Kp-2*Kd/2 >> K2=Kd/T >> >> There are implementation problems. If the sample period T is very >> short then Kd/T can be a very big number and Ki*T very small so that >> the range of numbers required to accurately represent K0,K1 and K2 >> exceeds 16 bits. >> >> I recommend that people read the appnotes in the TI, Freescale and AD >> libraries. The DSP chip maker are trying to make using their chips as >> easy possible. Get the hint? >> >> I know I am a fuddy duddy, stick and the mud etc. >> >> Peter Nachtwey > > A pure differentiator is never good - you need a filter on it or a > phase-lead.
Why? Never is a big word. I am not saying I disagree, I just think you should justify your statement.
> Often you can use more than one integrator provided you > stabilise the loop properly.
Can you provide an example?
> PID is just a quick fix but not the best > solution.
Why? I still use PIDs very effiectively. I sometimes add feed forwards, a 2nd derivative and/or an output filter. I would love to see your solution for the Bode plot below.
> For the best solution you need a Bode plot and design lag- > lead compensators along with integrators. >
Fine, where is your example filter? I provided one. How do you get the Bode plot? Don't worry, I have one. ftp://ftp.deltacompsys.com/public/NG/Bode%20Plot.rtf What do you do with the Bode plot? How do you use it to determine the filter and the coefficients? What is your best solution? Peter Nachtwey