UIデザイナーを使用したシミュレーションと連動するGUIの作成
Tutorial Level: Intermediate UIデザイナーからコードを生成し、それを使用してシミュレーションを実行する方法を学びます。
このチュートリアルのファイル
Original gui code.ui; Original gui code.oml; Gui for simulation.scm
概要
このチュートリアルのモデルにはIncludeブロックがあります。GUIを使用して、入力パラメータ、つまりStep信号を乗算するゲインを選択します。
Twin Activateモデルの作成
- パレットブラウザから、 を選択します。
-
パレットブラウザから
を選択し、ブロックのインクルード(Include)タブを変更します:
- 可能なダイアグラムの数(Number of possible diagrams):2
- ダイアグラムの選択(Diagram choice):Gain
- ダイアグラムのパス(Diagram path):'Gain1'' Gain2'
- を選択します。
-
これらを以下のように接続します:
-
2つの異なるスーパーブロックを作成し、それぞれにゲインを設定し、入力ポートと出力ポートを配置します。
スーパーブロックの外側のセットアップは、以下の通りです:
注: スーパーブロックの名前は、インクルードダイアグラムブロックで使用されているものと同じです。 - スーパーブロックを右クリックし を選択します。
-
リボンから
を開き、最終時間(Final Time)を "simtime "に設定します。
GUIの作成
-
リボン上
を選択します。
UI デザイナーが新しいウィンドウで表示されます。 -
ウィジェットボックスからダイアログにオブジェクトをドラッグしてGUIデザインを作成します。
次の例では、メインパネル、プッシュボタン、スライダー、ラジオボタン、ラベルのあるシンプルなGUIを示しています。
-
デザインに応じて、各オブジェクトの位置、名前、プロパティを変更します。
位置を調整するには、オブジェクトを選択し、マウスまたは矢印キーで移動します。プロパティを変更するには、オブジェクトを選択し、プロパティエディターで必要なプロパティを変更します。また、オブジェクトを右クリックし、コンテキストメニューから多くのプロパティを定義することもできます。ゲイン(Gain)の選択、その値、シミュレーション時間のパラメータは、Twin Activateのメインコードで操作します。そのため、それぞれのタグに"タグ"の値を設定するのがよい方法です。Gainラジオボタンの例:
-
UI デザイナーリボンから、Previewボタンをクリックします。
GUIデザインのプレビューが表示されます。デザインを確認し、プレビューウィンドウを閉じます。
-
デザインを保存し、保存するファイル形式を指定してください:
- *.UI: デザインをUI デザイナーファイルとして保存します。UI デザイナーで引き続きデザイン作業を行う場合は、このファイル形式を使用してください。
- *.OML: このフォーマットを使って、コードのスケルトンを生成してください。
注: デザインをコードとして保存すると、それをUI デザイナーに再びロードすることはできません。UI デザイナーで開くことができるデザインの作業バージョンを保持したい場合は、コードとして保存する前に.UI形式でバージョンを保存してください。 -
上記の設計例を .OML ファイルとして保存すると、以下のコードが生成されます:
% Auto-generated code [UI Designer] % Altair Twin Activate 2023 % Last update: Fri Jan 19 09:11:07 2024 %------------------------------------------------------------ % % Callback functions: %------------------------------------------------------------ function gain1_button_callback(handle, data, varargin) % Add implementation: [callback] end function gain2_button_callback(handle, data, varargin) % Add implementation: [callback] end function gain1_value_callback(handle, data, varargin) % Add implementation: [callback] end function gain2_value_callback(handle, data, varargin) % Add implementation: [callback] end function simtime_slider_callback(handle, data, varargin) % Add implementation: [callback] end function runsim_button_callback(handle, data, varargin) % Add implementation: [callback] end %------------------------------------------------------------ % % GUI objects: %------------------------------------------------------------ Dialog = figure('units', 'pixels', 'position', [0 0 167 346] ... , 'name','Dialog', 'windowicon','', 'windowstyle','undocked' ... , 'tag','Dialog', 'handlevisibility','on', 'numbertitle','off', 'visible','on'); uiPanel = uipanel('parent', Dialog, 'units','normalized', 'position',[0.00 0.03 0.96 0.96] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','normal', 'fontsize',8.25 , 'fontunits','pixels' ... , 'backgroundcolor', [246 246 246] ... , 'bordertype','none', 'borderwidth', 1 , 'title','' ... , 'aspectratio','off', 'backgroundimage','', 'tag','uiPanel' ... , 'userdata',[], 'value',0, 'tooltipstring','', 'visible','on'); gain1_button = uicontrol('parent', uiPanel, 'style','radiobutton' ... , 'callback','gain1_button_callback', 'units','normalized', 'position',[0.07 0.03 0.66 0.09] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','normal', 'fontsize',8.25 , 'fontunits','pixels' ... , 'string','Gain 1', 'value', 1 , 'foregroundcolor', [20 20 20] ... , 'interruptible','off', 'tag','goption', 'userdata',[], 'tooltipstring','', 'visible','on'); gain2_button = uicontrol('parent', uiPanel, 'style','radiobutton' ... , 'callback','gain2_button_callback', 'units','normalized', 'position',[0.07 0.12 0.66 0.09] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','normal', 'fontsize',8.25 , 'fontunits','pixels' ... , 'string','Gain 2', 'value', 0 , 'foregroundcolor', [20 20 20] ... , 'interruptible','off', 'tag','gain2_button', 'userdata',[], 'tooltipstring','', 'visible','on'); gain1_value = uicontrol('parent', uiPanel, 'style','edit' ... , 'callback','gain1_value_callback', 'units','normalized', 'position',[0.07 0.25 0.34 0.10] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','normal', 'fontsize',8.25 , 'fontunits','pixels' ... , 'string','0' ... , 'horizontalalignment','left' ... , 'verticalalignment','middle', 'foregroundcolor', [20 20 20], 'backgroundcolor', [255 255 255] ... , 'interruptible','off', 'tag','g1v', 'userdata',[] ... , 'value',0, 'tooltipstring','', 'visible','on'); gain1_text = uicontrol('parent', uiPanel, 'style','text' ... , 'units','normalized', 'position',[0.46 0.25 0.47 0.09] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','normal', 'fontsize',8.25 , 'fontunits','pixels' ... , 'string','Gain 1 Value' ... , 'horizontalalignment','left' ... , 'verticalalignment','middle', 'foregroundcolor', [20 20 20], 'backgroundcolor', [255 255 255] ... , 'tag','gain1_text', 'userdata',[], 'value',0, 'tooltipstring','', 'visible','on'); gain2_text = uicontrol('parent', uiPanel, 'style','text' ... , 'units','normalized', 'position',[0.46 0.37 0.47 0.09] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','normal', 'fontsize',8.25 , 'fontunits','pixels' ... , 'string','Gain 2 Value' ... , 'horizontalalignment','left' ... , 'verticalalignment','middle', 'foregroundcolor', [20 20 20], 'backgroundcolor', [255 255 255] ... , 'tag','gain2_text', 'userdata',[], 'value',0, 'tooltipstring','', 'visible','on'); gain2_value = uicontrol('parent', uiPanel, 'style','edit' ... , 'callback','gain2_value_callback', 'units','normalized', 'position',[0.07 0.37 0.34 0.10] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','normal', 'fontsize',8.25 , 'fontunits','pixels' ... , 'string','0' ... , 'horizontalalignment','left' ... , 'verticalalignment','middle', 'foregroundcolor', [20 20 20], 'backgroundcolor', [255 255 255] ... , 'interruptible','off', 'tag','g2v', 'userdata',[] ... , 'value',0, 'tooltipstring','', 'visible','on'); simtime_slider = uicontrol('parent', uiPanel, 'style','slider' ... , 'callback','simtime_slider_callback', 'units','normalized', 'position',[0.13 0.62 0.80 0.07] ... , 'enable','on', 'max', 30 , 'min', 1 , 'orient','horizontal', 'sliderstep', [1 10] ... , 'tracking', 'on', 'tickinterval', 1, 'tickmarks', 'nomarks' ... , 'callbackdelay',0.000000, 'interruptible','off', 'tag','simtimev' ... , 'userdata',[], 'tooltipstring','', 'visible','on'); simtime = uicontrol('parent', uiPanel, 'style','text' ... , 'units','normalized', 'position',[0.26 0.50 0.54 0.09] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','normal', 'fontsize',8.25 , 'fontunits','pixels' ... , 'string','Simulation Time' ... , 'horizontalalignment','left' ... , 'verticalalignment','middle', 'foregroundcolor', [20 20 20], 'backgroundcolor', [255 255 255] ... , 'tag','simtime', 'userdata',[], 'value',0, 'tooltipstring','', 'visible','on'); runsim_button = uicontrol('parent', uiPanel, 'style','pushbutton' ... , 'callback','runsim_button_callback', 'units','normalized', 'position',[0.20 0.84 0.66 0.09] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','bold', 'fontsize',8.00 , 'fontunits','pixels' ... , 'string','Run Simulation', 'horizontalalignment','center' ... , 'backgroundcolor', [255 255 255], 'foregroundcolor', [20 20 20] ... , 'backgroundimage','', 'aspectratio','on' ... , 'interruptible','off', 'tag','runsim_button', 'userdata',[] ... , 'value',0, 'tooltipstring','', 'visible','on'); min_simtime = uicontrol('parent', uiPanel, 'style','text' ... , 'units','normalized', 'position',[0.13 0.69 0.14 0.10] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','normal', 'fontsize',8.00 , 'fontunits','pixels' ... , 'string','1' ... , 'horizontalalignment','center' ... , 'verticalalignment','middle', 'foregroundcolor', [20 20 20], 'backgroundcolor', [246 246 246] ... , 'tag','min_simtime', 'userdata',[], 'value',0, 'tooltipstring','', 'visible','on'); max_simtime = uicontrol('parent', uiPanel, 'style','text' ... , 'units','normalized', 'position',[0.79 0.69 0.14 0.10] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','normal', 'fontsize',8.00 , 'fontunits','pixels' ... , 'string','30' ... , 'horizontalalignment','center' ... , 'verticalalignment','middle', 'foregroundcolor', [20 20 20], 'backgroundcolor', [246 246 246] ... , 'tag','max_simtime', 'userdata',[], 'value',0, 'tooltipstring','', 'visible','on');
GUIとモデルの組み込み
-
Twin Activateウィンドウに戻り、 を選択します。
元のOMLコードを少し変更する必要があります。まず、以下のコマンドを使って、コードの最初にワークスペースをクリアします。
このブロックからTwin Activateモデルを実行する場合、いくつかのパラメータと関数を使用する必要があります。このGUIにはシミュレーションを開始するPushButtonがあるので、以下のコマンドはこのWidgetのコールバック関数内で使用する必要があります:clc, clear, close all
model = bdeGetCurrentModel; %Get handle of current model aux_g1v = findobj('tag', 'g1v'); %Auxiliar variable to the Gain1 value ctx.g1 = str2num(get(aux_g1v, 'string')); %Getting Gain1 Value aux_g2v = findobj('tag', 'g2v'); %Auxiliar variable to the Gain2 value ctx.g2 = str2num(get(aux_g2v, 'string')); %Getting Gain2 Value aux_rbut = findobj('tag', 'goption'); %Auxiliar variable to the RadioButton option rbut = get(aux_rbut, 'value'); %Getting RadioButton Value - 1: 1st Gain, 0: 2nd Gain if rbut == 1 ctx.Gain = 1; %Select the first SuperBlock - Gain1 else ctx.Gain = 2; %Select the second SuperBlock - Gain2 end aux_sliv = findobj('tag', 'simtimev'); %Auxiliar variable to the Slider value ctx.simtime = get(aux_sliv, 'value'); %Getting Final Simulation Time env = getnewenv; %Create a new environment variable to read/write the signals vssRunSimulation(model,ctx,env); %Run Twin Activate simulation
以下は、ExecOmlScript内で使用する最終的なOMLコードです:
clc, clear, close all % Auto-generated code [UI Designer] % Altair Twin Activate 2023 % Last update: Fri Jan 19 09:02:01 2024 %------------------------------------------------------------ % % Callback functions: %------------------------------------------------------------ function gain1_button_callback(handle, data, varargin) % Add implementation: [callback] end function gain2_button_callback(handle, data, varargin) % Add implementation: [callback] end function gain1_value_callback(handle, data, varargin) % Add implementation: [callback] end function gain2_value_callback(handle, data, varargin) % Add implementation: [callback] end function simtime_slider_callback(handle, data, varargin) % Add implementation: [callback] end function runsim_button_callback(handle, data, varargin) model = bdeGetCurrentModel; %Get handle of current model aux_g1v = findobj('tag', 'g1v'); %Auxiliar variable to the Gain1 value ctx.g1 = str2num(get(aux_g1v, 'string')); %Getting Gain1 Value aux_g2v = findobj('tag', 'g2v'); %Auxiliar variable to the Gain2 value ctx.g2 = str2num(get(aux_g2v, 'string')); %Getting Gain2 Value aux_rbut = findobj('tag', 'goption'); %Auxiliar variable to the RadioButton option rbut = get(aux_rbut, 'value'); %Getting RadioButton Value - 1: 1st Gain, 0: 2nd Gain if rbut == 1 ctx.Gain = 1; %Select the first SuperBlock - Gain1 else ctx.Gain = 2; %Select the second SuperBlock - Gain2 end aux_sliv = findobj('tag', 'simtimev'); %Auxiliar variable to the Slider value ctx.simtime = get(aux_sliv, 'value'); %Getting Final Simulation Time env = getnewenv; %Create a new environment variable to read/write the signals vssRunSimulation(model,ctx,env); %Run Twin Activate simulation end %------------------------------------------------------------ % % GUI objects: %------------------------------------------------------------ Dialog = figure('units', 'pixels', 'position', [0 0 167 346] ... , 'name','Dialog', 'windowicon','', 'windowstyle','undocked' ... , 'tag','Dialog', 'handlevisibility','on', 'numbertitle','off', 'visible','on'); uiPanel = uipanel('parent', Dialog, 'units','normalized', 'position',[0.00 0.03 0.96 0.96] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','normal', 'fontsize',8.25 , 'fontunits','pixels' ... , 'backgroundcolor', [246 246 246] ... , 'bordertype','none', 'borderwidth', 1 , 'title','' ... , 'aspectratio','off', 'backgroundimage','', 'tag','uiPanel' ... , 'userdata',[], 'value',0, 'tooltipstring','', 'visible','on'); gain1_button = uicontrol('parent', uiPanel, 'style','radiobutton' ... , 'callback','gain1_button_callback', 'units','normalized', 'position',[0.07 0.03 0.66 0.09] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','normal', 'fontsize',8.25 , 'fontunits','pixels' ... , 'string','Gain 1', 'value', 1 , 'foregroundcolor', [20 20 20] ... , 'interruptible','off', 'tag','goption', 'userdata',[], 'tooltipstring','', 'visible','on'); gain2_button = uicontrol('parent', uiPanel, 'style','radiobutton' ... , 'callback','gain2_button_callback', 'units','normalized', 'position',[0.07 0.12 0.66 0.09] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','normal', 'fontsize',8.25 , 'fontunits','pixels' ... , 'string','Gain 2', 'value', 0 , 'foregroundcolor', [20 20 20] ... , 'interruptible','off', 'tag','gain2_button', 'userdata',[], 'tooltipstring','', 'visible','on'); gain1_value = uicontrol('parent', uiPanel, 'style','edit' ... , 'callback','gain1_value_callback', 'units','normalized', 'position',[0.07 0.25 0.34 0.10] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','normal', 'fontsize',8.25 , 'fontunits','pixels' ... , 'string','0' ... , 'horizontalalignment','left' ... , 'verticalalignment','middle', 'foregroundcolor', [20 20 20], 'backgroundcolor', [255 255 255] ... , 'interruptible','off', 'tag','g1v', 'userdata',[] ... , 'value',0, 'tooltipstring','', 'visible','on'); gain1_text = uicontrol('parent', uiPanel, 'style','text' ... , 'units','normalized', 'position',[0.46 0.25 0.47 0.09] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','normal', 'fontsize',8.25 , 'fontunits','pixels' ... , 'string','Gain 1 Value' ... , 'horizontalalignment','left' ... , 'verticalalignment','middle', 'foregroundcolor', [20 20 20], 'backgroundcolor', [255 255 255] ... , 'tag','gain1_text', 'userdata',[], 'value',0, 'tooltipstring','', 'visible','on'); gain2_text = uicontrol('parent', uiPanel, 'style','text' ... , 'units','normalized', 'position',[0.46 0.37 0.47 0.09] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','normal', 'fontsize',8.25 , 'fontunits','pixels' ... , 'string','Gain 2 Value' ... , 'horizontalalignment','left' ... , 'verticalalignment','middle', 'foregroundcolor', [20 20 20], 'backgroundcolor', [255 255 255] ... , 'tag','gain2_text', 'userdata',[], 'value',0, 'tooltipstring','', 'visible','on'); gain2_value = uicontrol('parent', uiPanel, 'style','edit' ... , 'callback','gain2_value_callback', 'units','normalized', 'position',[0.07 0.37 0.34 0.10] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','normal', 'fontsize',8.25 , 'fontunits','pixels' ... , 'string','0' ... , 'horizontalalignment','left' ... , 'verticalalignment','middle', 'foregroundcolor', [20 20 20], 'backgroundcolor', [255 255 255] ... , 'interruptible','off', 'tag','g2v', 'userdata',[] ... , 'value',0, 'tooltipstring','', 'visible','on'); simtime_slider = uicontrol('parent', uiPanel, 'style','slider' ... , 'callback','simtime_slider_callback', 'units','normalized', 'position',[0.13 0.62 0.80 0.07] ... , 'enable','on', 'max', 30 , 'min', 1 , 'orient','horizontal', 'sliderstep', [1 10] ... , 'tracking', 'on', 'tickinterval', 1, 'tickmarks', 'nomarks' ... , 'callbackdelay',0.000000, 'interruptible','off', 'tag','simtimev' ... , 'userdata',[], 'tooltipstring','', 'visible','on'); simtime = uicontrol('parent', uiPanel, 'style','text' ... , 'units','normalized', 'position',[0.26 0.50 0.54 0.09] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','normal', 'fontsize',8.25 , 'fontunits','pixels' ... , 'string','Simulation Time' ... , 'horizontalalignment','left' ... , 'verticalalignment','middle', 'foregroundcolor', [20 20 20], 'backgroundcolor', [255 255 255] ... , 'tag','simtime', 'userdata',[], 'value',0, 'tooltipstring','', 'visible','on'); runsim_button = uicontrol('parent', uiPanel, 'style','pushbutton' ... , 'callback','runsim_button_callback', 'units','normalized', 'position',[0.20 0.84 0.66 0.09] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','bold', 'fontsize',8.00 , 'fontunits','pixels' ... , 'string','Run Simulation', 'horizontalalignment','center' ... , 'backgroundcolor', [255 255 255], 'foregroundcolor', [20 20 20] ... , 'backgroundimage','', 'aspectratio','on' ... , 'interruptible','off', 'tag','runsim_button', 'userdata',[] ... , 'value',0, 'tooltipstring','', 'visible','on'); min_simtime = uicontrol('parent', uiPanel, 'style','text' ... , 'units','normalized', 'position',[0.13 0.69 0.14 0.10] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','normal', 'fontsize',8.00 , 'fontunits','pixels' ... , 'string','1' ... , 'horizontalalignment','center' ... , 'verticalalignment','middle', 'foregroundcolor', [20 20 20], 'backgroundcolor', [246 246 246] ... , 'tag','min_simtime', 'userdata',[], 'value',0, 'tooltipstring','', 'visible','on'); max_simtime = uicontrol('parent', uiPanel, 'style','text' ... , 'units','normalized', 'position',[0.79 0.69 0.14 0.10] ... , 'enable','on', 'fontname','MS Shell Dlg', 'fontangle','regular', 'fontweight','normal', 'fontsize',8.00 , 'fontunits','pixels' ... , 'string','30' ... , 'horizontalalignment','center' ... , 'verticalalignment','middle', 'foregroundcolor', [20 20 20], 'backgroundcolor', [246 246 246] ... , 'tag','max_simtime', 'userdata',[], 'value',0, 'tooltipstring','', 'visible','on');
-
Applyし、ExecOmlScript ブロックからスクリプトをプッシュします。