00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 from __future__ import with_statement
00036
00037 PKG = 'rosh_common'
00038
00039
00040
00041 import os
00042 import sys
00043 import time
00044
00045 import roslib.names
00046 import roslib.packages
00047
00048 import rosh
00049 import rosh.impl.proc
00050 import rosh.impl.topic
00051
00052 from rosh.impl.namespace import Namespace, Concept
00053
00054 _TIMEOUT_CAM_INFO = 3
00055
00056 class Camera(Namespace):
00057
00058 def __init__(self, name, config):
00059 """
00060 ctor.
00061 @param config: Namespace configuration instance with additional 'listener' attribute.
00062 @type config: L{NamespaceConfig}
00063 """
00064 super(Camera, self).__init__(name, config)
00065 self._cam_info = None
00066
00067 def _list(self):
00068 """
00069 Override Namespace._list()
00070 """
00071 try:
00072 pubs = self._config.master.getPublishedTopics(self._ns)
00073 candidates = [p[0] for p in pubs if p[0].endswith('/image_raw')]
00074 pub_names = [p[0] for p in pubs]
00075 matches = []
00076 for c in candidates:
00077 stem = c[:-len('image_raw')]
00078 if stem and stem+'camera_info' in pub_names:
00079 matches.append(stem)
00080 return matches
00081 except:
00082 return []
00083
00084 def __repr__(self):
00085 return self.__str__()
00086
00087 def __str__(self):
00088
00089 if self._cam_info is None:
00090 self._init_cam_info()
00091 if self._cam_info is None:
00092 return self._ns
00093 else:
00094 return str(self._cam_info)
00095
00096 def _init_cam_info(self):
00097 if self._ns == '/':
00098 return
00099
00100 if self._cam_info is None:
00101
00102 try:
00103 ns_obj = rosh.get_topics()[roslib.names.ns_join(self._ns, 'camera_info')]
00104 self._cam_info = rosh.impl.topic.next_msg(ns_obj, _TIMEOUT_CAM_INFO)
00105 except:
00106 pass
00107
00108 def __call__(self, *args, **kwds):
00109 """
00110 Poll image from camera.
00111
00112 @return: current transform
00113 @rtype: L{Transform}
00114 """
00115 ns_obj = rosh.get_topics()[self._ns]
00116 return rosh.impl.topic.next_msg(ns_obj)
00117
00118 def _show(self):
00119 return self._config.ctx.rosh_common__show_handler(self)
00120
00121 class Cameras(Concept):
00122
00123 def __init__(self, ctx, lock):
00124 ctx.rosh_common__show_handler = show_camera_py_image_view
00125 super(Cameras, self).__init__(ctx, lock, Camera)
00126
00127 def _register_show_handler(self, handler):
00128 self._config.ctx.rosh_common__show_handler = handler
00129
00130 def show_camera_py_image_view(ns_obj):
00131 """
00132 Default show camera handler that uses
00133 py_image_view. rosh_visualization provides an optimized rviz-based
00134 viewer.
00135
00136 TODO:
00137 @return: image viewer node (if single camera), or tuple of image viewer nodes (if stereo)
00138 @rtype: Node or (Node, Node)
00139 """
00140
00141 if ns_obj._cam_info is None:
00142
00143 l = ns_obj._getAttributeNames()
00144 if 'left' in l and 'right' in l:
00145 n1 = show_camera_py_image_view(ns_obj.left)
00146 n2 = show_camera_py_image_view(ns_obj.right)
00147 return n1, n2
00148 else:
00149
00150 try:
00151 ns_obj._init_cam_info()
00152 except:
00153 print >> sys.stderr, "%s does not appear to be a camera topic"%ns_obj._name
00154 return None
00155
00156
00157
00158
00159
00160
00161 d = roslib.packages.get_pkg_dir(PKG)
00162 mod = os.path.join(d, 'src', PKG, 'py_image_view.py')
00163 cmd = ['python', mod, '-t', roslib.names.ns_join(ns_obj._name, 'image_raw')]
00164 rosh.impl.proc.run(ns_obj._config, cmd)
00165 print "running py_image_viewer, this may be slow over a wireless network"
00166 return True