Adaptive PID Controller For DC Motor Speed Control
26 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am developing a program to control the speed of a DC motor using an adaptive PID controller. As I am just getting started with this type of control, I would greatly appreciate it if anyone could share code, algorithms for simulation, or practical implementation examples of such a controller for my reference
1 commentaire
Sam Chak
le 1 Oct 2025 à 6:37
Hi @Mnh
Could you share the code for the DC motor model and the PID controller without the adaptive feature? The adaptive feature is not necessary to determine to what extent the motor can maintain its performance.
Réponses (1)
Mathieu NOE
le 1 Oct 2025 à 16:07
hello
maybe this ?
clc
clearvars
% To control the speed of a DC motor using an adaptive PID controller in MATLAB, you can follow these steps. Below is an example implementation:
%
% Step 1: Define the DC Motor Model
% The DC motor can be modeled using its transfer function or state-space representation. For simplicity, we use a transfer function:
%
% ( J ): Moment of inertia
% ( b ): Damping coefficient
% ( K ): Motor constant
% ( R ): Resistance
% ( L ): Inductance
% Step 2: Implement Adaptive PID Controller
% An adaptive PID controller adjusts its parameters ((K_p), (K_i), (K_d)) dynamically based on system performance.
% Parameters of the DC motor
J = 0.01; % Moment of inertia
b = 0.1; % Damping coefficient
K = 0.01; % Motor constant
R = 1; % Resistance
L = 0.5; % Inductance
% Simulation parameters
dt = 0.01;
t = 0:dt:3; % Time vector
% Transfer function of the DC motor
num = K;
den = [(J*L) (J*R + L*b) (b*R + K^2)];
motor_tf = tf(num, den);
% discretization
[B,A] = c2dm(num,den,dt,'tustin');
% Initial PID parameters
Kpi = 100; Kii = 500; Kdi = 10;
%% init
desired_speed = 100; % Desired motor speed (rad/s)
actual_speed(1) = 0; % Initial speed
%% Adaptive PID control loop
% 1st sample
error(1) = desired_speed - 0 ; % Calculate error
error_int(1) = 0; % Calculate integral of error
error_der(1) = 0;% Calculate derivative of error
% PID controller output
u(1) = Kpi*error(1) + Kii*error_int(1) + Kdi*error_der(1);
% Simulate motor response
actual_speed(1) = B(1)*u(1);
% 2nd sample
error(2) = desired_speed - actual_speed(1); % Calculate error
error_int(2) = error_int(1) + 0.5*(error(2)+0)*dt; % Calculate integral of error
error_der(2) = (error(2)-error(1))/dt;% Calculate derivative of error
% PID controller output
u(2) = Kpi*error(2) + Kii*error_int(2) + Kdi*error_der(2);
% Simulate motor response
actual_speed(1) = B(1)*u(1);
actual_speed(2) = B(1)*u(2) + B(2)*u(1) + 0 - (A(2)*actual_speed(1) + 0 );
% 3rd sample and after
for i = 3:length(t)
error(i) = desired_speed - actual_speed(i-1); % Calculate error
error_int(i) = error_int(i-1) + 0.5*(error(i)+error(i-1))*dt; % Calculate integral of error
error_der(i) = (error(i)-error(i-1))/dt;% Calculate derivative of error
% Adaptive PID tuning
Kp = Kpi + 1*abs(error(i));
Ki = Kii + 1*abs(error(i));
Kd = Kdi + 0.01*abs(error(i));
% PID controller output
u(i) = Kp*error(i) + Ki*error_int(i) + Kd*error_der(i);
% Simulate motor response
actual_speed(i) = B(1)*u(i) + B(2)*u(i-1) + B(3)*u(i-2) - (A(2)*actual_speed(i-1) + A(3)*actual_speed(i-2));
end
%% Plot results
figure;
plot(t, actual_speed, 'b', 'LineWidth', 1.5); hold on;
yline(desired_speed, 'r--', 'LineWidth', 1.5);
xlabel('Time (s)');
ylabel('Motor Speed (rad/s)');
title('Adaptive PID Control of DC Motor');
legend('Actual Speed', 'Desired Speed');
grid on;
7 commentaires
Voir également
Catégories
En savoir plus sur Motor Drives dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!