天天看点

simulink bus总线创建方法

在simulink中创建bus总线,主要包含2种方法:

基于模块创建总线对象

使用模块,根据输入信号创建总线

simulink bus总线创建方法

基于 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进行总线的创建管理。