www.gusucode.com > robotics 案例源码程序 matlab代码 > robotics/SendFixedrateControlCommandsToARobotExample.m

    %% Send Fixed-rate Control Commands To A Robot 
% This example shows to send regular commands to a robot at a fixed rate.
% It uses the |Rate| object to execute a loop that publishes
% |std_msgs/Twist| messages to the network at a regular interval.
%%
% Setup ROS network. Specify the IP address if your ROS network already exists.
rosinit

%%
% Setup publisher and message for sending |Twist| commands.
[pub,msg] = rospublisher('/cmd_vel',rostype.geometry_msgs_Twist);
msg.Linear.X = 0.5;
msg.Angular.Z = -0.5;

%%
% Create |Rate| object with specified loop parameters.
desiredRate = 10;
rate = robotics.Rate(desiredRate);
rate.OverrunAction = 'drop'

%%
% Run loop and hold each iteration using |waitfor(rate)|. Send the |Twist|
% message inside the loop. Reset the |Rate| object before the loop to reset
% timing.
reset(rate)

while rate.TotalElapsedTime < 10
   send(pub,msg)
   waitfor(rate);
end

%% 
% View statistics of fixed-rate execution. Look at |AveragePeriod| to verify
% the desired rate was maintained.
statistics(rate)

%%
% Shut down ROS network
rosshutdown