blob: 7dea1dc2188046bc25ef4bcb0da733a5ebff1ee7 [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
Austin Schuhec7f06d2019-01-04 07:47:15 +110046 double DThetaDt(double distance, double velocity) const {
47 return DTheta(distance) * velocity;
48 }
49
Austin Schuh941b46d2018-12-19 18:06:05 +110050 // Returns the angular acceleration as a function of distance.
51 double DDTheta(double distance) const;
52
53 // Returns the length of the path in meters.
54 double length() const { return distances_.back(); }
55
56 private:
57 // Computes alpha for a distance
58 double DistanceToAlpha(double distance) const;
59
60 // The spline we are converting to a distance.
61 const Spline spline_;
62 // An interpolation table of distances evenly distributed in alpha.
63 ::std::vector<double> distances_;
64};
65
66} // namespace drivetrain
67} // namespace control_loops
68} // namespace frc971
69
70#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_DISTANCE_SPLINE_H_