blob: 0feb9027c94addf7d8d22b0698be3fbe20b9a621 [file] [log] [blame]
Austin Schuh941b46d2018-12-19 18:06:05 +11001#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 Kuszmaul75a18c52021-03-10 22:02:07 -08008#include "frc971/control_loops/drivetrain/trajectory_generated.h"
Austin Schuh941b46d2018-12-19 18:06:05 +11009#include "frc971/control_loops/fixed_quadrature.h"
10
11namespace frc971 {
12namespace control_loops {
13namespace drivetrain {
14
James Kuszmaul75a18c52021-03-10 22:02:07 -080015std::vector<Spline> FlatbufferToSplines(const MultiSpline *fb);
16
Austin Schuh941b46d2018-12-19 18:06:05 +110017// Class to hold a spline as a function of distance.
18class DistanceSpline {
19 public:
20 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
21
Austin Schuh280996e2019-01-19 17:43:37 -080022 DistanceSpline(const Spline &spline, int num_alpha = 0);
23 DistanceSpline(::std::vector<Spline> &&splines, int num_alpha = 0);
James Kuszmaul75a18c52021-03-10 22:02:07 -080024 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 Schuh941b46d2018-12-19 18:06:05 +110033
34 // Returns a point on the spline as a function of distance.
35 ::Eigen::Matrix<double, 2, 1> XY(double distance) const {
Austin Schuh280996e2019-01-19 17:43:37 -080036 const AlphaAndIndex a = DistanceToAlpha(distance);
37 return splines_[a.index].Point(a.alpha);
Austin Schuh941b46d2018-12-19 18:06:05 +110038 }
39
40 // Returns the velocity as a function of distance.
41 ::Eigen::Matrix<double, 2, 1> DXY(double distance) const {
Austin Schuh280996e2019-01-19 17:43:37 -080042 const AlphaAndIndex a = DistanceToAlpha(distance);
43 return splines_[a.index].DPoint(a.alpha).normalized();
Austin Schuh941b46d2018-12-19 18:06:05 +110044 }
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 Schuh280996e2019-01-19 17:43:37 -080051 const AlphaAndIndex a = DistanceToAlpha(distance);
52 return splines_[a.index].Theta(a.alpha);
Austin Schuh941b46d2018-12-19 18:06:05 +110053 }
54
55 // Returns the angular velocity as a function of distance.
James Kuszmaulaa2499d2020-06-02 21:31:19 -070056 // This is also equivalent to the curvature at the given point.
Austin Schuh941b46d2018-12-19 18:06:05 +110057 double DTheta(double distance) const {
58 // TODO(austin): We are re-computing DPoint here!
Austin Schuh280996e2019-01-19 17:43:37 -080059 const AlphaAndIndex a = DistanceToAlpha(distance);
60 const Spline &spline = splines_[a.index];
61 return spline.DTheta(a.alpha) / spline.DPoint(a.alpha).norm();
Austin Schuh941b46d2018-12-19 18:06:05 +110062 }
63
James Kuszmaulaa2499d2020-06-02 21:31:19 -070064 // 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 Schuhec7f06d2019-01-04 07:47:15 +110066 double DThetaDt(double distance, double velocity) const {
67 return DTheta(distance) * velocity;
68 }
69
Austin Schuh941b46d2018-12-19 18:06:05 +110070 // 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 Kuszmaul75a18c52021-03-10 22:02:07 -080076 const std::vector<float> &distances() const { return distances_; }
77 const std::vector<Spline> &splines() const { return splines_; }
78
Austin Schuh941b46d2018-12-19 18:06:05 +110079 private:
Austin Schuh280996e2019-01-19 17:43:37 -080080 struct AlphaAndIndex {
81 size_t index;
82 double alpha;
83 };
84
Austin Schuh941b46d2018-12-19 18:06:05 +110085 // Computes alpha for a distance
Austin Schuh280996e2019-01-19 17:43:37 -080086 AlphaAndIndex DistanceToAlpha(double distance) const;
87
James Kuszmaul75a18c52021-03-10 22:02:07 -080088 ::std::vector<float> BuildDistances(size_t num_alpha);
Austin Schuh941b46d2018-12-19 18:06:05 +110089
90 // The spline we are converting to a distance.
Austin Schuh280996e2019-01-19 17:43:37 -080091 const ::std::vector<Spline> splines_;
Austin Schuh941b46d2018-12-19 18:06:05 +110092 // An interpolation table of distances evenly distributed in alpha.
James Kuszmaul75a18c52021-03-10 22:02:07 -080093 const ::std::vector<float> distances_;
Austin Schuh941b46d2018-12-19 18:06:05 +110094};
95
96} // namespace drivetrain
97} // namespace control_loops
98} // namespace frc971
99
100#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_DISTANCE_SPLINE_H_