Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
99 changes: 99 additions & 0 deletions numerical/nonlinear_control/test/TestBacksteppingControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,102 @@ TEST_F(TestBacksteppingRealPlantOrder2, stabilizes_double_integrator)
EXPECT_NEAR(x[0], 0.0f, 1.0e-2f);
EXPECT_NEAR(x[1], 0.0f, 1.0e-2f);
}

TEST_F(TestBacksteppingRealPlantOrder1, reset_restores_initial_response_order1)
{
std::array<float, 1> x{ { 0.8f } };
nonlinear_control::BacksteppingControl<float, 1>::Reference ref{ 0.0f, 0.0f };

const float first = controller.ComputeControl(x, ref);

std::array<float, 1> xOther{ { 2.0f } };
controller.ComputeControl(xOther, ref);
controller.Reset();

const float afterReset = controller.ComputeControl(x, ref);

EXPECT_NEAR(afterReset, first, math::Tolerance<float>());
}

TEST_F(TestBacksteppingControlOrder1, zero_error_output_equals_negative_drift_over_gain)
{
const float d{ 1.2f };
const float g{ 2.0f };
std::array<float, 1> x{ { 0.5f } };
nonlinear_control::BacksteppingControl<float, 1>::Reference ref{ 0.5f, 0.0f };

EXPECT_CALL(model, Drift(0, x)).WillOnce(::testing::Return(d));
EXPECT_CALL(model, Gain(0, x)).WillOnce(::testing::Return(g));

const float u = controller.ComputeControl(x, ref);

EXPECT_NEAR(u, -d / g, math::Tolerance<float>());
}

TEST_F(TestBacksteppingControlOrder1, drift_and_error_combined_output)
{
const float e{ 0.6f };
const float d{ 1.0f };
const float g{ 2.5f };
std::array<float, 1> x{ { e } };
nonlinear_control::BacksteppingControl<float, 1>::Reference ref{ 0.0f, 0.0f };

EXPECT_CALL(model, Drift(0, x)).WillOnce(::testing::Return(d));
EXPECT_CALL(model, Gain(0, x)).WillOnce(::testing::Return(g));

const float u = controller.ComputeControl(x, ref);

EXPECT_NEAR(u, (-d - gains[0] * e) / g, math::Tolerance<float>());
}

TEST_F(TestBacksteppingControlOrder1, two_instances_do_not_share_state)
{
::testing::StrictMock<MockStrictFeedbackModel<float, 1>> modelB;
std::array<float, 1> gainsB{ { 2.0f } };
nonlinear_control::BacksteppingControl<float, 1> ctrlB{ modelB, gainsB };

std::array<float, 1> xA{ { 1.0f } };
std::array<float, 1> xB{ { 1.0f } };
nonlinear_control::BacksteppingControl<float, 1>::Reference ref{ 0.0f, 0.0f };

EXPECT_CALL(model, Drift(0, xA)).WillOnce(::testing::Return(0.0f));
EXPECT_CALL(model, Gain(0, xA)).WillOnce(::testing::Return(1.0f));
EXPECT_CALL(modelB, Drift(0, xB)).WillOnce(::testing::Return(0.0f));
EXPECT_CALL(modelB, Gain(0, xB)).WillOnce(::testing::Return(1.0f));

const float uA = controller.ComputeControl(xA, ref);
const float uB = ctrlB.ComputeControl(xB, ref);

EXPECT_FLOAT_EQ(uA, uB);
}

TEST_F(TestBacksteppingControlOrder1, large_state_produces_finite_output)
{
const float large{ 1.0e6f };
std::array<float, 1> x{ { large } };
nonlinear_control::BacksteppingControl<float, 1>::Reference ref{ 0.0f, 0.0f };

EXPECT_CALL(model, Drift(0, x)).WillOnce(::testing::Return(0.0f));
EXPECT_CALL(model, Gain(0, x)).WillOnce(::testing::Return(1.0f));

const float u = controller.ComputeControl(x, ref);

EXPECT_TRUE(std::isfinite(u));
EXPECT_NEAR(u, -gains[0] * large, 1.0f);
}

TEST_F(TestBacksteppingControlOrder1, determinism_same_input_same_output)
{
std::array<float, 1> x{ { 0.7f } };
nonlinear_control::BacksteppingControl<float, 1>::Reference ref{ 0.2f, 0.1f };

EXPECT_CALL(model, Drift(0, x)).Times(2).WillRepeatedly(::testing::Return(0.3f));
EXPECT_CALL(model, Gain(0, x)).Times(2).WillRepeatedly(::testing::Return(1.5f));

controller.Reset();
const float u1 = controller.ComputeControl(x, ref);
controller.Reset();
const float u2 = controller.ComputeControl(x, ref);

EXPECT_FLOAT_EQ(u1, u2);
}
60 changes: 53 additions & 7 deletions numerical/nonlinear_control/test/TestFeedbackLinearization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,65 @@ TEST_F(TestFeedbackLinearization, closed_loop_error_decays)
EXPECT_LT(finalNorm, prevErrorNorm);
}

