autosequences_viz_module.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 #  Copyright (c) 2011, UC Regents
00004 #  All rights reserved.
00005 #
00006 #  Redistribution and use in source and binary forms, with or without
00007 #  modification, are permitted provided that the following conditions
00008 #  are met:
00009 #
00010 #   * Redistributions of source code must retain the above copyright
00011 #     notice, this list of conditions and the following disclaimer.
00012 #   * Redistributions in binary form must reproduce the above
00013 #     copyright notice, this list of conditions and the following
00014 #     disclaimer in the documentation and/or other materials provided
00015 #     with the distribution.
00016 #   * Neither the name of the University of California nor the names of its
00017 #     contributors may be used to endorse or promote products derived
00018 #     from this software without specific prior written permission.
00019 #
00020 #  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 #  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 #  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 #  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 #  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 #  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 #  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 #  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 #  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 #  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 #  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 #  POSSIBILITY OF SUCH DAMAGE.
00032 from starmac_viz.viz_module_base import VizModuleBase
00033 import rospy
00034 
00035 import random
00036 import numpy as np
00037 from math import degrees, radians
00038 
00039 import tf.transformations as tft
00040 from tf import TransformBroadcaster
00041 
00042 from interactive_markers.interactive_marker_server import InteractiveMarkerServer, \
00043     InteractiveMarker, InteractiveMarkerControl
00044 from visualization_msgs.msg import Marker
00045 
00046 from starmac_roslib.viz.colors import Alpha, Colors
00047 from starmac_roslib.viz.markers import MarkerGroup, VerticalLineMarker, TextMarker, TrailMarker, \
00048     HeadingMarker, PolygonMarker, SphereMarker
00049     
00050 #messages:
00051 from flyer_controller.msg import control_mode_hover_info, control_mode_autosequence_info, Autosequence, controller_status
00052 #srv:
00053 from flyer_controller.srv import GetAutosequence, GetAutosequenceRequest, GetAutosequenceResponse
00054 
00055 
00056 class AutosequencesVizModule(VizModuleBase):
00057     """
00058     Visualization module for control_mode_autosequence.
00059     Provides:
00060     - polygon representing autosequence path
00061     - markers for autosequence points
00062     - labels
00063     - heading indicators at each point
00064     
00065     """
00066     got_autosequence = False
00067     current_autosequence = None
00068     latest_autoseq_info = None
00069     latest_hover_info = None
00070     loaded_as_name = ""
00071     
00072     def __init__(self):
00073         super(AutosequencesVizModule, self).__init__(id="autoseq")
00074 
00075     def init_viz(self):
00076         self.as_poly = PolygonMarker('autosequence', (), 
00077                               color=Colors.BLUE+Alpha(0.5), width=0.02, closed=False)
00078         self.as_hover_point_sphere = SphereMarker('hover_point', (0,0,0), radius=0.02, color=Colors.BLUE+Alpha(0.5))
00079         self.mg = MarkerGroup(VizModuleBase.mapub)
00080         self.mg.add(self.as_poly, self.as_hover_point_sphere)
00081         self.init_int_marker()
00082 
00083         
00084     def init_vars(self):
00085         pass
00086     
00087     def init_subscribers(self):
00088         prefix = self.subscriber_topic_prefix
00089         #self.llstatus_sub = rospy.Subscriber(prefix+'autopilot/LL_STATUS', LLStatus, self.llstatus_callback) # AscTec specific.. how to make more generic?
00090         self.autoseq_info_sub = rospy.Subscriber(prefix+'control_mode_autosequence/autosequence_info', 
00091                                                  control_mode_autosequence_info, self.autoseq_info_callback)
00092         self.hover_info_sub = rospy.Subscriber(prefix+'control_mode_autosequence/info', 
00093                                                  control_mode_hover_info, self.hover_info_callback)
00094         self.controller_status_sub = rospy.Subscriber(prefix+'controller/status', controller_status, self.controller_status_callback)
00095 
00096     def init_int_marker(self):
00097         self.ims = InteractiveMarkerServer("simple_marker")
00098         self.im = InteractiveMarker()
00099         self.im.header.frame_id = "/ned"
00100         self.im.name = "my_marker"
00101         self.im.description = "Simple 1-DOF control"
00102         
00103         bm = Marker()
00104         bm.type = Marker.CUBE
00105         bm.scale.x = bm.scale.y = bm.scale.z = 0.45
00106         bm.color.r = 0.0
00107         bm.color.g = 0.5
00108         bm.color.b = 0.5
00109         bm.color.a = 1.0
00110         
00111         bc = InteractiveMarkerControl()
00112         bc.always_visible = True
00113         bc.markers.append(bm)
00114         
00115         self.im.controls.append(bc)
00116         
00117         rc = InteractiveMarkerControl()
00118         rc.name = "move_x"
00119         rc.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
00120         
00121         self.im.controls.append(rc)
00122         
00123         self.ims.insert(self.im, self.process_marker_feedback)
00124         self.ims.applyChanges()
00125         
00126     def process_marker_feedback(self, feedback):
00127         p = feedback.pose.position
00128         print feedback.marker_name + " is now at " + str(p.x) + ", " + str(p.y) + ", " + str(p.z)
00129     
00130     def publish_timer_callback(self, event):
00131         if self.got_autosequence:
00132             as_points = [(p.hover_point.x, p.hover_point.y, 0) for p in self.current_autosequence.points]
00133             self.as_poly.set(points=as_points)
00134             if self.latest_hover_info is not None:
00135                 slhi = self.latest_hover_info
00136                 self.as_hover_point_sphere.set(origin=(slhi.north_cmd, slhi.east_cmd, 0))
00137             now = rospy.Time.now()
00138             VizModuleBase.publish(self,now)
00139 
00140     def autoseq_info_callback(self, msg):
00141         #rospy.loginfo('got autoseq info: %s' % str(msg))
00142         self.latest_autoseq_info = msg
00143 
00144     def hover_info_callback(self, msg):
00145         self.latest_hover_info = msg
00146 
00147     def _new_autosequence(self):
00148         return (self.latest_autoseq_info is not None 
00149                 and len(self.latest_autoseq_info.defined_autosequences) > 0
00150                 and (self.latest_autoseq_info.current_autosequence != self.loaded_as_name))
00151 
00152     def controller_status_callback(self, msg):
00153         self.latest_controller_status = msg
00154         #rospy.loginfo('got controller status: %s' % str(msg))
00155         if msg.active_mode == 'autosequence':
00156             if self._new_autosequence():
00157                 self.got_autosequence = False
00158             if (not self.got_autosequence 
00159                     and self.latest_autoseq_info is not None 
00160                     and len(self.latest_autoseq_info.defined_autosequences) > 0 
00161                     and len(self.latest_autoseq_info.current_autosequence) > 0):
00162                 as_name = self.latest_autoseq_info.current_autosequence
00163                 self.get_autosequence(as_name)
00164             
00165     def get_autosequence(self, as_name):
00166         get_auto_proxy = rospy.ServiceProxy('control_mode_autosequence/get_autosequence', GetAutosequence)
00167         rospy.loginfo("Asking for autosequence '%s'..." % as_name)
00168         resp = get_auto_proxy(autosequence_name=as_name)
00169         if resp.found:
00170             self.current_autosequence = resp.autosequence
00171             rospy.loginfo('Got autosequence: %s' % str(self.current_autosequence))
00172             self.got_autosequence = True
00173             self.loaded_as_name = as_name
00174         else:
00175             rospy.logerr("Service call failed: autosequence '%s' not found" % as_name)


starmac_viz
Author(s): bouffard
autogenerated on Sun Jan 5 2014 11:38:25