allows you to control the rate of your code execution based on the ROS Time
/clock topic or system time on your computer. By executing code at
constant intervals, you can accurately time and schedule tasks. These examples show
different applications for the
rosrate object including its uses with ROS
image messages and sending commands for robot control.
For other applications based on system time, consider the
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.
Initializing ROS master on http://bat1073807glnxa64:35047/. Initializing global node /matlab_global_node_55860 with NodeURI http://bat1073807glnxa64:45351/
Setup publisher and message for sending
[pub,msg] = rospublisher('/cmd_vel','geometry_msgs/Twist'); msg.Linear.X = 0.5; msg.Angular.Z = -0.5;
Rate object with specified loop parameters.
desiredRate = 10; rate = robotics.Rate(desiredRate); rate.OverrunAction = 'drop'
rate = rateControl with properties: DesiredRate: 10 DesiredPeriod: 0.1000 OverrunAction: 'drop' TotalElapsedTime: 0.3892 LastPeriod: NaN
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.
ans = struct with fields: Periods: [1x100 double] NumPeriods: 100 AveragePeriod: 0.1000 StandardDeviation: 0.0019 NumOverruns: 0
Shut down ROS network
Shutting down global node /matlab_global_node_55860 with NodeURI http://bat1073807glnxa64:45351/ Shutting down ROS master on http://bat1073807glnxa64:35047/.
This example shows how to regularly publish and receive image messages using ROS and the
rosrate function. The
rosrate function creates a
Rate object to regularly access the
/camera/rgb/image_raw topic on the ROS network using a subscriber. The rgb image is converted to a grayscale using
rgb2gray and republished at a regular interval. 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 = '192.168.203.129'; % Replace with your network address rosinit(ipaddress)
Initializing global node /matlab_global_node_10650 with NodeURI http://192.168.203.1:50899/
sub = rossubscriber('/camera/rgb/image_raw'); pub = rospublisher('/camera/gray/image_gray','sensor_msgs/Image'); msgGray = rosmessage('sensor_msgs/Image'); msgGray.Encoding = 'mono8';
Receive the first image message. Extract image and convert to a grayscale image. Display grayscale image and publish the message.
msgImg = receive(sub); img = readImage(msgImg); grayImg = rgb2gray(img); imshow(grayImg)
Rate object to execute at 10 Hz. Set a loop time and the
OverrunAction for handling
desiredRate = 10; loopTime = 5; overrunAction = 'slip'; rate = rosrate(desiredRate); r.OverrunAction = overrunAction;
Begin loop to receive, process and send messages every 0.1 seconds (10 Hz). Reset the
Rate object before beginning.
reset(rate) for i = 1:desiredRate*loopTime msgImg = receive(sub); img = readImage(msgImg); grayImg = rgb2gray(img); writeImage(msgGray,grayImg) send(pub,msgGray) waitfor(rate); end
View the statistics for the code execution.
StandardDeviation show how well the code maintained the
OverRuns occur when data processing takes longer than the desired period length.
ans = struct with fields: Periods: [1×50 double] NumPeriods: 50 AveragePeriod: 0.1000 StandardDeviation: 0.0083 NumOverruns: 0
Shut down ROS node
Shutting down global node /matlab_global_node_10650 with NodeURI http://192.168.203.1:50899/