www.gusucode.com > robotics 案例源码程序 matlab代码 > robotics/FixedratePublishingOfROSTimeDataExample.m
%% Fixed-rate Publishing of ROS Time Data % This example shows how to regularly publish and receive time messages % using ROS time. The |rosrate| function creates a |Rate| object to access % the |/clock| topic on the ROS network. The clock data is reconstructed as % an array to be republished on the network at a regular rate. Parameters % such as the IP address and topic names vary with your robot and setup. %% % Connect to ROS network. Setup subscriber, publisher, and data message. ipaddress = '172.28.194.188'; % Replace with your network address rosinit(ipaddress) sub = rossubscriber('/clock'); pub = rospublisher('/clock2','std_msgs/Int32MultiArray'); msg = rosmessage('std_msgs/Int32MultiArray'); %% % Receive the first clock message and publish data as an array. msgClock = receive(sub); msg.Data = [msgClock.Clock_.Sec, msgClock.Clock_.Nsec]; send(pub,msg) %% % Create an array to store time hits for verification. timeHits = zeros(10,2); %% % Create ROS |Rate| object to execute at a 100 Hz. Set a loop time and the % |OverrunAction| for handling desiredRate = 100; loopTime = 0.1; overrunAction = 'slip'; rate = rosrate(desiredRate); r.OverrunAction = overrunAction; %% % Begin loop to receive, process and send messages every 0.01 seconds (10 % Hz). Reset the |Rate| object before beginning. reset(rate) for i = 1:desiredRate*loopTime msgClock = receive(sub); msg.Data = [msgClock.Clock_.Sec, msgClock.Clock_.Nsec]; timeHits(i,:) = msg.Data; send(pub,msg); waitfor(rate); end %% % View the time hits for loop iterations. You can see that the loop % executes at 100 Hz for the 10 iterations. timeHits %% % Shut down ROS node rosshutdown