36 from __future__
import print_function
38 __author__ =
"mferguson@willowgarage.com (Michael Ferguson)" 63 """ For data values. """ 71 f.write(
' enum { %s = %s };\n' % (self.
name, self.
value))
74 """ Our datatype is a C/C++ primitive. """ 82 f.write(
' %s(0)%s\n' % (self.
name, trailer))
85 f.write(
' typedef %s _%s_type;\n _%s_type %s;\n' % (self.
type, self.
name, self.
name, self.
name) )
88 cn = self.name.replace(
"[",
"").replace(
"]",
"").split(
".")[-1]
91 f.write(
' %s real;\n' % self.
type)
93 f.write(
' } u_%s;\n' % cn)
94 f.write(
' u_%s.real = this->%s;\n' % (cn,self.
name))
95 for i
in range(self.
bytes):
96 f.write(
' *(outbuffer + offset + %d) = (u_%s.base >> (8 * %d)) & 0xFF;\n' % (i, cn, i) )
98 for i
in range(self.
bytes):
99 f.write(
' *(outbuffer + offset + %d) = (this->%s >> (8 * %d)) & 0xFF;\n' % (i, self.
name, i) )
100 f.write(
' offset += sizeof(this->%s);\n' % self.
name)
103 cn = self.name.replace(
"[",
"").replace(
"]",
"").split(
".")[-1]
105 f.write(
' union {\n')
106 f.write(
' %s real;\n' % self.
type)
108 f.write(
' } u_%s;\n' % cn)
109 f.write(
' u_%s.base = 0;\n' % cn)
110 for i
in range(self.
bytes):
111 f.write(
' u_%s.base |= ((%s) (*(inbuffer + offset + %d))) << (8 * %d);\n' % (cn,
type_to_var(self.
bytes),i,i) )
112 f.write(
' this->%s = u_%s.real;\n' % (self.
name, cn) )
114 f.write(
' this->%s = ((%s) (*(inbuffer + offset)));\n' % (self.
name,self.
type) )
115 for i
in range(self.
bytes-1):
116 f.write(
' this->%s |= ((%s) (*(inbuffer + offset + %d))) << (8 * %d);\n' % (self.
name,self.
type,i+1,i+1) )
117 f.write(
' offset += sizeof(this->%s);\n' % self.
name)
121 """ For when our data type is another message. """ 124 f.write(
' %s()%s\n' % (self.
name, trailer))
127 f.write(
' offset += this->%s.serialize(outbuffer + offset);\n' % self.
name)
130 f.write(
' offset += this->%s.deserialize(inbuffer + offset);\n' % self.
name)
134 """ AVR C/C++ has no native 64-bit support, we automatically convert to 32-bit float. """ 137 f.write(
' %s(0)%s\n' % (self.
name, trailer))
140 f.write(
' typedef float _%s_type;\n _%s_type %s;\n' % (self.
name, self.
name, self.
name) )
143 f.write(
' offset += serializeAvrFloat64(outbuffer + offset, this->%s);\n' % self.
name)
146 f.write(
' offset += deserializeAvrFloat64(inbuffer + offset, &(this->%s));\n' % self.
name)
150 """ Need to convert to signed char *. """ 153 f.write(
' %s("")%s\n' % (self.
name, trailer))
156 f.write(
' typedef const char* _%s_type;\n _%s_type %s;\n' % (self.
name, self.
name, self.
name) )
159 cn = self.name.replace(
"[",
"").replace(
"]",
"")
160 f.write(
' uint32_t length_%s = strlen(this->%s);\n' % (cn,self.
name))
161 f.write(
' varToArr(outbuffer + offset, length_%s);\n' % cn)
162 f.write(
' offset += 4;\n')
163 f.write(
' memcpy(outbuffer + offset, this->%s, length_%s);\n' % (self.
name,cn))
164 f.write(
' offset += length_%s;\n' % cn)
167 cn = self.name.replace(
"[",
"").replace(
"]",
"")
168 f.write(
' uint32_t length_%s;\n' % cn)
169 f.write(
' arrToVar(length_%s, (inbuffer + offset));\n' % cn)
170 f.write(
' offset += 4;\n')
171 f.write(
' for(unsigned int k= offset; k< offset+length_%s; ++k){\n'%cn)
172 f.write(
' inbuffer[k-1]=inbuffer[k];\n')
174 f.write(
' inbuffer[offset+length_%s-1]=0;\n'%cn)
175 f.write(
' this->%s = (char *)(inbuffer + offset-1);\n' % self.
name)
176 f.write(
' offset += length_%s;\n' % cn)
188 f.write(
' %s()%s\n' % (self.
name, trailer))
191 f.write(
' typedef %s _%s_type;\n _%s_type %s;\n' % (self.
type, self.
name, self.
name, self.
name) )
194 self.sec.serialize(f)
195 self.nsec.serialize(f)
198 self.sec.deserialize(f)
199 self.nsec.deserialize(f)
204 def __init__(self, name, ty, bytes, cls, array_size=None):
212 if self.
size ==
None:
213 f.write(
' %s_length(0), %s(NULL)%s\n' % (self.
name, self.
name, trailer))
215 f.write(
' %s()%s\n' % (self.
name, trailer))
218 if self.
size ==
None:
219 f.write(
' uint32_t %s_length;\n' % self.
name)
220 f.write(
' typedef %s _%s_type;\n' % (self.
type, self.
name))
221 f.write(
' _%s_type st_%s;\n' % (self.
name, self.
name))
222 f.write(
' _%s_type * %s;\n' % (self.
name, self.
name))
224 f.write(
' %s %s[%d];\n' % (self.
type, self.
name, self.
size))
228 if self.
size ==
None:
230 f.write(
' *(outbuffer + offset + 0) = (this->%s_length >> (8 * 0)) & 0xFF;\n' % self.
name)
231 f.write(
' *(outbuffer + offset + 1) = (this->%s_length >> (8 * 1)) & 0xFF;\n' % self.
name)
232 f.write(
' *(outbuffer + offset + 2) = (this->%s_length >> (8 * 2)) & 0xFF;\n' % self.
name)
233 f.write(
' *(outbuffer + offset + 3) = (this->%s_length >> (8 * 3)) & 0xFF;\n' % self.
name)
234 f.write(
' offset += sizeof(this->%s_length);\n' % self.
name)
235 f.write(
' for( uint32_t i = 0; i < %s_length; i++){\n' % self.
name)
239 f.write(
' for( uint32_t i = 0; i < %d; i++){\n' % (self.
size) )
244 if self.
size ==
None:
247 f.write(
' uint32_t %s_lengthT = ((uint32_t) (*(inbuffer + offset))); \n' % self.
name)
248 f.write(
' %s_lengthT |= ((uint32_t) (*(inbuffer + offset + 1))) << (8 * 1); \n' % self.
name)
249 f.write(
' %s_lengthT |= ((uint32_t) (*(inbuffer + offset + 2))) << (8 * 2); \n' % self.
name)
250 f.write(
' %s_lengthT |= ((uint32_t) (*(inbuffer + offset + 3))) << (8 * 3); \n' % self.
name)
251 f.write(
' offset += sizeof(this->%s_length);\n' % self.
name)
252 f.write(
' if(%s_lengthT > %s_length)\n' % (self.
name, self.
name))
253 f.write(
' this->%s = (%s*)realloc(this->%s, %s_lengthT * sizeof(%s));\n' % (self.
name, self.
type, self.
name, self.
name, self.
type))
254 f.write(
' %s_length = %s_lengthT;\n' % (self.
name, self.
name))
256 f.write(
' for( uint32_t i = 0; i < %s_length; i++){\n' % (self.
name) )
258 f.write(
' memcpy( &(this->%s[i]), &(this->st_%s), sizeof(%s));\n' % (self.
name, self.
name, self.
type))
262 f.write(
' for( uint32_t i = 0; i < %d; i++){\n' % (self.
size) )
270 """ Parses message definitions into something we can export. """ 271 global ROS_TO_EMBEDDED_TYPES
273 def __init__(self, name, package, definition, md5):
284 for line
in definition:
286 line = line.strip().rstrip()
288 if line.find(
"#") > -1:
289 line = line[0:line.find(
"#")]
290 if line.find(
"=") > -1:
292 value = line[line.find(
"=")+1:]
294 value =
'"' + line[line.find(
"=")+1:] +
'"';
295 line = line[0:line.find(
"=")]
298 line = line.replace(
"\t",
" ")
310 type_package, type_name = ty.split(
"/")
315 if type_name.find(
'[') > 0:
318 type_array_size = int(type_name[type_name.find(
'[')+1:type_name.find(
']')])
320 type_array_size =
None 321 type_name = type_name[0:type_name.find(
'[')]
325 code_type = ROS_TO_EMBEDDED_TYPES[type_name][0]
326 size = ROS_TO_EMBEDDED_TYPES[type_name][1]
327 cls = ROS_TO_EMBEDDED_TYPES[type_name][2]
328 for include
in ROS_TO_EMBEDDED_TYPES[type_name][3]:
330 self.includes.append(include)
332 if type_package ==
None:
334 if type_package+
"/"+type_name
not in self.
includes:
335 self.includes.append(type_package+
"/"+type_name)
336 cls = MessageDataType
337 code_type = type_package +
"::" + type_name
340 self.data.append(
ArrayDataType(name, code_type, size, cls, type_array_size ) )
342 self.data.append( cls(name, code_type, size) )
346 f.write(
' virtual int serialize(unsigned char *outbuffer) const\n')
348 f.write(
' int offset = 0;\n')
351 f.write(
' return offset;\n');
357 f.write(
' virtual int deserialize(unsigned char *inbuffer)\n')
359 f.write(
' int offset = 0;\n')
362 f.write(
' return offset;\n');
367 f.write(
'#include <stdint.h>\n')
368 f.write(
'#include <string.h>\n')
369 f.write(
'#include <stdlib.h>\n')
370 f.write(
'#include "ros/msg.h"\n')
374 f.write(
'#include "%s.h"\n' % i)
377 f.write(
' %s()%s\n' % (self.
name,
':' if self.
data else ''))
379 for d
in self.
data[:-1]:
380 d.make_initializer(f,
',')
381 self.
data[-1].make_initializer(f,
'')
382 f.write(
' {\n }\n\n')
386 d.make_declaration(f)
388 e.make_declaration(f)
392 f.write(
' const char * getType(){ return "%s/%s"; };\n'%(self.
package, self.
name))
395 f.write(
' const char * getMD5(){ return "%s"; };\n'%self.
md5)
398 f.write(
' class %s : public ros::Msg\n' % self.
name)
400 f.write(
' public:\n')
411 f.write(
'#ifndef _ROS_%s_%s_h\n'%(self.
package, self.
name))
412 f.write(
'#define _ROS_%s_%s_h\n'%(self.
package, self.
name))
418 f.write(
'namespace %s\n' % self.
package)
428 def __init__(self, name, package, definition, md5req, md5res):
430 @param name - name of service 431 @param package - name of service package 432 @param definition - list of lines of definition 438 sep_line = len(definition)
439 sep = re.compile(
'---*')
440 for i
in range(0, len(definition)):
441 if (
None!= re.match(sep, definition[i]) ):
451 f.write(
'#ifndef _ROS_SERVICE_%s_h\n' % self.
name)
452 f.write(
'#define _ROS_SERVICE_%s_h\n' % self.
name)
454 self.req._write_std_includes(f)
455 includes = self.req.includes
456 includes.extend(self.resp.includes)
457 includes = list(set(includes))
459 f.write(
'#include "%s.h"\n' % inc)
462 f.write(
'namespace %s\n' % self.
package)
465 f.write(
'static const char %s[] = "%s/%s";\n'%(self.name.upper(), self.
package, self.
name))
467 def write_type(out, name):
468 out.write(
' const char * getType(){ return %s; };\n'%(name))
469 _write_getType =
lambda out: write_type(out, self.name.upper())
470 self.req._write_getType = _write_getType
471 self.resp._write_getType = _write_getType
474 self.req._write_impl(f)
476 self.resp._write_impl(f)
478 f.write(
' class %s {\n' % self.
name )
479 f.write(
' public:\n')
480 f.write(
' typedef %s Request;\n' % self.req.name )
481 f.write(
' typedef %s Response;\n' % self.resp.name )
494 pkg_dir = rospack.get_path(package)
498 if os.path.exists(pkg_dir+
"/msg"):
499 print(
'Exporting %s\n'%package)
500 sys.stdout.write(
' Messages:')
501 sys.stdout.write(
'\n ')
502 for f
in os.listdir(pkg_dir+
"/msg"):
503 if f.endswith(
".msg"):
504 msg_file = pkg_dir +
"/msg/" + f
506 print(
'%s,'%f[0:-4], end=
'')
507 definition = open(msg_file).readlines()
508 msg_class = roslib.message.get_message_class(package+
'/'+f[0:-4])
510 md5sum = msg_class._md5sum
511 messages.append(
Message(f[0:-4], package, definition, md5sum) )
513 err_msg =
"Unable to build message: %s/%s\n" % (package, f[0:-4])
514 sys.stderr.write(err_msg)
517 if (os.path.exists(pkg_dir+
"/srv/")):
518 if messages == list():
519 print(
'Exporting %s\n'%package)
522 sys.stdout.write(
' Services:')
523 sys.stdout.write(
'\n ')
524 for f
in os.listdir(pkg_dir+
"/srv"):
525 if f.endswith(
".srv"):
526 srv_file = pkg_dir +
"/srv/" + f
528 print(
'%s,'%f[0:-4], end=
'')
529 definition, service = roslib.srvs.load_from_file(srv_file)
530 definition = open(srv_file).readlines()
531 srv_class = roslib.message.get_service_class(package+
'/'+f[0:-4])
533 md5req = srv_class._request_class._md5sum
534 md5res = srv_class._response_class._md5sum
535 messages.append(
Service(f[0:-4], package, definition, md5req, md5res ) )
537 err_msg =
"Unable to build service: %s/%s\n" % (package, f[0:-4])
538 sys.stderr.write(err_msg)
540 elif messages != list():
544 output_path = output_path +
"/" + package
546 if not os.path.exists(output_path):
547 os.makedirs(output_path)
548 header = open(output_path +
"/" + msg.name +
".h",
"w")
549 msg.make_header(header)
554 global ROS_TO_EMBEDDED_TYPES
555 ROS_TO_EMBEDDED_TYPES = mapping
559 for p
in sorted(rospack.list()):
562 except Exception
as e:
563 failed.append(p +
" ("+str(e)+
")")
564 print(
'[%s]: Unable to build messages: %s\n' % (p, str(e)))
565 print(traceback.format_exc())
568 print(
'*** Warning, failed to generate libraries for the following packages: ***')
571 raise Exception(
"Failed to generate libraries for: " + str(failed))
575 os.makedirs(path+
"/ros")
576 os.makedirs(path+
"/tf")
577 files = [
'duration.cpp',
583 'ros/service_client.h',
584 'ros/service_server.h',
588 'tf/transform_broadcaster.h']
589 mydir = rospack.get_path(
"rosserial_client")
591 shutil.copy(mydir+
"/src/ros_lib/"+f, path+f)
def _write_deserializer(self, f)
def _write_std_includes(self, f)
def __init__(self, name, ty, bytes)
def _write_msg_includes(self, f)
def MakeLibrary(package, output_path, rospack)
def make_initializer(self, f, trailer)
def rosserial_generate(rospack, path, mapping)
def __init__(self, name, ty, bytes, cls, array_size=None)
def make_initializer(self, f, trailer)
def make_declaration(self, f)
def make_declaration(self, f)
def __init__(self, name, ty, value)
def make_declaration(self, f)
def __init__(self, name, ty, bytes)
def _write_getType(self, f)
def __init__(self, name, package, definition, md5)
def make_initializer(self, f, trailer)
def __init__(self, name, package, definition, md5req, md5res)
def make_initializer(self, f, trailer)
def make_initializer(self, f, trailer)
def make_declaration(self, f)
def _write_getMD5(self, f)
def rosserial_client_copy_files(rospack, path)
def _write_serializer(self, f)
def make_declaration(self, f)
def make_declaration(self, f)
def make_initializer(self, f, trailer)
def _write_constructor(self, f)