+ Post New Thread
Results 1 to 4 of 4
  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

    Digital Control in MCU

    Hi

    I have recently simulated a digital control system comprising a PI compensator for a integerator plant for tracking a sawtooth signal. Everything works fine in simulation.
    Converted to difference equation and implemented the algorithm in MCU and I am experiencing a lag. internally generated sawtooth lags external signal by substantial amount. what could reason and how to fix it?

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 77,803, Level: 68
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    15,766
    Helped
    3590 / 3590
    Points
    77,803
    Level
    68

    Re: Digital Control in MCU

    Hi,

    No values we can calculate with.

    Thus we have to guess:
    * it's a timing problem
    * maybe caused by the algorithm
    * maybe caused by using wrong variables (float)
    * or maybe caused by wrong algorithm parameters
    * or maybe a programming mistake: not using timers to ensure a fixed loop calculation rate
    * or maybe, or maybe, or maybe...

    Klaus
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



    •   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: Digital Control in MCU

    Hi

    I am going to provide details. But please have a quick look at my matlab simulation code provided. In bode plot, I see variable phase lag. I use this program to discretize(open loop TF) and implement in MCU.

    Code:
    t=1/2500
    D=.7  // damping
    w=3500 //natural frequency
    a=2*D*w
    b=w*w
    g=tf([a b],[1 0 0])
    w=feedback(g,1)
    h=c2d(g,t,'tustin')
    %step(w)
    bode(w)



    •   AltAdvertisement

        
       

  4. #4
    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: Digital Control in MCU

    I think slow convergence is due to floating point variables. Tried using Fixed point arithmetic. But the output seems to be oscillating. The code is given below. All variables are Q15 format. Should I operate on Q32?

    D=.84;
    w=50;
    kf=_Q15ftoi((w*w)*.0004);
    kp=_Q15ftoi((2*D)/w);
    ki=_Q15ftoi(1.0);
    pi=0;
    i=0;
    input=_Q15ftoi(0.5);

    while(1)
    {
    // error goes through first stage integrator. The output of integrator goes through PI compensator
    error=_Q15sub(input,Output);
    i=_Q15add(i,Q15_MUL(error,kf)); //Integrator stage;add,sub are microchip in-buit functions
    pi=_Q15add(pi,Q15_MUL(i,ki)); //PI stage
    Output=_Q15add(pi,Q15_MUL(i,kp));
    printf("%f\n",(double)_itofQ15(Output)); // output oscillating- should settle at 0.5
    }

    _Q15 Q15_MUL(_Q15 x, _Q15 y)
    {
    long temp,test1,test2;
    test1=x;
    test2=y;
    if ((test1==0x8000)&& (test2==0x8000))
    {
    temp=0x7fff;
    }
    else
    {
    temp=(test1*test2)>>15;
    }
    return(temp);
    }



--[[ ]]--