2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2020-08-14 23:40:33 -07:00
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
#include <random>
|
|
|
|
|
|
2023-08-28 15:13:34 -07:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/math/controller/LinearPlantInversionFeedforward.hpp"
|
|
|
|
|
#include "wpi/math/controller/LinearQuadraticRegulator.hpp"
|
|
|
|
|
#include "wpi/math/estimator/KalmanFilter.hpp"
|
2025-11-07 19:57:55 -05:00
|
|
|
#include "wpi/math/linalg/EigenCore.hpp"
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/math/system/LinearSystem.hpp"
|
|
|
|
|
#include "wpi/math/system/LinearSystemLoop.hpp"
|
|
|
|
|
#include "wpi/math/system/plant/DCMotor.hpp"
|
|
|
|
|
#include "wpi/math/system/plant/LinearSystemId.hpp"
|
|
|
|
|
#include "wpi/units/time.hpp"
|
2020-08-14 23:40:33 -07:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
namespace wpi::math {
|
2020-08-14 23:40:33 -07:00
|
|
|
|
|
|
|
|
constexpr double kPositionStddev = 0.0001;
|
|
|
|
|
constexpr auto kDt = 0.00505_s;
|
|
|
|
|
|
2021-09-21 06:12:50 -07:00
|
|
|
class StateSpaceTest : public testing::Test {
|
2020-08-14 23:40:33 -07:00
|
|
|
public:
|
|
|
|
|
LinearSystem<2, 1, 1> plant = [] {
|
|
|
|
|
auto motors = DCMotor::Vex775Pro(2);
|
|
|
|
|
|
|
|
|
|
// Carriage mass
|
|
|
|
|
constexpr auto m = 5_kg;
|
|
|
|
|
|
|
|
|
|
// Radius of pulley
|
|
|
|
|
constexpr auto r = 0.0181864_m;
|
|
|
|
|
|
|
|
|
|
// Gear ratio
|
|
|
|
|
constexpr double G = 40.0 / 40.0;
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
return wpi::math::LinearSystemId::ElevatorSystem(motors, m, r, G).Slice(0);
|
2020-08-14 23:40:33 -07:00
|
|
|
}();
|
|
|
|
|
LinearQuadraticRegulator<2, 1> controller{plant, {0.02, 0.4}, {12.0}, kDt};
|
|
|
|
|
KalmanFilter<2, 1, 1> observer{plant, {0.05, 1.0}, {0.0001}, kDt};
|
2020-10-02 17:37:26 -07:00
|
|
|
LinearSystemLoop<2, 1, 1> loop{plant, controller, observer, 12_V, kDt};
|
2020-08-14 23:40:33 -07:00
|
|
|
};
|
|
|
|
|
|
2020-12-28 10:35:51 -08:00
|
|
|
void Update(const LinearSystem<2, 1, 1>& plant, LinearSystemLoop<2, 1, 1>& loop,
|
|
|
|
|
double noise) {
|
2022-04-29 22:29:20 -07:00
|
|
|
Vectord<1> y = plant.CalculateY(loop.Xhat(), loop.U()) + Vectord<1>{noise};
|
2020-08-14 23:40:33 -07:00
|
|
|
loop.Correct(y);
|
|
|
|
|
loop.Predict(kDt);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-21 06:12:50 -07:00
|
|
|
TEST_F(StateSpaceTest, CorrectPredictLoop) {
|
2020-08-14 23:40:33 -07:00
|
|
|
std::default_random_engine generator;
|
|
|
|
|
std::normal_distribution<double> dist{0.0, kPositionStddev};
|
|
|
|
|
|
2022-04-29 22:29:20 -07:00
|
|
|
Vectord<2> references{2.0, 0.0};
|
2020-08-14 23:40:33 -07:00
|
|
|
loop.SetNextR(references);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 1000; i++) {
|
2020-12-28 10:35:51 -08:00
|
|
|
Update(plant, loop, dist(generator));
|
2020-08-14 23:40:33 -07:00
|
|
|
EXPECT_PRED_FORMAT2(testing::DoubleLE, -12.0, loop.U(0));
|
|
|
|
|
EXPECT_PRED_FORMAT2(testing::DoubleLE, loop.U(0), 12.0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EXPECT_NEAR(loop.Xhat(0), 2.0, 0.05);
|
|
|
|
|
EXPECT_NEAR(loop.Xhat(1), 0.0, 0.5);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
} // namespace wpi::math
|