ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Create publisher each time a service is called

asked 2022-06-20 06:32:24 -0500

devjav9 gravatar image

I have a node that offers some services for sending commands to different devices.

Each one of these devices have an identifier, so when a service is called, you have to send the identifier and then, this node will send an String message to the topic json_cmd/ + identifier.

I'm doing this right now inside the callback of each service:

def command(self, req):
    template_copy = copy.deepcopy(self.command_template)
    template_copy["timeStamp"]   = self.__get_timestamp()
    template_copy["commandUUID"] = str(uuid.uuid1())
    template_copy["commandType"] = CommandList.PAUSE

    publisher = rospy.Publisher("json_cmd/" + req.id, String, queue_size=10)
    publisher.publish(json.dumps(template_copy))
    return True

But I'm not sure if it's a good practice to create the publisher each time or if there's a way to publish once without creating an object.

Also it's important to say that I don't know all the identifiers at the beginning, so I can't create the publishers on start.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-06-20 17:05:32 -0500

Mike Scheutzow gravatar image

updated 2022-06-20 17:12:34 -0500

You are going to have huge message-delivery problems with this design. If you have a remote master node, or the master is otherwise busy, it can take 100's of milliseconds to create a publisher. Any messages sent to a publisher object before the proper connections are established are simply discarded on the sending side (this is not a bug - ros1 does not guarantee message delivery.)

Creating a publisher-object is quite a bit more expensive than just holding the publisher-object open. Of course, if you create an insane number of publishers, that's going to be a different problem.

And lastly: ros service calls are not very efficient, and are not meant to be used for high-rate communication or activities. If your ros service is being called even once a second, that is too often. You need to figure out a more efficient approach.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2022-06-20 06:17:23 -0500

Seen: 70 times

Last updated: Jun 20 '22