blob: a5bac4a9b3ab31a7381232074d85dc4386189dda [file] [log] [blame]
Austin Schuh085eab92020-11-26 13:54:51 -08001#!/usr/bin/python3
Diana Vandenberg223703d2017-01-28 17:39:53 -08002
3import polydrivetrain
4import numpy
5from numpy.testing import *
6import polytope
7import unittest
8
9__author__ = 'Austin Schuh (austin.linux@gmail.com)'
10
11
12class TestVelocityDrivetrain(unittest.TestCase):
Diana Vandenberg223703d2017-01-28 17:39:53 -080013
Ravago Jones5127ccc2022-07-31 16:32:45 -070014 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)
Diana Vandenberg223703d2017-01-28 17:39:53 -080018
Ravago Jones5127ccc2022-07-31 16:32:45 -070019 def test_coerce_inside(self):
20 """Tests coercion when the point is inside the box."""
21 box = self.MakeBox(1, 2, 1, 2)
Diana Vandenberg223703d2017-01-28 17:39:53 -080022
Ravago Jones5127ccc2022-07-31 16:32:45 -070023 # x1 = x2
24 K = numpy.matrix([[1, -1]])
25 w = 0
Diana Vandenberg223703d2017-01-28 17:39:53 -080026
Ravago Jones5127ccc2022-07-31 16:32:45 -070027 assert_array_equal(
28 polydrivetrain.CoerceGoal(box, K, w, numpy.matrix([[1.5], [1.5]])),
29 numpy.matrix([[1.5], [1.5]]))
Diana Vandenberg223703d2017-01-28 17:39:53 -080030
Ravago Jones5127ccc2022-07-31 16:32:45 -070031 def test_coerce_outside_intersect(self):
32 """Tests coercion when the line intersects the box."""
33 box = self.MakeBox(1, 2, 1, 2)
Diana Vandenberg223703d2017-01-28 17:39:53 -080034
Ravago Jones5127ccc2022-07-31 16:32:45 -070035 # x1 = x2
36 K = numpy.matrix([[1, -1]])
37 w = 0
Diana Vandenberg223703d2017-01-28 17:39:53 -080038
Ravago Jones5127ccc2022-07-31 16:32:45 -070039 assert_array_equal(
40 polydrivetrain.CoerceGoal(box, K, w, numpy.matrix([[5], [5]])),
41 numpy.matrix([[2.0], [2.0]]))
Diana Vandenberg223703d2017-01-28 17:39:53 -080042
Ravago Jones5127ccc2022-07-31 16:32:45 -070043 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)
Diana Vandenberg223703d2017-01-28 17:39:53 -080046
Ravago Jones5127ccc2022-07-31 16:32:45 -070047 # x1 = x2
48 K = numpy.matrix([[1, -1]])
49 w = 0
Diana Vandenberg223703d2017-01-28 17:39:53 -080050
Ravago Jones5127ccc2022-07-31 16:32:45 -070051 assert_array_equal(
52 polydrivetrain.CoerceGoal(box, K, w, numpy.matrix([[5], [5]])),
53 numpy.matrix([[3.0], [2.0]]))
Diana Vandenberg223703d2017-01-28 17:39:53 -080054
Ravago Jones5127ccc2022-07-31 16:32:45 -070055 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)
Diana Vandenberg223703d2017-01-28 17:39:53 -080058
Ravago Jones5127ccc2022-07-31 16:32:45 -070059 # x1 = x2
60 K = numpy.matrix([[-1, 1]])
61 w = 0
Diana Vandenberg223703d2017-01-28 17:39:53 -080062
Ravago Jones5127ccc2022-07-31 16:32:45 -070063 assert_array_equal(
64 polydrivetrain.CoerceGoal(box, K, w, numpy.matrix([[5], [5]])),
65 numpy.matrix([[2.0], [2.0]]))
Diana Vandenberg223703d2017-01-28 17:39:53 -080066
Ravago Jones5127ccc2022-07-31 16:32:45 -070067 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)
Diana Vandenberg223703d2017-01-28 17:39:53 -080070
Ravago Jones5127ccc2022-07-31 16:32:45 -070071 # 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]]))
Diana Vandenberg223703d2017-01-28 17:39:53 -080078
79
80if __name__ == '__main__':
Ravago Jones5127ccc2022-07-31 16:32:45 -070081 unittest.main()