basic_localization.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import sys
4 import time
5 from math import fmod, pi
6 
7 import unittest
8 import rospy
9 import rostest
10 
11 from tf.msg import tfMessage
12 from tf.transformations import euler_from_quaternion
13 from std_srvs.srv import Empty
14 
15 
16 class TestBasicLocalization(unittest.TestCase):
17  def setUp(self):
18  self.tf = None
19  self.target_x = None
20  self.target_y = None
21  self.target_a = None
22 
23  def tf_cb(self, msg):
24  for t in msg.transforms:
25  if t.header.frame_id == 'map':
26  self.tf = t.transform
27  (a_curr, a_diff) = self.compute_angle_diff()
28  print 'Curr:\t %16.6f %16.6f %16.6f' % (self.tf.translation.x, self.tf.translation.y, a_curr)
29  print 'Target:\t %16.6f %16.6f %16.6f' % (self.target_x, self.target_y, self.target_a)
30  print 'Diff:\t %16.6f %16.6f %16.6f' % (
31  abs(self.tf.translation.x - self.target_x), abs(self.tf.translation.y - self.target_y), a_diff)
32 
33  def compute_angle_diff(self):
34  rot = self.tf.rotation
35  a = euler_from_quaternion([rot.x, rot.y, rot.z, rot.w])[2]
36  d_a = self.target_a
37 
38  return (a, abs(fmod(a - d_a + 5*pi, 2*pi) - pi))
39 
41  global_localization = int(sys.argv[1])
42  self.target_x = float(sys.argv[2])
43  self.target_y = float(sys.argv[3])
44  self.target_a = float(sys.argv[4])
45  tolerance_d = float(sys.argv[5])
46  tolerance_a = float(sys.argv[6])
47  target_time = float(sys.argv[7])
48 
49  if global_localization == 1:
50  #print 'Waiting for service global_localization'
51  rospy.wait_for_service('global_localization')
52  global_localization = rospy.ServiceProxy('global_localization', Empty)
53  global_localization()
54 
55  rospy.init_node('test', anonymous=True)
56  while(rospy.rostime.get_time() == 0.0):
57  #print 'Waiting for initial time publication'
58  time.sleep(0.1)
59  start_time = rospy.rostime.get_time()
60  # TODO: This should be replace by a pytf listener
61  rospy.Subscriber('/tf', tfMessage, self.tf_cb)
62 
63  while (rospy.rostime.get_time() - start_time) < target_time:
64  #print 'Waiting for end time %.6f (current: %.6f)'%(target_time,(rospy.rostime.get_time() - start_time))
65  time.sleep(0.1)
66 
67  (a_curr, a_diff) = self.compute_angle_diff()
68  print 'Curr:\t %16.6f %16.6f %16.6f' % (self.tf.translation.x, self.tf.translation.y, a_curr)
69  print 'Target:\t %16.6f %16.6f %16.6f' % (self.target_x, self.target_y, self.target_a)
70  print 'Diff:\t %16.6f %16.6f %16.6f' % (
71  abs(self.tf.translation.x - self.target_x), abs(self.tf.translation.y - self.target_y), a_diff)
72  self.assertNotEquals(self.tf, None)
73  self.assertTrue(abs(self.tf.translation.x - self.target_x) <= tolerance_d)
74  self.assertTrue(abs(self.tf.translation.y - self.target_y) <= tolerance_d)
75  self.assertTrue(a_diff <= tolerance_a)
76 
77 if __name__ == '__main__':
78  rostest.run('amcl', 'amcl_localization',
79  TestBasicLocalization, sys.argv)


amcl
Author(s): Brian P. Gerkey, contradict@gmail.com
autogenerated on Sun Mar 3 2019 03:44:09