00001
00002
00003 import numpy as np, math
00004
00005 import roslib; roslib.load_manifest('hrl_common_code_darpa_m3')
00006 import hrl_lib.viz as hv
00007 import rospy
00008
00009 from visualization_msgs.msg import Marker
00010 from hrl_haptic_manipulation_in_clutter_msgs.msg import SkinContact
00011
00012
00013
00014
00015 def get_arrow_text_markers(p, f, frame, m_id, duration, c=(1.,0.,0.,1.0)):
00016 t_now = rospy.Time.now()
00017 q = hv.arrow_direction_to_quat(f)
00018 arrow_len = np.linalg.norm(f) * 0.04
00019
00020 f_unit = f/np.linalg.norm(f)
00021
00022
00023
00024
00025
00026
00027 scale = (0.20, 0.20, arrow_len)
00028
00029 m1 = hv.single_marker(p, q, 'arrow', frame, scale, c, m_id = m_id,
00030 duration = duration)
00031 m1.header.stamp = t_now
00032
00033 m2 = hv.single_marker(p + f_unit * arrow_len * 1.6, q, 'text_view_facing', frame,
00034
00035 (0.07, 0.07, 0.07), m_id = m_id+1,
00036 duration = duration, color=c)
00037 m2.text = '%.1fN'%(np.linalg.norm(f))
00038 m2.header.stamp = t_now
00039 return m1, m2
00040
00041 def visualize_skin(sc, callback_args):
00042 marker_pub, display_normal_component = callback_args
00043 if sc.link_names == []:
00044 return
00045
00046 n = len(sc.link_names)
00047 for i in range(len(sc.link_names)):
00048 l = sc.locations[i]
00049 mn = np.matrix([l.x, l.y, l.z]).T
00050 ff = sc.forces[i]
00051 f = np.matrix([ff.x, ff.y, ff.z]).T
00052 nn = sc.normals[i]
00053 nrml = np.matrix([nn.x, nn.y, nn.z]).T
00054
00055 frame = sc.header.frame_id
00056 if opt.ac == 'green':
00057 c = (0.,0.5,0.,1.0)
00058 elif opt.ac == 'blue':
00059 c = (0.,0.,1.0,1.0)
00060 else:
00061 raise 'Unrecognized color'
00062 m1, m2 = get_arrow_text_markers(mn, f, frame,
00063 m_id = 4*i+1, duration=0.2,
00064 c=c)
00065 m1.header.stamp = sc.header.stamp
00066 m2.header.stamp = sc.header.stamp
00067 marker_pub.publish(m1)
00068 marker_pub.publish(m2)
00069
00070 if display_normal_component:
00071 m3, m4 = get_arrow_text_markers(mn, nrml*(nrml.T*f)[0,0],
00072 frame, m_id = 4*i+3,
00073 duration=0.2,
00074 c=(1.0,0.,0,0.5))
00075 m3.header.stamp = sc.header.stamp
00076 m4.header.stamp = sc.header.stamp
00077 marker_pub.publish(m3)
00078
00079
00080
00081
00082 if __name__ == '__main__':
00083 import optparse
00084 p = optparse.OptionParser()
00085 p.add_option('--arrow_color', action='store', dest='ac',
00086 type='string', help='green or blue',
00087 default='green')
00088 p.add_option('--display_normal_component', action='store_true',
00089 dest='dnc', help='display an rviz marker for the normal conponent of the contact forces.')
00090 opt, args = p.parse_args()
00091
00092
00093 rospy.init_node('skin_visualize_publisher')
00094 marker_pub = rospy.Publisher('/skin/viz/contacts', Marker)
00095 rospy.Subscriber('/skin/contacts', SkinContact, visualize_skin,
00096 callback_args = (marker_pub, opt.dnc))
00097 rospy.loginfo('Started visulizing skin!')
00098 rospy.spin()
00099
00100
00101
00102
00103
00104