www.gusucode.com > robotsimulink 工具箱 matlab源码程序 > robotsimulink/robotslros/+robotics/+slros/+internal/+bus/createBusDefnInBaseWorkspace.m

    function createBusDefnInBaseWorkspace(emptyRosMsg, model)
%This function is for internal use only. It may be removed in the future.

%createBusDefnInBaseWorkspace - Create Simulink bus object in base workspace
%
%   createBusDefnInBaseWorkspace(MSG, MODEL) creates the Simulink bus
%   objects in the base workspace  corresponding to the ROS message MSG and 
%   any nested messages inside it, using the variable-length array size
%   information from MODEL. MSG should  be an empty message (i.e., created
%   using ROSMESSAGE without any subsequent assignments), since the only
%   way to determine if a property is a variable-length array is to check
%   if its value is [].
%
%   createBusDefnInBaseWorkspace(MSG) creates the bus object using default
%   sizes for variable-length arrays.
%
%   Note: 
%   * If MODEL is a Simulink block library, the default sizes are
%     applied.

%   Copyright 2014 The MathWorks, Inc.

if exist('model', 'var') && ~isempty(model)
    assert(ischar(model));    
    isLibraryContext = strcmpi(get_param(bdroot(model), 'BlockDiagramType'), 'library');
    nullModel = isLibraryContext;
else
    nullModel = true;
end

if nullModel
    model = '';
    varlenArrayStore = [];  
else
    varlenArrayStore = robotics.slros.internal.bus.VarlenArraySizeStore(model);
end

if ~isempty(varlenArrayStore)
    truncateAction = varlenArrayStore.getTruncateAction();
else
    truncateAction = robotics.slros.internal.bus.VarlenArraySizeStore.getDefaultTruncateAction();
end

requiredBuses = robotics.slros.internal.bus.getBusDefnForROSMsg(emptyRosMsg, model);
createVarlenInfoBus = false;
for i = 1:numel(requiredBuses)
    bus = requiredBuses(i).Bus;
    elemInfo = robotics.slros.internal.bus.BusItemInfo( bus.Description );
    msgType = elemInfo.MsgType;
    busname = robotics.slros.internal.bus.Util.rosMsgTypeToBusName(msgType, model);
        
    varlenInfo = getVariableLengthArrayInfo(bus);
    if numel(varlenInfo.ArrayProps) > 0
        if ~isempty(varlenArrayStore)
            % this will automatically apply user-customizations if present
            varlenArrayStore.applyMaxLengths(varlenInfo);
        else
            robotics.slros.internal.bus.VarlenArraySizeStore.applyDefaultMaxLengths(varlenInfo);
        end
        updatedBus = updateVarlenArrayLimits(bus, varlenInfo, truncateAction);
        assignin('base', busname, updatedBus);
        createVarlenInfoBus = true;
    else
        % no variable-length arrays
        assignin('base', busname, bus);
    end
end

if createVarlenInfoBus
    robotics.slros.internal.bus.Util.createVarlenInfoBusIfNeeded();
end

end


%%
function info = getVariableLengthArrayInfo(bus)
% Get max-lengths for variable-length arrays

businfo = robotics.slros.internal.bus.BusItemInfo( bus.Description );
info = robotics.slros.internal.bus.VarLenArrayInfo(businfo.MsgType);

for j = 1:numel(bus.Elements)
    elem = bus.Elements(j);
    elemInfo = robotics.slros.internal.bus.BusItemInfo( bus.Elements(j).Description );
    
    if elemInfo.IsVarLen &&  strcmpi(elemInfo.VarLenCategory, 'data')
        s.PropertyName = elem.Name;
        if ~isempty(elemInfo.MsgType)
            s.DataType = elemInfo.MsgType;
        else % primitive type
            s.DataType = elem.DataType;
        end
        s.MaxLength = 0;
        info.addArrayProp(s);
    end
    
end

end


%%
function [bus, requiresVarlenInfoBus] = updateVarlenArrayLimits(bus, varlenInfo, truncateAction)

requiresVarlenInfoBus = false;
varlenProps = {varlenInfo.ArrayProps.PropertyName};
for j = 1:numel(bus.Elements)
    elemInfo = robotics.slros.internal.bus.BusItemInfo( bus.Elements(j).Description );
    
    if elemInfo.IsVarLen  &&  strcmpi(elemInfo.VarLenCategory, 'data')
        assert(strcmpi(bus.Elements(j).DimensionsMode, 'Fixed'));        
        idx = find(strcmp(varlenProps, bus.Elements(j).Name));
        assert(numel(idx)==1);
        bus.Elements(j).Dimensions = varlenInfo.getMaxLength(idx);
        
        if truncateAction == robotics.slros.internal.bus.VarLenArrayTruncationAction.EmitWarning
            elemInfo.TruncateAction = 'warn';
            bus.Elements(j).Description = elemInfo.toDescription();
        end
        
        requiresVarlenInfoBus = true;
    end
    
end

end