# PD controller (Time domain)

18 views (last 30 days)
Hidd_1 on 11 Mar 2021
Answered: Mathieu NOE on 12 Mar 2021
I would like to design a PD controller, to control a moving mass (=1Kg) along the x-axis so it moves from rest (point A) to another point B, like this: I would appreciate any help!
##### 2 CommentsShowHide 1 older comment
Hidd_1 on 11 Mar 2021
Yes here is it:
clf
clc
%Time:
t_ges = 2;
dt = 0.1;
t_vect = 0 : dt : t_ges;
%Initial position, Initial velocity, Initial acceleration:
q_0=0;
dq_0=0;
ddq_0=0;
%The position, velocity, acceleration:
q = q_0;
dq = dq_0;
ddq = dq_0;
%PD constants
Kp=2;
Td=2;
%Matrices to store the calculate values:
n_times = length(t_vect);
q_Matrix = zeros(1, n_times);
dq_Matrix = zeros(1, n_times);
ddq_Matrix = zeros(1, n_times);
F_Matrix = zeros(1, n_times);
for t = t_vect
%position calculation:
q = q + dt * dq + dt^2/2 * ddq;
q_Matrix(:, 1) = q;
%Velocity calculation:
dq = dq + dt * ddq;
dq_Matrix(:, 1) = dq;
%PD controller calculation: (closed-loop, with the reference is 1 meter (Point B))
F = Kp*(1-q) + Td*(0-dq);
F_Matrix(:, 1) = F;
%acceleration calculation:
ddq = F;
ddq_Matrix(:, 1) = ddq;
end
And for the rectangle mass simulation I am planning to use the following function: (I still don't know how to implement it for an array thou)
function ac_plot(~, marker, color, size)
plot(rectangle('Position',[0 0 0.2 0.2],'EdgeColor','b','LineWidth',2),...
rectangle('Position',[0 0 0.2 0.2],'EdgeColor','b','LineWidth',2),...
marker, 'Color', color, 'MarkerSize', size)

Mathieu NOE on 12 Mar 2021
hello again
so this is a modified code :
• the integration process computes the velocity first (from acceleration) , then the position (from the velocity)
• force can be limited , so you can have large gains from the controller (faster response) but you can then saturate the controller output to any given Fmax value. Results will look pretty much like a bang bang controller
• plot update by using handles (faster than re plotting the entire plot at each step)
• still need to figure out how to add the moving rectangle at the top of the figure
hope it helps
clf
clc
%Time:
t_ges = 2.5;
dt = 0.025;
t_vect = 0 : dt : t_ges;
%Initial position, Initial velocity, Initial acceleration:
q_0=0;
dq_0=0;
ddq_0=0;
%The position, velocity, acceleration:
q = q_0;
dq = dq_0;
ddq = dq_0;
%PD constants
Kp=10;
Kd=Kp/2;
% force limitation to Fmax (limitation of output of PD controller)
Fmax = 1;
%Matrices to store the calculate values:
n_times = length(t_vect);
q_Matrix = zeros(1, n_times);
dq_Matrix = zeros(1, n_times);
ddq_Matrix = zeros(1, n_times);
F_Matrix = zeros(1, n_times);
%// initiallize plot. Get a handle to graphic object
f = figure(1);
s1 = subplot(3,1,1);
p1 = plot(NaN,NaN,'b');
title('Position')
ylabel('m');
axis([0 t_ges -0.25 1.25]);%// freeze axes to their final size, to prevent Matlab from rescaling them dynamically
s2 = subplot(3,1,2);
p2 = plot(NaN,NaN,'b');
title('Velocity');
ylabel('m/s');
axis([0 t_ges -1 2]);%// freeze axes to their final size, to prevent Matlab from rescaling them dynamically
s3 = subplot(3,1,3);
p3 = plot(NaN,NaN,'b');
title('Force');
ylabel('N');
axis([0 t_ges -Fmax*1.25 Fmax*1.25]);%// freeze axes to their final size, to prevent Matlab from rescaling them dynamically
for ci = 1:n_times
%Velocity calculation:
dq = dq + dt * ddq;
dq_Matrix(1,ci) = dq;
%position calculation:
q = q + dt * dq;
q_Matrix(1,ci) = q;
%PD controller calculation: (closed-loop, with the reference is 1 meter (Point B))
F = Kp*(1-q) + Kd*(0-dq);
% force limitation (optionnal)
if abs(F)> Fmax
F = Fmax*sign(F);
end
F_Matrix(1,ci) = F;
%acceleration calculation:
ddq = F;
ddq_Matrix(1,ci) = ddq;
% update the plot
ind = (1:ci);
t_plot = t_vect(ind);
pause(0.01)
set(p1, 'XData', t_plot, 'YData', q_Matrix(ind));
set(p2, 'XData', t_plot, 'YData', dq_Matrix(ind));
set(p3, 'XData', t_plot, 'YData', F_Matrix(ind));
end

Mathieu NOE on 11 Mar 2021
a very simple and straigthforward simulation without much science behind :
NB : controller and plant in series in the main path, unitary feedback %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% original plant
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
s = tf('s');
% Gs = 1.44e09/(s^2+5333*s+9.6e07);
Gs = 1/(s^2+0*s+0);
%PID
P= 10;
I= 0.0;
D= 1*P;
Ct = P+I/s+D*s/(1e-6*s+1);
figure()
Gscl = feedback(series(Ct,Gs),1);
step(Gscl);
legend('closed loop');
grid on;
Hidd_1 on 11 Mar 2021
I would like to make the simulation, that's why I tried to discretized the time, do you have any idea how to make a similar simulation like the one above.
The porpuse for my problem is to examine the controller which is going to take the shortest amount of time between the points A and B, and taking in consideration limits on the force F. ==> I think I need dynamic Programming to solve that! (Bang-bang controller)