arm_viz.py
Go to the documentation of this file.
00001 #
00002 # Copyright (c) 2009, Georgia Tech Research Corporation
00003 # All rights reserved.
00004 # 
00005 # Redistribution and use in source and binary forms, with or without
00006 # modification, are permitted provided that the following conditions are met:
00007 #     * Redistributions of source code must retain the above copyright
00008 #       notice, this list of conditions and the following disclaimer.
00009 #     * Redistributions in binary form must reproduce the above copyright
00010 #       notice, this list of conditions and the following disclaimer in the
00011 #       documentation and/or other materials provided with the distribution.
00012 #     * Neither the name of the Georgia Tech Research Corporation nor the
00013 #       names of its contributors may be used to endorse or promote products
00014 #       derived from this software without specific prior written permission.
00015 # 
00016 # THIS SOFTWARE IS PROVIDED BY GEORGIA TECH RESEARCH CORPORATION ''AS IS'' AND
00017 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 # DISCLAIMED. IN NO EVENT SHALL GEORGIA TECH BE LIABLE FOR ANY DIRECT, INDIRECT,
00020 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00021 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00022 # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00023 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00024 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00025 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 #
00027 
00028 # Author: Advait Jain
00029 
00030 
00031 import math, numpy as np
00032 
00033 import arm_client as ac
00034 import arms as ar
00035 import roslib; roslib.load_manifest('hrl_cody_arms')
00036 import rospy
00037 
00038 import tf.broadcaster as tfb
00039 import hrl_lib.transforms as tr
00040 
00041 from hrl_msgs.msg import FloatArray
00042 from std_msgs.msg import Header
00043 from visualization_msgs.msg import Marker
00044 
00045 def publish_cartesian_markers(arm, time_stamp, cep, rot, c1, c2, marker_id):
00046     marker = Marker()
00047     marker.header.frame_id = ar.link_tf_name(arm, 0)
00048     marker.header.stamp = time_stamp
00049     marker.ns = arm
00050     marker.type = Marker.ARROW
00051     marker.action = Marker.ADD
00052     marker.pose.position.x = cep[0,0]
00053     marker.pose.position.y = cep[1,0]
00054     marker.pose.position.z = cep[2,0]
00055     marker.scale.x = 0.1
00056     marker.scale.y = 0.2
00057     marker.scale.z = 0.2
00058     marker.lifetime = rospy.Duration()
00059 
00060     marker.id = marker_id*100 + 0
00061     #rot1 = tr.Ry(math.radians(90.)) * rot.T
00062     rot1 = rot * tr.rotY(math.pi/2)
00063     quat = tr.matrix_to_quaternion(rot1)
00064     marker.pose.orientation.x = quat[0]
00065     marker.pose.orientation.y = quat[1]
00066     marker.pose.orientation.z = quat[2]
00067     marker.pose.orientation.w = quat[3]
00068 
00069     marker.color.r = c1[0]
00070     marker.color.g = c1[1]
00071     marker.color.b = c1[2]
00072     marker.color.a = 1.
00073     marker_pub.publish(marker)
00074 
00075     marker.id = marker_id*100 + 1
00076     if arm == 'left_arm':
00077         #rot2 = tr.Rz(math.radians(90.)) * rot.T
00078         rot2 = rot * tr.rotZ(-math.pi/2)
00079     else:
00080         #rot2 = tr.Rz(math.radians(-90.)) * rot.T
00081         rot2 = rot * tr.rotZ(math.pi/2)
00082 
00083     quat = tr.matrix_to_quaternion(rot2)
00084     marker.pose.orientation.x = quat[0]
00085     marker.pose.orientation.y = quat[1]
00086     marker.pose.orientation.z = quat[2]
00087     marker.pose.orientation.w = quat[3]
00088 
00089     marker.color.r = c2[0]
00090     marker.color.g = c2[1]
00091     marker.color.b = c2[2]
00092     marker.color.a = 1.
00093     marker_pub.publish(marker)
00094 
00095 
00096 def publish_sphere_marker(color, size, frameid, time_stamp, ns,
00097                           marker_id):
00098     marker = Marker()
00099     marker.header.frame_id = frameid
00100     marker.header.stamp = time_stamp
00101     marker.ns = ns
00102     marker.type = Marker.SPHERE
00103     marker.action = Marker.ADD
00104     marker.pose.position.x = 0.
00105     marker.pose.position.y = 0.
00106     marker.pose.position.z = 0.
00107     marker.scale.x = size
00108     marker.scale.y = size
00109     marker.scale.z = size
00110     marker.lifetime = rospy.Duration()
00111 
00112     marker.id = marker_id
00113     marker.pose.orientation.x = 0
00114     marker.pose.orientation.y = 0
00115     marker.pose.orientation.z = 0
00116     marker.pose.orientation.w = 1
00117 
00118     marker.color.r = color[0]
00119     marker.color.g = color[1]
00120     marker.color.b = color[2]
00121     marker.color.a = 1.
00122     marker_pub.publish(marker)
00123 
00124 
00125 if __name__ == '__main__':
00126     arms = ar.M3HrlRobot()
00127     arm_client = ac.MekaArmClient(arms)
00128 
00129     force_r_pub = rospy.Publisher('/r_arm/force_base', FloatArray)
00130     force_l_pub = rospy.Publisher('/l_arm/force_base', FloatArray)
00131     marker_pub = rospy.Publisher('/cody_arms/viz_marker', Marker)
00132 
00133     rospy.logout('Sleeping ...')
00134     rospy.sleep(1.0)
00135     rospy.logout('... begin')
00136 
00137     r_arm = 'right_arm'
00138     l_arm = 'left_arm'
00139 
00140     transform_bcast = tfb.TransformBroadcaster()
00141     torso_link_name = ar.link_tf_name(r_arm, 0)
00142     while not rospy.is_shutdown():
00143         rospy.sleep(0.1)
00144         f_r = arm_client.get_wrist_force(r_arm, base_frame=True)
00145         f_l = arm_client.get_wrist_force(l_arm, base_frame=True)
00146         time_stamp = rospy.Time.now()
00147         h = Header()
00148         h.stamp = time_stamp
00149         force_r_pub.publish(FloatArray(h, f_r))
00150         force_l_pub.publish(FloatArray(h, f_l))
00151 
00152         publish_sphere_marker((0.5,0.5,0.5), 0.08, torso_link_name,
00153                               time_stamp, 'both_arms', 0)
00154 
00155         for arm in [r_arm, l_arm]:
00156             q = arm_client.get_joint_angles(arm)
00157             links = [2, 3, 7]
00158             for i in links:
00159                 p, rot = arms.FK_all(arm, q, i)
00160                 qaut = tr.matrix_to_quaternion(rot)
00161                 frameid = ar.link_tf_name(arm, i)
00162                 transform_bcast.sendTransform(p.A1.tolist(), qaut, time_stamp,
00163                                               frameid, torso_link_name)
00164                 publish_sphere_marker((0.5,0.1,0.5), 0.05, frameid,
00165                                       time_stamp, arm, i)
00166 
00167             c1 = (0.5, 0.1, 0.5)
00168             c2 = (0.5, 0.5, 0.1)
00169             p, rot = arms.FK_all(arm, q)
00170             publish_cartesian_markers(arm, time_stamp, p, rot, c1, c2,
00171                                       marker_id=76)
00172 
00173             c1 = (0.2, 0.2, 0.2)
00174             c2 = (0.6, 0.6, 0.6)
00175             jep = arm_client.get_jep(arm)
00176             jep = arms.clamp_to_physical_joint_limits(arm, jep)
00177             cep, rot = arms.FK_all(arm, jep)
00178             publish_cartesian_markers(arm, time_stamp, cep, rot, c1, c2,
00179                                 marker_id = 77)
00180 
00181 
00182 
00183 


hrl_cody_arms
Author(s): Advait Jain, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab
autogenerated on Wed Nov 27 2013 12:11:49