www.gusucode.com > ros 工具箱 matlab源码程序 > ros/rospublisher.m

    function [pub, msg] = rospublisher(topic, varargin)
%ROSPUBLISHER Publish messages on a topic
%   PUB = ROSPUBLISHER('TOPIC') creates a publisher, PUB, for a topic
%   with name TOPIC that already exists on the ROS master topic list. The 
%   publisher gets the topic message type from the topic list on the ROS 
%   master. If the topic is not on the ROS master topic list, this function
%   displays an error message.
%
%   PUB = ROSPUBLISHER('TOPIC', 'TYPE') creates a publisher for a topic
%   and adds that topic to the ROS master topic list. If the ROS master
%   topic list already contains a matching topic, the ROS master adds
%   the MATLAB global node to the list of publishers for that topic.
%   If the topic type differs from the topic on ROS master topic list,
%   the function displays an error.
%
%   PUB = ROSPUBLISHER(___, Name, Value) provides additional options
%   specified by one or more Name,Value pair arguments. Name must appear
%   inside single quotes (''). You can specify several name-value pair
%   arguments in any order as Name1,Value1,...,NameN,ValueN:
%
%      'IsLatching' -  If enabled, latch mode saves the last message
%                      sent by the publisher and re-sends it to new
%                      subscribers. By default, latch mode is enabled.
%                      To disable latch mode, set Value to false.
%                      Default: true
%
%   [PUB,MSG] = ROSPUBLISHER(___) returns a message MSG that
%   you can send with the publisher PUB. The message will be
%   initialized with default values.
%
%   ROSPUBLISHER('TOPIC', MSG) publishes a message, MSG, to the specified
%   topic without creating a publisher.
%
%   Use ROSPUBLISHER to publish messages on a topic. When MATLAB's
%   global node publishes messages on that topic, ROS nodes that
%   subscribe to that topic receive those messages.
%
%
%   Example:
%      % Create publisher and send string data
%      chatpub = ROSPUBLISHER('/chatter', 'std_msgs/String');
%      msg = rosmessage(chatpub);
%      msg.Data = 'Some test string';
%      send(chatpub,msg);
%
%      % Create publisher. Message type is inferred from existing topic.
%      % Do not latch the topic.
%      latchpub = ROSPUBLISHER('/chatter', 'IsLatching', false);
%
%      % Convenience function: just send one message
%      ROSPUBLISHER('/chatter', msg);
%
%   See also robotics.ros.Publisher, ROSMESSAGE.

%   Copyright 2014-2015 The MathWorks, Inc.

try
    % Handle direct publishing quickly
    if nargin == 2 && isa(varargin{1}, 'robotics.ros.Message')
        rospublisherDirect(topic, varargin{1});
        pub = [];
        return;
    end
    
    % Create and return publisher object
    pub = robotics.ros.Publisher([], topic, varargin{:});
    
    % Assign output message, if requested
    if nargout == 2
        msg = rosmessage(pub);
    end
catch ex
    % Save stack traces and exception causes internally, but do not
    % print them to the console
    rosex = robotics.ros.internal.ROSException.fromException(ex);
    throwAsCaller(rosex);
end

end

function rospublisherDirect(topic, msg)
%rospublisherDirect Directly publish to a topic.

% Make the publisher persistent to allow messages to be
% delivered before destruction
persistent pubTmp;

% Publish message directly
pubTmp = []; %#ok<NASGU>
pubTmp = robotics.ros.Publisher([], topic, msg.MessageType);
send(pubTmp, msg);
end