43 from genmsg
import SrvSpec, MsgSpec, MsgContext
44 from genmsg.msg_loader
import load_srv_from_file, load_msg_by_type
45 import genmsg.gentools
48 from cStringIO
import StringIO
50 from io
import StringIO
57 return t
in [
'byte',
'char',
'int8',
'uint8',
'int16',
'uint16',
'int32',
'uint32',
'int64',
'uint64']
60 return t
in [
'byte',
'int8',
'int16',
'int32',
'int64']
63 return t
in [
'char',
'uint8',
'uint16',
'uint32',
'uint64']
72 return t
in [
'float16',
'float32',
'float64']
75 return t
in [
'time',
'duration']
79 elt_type =
lisp_type(f.base_type, f.is_array)
85 "returns (package, msg_or_srv)" 86 if f.base_type ==
'Header':
87 return (
'std_msgs',
'Header')
89 return f.base_type.split(
'/')
92 "returns roseus namespace package::msg_or_srv" 94 return '%s::%s'%(pkg, msg)
97 if t ==
'uint8' and array:
110 raise ValueError(
'%s is not a recognized primitive type'%t)
114 if f.is_builtin
and not f.is_array:
116 return '(round %s)'%var
118 return '(float %s)'%var
120 return '(string %s)'%var
125 elt_type =
lisp_type(f.base_type, f.is_array)
127 elt_type =
':'+elt_type
129 len = f.array_len
or 0
131 return '(make-array %s :initial-element %s :element-type %s)'%(len, initvalue, elt_type)
133 return '(let (r) (dotimes (i %s) (push %s r)) r)'%(len, initvalue)
145 return '(instance ros::time :init)' 149 raise ValueError(
'%s is not a recognized primitive type'%t)
163 raise ValueError(
'%s is not a recognized primitive type'%t)
165 NUM_BYTES = {
'byte': 1,
'int8': 1,
'int16': 2,
'int32': 4,
'int64': 8,
166 'char': 1,
'uint8': 1,
'uint16': 2,
'uint32': 4,
'uint64': 8}
181 def write(self, s, indent=True, newline=True):
217 self.writer.inc_indent(self.
inc)
219 self.writer.block_next_indent()
222 self.writer.dec_indent(self.
inc)
225 "Writes the beginning of the file: a comment saying it's auto-generated and the in-package form" 227 s.write(
';; Auto-generated. Do not edit!\n\n', newline=
False)
228 suffix =
'srv' if is_service
else 'msg' 230 spec.actual_name=spec.short_name
231 s.write(
'(when (boundp \'%s::%s)'%(spec.package, spec.actual_name))
232 s.write(
' (if (not (find-package "%s"))'%(spec.package.upper()))
233 s.write(
' (make-package "%s"))'%(spec.package.upper()))
234 s.write(
' (shadow \'%s (find-package "%s")))'%(spec.actual_name, spec.package.upper()))
235 s.write(
'(unless (find-package "%s::%s")'%(spec.package.upper(), spec.actual_name.upper()))
236 s.write(
' (make-package "%s::%s"))'%(spec.package.upper(), spec.actual_name.upper()))
238 s.write(
'(unless (find-package "%s::%sREQUEST")'%(spec.package.upper(), spec.actual_name.upper()))
239 s.write(
' (make-package "%s::%sREQUEST"))'%(spec.package.upper(), spec.actual_name.upper()))
240 s.write(
'(unless (find-package "%s::%sRESPONSE")'%(spec.package.upper(), spec.actual_name.upper()))
241 s.write(
' (make-package "%s::%sRESPONSE"))'%(spec.package.upper(), spec.actual_name.upper()))
243 s.write(
'(in-package "ROS")')
248 s.write(
';;//! \\htmlinclude %s.msg.html'%spec.actual_name, newline=
False)
249 for msg_type
in sorted(set([
parse_msg_type(field)[0]
for field
in spec.parsed_fields()
if not field.is_builtin
and parse_msg_type(field)[0] != spec.package])):
250 s.write(
'(if (not (find-package "%s"))'%msg_type.upper())
251 s.write(
' (ros::roseus-add-msgs "%s"))'%msg_type)
256 "Write the definition of a slot corresponding to a single message field" 257 s.write(
'_%s '%field.name, indent=
False, newline=
False)
261 "Write the key arguments of a slot corresponding to a single message field" 267 len = field.array_len
or 0
268 s.write(
'((:%s __%s) (let (r) (dotimes (i %s) (push (instance %s :init) r)) r))'%(var, var, len,
field_type(field)))
270 s.write(
'((:%s __%s) (instance %s :init))'%(var, var,
field_type(field)))
273 "Write the initialization of a slot corresponding to a single message field" 277 "Writes the defclass that defines the message type" 278 s.write(
'(defclass %s::%s'%(spec.package, spec.actual_name))
280 s.write(
':super ros::object')
282 with
Indent(s, inc=1, indent_first=
False):
283 for field
in spec.parsed_fields():
285 s.write(
'))', indent=
False)
289 s.write(
'(defmethod %s::%s'%(spec.package, spec.actual_name))
295 for field
in spec.parsed_fields():
298 s.write(
'(send-super :init)')
299 for field
in spec.parsed_fields():
306 for field
in spec.parsed_fields():
307 s.write(
'(:%s'%field.name)
308 var =
'_%s'%field.name
311 if field.type ==
"bool":
312 s.write(
'(&optional (_%s :null))'%var)
313 s.write(
'(if (not (eq _%s :null)) (setq %s _%s)) %s)'%(var,var,var,var))
315 s.write(
'(&optional _%s)'%var)
316 s.write(
'(if _%s (setq %s _%s)) %s)'%(var,var,var,var))
318 s.write(
'(&rest _%s)'%var)
319 s.write(
'(if (keywordp (car _%s))'%var)
320 s.write(
' (send* %s _%s)'%(var,var))
323 s.write(
' (if _%s (setq %s (car _%s)))'%(var,var,var))
324 s.write(
' %s)))'%var)
328 s.write(
'(write-long (length %s) s)'%(v))
330 s.write(
'(write-long (length %s) s) (princ %s s)'%(v,v))
335 s.write(
'(write-byte %s s)'%v)
337 s.write(
'(write-word %s s)'%v)
339 s.write(
'(write-long %s s)'%v)
341 s.write(
'\n', indent=
False)
342 s.write(
'#+(or :alpha :irix6 :x86_64)', indent=
False, newline=
False)
343 s.write(
'(progn (sys::poke %s (send s :buffer) (send s :count) :long) (incf (stream-count s) 8))'%v)
344 s.write(
'\n', indent=
False)
345 s.write(
'#-(or :alpha :irix6 :x86_64)', indent=
False)
346 s.write(
'(cond ((and (class %s) (= (length (%s . bv)) 2)) ;; bignum'%(v,v))
347 s.write(
' (write-long (ash (elt (%s . bv) 0) 0) s)'%v)
348 s.write(
' (write-long (ash (elt (%s . bv) 1) -1) s))'%v)
349 s.write(
' ((and (class %s) (= (length (%s . bv)) 1)) ;; big1'%(v,v))
350 s.write(
' (write-long (elt (%s . bv) 0) s)'%v)
351 s.write(
' (write-long (if (>= %s 0) 0 #xffffffff) s))'%v)
352 s.write(
' (t ;; integer')
353 s.write(
' (write-long %s s)(write-long (if (>= %s 0) 0 #xffffffff) s)))'%(v,v))
360 if f.base_type ==
'string':
362 elif f.base_type ==
'float32':
363 s.write(
'(sys::poke %s (send s :buffer) (send s :count) :float) (incf (stream-count s) 4)'%v)
364 elif f.base_type ==
'float64':
365 s.write(
'(sys::poke %s (send s :buffer) (send s :count) :double) (incf (stream-count s) 8)'%v)
366 elif f.base_type ==
'bool':
367 s.write(
'(if %s (write-byte -1 s) (write-byte 0 s))'%v)
368 elif f.base_type
in [
'byte',
'char']:
369 s.write(
'(write-byte %s s)'%v)
370 elif f.base_type
in [
'duration',
'time']:
371 s.write(
'(write-long (send %s :sec) s) (write-long (send %s :nsec) s)'%(v,v))
377 raise ValueError(
'Unknown type: %s', f.base_type)
380 s.write(
';; %s _%s'%(f.type, f.name))
383 if f.is_array
and f.base_type ==
'uint8':
385 s.write(
'(write-long (length %s) s)'%slot)
386 s.write(
'(princ %s s)'%slot)
387 elif f.is_array
and is_string(f.base_type):
388 s.write(
'(write-long (length %s) s)'%slot)
389 s.write(
'(dolist (elem %s)'%slot)
394 if f.is_builtin
and f.array_len:
395 s.write(
'(dotimes (i %s)'%f.array_len)
396 elif f.is_builtin
and not f.array_len:
397 s.write(
'(dotimes (i (length %s))'%var)
399 s.write(
'(dolist (elem %s)'%slot)
401 var =
'(elt %s i)'%var
402 s.block_next_indent()
405 if f.is_array
and f.base_type ==
'uint8':
412 s.write(
'(send %s :serialize s)'%slot)
414 if f.is_array
and f.base_type !=
'uint8':
419 Write the serialize method 422 s.write(
'(:serialize')
424 s.write(
'(&optional strm)')
425 s.write(
'(let ((s (if strm strm')
426 s.write(
' (make-string-output-stream (send self :serialization-length)))))')
428 for f
in spec.parsed_fields():
431 s.write(
'(if (null strm) (get-output-stream-string s))))')
437 s.write(
'(setq n (sys::peek buf ptr- :integer)) (incf ptr- 4)')
438 s.write(
'(setq %s (let (r) (dotimes (i n) (push (instance %s :init) r)) r))'%(v,
field_type(f)))
440 set =
'setf' if v[0] ==
'(' else 'setq' 441 s.write(
'(let (n) (setq n (sys::peek buf ptr- :integer)) (incf ptr- 4) (%s %s (subseq buf ptr- (+ ptr- n))) (incf ptr- n))'%(set, v))
455 raise ValueError(
'Unknown size: %s', num_bytes)
457 set =
'setf' if v[0] ==
'(' else 'setq' 458 s.write(
'(%s %s (sys::peek buf ptr- %s)) (incf ptr- %s)'%(set,v,type,num_bytes))
461 if num_bytes
in [1,2,4]:
464 s.write(
'\n', indent=
False)
465 s.write(
'#+(or :alpha :irix6 :x86_64)', indent=
False)
466 s.write(
' (setf %s (prog1 (sys::peek buf ptr- :long) (incf ptr- 8)))\n'%v)
467 s.write(
'#-(or :alpha :irix6 :x86_64)', indent=
False)
468 s.write(
' (setf %s (let ((b0 (prog1 (sys::peek buf ptr- :integer) (incf ptr- 4)))'%v)
469 s.write(
' (b1 (prog1 (sys::peek buf ptr- :integer) (incf ptr- 4))))')
470 s.write(
' (cond ((= b1 -1) b0)')
471 s.write(
' ((and (= b1 0)')
472 s.write(
' (<= lisp::most-negative-fixnum b0 lisp::most-positive-fixnum))')
474 s.write(
' ((= b1 0) (make-instance bignum :size 1 :bv (integer-vector b0)))')
475 s.write(
' (t (make-instance bignum :size 2 :bv (integer-vector b0 (ash b1 1)))))))')
478 set =
'setf' if v[0] ==
'(' else 'setq' 479 if f.base_type ==
'string':
481 elif f.base_type ==
'float32':
482 s.write(
'(%s %s (sys::peek buf ptr- :float)) (incf ptr- 4)'%(set, v))
483 elif f.base_type ==
'float64':
484 s.write(
'(%s %s (sys::peek buf ptr- :double)) (incf ptr- 8)'%(set, v))
485 elif f.base_type ==
'bool':
486 s.write(
'(%s %s (not (= 0 (sys::peek buf ptr- :char)))) (incf ptr- 1)'%(set, v))
487 elif f.base_type
in [
'duration',
'time']:
488 s.write(
'(send %s :sec (sys::peek buf ptr- :integer)) (incf ptr- 4) (send %s :nsec (sys::peek buf ptr- :integer)) (incf ptr- 4)'%(v,v))
491 if NUM_BYTES[f.base_type] == 1:
492 s.write(
'(if (> %s 127) (%s %s (- %s 256)))'%(v,set,v,v))
496 raise ValueError(
'%s unknown'%f.base_type)
500 s.write(
';; %s %s'%(f.type, var))
503 if f.base_type ==
'uint8':
505 s.write(
'(setq %s (make-array %d :element-type :char))'%(var,f.array_len))
506 s.write(
'(replace %s buf :start2 ptr-) (incf ptr- %d)'%(var,f.array_len))
508 s.write(
'(let ((n (sys::peek buf ptr- :integer))) (incf ptr- 4)')
509 s.write(
' (setq %s (make-array n :element-type :char))'%var)
510 s.write(
' (replace %s buf :start2 ptr-) (incf ptr- n))'%(var))
512 s.write(
'(dotimes (i (length %s))'%var)
513 var =
'(elt %s i)'%var
518 s.write(
'(setq n (sys::peek buf ptr- :integer)) (incf ptr- 4)')
520 s.write(
'(setq %s (make-list n))'%var)
522 s.write(
'(setq %s (instantiate %s-vector n))'%(var,
lisp_type(f.base_type, f.is_array)))
523 s.write(
'(dotimes (i n)')
524 var =
'(elt %s i)'%var
529 s.write(
'(dolist (%s _%s)'%(var, f.name))
532 s.write(
'(dotimes (i %s)'%f.array_len)
533 var =
'(elt _%s i)'%f.name
538 s.write(
'(dolist (%s _%s)'%(var, f.name))
539 if f.is_array
and f.base_type ==
'uint8':
546 s.write(
'(send %s :deserialize buf ptr-) (incf ptr- (send %s :serialization-length))'%(var, var))
548 if f.is_array
and not f.base_type ==
'uint8':
558 Write the deserialize method 561 s.write(
'(:deserialize')
563 s.write(
'(buf &optional (ptr- 0))')
564 for f
in spec.parsed_fields():
572 md5sum = genmsg.compute_md5(msg_context, parent
or spec)
573 s.write(
'(setf (get %s::%s :md5sum-) "%s")'%(spec.package, spec.actual_name, md5sum))
576 s.write(
'(setf (get %s::%s :datatype-) "%s/%s")'%(spec.package, spec.actual_name, spec.package, spec.actual_name))
579 s.write(
'(setf (get %s::%s :definition-)'%(spec.package, spec.actual_name))
582 definition = genmsg.compute_full_text(msg_context, spec)
583 lines = definition.split(
'\n')
585 l = line.replace(
'\\',
'\\\\')
586 l = l.replace(
'"',
'\\"')
587 s.write(
'%s\n'%l, indent=
False, newline=
False)
588 s.write(
'")', newline=
False)
592 s.write(
'(setf (get %s::%s :definition-)'%(parent.package, parent.actual_name))
595 for spec_service
in [spec.request, spec.response]:
596 definition = genmsg.compute_full_text(msg_context, spec_service)
597 lines = definition.split(
'\n')
598 for line
in lines[:-1]:
599 l = line.replace(
'\\',
'\\\\')
600 l = l.replace(
'"',
'\\"')
601 s.write(
'%s\n'%l, indent=
False, newline=
False)
602 if spec_service == spec.request:
603 s.write(
'---\n', indent=
False, newline=
False)
604 s.write(
'")', newline=
False)
607 if f.base_type
in [
'int8',
'uint8']:
609 elif f.base_type
in [
'int16',
'uint16']:
611 elif f.base_type
in [
'int32',
'uint32',
'float32']:
613 elif f.base_type
in [
'int64',
'uint64',
'float64',
'duration',
'time']:
615 elif f.base_type ==
'string':
616 s.write(
'4 (length _%s)'%f.name)
617 elif f.base_type
in [
'bool',
'byte',
'char']:
620 raise ValueError(
'Unknown: %s', f.base_type)
624 s.write(
'(:serialization-length')
629 if not spec.parsed_fields():
631 for field
in spec.parsed_fields():
632 s.write(
';; %s _%s'%(field.type, field.name))
634 if field.is_builtin
and not is_string(field.base_type):
637 s.write(
'(apply #\'+ ')
638 s.block_next_indent()
641 if not field.array_len:
643 s.write(
'(mapcar #\'(lambda (x) (+ 4 (length x))) _%s)) 4'%(field.name))
646 s.write(
'(length _%s)) 4'%field.name, newline=
False)
649 s.write(
'%s)'%field.array_len, newline=
False)
652 s.write(
'(send-all _%s :serialization-length))'%field.name)
654 s.write(
'(send-all _%s :serialization-length)) 4'%field.name)
659 s.write(
'(send _%s :serialization-length)'%field.name)
665 md5sum = genmsg.compute_md5(msg_context, spec)
666 s.write(
'(provide :%s/%s "%s")'%(spec.package, spec.actual_name,md5sum))
671 for c
in spec.constants:
672 s.write(
'(intern "*%s*" (find-package "%s::%s"))'%(c.name.upper(), spec.package.upper(), spec.actual_name.upper()))
673 s.write(
'(shadow \'*%s* (find-package "%s::%s"))'%(c.name.upper(), spec.package.upper(), spec.actual_name.upper()))
674 if c.type ==
'string':
675 s.write(
'(defconstant %s::%s::*%s* "%s")'%(spec.package, spec.actual_name, c.name.upper(), c.val.replace(
'"',
'\\"')))
676 elif c.type ==
'bool':
677 s.write(
'(defconstant %s::%s::*%s* %s)'%(spec.package, spec.actual_name, c.name.upper(),
"t" if c.val ==
True else "nil"))
679 s.write(
'(defconstant %s::%s::*%s* %s)'%(spec.package, spec.actual_name, c.name.upper(), c.val))
682 spec.component_type=
'service' 693 s.write(
'(defclass %s::%s'%(spec.package, spec.actual_name))
695 s.write(
':super ros::object')
696 s.write(
':slots ())')
700 s.write(
'(setf (get %s::%s :request) %s::%s)'%(spec.package, spec.actual_name, spec.request.package, spec.request.actual_name))
701 s.write(
'(setf (get %s::%s :response) %s::%s)'%(spec.package, spec.actual_name, spec.response.package, spec.response.actual_name))
703 s.write(
'(defmethod %s::%s'%(spec.request.package, spec.request.actual_name))
704 s.write(
' (:response () (instance %s::%s :init)))'%(spec.response.package, spec.response.actual_name))
706 for spec_service
in [spec.request, spec.response]:
713 s.write(
'\n', newline=
False)
717 Generate euslisp code for all messages in a package 719 msg_context = MsgContext.create_default()
721 f = os.path.abspath(f)
722 infile = os.path.basename(f)
723 full_type = genmsg.gentools.compute_full_type_name(pkg, infile)
724 spec = genmsg.msg_loader.load_msg_from_file(msg_context, f, full_type)
729 Generate euslisp code for all services in a package 731 msg_context = MsgContext.create_default()
733 f = os.path.abspath(f)
734 infile = os.path.basename(f)
735 full_type = genmsg.gentools.compute_full_type_name(pkg, infile)
736 spec = genmsg.msg_loader.load_srv_from_file(msg_context, f, full_type)
740 dir_list = search_path[pkg]
743 files.extend([f
for f
in os.listdir(d)
if f.endswith(ext)])
744 return [f[:-len(ext)]
for f
in files]
750 @param msg_path: The path to the .msg file 753 genmsg.msg_loader.load_depends(msg_context, spec, search_path)
754 spec.actual_name=spec.short_name
755 spec.component_type=
'message' 756 msgs =
msg_list(package, search_path,
'.msg')
758 genmsg.load_msg_by_type(msg_context,
'%s/%s'%(package, m), search_path)
781 if (
not os.path.exists(output_dir)):
785 os.makedirs(output_dir)
789 with open(
'%s/%s.l'%(output_dir, spec.short_name),
'w')
as f:
790 f.write(io.getvalue() +
"\n")
796 "Generate code from .srv file" 797 genmsg.msg_loader.load_depends(msg_context, spec, search_path)
799 srvs = [f[:-len(ext)]
for f
in os.listdir(os.path.dirname(path))
if f.endswith(ext)]
801 load_srv_from_file(msg_context, path,
'%s/%s'%(package, s))
812 spec.request.actual_name=
'%sRequest'%spec.short_name
813 spec.response.actual_name=
'%sResponse'%spec.short_name
818 with open(
'%s/%s.l'%(output_dir, spec.short_name),
'w')
as f:
819 f.write(io.getvalue())
def write_serialize_bits_signed(s, v, num_bytes)
def write_service_specific_methods(s, context, spec)
def __init__(self, w, inc=2, indent_first=True)
def inc_indent(self, inc=2)
def generate_msg(pkg, files, out_dir, search_path)
def write_deserialize_bits(s, v, num_bytes)
def write(self, s, indent=True, newline=True)
def write_md5sum(s, msg_context, spec, parent=None)
def write_serialize_bits(s, v, num_bytes)
def write_ros_datatype(s, spec)
def generate_msg_from_spec(msg_context, spec, search_path, output_dir, package)
def write_begin(s, spec, is_service=False)
def write_slot_definition(s, field)
def write_message_definition(s, msg_context, spec)
def write_deserialize_bits_signed(s, v, num_bytes)
def write_serialize_field(s, f)
def write_service_definition(s, msg_context, spec, parent)
def msg_list(pkg, search_path, ext)
def write_deserialize_length(s, f, v, is_array=False)
def write_deserialize_field(s, f, pkg)
def write_builtin_length(s, f, var='msg')
def write_defmethod(s, spec)
def dec_indent(self, dec=2)
def generate_srv(pkg, files, out_dir, search_path)
def write_slot_initialize(s, field)
def block_next_indent(self)
def is_integer(t)
Built in types.
def __exit__(self, type, val, traceback)
def write_serialize_length(s, v, is_array=False)
def write_serialization_length(s, spec)
def generate_srv_from_spec(msg_context, spec, search_path, output_dir, package, path)
def write_slot_argument(s, field)
def write_srv_component(s, spec, context, parent)
def write_constants(s, spec)
def write_deserialize_builtin(s, f, v)
def write_serialize_builtin(s, f, v)
def write_include(s, spec, is_srv=False)
def write_accessors(s, spec)
def write_defclass(s, spec)
def write_deserialize(s, spec)
def write_provide(s, msg_context, spec)
def write_serialize(s, spec)