Madalynn asked . 2021-03-25

Populating ros messages is slow

I noticed that populating ROS messages with values is quite slow. Filling 16 fields of a message takes in average around 2.2 ms. While this is not that much on its own, it quickly adds up in a control loop. The message in question is a control command which has to be sent at around 10 Hz to at least 10 different robots. That means at least a fifth of the control interval is spent on "creating" the messages. Together with the issue that rostime("now") is slow too (https://www.mathworks.com/matlabcentral/answers/658878-rostime-call-is-slow?s_tid=srchtitle), this leaves almost no time for the actual computation of the control commands.
 
I wonder if I am doing something fundamentally wrong or if the Matlab ROS API is simply that slow?
 
Script to time population of ROS message:
cmdFullStateMsg = rosmessage("crazyflie_driver/FullState");

pos = ones(3, 1);
vel = ones(3, 1);
acc = ones(3, 1);
yaw = 1;
omega = ones(3, 1);
quat = eul2quat([0, 0, yaw], 'xyz');

tic
for i = 1:1000  
    cmdFullStateMsg.Pose.Position.X    = pos(1);
    cmdFullStateMsg.Pose.Position.Y    = pos(2);
    cmdFullStateMsg.Pose.Position.Z    = pos(3);
    cmdFullStateMsg.Twist.Linear.X     = vel(1);
    cmdFullStateMsg.Twist.Linear.Y     = vel(2);
    cmdFullStateMsg.Twist.Linear.Z     = vel(3);
    cmdFullStateMsg.Acc.X              = acc(1);
    cmdFullStateMsg.Acc.Y              = acc(2);
    cmdFullStateMsg.Acc.Z              = acc(3);
    cmdFullStateMsg.Pose.Orientation.W = quat(1);
    cmdFullStateMsg.Pose.Orientation.X = quat(2);
    cmdFullStateMsg.Pose.Orientation.Y = quat(3);
    cmdFullStateMsg.Pose.Orientation.Z = quat(4);
    cmdFullStateMsg.Twist.Angular.X    = omega(1);
    cmdFullStateMsg.Twist.Angular.Y    = omega(2);
    cmdFullStateMsg.Twist.Angular.Z    = omega(3);   
end
cmd_full_state_msg_time = toc / 1000

matlab , signal , signal processing , ros, performance

Expert Answer

John Williams answered . 2024-05-17 20:39:02

Hello ,
 
Cause of Performance Issues
 
Accessing Nested Messages (pre-R2020b)
 
Based on your other question, I assume you are using R2019a. All releases up through R2020a use rosjava under the hood, and the messages contain Java message objects. This means that as you use fields of deeply-nested messages, for either setting or getting data,  MATLAB has to do several conversions of message objects.
 
For example, when you call :
 
 
cmdFullStateMsg.Pose.Position.X = pos(1)
then MATLAB:
  • Accesses the geometry_msgs/Pose Java message object from the "Pose" property
  • Converts that to the MATLAB robotics.ros.msggen.geometry_msgs.Pose object
  • Accesses geometry_msgs/Point Java message object from the "Position" property
  • Converts that to the MATLAB robotics.ros.msggen.geometry_msgs.Point object
  • Accesses the "X" property of the underlying geometry_msgs/Point Java message and assigns it to the provided value
  • Assigns the appropriate properties of the various objects all the way up the nested tree
Multiple Calls to rosmessage
 
Additionally, any call to rosmessage requires the construction of the object and its underlying Java message. Limiting the number of calls is advantageous when trying to improve performance.
 
Solutions
Disclaimer
 
The performance improvement is just what I'm seeing through casual measurement (tic-toc) on my machine in particular. You may not see the same improvement depending on your machine, resources, environment, code structure or other factors.
 
Extract Nested Messages Before Setting Values
 
When setting multiple fields in a single message that is deeply nested, it can help a lot to pull out the message manually and set the fields. Since mesage objects in MATLAB are handles, you don't need to reassign it to the nested field either.
 
Testing with nav_msgs/Odometry (which is similar, if not quite the same as your message), I compared running this:
 
 
m = rosmessage("nav_msgs/Odometry");
m.Pose.Pose.Position.X = 1;
m.Pose.Pose.Position.Y = 2;
m.Pose.Pose.Position.Z = 3;
m.Twist.Twist.Linear.X = 4;
m.Twist.Twist.Linear.Y = 5;
m.Twist.Twist.Linear.Z = 6;
m.Header.Stamp.Sec = 7;
m.Header.Stamp.Nsec = 8;
m.Header.FrameId = '';
m.Pose.Pose.Orientation.W = 1;
m.Pose.Pose.Orientation.X = 2;
m.Pose.Pose.Orientation.Y = 3;
m.Pose.Pose.Orientation.Z = 4;
m.Twist.Twist.Angular.X = 5;
m.Twist.Twist.Angular.Y = 6;
m.Twist.Twist.Angular.Z = 7;

To this:

m = rosmessage("nav_msgs/Odometry");
pose = m.Pose.Pose;
twist = m.Twist.Twist;
posePos = pose.Position;
poseOri = pose.Orientation;
twistLin = twist.Linear;
twistAng = twist.Angular;
head = m.Header;
posePos.X = 1;
posePos.Y = 2;
posePos.Z = 3;
twistLin.X = 4;
twistLin.Y = 5;
twistLin.Z = 6;
head.Stamp.Sec = 7;
head.Stamp.Nsec = 8;
head.FrameId = '';
poseOri.W = 1;
poseOri.X = 2;
poseOri.Y = 3;
poseOri.Z = 4;
twistAng.X = 5;
twistAng.Y = 6;
twistAng.Z = 7;</pre.

in a loop of 1000 iterations, and got about a 2X improvement with the second code. It's not ground-breaking, but it's significant enough to be worth doing.

Reuse Message Objects
 
A large part of this time is spent running rosmessage. If you only are using this message for immediate sending (and aren't trying to store data for later or something), and you are always overwriting the same values, you can just reuse the same message object again and again. Call rosmessage outside of the control loop and make it available to the inside in some way. Set the values on the inside and send the message. The values will remain the same when the next iteration comes up, so they'll need to all be overwritten again, but if you're doing that anyway it's not an additional cost. Testing with this method, showed an 5X improvement over the original code.
 
If you're going for every bit of performance possible, you can even extract all of the inner fields you're setting initially (after calling rosmessage), then pass those variables to the control loop to set. This eeked into the 6X improvement range over the original code, but it was a pretty minimal difference.
 
Use R2020b+
 
Additionally, if you combine these tips with updating to R2020b (if that is possible for you), I'm seeing around a 3X-5X improvement in control loop runtime between the two versions running the same code. This is because R2020b no longer uses a Java back-end, and so message fields are being retrieved and set directly in the MATLAB objects. Definitely create the rosmessage outside of the control still, as that nets the biggest improvement in performance.


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.