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