
Digital PID controller
Discrete transfer function
Open-loop response
Proportional control
Proportional-Derivative control
In this digital control version of the ball and beam experiment, we are going to use the PID control method to design the digital controller. If you refer to the Ball and Beam Modeling page, the open-loop transfer function was derived as

| m | mass of the ball | 0.11 kg |
| g | gravitational acceleration | 9.8 m/s^2 |
| d | lever arm offset | 0.03 m |
| L | length of the beam | 1.0 m |
| R | radius of the ball | 0.015 m |
| J | ball's moment of inertia | 9.99e-6 kgm^2 |
| R(s) | ball position coordinate (m) | |
| theta(s) | servo gear angle | 0.25 rad |
The design criteria for this problem are:

As you noticed the above transfer function was written in terms of s. For the digital PID control, we use the following transfer function in terms of z.

m = 0.111; R = 0.015; g = -9.8; L = 1.0; d = 0.03; J = 9.99e-6; K = (m*g*d)/(L*(J/R^2+m)); %simplifies input num = [-K]; den = [1 0 0]; ball = tf(num,den); Ts = 1/50; ball_d = c2d(ball,Ts,'zoh')
Running this m-file in the MATLAB command window gives you the following matrices.
Transfer function: 4.2e-05 z + 4.2e-05 ------------------- z^2 - 2 z + 1 Sampling time: 0.02
numDz = 0.0001*[0.42 0.42]; denDz = [1 -2 1]; Ts = 1/50; ball_d = tf(numDz,denDz,Ts); [x,t] = step(0.25*ball_d,5); stairs(t,x)

From this plot, it is clear that the open-loop system is unstable causing the ball to roll off the end of the beam.
numDz = 0.0001*[0.42 0.42]; denDz = [1 -2 1]; Ts = 1/50; ball_d = tf(numDz,denDz,Ts); Kp=100; sys_cl = feedback(Kp*ball_d,1); [x,t] = step(0.25*sys_cl,5); stairs(t,x)

As you can see, the addition of proportional control does not make the system stable. You may try to increase the proportional gain (Kp) and confirm that the system remains unstable.
numDz = 0.0001*[0.42 0.42]; denDz = [1 -2 1]; Ts = 1/50; ball_d = tf(numDz,denDz,Ts); Kp=100; Kd=10; numpd = [Kp+Kd -(Kp+2*Kd) Kd]; denpd = [1 1 0]; contr = tf(numpd,denpd,Ts); sys_cl = feedback(contr*ball_d,1); [x,t] = step(0.25*sys_cl,5); stairs(t,x)

Now the system is stable, but the rise time is too long. From the PID Tutorial page, we see that the increasing the proportional gain (Kp) will decrease the rise time. Let's increase the proportional gain (Kp) to 1000 and see what happens. Change Kp in the above m-file from 100 to 1000 and rerun it in the command window. You should see the following step response.

As you can see, all of the design requirements are satisfied. For this particular problem, no implementation of an integral control was needed. But remember there is more than one solution for a control problem. For practice, you may try different P, I and D combinations to obtain a satisfactory response.
