+ Post New Thread
Results 1 to 6 of 6
  1. #1
    Full Member level 4
    Points: 2,304, Level: 11
    Achievements:
    7 years registered

    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=input-output;
              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

    •   AltAdvertisement

        
       

  2. #2
    Full Member level 6
    Points: 2,035, Level: 10

    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



    •   AltAdvertisement

        
       

  3. #3
    Full Member level 4
    Points: 2,304, Level: 11
    Achievements:
    7 years registered

    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?



    •   AltAdvertisement

        
       

  4. #4
    Full Member level 6
    Points: 2,035, Level: 10

    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.



  5. #5
    Advanced Member level 4
    Points: 8,335, Level: 21
    Achievements:
    7 years registered

    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;"



    •   AltAdvertisement

        
       

  6. #6
    Super Moderator
    Points: 260,240, Level: 100
    Awards:
    1st Helpful Member

    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;"
    That's o.k. Problem is unsuitable combination of kp and ki, as already stated. kp=1 seems generally problematic. A controller with practical use has set point and process value inputs, by the way. I understand that the simplified example is modelling a plant with unity transfer function.



--[[ ]]--