Good evening folks!
While tuning my quadrotor model in Matlab I found that the Matlab PID function had a Filter coefficient (N) term in the PID equation. However I have not encountered such a term in my typical PID programs. How would one implement this term into the PID equation and what would be some simple C code to add this term into the derivative? I tried generating C code from my model but got a lot of header and C program files with nothing close to simple.
My current PID code line in C is:
U2 = KP * phi_error + KI * phi_errorsum + KD * (PHI_RATE_desired-PHI_RATE);
Many Thanks, Robert

 채택된 답변

Arkadiy Turevskiy
Arkadiy Turevskiy 2013년 3월 12일

0 개 추천

The code would look like that.
FilterCoefficient = (Kd * u - Filter_DSTATE) * N;
y = (Kp * u + Integrator_DSTATE) + FilterCoefficient;
Integrator_DSTATE += Ki * u * 0.01;
Filter_DSTATE += 0.01 * FilterCoefficient;
HTH.
Arkadiy

댓글 수: 4

Robert
Robert 2013년 3월 12일
편집: Robert 2013년 3월 12일
Awesome!
Why are Filter_DSTATE and Integrator_DSTATE scaled by 0.01?
Thank you!! Robert
Arkadiy Turevskiy
Arkadiy Turevskiy 2013년 3월 13일
0.01 is the sampling time I specified in the block before generating code. You can set the sampling time to whatever you want and the code will of course be updated accordingly.
Marzieh Hosseini
Marzieh Hosseini 2021년 5월 6일
Hi,
Is it the same code if I want to implement PID in Matlab code, while I am controlling a device directly with Matlab and I got real time information in my Matlab?
Andre Elliot
Andre Elliot 2023년 5월 15일
Hi what are u, N, y and the meaning of DSTATE? nice day.

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

추가 답변 (4개)

Shashank Prasanna
Shashank Prasanna 2013년 2월 17일

2 개 추천

Hi Robert, this link should give you a better idea of what N means:
If you have Simulink Control Design toolbox, then you can use the built in pidtune functionality to tune the PID.

댓글 수: 1

Brian Candiloro
Brian Candiloro 2023년 1월 13일
Can this filter term be realized in a physical opamp differentiator circuit or only in code?

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

Robert
Robert 2013년 2월 17일

0 개 추천

Thank you for the response, but I saw that link and do not quite understand it entirely, especially the filter block.
So in essence should the code look something like this:
Derivative PID:
Derivative_gain*(set_rate - N*acutal_rate)?
I am not quite sure where the filter (1/s) comes into play.
Thank you
Robert
Robert 2013년 2월 21일

0 개 추천

Any ideas?

댓글 수: 1

Hi Robert,
In essence, the filtered derivative is doing a first order filtering on the change of the inputs, scaled by D gain.
The 1/s integrates the previous output scaled by N term, then adding the input change scaled by D * N to produce the output.
If you multiply the D term of the continuous form by s, you will see it better how the filtering comes into play.
In discrete form:
y[n] = D*N*(u[n]-u[n-1]) + (1-N*Ts)*y[n-1];

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

stanley
stanley 2025년 7월 20일

0 개 추천

filter coffiect

댓글 수: 2

stanley
stanley 2025년 7월 20일
PID Block → Filter Coefficient
Sam Chak
Sam Chak 2025년 7월 20일
Hi @stanley, What exactly is your question on the derivative filter coefficient of the PID Controller?

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

카테고리

질문:

2013년 2월 17일

댓글:

2025년 7월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by