# how to get PID output?

28 views (last 30 days)
mohamed El Hamzawy on 3 Jul 2014
Answered: Komal Damodara on 4 Apr 2019
hello i am currently doin my bachelor thesis ,, the project is ball on beam. i am using PID function for matlab to control a servo motor . the code is as follows:
s = tf('s');
P_ball = 0.48/s^2;
Kp =2;
Kd =1;
Ki=.75;
C = pid(Kp,Ki,Kd);
sys_cl=feedback(P_ball,1);
t=0:0.01:5;
step(0.25*sys_cl)
disp(sys_cl);
The problem is extracting the output value from the PID function. the output of PID function is supposed to be the angle of the servo motor,, now i dont know how to get this angle in order to give it to the servo motor continuously. can anyone help?

Arkadiy Turevskiy on 7 Jul 2014
Your code, as written does not make much sense. You define controller C, but then do not use it, and instead just use unity gain when computing closed-loop response. I am also not sure why you are mutiplying sys_cl by 0.25 when computing the step reponse.
The more meaningful code would be:
s = tf('s');
P_ball = 0.48/s^2;
Kp =2;
Kd =1;
Ki=.75;
C = pid(Kp,Ki,Kd);
sys_cl=feedback(C*P_ball,1);
t=0:0.01:5;
step(sys_cl)
what you see then is the angle of the motor (the variable you control). The output of the PID would be something like a voltage request to the motor. Another issue is that typically a motor transfer function is not a double integrator, like your plant transfer function, but that's for you to figure out.

#### 1 Comment

mohamed El Hamzawy on 8 Jul 2014
First of all thanks for answering.
Second of all, in the code C controller is used in the feed back function "sys_cl=feedback(C*P_ball,1);". The transfer function is not for the motor too, it is for the system of ball on beam. its is a relation between the output which is the position of the ball, and the input which is the angle of the servo. The problem is i am using arduino connections with matlab to control the servo motor as follows:
" a.servoAttach(9);
a.servoWrite(9,Outputpid); "
the Problem is i am trying to get the output of pid function as an integer for each time the pid calculates a new output.

Komal Damodara on 4 Apr 2019
s = tf('s');
P_ball = 0.48/s^2;
Kp =2;
Kd =1;
Ki=.75;
C = pid(Kp,Ki,Kd);
sys_cl=feedback(C*P_ball,1);
t=0:0.01:5;
step(sys_cl)

Komal Damodara on 4 Apr 2019
s = tf('s');
P_ball = 0.48/s^2;
Kp =2;
Kd =1;
Ki=.75;
C = pid(Kp,Ki,Kd);
sys_cl=feedback(C*P_ball,1);
t=0:0.01:5;
step(sys_cl)