Suppose I am creating a custom message type from my ROS device and publishing it under one topic(This topic might be a dictionary with different data types). Now on simulink on a different computer, can I subscribe to this topic with this custom message? Does simulink automatically understand the different data types? Or do I have to do something else?
PS. I am using ROS2

Réponses (1)

Josh Chen
Josh Chen le 24 Avr 2023

0 votes

Hello Rahul,
In order for Simulink to subscribe to the topic and receive custom messages, you need to provide message definition and build the ROS 2 package same as what you need to do with a rclcpp ROS 2 node on a different computer.
To begin with, you can refer to the function ros2genmsg, the example in this page shows you how to use ros2genmsg to build a ROS 2 package containing definition of messages.
In your use case, you can:
  1. copy message (service, action if you have) files to the computer that runs MATLAB/Simulink
  2. navigate to that folder and use ros2genmsg to generate custom messages from ROS 2 definitions
  3. use the following command to check whether they are available:
>> ros2 msg list
4. Create a ROS 2 Subscribe block and select the topic, message type you would like to subscribe
Hope this helps,
Thanks,
Josh

6 commentaires

Rahul
Rahul le 24 Avr 2023
Hello Josh,
Suppose I follow the steps you gave me to create a custom message package. If I generate code of my simulink model with the subscriber block after creating the message pacakge, will this code run on my ROS machine and be able to subscribe to that data with the same topic name and fixed format?
Or are there more steps needed?
Josh Chen
Josh Chen le 24 Avr 2023
Hi Rahul,
Is this "ROS machine" same as the one you are publishing the message from? If yes, it should already built message packages. In that case, the generated code should work.
-Josh
Rahul
Rahul le 26 Avr 2023
Yes,
For example I am publishing on a topic from one ROS machine. (With some custom message type)
Now on MATLAB(different device from the ROS machine), I can create a ros2 msg package with that custom message type(the definition).
Next, On simulink I use a subscriber block, and choose this new custom message type(defined in matlab), as well as the topic name( which is the same as the one being published from my ros device)..
Now after I do so, I can generate code from this SIMULINK model(already configured with the subscriber and the correct topic name) and then deploy this code on the ROS device(the one that is publishing) and it should work?
Also, in the subscriber parameters block, there is an option for Topic source: Select from network.. Do i need to connect to my ROS device before code generating? or does this serve some other purpose.
Josh Chen
Josh Chen le 26 Avr 2023
Hi Rahul,
It should work.
The "Select from network" is a convenience feature for you to get topic name and message type information from the current ROS/ROS 2 network. If you are publishing message from your ROS machine at the time you configure the Simulink model, you should be able to see that topic shows up when you click the "Select..." button.
If, on the other hand, you want to manually specify the topic name and message type, you can also pick "Specify your own" as "Topic source", and specify your own topic name and message type.
-Josh
Rahul
Rahul le 26 Avr 2023
Thank you very much!
If im doing a "Select from network" on Simulink, and I'm still publishing a custom message from my ROS machine. Is it necessary that I define the message type on MATLAB? Or can Simulink "Understand" the custom message type(Since you said that it is a convenience feature) or does this only work for the message types available on Simulinlk.
Josh Chen
Josh Chen le 26 Avr 2023
you still need to define message type on MATLAB. There are some dynamic library files need to be generated with ros2genmsg so that Simulink can refer to.

Connectez-vous pour commenter.

Produits

Version

R2022b

Question posée :

le 22 Avr 2023

Commenté :

le 26 Avr 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by