Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_DRIVETRAIN_DISTANCE_SPLINE_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_DRIVETRAIN_DISTANCE_SPLINE_H_ |
| 3 | |
| 4 | #include <vector> |
| 5 | |
| 6 | #include "Eigen/Dense" |
| 7 | #include "frc971/control_loops/drivetrain/spline.h" |
| 8 | #include "frc971/control_loops/fixed_quadrature.h" |
| 9 | |
| 10 | namespace frc971 { |
| 11 | namespace control_loops { |
| 12 | namespace drivetrain { |
| 13 | |
| 14 | // Class to hold a spline as a function of distance. |
| 15 | class DistanceSpline { |
| 16 | public: |
| 17 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
| 18 | |
| 19 | DistanceSpline(const Spline &spline, int num_alpha = 100); |
| 20 | |
| 21 | // Returns a point on the spline as a function of distance. |
| 22 | ::Eigen::Matrix<double, 2, 1> XY(double distance) const { |
| 23 | return spline_.Point(DistanceToAlpha(distance)); |
| 24 | } |
| 25 | |
| 26 | // Returns the velocity as a function of distance. |
| 27 | ::Eigen::Matrix<double, 2, 1> DXY(double distance) const { |
| 28 | return spline_.DPoint(DistanceToAlpha(distance)).normalized(); |
| 29 | } |
| 30 | |
| 31 | // Returns the acceleration as a function of distance. |
| 32 | ::Eigen::Matrix<double, 2, 1> DDXY(double distance) const; |
| 33 | |
| 34 | // Returns the heading as a function of distance. |
| 35 | double Theta(double distance) const { |
| 36 | return spline_.Theta(DistanceToAlpha(distance)); |
| 37 | } |
| 38 | |
| 39 | // Returns the angular velocity as a function of distance. |
| 40 | double DTheta(double distance) const { |
| 41 | // TODO(austin): We are re-computing DPoint here! |
| 42 | const double alpha = DistanceToAlpha(distance); |
| 43 | return spline_.DTheta(alpha) / spline_.DPoint(alpha).norm(); |
| 44 | } |
| 45 | |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame^] | 46 | double DThetaDt(double distance, double velocity) const { |
| 47 | return DTheta(distance) * velocity; |
| 48 | } |
| 49 | |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 50 | // Returns the angular acceleration as a function of distance. |
| 51 | double DDTheta(double distance) const; |
| 52 | |
| 53 | // Returns the length of the path in meters. |
| 54 | double length() const { return distances_.back(); } |
| 55 | |
| 56 | private: |
| 57 | // Computes alpha for a distance |
| 58 | double DistanceToAlpha(double distance) const; |
| 59 | |
| 60 | // The spline we are converting to a distance. |
| 61 | const Spline spline_; |
| 62 | // An interpolation table of distances evenly distributed in alpha. |
| 63 | ::std::vector<double> distances_; |
| 64 | }; |
| 65 | |
| 66 | } // namespace drivetrain |
| 67 | } // namespace control_loops |
| 68 | } // namespace frc971 |
| 69 | |
| 70 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_DISTANCE_SPLINE_H_ |