EmbeddedRelated.com
Forums

PI controller algorithm

Started by cerr April 28, 2011
On Apr 28, 4:26=A0pm, cerr <ron.egg...@gmail.com> wrote:
> > Read this:http://www.wescottdesign.com/articles/Sampling/pidwophd.html > > This article is really helpful and for sure the best of all I read > today (and that was quite a few). > Thank you for that, I rewrote the code and came up with below which > should look better than what I had before: > > typedef struct > { > =A0 signed float iState; > =A0 float iMax, > =A0 =A0 =A0 =A0 =A0 =A0 iMin; > > =A0 float iGain, > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 pGain; > > }SPid; > > SPid CntrlData; > CntrlData.iGain=3D1; > CntrlData.pGain=3D1; > CntrlData.iMax=3D50; > CntrlData.iMin=3D0; > > signed int8 error=3D0; > int8 target=3D150; > > error =3D adcval() - target; //adcval() will return 8bit value > CntrlLEDs(&CntrlData, error); > //-----------------------------------------------------------------------=
-- ----
> signed float CntrlLEDs(Spid *pid, signed int8 error) > { > signed float pTerm,iTerm; > > /** P **/ > pTerm =3D pid->pGain * error; > > /** I **/ > pid->iState +=3D error; > > if (pid->iState > pid->iMax) > =A0 pid->iState=3Dpid->iMax; > else if (pid->iState < pid->iMin) > =A0 pid->iState=3Dpid->iMin; > > iTerm =3D pid->iGain * pid->iState; > > return pTerm+iTerm; > > } > > Thanks again for your help and your article sure is a keeper!
Well, I'll use above PI controller for now, built it into my software but can't test it yet as i don't have the hardware available yet... any one know of a free controller tune application where i an enter my possible values and maybe even let it run with my code...? Thanks, Ron
On Apr 29, 3:40=A0am, Paul <p...@pcserviceselectronics.co.uk> wrote:
> In article <ipclva$qq...@speranza.aioe.org>, not.going.to...@seen.com > says... > > > > > Hi Ron, > > > On 4/28/2011 1:32 PM, cerr wrote: > > > I need to resolve following problem: > > > I have an led panel with an opto transistor on it that measures the > > > light emitted/reflected from the target. I want to control the emitte=
d
> > > light to be equal to a certain setpoint from what the opto transistor > > > measures. Does that make sense? I'm using an 18F pic to control the > > > whole circuit and I wanted to come up with a PI if not a PID > > > controller algorithm that controls my LEDs. I've been reading about > > > PID controllers but am not 100% sure about some things. So far I've > > > come up with below code: > > ...... > > > Also, depending on what you are controlling and how it is likely > > to *fail*, you might want to put some anti-windup protection on > > your integral term to keep "Integrator" from becoming *so* > > large (positive or negative) that it dominates the loop's > > performance for an inordinately long time. =A0E.g., imagine using > > "doubles" for these variables. =A0Now, imagine the detector is > > obstructed for 10,000 iterations of your control loop. =A0In > > that case, you are likely to see a big "Error" *and* accumulate > > it repeatedly -- regardless of what your control is trying to > > do to compensate for it! =A0When the detector is eventually > > unobstructed, the "Output" is way out of whack for a long > > time (until the integrator "clears"). > > As a general rule for any electronics or algorithm (software or > hardware) that is measuring light level from photodetectors to imaging > sensors should alwasy cope with the error conditions first of > > a) The output is always minimum (obstruction/lens cap) > > b) The output is always maximum (pointed at sun or bright light) > > c) The output of the sensor is disconnected, or failed. > > At start up I always ignore at least the FIRST two samples to determine > what the ambient condition is. In your case I would also every once > in a while do a detection cycle with the emitter OFF to get an ambient > condition. This is not used in control loop but tests the sensor is > working if ambient is not min or max. Then with emmitter ON your error > must be at least a minimum away from ambient for operations to be > useable.
Well, I have a software low passfilter after I read thwe adc and i let it swing it on bootup, meaning i loop 20 * FILTER in order to get a valid value, I determined that 20x is a pretty good value to get a decent start value with a filter like this: MyVal =3D (MyVal * FILTER + ADCVal) / (FILTER + 1) and i'm currently using a FILTER value of 10.
> If you sampling regime is 50 or 60 Hz you should be able to fit a test > sample inbetween sampling times without effecting the sampling times > without affecting the sampling loop.
Yep, that's actually the plan, in between the LED-on cycles, I'm sampling the ambient brightness which is fed back to the "main-brain" of the system. Thanks for your contribution too! roN
In article <16c21154-6e25-4c2a-bf76-
57afd41477ac@n2g2000prj.googlegroups.com>, ron.eggler@gmail.com says...
> > On Apr 29, 3:40&#4294967295;am, Paul <p...@pcserviceselectronics.co.uk> wrote: > > In article <ipclva$qq...@speranza.aioe.org>, not.going.to...@seen.com > > says... > > > > > > > > > Hi Ron, > > > > > On 4/28/2011 1:32 PM, cerr wrote: > > > > I need to resolve following problem: > > > > I have an led panel with an opto transistor on it that measures the > > > > light emitted/reflected from the target. I want to control the emitted > > > > light to be equal to a certain setpoint from what the opto transistor > > > > measures. Does that make sense? I'm using an 18F pic to control the > > > > whole circuit and I wanted to come up with a PI if not a PID > > > > controller algorithm that controls my LEDs. I've been reading about > > > > PID controllers but am not 100% sure about some things. So far I've > > > > come up with below code: > > > > ...... > > > > > Also, depending on what you are controlling and how it is likely > > > to *fail*, you might want to put some anti-windup protection on > > > your integral term to keep "Integrator" from becoming *so* > > > large (positive or negative) that it dominates the loop's > > > performance for an inordinately long time. &#4294967295;E.g., imagine using > > > "doubles" for these variables. &#4294967295;Now, imagine the detector is > > > obstructed for 10,000 iterations of your control loop. &#4294967295;In > > > that case, you are likely to see a big "Error" *and* accumulate > > > it repeatedly -- regardless of what your control is trying to > > > do to compensate for it! &#4294967295;When the detector is eventually > > > unobstructed, the "Output" is way out of whack for a long > > > time (until the integrator "clears"). > > > > As a general rule for any electronics or algorithm (software or > > hardware) that is measuring light level from photodetectors to imaging > > sensors should alwasy cope with the error conditions first of > > > > a) The output is always minimum (obstruction/lens cap) > > > > b) The output is always maximum (pointed at sun or bright light) > > > > c) The output of the sensor is disconnected, or failed. > > > > At start up I always ignore at least the FIRST two samples to determine > > what the ambient condition is. In your case I would also every once > > in a while do a detection cycle with the emitter OFF to get an ambient > > condition. This is not used in control loop but tests the sensor is > > working if ambient is not min or max. Then with emmitter ON your error > > must be at least a minimum away from ambient for operations to be > > useable. > > Well, I have a software low passfilter after I read thwe adc and i let > it swing it on bootup, meaning i loop 20 * FILTER in order to get a > valid value, I determined that 20x is a pretty good value to get a > decent start value with a filter like this: > MyVal = (MyVal * FILTER + ADCVal) / (FILTER + 1) and i'm currently > using a FILTER value of 10.
You have heard of 'Garbage In Garbage Out' If all the readings are due to pointing at a bright light, so reading is out of normal range, your filter will still produce an out of output.
> > If you sampling regime is 50 or 60 Hz you should be able to fit a test > > sample inbetween sampling times without effecting the sampling times > > without affecting the sampling loop. > > Yep, that's actually the plan, in between the LED-on cycles, I'm > sampling the ambient brightness which is fed back to the "main-brain" > of the system.
Do you do any the reading is meaningless do NOT process, flag error states.
> Thanks for your contribution too! > roN
-- Paul Carpenter | paul@pcserviceselectronics.co.uk <http://www.pcserviceselectronics.co.uk/> PC Services <http://www.pcserviceselectronics.co.uk/fonts/> Timing Diagram Font <http://www.gnuh8.org.uk/> GNU H8 - compiler & Renesas H8/H8S/H8 Tiny <http://www.badweb.org.uk/> For those web sites you hate