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 | |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 19 | DistanceSpline(const Spline &spline, int num_alpha = 0); |
| 20 | DistanceSpline(::std::vector<Spline> &&splines, int num_alpha = 0); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 21 | |
| 22 | // Returns a point on the spline as a function of distance. |
| 23 | ::Eigen::Matrix<double, 2, 1> XY(double distance) const { |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 24 | const AlphaAndIndex a = DistanceToAlpha(distance); |
| 25 | return splines_[a.index].Point(a.alpha); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | // Returns the velocity as a function of distance. |
| 29 | ::Eigen::Matrix<double, 2, 1> DXY(double distance) const { |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 30 | const AlphaAndIndex a = DistanceToAlpha(distance); |
| 31 | return splines_[a.index].DPoint(a.alpha).normalized(); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | // Returns the acceleration as a function of distance. |
| 35 | ::Eigen::Matrix<double, 2, 1> DDXY(double distance) const; |
| 36 | |
| 37 | // Returns the heading as a function of distance. |
| 38 | double Theta(double distance) const { |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 39 | const AlphaAndIndex a = DistanceToAlpha(distance); |
| 40 | return splines_[a.index].Theta(a.alpha); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // Returns the angular velocity as a function of distance. |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame^] | 44 | // This is also equivalent to the curvature at the given point. |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 45 | double DTheta(double distance) const { |
| 46 | // TODO(austin): We are re-computing DPoint here! |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 47 | const AlphaAndIndex a = DistanceToAlpha(distance); |
| 48 | const Spline &spline = splines_[a.index]; |
| 49 | return spline.DTheta(a.alpha) / spline.DPoint(a.alpha).norm(); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 50 | } |
| 51 | |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame^] | 52 | // Returns the derivative of heading with respect to time at a given |
| 53 | // distance along the spline, if we are travelling at the provided velocity. |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 54 | double DThetaDt(double distance, double velocity) const { |
| 55 | return DTheta(distance) * velocity; |
| 56 | } |
| 57 | |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 58 | // Returns the angular acceleration as a function of distance. |
| 59 | double DDTheta(double distance) const; |
| 60 | |
| 61 | // Returns the length of the path in meters. |
| 62 | double length() const { return distances_.back(); } |
| 63 | |
| 64 | private: |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 65 | struct AlphaAndIndex { |
| 66 | size_t index; |
| 67 | double alpha; |
| 68 | }; |
| 69 | |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 70 | // Computes alpha for a distance |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 71 | AlphaAndIndex DistanceToAlpha(double distance) const; |
| 72 | |
| 73 | ::std::vector<double> BuildDistances(size_t num_alpha); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 74 | |
| 75 | // The spline we are converting to a distance. |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 76 | const ::std::vector<Spline> splines_; |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 77 | // An interpolation table of distances evenly distributed in alpha. |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 78 | const ::std::vector<double> distances_; |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | } // namespace drivetrain |
| 82 | } // namespace control_loops |
| 83 | } // namespace frc971 |
| 84 | |
| 85 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_DISTANCE_SPLINE_H_ |