Go to the documentation of this file.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 import argparse
00031 import socket
00032 import sys
00033
00034 import rospy
00035 import rosgraph
00036
00037 import std_srvs.srv
00038
00039 from baxter_core_msgs.srv import (
00040 ListCameras,
00041 )
00042 from baxter_interface.camera import CameraController
00043
00044
00045 def list_cameras(*_args, **_kwds):
00046 ls = rospy.ServiceProxy('cameras/list', ListCameras)
00047 rospy.wait_for_service('cameras/list', timeout=10)
00048 resp = ls()
00049 if len(resp.cameras):
00050
00051 master = rosgraph.Master('/rostopic')
00052 resp.cameras
00053 cam_topics = dict([(cam, "/cameras/%s/image" % cam)
00054 for cam in resp.cameras])
00055 open_cams = dict([(cam, False) for cam in resp.cameras])
00056 try:
00057 topics = master.getPublishedTopics('')
00058 for topic in topics:
00059 for cam in resp.cameras:
00060 if topic[0] == cam_topics[cam]:
00061 open_cams[cam] = True
00062 except socket.error:
00063 raise ROSTopicIOException("Cannot communicate with master.")
00064 for cam in resp.cameras:
00065 print("%s%s" % (cam, (" - (open)" if open_cams[cam] else "")))
00066 else:
00067 print ('No cameras found')
00068
00069
00070 def reset_cameras(*_args, **_kwds):
00071 reset_srv = rospy.ServiceProxy('cameras/reset', std_srvs.srv.Empty)
00072 rospy.wait_for_service('cameras/reset', timeout=10)
00073 reset_srv()
00074
00075
00076 def enum_cameras(*_args, **_kwds):
00077 try:
00078 reset_cameras()
00079 except:
00080 srv_ns = rospy.resolve_name('cameras/reset')
00081 rospy.logerr("Failed to call reset devices service at %s", srv_ns)
00082 raise
00083 else:
00084 list_cameras()
00085
00086
00087 def open_camera(camera, res, *_args, **_kwds):
00088 cam = CameraController(camera)
00089 cam.close()
00090 cam.resolution = res
00091 cam.open()
00092
00093
00094 def close_camera(camera, *_args, **_kwds):
00095 cam = CameraController(camera)
00096 cam.close()
00097
00098
00099 def main():
00100 str_res = ["%rx%r" % (r[0], r[1]) for r in CameraController.MODES]
00101 fmt_res = ("Valid resolutions:\n[" +
00102 ("%s, " * (len(CameraController.MODES) - 1)) + "%s]")
00103 parser = argparse.ArgumentParser(
00104 formatter_class=argparse.RawDescriptionHelpFormatter,
00105 epilog=(fmt_res % tuple(str_res))
00106 )
00107 action_grp = parser.add_mutually_exclusive_group(required=True)
00108 action_grp.add_argument(
00109 '-o', '--open', metavar='CAMERA', help='Open specified camera'
00110 )
00111 parser.add_argument(
00112 '-r', '--resolution', metavar='[X]x[Y]', default='1280x800',
00113 help='Set camera resolution (default: 1280x800)'
00114 )
00115 action_grp.add_argument(
00116 '-c', '--close', metavar='CAMERA', help='Close specified camera'
00117 )
00118 action_grp.add_argument(
00119 '-l', '--list', action='store_true', help='List available cameras'
00120 )
00121 action_grp.add_argument(
00122 '-e', '--enumerate', action='store_true',
00123 help='Clear and re-enumerate connected devices'
00124 )
00125 args = parser.parse_args(rospy.myargv()[1:])
00126
00127 action = None
00128 camera = None
00129 res = (1280, 800)
00130
00131 if args.list:
00132 action = list_cameras
00133 elif args.open:
00134 action = open_camera
00135 camera = args.open
00136 lres = args.resolution.split('x')
00137 if len(lres) != 2:
00138 print fmt_res % tuple(str_res)
00139 parser.error("Invalid resolution format: %s. Use (X)x(Y).")
00140 res = (int(lres[0]), int(lres[1]))
00141 if not any((res[0] == r[0] and res[1] == r[1])
00142 for r in CameraController.MODES):
00143 print fmt_res % tuple(str_res)
00144 parser.error("Invalid resolution provided.")
00145 elif args.close:
00146 action = close_camera
00147 camera = args.close
00148 elif args.enumerate:
00149 action = enum_cameras
00150 else:
00151
00152 parser.print_usage()
00153 parser.error("No action defined.")
00154
00155 rospy.init_node('rsdk_camera_control')
00156 action(camera, res)
00157 return 0
00158
00159 if __name__ == '__main__':
00160 sys.exit(main())