|
5 | 5 |
|
6 | 6 | import unittest
|
7 | 7 | import numpy as np
|
8 |
| -from control.statefbk import ctrb, obsv, place, lqr, gram |
| 8 | +from control.statefbk import ctrb, obsv, place, lqr, gram, acker |
9 | 9 | from control.matlab import *
|
10 | 10 |
|
11 | 11 | class TestStatefbk(unittest.TestCase):
|
| 12 | + """Test state feedback functions""" |
| 13 | + |
| 14 | + def setUp(self): |
| 15 | + # Maximum number of states to test + 1 |
| 16 | + self.maxStates = 5 |
| 17 | + # Maximum number of inputs and outputs to test + 1 |
| 18 | + self.maxTries = 4 |
| 19 | + # Set to True to print systems to the output. |
| 20 | + self.debug = False |
| 21 | + |
12 | 22 | def testCtrbSISO(self):
|
13 | 23 | A = np.matrix("1. 2.; 3. 4.")
|
14 | 24 | B = np.matrix("5.; 7.")
|
@@ -83,6 +93,34 @@ def testGramsys(self):
|
83 | 93 | self.assertRaises(ValueError, gram, sys, 'o')
|
84 | 94 | self.assertRaises(ValueError, gram, sys, 'c')
|
85 | 95 |
|
| 96 | + def testAcker(self): |
| 97 | + for states in range(1, self.maxStates): |
| 98 | + for i in range(self.maxTries): |
| 99 | + # start with a random SS system and transform to TF then |
| 100 | + # back to SS, check that the matrices are the same. |
| 101 | + sys = rss(states, 1, 1) |
| 102 | + if (self.debug): |
| 103 | + print sys |
| 104 | + |
| 105 | + # Make sure the system is not degenerate |
| 106 | + Cmat = ctrb(sys.A, sys.B) |
| 107 | + if (np.linalg.matrix_rank(Cmat) != states): |
| 108 | + if (self.debug): |
| 109 | + print " skipping (not reachable)" |
| 110 | + continue |
| 111 | + |
| 112 | + # Place the poles at random locations |
| 113 | + des = rss(states, 1, 1); |
| 114 | + poles = pole(des) |
| 115 | + |
| 116 | + # Now place the poles using acker |
| 117 | + K = acker(sys.A, sys.B, poles) |
| 118 | + new = ss(sys.A - sys.B * K, sys.B, sys.C, sys.D) |
| 119 | + placed = pole(new) |
| 120 | + |
| 121 | + np.testing.assert_array_almost_equal(np.sort(poles), |
| 122 | + np.sort(placed)) |
| 123 | + |
86 | 124 | def suite():
|
87 | 125 | return unittest.TestLoader().loadTestsFromTestCase(TestStatefbk)
|
88 | 126 |
|
|
0 commit comments