TEST_F(TestFeedbackLinearization, reference_feedforward_used)
TEST_F(TestFeedbackLinearization, superposition_all_terms_active)
{
const math::SquareMatrix<float, 2> identity{ math::SquareMatrix<float, 2>::Identity() };
const math::SquareMatrix<float, 2> B{ 2.0f, 1.0f, 0.5f, 3.0f };
const math::Vector<float, 2> drift{ { 4.0f }, { -1.0f } };
const math::Vector<float, 2> x{ { 1.0f }, { -1.0f } };
const math::Vector<float, 2> xDot{ { 0.5f }, { 0.25f } };
const math::Vector<float, 2> yd{ { 3.0f }, { 1.0f } };
const math::Vector<float, 2> ydDot{ { 1.5f }, { -0.5f } };
const math::Vector<float, 2> ydDdot{ { 0.2f }, { -0.3f } };

EXPECT_CALL(model, DecouplingMatrixAt(::testing::_)).WillOnce(::testing::Return(B));
EXPECT_CALL(model, DriftTerm(::testing::_)).WillOnce(::testing::Return(drift));

const auto u{ controller.ComputeInput(x, xDot, yd, ydDot, ydDdot) };

const math::Vector<float, 2> e{ yd - x };
const math::Vector<float, 2> eDot{ ydDot - xDot };
const math::Vector<float, 2> v{ ydDdot + kd * eDot + kp * e };
const math::Vector<float, 2> expected{ B * v + drift };

EXPECT_NEAR(u.at(0, 0), expected.at(0, 0), math::Tolerance<float>());
EXPECT_NEAR(u.at(1, 0), expected.at(1, 0), math::Tolerance<float>());
}

TEST_F(TestFeedbackLinearization, off_diagonal_decoupling_matrix_couples_axes)
{
const math::SquareMatrix<float, 2> B{ 0.0f, 1.0f, 1.0f, 0.0f };
const math::Vector<float, 2> a{ { 0.0f }, { 0.0f } };
const math::Vector<float, 2> c{ { 7.0f }, { -4.0f } };
const math::Vector<float, 2> v{ { 5.0f }, { 7.0f } };

EXPECT_CALL(model, DecouplingMatrixAt(::testing::_)).WillOnce(::testing::Return(identity));
EXPECT_CALL(model, DecouplingMatrixAt(::testing::_)).WillOnce(::testing::Return(B));
EXPECT_CALL(model, DriftTerm(::testing::_)).WillOnce(::testing::Return(a));

const auto u{ controller.ComputeInput(zero, zero, zero, zero, c) };
const auto u{ controller.ComputeInput(zero, zero, zero, zero, v) };

EXPECT_NEAR(u.at(0, 0), c.at(0, 0), math::Tolerance<float>());
EXPECT_NEAR(u.at(1, 0), c.at(1, 0), math::Tolerance<float>());
EXPECT_NEAR(u.at(0, 0), 7.0f, math::Tolerance<float>());
EXPECT_NEAR(u.at(1, 0), 5.0f, math::Tolerance<float>());
}

TEST_F(TestFeedbackLinearization, model_queried_with_exact_state)
{
const math::SquareMatrix<float, 2> identity{ math::SquareMatrix<float, 2>::Identity() };
const math::Vector<float, 2> a{ { 0.0f }, { 0.0f } };
const math::Vector<float, 2> xQuery{ { 2.5f }, { -3.1f } };

math::Vector<float, 2> capturedB{};
math::Vector<float, 2> capturedA{};

EXPECT_CALL(model, DecouplingMatrixAt(::testing::_))
.WillOnce(::testing::DoAll(::testing::SaveArg<0>(&capturedB), ::testing::Return(identity)));
EXPECT_CALL(model, DriftTerm(::testing::_))
.WillOnce(::testing::DoAll(::testing::SaveArg<0>(&capturedA), ::testing::Return(a)));

controller.ComputeInput(xQuery, zero, zero, zero, zero);

EXPECT_FLOAT_EQ(capturedB.at(0, 0), xQuery.at(0, 0));
EXPECT_FLOAT_EQ(capturedB.at(1, 0), xQuery.at(1, 0));
EXPECT_FLOAT_EQ(capturedA.at(0, 0), xQuery.at(0, 0));
EXPECT_FLOAT_EQ(capturedA.at(1, 0), xQuery.at(1, 0));
}

namespace
Expand Down
Loading
Loading