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