39 python3 =
True if sys.hexversion > 0x03000000
else False 40 python_to_ros_type_map = {
42 'int' : [
'int8',
'byte',
'uint8',
'char',
43 'int16',
'uint16',
'int32',
'uint32',
44 'int64',
'uint64',
'float32',
'float64'],
45 'float' : [
'float32',
'float64'],
47 'unicode' : [
'string'],
51 python_string_types = [str]
53 python_string_types = [str, unicode]
54 python_list_types = [list, tuple]
56 ros_time_types = [
'time',
'duration']
57 ros_primitive_types = [
'bool',
'byte',
'char',
'int8',
'uint8',
'int16',
58 'uint16',
'int32',
'uint32',
'int64',
'uint64',
59 'float32',
'float64',
'string']
60 ros_header_types = [
'Header',
'std_msgs/Header',
'roslib/Header']
61 ros_binary_types_regexp = re.compile(
r'(uint8|char)\[[^\]]*\]')
63 list_brackets = re.compile(
r'\[[^\]]*\]')
67 Takes in the message type and a Python dictionary and returns a ROS message. 70 message_type = "std_msgs/String" 71 dict_message = { "data": "Hello, Robot" } 72 ros_message = convert_dictionary_to_ros_message(message_type, dict_message) 74 message_type = "std_srvs/SetBool" 75 dict_message = { "data": True } 77 ros_message = convert_dictionary_to_ros_message(message_type, dict_message, kind) 80 message_class = roslib.message.get_message_class(message_type)
81 message = message_class()
82 elif kind ==
'request':
83 service_class = roslib.message.get_service_class(message_type)
84 message = service_class._request_class()
85 elif kind ==
'response':
86 service_class = roslib.message.get_service_class(message_type)
87 message = service_class._response_class()
89 raise ValueError(
'Unknown kind "%s".' % kind)
92 for field_name, field_value
in dictionary.items():
93 if field_name
in message_fields:
94 field_type = message_fields[field_name]
96 setattr(message, field_name, field_value)
98 error_message =
'ROS message type "{0}" has no field named "{1}"'\
99 .format(message_type, field_name)
100 raise ValueError(error_message)
107 elif field_type
in ros_time_types:
109 elif field_type
in ros_primitive_types:
119 if type(field_value)
in python_string_types:
120 binary_value_as_string = base64.standard_b64decode(field_value)
122 binary_value_as_string = str(bytearray(field_value))
124 return binary_value_as_string
129 if field_type ==
'time' and field_value ==
'now':
130 time = rospy.get_rostime()
132 if field_type ==
'time':
133 time = rospy.rostime.Time()
134 elif field_type ==
'duration':
135 time = rospy.rostime.Duration()
136 if 'secs' in field_value:
137 setattr(time,
'secs', field_value[
'secs'])
138 if 'nsecs' in field_value:
139 setattr(time,
'nsecs', field_value[
'nsecs'])
144 if field_type ==
"string":
145 field_value = field_value.encode(
'utf-8')
149 list_type = list_brackets.sub(
'', field_type)
154 Takes in a ROS message and returns a Python dictionary. 157 ros_message = std_msgs.msg.String(data="Hello, Robot") 158 dict_message = convert_ros_message_to_dictionary(ros_message) 162 for field_name, field_type
in message_fields:
163 field_value = getattr(message, field_name)
171 elif field_type
in ros_time_types:
173 elif field_type
in ros_primitive_types:
174 field_value = field_value
184 """ Checks if the field is a binary array one, fixed size or not 186 is_ros_binary_type("uint8", 42) 188 is_ros_binary_type("uint8[]", [42, 18]) 190 is_ros_binary_type("uint8[3]", [42, 18, 21] 192 is_ros_binary_type("char", 42) 194 is_ros_binary_type("char[]", [42, 18]) 196 is_ros_binary_type("char[3]", [42, 18, 21] 199 return re.search(ros_binary_types_regexp, field_type)
is not None 202 field_value = base64.standard_b64encode(field_value)
207 'secs' : field_value.secs,
208 'nsecs' : field_value.nsecs
216 list_type = list_brackets.sub(
'', field_type)
220 return zip(message.__slots__, message._slot_types)
223 return list_brackets.search(field_type)
is not None def _convert_to_ros_array(field_type, list_value)
def _convert_from_ros_array(field_type, field_value)
def _convert_to_ros_type(field_type, field_value)
def _convert_from_ros_type(field_type, field_value)
def _convert_from_ros_time(field_type, field_value)
def _convert_to_ros_time(field_type, field_value)
def convert_dictionary_to_ros_message(message_type, dictionary, kind='message')
def _is_field_type_an_array(field_type)
def convert_ros_message_to_dictionary(message)
def _convert_from_ros_binary(field_type, field_value)
def _convert_to_ros_binary(field_type, field_value)
def _convert_to_ros_primitive(field_type, field_value)
def is_ros_binary_type(field_type, field_value)
def _get_message_fields(message)
def _convert_from_ros_primitive(field_type, field_value)