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