Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_DRIVETRAIN_SPLINEDRIVETRAIN_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_DRIVETRAIN_SPLINEDRIVETRAIN_H_ |
| 3 | |
Alex Perry | a71badb | 2019-02-06 19:40:41 -0800 | [diff] [blame] | 4 | #include "Eigen/Dense" |
| 5 | |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 6 | #include "frc971/control_loops/drivetrain/distance_spline.h" |
| 7 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 8 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
| 9 | #include "frc971/control_loops/drivetrain/spline.h" |
| 10 | #include "frc971/control_loops/drivetrain/trajectory.h" |
| 11 | |
| 12 | namespace frc971 { |
| 13 | namespace control_loops { |
| 14 | namespace drivetrain { |
| 15 | |
| 16 | class SplineDrivetrain { |
| 17 | public: |
| 18 | SplineDrivetrain(const DrivetrainConfig<double> &dt_config); |
| 19 | |
| 20 | void SetGoal(const ::frc971::control_loops::DrivetrainQueue::Goal &goal); |
| 21 | |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 22 | void Update(bool enabled, const ::Eigen::Matrix<double, 5, 1> &state); |
Alex Perry | a71badb | 2019-02-06 19:40:41 -0800 | [diff] [blame] | 23 | |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 24 | void SetOutput( |
| 25 | ::frc971::control_loops::DrivetrainQueue::Output *output); |
| 26 | // TODO(alex): What status do we need? |
| 27 | void PopulateStatus( |
| 28 | ::frc971::control_loops::DrivetrainQueue::Status *status) const; |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 29 | |
| 30 | // Accessor for the current goal state, pretty much only present for debugging |
| 31 | // purposes. |
| 32 | Eigen::Matrix<double, 5, 1> CurrentGoalState() const { |
| 33 | return current_trajectory_->GoalState(current_xva_(0), current_xva_(1)); |
| 34 | } |
| 35 | |
| 36 | bool IsAtEnd() const { |
| 37 | return current_trajectory_->is_at_end(current_state_); |
| 38 | } |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 39 | private: |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 40 | void ScaleCapU(Eigen::Matrix<double, 2, 1> *U); |
| 41 | |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 42 | const DrivetrainConfig<double> dt_config_; |
| 43 | |
| 44 | uint32_t current_spline_handle_; // Current spline told to excecute. |
| 45 | uint32_t current_spline_idx_; // Current excecuting spline. |
| 46 | ::std::unique_ptr<DistanceSpline> distance_spline_; |
| 47 | ::std::unique_ptr<Trajectory> current_trajectory_; |
Alex Perry | a71badb | 2019-02-06 19:40:41 -0800 | [diff] [blame] | 48 | ::Eigen::Matrix<double, 3, 1> current_xva_, next_xva_; |
| 49 | ::Eigen::Matrix<double, 2, 1> current_state_; |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 50 | ::Eigen::Matrix<double, 2, 1> next_U_; |
| 51 | ::Eigen::Matrix<double, 2, 1> uncapped_U_; |
| 52 | |
| 53 | bool enable_; |
| 54 | |
| 55 | // TODO(alex): pull this out of dt_config. |
| 56 | const ::Eigen::DiagonalMatrix<double, 5> Q = |
| 57 | (::Eigen::DiagonalMatrix<double, 5>().diagonal() |
| 58 | << 1.0 / ::std::pow(0.05, 2), |
| 59 | 1.0 / ::std::pow(0.05, 2), 1.0 / ::std::pow(0.2, 2), |
| 60 | 1.0 / ::std::pow(0.5, 2), 1.0 / ::std::pow(0.5, 2)) |
| 61 | .finished() |
| 62 | .asDiagonal(); |
| 63 | const ::Eigen::DiagonalMatrix<double, 2> R = |
| 64 | (::Eigen::DiagonalMatrix<double, 2>().diagonal() |
| 65 | << 1.0 / ::std::pow(12.0, 2), |
| 66 | 1.0 / ::std::pow(12.0, 2)) |
| 67 | .finished() |
| 68 | .asDiagonal(); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | } // namespace drivetrain |
| 72 | } // namespace control_loops |
| 73 | } // namespace frc971 |
| 74 | |
| 75 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_SPLINEDRIVETRAIN_H_ |