Digital Control in MCU

1. 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? •

2. 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 •

3. 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) •

4. 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
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);
} --[[ ]]--