[wpimath] Add typedefs for common types

This makes complex code significantly easier to read.

frc::Vectord<Size> = Eigen::Vector<double, Size>
frc::Matrixd<Rows, Cols> = Eigen::Matrix<double, Rows, Cols>
This commit is contained in:
Peter Johnson
2022-04-29 22:29:20 -07:00
parent 97c493241f
commit e767605e94
76 changed files with 1136 additions and 1449 deletions

View File

@@ -4,11 +4,11 @@
#pragma once
#include <frc/EigenCore.h>
#include <frc/kinematics/DifferentialDriveKinematics.h>
#include <frc/system/LinearSystem.h>
#include <frc/system/plant/DCMotor.h>
#include <Eigen/Core>
#include <units/length.h>
#include <units/moment_of_inertia.h>
#include <units/time.h>
@@ -80,7 +80,7 @@ class DifferentialDrivetrainSim {
* @param u The input vector.
* @return The normalized input.
*/
Eigen::Vector<double, 2> ClampInput(const Eigen::Vector<double, 2>& u);
Vectord<2> ClampInput(const Vectord<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::Vector<double, 7>& state);
void SetState(const Vectord<7>& state);
/**
* Sets the system pose.
@@ -187,8 +187,7 @@ class DifferentialDrivetrainSim {
*/
void SetPose(const frc::Pose2d& pose);
Eigen::Vector<double, 7> Dynamics(const Eigen::Vector<double, 7>& x,
const Eigen::Vector<double, 2>& u);
Vectord<7> Dynamics(const Vectord<7>& x, const Vectord<2>& u);
class State {
public:
@@ -301,7 +300,7 @@ class DifferentialDrivetrainSim {
/**
* Returns the current output vector y.
*/
Eigen::Vector<double, 7> GetOutput() const;
Vectord<7> GetOutput() const;
/**
* Returns an element of the state vector. Note that this will not include
@@ -314,7 +313,7 @@ class DifferentialDrivetrainSim {
/**
* Returns the current state vector x. Note that this will not include noise!
*/
Eigen::Vector<double, 7> GetState() const;
Vectord<7> GetState() const;
LinearSystem<2, 2, 2> m_plant;
units::meter_t m_rb;
@@ -325,9 +324,9 @@ class DifferentialDrivetrainSim {
double m_originalGearing;
double m_currentGearing;
Eigen::Vector<double, 7> m_x;
Eigen::Vector<double, 2> m_u;
Eigen::Vector<double, 7> m_y;
Vectord<7> m_x;
Vectord<2> m_u;
Vectord<7> m_y;
std::array<double, 7> m_measurementStdDevs;
};
} // namespace frc::sim

View File

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

View File

@@ -6,10 +6,10 @@
#include <array>
#include <Eigen/Core>
#include <units/current.h>
#include <units/time.h>
#include "frc/EigenCore.h"
#include "frc/RobotController.h"
#include "frc/StateSpaceUtil.h"
#include "frc/system/LinearSystem.h"
@@ -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::Vector<double, States>::Zero();
m_y = Eigen::Vector<double, Outputs>::Zero();
m_u = Eigen::Vector<double, Inputs>::Zero();
m_x = Vectord<States>::Zero();
m_y = Vectord<Outputs>::Zero();
m_u = Vectord<Inputs>::Zero();
}
virtual ~LinearSystemSim() = default;
@@ -71,7 +71,7 @@ class LinearSystemSim {
*
* @return The current output of the plant.
*/
const Eigen::Vector<double, Outputs>& GetOutput() const { return m_y; }
const Vectord<Outputs>& GetOutput() const { return m_y; }
/**
* Returns an element of the current output of the plant.
@@ -86,7 +86,7 @@ class LinearSystemSim {
*
* @param u The system inputs.
*/
void SetInput(const Eigen::Vector<double, Inputs>& u) { m_u = ClampInput(u); }
void SetInput(const Vectord<Inputs>& u) { m_u = ClampInput(u); }
/*
* Sets the system inputs.
@@ -104,7 +104,7 @@ class LinearSystemSim {
*
* @param state The new state.
*/
void SetState(const Eigen::Vector<double, States>& state) { m_x = state; }
void SetState(const Vectord<States>& state) { m_x = state; }
/**
* Returns the current drawn by this simulated system. Override this method to
@@ -124,9 +124,9 @@ class LinearSystemSim {
* @param u The system inputs (usually voltage).
* @param dt The time difference between controller updates.
*/
virtual Eigen::Vector<double, States> UpdateX(
const Eigen::Vector<double, States>& currentXhat,
const Eigen::Vector<double, Inputs>& u, units::second_t dt) {
virtual Vectord<States> UpdateX(const Vectord<States>& currentXhat,
const Vectord<Inputs>& u,
units::second_t dt) {
return m_plant.CalculateX(currentXhat, u, dt);
}
@@ -137,16 +137,16 @@ class LinearSystemSim {
* @param u The input vector.
* @return The normalized input.
*/
Eigen::Vector<double, Inputs> ClampInput(Eigen::Vector<double, Inputs> u) {
Vectord<Inputs> ClampInput(Vectord<Inputs> u) {
return frc::DesaturateInputVector<Inputs>(
u, frc::RobotController::GetInputVoltage());
}
LinearSystem<States, Inputs, Outputs> m_plant;
Eigen::Vector<double, States> m_x;
Eigen::Vector<double, Outputs> m_y;
Eigen::Vector<double, Inputs> m_u;
Vectord<States> m_x;
Vectord<Outputs> m_y;
Vectord<Inputs> m_u;
std::array<double, Outputs> m_measurementStdDevs;
};
} // namespace frc::sim

View File

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