Robotics StackExchange | Archived questions

Rosservice only works on shell and not on rospy

Hi, It's my first post here so i hope everything's ok.

I have my robot "snake" in gazebo simulation already spawned and if i type in the shell

rosservice call /gazebo/delete_model "model_name: 'snake'"

it actually delete the snake. But if i run a python script with the following code:

delsnake = DeleteModel()
delsnake.model_name = 'snake'
delete = rospy.ServiceProxy('delete_model', DeleteModel)
delete(delsnake)

or even

delete = rospy.ServiceProxy('delete_model', DeleteModel)
delete(model_name = 'snake')

which should be equivalent the terminal tells me:

raise ServiceException("service [%s] unavailable"%self.resolved_name)
rospy.service.ServiceException: service [/delete_model] unavailable

But the service is avaible! I cannot find a solution, the only thing i could do is actually run the command line from the python script but i don't think that's the way thing should go.

Can you help me please?

Thanks in advace!

Asked by abcamiletto on 2019-11-22 01:42:09 UTC

Comments

Answers

Your service name is missing the gazebo/ prefix and the first code isn't equivalent to the second one because you need to specify the argument as a string. Here a working code to call the service :

# you can also do this before :
# rospy.wait_for_service("/gazebo/delete_model") to ensure the service is available
delete = rospy.ServiceProxy("/gazebo/delete_model", DeleteModel)
try:
    delete(model_name = "snake")
    # or simply delete("snake")
except rospy.ServiceException as exc:
    print("Service did not process request: " + str(exc))

Asked by Delb on 2019-11-22 05:37:31 UTC

Comments

Mhhh with your code now it tells me:

File "/home/andrea/Desktop/simsnake/src/killnspawn/launch/kns.py", line 12, in <module>
delete = rospy.ServiceProxy("/gazebo/delete_model", DeleteModel)
NameError: name 'DeleteModel' is not defined

Asked by abcamiletto on 2019-11-22 11:54:01 UTC

Have you still the import at the beginning ?

from gazebo_msgs import DeleteModel

Asked by Delb on 2019-11-22 19:24:57 UTC