I have a drone which has certain services, say random.srv. I want to use this service via Python. Now I know that there is a page in ROS wiki that tells you how to create a service client in Python. Unfortunately that is not sufficient for me. Here goes my case:
Once I start the drone node, the service is listed as
/main/random_task
Now an important point to note is that the name of the service is as stated above
random.srv
The function that I am writing is:
from random.srv import *
import rospy
def enableRandom():
rospy.wait_for_service('main/random_task')
try:
droneRandom = rospy.ServiceProxy('main/random_task', random)
doSomething(1) # something seems to be missing
except rospy.ServiceException:
print('Service call failed')
if __name__ == '__main__':
enableRandom()
The thing is it gives an error saying random is undefined. But that is the name of the srv file.
Can someone also explain why it doesn't work the way it should. You can guess by this question that I am new to this.
Edit 1: Now I understand that in the statement
from random.srv import *
the 'random' should be the package name. This is not the case. The package name is, lets call it my-package1. The service random.srv that I want to use is in
src/my-package1/main/srv/
Another thing to clear up is that /main/random_task is the name with which random.srv is shown in rosservice list. The new code now is
from my-package1.srv import random
import rospy
def enableRandom():
rospy.wait_for_service('/main/random_task')
try:
droneRandom = rospy.ServiceProxy('/main/random_task', random)
doSomething()
except rospy.ServiceException:
print('Service call failed')
if __name__ == '__main__':
enableRandom()
Now I get my-project1 as having invalid syntax (I guess due to having '-'symbol). If I use
from main.srv import random
it says it cannot find 'main'.
First if I have to use 'my-project1.srv' how to I get around the syntax issue with '-'.
Secondly, if I have to use the subfolder name i.e. main.srv, why does it not recognize the package if it can recognize the services inside it (when using rosservice list on the terminal).
I think there is an issue with the syntax.