def publish(self, *args, **kwds): """ Publish message data object to this topic. Publish can either be called with the message instance to publish or with the constructor args for a new Message instance, i.e.:: pub.publish(message_instance) pub.publish(message_field_1, message_field_2...) pub.publish(message_field_1='foo', message_field_2='bar') @param args : L{Message} instance, message arguments, or no args if keyword arguments are used @param kwds : Message keyword arguments. If kwds are used, args must be unset @raise ROSException: If rospy node has not been initialized @raise ROSSerializationException: If unable to serialize message. This is usually a type error with one of the fields. """ if self.impl is None: raise ROSException("publish() to an unregistered() handle") if not is_initialized(): raise ROSException("ROS node has not been initialized yet. Please call init_node() first") data = args_kwds_to_message(self.data_class, args, kwds) try: self.impl.acquire() self.impl.publish(data) except genpy.SerializationError as e: # can't go to rospy.logerr(), b/c this could potentially recurse _logger.error(traceback.format_exc(e)) raise ROSSerializationException(str(e)) finally: self.impl.release()
def publish(self, *args, **kwds): """ Publish message data object to this topic. Publish can either be called with the message instance to publish or with the constructor args for a new Message instance, i.e.:: pub.publish(message_instance) pub.publish(message_field_1, message_field_2...) pub.publish(message_field_1='foo', message_field_2='bar') @param args : L{Message} instance, message arguments, or no args if keyword arguments are used @param kwds : Message keyword arguments. If kwds are used, args must be unset @raise ROSException: If rospy node has not been initialized @raise ROSSerializationException: If unable to serialize message. This is usually a type error with one of the fields. """ if self.impl is None: raise ROSException("publish() to an unregistered() handle") if not is_initialized(): raise ROSException( "ROS node has not been initialized yet. Please call init_node() first" ) data = args_kwds_to_message(self.data_class, args, kwds) try: self.impl.acquire() self.impl.publish(data) except genpy.SerializationError as e: # can't go to rospy.logerr(), b/c this could potentially recurse _logger.error(traceback.format_exc(e)) raise ROSSerializationException(str(e)) finally: self.impl.release()
def callService(self, service_uri, service, type, *args, **kwds): ''' Calls the service and return the response. To call the service the ServiceProxy can't be used, because it uses environment variables to determine the URI of the running service. In our case this service can be running using another ROS master. The changes on the environment variables is not thread safe. So the source code of the rospy.SerivceProxy (tcpros_service.py) was modified. @param service_uri: the URI of the service @type service_uri: C{str} @param service: full service name (with name space) @type service: C{str} @param type: service class @type type: ServiceDefinition: service class @param args: arguments to remote service @param kwds: message keyword arguments @return: the tuple of request and response. @rtype: C{(request object, response object)} @raise StartException: on error @see: L{rospy.SerivceProxy} ''' from rospy.core import parse_rosrpc_uri, is_shutdown from rospy.msg import args_kwds_to_message from rospy.exceptions import TransportInitError, TransportException from rospy.impl.tcpros_base import TCPROSTransport, TCPROSTransportProtocol, DEFAULT_BUFF_SIZE from rospy.impl.tcpros_service import TCPROSServiceClient from rospy.service import ServiceException request = args_kwds_to_message(type._request_class, args, kwds) transport = None protocol = TCPROSServiceClient(service, type, headers={}) transport = TCPROSTransport(protocol, service) # initialize transport dest_addr, dest_port = parse_rosrpc_uri(service_uri) # connect to service transport.buff_size = DEFAULT_BUFF_SIZE try: transport.connect(dest_addr, dest_port, service_uri) except TransportInitError as e: # can be a connection or md5sum mismatch raise StartException("unable to connect to service: %s"%e) transport.send_message(request, 0) try: responses = transport.receive_once() if len(responses) == 0: raise StartException("service [%s] returned no response"%service) elif len(responses) > 1: raise StartException("service [%s] returned multiple responses: %s"%(service, len(responses))) except TransportException as e: # convert lower-level exception to exposed type if is_shutdown(): raise StartException("node shutdown interrupted service call") else: raise StartException("transport error completing service call: %s"%(str(e))) except ServiceException, e: raise StartException("Service error: %s"%(str(e)))
def test_args_kwds_to_message(self): import rospy from rospy.msg import args_kwds_to_message from test_rospy.msg import Val, Empty v = Val('hello world-1') d = args_kwds_to_message(Val, (v, ), None) self.assert_(d == v) d = args_kwds_to_message(Val, ('hello world-2', ), None) self.assertEquals(d.val, 'hello world-2') d = args_kwds_to_message(Val, (), {'val': 'hello world-3'}) self.assertEquals(d.val, 'hello world-3') # error cases try: args_kwds_to_message(Val, 'hi', val='hello world-3') self.fail("should not allow args and kwds") except TypeError: pass try: args_kwds_to_message(Empty, (Val('hola'), ), None) self.fail( "should raise TypeError when publishing Val msg to Empty topic" ) except TypeError: pass
def test_args_kwds_to_message(self): import rospy from rospy.msg import args_kwds_to_message from test_rospy.msg import Val v = Val('hello world-1') d = args_kwds_to_message(Val, (v,), None) self.assert_(d == v) d = args_kwds_to_message(Val, ('hello world-2',), None) self.assertEquals(d.val, 'hello world-2') d = args_kwds_to_message(Val, (), {'val':'hello world-3'}) self.assertEquals(d.val, 'hello world-3') # error cases try: args_kwds_to_message(Val, 'hi', val='hello world-3') self.fail("should not allow args and kwds") except TypeError: pass
def test_args_kwds_to_message(self): import rospy from rospy.msg import args_kwds_to_message from test_rospy.msg import Val v = Val('hello world-1') d = args_kwds_to_message(Val, (v, ), None) self.assert_(d == v) d = args_kwds_to_message(Val, ('hello world-2', ), None) self.assertEquals(d.val, 'hello world-2') d = args_kwds_to_message(Val, (), {'val': 'hello world-3'}) self.assertEquals(d.val, 'hello world-3') # error cases try: args_kwds_to_message(Val, 'hi', val='hello world-3') self.fail("should not allow args and kwds") except TypeError: pass
def test_args_kwds_to_message(self): import rospy from rospy.msg import args_kwds_to_message from test_rospy.msg import Val v = Val("hello world-1") d = args_kwds_to_message(Val, (v,), None) self.assert_(d == v) d = args_kwds_to_message(Val, ("hello world-2",), None) self.assertEquals(d.val, "hello world-2") d = args_kwds_to_message(Val, (), {"val": "hello world-3"}) self.assertEquals(d.val, "hello world-3") # error cases try: args_kwds_to_message(Val, "hi", val="hello world-3") self.fail("should not allow args and kwds") except TypeError: pass
def publish(self, *args, **kwds): """! Publish a message on the topic associated with this `Pubscriber`. Can be called either with a message or constructor arguments for a new message. @param args: L{Message} instance, message arguments, or no args if keyword arguments are used @param kwds: Message keyword arguments. If kwds are used, args must be unset """ data = args_kwds_to_message(self._publisher.data_class, args, kwds) self._sent_message_timestamps = self._sent_message_timestamps[ 1 if len(self._sent_message_timestamps) > self. _maximum_messages_in_flight else 0:] + [data.header.stamp] self._publisher.publish(data)
def call(self, *args, **kwds): """ Call the service. This accepts either a request message instance, or you can call directly with arguments to create a new request instance. e.g.:: add_two_ints(AddTwoIntsRequest(1, 2)) add_two_ints(1, 2) add_two_ints(a=1, b=2) The first service invokation is executed in a blocking manner until the transport connection is established. If the connection is set to be persistent (default) subsequent service calls are executed asynchronously. In either case the result is passed to the specified callback method. @raise TypeError: if request is not of the valid type (Message) @raise ServiceException: if communication with remote service fails @raise ROSInterruptException: if node shutdown (e.g. ctrl-C) interrupts service call @raise ROSSerializationException: If unable to serialize message. This is usually a type error with one of the fields. """ if self.__retrieve_result.is_set(): raise ServiceException("Previous call did not return yet") self.__call_future = Future() self.__call_future.set_running_or_notify_cancel() self.__call_future.start = time.time() # initialize transport if self.__proxy.transport is None: result = self.__proxy.call(*args, **kwds) self.__call_future.end = time.time() self.__call_future.set_result(result) else: if not self.__await_finish_thread.is_alive(): self.__await_finish_thread.start() # convert args/kwds to request message class request = args_kwds_to_message(self.__proxy.request_class, args, kwds) # send the actual request message self.__proxy.seq += 1 self.__proxy.transport.send_message(request, self.__proxy.seq) self.__retrieve_result.set() return self.__call_future
def publish(self, *args, **kwargs): # Convert arguments into an instance of provided data_class orig = args_kwds_to_message(self.data_class, args, kwargs) # Get the attribute information buff = StringIO() orig.serialize(buff) contents = buff.getvalue() buff.close() # Create the new message to send msg = SecuredMessage() msg.MessageType = str(self.data_class) msg.MessageContent = self.encode(contents) self.pub.publish(msg)
def test_args_kwds_to_message(self): import rospy from rospy.msg import args_kwds_to_message from test_rospy.msg import Val, Empty v = Val('hello world-1') d = args_kwds_to_message(Val, (v,), None) self.assert_(d == v) d = args_kwds_to_message(Val, ('hello world-2',), None) self.assertEquals(d.val, 'hello world-2') d = args_kwds_to_message(Val, (), {'val':'hello world-3'}) self.assertEquals(d.val, 'hello world-3') # error cases try: args_kwds_to_message(Val, 'hi', val='hello world-3') self.fail("should not allow args and kwds") except TypeError: pass try: args_kwds_to_message(Empty, (Val('hola'),), None) self.fail("should raise TypeError when publishing Val msg to Empty topic") except TypeError: pass
def publish(self, *args, **kwargs): self.payload = args_kwds_to_message(self.data_class, args, kwargs) super(CachingPublisher, self).publish(*args, **kwargs)
def callService(self, service_uri, service, type, *args, **kwds): ''' Calls the service and return the response. To call the service the ServiceProxy can't be used, because it uses environment variables to determine the URI of the running service. In our case this service can be running using another ROS master. The changes on the environment variables is not thread safe. So the source code of the rospy.SerivceProxy (tcpros_service.py) was modified. @param service_uri: the URI of the service @type service_uri: C{str} @param service: full service name (with name space) @type service: C{str} @param type: service class @type type: ServiceDefinition: service class @param args: arguments to remote service @param kwds: message keyword arguments @return: the tuple of request and response. @rtype: C{(request object, response object)} @raise StartException: on error @see: L{rospy.SerivceProxy} ''' from rospy.core import parse_rosrpc_uri, is_shutdown from rospy.msg import args_kwds_to_message from rospy.exceptions import TransportInitError, TransportException from rospy.impl.tcpros_base import TCPROSTransport, TCPROSTransportProtocol, DEFAULT_BUFF_SIZE from rospy.impl.tcpros_service import TCPROSServiceClient from rospy.service import ServiceException request = args_kwds_to_message(type._request_class, args, kwds) transport = None protocol = TCPROSServiceClient(service, type, headers={}) transport = TCPROSTransport(protocol, service) # initialize transport dest_addr, dest_port = parse_rosrpc_uri(service_uri) # connect to service transport.buff_size = DEFAULT_BUFF_SIZE try: transport.connect(dest_addr, dest_port, service_uri) except TransportInitError as e: # can be a connection or md5sum mismatch raise StartException("unable to connect to service: %s" % e) transport.send_message(request, 0) try: responses = transport.receive_once() if len(responses) == 0: raise StartException("service [%s] returned no response" % service) elif len(responses) > 1: raise StartException( "service [%s] returned multiple responses: %s" % (service, len(responses))) except TransportException as e: # convert lower-level exception to exposed type if is_shutdown(): raise StartException("node shutdown interrupted service call") else: raise StartException( "transport error completing service call: %s" % (str(e))) except ServiceException, e: raise StartException("Service error: %s" % (str(e)))