Hi all,
I am wondering how to pass an arguments when trying to call rosservice from python script.
I have this server called bcap_service which takes 2 arguments. (This is bcap.srv)
int32 func_id
variant[] vntArgs
---
int32 HRESULT
variant vntRet
(This is variant.msg)
int16 vt
string value
and I am trying to pass these arguments.
rosservice call /bcap_service '{func_id: 3, vntArgs: [{vt: 8, value: "b-CAP"}, {vt: 8, value: "CaoProv.DENSO.VRC"}, {vt: 8, value: "localhost"}, {vt: 8, value: ""}] }'
I have tried assigning them to variables. Tried writing like a YAML and had no results. Here is my code:
def gripper_control():
rospy.wait_for_service('bcap_service')
func_id = 3
vt = 8
value_1 = 'value: b-CAP'
value_2 = 'CaoProv.DENSO.VRC'
value_3 = 'localhost'
value_4 = ''
vntArgs = [vt, value_1, vt, value_2, vt, value_3, vt, value_4]
rospy.ServiceProxy('bcap_service', func_id, vntArgs)
After running the code, I get error like this:
Traceback (most recent call last):
File "/opt/ros/kinetic/lib/denso_robot_bringup/move_group_python_interface_cobotta.py", line 224, in <module>
gripper_control()
File "/opt/ros/kinetic/lib/denso_robot_bringup/move_group_python_interface_cobotta.py", line 197, in gripper_control
rospy.ServiceProxy('bcap_service', func_id, vntArgs)
File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/impl/tcpros_service.py", line 404, in __init__
super(ServiceProxy, self).__init__(name, service_class)
File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/service.py", line 59, in __init__
self.request_class = service_class._request_class
AttributeError: 'int' object has no attribute '_request_class'
EDIT:
Full code below:
import sys
import copy
import rospy
import math
import moveit_commander
import moveit_msgs.msg
import geometry_msgs.msg
from geometry_msgs.msg import Pose, Point
from math import pi
from std_msgs.msg import String
from bcap_service.srv import *
def gripper_control():
rospy.wait_for_service('bcap_service')
try:
#create handler to call the service
bcap_service = rospy.ServiceProxy('bcap_service', bcap)
print "ServiceProxy success ..."
#define the request
func_id = 3
vt = 8
value_1 = 'value: b-CAP'
value_2 = 'CaoProv.DENSO.VRC'
value_3 = 'localhost'
value_4 = ''
vntArgs = [vt, value_1, vt, value_2, vt, value_3, vt, value_4]
print "Assignment success ..."
print vntArgs
#call the service by passing the request to the handler
#it doesn't require to have the same names as in the .srv
#But I did it for clarity
response = bcap_service(func_id, vntArgs)
print "rosservice call success"
#store the response in some variables
result1 = response.HRESULT
result2 = response.vntRet
#return the whole response
return response
except rospy.ServiceException, e:
print "Service call failed: %s"%e
if __name__ == '__main__':
rospy.init_node('cobotta_node')
robot = moveit_commander.RobotCommander()
scene = moveit_commander.PlanningSceneInterface()
group = moveit_commander.MoveGroupCommander("arm")
gripper_control()
Originally posted by Tulga on ROS Answers with karma: 16 on 2019-04-24
Post score: 0