test_hironx_ros_bridge.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 PKG = 'hironx_ros_bridge'
00005 # rosbuild needs load_manifest
00006 try:
00007     import roslib
00008     import hironx_ros_bridge
00009 except:
00010     import roslib; roslib.load_manifest(PKG)
00011     import hironx_ros_bridge
00012 
00013 from hironx_ros_bridge import hironx_client as hironx
00014 import rospy, actionlib, math, numpy
00015 import tf
00016 from tf.transformations import quaternion_matrix, euler_from_matrix
00017 
00018 import unittest
00019 
00020 # for catkin compiled environment, pr2_controller_msgs is not catkinized
00021 roslib.load_manifest('pr2_controllers_msgs')
00022 import pr2_controllers_msgs.msg
00023 import trajectory_msgs.msg
00024 
00025 from sensor_msgs.msg import JointState
00026 from pr2_controllers_msgs.msg import JointTrajectoryAction
00027 from trajectory_msgs.msg import JointTrajectoryPoint
00028 from hrpsys_ros_bridge.srv import *
00029 
00030 import time
00031 import tempfile
00032 
00033 class TestHiroROSBridge(unittest.TestCase):
00034 
00035     def joint_states_cb(self, msg):
00036         self.joint_states.append(msg)
00037         self.joint_states = self.joint_states[0:3000]
00038 
00039     def __init__(self, *args, **kwargs):
00040         super(TestHiroROSBridge, self).__init__(*args, **kwargs)
00041         #
00042         self.joint_states = []
00043         rospy.init_node('hironx_ros_bridge_test')
00044         self.joint_states_sub = rospy.Subscriber("joint_states", JointState, self.joint_states_cb)
00045         self.filename_base = tempfile.mkstemp()[1]
00046         self.filenames = []
00047 
00048     @classmethod
00049     def setUpClass(self):
00050         self.robot = hironx.HIRONX()
00051         self.robot.init()
00052 
00053         self.listener = tf.TransformListener()
00054 
00055         self.larm = actionlib.SimpleActionClient("/larm_controller/joint_trajectory_action", JointTrajectoryAction)
00056         self.rarm = actionlib.SimpleActionClient("/rarm_controller/joint_trajectory_action", JointTrajectoryAction)
00057         self.torso = actionlib.SimpleActionClient("/torso_controller/joint_trajectory_action", JointTrajectoryAction)
00058         self.head = actionlib.SimpleActionClient("/head_controller/joint_trajectory_action", JointTrajectoryAction)
00059         self.larm.wait_for_server()
00060         self.rarm.wait_for_server()
00061         self.torso.wait_for_server()
00062         self.head.wait_for_server()
00063 
00064         rospy.wait_for_service('/SequencePlayerServiceROSBridge/setTargetPose')
00065         self.set_target_pose = rospy.ServiceProxy('/SequencePlayerServiceROSBridge/setTargetPose', OpenHRP_SequencePlayerService_setTargetPose)
00066         rospy.wait_for_service('/SequencePlayerServiceROSBridge/waitInterpolationOfGroup')
00067         self.wait_interpolation_of_group = rospy.ServiceProxy('/SequencePlayerServiceROSBridge/waitInterpolationOfGroup', OpenHRP_SequencePlayerService_waitInterpolationOfGroup)
00068 
00069 
00070     def tearDown(self):
00071         self.reset_Pose()
00072         True
00073 
00074     def setUp(self):
00075         self.reset_Pose()
00076 
00077     def reset_Pose(self):
00078         larm_goal = self.goal_LArm()
00079         larm_goal = self.setup_Positions(larm_goal, [[ 0.0, -40.0, -90.0, 0.0, 0.0, 0.0]])
00080         rarm_goal = self.goal_RArm()
00081         rarm_goal = self.setup_Positions(rarm_goal, [[ 0.0, -40.0, -90.0, 0.0, 0.0, 0.0]])
00082         head_goal = self.goal_Head()
00083         head_goal = self.setup_Positions(head_goal, [[0, 20]])
00084         torso_goal = self.goal_Torso()
00085         torso_goal = self.setup_Positions(torso_goal, [[0]])
00086         self.larm.send_goal(larm_goal)
00087         self.rarm.send_goal(rarm_goal)
00088         self.head.send_goal(head_goal)
00089         self.torso.send_goal(torso_goal)
00090         self.larm.wait_for_result()
00091         self.rarm.wait_for_result()
00092         self.head.wait_for_result()
00093         self.torso.wait_for_result()
00094 
00095     def goal_LArm(self):
00096         goal = pr2_controllers_msgs.msg.JointTrajectoryGoal()
00097         goal.trajectory.joint_names.append("LARM_JOINT0")
00098         goal.trajectory.joint_names.append("LARM_JOINT1")
00099         goal.trajectory.joint_names.append("LARM_JOINT2")
00100         goal.trajectory.joint_names.append("LARM_JOINT3")
00101         goal.trajectory.joint_names.append("LARM_JOINT4")
00102         goal.trajectory.joint_names.append("LARM_JOINT5")
00103         return goal
00104 
00105     def goal_RArm(self):
00106         goal = pr2_controllers_msgs.msg.JointTrajectoryGoal()
00107         goal.trajectory.joint_names.append("RARM_JOINT0")
00108         goal.trajectory.joint_names.append("RARM_JOINT1")
00109         goal.trajectory.joint_names.append("RARM_JOINT2")
00110         goal.trajectory.joint_names.append("RARM_JOINT3")
00111         goal.trajectory.joint_names.append("RARM_JOINT4")
00112         goal.trajectory.joint_names.append("RARM_JOINT5")
00113         return goal
00114 
00115     def goal_Torso(self):
00116         goal = pr2_controllers_msgs.msg.JointTrajectoryGoal()
00117         goal.trajectory.joint_names.append("CHEST_JOINT0")
00118         return goal
00119 
00120     def goal_Head(self):
00121         goal = pr2_controllers_msgs.msg.JointTrajectoryGoal()
00122         goal.trajectory.joint_names.append("HEAD_JOINT0")
00123         goal.trajectory.joint_names.append("HEAD_JOINT1")
00124         return goal
00125 
00126     def setup_Positions(self, goal, positions, tm = 1.0):
00127         for p in positions:
00128             point = trajectory_msgs.msg.JointTrajectoryPoint()
00129             point.positions = [ x * math.pi / 180.0 for x in p]
00130             point.time_from_start = rospy.Duration(tm)
00131             goal.trajectory.points.append(point)
00132             tm += tm
00133         return goal
00134 
00135     def check_q_data(self, name):
00136         import math
00137         data = []
00138 
00139         name = name+".q"
00140         f = open(name, 'w')
00141         for j in self.joint_states:
00142             current_time = j.header.stamp.to_sec();
00143             current_value = [p*180/math.pi for p in j.position]
00144             data.append([current_time, current_value[5]])
00145             f.write(str(current_time)+' '+' '.join([str(i) for i in current_value])+"\n")
00146         f.close()
00147         self.filenames.append(name)
00148         return data
00149 
00150     def write_output_to_pdf (self,name):
00151         import os
00152         cmd = "gnuplot -p -e \"set terminal pdf; set output '"+name+"'; plot "
00153         for name in self.filenames:
00154             cmd += "'"+name+"' using 0:7 title '"+name+"' with lines"
00155             if name != self.filenames[-1]:
00156                 cmd += ","
00157         cmd += "\""
00158         os.system(cmd)
00159         return cmd
00160 
00161 # unittest.main()
00162 if __name__ == '__main__':
00163     import rostest
00164     rostest.rosrun(PKG, 'test_hronx_ros_bridge', TestHiroROSBridge)
00165 
00166 
00167 
00168 


hironx_ros_bridge
Author(s): Kei Okada
autogenerated on Sun Sep 13 2015 23:21:39