markers_demo.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Software License Agreement (BSD License)
00004 #
00005 #  Copyright (c) 2011, UC Regents
00006 #  All rights reserved.
00007 #
00008 #  Redistribution and use in source and binary forms, with or without
00009 #  modification, are permitted provided that the following conditions
00010 #  are met:
00011 #
00012 #   * Redistributions of source code must retain the above copyright
00013 #     notice, this list of conditions and the following disclaimer.
00014 #   * Redistributions in binary form must reproduce the above
00015 #     copyright notice, this list of conditions and the following
00016 #     disclaimer in the documentation and/or other materials provided
00017 #     with the distribution.
00018 #   * Neither the name of the University of California nor the names of its
00019 #     contributors may be used to endorse or promote products derived
00020 #     from this software without specific prior written permission.
00021 #
00022 #  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 #  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 #  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 #  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 #  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 #  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 #  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 #  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 #  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 #  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 #  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 #  POSSIBILITY OF SUCH DAMAGE.
00034 
00035 import math
00036 import random
00037 import numpy as np
00038 from numpy.linalg import norm
00039 
00040 import roslib
00041 roslib.load_manifest('starmac_roslib')
00042 from visualization_msgs.msg import Marker, MarkerArray
00043 from geometry_msgs.msg import Point
00044 import rospy
00045 
00046 from starmac_roslib.viz.colors import Alpha, Colors
00047 from starmac_roslib.viz.markers import MarkerGroup, VerticalLineMarker, TextMarker, \
00048     TrailMarker, HeadingMarker, PolygonMarker, PlanarRectangleWithRoundedEnds, \
00049     SphereMarker, SphereListMarker
00050 
00051 def batt_color(voltage):
00052     if voltage > 10.5:
00053         return Colors.GREEN
00054     elif voltage >= 10.0:
00055         return Colors.YELLOW
00056     else:
00057         return Colors.RED
00058     
00059 rospy.init_node('markers_test')
00060 mpub = rospy.Publisher('markers', Marker)
00061 mapub = rospy.Publisher('markers_array', MarkerArray)
00062 mg = MarkerGroup(mapub)
00063 default_frame_id = "/ned"
00064 vlm = VerticalLineMarker('demo', (1,1), color=Colors.WHITE + Alpha(0.5))
00065 tm = TextMarker('demo', 'foo', (0,0,0))
00066 tm2 = TextMarker('demo', 'Hello', (0,0,0))
00067 batt_txt = TextMarker('battery', '', (0,0,0))
00068 trail = TrailMarker('trail', [], colors=[], color=Colors.BLUE + Alpha(0.8))
00069 trail.set_max_points(500)
00070 ground_trail = TrailMarker('ground_track', [], color=Colors.GREEN + Alpha(0.2))
00071 ground_nominal_trail = TrailMarker('commanded', [], color=Colors.YELLOW + Alpha(0.2), width=0.015)
00072 heading = HeadingMarker('heading', (0,0,0), height=0.02)
00073 boundary = PolygonMarker('boundary', ((1,1,0), (-1,1,0), (-1,-1,0), (1,-1,0)), color=Colors.RED+Alpha(0.5), width=0.02)
00074 prre = PlanarRectangleWithRoundedEnds('prre', (0,0,0), (1,0), 5, 0.25, color=Colors.GREEN+Alpha(0.8))
00075 sm = SphereMarker('sphere', (1,1,1), 0.05, color=Colors.BLUE+Alpha(0.9))
00076 slm = SphereListMarker('spherelist', ((0,0,0), (0,0,1)), 0.1, color=Colors.RED+Alpha(0.5))
00077 mg.add(vlm, tm, tm2, batt_txt, trail, ground_trail, ground_nominal_trail, heading, boundary, prre, sm, slm)
00078 r = rospy.Rate(50)
00079 angle = 0
00080 points = []
00081 print "Run the following command:"
00082 print "rosrun rviz rviz -d $(rospack find starmac_roslib)/demos/testviz.vcg"
00083 last_time = rospy.Time.now()
00084 last_pub_time = rospy.Time.now()
00085 x, y, z, vx, vy, vz = 0, 0, 1, 0, 0, 0
00086 xerr, yerr = 0, 0
00087 vxerr, vyerr = 0, 0 
00088 voltage = 12.6
00089 while not rospy.is_shutdown():
00090     this_time = rospy.Time.now()
00091     dt = (this_time - last_time).to_sec()
00092     last_time = this_time
00093     ax, ay, az = (random.gauss(0.05,0.5)*math.cos(angle)+random.gauss(0,0.05) - 0.1*x, 
00094         random.gauss(0.1,0.5)*math.sin(angle)+random.gauss(0,0.1)-0.1*y, 
00095         random.gauss(0,0.5))
00096     vx, vy, vz = vx + ax*dt, vy + ay*dt, vz + az*dt
00097     vxerr, vyerr = random.gauss(0,0.1), random.gauss(0,0.1)
00098     xerr, yerr = xerr + vxerr*dt, yerr + vyerr*dt
00099     if z <= 0.0 and vz < 0:
00100         vz = 0
00101     elif z >= 1.5 and vz > 0:
00102         vz = 0
00103     x, y, z = min(10,max(-10,x + vx*dt )), min(10,max(-10,y + vy*dt )), min(1.5,max(0,z + vz*dt)) 
00104     xout,yout = x + xerr, y+ yerr
00105     alt = z + random.gauss(0,0.001)
00106     voltage = max(9.0, voltage - dt*3/(3*60))
00107     rospy.logdebug('%f, %f, %f' % (xout, yout, alt))
00108     trail.append_points([(xout,yout,alt)], [(min(1,1.0*alt/2.0), 1-min(1,1.0*alt/1.0), 1-min(1,alt/0.8), 0.9)])
00109     ground_trail.append_points([(xout,yout,0)])
00110     ground_nominal_trail.append_points([(x,y,0)])
00111     vlm.set((xout,yout), zend=alt)
00112     tm.set(text="%.2f,%.2f" % (xout,yout), pos=(xout,yout,-0.02))
00113     tm2.set(text="%.2f" % alt, pos=(xout+0.1,yout,alt/2))
00114     batt_txt.set(text="%.2f V" % voltage, color=batt_color(voltage) + Alpha(1.0), pos=(xout,yout,alt+0.1))
00115     heading.set(pos=(xout,yout,z+0.01), heading=math.degrees(math.atan2(vy,vx)))
00116     prre.set(origin=(xout,yout,0), axis=(vx,vy), length=norm(np.array([vx,vy])))
00117     dt_pub = (this_time - last_pub_time).to_sec()
00118     if dt_pub > 0.05:
00119         mg.publish()
00120         last_pub_time = this_time
00121     angle += math.radians(0.5) % math.radians(360)
00122     r.sleep()


starmac_roslib
Author(s): Patrick Bouffard
autogenerated on Sun Jan 5 2014 11:38:14