Package rospy :: Module numpy_msg

Source Code for Module rospy.numpy_msg

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2009, Willow Garage, Inc. 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Willow Garage, Inc. nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 32  # 
 33  # Revision $Id$ 
 34   
 35  """ 
 36  Support for using numpy with rospy messages. 
 37   
 38  For tutorials, see U{http://www.ros.org/wiki/rospy_tutorials/Tutorials/numpy} 
 39   
 40  Listener example:: 
 41      from rospy.numpy_msg import numpy_msg 
 42   
 43      rospy.init_node('mynode') 
 44      rospy.Subscriber("mytopic", numpy_msg(TopicType) 
 45   
 46  Publisher example:: 
 47   
 48      from rospy.numpy_msg import numpy_msg 
 49      import numpy 
 50       
 51      pub = rospy.Publisher('mytopic', numpy_msg(TopicType), queue_size=10) 
 52      rospy.init_node('mynode') 
 53      a = numpy.array([1.0, 2.1, 3.2, 4.3, 5.4, 6.5], dtype=numpy.float32) 
 54      pub.publish(a) 
 55  """ 
 56   
 57  import numpy 
 58   
 59  # TODO: we will need to generate a new type structure with 
 60  # little-endian specified and then pass that type structure into the 
 61  # *_numpy calls. 
 62   
63 -def _serialize_numpy(self, buff):
64 """ 65 wrapper for factory-generated class that passes numpy module into serialize 66 """ 67 # pass in numpy module reference to prevent import in auto-generated code 68 return self.serialize_numpy(buff, numpy)
69
70 -def _deserialize_numpy(self, str):
71 """ 72 wrapper for factory-generated class that passes numpy module into deserialize 73 """ 74 # pass in numpy module reference to prevent import in auto-generated code 75 return self.deserialize_numpy(str, numpy)
76 77 _numpy_msg_types = {} 78 ## Use this function to generate message instances using numpy array 79 ## types for numerical arrays. 80 ## @msg_type Message class: call this functioning on the message type that you pass 81 ## into a Publisher or Subscriber call. 82 ## @returns Message class
83 -def numpy_msg(msg_type):
84 if msg_type in _numpy_msg_types: 85 return _numpy_msg_types[msg_type] 86 87 classdict = { '__slots__': msg_type.__slots__, '_slot_types': msg_type._slot_types, 88 '_md5sum': msg_type._md5sum, '_type': msg_type._type, 89 '_has_header': msg_type._has_header, '_full_text': msg_type._full_text, 90 'serialize': _serialize_numpy, 'deserialize': _deserialize_numpy, 91 'serialize_numpy': msg_type.serialize_numpy, 92 'deserialize_numpy': msg_type.deserialize_numpy 93 } 94 95 # create the numpy message type 96 msg_type_name = "Numpy_%s"%msg_type._type.replace('/', '__') 97 numpy_type = type(msg_type_name,(msg_type,),classdict) 98 _numpy_msg_types[msg_type] = numpy_type 99 return numpy_type
100