+ Post New Thread
Results 1 to 6 of 6

18th March 2019, 02:51 #1
 Join Date
 Aug 2011
 Posts
 193
 Helped
 0 / 0
 Points
 2,304
 Level
 11
why does this simple program not work
Hi
I have a simple code to learn about PI controller. The set point is 1.0 and output is not converging. What is the problem?
Code C  [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int main() { /* Variables*/ double input,output,kp,error,ki,kpo,kio,T; input=1.00; kp=1; ki=0.1; output=0; T=1/100; while(1) { error=inputoutput; kpo=error*kp; kio=kio+(error*ki*0.01); output=kpo+kio; printf("output is %f\t%f\t%f\t%f\n",error,kpo,kio,output); } }
Last edited by BradtheRad; 18th March 2019 at 04:16. Reason: Put code in formatted window

Advertisement

18th March 2019, 03:17 #2
 Join Date
 Jan 2019
 Posts
 378
 Helped
 94 / 94
 Points
 2,035
 Level
 10
Re: why does this simple program not work
I don't know why it isn't working
I note that kio is not initialized and you never use T
I tried to simulate it with excel, since it is only a few lines.
output oscillates between (about) 2+x and (about) x, where x is 0.01, 0.02, 0.03... and increases like that every iteration
I suggest you check your equations and initial values.
I changed input to 5, kp to 0.1, ki to 2, and the output converged to 5 (< 1% error) at about 265 steps
I then changed input to 3 at step 265 and the putput converged to (< 1%) at step 435
looks like changing some parameters is sufficient

Advertisement

18th March 2019, 03:33 #3
 Join Date
 Aug 2011
 Posts
 193
 Helped
 0 / 0
 Points
 2,304
 Level
 11
Re: why does this simple program not work
Hi
I hooked these values kp=1 and ki=0.1 in simulink and works ok. Converges in 100s. But no random fluctuations. I also changed the values as you suggested and indeed there was convergence. Strange but why does my earlier program oscillate so much?

Advertisement

18th March 2019, 17:33 #4
 Join Date
 Jan 2019
 Posts
 378
 Helped
 94 / 94
 Points
 2,035
 Level
 10
Re: why does this simple program not work
an overly simplified answer:
control theory rests on the roots of quadratic equations.
depending on the roots, you can get decay (e^x), decay with oscillation (e^x sin x), growth with oscillation (e^x sin x) and maybe even just growth (e^x)
the parameters you chose led to growth (albeit small) with oscillation (that's what it looked like to me)
choosing different parameters led to different result.

18th March 2019, 21:41 #5
 Join Date
 Nov 2009
 Location
 Italy
 Posts
 1,125
 Helped
 386 / 386
 Points
 8,335
 Level
 21
Re: why does this simple program not work
You wrote "output=kpo+kio;" instead of "output=output+kpo+kio;"

Advertisement

18th March 2019, 22:11 #6
 Join Date
 Jan 2008
 Location
 Bochum, Germany
 Posts
 45,443
 Helped
 13827 / 13827
 Points
 260,240
 Level
 100
Re: why does this simple program not work
You wrote "output=kpo+kio;" instead of "output=output+kpo+kio;"
+ Post New Thread
Please login