Skip to content

Instantly share code, notes, and snippets.

@sbasami
Last active March 25, 2020 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbasami/a5a2739dc9e0c6d1cc88e0b96e2f86e5 to your computer and use it in GitHub Desktop.
Save sbasami/a5a2739dc9e0c6d1cc88e0b96e2f86e5 to your computer and use it in GitHub Desktop.
MATLABでDCモータのIPD制御シミュレーションを行うプログラム
clear all;
clc;
%% シミュレーション用のモデル作成
dt=0.001; %シミュレーション周期[s]
Tc=0.001; %マイコン制御周期[s]
FinishTime=1.2; %シミュレーションの終了時間[s]
% 359.2
% ------------
% s^2 + 37.33s
[A, B, C, D] = tf2ss(359.2, [1 37.33 0]);%伝達関数を状態方程式に変換
sys = ss(A, B, C, D);%状態空間モデルの作成
Gz=c2d(sys,dt);%シミュレーション周期で状態空間モデルを離散化
%% 変数の初期化
r = 1*pi/360; %目標値[rad]
u = 0; %モデルの入力[V]
y = 0; %モデルの出力[rad]
v = 5; %電源電圧[V]
y_old=0; %過去の出力
u_I=0; %積分
xp = [0;0]; %状態変数
Kp = 0; %Pゲイン
Ki = 0; %Iゲイン
Kd = 0; %Dゲイン
count = 1; %ログ用のカウント
%% シミュレーションループ
for Time = 0:dt:FinishTime
if mod(Time,Tc) == 0
%ログ用の変数
umem(count)=u;
ymem(count)=y*360/pi;%分かりやすいように[rad]から[deg]に変換
tmem(count)=Time;
e = r - y;%偏差の計算
u_I = u_I + e*Tc;%積分の計算
u = -Kp*y - Kd*(y - y_old)/Tc + Ki*u_I;%I-PD制御
y_old = y;
%入力が電源電圧以上にならないよう入力を制限
if u > v
u = v;%入力の制限
u_I = u_I - e*Tc;%積分器の停止(アンチワインドアップ制御)
end
if u < -v
u = -v;%入力の制限
u_I = u_I - e*Tc;%積分器の停止(アンチワインドアップ制御)
end
count=count+1;%ログカウントの更新
end
xp=Gz.A*xp+Gz.B*u;
y=Gz.C*xp+Gz.D*u;
end
%% シミュレーション結果の表示
figure(1),plot(tmem, umem)
figure(2),plot(tmem, ymem)
S3 = stepinfo(ymem,tmem,r,'RiseTimeThreshold',[0 0.632])%時定数とかの表示
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment