Trouble writing PWM signal from PID controller

조회 수: 10 (최근 30일)
Chris Hubbard
Chris Hubbard 2019년 12월 3일
댓글: Nikhil Rajkumar 2020년 7월 29일
I am working on a project to use an Arduino Uno with MATLAB PID control, a moisture sensor, and water pump to keep a plant at a specified moisture level. I've posted my code below, everything works perfect until the if/ifelse statements where I can't seem to get an output of anything but 0 or 1 duty cycle even when I confirmed that PID was outputting a value between 0 and 1. Any help would be greatly appreciated, thank you.
%%
End_time_CS= 10;
moisture_Threshold= 20;
tic
Kp = .05;
Ki = 0.01;
Kd = 0.01;
dt = 0.25;
i=1;
while toc <= End_time_CS
V_moisture=readVoltage(a,'A0');
moisture_f=(4.688.*exp(0.2801.*V_moisture))+((1.03.*(10.^(-6))).*exp(14.4.*V_moisture));
rec_temp_CS(i+1) = moisture_f;
rec_time_CS(i+1) = toc;
Error(i+1) = moisture_Threshold - rec_temp_CS(i);
Prop(i+1) = Error(i+1);
Der(i+1) = (Error(i+1) - Error(i))/dt;
Int(i+1) = (Error(i+1) + Error(i))*dt/2;
I(i+1) = sum(Int);
PID(i+1) = Kp*Prop(i) + Ki*I(i+1)+ Kd*Der(i);
i=i+1;
pause(.25)
%this is where the problem is I can only get it to output 0 or 1 duty
%cycle, not the PID value even when PID is between 0 and 1
if PID(i) > 1
writePWMDutyCycle(a,'D9',1)
elseif PID(i) < 0
writePWMDutyCycle(a,'D9',0)
elseif 0 < PID(i) < 1
writePWMDutyCycle(a,'D9',PID(i))
end
%PWM_log=readVoltage(a,'A5')
end
display('Control System has stopped')
  댓글 수: 3
Chris Hubbard
Chris Hubbard 2019년 12월 4일
편집: Chris Hubbard 2019년 12월 4일
It is never getting to the third option, I had it log the PID(i) values and its output was between 0 and 1 (the condition for the third option) for most of the run time, but my duty cycle was only ever 0 or 1 (the first two if/ifelse options). Unless the way I wrote my loops was incorrect to have PID(i) in the third option equal my PID equation.
Nikhil Rajkumar
Nikhil Rajkumar 2020년 7월 29일
Hey! I am working on a similar project that requires a PID controllor to operate LED brightness. I was wondering if you explain what Error(i+1)/Error(i), Prop(i+1), Der(i+1), Int(i+1) and PID(i+1) do. Are they functions that you have defined? If so, please briefly explain what each of them do. I would really appreciate some help as I am new to MATLAB. Thank you so much!

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

답변 (1개)

Pranav Murali
Pranav Murali 2020년 3월 16일
Hi Chris,
I understand that you are receiving the correct value of PID but you are unable to control the output duty cycle of your system. The problem here is MATLAB does not support testing whether PID(i) is in between 0 and 1 with this syntax. Instead, MATLAB interprets 0 < PID(i) < 1 as (0 < PID(i)) < 1, comparing the logical result of 0 < PID(i) (either 0 or 1) to the value 1.
I would suggest you rather try with the syntax below:
elseif ((0 < PID(i)) && (PID(i) < 1))
That should solve your problem.

카테고리

Help CenterFile Exchange에서 PID Controller Tuning에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by