Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscribe to topic by specifying the Type as a string #447

Open
3 tasks done
sosborne-control2k opened this issue Sep 7, 2023 · 0 comments
Open
3 tasks done

Subscribe to topic by specifying the Type as a string #447

sosborne-control2k opened this issue Sep 7, 2023 · 0 comments

Comments

@sosborne-control2k
Copy link

sosborne-control2k commented Sep 7, 2023

  • I am at the right place and my issue is directly related to ROS#. General technical questions I would post e.g. at ROS Answers or Stack Overflow. For library-specific questions I would look for help in the corresponding library forums.
  • I have thoroughly read the Contributing Guideline and writing this issue is the right thing to do in my case.

I have a question!

  • I searched the Wiki, open and closed issues for an answer. I tried my best to find the answer by myself without success. I believe that the discussion we will have in this issue, and the solutions we might find, will help me, and likely other community members who have a similar problem.

Here is my question:
I am trying to connect to and subscribe to Topics on ROSbridge. It works fine if i specify the Type of message upon subscribing as per the code example in the Test Client:

          rosSocket = new RosSocket(new RosBridgeClient.Protocols.WebSocketNetProtocol(uri), RosSocket.SerializerEnum.Newtonsoft_JSON);
string subscription_id = rosSocket.Subscribe<JointState>("/joint_states", SubscriptionHandler);

however I wish to be able to specify the topic and type by string and find that type and call the subscribe method.

I know I can do it with an exhaustive Switch e.g.:

        public static string Subscribe(string type, string topic) 
        {
            switch (type)
            {
                case "sensor_msgs/JointState":
                    return rosSocket.Subscribe<JointState>(topic, SubscriptionHandler);
                    break;
                case "actionlib_msgs/GoalID":
                    return rosSocket.Subscribe<GoalID>(topic, SubscriptionHandler);
                    break;
            }
            return "";
        }

But this will get pretty long and unmanageable especially if i add new Message types for different robots. I have therefore spent some time trying to use reflection to get the type to invoke the Subscribe call, but I seem to be falling over when trying to pass in the Subscription handler. Has anyone done similar, or provide an example of how to do this - many thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant