Is it possible to parse json file based on ROS2 msg file

asked 2020-12-10 04:36:16 -0600

nishal gravatar image

Hi,

I am planning to have a json file that will tell ros2 channel and ros2 message type. So Is there a way that this json file can be parsed at compiled at runtime, though it can be done at runtime? Or are there any better methods available to do so?

Alternatively, We can have an application that takes JSON file and parse them to generate a valid json file.

Thanks in advance!

edit retag flag offensive close merge delete

Comments

I'm not entirely sure what you are trying to do, but at least for the topic name, you should be able to use the remapping functionality (so you don't have to specify it in JSON). That is, you can run your node like:

ros2 run my_package my_node --ros-args -r topic_name:=new_topic_name

And your topic will show up as new_topic_name.

If you are trying to accomplish something else, can you give some more details of what it is you are trying to accomplish?

clalancette gravatar image clalancette  ( 2020-12-11 15:14:42 -0600 )edit

Hi,

To give more context here is some more details * I will have few ros2 nodes with custom messages define in msg(.msg) folder * I will have a different topic and msg defined for different usecase. * To be dynamic, I will provide this configuration using a JSON file * The application shall read this json file and configure the ros2 nodes, channels, and message types * But, all this will happen in runtime

So, I wanted to know if during build time or while generating the JSON file if I can validate the message types that I specify in the arguments

Ex JSON file { "Topic": "/nodes/MyTopic1", "Message":"my_message1" }

Where I have 2 messages defined in IDL msg/my_message1 msg/my_message2

Here, "my_message1" is valid message type, however if I give "MyMessage1", this is not a valid message type in my project.

Thanks in advance!

nishal gravatar image nishal  ( 2020-12-13 21:17:46 -0600 )edit

Here are some things I would do differently:

  1. You shouldn't be specifying topic names dynamically like this. ROS 2 has built-in support for topic renaming. You should use that support wherever possible, since it makes your code look and feel like other ROS 2 code. It also makes it easier to do remapping in launch files, etc.
  2. It's usually not a good idea to make a topic have a dynamic message type. It makes it hard to document exactly what your node subscribes to and publishes to, since it can change on the fly. There are exceptions to this rule (if you are writing a tool that wants to talk to any topic), but generally you should keep one topic one topic type.

Instead of using a JSON file to configure all of this, I have two different suggestions: 1. Write two separate nodes, one that deals with ...(more)

clalancette gravatar image clalancette  ( 2020-12-14 09:31:28 -0600 )edit