cd_testing.py
Go to the documentation of this file.
00001 #! /usr/bin/python
00002 
00003 import sys
00004 import numpy as np
00005 
00006 import roslib
00007 roslib.load_manifest("hrl_pr2_arms")
00008 roslib.load_manifest("kelsey_sandbox")
00009 import rospy
00010 from visualization_msgs.msg import Marker
00011 from geometry_msgs.msg import Point, Quaternion, Vector3
00012 from std_msgs.msg import ColorRGBA
00013 from sensor_msgs.msg import JointState
00014 
00015 import urdf_interface as urdf
00016 import kdl_parser as kdlp
00017 from hrl_pr2_arms.kdl_arm_kinematics import KDLArmKinematics
00018 
00019 JOINT_STATE_INDS_R = [17, 18, 16, 20, 19, 21, 22]
00020 JOINT_STATE_INDS_L = [29, 30, 28, 32, 31, 33, 34]
00021 
00022 class JointForceViz:
00023     def __init__(self, arm):
00024         self.arm = arm
00025         self.vis_pub = rospy.Publisher("force_torque_markers", Marker)
00026         if arm == 'r':
00027             self.arm_inds = JOINT_STATE_INDS_R
00028         else:
00029             self.arm_inds = JOINT_STATE_INDS_L
00030         model = urdf.create_model_from_file("/etc/ros/diamondback/urdf/robot.xml")
00031         tree = kdlp.tree_from_urdf_model(model)
00032         chain = tree.getChain("torso_lift_link", "r_gripper_tool_frame")
00033         link_dict = {0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:11}
00034         self.kinematics = KDLArmKinematics(chain=chain, link_dict=link_dict)
00035         self.colors = [ColorRGBA(1., 0., 0., 1.), ColorRGBA(0., 1., 0., 1.)]
00036         rospy.Subscriber("joint_states", JointState, self.joint_states_cb)
00037 
00038         self.last_CI = np.zeros((6, 6))
00039         self.last_q_pos = np.zeros((7, 1))
00040 
00041     def joint_states_cb(self, msg):
00042         q_pos = np.mat([msg.position[i] for i in self.arm_inds]).T
00043 #q_vel = np.mat([msg.velocity[i] for i in self.arm_inds]).T
00044         q_vel = (q_pos - self.last_q_pos) / 0.01
00045         q_effort = np.mat([msg.effort[i] for i in self.arm_inds]).T
00046         fk_pos, fk_rot = self.kinematics.FK(q_pos)
00047         J = self.kinematics.jacobian(q_pos)
00048         F, res, rank, sing = np.linalg.lstsq(J.T, q_effort)
00049         #print "Force:", F[0:3]
00050         #print "Torque:", F[3:6]
00051         self.publish_vector(fk_pos, 0.01 * F[0:3], 0)
00052         self.publish_vector(fk_pos, 0.04 * F[3:6], 1)
00053         H = self.kinematics.inertia(q_pos)
00054         CI = self.kinematics.cart_inertia(q_pos)
00055 #print q_vel
00056 #print np.round(np.log10(np.fabs(CI)))
00057 #print np.linalg.norm(H, 2), np.linalg.norm(CI, 2), np.linalg.cond(J * np.linalg.inv(H) * J.T), np.linalg.cond(H)
00058         x_vel = J * q_vel
00059 #print np.linalg.norm(x_vel[:3],2)
00060 #print np.linalg.norm(x_vel, 2)
00061 #print np.linalg.norm((CI - self.last_CI) / 0.01 * x_vel, 2)
00062         print np.linalg.norm((CI * x_vel)[:3], 2)
00063 #print np.around(CI * x_vel, 3)
00064         self.last_CI = CI
00065         self.last_q_pos = q_pos
00066         
00067 
00068     def publish_vector(self, loc, v, m_id):
00069         m = Marker()
00070         m.header.frame_id = "torso_lift_link"
00071         m.header.stamp = rospy.Time()
00072         m.ns = "force_torque"
00073         m.id = m_id
00074         m.type = Marker.ARROW
00075         m.action = Marker.ADD
00076         m.points.append(Point(*loc))
00077         m.points.append(Point(*(loc + v)))
00078         m.scale = Vector3(0.01, 0.02, 0.01)
00079         m.color = self.colors[m_id]
00080         self.vis_pub.publish(m)
00081 
00082 
00083 def main():
00084     assert sys.argv[1] in ['r', 'l']
00085     rospy.init_node("test_markers")
00086 
00087     jfv = JointForceViz(sys.argv[1])
00088     rospy.spin()
00089     return
00090 
00091 if __name__ == "__main__":
00092     main()


kelsey_sandbox
Author(s): kelsey
autogenerated on Wed Nov 27 2013 11:52:03