Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 2 | |
| 3 | import polydrivetrain |
| 4 | import numpy |
| 5 | from numpy.testing import * |
| 6 | import polytope |
| 7 | import unittest |
| 8 | |
| 9 | __author__ = 'Austin Schuh (austin.linux@gmail.com)' |
| 10 | |
| 11 | |
| 12 | class TestVelocityDrivetrain(unittest.TestCase): |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 13 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 14 | def MakeBox(self, x1_min, x1_max, x2_min, x2_max): |
| 15 | H = numpy.matrix([[1, 0], [-1, 0], [0, 1], [0, -1]]) |
| 16 | K = numpy.matrix([[x1_max], [-x1_min], [x2_max], [-x2_min]]) |
| 17 | return polytope.HPolytope(H, K) |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 18 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 19 | def test_coerce_inside(self): |
| 20 | """Tests coercion when the point is inside the box.""" |
| 21 | box = self.MakeBox(1, 2, 1, 2) |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 22 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 23 | # x1 = x2 |
| 24 | K = numpy.matrix([[1, -1]]) |
| 25 | w = 0 |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 26 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 27 | assert_array_equal( |
| 28 | polydrivetrain.CoerceGoal(box, K, w, numpy.matrix([[1.5], [1.5]])), |
| 29 | numpy.matrix([[1.5], [1.5]])) |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 30 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 31 | def test_coerce_outside_intersect(self): |
| 32 | """Tests coercion when the line intersects the box.""" |
| 33 | box = self.MakeBox(1, 2, 1, 2) |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 34 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 35 | # x1 = x2 |
| 36 | K = numpy.matrix([[1, -1]]) |
| 37 | w = 0 |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 38 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 39 | assert_array_equal( |
| 40 | polydrivetrain.CoerceGoal(box, K, w, numpy.matrix([[5], [5]])), |
| 41 | numpy.matrix([[2.0], [2.0]])) |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 42 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 43 | def test_coerce_outside_no_intersect(self): |
| 44 | """Tests coercion when the line does not intersect the box.""" |
| 45 | box = self.MakeBox(3, 4, 1, 2) |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 46 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 47 | # x1 = x2 |
| 48 | K = numpy.matrix([[1, -1]]) |
| 49 | w = 0 |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 50 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 51 | assert_array_equal( |
| 52 | polydrivetrain.CoerceGoal(box, K, w, numpy.matrix([[5], [5]])), |
| 53 | numpy.matrix([[3.0], [2.0]])) |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 54 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 55 | def test_coerce_middle_of_edge(self): |
| 56 | """Tests coercion when the line intersects the middle of an edge.""" |
| 57 | box = self.MakeBox(0, 4, 1, 2) |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 58 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 59 | # x1 = x2 |
| 60 | K = numpy.matrix([[-1, 1]]) |
| 61 | w = 0 |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 62 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 63 | assert_array_equal( |
| 64 | polydrivetrain.CoerceGoal(box, K, w, numpy.matrix([[5], [5]])), |
| 65 | numpy.matrix([[2.0], [2.0]])) |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 66 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 67 | def test_coerce_perpendicular_line(self): |
| 68 | """Tests coercion when the line does not intersect and is in quadrant 2.""" |
| 69 | box = self.MakeBox(1, 2, 1, 2) |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 70 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 71 | # x1 = -x2 |
| 72 | K = numpy.matrix([[1, 1]]) |
| 73 | w = 0 |
| 74 | |
| 75 | assert_array_equal( |
| 76 | polydrivetrain.CoerceGoal(box, K, w, numpy.matrix([[5], [5]])), |
| 77 | numpy.matrix([[1.0], [1.0]])) |
Comran Morshed | 9a9948c | 2016-01-16 15:58:04 +0000 | [diff] [blame] | 78 | |
| 79 | |
| 80 | if __name__ == '__main__': |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 81 | unittest.main() |