[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:
Tyler Veness
2021-08-19 00:23:48 -07:00
committed by Peter Johnson
parent 72716f51ce
commit 9359431bad
63 changed files with 821 additions and 955 deletions

View File

@@ -80,7 +80,7 @@ class DifferentialDrivetrainSim {
* @param maxVoltage The maximum voltage.
* @return The normalized input.
*/
Eigen::Matrix<double, 2, 1> ClampInput(Eigen::Matrix<double, 2, 1> u);
Eigen::Vector<double, 2> ClampInput(const Eigen::Vector<double, 2>& u);
/**
* Sets the applied voltage to the drivetrain. Note that positive voltage must
@@ -178,7 +178,7 @@ class DifferentialDrivetrainSim {
*
* @param state The state.
*/
void SetState(const Eigen::Matrix<double, 7, 1>& state);
void SetState(const Eigen::Vector<double, 7>& state);
/**
* Sets the system pose.
@@ -187,8 +187,8 @@ class DifferentialDrivetrainSim {
*/
void SetPose(const frc::Pose2d& pose);
Eigen::Matrix<double, 7, 1> Dynamics(const Eigen::Matrix<double, 7, 1>& x,
const Eigen::Matrix<double, 2, 1>& u);
Eigen::Vector<double, 7> Dynamics(const Eigen::Vector<double, 7>& x,
const Eigen::Vector<double, 2>& u);
class State {
public:
@@ -295,7 +295,7 @@ class DifferentialDrivetrainSim {
/**
* Returns the current output vector y.
*/
Eigen::Matrix<double, 7, 1> GetOutput() const;
Eigen::Vector<double, 7> GetOutput() const;
/**
* Returns an element of the state vector. Note that this will not include
@@ -308,7 +308,7 @@ class DifferentialDrivetrainSim {
/**
* Returns the current state vector x. Note that this will not include noise!
*/
Eigen::Matrix<double, 7, 1> GetState() const;
Eigen::Vector<double, 7> GetState() const;
LinearSystem<2, 2, 2> m_plant;
units::meter_t m_rb;
@@ -319,9 +319,9 @@ class DifferentialDrivetrainSim {
double m_originalGearing;
double m_currentGearing;
Eigen::Matrix<double, 7, 1> m_x;
Eigen::Matrix<double, 2, 1> m_u;
Eigen::Matrix<double, 7, 1> m_y;
Eigen::Vector<double, 7> m_x;
Eigen::Vector<double, 2> m_u;
Eigen::Vector<double, 7> m_y;
std::array<double, 7> m_measurementStdDevs;
};
} // namespace frc::sim

View File

@@ -123,9 +123,9 @@ class ElevatorSim : public LinearSystemSim<2, 1, 1> {
* @param u The system inputs (voltage).
* @param dt The time difference between controller updates.
*/
Eigen::Matrix<double, 2, 1> UpdateX(
const Eigen::Matrix<double, 2, 1>& currentXhat,
const Eigen::Matrix<double, 1, 1>& u, units::second_t dt) override;
Eigen::Vector<double, 2> UpdateX(const Eigen::Vector<double, 2>& currentXhat,
const Eigen::Vector<double, 1>& u,
units::second_t dt) override;
private:
DCMotor m_gearbox;

View File

@@ -40,9 +40,9 @@ class LinearSystemSim {
const LinearSystem<States, Inputs, Outputs>& system,
const std::array<double, Outputs>& measurementStdDevs = {})
: m_plant(system), m_measurementStdDevs(measurementStdDevs) {
m_x = Eigen::Matrix<double, States, 1>::Zero();
m_y = Eigen::Matrix<double, Outputs, 1>::Zero();
m_u = Eigen::Matrix<double, Inputs, 1>::Zero();
m_x = Eigen::Vector<double, States>::Zero();
m_y = Eigen::Vector<double, Outputs>::Zero();
m_u = Eigen::Vector<double, Inputs>::Zero();
}
/**
@@ -69,7 +69,7 @@ class LinearSystemSim {
*
* @return The current output of the plant.
*/
const Eigen::Matrix<double, Outputs, 1>& GetOutput() const { return m_y; }
const Eigen::Vector<double, Outputs>& GetOutput() const { return m_y; }
/**
* Returns an element of the current output of the plant.
@@ -84,9 +84,7 @@ class LinearSystemSim {
*
* @param u The system inputs.
*/
void SetInput(const Eigen::Matrix<double, Inputs, 1>& u) {
m_u = ClampInput(u);
}
void SetInput(const Eigen::Vector<double, Inputs>& u) { m_u = ClampInput(u); }
/*
* Sets the system inputs.
@@ -104,7 +102,7 @@ class LinearSystemSim {
*
* @param state The new state.
*/
void SetState(const Eigen::Matrix<double, States, 1>& state) { m_x = state; }
void SetState(const Eigen::Vector<double, States>& state) { m_x = state; }
/**
* Returns the current drawn by this simulated system. Override this method to
@@ -124,9 +122,9 @@ class LinearSystemSim {
* @param u The system inputs (usually voltage).
* @param dt The time difference between controller updates.
*/
virtual Eigen::Matrix<double, States, 1> UpdateX(
const Eigen::Matrix<double, States, 1>& currentXhat,
const Eigen::Matrix<double, Inputs, 1>& u, units::second_t dt) {
virtual Eigen::Vector<double, States> UpdateX(
const Eigen::Vector<double, States>& currentXhat,
const Eigen::Vector<double, Inputs>& u, units::second_t dt) {
return m_plant.CalculateX(currentXhat, u, dt);
}
@@ -137,17 +135,16 @@ class LinearSystemSim {
* @param u The input vector.
* @return The normalized input.
*/
Eigen::Matrix<double, Inputs, 1> ClampInput(
Eigen::Matrix<double, Inputs, 1> u) {
Eigen::Vector<double, Inputs> ClampInput(Eigen::Vector<double, Inputs> u) {
return frc::NormalizeInputVector<Inputs>(
u, frc::RobotController::GetInputVoltage());
}
LinearSystem<States, Inputs, Outputs> m_plant;
Eigen::Matrix<double, States, 1> m_x;
Eigen::Matrix<double, Outputs, 1> m_y;
Eigen::Matrix<double, Inputs, 1> m_u;
Eigen::Vector<double, States> m_x;
Eigen::Vector<double, Outputs> m_y;
Eigen::Vector<double, Inputs> m_u;
std::array<double, Outputs> m_measurementStdDevs;
};
} // namespace frc::sim

View File

@@ -142,9 +142,9 @@ class SingleJointedArmSim : public LinearSystemSim<2, 1, 1> {
* @param u The system inputs (voltage).
* @param dt The time difference between controller updates.
*/
Eigen::Matrix<double, 2, 1> UpdateX(
const Eigen::Matrix<double, 2, 1>& currentXhat,
const Eigen::Matrix<double, 1, 1>& u, units::second_t dt) override;
Eigen::Vector<double, 2> UpdateX(const Eigen::Vector<double, 2>& currentXhat,
const Eigen::Vector<double, 1>& u,
units::second_t dt) override;
private:
units::meter_t m_r;