single_transform_unittest.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2008, Willow Garage, Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of Willow Garage, Inc. nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 
34 
35 import sys
36 import unittest
37 import os
38 import rospy
39 import time
40 import numpy
41 
42 from calibration_estimation.single_transform import SingleTransform, rpy_to_quat, quat_to_rpy
43 from numpy import *
44 
45 class TestSingleTransform(unittest.TestCase):
46 
47  def test_free(self):
48 
49  st = SingleTransform([0, 0, 0, 0, 0, 0])
50  free_list = st.calc_free( [0, 0, 1, 1, 0, 0] )
51 
52  self.assertEqual(free_list[0], False)
53  self.assertEqual(free_list[1], False)
54  self.assertEqual(free_list[2], True)
55  self.assertEqual(free_list[3], True)
56  self.assertEqual(free_list[4], False)
57  self.assertEqual(free_list[5], False)
58 
59  def test_inflate(self):
60  st = SingleTransform([0, 0, 0, 0, 0, 0])
61  st.inflate( reshape( matrix([1, 0, 0, 0, 0, 0], float), (-1,1) ))
62  expected = numpy.matrix( [[ 1, 0, 0, 1],
63  [ 0, 1, 0, 0],
64  [ 0, 0, 1, 0],
65  [ 0, 0, 0, 1]], float )
66 
67  print ""
68  print st.transform
69 
70  self.assertAlmostEqual(numpy.linalg.norm(st.transform-expected), 0.0, 6)
71 
72  def test_deflate(self):
73  st = SingleTransform([0, 0, 0, 0, 0, 0])
74  p = reshape( matrix([1, 0, 0, 0, 0, 0], float), (-1,1) )
75  st.inflate(p)
76  result = st.deflate()
77  self.assertAlmostEqual(numpy.linalg.norm(p-result), 0.0, 6)
78 
80  st = SingleTransform()
81  p = reshape( matrix([1, 0, 0, 0, 0, 0], float), (-1,1) )
82  config = st.params_to_config(p)
83  self.assertAlmostEqual( config[0], 1, 6)
84  self.assertAlmostEqual( config[1], 0, 6)
85 
86 
87  def test_easy_trans_1(self):
88  st = SingleTransform([1, 2, 3, 0, 0, 0])
89  expected = numpy.matrix( [[ 1, 0, 0, 1],
90  [ 0, 1, 0, 2],
91  [ 0, 0, 1, 3],
92  [ 0, 0, 0, 1]], float )
93 
94  print ""
95  print st.transform
96 
97  self.assertAlmostEqual(numpy.linalg.norm(st.transform-expected), 0.0, 6)
98 
99  def test_easy_rot_1(self):
100  st = SingleTransform([0, 0, 0, 0, 0, pi/2])
101  expected = numpy.matrix( [[ 0,-1, 0, 0],
102  [ 1, 0, 0, 0],
103  [ 0, 0, 1, 0],
104  [ 0, 0, 0, 1]], float )
105  print ""
106  print st.transform
107 
108  self.assertAlmostEqual(numpy.linalg.norm(st.transform-expected), 0.0, 6)
109 
110  def test_easy_rot_2(self):
111  st = SingleTransform([0, 0, 0, 0, pi/2, 0])
112  expected = numpy.matrix( [[ 0, 0, 1, 0],
113  [ 0, 1, 0, 0],
114  [-1, 0, 0, 0],
115  [ 0, 0, 0, 1]], float )
116  print ""
117  print st.transform
118 
119  self.assertAlmostEqual(numpy.linalg.norm(st.transform-expected), 0.0, 6)
120 
121  def test_easy_rot_3(self):
122  st = SingleTransform([0, 0, 0, pi/2, 0, 0])
123  expected = numpy.matrix( [[ 1, 0, 0, 0],
124  [ 0, 0,-1, 0],
125  [ 0, 1, 0, 0],
126  [ 0, 0, 0, 1]], float )
127  print ""
128  print st.transform
129 
130  self.assertAlmostEqual(numpy.linalg.norm(st.transform-expected), 0.0, 6)
131 
132  def test_length(self):
133  st = SingleTransform([0, 0, 0, 0, 0, 0])
134  self.assertEqual(st.get_length(), 6)
135 
136  def test_hard(self):
137  sample_nums = range(1,6)
138  dir_name = os.path.dirname(os.path.realpath(__file__))
139  params_filenames = [os.path.join(dir_name, 'data', 'single_transform_data', 'params_%02u.txt' % n) for n in sample_nums]
140  transform_filenames = [os.path.join(dir_name, 'data', 'single_transform_data', 'transform_%02u.txt' % n) for n in sample_nums]
141 
142  for params_filename, transform_filename in zip(params_filenames, transform_filenames):
143  f_params = open(params_filename)
144  f_transforms = open(transform_filename)
145  param_elems = [float(x) for x in f_params.read().split()]
146  transform_elems = [float(x) for x in f_transforms.read().split()]
147 
148  st = SingleTransform(param_elems)
149  expected_result = numpy.matrix(transform_elems)
150  expected_result.shape = 4,4
151 
152  self.assertAlmostEqual(numpy.linalg.norm(st.transform-expected_result), 0.0, 4, "Failed on %s" % params_filename)
153 
154  def test_math(self):
155  rpy_init = (1, 0.5, 0.7)
156  rpy = quat_to_rpy(rpy_to_quat(rpy_init))
157  for i in range(3):
158  self.assertAlmostEqual(rpy_init[i], rpy[i])
159 
160 if __name__ == '__main__':
161  import rostest
162  rostest.unitrun('pr2_calibration_estimation', 'test_SingleTransform', TestSingleTransform, coverage_packages=['pr2_calibration_estimation.single_transform'])
163 


calibration_estimation
Author(s): Vijay Pradeep, Michael Ferguson
autogenerated on Fri Apr 2 2021 02:12:53