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. |
| 44 | double DTheta(double distance) const { |
| 45 | // TODO(austin): We are re-computing DPoint here! |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 46 | const AlphaAndIndex a = DistanceToAlpha(distance); |
| 47 | const Spline &spline = splines_[a.index]; |
| 48 | return spline.DTheta(a.alpha) / spline.DPoint(a.alpha).norm(); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 49 | } |
| 50 | |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 51 | double DThetaDt(double distance, double velocity) const { |
| 52 | return DTheta(distance) * velocity; |
| 53 | } |
| 54 | |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 55 | // Returns the angular acceleration as a function of distance. |
| 56 | double DDTheta(double distance) const; |
| 57 | |
| 58 | // Returns the length of the path in meters. |
| 59 | double length() const { return distances_.back(); } |
| 60 | |
| 61 | private: |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 62 | struct AlphaAndIndex { |
| 63 | size_t index; |
| 64 | double alpha; |
| 65 | }; |
| 66 | |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 67 | // Computes alpha for a distance |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 68 | AlphaAndIndex DistanceToAlpha(double distance) const; |
| 69 | |
| 70 | ::std::vector<double> BuildDistances(size_t num_alpha); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 71 | |
| 72 | // The spline we are converting to a distance. |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 73 | const ::std::vector<Spline> splines_; |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 74 | // An interpolation table of distances evenly distributed in alpha. |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 75 | const ::std::vector<double> distances_; |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | } // namespace drivetrain |
| 79 | } // namespace control_loops |
| 80 | } // namespace frc971 |
| 81 | |
| 82 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_DISTANCE_SPLINE_H_ |