Ask Your Question
0

How to setup a proper node that uses Services form other nodes

asked 2019-04-03 05:57:17 -0600

stefvanlierop gravatar image

updated 2019-04-03 05:57:49 -0600

Hi i have been working on a node that has to use a service form ur_msgs called set_io i understand what parameters have to set in order for it to work because i can do that with the rosservice commands in my linux kernel and that worked. But now i cannot seem to understand what i need in python code to make that exact same call run in a node.

setIO documentation

after a little bit of searching i found out that a service proxy has to be setup. What i have on code at the moment looks like this.

import time, socket, struct, math , sys, os 
import rospy
from ur_msgs.srv import SetIO

rospy.init_node('relay_test')
rospy.wait_for_service('set_io')
rospy.ServiceProxy(set_io,SetIO')

req = set_io()
 req.fun = 4
 req.pin = 0    

try:
#pseudo code because i do not know how to continue, the parameters for set_io are fun, pin and state, the state has to change from 24 to 0 and back every 10 seconds so i can see if the relay works.    
req.state = 24
rospy.sleep(10.)
req.state = 0
rospy.sleep(10.)   

except: rospy.ServiceException as exc:
edit retag flag offensive close merge delete

Comments

Just making sure: have you seen (and completed) the Writing a Simple Service and Client (Python)? The UR driver is the server in your case. You'd write the client.

gvdhoorn gravatar imagegvdhoorn ( 2019-04-03 06:50:05 -0600 )edit

I have seen it, but did not understand it at the time i asked the question. I looked at it again and realized that i was not using the right structure.

stefvanlierop gravatar imagestefvanlierop ( 2019-04-04 02:56:41 -0600 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2019-04-04 03:15:25 -0600

stefvanlierop gravatar image

updated 2019-04-04 05:46:07 -0600

The code that made the functionality work looks like this

edit: code to initialize a simple node has been added and commentary

import sys  
import rospy
from ur_msgs.srv import SetIO

def set_io_client():
     rospy.wait_for_service('ur_driver/set_io') # look if service is online
     rospy.ini_node('relay_test') # initialize the node
     while not rospy.is_shutdown() # making sure it only runs when ros is running
     try:
           set_io = rospy.ServiceProxy('ur_driver/set_io',SetIO) # setup the proxy
           set_io(fun = 4, pin = 0, state = 24) # set the pin to 24v for the relay
           rospy.sleep(10.) # sleep for 10s
           set_io(fun = 4,pin = 0, state = 0) # set the pin to 0v for the relay
           rospy.sleep(10.)
     except: rospy.ServiceException, e: # this will do something when the function is canceld
           print "doesnt work" # debug code, so in this case it will print doesnt work when the function is interrupted or canceled


if __name__ == "__main__"
       set_io_client()
else:
       print"doesnt work right here" # only prints if __main__ condition is not met. 
       sys.exit(1) # exits system with a unnormal termination.
edit flag offensive delete link more

Comments

I would suggest to use Python's named argument support. It will probably make the code much more readable.

Something like this should work:

set_io(fun=SetIORequest.FUN_SET_TOOL_VOLTAGE, state=24)

(note: the constants are defined in the request part of the service definition, so you'll need to add an from ur_msgs.srv import SetIORequest to your script).

gvdhoorn gravatar imagegvdhoorn ( 2019-04-04 03:17:42 -0600 )edit

This does not seem to work, for fun=SetIO.FUN_SET_TOOL_VOLTAGE because i get an attributeError.

AttributeError: type object SetIO has no attribute 'FUN_SET_TOOL_VOLTAGE'

it does seem to work if you simply use set_io(fun = 4,pin = 0,state = 24)

stefvanlierop gravatar imagestefvanlierop ( 2019-04-04 04:42:45 -0600 )edit

I've fixed the code snippet I posted.

Btw: you don't need to provide all arguments. pin is ignored when setting tool voltage.

gvdhoorn gravatar imagegvdhoorn ( 2019-04-04 05:04:48 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-04-03 05:57:17 -0600

Seen: 80 times

Last updated: Apr 04