Simulink仿真入門到精通(十四) Simulink自定義環境


14.1 Simulink環境自定義功能

sl_sustomization.m函數是Simulink提供給用戶使用MATLAB語言自定義Simulink標准人機界面的函數機制。若sl_sustomization.m函數存在於MATLAB的搜索路徑中,在當Simulink啟動時就會讀取此文件的內容進行Simulink的人機界面的初始化。Simulink本身就提供了這個函數,用戶每次修改之后,必須重啟Simulink或者使用命令sl_refresh_customizations使變更起作用。

參考:https://www.mathworks.com/help/simulink/ug/registering-customizations.html

Reading and Refreshing the Customization File

The sl_customization.m file is read when Simulink starts. If you change the sl_customization.m file, either restart Simulink or enter this command to see the changes:

sl_refresh_customizations

This command runs all sl_customization.m files on the MATLAB path and in the current folder. Some side-effects of running sl_refresh_customizations include:

  • Rebuilding the Simulink Toolstrip

  • Rebuilding all Simulink Editor menus

  • Rebuilding the Library Browser menus and toolbars

  • Clearing the Library Browser cache and refreshing the Library Browser

  • Reloading the Viewers and Generators Manager data

14.2 Simulink工具欄菜單自定義

可以在Simulink提供的現有菜單欄的基礎上進行菜單項的添加。Simulink Model Editor中用於添加菜單項的位置有3個:頂層菜單的末尾、菜單欄和右鍵菜單的開始或結尾處。

添加的對象成為項目(item),為了添加項目,需要以下步驟:

  1. 創建一個定義項目的模式函數(schema function);
  2. 將這個定義菜單項目的函數注冊在sl_customization.m中;
  3. 為這個菜單項目定義一個觸發運行的回調函數。

例:增加一個顯示當前所選模塊屬性列表的菜單項目。

首先創建一個定義此項的模式函數。

function schema = get_block_property(callbackInfo)
schema = sl_action_schema;    %使用sl_action_schema函數創建一個對象
schema.label = 'block property';
schema.userdata = 'Custom';
schema.callback = @custom_callback;
end

選中此菜單時觸發回調函數 custom_callback,顯示當前選中的模塊的屬性列表。

function custom_callback(callbackInfo)
inspect(gcbh);
disp('### The property of current block is displayed.');
end

接着將定義的函數注冊到sl_customization.m中。

以下代碼將Simulink菜單欄以及子層菜單的WidgetId顯示出來。 

使用addCustomMenuFcn方法可以注冊一個自定義菜單項目函數。

function sl_customization(cm)
%% Register custom menu function.
cm.addCustomMenuFcn('Simulink:ToolsMenu', @custom_items);
end

在注冊的cuntom_items函數中使用句柄函數方式,亦成為匿名函數。

%% Define the custom menu function.
function schemaFcns = custom_items(callbackInfo)
schemaFcns = {@get_block_property};
end

這樣,就在Tools菜單最末一項得到所設定的菜單項。

注:函數custom_items、get_block_property和custom_callback的參數callbackInfo並沒有被使用,可使用~表示。

自定義Model Editor的菜單不僅可以是一級菜單,還可以是多級菜單。此時需要在sl_container_schema函數創建一個容器對象,並將剛才創建的菜單項目作為其成員添加進去。

%% Define the schema function for first menu item.
function schema = menu_control(callbackInfo)
schema = sl_container_schema;
schema.label = 'Customized';
schema.childrenFcns = {@get_block_property;}
end

在sl_costomization.m中注冊菜單項目模式函數時將最上層的模式容器對象注冊進去。即將@get_block_property改為@menu_control。

自定義菜單項也可以添加到右鍵菜單中。只需將sl_customization.m中addCustomMenuFcn函數的首個參數'Simulink:ToolMenu'改為'Simulink:ContextMenu'即可。

14.3 Simulink Library Browser菜單欄自定義

可參考:https://www.mathworks.com/help/simulink/ug/customizing-the-library-browser.html

Simulink Library Browser菜單欄也可以添加用戶自定義的菜單,方法同前。

