2013-08-06 2 views
0

크루즈 컨트롤 애플리케이션을위한 PI 제어를 수행하는 간단한 스크립트를 구현하려고하지만 통합 부분에 문제가 있습니다. 여기 내 코드입니다 :PI 제어 알고리즘이 matlab에서 수동으로 구현되었습니다.

function [] = PI_cruisecontrol() 
clc; close all; 
t0 = 0; tfinal = 50; dt = 0.001;    % time parameters 
r = 10;           % reference of 10 m/s 
m = 1000;          % mass 
b = 50;           % friction coeff. (depends on v) 
yp = zeros(tfinal/dt,1); t = yp;    % initialize speed and time array 
Ki = 40;          % integrarl constant 
Kp = 800;          % proportional constant 
int = 0;          % itinialize int error 
% CONTROL LOOP (Forward-Euler integrator is used to solve the ODE) 
for i=t0+2:tfinal/dt 
    err = r-yp(i-1);       % update error 
    int = int+err;       % integral term 
    u  = (Kp*err)+(Ki*int*dt);    % action of control 
    yp(i) = yp(i-1)+((-b*yp(i)/m) + (u/m))*dt; % solve ode for speed 
    t(i) = t(i)+dt*i;       % log the time     
end 
% Results 
figure(1) 
plot(t,yp) 
title ('Step Response') 
xlabel('Time (seconds)') 
ylabel('Amplitud') 
axis([0 20 0 12]) 
hold on 
reference = ones(tfinal/dt,1)*10; 
plot(t,reference,':') 
end 

는 그리고이 predefinided MATLAB 함수 사용, 어떻게해야입니다 :

function [] = PI_cruisecontrol2() 
m = 1000; 
b = 50; 
r = 10; 
s = tf('s'); 
P_cruise = 1/(m*s + b); 
Kp = 800; 
Ki = 40; 
C = pid(Kp,Ki); 
T = feedback(C*P_cruise,1); 
t = 0:0.1:20; 
step(r*T,t) 
axis([0 20 0 12]) 
end 

내 코드에서 잘못하고 있어요 무엇을? 감사합니다.

답변

1

배열 대신 float 변수를 사용하여 문제를 해결할 수있었습니다. 더구나, 파생어를 추가했습니다. (이 첫 주문 문제는 필요하지 않습니다.) 여기 코드를 남겼습니다 :

function [] = aFortran_PI() 
clc; close all; 
r = 10;       % reference of 10 m/s 
m = 1000;      % mass 
b = 50;       % friction coeff. (depends on v) 
yp = 0;       % init response  
Kp = 800;      % proportional constant 
Ki = 40;      % proportional constant 
Kd = 0;       % derivative term is not necessary in this problem 
previous_error = 0; 
integral = 0; 
dt = 0.001; 
% CONTROL LOOP 
for i=1:20000 
    error = r-yp;      % update error 
    integral  = integral + error*dt;  % integral term 
    derivative = (error-previous_error)/dt; % derivative term 
    u = Kp*error+Ki*integral+Kd*derivative; % action of control 
    yp = yp+(-b*yp/m + u/m)*dt % solve ode for velocity 

end 
end 
관련 문제