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" |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 7 | #include "absl/types/span.h" |
| 8 | #include "aos/containers/sized_array.h" |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 9 | #include "frc971/control_loops/drivetrain/spline.h" |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 10 | #include "frc971/control_loops/drivetrain/trajectory_generated.h" |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 11 | #include "frc971/control_loops/fixed_quadrature.h" |
| 12 | |
| 13 | namespace frc971 { |
| 14 | namespace control_loops { |
| 15 | namespace drivetrain { |
| 16 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 17 | std::vector<Spline> FlatbufferToSplines(const MultiSpline *fb); |
| 18 | |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 19 | // Class to hold a spline as a function of distance. |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 20 | class DistanceSplineBase { |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 21 | public: |
| 22 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
| 23 | |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 24 | virtual ~DistanceSplineBase() {} |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 25 | |
| 26 | flatbuffers::Offset<fb::DistanceSpline> Serialize( |
| 27 | flatbuffers::FlatBufferBuilder *fbb, |
| 28 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Constraint>>> |
| 29 | constraints) const; |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 30 | |
| 31 | // Returns a point on the spline as a function of distance. |
| 32 | ::Eigen::Matrix<double, 2, 1> XY(double distance) const { |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 33 | const AlphaAndIndex a = DistanceToAlpha(distance); |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 34 | return splines()[a.index].Point(a.alpha); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // Returns the velocity as a function of distance. |
| 38 | ::Eigen::Matrix<double, 2, 1> DXY(double distance) const { |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 39 | const AlphaAndIndex a = DistanceToAlpha(distance); |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 40 | return splines()[a.index].DPoint(a.alpha).normalized(); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // Returns the acceleration as a function of distance. |
| 44 | ::Eigen::Matrix<double, 2, 1> DDXY(double distance) const; |
| 45 | |
| 46 | // Returns the heading as a function of distance. |
| 47 | double Theta(double distance) const { |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 48 | const AlphaAndIndex a = DistanceToAlpha(distance); |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 49 | return splines()[a.index].Theta(a.alpha); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // Returns the angular velocity as a function of distance. |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 53 | // This is also equivalent to the curvature at the given point. |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 54 | double DTheta(double distance) const { |
| 55 | // TODO(austin): We are re-computing DPoint here! |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 56 | const AlphaAndIndex a = DistanceToAlpha(distance); |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 57 | const Spline &spline = splines()[a.index]; |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 58 | return spline.DTheta(a.alpha) / spline.DPoint(a.alpha).norm(); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 59 | } |
| 60 | |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 61 | // Returns the derivative of heading with respect to time at a given |
| 62 | // distance along the spline, if we are travelling at the provided velocity. |
Austin Schuh | ec7f06d | 2019-01-04 07:47:15 +1100 | [diff] [blame] | 63 | double DThetaDt(double distance, double velocity) const { |
| 64 | return DTheta(distance) * velocity; |
| 65 | } |
| 66 | |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 67 | // Returns the angular acceleration as a function of distance. |
| 68 | double DDTheta(double distance) const; |
| 69 | |
| 70 | // Returns the length of the path in meters. |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 71 | double length() const { return distances().back(); } |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 72 | |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 73 | virtual const absl::Span<const float> distances() const = 0; |
| 74 | virtual const absl::Span<const Spline> splines() const = 0; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 75 | |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 76 | private: |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 77 | struct AlphaAndIndex { |
| 78 | size_t index; |
| 79 | double alpha; |
| 80 | }; |
| 81 | |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 82 | // Computes alpha for a distance |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 83 | AlphaAndIndex DistanceToAlpha(double distance) const; |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 84 | }; |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 85 | |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 86 | // Class which computes a DistanceSpline and stores it. |
| 87 | class DistanceSpline final : public DistanceSplineBase { |
| 88 | public: |
| 89 | DistanceSpline(const Spline &spline, int num_alpha = 0); |
| 90 | DistanceSpline(::std::vector<Spline> &&splines, int num_alpha = 0); |
| 91 | DistanceSpline(const MultiSpline *fb, int num_alpha = 0); |
| 92 | |
| 93 | const absl::Span<const float> distances() const override { |
| 94 | return distances_; |
| 95 | } |
| 96 | const absl::Span<const Spline> splines() const override { return splines_; } |
| 97 | |
| 98 | private: |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 99 | ::std::vector<float> BuildDistances(size_t num_alpha); |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 100 | |
| 101 | // The spline we are converting to a distance. |
Austin Schuh | 280996e | 2019-01-19 17:43:37 -0800 | [diff] [blame] | 102 | const ::std::vector<Spline> splines_; |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 103 | // An interpolation table of distances evenly distributed in alpha. |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 104 | const ::std::vector<float> distances_; |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 105 | }; |
| 106 | |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 107 | // Class exposing a finished DistanceSpline flatbuffer as a DistanceSpline. |
| 108 | // |
| 109 | // The lifetime of the provided fb::DistanceSpline needs to out-live this class. |
| 110 | class FinishedDistanceSpline final : public DistanceSplineBase { |
| 111 | public: |
| 112 | static constexpr size_t kMaxSplines = 6; |
| 113 | FinishedDistanceSpline(const fb::DistanceSpline &fb); |
| 114 | |
| 115 | const absl::Span<const float> distances() const override { |
| 116 | return distances_; |
| 117 | } |
| 118 | const absl::Span<const Spline> splines() const override { return splines_; } |
| 119 | |
| 120 | private: |
| 121 | ::std::vector<float> BuildDistances(size_t num_alpha); |
| 122 | |
| 123 | // The spline we are converting to a distance. |
| 124 | aos::SizedArray<Spline, kMaxSplines> splines_; |
| 125 | // An interpolation table of distances evenly distributed in alpha. |
James Kuszmaul | dc53443 | 2023-02-05 14:51:11 -0800 | [diff] [blame] | 126 | absl::Span<const float> distances_; |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
Austin Schuh | 941b46d | 2018-12-19 18:06:05 +1100 | [diff] [blame] | 129 | } // namespace drivetrain |
| 130 | } // namespace control_loops |
| 131 | } // namespace frc971 |
| 132 | |
| 133 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_DISTANCE_SPLINE_H_ |