|
1 | | -// Copyright (c) 2024, Numerical Toolbox Contributors. All rights reserved. |
2 | 1 | #include "numerical/filters/passive/BiquadCascade.hpp" |
3 | 2 | #include "numerical/math/LinearTimeInvariant.hpp" |
4 | 3 | #include "numerical/math/Matrix.hpp" |
@@ -26,6 +25,18 @@ namespace |
26 | 25 | return plant; |
27 | 26 | } |
28 | 27 |
|
| 28 | + math::LinearTimeInvariant<float, 2, 2, 2> MakeTwoChannelPlant() |
| 29 | + { |
| 30 | + math::LinearTimeInvariant<float, 2, 2, 2> plant{}; |
| 31 | + plant.A.at(0, 0) = kPlantA; |
| 32 | + plant.A.at(1, 1) = kPlantA; |
| 33 | + plant.B.at(0, 0) = kPlantB; |
| 34 | + plant.B.at(1, 1) = kPlantB; |
| 35 | + plant.C.at(0, 0) = kPlantC; |
| 36 | + plant.C.at(1, 1) = kPlantC; |
| 37 | + return plant; |
| 38 | + } |
| 39 | + |
29 | 40 | filters::passive::BiquadCoeffs<float> MakeLowPassQ() |
30 | 41 | { |
31 | 42 | return filters::passive::Biquad<float>::LowPass(kCutoffHz, kSampleRateHz, kQ); |
@@ -241,3 +252,147 @@ TEST_F(TestDisturbanceObserver, robust_to_small_model_mismatch) |
241 | 252 |
|
242 | 253 | EXPECT_LT(std::abs(dob.Disturbance().at(0, 0) - kDisturbance), kDisturbance); |
243 | 254 | } |
| 255 | + |
| 256 | +TEST_F(TestDisturbanceObserver, zero_disturbance_passthrough) |
| 257 | +{ |
| 258 | + static constexpr int kSteps{ 4000 }; |
| 259 | + |
| 260 | + math::Vector<float, 1> x{}; |
| 261 | + math::Vector<float, 1> u{}; |
| 262 | + |
| 263 | + math::Vector<float, 1> c{}; |
| 264 | + c.at(0, 0) = 0.7f; |
| 265 | + |
| 266 | + for (int k{ 0 }; k < kSteps; ++k) |
| 267 | + { |
| 268 | + const math::Vector<float, 1> y{ nominal.Output(x, u) }; |
| 269 | + u = dob.Compute(c, y); |
| 270 | + x = nominal.Step(x, u); |
| 271 | + } |
| 272 | + |
| 273 | + EXPECT_NEAR(u.at(0, 0), c.at(0, 0), 1e-2f); |
| 274 | +} |
| 275 | + |
| 276 | +TEST_F(TestDisturbanceObserver, initial_disturbance_is_zero) |
| 277 | +{ |
| 278 | + EXPECT_NEAR(dob.Disturbance().at(0, 0), 0.0f, math::Tolerance<float>()); |
| 279 | +} |
| 280 | + |
| 281 | +TEST_F(TestDisturbanceObserver, reset_then_reconverge) |
| 282 | +{ |
| 283 | + static constexpr float kDisturbance{ 0.5f }; |
| 284 | + static constexpr int kWarmup{ 4000 }; |
| 285 | + |
| 286 | + math::Vector<float, 1> x{}; |
| 287 | + math::Vector<float, 1> c{}; |
| 288 | + math::Vector<float, 1> u{}; |
| 289 | + |
| 290 | + for (int k{ 0 }; k < kWarmup; ++k) |
| 291 | + { |
| 292 | + const math::Vector<float, 1> y{ nominal.Output(x, u) }; |
| 293 | + u = dob.Compute(c, y); |
| 294 | + math::Vector<float, 1> uActual{}; |
| 295 | + uActual.at(0, 0) = u.at(0, 0) + kDisturbance; |
| 296 | + x = nominal.Step(x, uActual); |
| 297 | + } |
| 298 | + |
| 299 | + dob.Reset(); |
| 300 | + x = math::Vector<float, 1>{}; |
| 301 | + u = math::Vector<float, 1>{}; |
| 302 | + |
| 303 | + for (int k{ 0 }; k < kWarmup; ++k) |
| 304 | + { |
| 305 | + const math::Vector<float, 1> y{ nominal.Output(x, u) }; |
| 306 | + u = dob.Compute(c, y); |
| 307 | + math::Vector<float, 1> uActual{}; |
| 308 | + uActual.at(0, 0) = u.at(0, 0) + kDisturbance; |
| 309 | + x = nominal.Step(x, uActual); |
| 310 | + } |
| 311 | + |
| 312 | + EXPECT_NEAR(dob.Disturbance().at(0, 0), kDisturbance, 1e-2f); |
| 313 | +} |
| 314 | + |
| 315 | +TEST_F(TestDisturbanceObserver, determinism_two_instances_agree) |
| 316 | +{ |
| 317 | + static constexpr float kDisturbance{ 0.3f }; |
| 318 | + static constexpr int kSteps{ 2000 }; |
| 319 | + |
| 320 | + robust_control::DisturbanceObserver<float, 1, 1, 1> dob2{ nominal, qCoeffs }; |
| 321 | + |
| 322 | + math::Vector<float, 1> x1{}; |
| 323 | + math::Vector<float, 1> x2{}; |
| 324 | + math::Vector<float, 1> c{}; |
| 325 | + math::Vector<float, 1> u1{}; |
| 326 | + math::Vector<float, 1> u2{}; |
| 327 | + |
| 328 | + for (int k{ 0 }; k < kSteps; ++k) |
| 329 | + { |
| 330 | + const math::Vector<float, 1> y1{ nominal.Output(x1, u1) }; |
| 331 | + const math::Vector<float, 1> y2{ nominal.Output(x2, u2) }; |
| 332 | + u1 = dob.Compute(c, y1); |
| 333 | + u2 = dob2.Compute(c, y2); |
| 334 | + math::Vector<float, 1> ua1{}; |
| 335 | + math::Vector<float, 1> ua2{}; |
| 336 | + ua1.at(0, 0) = u1.at(0, 0) + kDisturbance; |
| 337 | + ua2.at(0, 0) = u2.at(0, 0) + kDisturbance; |
| 338 | + x1 = nominal.Step(x1, ua1); |
| 339 | + x2 = nominal.Step(x2, ua2); |
| 340 | + } |
| 341 | + |
| 342 | + EXPECT_FLOAT_EQ(dob.Disturbance().at(0, 0), dob2.Disturbance().at(0, 0)); |
| 343 | +} |
| 344 | + |
| 345 | +TEST_F(TestDisturbanceObserver, two_channel_independent_disturbance_estimation) |
| 346 | +{ |
| 347 | + static constexpr float kDist0{ 0.4f }; |
| 348 | + static constexpr float kDist1{ 0.8f }; |
| 349 | + static constexpr int kSteps{ 4000 }; |
| 350 | + |
| 351 | + const math::LinearTimeInvariant<float, 2, 2, 2> plant2ch{ MakeTwoChannelPlant() }; |
| 352 | + filters::passive::BiquadCoeffs<float> q2{ MakeLowPassQ() }; |
| 353 | + robust_control::DisturbanceObserver<float, 2, 2, 2> dob2ch{ plant2ch, q2 }; |
| 354 | + |
| 355 | + math::Vector<float, 2> x{}; |
| 356 | + math::Vector<float, 2> c{}; |
| 357 | + math::Vector<float, 2> u{}; |
| 358 | + |
| 359 | + for (int k{ 0 }; k < kSteps; ++k) |
| 360 | + { |
| 361 | + const math::Vector<float, 2> y{ plant2ch.Output(x, u) }; |
| 362 | + u = dob2ch.Compute(c, y); |
| 363 | + math::Vector<float, 2> uActual{}; |
| 364 | + uActual.at(0, 0) = u.at(0, 0) + kDist0; |
| 365 | + uActual.at(1, 0) = u.at(1, 0) + kDist1; |
| 366 | + x = plant2ch.Step(x, uActual); |
| 367 | + } |
| 368 | + |
| 369 | + EXPECT_NEAR(dob2ch.Disturbance().at(0, 0), kDist0, 1e-2f); |
| 370 | + EXPECT_NEAR(dob2ch.Disturbance().at(1, 0), kDist1, 1e-2f); |
| 371 | +} |
| 372 | + |
| 373 | +TEST_F(TestDisturbanceObserver, step_disturbance_transient_crosses_half_value) |
| 374 | +{ |
| 375 | + static constexpr float kDisturbance{ 1.0f }; |
| 376 | + static constexpr int kMaxTransientSteps{ 500 }; |
| 377 | + |
| 378 | + math::Vector<float, 1> x{}; |
| 379 | + math::Vector<float, 1> c{}; |
| 380 | + math::Vector<float, 1> u{}; |
| 381 | + |
| 382 | + bool crossedHalf{ false }; |
| 383 | + for (int k{ 0 }; k < kMaxTransientSteps; ++k) |
| 384 | + { |
| 385 | + const math::Vector<float, 1> y{ nominal.Output(x, u) }; |
| 386 | + u = dob.Compute(c, y); |
| 387 | + math::Vector<float, 1> uActual{}; |
| 388 | + uActual.at(0, 0) = u.at(0, 0) + kDisturbance; |
| 389 | + x = nominal.Step(x, uActual); |
| 390 | + if (dob.Disturbance().at(0, 0) >= 0.5f * kDisturbance) |
| 391 | + { |
| 392 | + crossedHalf = true; |
| 393 | + break; |
| 394 | + } |
| 395 | + } |
| 396 | + |
| 397 | + EXPECT_TRUE(crossedHalf); |
| 398 | +} |
0 commit comments