blob: 3c1824c302e2ea2b71ecfc136a4234c3c5d4ee19 [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"
8#include "frc971/control_loops/fixed_quadrature.h"
9
10namespace frc971 {
11namespace control_loops {
12namespace drivetrain {
13
14// Class to hold a spline as a function of distance.
15class DistanceSpline {
16 public:
17 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
18
19 DistanceSpline(const Spline &spline, int num_alpha = 100);
20
21 // Returns a point on the spline as a function of distance.
22 ::Eigen::Matrix<double, 2, 1> XY(double distance) const {
23 return spline_.Point(DistanceToAlpha(distance));
24 }
25
26 // Returns the velocity as a function of distance.
27 ::Eigen::Matrix<double, 2, 1> DXY(double distance) const {
28 return spline_.DPoint(DistanceToAlpha(distance)).normalized();
29 }
30
31 // Returns the acceleration as a function of distance.
32 ::Eigen::Matrix<double, 2, 1> DDXY(double distance) const;
33
34 // Returns the heading as a function of distance.
35 double Theta(double distance) const {
36 return spline_.Theta(DistanceToAlpha(distance));
37 }
38
39 // Returns the angular velocity as a function of distance.
40 double DTheta(double distance) const {
41 // TODO(austin): We are re-computing DPoint here!
42 const double alpha = DistanceToAlpha(distance);
43 return spline_.DTheta(alpha) / spline_.DPoint(alpha).norm();
44 }
45
46 // Returns the angular acceleration as a function of distance.
47 double DDTheta(double distance) const;
48
49 // Returns the length of the path in meters.
50 double length() const { return distances_.back(); }
51
52 private:
53 // Computes alpha for a distance
54 double DistanceToAlpha(double distance) const;
55
56 // The spline we are converting to a distance.
57 const Spline spline_;
58 // An interpolation table of distances evenly distributed in alpha.
59 ::std::vector<double> distances_;
60};
61
62} // namespace drivetrain
63} // namespace control_loops
64} // namespace frc971
65
66#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_DISTANCE_SPLINE_H_