更改各個Simulink工具線的順序:Simulink Library Browser中各個工具箱的排列順序是內置的,由優先級和名稱2個因素決定。優先級數字越小工具箱排位越靠前;對於同一優先級的工具箱按照字母順序排列。默認情況下Simulink庫優先級為-1,其他工具箱優先級為0。

cm.LibraryBrowserCustomizer.applyOrder({'Embedder Coder',-2});

該命令將Embedded Coder工具箱顯示在首位。

cm.LibraryBrowserCustomizer.applyFilter({'Embedded Coder','Hidden'});
cm.LibraryBrowserCustomizer.applyFilter({'Simulink','Disabled'});
sl_refresh_customizations

以上代碼將Embedded Coder工具箱隱藏,將Simulink工具箱設置為不可用。

14.4 Simulink目標硬件自定義

在Configuration Parameter對話框中的Hardware Implementation子頁面可以選擇各個廠家提供的各種目標硬件芯片類型,設置其大小端方式、所支持的各種數據類型的位數等。

在sl_customization.m中使用loc_register_device注冊一個函數,在這個函數中對硬件的屬性進行描述即可。描述目標硬件的結構體對象需要使用RTW.HWDeviceRegistry創建並配置。需要配置的成員信息有:

配置成員名 說明
Vendor 芯片生產商
Type 芯片系列號或類型名
Alias 芯片別名,不使用時不用設置,為空
Platform Production hardware以及Test hardware兩個組別中是否都顯示控件清單,二者組別名分別以字符串'prod','test'表示
setWordSizes 設置各種數據類型的位數,按照長度順序char≤short≤int≤long,long以外類型位數必須為8的整數倍,最大為32位,long型位數不能少於32位
LargestAtomicInteger 最大原子整數,生成代碼中存在數據拷貝時使用,若對位數大於此選項設置的數據進行整數數據拷貝,則檢測數據拷貝完整性體現在代碼生成中
LargestAtomicFloat 最大原子浮點數,生成代碼中存在數據拷貝時使用,若對位數大於此選項設置的數據進行浮點數數據拷貝,則檢測數據拷貝完整性體現在代碼生成中
Endianess 字節次序設定,打斷big或小端little
IntDivRoundTo 有符號整數進行除法時的四舍五入方法選擇,'zero','floor'或'undefined'
ShiftRightIntArith true/false,設置是否將有符號整數的右移操作作為算數右移
setEnabled 選擇此目標硬件時哪些GUI控件是可選的

例:描述一個虛擬目標硬件商Custom提供的SimulinkType系列芯片。

function sl_customization(cm)
cm.registerTargetInfo(@loc_register_device);

end

% loc_register_devie resigters self defined device into Simulink Parameter
% cofiguration
function thisprod = loc_register_device
  thisprod = RTW.HWDeviceRegistry;
  thisprod.Vendor = 'Custom';
  thisprod.Type = 'Simulink Type';
  thisprod.Alias = {};
  thisprod.Platform = {'Prod','Test'};
  thisprod.setWordSizes([8 16 16 32 32]);
  thisprod.LargestAtomicInteger = 'Char';
  thisprod.LargestAtomicFloat = 'Float';
  thisprod.Endianess = 'Big';
  thisprod.IntDivRoundTo = 'floor';
  thisprod.ShiftRightIntArith = true;
  thisprod.setEnabled({'BitPerPointer'});
end

刷新后可以看到vendor中新增了Custom選項,上述配置已添加進去。

14.5 Simulink參數對話框控制

Simulink環境下的對話框也可以通過屬性設置的方法進行一定的自定義。

實現步驟是首先在sl_customization.m中使用addDlgPreOpenFcn方法注冊一個回調函數,回調函數綁定在目標控件的父對象上。再在回調函數中編寫設置目標控件屬性的M語句。對一個控件的回調函數需要注冊到其父對象的ConfigSet中,本質就是在其父控件被打開之前調用這個自定義的回調函數實現某控件的使能及可見性設置。

例:將SolverType隱藏,將仿真StartTime變為不可編輯。

function sl_customization(cm)
cm.addDlgPreOpenFcn('Simulink.SolverCC',@disable_solver_type);

end

function disable_solver_type(dialogH)
dialogH.hideWidgets({'Simulink.SolverCC.SolverType'});
dialogH.disableWidgets({'Simulink.SolverCC.StartTime'});
end


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM