Query Regarding ROS Service’s call back function

I have a query regarding a Service call function. Please refer the code at the end.

  1. When I create a Service, with a call back function, and put the script in loop using rospy.spin() ,does it means that the service '/move_bb8_in_circle' will continue to persist?

  2. Also I checked with the rostopic info /cmd_vel , the publisher node “bb8_move_node” continue to exist even when the publisher object is getting created inside the service callback function. Doesn’t the service callback function is called whenever the service is called for example when a client script is run for this service( #I am referring to Exercise 2 , Unit 6: Services in ROS: Servers & Messages) . The “bb8_move_node” remains as a publisher until I close the launch file to run the below script. The other launch file which I used to call the client to this service exits after running the code once.

I am a bit confused with the function call flow here. My query is why the publisher persists when I check the info for /cmd_vel even when the publisher object is getting created inside the callback function. Shouldn’t it be created once, publish the Twist values once, and then it gets removed?


def bb8_service_callback(request):
    print("My_callback has been called")
    return EmptyResponse()


rospy.loginfo("Service Ready")



Once you created a service, it will be available until the server node dies.

As for the creation of a publisher inside a callback, I would recommend against it unless you can’t access it from outside the function.

You say the publisher persist, what do you mean by that? That the topic is still available, or that you are receiving command values you shouldn’t be getting?

Yes, the rospy.spin() at the end of your script ensure the script does not exit unless you manually stop it. So your script’s node, bb8_move_node, will persist until you force-stop the script.

No. When you call the service for the first time, the node is registered as a publisher to /cmd_vel and it remains registered as long as it lives.

Take care to differentiate between the Publisher object and the publishing node. The publisher object my_pub “gets removed” once the callback function finishes execution, but the publishing node bb8_move_node is still very much alive and kicking :slight_smile:

1 Like

I meant like when I query rostopic info /cmd_vel , the publisher node bb8_move_node is still shown as the publisher for the topic /cmd_vel .

So we should declare the Publisher object inside the service script , in case where I need to modify the publisher object values ? Am I correct?

Thanks alot for your reply and it does clarify my doubts.

So as long as the service is up, the node will persist and I can publish new values to the node from another script or maybe same service python script (with modified code for example using some conditional statements) to modify the values for the topic /cmd_vel ?

The publisher object you defined is only available within the callback (limited scope). If you define it outside the callback (in the same place where you defined the service), you can use it in the callback and anywhere else.

Don’t bother too much about this. It’s a programming concept called “variable scope” and you will understand it more as you program. What you have done here is just fine for the goal of the task.

Not exactly, just take it that what you are seeing is normal. The “node” is the running Python script. This node will live as long as the script is running. You can still do much more with the python script, but don’t bother about it now. You will cross the bridge when you get there.

1 Like