在simulink中建立bus總線,主要包含2種方法:
基于子產品建立總線對象
使用子產品,根據輸入信号建立總線
基于 MATLAB 資料建立總線對象
可以使用 Simulink.Bus.cellToObject 函數,基于 MATLAB 中的總線元胞數組資訊建立總線對象。每個元胞子元胞數組代表一個總線對象,并包括反映 Simulink.Bus 對象屬性的以下資料:
{BusName,HeaderFile,Description,DataScope,Alignment,Elements}
Elements 字段是為每個 Simulink.BusElement 對象定義以下屬性的元胞數組:
{ElementName,Dimensions,DataType,SampleTime,Complexity,DimensionsMode,Min,Max,Units,Description}
其中FirmamentPilot開源MBD飛控工程便是采用基于MATLAB資料建立總線對象,如控制器輸出PWM的bus代碼如下:
function cellInfo = control_bus(varargin)
% CONTROL_BUS returns a cell array containing bus object information
%
% Optional Input: 'false' will suppress a call to Simulink.Bus.cellToObject
% when the MATLAB file is executed.
% The order of bus element attributes is as follows:
% ElementName, Dimensions, DataType, SampleTime, Complexity, SamplingMode, DimensionsMode, Min, Max, DocUnits, Description
suppressObject = false;
if nargin == 1 && islogical(varargin{1}) && varargin{1} == false
suppressObject = true;
elseif nargin > 1
error('Invalid input argument(s) encountered');
end
cellInfo = { ...
{ ...
'Control_Out_Bus', ...
'', ...
'', ...
'Auto', ...
'-1', {...
{'timestamp', 1, 'uint32', -1, 'real', 'Sample', 'Fixed', [], [], sprintf('ms'), ''}; ...
{'actuator_cmd', 16, 'uint16', -1, 'real', 'Sample', 'Fixed', [], [], '', ''}; ...
} ...
} ...
}';
if ~suppressObject
% Create bus objects in the MATLAB base workspace
Simulink.Bus.cellToObject(cellInfo)
end
項目源碼位址:https://github.com/FirmamentPilot
使用.m檔案進行分子產品管理總線,在自動代碼生成時友善生成各總線對應的頭檔案,但需進行總線m檔案和啟動加載m檔案的編寫,下篇推文介紹使用bus editor與data dictionary進行總線的建立管理。