utest.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #*********************************************************************
3 # Software License Agreement (BSD License)
4 #
5 # Copyright (c) 2015, Bossa Nova Robotics
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 #
12 # * Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # * Redistributions in binary form must reproduce the above
15 # copyright notice, this list of conditions and the following
16 # disclaimer in the documentation and/or other materials provided
17 # with the distribution.
18 # * Neither the name of the Bossa Nova Robotics nor the names of its
19 # contributors may be used to endorse or promote products derived
20 # from this software without specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 # POSSIBILITY OF SUCH DAMAGE.
34 #********************************************************************/
35 from angles import normalize_angle_positive, normalize_angle, shortest_angular_distance, two_pi_complement, shortest_angular_distance_with_limits
36 from angles import _find_min_max_delta
37 import sys
38 import unittest
39 from math import pi, fabs
40 
41 ## A sample python unit test
42 class TestAngles(unittest.TestCase):
44  result, shortest_angle = shortest_angular_distance_with_limits(-0.5, 0.5,-0.25,0.25)
45  self.assertFalse(result)
46 
47  result, shortest_angle = shortest_angular_distance_with_limits(-0.5, 0.5,0.25,0.25)
48  self.assertFalse(result)
49 
50  result, shortest_angle = shortest_angular_distance_with_limits(-0.5, 0.5,0.25,-0.25)
51  self.assertTrue(result)
52  self.assertAlmostEqual(shortest_angle, -2*pi+1.0)
53 
54  result, shortest_angle = shortest_angular_distance_with_limits(0.5, 0.5,0.25,-0.25)
55  self.assertTrue(result)
56  self.assertAlmostEqual(shortest_angle, 0)
57 
58  result, shortest_angle = shortest_angular_distance_with_limits(0.5, 0,0.25,-0.25)
59  self.assertFalse(result)
60  self.assertAlmostEqual(shortest_angle, -0.5)
61 
62  result, shortest_angle = shortest_angular_distance_with_limits(-0.5, 0,0.25,-0.25)
63  self.assertFalse(result)
64  self.assertAlmostEqual(shortest_angle, 0.5)
65 
66  result, shortest_angle = shortest_angular_distance_with_limits(-0.2,0.2,0.25,-0.25)
67  self.assertFalse(result)
68  self.assertAlmostEqual(shortest_angle, -2*pi+0.4)
69 
70  result, shortest_angle = shortest_angular_distance_with_limits(0.2,-0.2,0.25,-0.25)
71  self.assertFalse(result)
72  self.assertAlmostEqual(shortest_angle,2*pi-0.4)
73 
74  result, shortest_angle = shortest_angular_distance_with_limits(0.2,0,0.25,-0.25)
75  self.assertFalse(result)
76  self.assertAlmostEqual(shortest_angle,2*pi-0.2)
77 
78  result, shortest_angle = shortest_angular_distance_with_limits(-0.2,0,0.25,-0.25)
79  self.assertFalse(result)
80  self.assertAlmostEqual(shortest_angle,-2*pi+0.2)
81 
82  result, shortest_angle = shortest_angular_distance_with_limits(-0.25,-0.5,0.25,-0.25)
83  self.assertTrue(result)
84  self.assertAlmostEqual(shortest_angle,-0.25)
85 
86  result, shortest_angle = shortest_angular_distance_with_limits(-0.25,0.5,0.25,-0.25)
87  self.assertTrue(result)
88  self.assertAlmostEqual(shortest_angle,-2*pi+0.75)
89 
90  result, shortest_angle = shortest_angular_distance_with_limits(-0.2500001,0.5,0.25,-0.25)
91  self.assertTrue(result)
92  self.assertAlmostEqual(shortest_angle,-2*pi+0.5+0.2500001)
93 
94  result, shortest_angle = shortest_angular_distance_with_limits(-0.6, 0.5,-0.25,0.25)
95  self.assertFalse(result)
96 
97  result, shortest_angle = shortest_angular_distance_with_limits(-0.5, 0.6,-0.25,0.25)
98  self.assertFalse(result)
99 
100  result, shortest_angle = shortest_angular_distance_with_limits(-0.6, 0.75,-0.25,0.3)
101  self.assertFalse(result)
102 
103  result, shortest_angle = shortest_angular_distance_with_limits(-0.6, pi*3.0/4.0,-0.25,0.3)
104  self.assertFalse(result)
105 
106  result, shortest_angle = shortest_angular_distance_with_limits(-pi, pi,-pi,pi)
107  self.assertTrue(result)
108  self.assertAlmostEqual(shortest_angle,0.0)
109 
111  self.assertAlmostEqual(0, normalize_angle_positive(0))
112  self.assertAlmostEqual(pi, normalize_angle_positive(pi))
113  self.assertAlmostEqual(0, normalize_angle_positive(2*pi))
114  self.assertAlmostEqual(pi, normalize_angle_positive(3*pi))
115  self.assertAlmostEqual(0, normalize_angle_positive(4*pi))
116 
117  self.assertAlmostEqual(0, normalize_angle_positive(-0))
118  self.assertAlmostEqual(pi, normalize_angle_positive(-pi))
119  self.assertAlmostEqual(0, normalize_angle_positive(-2*pi))
120  self.assertAlmostEqual(pi, normalize_angle_positive(-3*pi))
121  self.assertAlmostEqual(0, normalize_angle_positive(-4*pi))
122 
123  self.assertAlmostEqual(0, normalize_angle_positive(-0))
124  self.assertAlmostEqual(3*pi/2, normalize_angle_positive(-pi/2))
125  self.assertAlmostEqual(pi, normalize_angle_positive(-pi))
126  self.assertAlmostEqual(pi/2, normalize_angle_positive(-3*pi/2))
127  self.assertAlmostEqual(0, normalize_angle_positive(-4*pi/2))
128 
129  self.assertAlmostEqual(0, normalize_angle_positive(0))
130  self.assertAlmostEqual(pi/2, normalize_angle_positive(pi/2))
131  self.assertAlmostEqual(pi/2, normalize_angle_positive(5*pi/2))
132  self.assertAlmostEqual(pi/2, normalize_angle_positive(9*pi/2))
133  self.assertAlmostEqual(pi/2, normalize_angle_positive(-3*pi/2))
134 
136  self.assertAlmostEqual(0, normalize_angle(0))
137  self.assertAlmostEqual(pi, normalize_angle(pi))
138  self.assertAlmostEqual(0, normalize_angle(2*pi))
139  self.assertAlmostEqual(pi, normalize_angle(3*pi))
140  self.assertAlmostEqual(0, normalize_angle(4*pi))
141 
142  self.assertAlmostEqual(0, normalize_angle(-0))
143  self.assertAlmostEqual(pi, normalize_angle(-pi))
144  self.assertAlmostEqual(0, normalize_angle(-2*pi))
145  self.assertAlmostEqual(pi, normalize_angle(-3*pi))
146  self.assertAlmostEqual(0, normalize_angle(-4*pi))
147 
148  self.assertAlmostEqual(0, normalize_angle(-0))
149  self.assertAlmostEqual(-pi/2, normalize_angle(-pi/2))
150  self.assertAlmostEqual(pi, normalize_angle(-pi))
151  self.assertAlmostEqual(pi/2, normalize_angle(-3*pi/2))
152  self.assertAlmostEqual(0, normalize_angle(-4*pi/2))
153 
154  self.assertAlmostEqual(0, normalize_angle(0))
155  self.assertAlmostEqual(pi/2, normalize_angle(pi/2))
156  self.assertAlmostEqual(pi/2, normalize_angle(5*pi/2))
157  self.assertAlmostEqual(pi/2, normalize_angle(9*pi/2))
158  self.assertAlmostEqual(pi/2, normalize_angle(-3*pi/2))
159 
161  self.assertAlmostEqual(pi/2, shortest_angular_distance(0, pi/2))
162  self.assertAlmostEqual(-pi/2, shortest_angular_distance(0, -pi/2))
163  self.assertAlmostEqual(-pi/2, shortest_angular_distance(pi/2, 0))
164  self.assertAlmostEqual(pi/2, shortest_angular_distance(-pi/2, 0))
165 
166  self.assertAlmostEqual(-pi/2, shortest_angular_distance(pi, pi/2))
167  self.assertAlmostEqual(pi/2, shortest_angular_distance(pi, -pi/2))
168  self.assertAlmostEqual(pi/2, shortest_angular_distance(pi/2, pi))
169  self.assertAlmostEqual(-pi/2, shortest_angular_distance(-pi/2, pi))
170 
171  self.assertAlmostEqual(-pi/2, shortest_angular_distance(5*pi, pi/2))
172  self.assertAlmostEqual(pi/2, shortest_angular_distance(7*pi, -pi/2))
173  self.assertAlmostEqual(pi/2, shortest_angular_distance(9*pi/2, pi))
174  self.assertAlmostEqual(pi/2, shortest_angular_distance(-3*pi/2, pi))
175 
176  # Backside wrapping
177  self.assertAlmostEqual(-pi/2, shortest_angular_distance(-3*pi/4, 3*pi/4))
178  self.assertAlmostEqual(pi/2, shortest_angular_distance(3*pi/4, -3*pi/4))
179 
181  epsilon = 1e-9
182  self.assertAlmostEqual(two_pi_complement(0), 2*pi)
183  self.assertAlmostEqual(two_pi_complement(2*pi), 0)
184  self.assertAlmostEqual(two_pi_complement(-2*pi), 0)
185  self.assertAlmostEqual(two_pi_complement(2*pi-epsilon), -epsilon)
186  self.assertAlmostEqual(two_pi_complement(-2*pi+epsilon), epsilon)
187  self.assertAlmostEqual(two_pi_complement(pi/2), -3*pi/2)
188  self.assertAlmostEqual(two_pi_complement(pi), -pi)
189  self.assertAlmostEqual(two_pi_complement(-pi), pi)
190  self.assertAlmostEqual(two_pi_complement(-pi/2), 3*pi/2)
191 
192  self.assertAlmostEqual(two_pi_complement(3*pi), -pi)
193  self.assertAlmostEqual(two_pi_complement(-3.0*pi), pi)
194  self.assertAlmostEqual(two_pi_complement(-5.0*pi/2.0), 3*pi/2)
195 
197  epsilon = 1e-9
198  # Straight forward full range
199  flag, min_delta, max_delta = _find_min_max_delta( 0, -pi, pi)
200  self.assertTrue(flag)
201  self.assertAlmostEqual(min_delta, -pi)
202  self.assertAlmostEqual(max_delta, pi)
203 
204  # pi/2 Full Range
205  flag, min_delta, max_delta = _find_min_max_delta( pi/2, -pi, pi)
206  self.assertTrue(flag)
207  self.assertAlmostEqual(min_delta, -3*pi/2)
208  self.assertAlmostEqual(max_delta, pi/2)
209 
210  # -pi/2 Full range
211  flag, min_delta, max_delta = _find_min_max_delta( -pi/2, -pi, pi)
212  self.assertTrue(flag)
213  self.assertAlmostEqual(min_delta, -pi/2)
214  self.assertAlmostEqual(max_delta, 3*pi/2)
215 
216  # Straight forward partial range
217  flag, min_delta, max_delta = _find_min_max_delta( 0, -pi/2, pi/2)
218  self.assertTrue(flag)
219  self.assertAlmostEqual(min_delta, -pi/2)
220  self.assertAlmostEqual(max_delta, pi/2)
221 
222  # pi/4 Partial Range
223  flag, min_delta, max_delta = _find_min_max_delta( pi/4, -pi/2, pi/2)
224  self.assertTrue(flag)
225  self.assertAlmostEqual(min_delta, -3*pi/4)
226  self.assertAlmostEqual(max_delta, pi/4)
227 
228  # -pi/4 Partial Range
229  flag, min_delta, max_delta = _find_min_max_delta( -pi/4, -pi/2, pi/2)
230  self.assertTrue(flag)
231  self.assertAlmostEqual(min_delta, -pi/4)
232  self.assertAlmostEqual(max_delta, 3*pi/4)
233 
234  # bump stop negative full range
235  flag, min_delta, max_delta = _find_min_max_delta( -pi, -pi, pi)
236  self.assertTrue(flag)
237  self.assertTrue((fabs(min_delta) <= epsilon and fabs(max_delta - 2*pi) <= epsilon) or (fabs(min_delta+2*pi) <= epsilon and fabs(max_delta) <= epsilon))
238  self.assertAlmostEqual(min_delta, 0.0)
239  self.assertAlmostEqual(max_delta, 2*pi)
240 
241  flag, min_delta, max_delta = _find_min_max_delta(-0.25,0.25,-0.25)
242  self.assertTrue(flag)
243  self.assertAlmostEqual(min_delta, -2*pi+0.5)
244  self.assertAlmostEqual(max_delta, 0.0)
245 
246  # bump stop positive full range
247  flag, min_delta, max_delta = _find_min_max_delta( pi-epsilon, -pi, pi)
248  self.assertTrue(flag)
249  #self.assertTrue((fabs(min_delta) <= epsilon and fabs(max_delta - 2*pi) <= epsilon) or (fabs(min_delta+2*pi) <= epsilon and fabs(max_delta) <= epsilon))
250  self.assertAlmostEqual(min_delta, -2*pi+epsilon)
251  self.assertAlmostEqual(max_delta, epsilon)
252 
253  # bump stop negative partial range
254  flag, min_delta, max_delta = _find_min_max_delta( -pi, -pi, pi)
255  self.assertTrue(flag)
256  self.assertAlmostEqual(min_delta, 0)
257  self.assertAlmostEqual(max_delta, 2*pi)
258 
259  # bump stop positive partial range
260  flag, min_delta, max_delta = _find_min_max_delta( -pi/2, -pi/2, pi/2)
261  self.assertTrue(flag)
262  self.assertAlmostEqual(min_delta, 0.0)
263  self.assertAlmostEqual(max_delta, pi)
264 
265  #Test out of range negative
266  flag, min_delta, max_delta = _find_min_max_delta( -pi, -pi/2, pi/2)
267  self.assertFalse(flag)
268  #Test out of range postive
269  flag, min_delta, max_delta = _find_min_max_delta( pi, -pi/2, pi/2)
270  self.assertFalse(flag)
271 
272  # pi/4 Partial Range
273  flag, min_delta, max_delta = _find_min_max_delta( 3*pi/4, pi/2, -pi/2)
274  self.assertTrue(flag)
275  self.assertAlmostEqual(min_delta, -pi/4)
276  self.assertAlmostEqual(max_delta, 3*pi/4)
277 
278 if __name__ == '__main__':
279  import rosunit
280  rosunit.unitrun('angles', 'test_python_angles', TestAngles)
static bool shortest_angular_distance_with_limits(double from, double to, double left_limit, double right_limit, double &shortest_angle)
Returns the delta from "from_angle" to "to_angle" making sure it does not violate limits specified by...
Definition: angles.h:215
def test_shortest_angular_distance(self)
Definition: utest.py:160
static double shortest_angular_distance(double from, double to)
shortest_angular_distance
Definition: angles.h:102
def test_shortestDistanceWithLimits(self)
Definition: utest.py:43
static double normalize_angle(double angle)
normalize
Definition: angles.h:81
def test_two_pi_complement(self)
Definition: utest.py:180
static double normalize_angle_positive(double angle)
normalize_angle_positive
Definition: angles.h:68
def test_normalize_angle(self)
Definition: utest.py:135
static double two_pi_complement(double angle)
returns the angle in [-2*M_PI, 2*M_PI] going the other way along the unit circle. ...
Definition: angles.h:116
def test_find_min_max_delta(self)
Definition: utest.py:196
def _find_min_max_delta(from_angle, left_limit, right_limit)
Definition: __init__.py:75
def test_normalize_angle_positive(self)
Definition: utest.py:110
A sample python unit test.
Definition: utest.py:42


angles
Author(s): John Hsu
autogenerated on Thu Jun 6 2019 19:19:27