check_walk_pose.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (c) 2020 Kei Okada
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 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 # 2. 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 # 3. Neither the name of the Rethink Robotics 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 argparse
31 import csv
32 import unittest
33 import rospy
34 import actionlib
35 from nav_msgs.msg import Odometry
36 from std_srvs.srv import Empty
37 from tf.transformations import euler_from_quaternion
38 from control_msgs.msg import FollowJointTrajectoryAction, FollowJointTrajectoryGoal
39 from trajectory_msgs.msg import JointTrajectoryPoint
40 
41 
42 class TestWalkPose(unittest.TestCase):
43 
44  @classmethod
45  def setUpClass(self):
46  parser = argparse.ArgumentParser(description='Check gundam robot.')
47  parser.add_argument('--pos', dest='pos', default=[0, 0, 0], nargs=3, type=float,
48  help='target position')
49  parser.add_argument('--rot', dest='rot', default=[0, 0, 0], nargs=3, type=float,
50  help='target orientation')
51  parser.add_argument('filename', type=str, nargs='?',
52  help='filename for trajectory pattern csv')
53  args, unknown = parser.parse_known_args()
54  self.goal_pos = args.pos
55  self.goal_rot = args.rot
56  self.filename = args.filename
57 
58  print("Initializing node... ")
59  rospy.init_node('test_walk_pose', anonymous=True)
60  self.base_success = False
61  rospy.sleep(1)
62  print("Running.")
63 
64  # copied cdoe from gundam_rx78_control/sample/joint_trajectory_client_csv.py
65  # init goal
66  goal = FollowJointTrajectoryGoal()
67  goal.goal_time_tolerance = rospy.Time(1)
68 
69  rospy.loginfo("Opening {}".format(self.filename))
70  with open(self.filename) as f:
71  reader = csv.reader(f, skipinitialspace=True)
72  first_row = True
73  for row in reader:
74  if first_row:
75  goal.trajectory.joint_names = row[1:]
76  first_row = False
77  else:
78  point = JointTrajectoryPoint()
79  point.positions = [float(n) for n in row[1:]]
80  point.time_from_start = rospy.Duration(float(row[0]))
81  goal.trajectory.points.append(point)
83  '/fullbody_controller/follow_joint_trajectory',
84  FollowJointTrajectoryAction,
85  )
86 
87  if not self.client.wait_for_server(timeout=rospy.Duration(10)):
88  rospy.logerr("Timed out waiting for Action Server")
89  rospy.signal_shutdown("Timed out waiting for Action Server")
90  sys.exit(1)
91 
92  # send goal
93  goal.trajectory.header.stamp = rospy.Time.now()
94  self.client.send_goal(goal)
95 
96  def base_link_cb(self, msg):
97  p = msg.pose.pose.position
98  r = msg.pose.pose.orientation
99  pos = [p.x, p.y, p.z]
100  rot = euler_from_quaternion([r.x, r.y, r.z, r.w])
101 
102  diff_pos = [abs(pos[0] - self.goal_pos[0]), abs(pos[1] - self.goal_pos[1]), abs(pos[2] - self.goal_pos[2])]
103  diff_rot = [abs(rot[0] - self.goal_rot[0]), abs(rot[1] - self.goal_rot[1]), abs(rot[2] - self.goal_rot[2])]
104 
105  rospy.loginfo("pos: {:6.3f} {:6.3f} {:6.3f} - ".format(pos[0], pos[1], pos[2]) +
106  "rot: {:6.3f} {:6.3f} {:6.3f} < ".format(rot[0], rot[1], rot[2]) +
107  "pos: {:6.3f} {:6.3f} {:6.3f} - ".format(diff_pos[0], diff_pos[1], diff_pos[2]) +
108  "rot: {:6.3f} {:6.3f} {:6.3f}".format(diff_rot[0], diff_rot[1], diff_rot[2]))
109 
110  # check position
111  if (diff_pos[0] < 0.5 and diff_pos[1] < 0.5 and diff_pos[2] < 0.5 and
112  diff_rot[0] < 0.1 and diff_rot[1] < 0.1 and diff_rot[2] < 0.1):
113  self.base_success = True
114 
115  def test_walk_pose(self):
116  rospy.Subscriber("/base_link_ground_truth", Odometry, self.base_link_cb)
117  timeout_t = rospy.Time.now() + rospy.Duration(40)
118  while not rospy.is_shutdown() and not self.base_success and rospy.Time.now() < timeout_t:
119  rospy.sleep(rospy.Duration(1.0))
120  self.assertTrue(self.base_success)
121 
122 
123 if __name__ == '__main__':
124  import rostest
125  rostest.rosrun('gundam_rx78_gazebo', 'test_walk_pose', TestWalkPose)


gundam_rx78_gazebo
Author(s): Kei Okada
autogenerated on Wed Sep 2 2020 03:33:39