teleop_pimp.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # License: BSD
00004 #   https://raw.github.com/robotics-in-concert/concert_services/license/LICENSE
00005 #
00006 ##############################################################################
00007 # About
00008 ##############################################################################
00009 
00010 # Simple script to pimp out teleop operations for rocon interactions.
00011 #
00012 # - watch the app manager status and when it has a remote controller,
00013 # - flip a spawn/kill pair across
00014 # - call the spawn api
00015 #  - the turtle herder will flip back some handles then.
00016 
00017 ##############################################################################
00018 # Imports
00019 ##############################################################################
00020 
00021 import rospy
00022 import rocon_uri
00023 import concert_service_utilities
00024 import unique_id
00025 import rocon_std_msgs.msg as rocon_std_msgs
00026 import scheduler_msgs.msg as scheduler_msgs
00027 import concert_service_msgs.msg as concert_service_msgs
00028 
00029 from concert_software_farmer import SoftwareFarmClient, FailedToStartSoftwareException
00030 
00031 class TeleopPimp(concert_service_utilities.ResourcePimp):
00032 
00033     _default_cmd_vel_topic = '/teleop/cmd_vel'
00034     _default_compressed_image_topic = '/teleop/compressed_image'
00035 
00036     def setup_variables(self): 
00037         '''
00038             Need to setup the following variables
00039             service_priority, service_id, resource_type, available_resource_publisher_name, capture_topic_name
00040         '''
00041         (service_name, service_description, service_priority, service_id) = concert_service_utilities.get_service_info()
00042         self.service_priority = service_priority
00043         self.service_id = service_id
00044         self.resource_type = 'rocon_apps/video_teleop'
00045         self.available_resource_publisher_name = 'available_teleops'
00046         self.capture_topic_name = 'capture_teleop'
00047 
00048     def ros_capture_callback(self, request_id, msg):
00049         '''
00050          Processes the service pair server 'capture_teleop'. This will run
00051          in a thread of its own for each request. It has a significantly long lock
00052          though - this needs to get fixed.
00053         '''
00054         # Todo : request the scheduler for this resource,
00055         # use self.allocation_timeout to fail gracefully
00056 
00057         response = concert_service_msgs.CaptureResourceResponse()
00058         response.result = False
00059         if not msg.release:  # i.e. we're capturing:
00060             if msg.rocon_uri not in [r.uri for r in self.available_resources]:
00061                 self.logwarn("couldn't capture teleopable robot [not available][%s]" % msg.rocon_uri)
00062                 response.result = False
00063             else:
00064                 resource = self._create_resource(msg.rocon_uri)
00065                 request_result, resource_request_id = self.send_allocation_request(resource)
00066                 response.result = request_result
00067                 if request_result == False:
00068                     self.logwarn("couldn't capture teleopable robot [timed out][%s]" % msg.rocon_uri)
00069                 else:
00070                     self.loginfo("captured teleopable robot [%s][%s]" % (msg.rocon_uri, resource_request_id))
00071                     response.remappings = resource.remappings
00072         else:  # we're releasing
00073             self.send_releasing_request(msg.rocon_uri)
00074             response.result = True
00075         return response
00076 
00077     def _create_resource(self, uri):
00078         # Create a resource to request
00079         resource = scheduler_msgs.Resource()
00080         resource.id = unique_id.toMsg(unique_id.fromRandom())
00081         resource.rapp = self.resource_type
00082         resource.uri = uri
00083         cmd_vel_remapped, compressed_image_topic_remapped = self._get_remapped_topic(rocon_uri.parse(resource.uri).name.string)
00084         resource.remappings = [rocon_std_msgs.Remapping(self._default_cmd_vel_topic, cmd_vel_remapped), rocon_std_msgs.Remapping(self._default_compressed_image_topic, compressed_image_topic_remapped)]
00085         return resource
00086 
00087     def _get_remapped_topic(self, name):
00088         '''
00089           Sets up remapping rules for Rapp configuration
00090         '''
00091         cmd_vel_remapped = '/' + name + self._default_cmd_vel_topic
00092         compressed_image_topic_remapped = '/' + name + self._default_compressed_image_topic
00093 
00094         return cmd_vel_remapped, compressed_image_topic_remapped 
00095 
00096     def loginfo(self, msg):
00097         rospy.loginfo("TeleopPimp : %s"%str(msg))
00098 
00099     def logwarn(self, msg):
00100         rospy.logwarn("TeleopPimp : %s"%str(msg))
00101 
00102     def logerr(self, msg):
00103         rospy.logerr("TeleopPimp : %s"%str(msg))
00104 
00105 
00106 ##############################################################################
00107 # Launch point
00108 ##############################################################################
00109 
00110 WEB_VIDEO_SOFTWARE = 'concert_software_common/web_video_server'
00111 
00112 def create_web_video_parameters(address, port):
00113     params = []
00114     params.append(rocon_std_msgs.KeyValue('address', str(address)))
00115     params.append(rocon_std_msgs.KeyValue('port', str(port)))
00116     return params
00117 
00118 if __name__ == '__main__':
00119     rospy.init_node('teleop_pimp')
00120     pimp = TeleopPimp()
00121 
00122     try:
00123         enable_web_video = rospy.get_param('enable_web_video', False)
00124         if enable_web_video:
00125             address = rospy.get_param('web_video_server_address', 'localhost')
00126             port = rospy.get_param('web_video_server_port', 8080)
00127             sfc = SoftwareFarmClient()
00128             parameters = create_web_video_parameters(address, port)
00129             success, namespace, parameters = sfc.allocate(WEB_VIDEO_SOFTWARE, parameters)
00130 
00131             if not success:
00132                 raise FailedToStartSoftwareException("Failed to allocate software")
00133             rospy.loginfo("Teleop pimp : Web video server : %s"%parameters)
00134             rospy.loginfo("Done")
00135             rospy.spin()
00136             sfc.deallocate(WEB_VIDEO_SOFTWARE)
00137         else:
00138             rospy.loginfo("Done")
00139             rospy.spin()
00140     except FailedToStartSoftwareException as e:
00141         rospy.logerr("Teleop Pimp : %s"%str(e))
00142 
00143     if not rospy.is_shutdown():
00144         pimp.cancel_all_requests()


concert_service_teleop
Author(s): Daniel Stonier
autogenerated on Thu Jun 6 2019 21:35:20