Matlab function in Simulink

Hello,
I have problem with MATLAB function in Simulink - after Run values are always zero, while other parts of code (built in PID controller) are working quite good.
function PWMout = myPID(valTarget,valPres, kp, ki, kd, Td, Ts,errorPrev,...
PIDoutMax, PIDoutMin, errorTolerance)
upperBound = PIDoutMax - (PIDoutMax*(1-errorTolerance));
lowerBound = PIDoutMin - (PIDoutMin*(1-errorTolerance));
errPres = valTarget - valPres;
Pout = kp*(errPres - errorPrev);
Iout = ki*errPres;
Dout = kd*(Td/Ts)*(errPres - (2*errorPrev) + ...
+ errorPrev);
PIDoutTemp = Pout + Iout + Dout;
PWMout = PIDoutTemp;
errorPrev = errPres;
end

댓글 수: 7

Ameer Hamza
Ameer Hamza 2020년 10월 3일
Try adding a breakpoint inside the function to see whether what is causing the output to remain zero. https://www.mathworks.com/help/matlab/matlab_prog/set-breakpoints.html
ps19
ps19 2020년 10월 3일
편집: ps19 2020년 10월 3일
Yes I know about debugging but code is in Subsystem/MATLAb function block and when I will add breakpoint it didn`t stop on it - it didn't matter which place in code of subsystem.
Ameer Hamza
Ameer Hamza 2020년 10월 3일
Can you attach the model?
ps19
ps19 2020년 10월 3일
편집: ps19 2020년 10월 5일
Yes no problem. In attachment
Ameer Hamza
Ameer Hamza 2020년 10월 4일
I tried running the model. The brakpoint is working and it does give non zero value in PWMout.
ps19
ps19 2020년 10월 5일
It`s started working, I created new model recreated block by block, something must have been crashed in some configuration stage.
Ameer Hamza
Ameer Hamza 2020년 10월 6일
Btw, look at persistent variables. You will need that to properly save PID values from the last step inside the function block.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Programmatic Model Editing에 대해 자세히 알아보기

제품

릴리스

R2020a

질문:

2020년 10월 3일

댓글:

2020년 10월 6일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by