ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The code that made the functionality work looks like this

import sys  
import rospy
from ur_msgs.srv import SetIO

def set_io_client():
     rospy.wait_for_service('ur_driver/set_io')
     try:
          set_io = rospy.ServiceProxy('ur_driver/set_io',SetIO)
          set_io(4,0,24)
          rospy.sleep(10.)
          set_io(4,0,0)
          rospy.sleep(10.)
     except: rospy.ServiceException, e:
          print "doesnt work"

if __name__ == "__main__"
       set_io_client()
else:
       print"doesnt work right here"
       sys.exit(1)

however, this is not a node yet. i will edit the code once i have the node working. This code runs only once and turns my relay on with 24 volts and then shuts it off after 10 seconds.

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')
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)
          set_io(4,0,24)
          rospy.sleep(10.)
          set_io(4,0,0)
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:
e: # this will do something when the function is canceld
           print "doesnt work"
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"
       sys.exit(1)
here" # only prints if __main__ condition is not met. 
       sys.exit(1) # exits system with a unnormal termination.

however, this is not a node yet. i will edit the code once i have the node working. This code runs only once and turns my relay on with 24 volts and then shuts it off after 10 seconds.