display_3D_visualization.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (c) 2012, Tang Tiong Yew
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # * Neither the name of the Willow Garage, Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29 
30 import rospy
31 try:
32  from visual import *
33 except:
34  from vpython import *
35 import math
36 import wx
37 
38 from sensor_msgs.msg import Imu
39 from tf.transformations import euler_from_quaternion
40 
41 rad2degrees = 180.0/math.pi
42 precision = 2 #round to this number of digits
43 yaw_offset = 0 #used to align animation upon key press
44 
45 
46 #Create shutdown hook to kill visual displays
48  #print "Killing displays"
49  wx.Exit()
50 
51 #register shutdown hook
52 rospy.on_shutdown(shutdown_hook)
53 
54 
55 # Main scene
56 scene=display(title="9DOF Razor IMU Main Screen")
57 #scene.range=(1.3,1.3,1.3)
58 scene.range=1.3
59 scene.forward = (1,0,-0.25)
60 # Change reference axis (x,y,z) - z is up
61 scene.up=(0,0,1)
62 scene.width=500
63 scene.height=500
64 
65 # Second scene (Roll, Pitch, Yaw)
66 scene2 = display(title='9DOF Razor IMU Roll, Pitch, Yaw',x=550, y=0, width=500, height=500,center=(0,0,0), background=(0,0,0))
67 #scene2.range=(1,1,1)
68 scene2.range=1
69 scene2.select()
70 #Roll, Pitch, Yaw
71 #Default reference, i.e. x runs right, y runs up, z runs backward (out of screen)
72 cil_roll = cylinder(pos=(-0.5,0.3,0),axis=(0.2,0,0),radius=0.01,color=color.red)
73 cil_roll2 = cylinder(pos=(-0.5,0.3,0),axis=(-0.2,0,0),radius=0.01,color=color.red)
74 cil_pitch = arrow(pos=(0.5,0.3,0),axis=(0,0,-0.4),shaftwidth=0.02,color=color.green)
75 arrow_course = arrow(pos=(0.0,-0.4,0),color=color.cyan,axis=(0,0.2,0), shaftwidth=0.02, fixedwidth=1)
76 
77 #Roll,Pitch,Yaw labels
78 label(pos=(-0.5,0.6,0),text="Roll (degrees / radians)",box=0,opacity=0)
79 label(pos=(0.5,0.6,0),text="Pitch (degrees / radians)",box=0,opacity=0)
80 label(pos=(0.0,0.02,0),text="Yaw (degrees / radians)",box=0,opacity=0)
81 label(pos=(0.0,-0.16,0),text="N",box=0,opacity=0,color=color.yellow)
82 label(pos=(0.0,-0.64,0),text="S",box=0,opacity=0,color=color.yellow)
83 label(pos=(-0.24,-0.4,0),text="W",box=0,opacity=0,color=color.yellow)
84 label(pos=(0.24,-0.4,0),text="E",box=0,opacity=0,color=color.yellow)
85 label(pos=(0.18,-0.22,0),height=7,text="NE",box=0,color=color.yellow)
86 label(pos=(-0.18,-0.22,0),height=7,text="NW",box=0,color=color.yellow)
87 label(pos=(0.18,-0.58,0),height=7,text="SE",box=0,color=color.yellow)
88 label(pos=(-0.18,-0.58,0),height=7,text="SW",box=0,color=color.yellow)
89 
90 rollLabel = label(pos=(-0.5,0.52,0),text="-",box=0,opacity=0,height=12)
91 pitchLabel = label(pos=(0.5,0.52,0),text="-",box=0,opacity=0,height=12)
92 yawLabel = label(pos=(0,-0.06,0),text="-",box=0,opacity=0,height=12)
93 
94 #acceleration labels
95 label(pos=(0,0.9,0),text="Linear Acceleration x / y / z (m/s^2)",box=0,opacity=0)
96 label(pos=(0,-0.8,0),text="Angular Velocity x / y / z (rad/s)",box=0,opacity=0)
97 linAccLabel = label(pos=(0,0.82,0),text="-",box=0,opacity=0,height=12)
98 angVelLabel = label(pos=(0,-0.88,0),text="-",box=0,opacity=0,height=12)
99 
100 # Main scene objects
101 scene.select()
102 # Reference axis (x,y,z) - using ROS conventions (REP 103) - z is up, y left (west, 90 deg), x is forward (north, 0 deg)
103 # In visual, z runs up, x runs forward, y runs left (see scene.up command earlier)
104 # So everything is good
105 arrow(color=color.green,axis=(1,0,0), shaftwidth=0.04, fixedwidth=1)
106 arrow(color=color.green,axis=(0,1,0), shaftwidth=0.04 , fixedwidth=1)
107 arrow(color=color.green,axis=(0,0,1), shaftwidth=0.04, fixedwidth=1)
108 
109 # labels
110 label(pos=(0,0,-1.2),text="Press 'a' to align",box=0,opacity=0)
111 label(pos=(1,0.1,0),text="X",box=0,opacity=0)
112 label(pos=(0,1,-0.1),text="Y",box=0,opacity=0)
113 label(pos=(0,-0.1,1),text="Z",box=0,opacity=0)
114 # IMU object
115 platform = box(length=1.0, height=0.05, width=0.65, color=color.red,up=(0,0,1),axis=(-1,0,0))
116 p_line = box(length=1.1,height=0.08,width=0.1,color=color.yellow,up=(0,0,1),axis=(-1,0,0))
117 plat_arrow = arrow(length=-0.8,color=color.cyan,up=(0,0,1),axis=(-1,0,0), shaftwidth=0.06, fixedwidth=1)
118 plat_arrow_up = arrow(length=0.4,color=color.cyan,up=(-1,0,0),axis=(0,0,1), shaftwidth=0.06, fixedwidth=1)
119 rospy.init_node("display_3D_visualization_node")
120 
121 def processIMU_message(imuMsg):
122  global yaw_offset
123 
124  roll=0
125  pitch=0
126  yaw=0
127 
128  quaternion = (
129  imuMsg.orientation.x,
130  imuMsg.orientation.y,
131  imuMsg.orientation.z,
132  imuMsg.orientation.w)
133  (roll,pitch,yaw) = euler_from_quaternion(quaternion)
134 
135  #add align offset to yaw
136  yaw += yaw_offset
137 
138  axis=(-cos(pitch)*cos(yaw),-cos(pitch)*sin(yaw),sin(pitch))
139  up=(sin(roll)*sin(yaw)+cos(roll)*sin(pitch)*cos(yaw),-sin(roll)*cos(yaw)+cos(roll)*sin(pitch)*sin(yaw),cos(roll)*cos(pitch))
140  platform.axis=axis
141  platform.up=up
142  platform.length=1.0
143  plat_arrow_up.axis=up
144  plat_arrow_up.up=axis
145  plat_arrow_up.length=0.4
146  plat_arrow.axis=axis
147  plat_arrow.up=up
148  plat_arrow.length=-0.8
149  p_line.axis=axis
150  p_line.up=up
151  p_line.length=1.1
152  cil_roll.axis=(-0.2*cos(roll),0.2*sin(roll),0)
153  cil_roll2.axis=(0.2*cos(roll),-0.2*sin(roll),0)
154  cil_pitch.axis=(0,-0.4*sin(pitch),-0.4*cos(pitch))
155  #remove yaw_offset from yaw display
156  arrow_course.axis=(-0.2*sin(yaw-yaw_offset),0.2*cos(yaw-yaw_offset),0)
157 
158  #display in degrees / radians
159  rollLabel.text = str(round(roll*rad2degrees, precision)) + " / " + str(round(roll,precision))
160  pitchLabel.text = str(round(pitch*rad2degrees, precision)) + " / " + str(round(pitch, precision))
161  #remove yaw_offset from yaw display
162  yawLabel.text = str(round((yaw-yaw_offset)*rad2degrees, precision)) + " / " + str(round((yaw-yaw_offset), precision))
163 
164  linAccLabel.text = str(round(imuMsg.linear_acceleration.x, precision)) + " / " + str(round(imuMsg.linear_acceleration.y, precision)) + " / " + str(round(imuMsg.linear_acceleration.z, precision))
165  angVelLabel.text = str(round(imuMsg.angular_velocity.x, precision)) + " / " + str(round(imuMsg.angular_velocity.y, precision)) + " / " + str(round(imuMsg.angular_velocity.z, precision))
166 
167  #check for align key press - if pressed, next refresh will be aligned
168  try:
169  if scene.kb.keys: # event waiting to be processed?
170  s = scene.kb.getkey() # get keyboard info
171  if s == 'a':
172  #align key pressed - align
173  yaw_offset += -yaw
174  except:
175  k = keysdown()
176  if 'a' in k:
177  #align key pressed - align
178  yaw_offset += -yaw
179 
180 
181 sub = rospy.Subscriber('imu', Imu, processIMU_message)
182 rospy.spin()


razor_imu_9dof
Author(s): Tang Tiong Yew, Kristof Robot, Paul Bouchier, Peter Bartz
autogenerated on Sat May 1 2021 02:09:10