mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
[wpimath] Clean up Eigen usage
* Replace Matrix<> with Vector<> where vectors are explicitly intended. I found these via `rg "Eigen::Matrix<double, \w+, 1>"`. * Pass all Eigen matrices by const reference. I found these via `rg "\(Eigen"` on main (the initializer list constructors make more false positives). * Replace MakeMatrix() and operator<< usage with initializer list constructors. I found these via `rg MakeMatrix` and `rg "<<"` respectively. * Deprecate MakeMatrix()
This commit is contained in:
committed by
Peter Johnson
parent
72716f51ce
commit
9359431bad
@@ -7,61 +7,46 @@
|
||||
#include <cmath>
|
||||
|
||||
#include "Eigen/Core"
|
||||
#include "frc/StateSpaceUtil.h"
|
||||
#include "frc/controller/ControlAffinePlantInversionFeedforward.h"
|
||||
#include "units/time.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
Eigen::Matrix<double, 2, 1> Dynamics(const Eigen::Matrix<double, 2, 1>& x,
|
||||
const Eigen::Matrix<double, 1, 1>& u) {
|
||||
Eigen::Matrix<double, 2, 1> result;
|
||||
|
||||
result = (frc::MakeMatrix<2, 2>(1.0, 0.0, 0.0, 1.0) * x) +
|
||||
(frc::MakeMatrix<2, 1>(0.0, 1.0) * u);
|
||||
|
||||
return result;
|
||||
Eigen::Vector<double, 2> Dynamics(const Eigen::Vector<double, 2>& x,
|
||||
const Eigen::Vector<double, 1>& u) {
|
||||
return Eigen::Matrix<double, 2, 2>{{1.0, 0.0}, {0.0, 1.0}} * x +
|
||||
Eigen::Matrix<double, 2, 1>{0.0, 1.0} * u;
|
||||
}
|
||||
|
||||
Eigen::Matrix<double, 2, 1> StateDynamics(
|
||||
const Eigen::Matrix<double, 2, 1>& x) {
|
||||
Eigen::Matrix<double, 2, 1> result;
|
||||
|
||||
result = (frc::MakeMatrix<2, 2>(1.0, 0.0, 0.0, 1.0) * x);
|
||||
|
||||
return result;
|
||||
Eigen::Vector<double, 2> StateDynamics(const Eigen::Vector<double, 2>& x) {
|
||||
return Eigen::Matrix<double, 2, 2>{{1.0, 0.0}, {0.0, 1.0}} * x;
|
||||
}
|
||||
|
||||
TEST(ControlAffinePlantInversionFeedforwardTest, Calculate) {
|
||||
std::function<Eigen::Matrix<double, 2, 1>(const Eigen::Matrix<double, 2, 1>&,
|
||||
const Eigen::Matrix<double, 1, 1>&)>
|
||||
std::function<Eigen::Vector<double, 2>(const Eigen::Vector<double, 2>&,
|
||||
const Eigen::Vector<double, 1>&)>
|
||||
modelDynamics = [](auto& x, auto& u) { return Dynamics(x, u); };
|
||||
|
||||
frc::ControlAffinePlantInversionFeedforward<2, 1> feedforward{
|
||||
modelDynamics, units::second_t(0.02)};
|
||||
modelDynamics, units::second_t{0.02}};
|
||||
|
||||
Eigen::Matrix<double, 2, 1> r;
|
||||
r << 2, 2;
|
||||
Eigen::Matrix<double, 2, 1> nextR;
|
||||
nextR << 3, 3;
|
||||
Eigen::Vector<double, 2> r{2, 2};
|
||||
Eigen::Vector<double, 2> nextR{3, 3};
|
||||
|
||||
EXPECT_NEAR(48, feedforward.Calculate(r, nextR)(0, 0), 1e-6);
|
||||
}
|
||||
|
||||
TEST(ControlAffinePlantInversionFeedforwardTest, CalculateState) {
|
||||
std::function<Eigen::Matrix<double, 2, 1>(const Eigen::Matrix<double, 2, 1>&)>
|
||||
std::function<Eigen::Vector<double, 2>(const Eigen::Vector<double, 2>&)>
|
||||
modelDynamics = [](auto& x) { return StateDynamics(x); };
|
||||
|
||||
Eigen::Matrix<double, 2, 1> B;
|
||||
B << 0, 1;
|
||||
Eigen::Matrix<double, 2, 1> B{0, 1};
|
||||
|
||||
frc::ControlAffinePlantInversionFeedforward<2, 1> feedforward{
|
||||
modelDynamics, B, units::second_t(0.02)};
|
||||
|
||||
Eigen::Matrix<double, 2, 1> r;
|
||||
r << 2, 2;
|
||||
Eigen::Matrix<double, 2, 1> nextR;
|
||||
nextR << 3, 3;
|
||||
Eigen::Vector<double, 2> r{2, 2};
|
||||
Eigen::Vector<double, 2> nextR{3, 3};
|
||||
|
||||
EXPECT_NEAR(48, feedforward.Calculate(r, nextR)(0, 0), 1e-6);
|
||||
}
|
||||
|
||||
@@ -13,19 +13,14 @@
|
||||
namespace frc {
|
||||
|
||||
TEST(LinearPlantInversionFeedforwardTest, Calculate) {
|
||||
Eigen::Matrix<double, 2, 2> A;
|
||||
A << 1, 0, 0, 1;
|
||||
|
||||
Eigen::Matrix<double, 2, 1> B;
|
||||
B << 0, 1;
|
||||
Eigen::Matrix<double, 2, 2> A{{1, 0}, {0, 1}};
|
||||
Eigen::Matrix<double, 2, 1> B{0, 1};
|
||||
|
||||
frc::LinearPlantInversionFeedforward<2, 1> feedforward{A, B,
|
||||
units::second_t(0.02)};
|
||||
|
||||
Eigen::Matrix<double, 2, 1> r;
|
||||
r << 2, 2;
|
||||
Eigen::Matrix<double, 2, 1> nextR;
|
||||
nextR << 3, 3;
|
||||
Eigen::Vector<double, 2> r{2, 2};
|
||||
Eigen::Vector<double, 2> nextR{3, 3};
|
||||
|
||||
EXPECT_NEAR(47.502599, feedforward.Calculate(r, nextR)(0, 0), 0.002);
|
||||
}
|
||||
|
||||
@@ -21,21 +21,16 @@ TEST(SimpleMotorFeedforwardTest, Calculate) {
|
||||
double Ka = 0.6;
|
||||
auto dt = 0.02_s;
|
||||
|
||||
Eigen::Matrix<double, 1, 1> A;
|
||||
A << -Kv / Ka;
|
||||
|
||||
Eigen::Matrix<double, 1, 1> B;
|
||||
B << 1.0 / Ka;
|
||||
Eigen::Matrix<double, 1, 1> A{-Kv / Ka};
|
||||
Eigen::Matrix<double, 1, 1> B{1.0 / Ka};
|
||||
|
||||
frc::LinearPlantInversionFeedforward<1, 1> plantInversion{A, B, dt};
|
||||
frc::SimpleMotorFeedforward<units::meter> simpleMotor{
|
||||
units::volt_t{Ks}, units::volt_t{Kv} / 1_mps,
|
||||
units::volt_t{Ka} / 1_mps_sq};
|
||||
|
||||
Eigen::Matrix<double, 1, 1> r;
|
||||
r << 2;
|
||||
Eigen::Matrix<double, 1, 1> nextR;
|
||||
nextR << 3;
|
||||
Eigen::Vector<double, 1> r{2.0};
|
||||
Eigen::Vector<double, 1> nextR{3.0};
|
||||
|
||||
EXPECT_NEAR(37.524995834325161 + Ks,
|
||||
simpleMotor.Calculate(2_mps, 3_mps, dt).to<double>(), 0.002);
|
||||
|
||||
Reference in New Issue
Block a user