Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update create_statefbk_iosys/lqr/dlqr errors + unit tests
  • Loading branch information
murrayrm committed Mar 16, 2022
commit ca77cca687af1e79bc05bf7c27d31f4f87da30a4
6 changes: 3 additions & 3 deletions control/statefbk.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ def lqr(*args, **kwargs):
raise ControlArgument("Integral action must pass an array")
elif integral_action.shape[1] != nstates:
raise ControlArgument(
"Integral gain output size must match system input size")
"Integral gain size must match system state size")

# Process the states to be integrated
nintegrators = integral_action.shape[0]
Expand Down Expand Up @@ -829,7 +829,7 @@ def dlqr(*args, **kwargs):
raise ControlArgument("Integral action must pass an array")
elif integral_action.shape[1] != nstates:
raise ControlArgument(
"Integral gain output size must match system input size")
"Integral gain size must match system state size")
else:
nintegrators = integral_action.shape[0]
C = integral_action
Expand Down Expand Up @@ -951,7 +951,7 @@ def create_statefbk_iosystem(
raise ControlArgument("Integral action must pass an array")
elif integral_action.shape[1] != sys.nstates:
raise ControlArgument(
"Integral gain output size must match system input size")
"Integral gain size must match system state size")
else:
nintegrators = integral_action.shape[0]
C = integral_action
Expand Down
59 changes: 59 additions & 0 deletions control/tests/statefbk_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,3 +826,62 @@ def test_lqr_integral_discrete(self):
np.testing.assert_array_almost_equal(ctrl.B, B_ctrl)
np.testing.assert_array_almost_equal(ctrl.C, C_ctrl)
np.testing.assert_array_almost_equal(ctrl.D, D_ctrl)

@pytest.mark.parametrize(
"rss_fun, lqr_fun",
[(ct.rss, lqr), (ct.drss, dlqr)])
def test_lqr_errors(self, rss_fun, lqr_fun):
# Generate a discrete time system for testing
sys = rss_fun(4, 4, 2, strictly_proper=True)

with pytest.raises(ControlArgument, match="must pass an array"):
K, _, _ = lqr_fun(
sys, np.eye(sys.nstates), np.eye(sys.ninputs),
integral_action="invalid argument")

with pytest.raises(ControlArgument, match="gain size must match"):
C_int = np.eye(2, 3)
K, _, _ = lqr_fun(
sys, np.eye(sys.nstates), np.eye(sys.ninputs),
integral_action=C_int)

with pytest.raises(TypeError, match="unrecognized keywords"):
K, _, _ = lqr_fun(
sys, np.eye(sys.nstates), np.eye(sys.ninputs),
integrator=None)

def test_statefbk_errors(self):
sys = ct.rss(4, 4, 2, strictly_proper=True)
K, _, _ = ct.lqr(sys, np.eye(sys.nstates), np.eye(sys.ninputs))

with pytest.raises(ControlArgument, match="must be I/O system"):
sys_tf = ct.tf([1], [1, 1])
ctrl, clsys = ct.create_statefbk_iosystem(sys_tf, K)

with pytest.raises(ControlArgument, match="output size must match"):
est = ct.rss(3, 3, 2)
ctrl, clsys = ct.create_statefbk_iosystem(sys, K, estimator=est)

with pytest.raises(ControlArgument, match="must be the full state"):
sys_nf = ct.rss(4, 3, 2, strictly_proper=True)
ctrl, clsys = ct.create_statefbk_iosystem(sys_nf, K)

with pytest.raises(ControlArgument, match="gain must be an array"):
ctrl, clsys = ct.create_statefbk_iosystem(sys, "bad argument")

with pytest.raises(ControlArgument, match="unknown type"):
ctrl, clsys = ct.create_statefbk_iosystem(sys, K, type=1)

# Errors involving integral action
C_int = np.eye(2, 4)
K_int, _, _ = ct.lqr(
sys, np.eye(sys.nstates + C_int.shape[0]), np.eye(sys.ninputs),
integral_action=C_int)

with pytest.raises(ControlArgument, match="must pass an array"):
ctrl, clsys = ct.create_statefbk_iosystem(
sys, K_int, integral_action="bad argument")

with pytest.raises(ControlArgument, match="must be an array of size"):
ctrl, clsys = ct.create_statefbk_iosystem(
sys, K, integral_action=C_int)