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" |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame^] | 8 | #include "frc971/control_loops/drivetrain/trajectory_generated.h" |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 9 | #include "frc971/control_loops/fixed_quadrature.h" |
| 10 | |
| 11 | namespace frc971 { |
| 12 | namespace control_loops { |
| 13 | namespace drivetrain { |
| 14 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame^] | 15 | std::vector<Spline> FlatbufferToSplines(const MultiSpline *fb); |
| 16 | |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 17 | // Class to hold a spline as a function of distance. |
| 18 | class DistanceSpline { |
| 19 | public: |
| 20 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
| 21 | |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 22 | DistanceSpline(const Spline &spline, int num_alpha = 0); |
| 23 | DistanceSpline(::std::vector<Spline> &&splines, int num_alpha = 0); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame^] | 24 | DistanceSpline(const MultiSpline *fb, int num_alpha = 0); |
| 25 | // Copies the distances for the spline directly out of the provided buffer, |
| 26 | // rather than constructing the distances from the original splines. |
| 27 | DistanceSpline(const fb::DistanceSpline &fb); |
| 28 | |
| 29 | flatbuffers::Offset<fb::DistanceSpline> Serialize( |
| 30 | flatbuffers::FlatBufferBuilder *fbb, |
| 31 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Constraint>>> |
| 32 | constraints) const; |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 33 | |
| 34 | // Returns a point on the spline as a function of distance. |
| 35 | ::Eigen::Matrix<double, 2, 1> XY(double distance) const { |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 36 | const AlphaAndIndex a = DistanceToAlpha(distance); |
| 37 | return splines_[a.index].Point(a.alpha); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // Returns the velocity as a function of distance. |
| 41 | ::Eigen::Matrix<double, 2, 1> DXY(double distance) const { |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 42 | const AlphaAndIndex a = DistanceToAlpha(distance); |
| 43 | return splines_[a.index].DPoint(a.alpha).normalized(); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // Returns the acceleration as a function of distance. |
| 47 | ::Eigen::Matrix<double, 2, 1> DDXY(double distance) const; |
| 48 | |
| 49 | // Returns the heading as a function of distance. |
| 50 | double Theta(double distance) const { |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 51 | const AlphaAndIndex a = DistanceToAlpha(distance); |
| 52 | return splines_[a.index].Theta(a.alpha); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // Returns the angular velocity as a function of distance. |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 56 | // This is also equivalent to the curvature at the given point. |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 57 | double DTheta(double distance) const { |
| 58 | // TODO(austin): We are re-computing DPoint here! |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 59 | const AlphaAndIndex a = DistanceToAlpha(distance); |
| 60 | const Spline &spline = splines_[a.index]; |
| 61 | return spline.DTheta(a.alpha) / spline.DPoint(a.alpha).norm(); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 62 | } |
| 63 | |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 64 | // Returns the derivative of heading with respect to time at a given |
| 65 | // distance along the spline, if we are travelling at the provided velocity. |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 66 | double DThetaDt(double distance, double velocity) const { |
| 67 | return DTheta(distance) * velocity; |
| 68 | } |
| 69 | |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 70 | // Returns the angular acceleration as a function of distance. |
| 71 | double DDTheta(double distance) const; |
| 72 | |
| 73 | // Returns the length of the path in meters. |
| 74 | double length() const { return distances_.back(); } |
| 75 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame^] | 76 | const std::vector<float> &distances() const { return distances_; } |
| 77 | const std::vector<Spline> &splines() const { return splines_; } |
| 78 | |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 79 | private: |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 80 | struct AlphaAndIndex { |
| 81 | size_t index; |
| 82 | double alpha; |
| 83 | }; |
| 84 | |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 85 | // Computes alpha for a distance |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 86 | AlphaAndIndex DistanceToAlpha(double distance) const; |
| 87 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame^] | 88 | ::std::vector<float> BuildDistances(size_t num_alpha); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 89 | |
| 90 | // The spline we are converting to a distance. |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 91 | const ::std::vector<Spline> splines_; |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 92 | // An interpolation table of distances evenly distributed in alpha. |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame^] | 93 | const ::std::vector<float> distances_; |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | } // namespace drivetrain |
| 97 | } // namespace control_loops |
| 98 | } // namespace frc971 |
| 99 | |
| 100 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_DISTANCE_SPLINE_H_ |