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 | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 4 | #include <atomic> |
| 5 | #include <thread> |
| 6 | |
Alex Perry | a71badb | 2019-02-06 19:40:41 -0800 | [diff] [blame] | 7 | #include "Eigen/Dense" |
| 8 | |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 9 | #include "aos/condition.h" |
| 10 | #include "aos/mutex/mutex.h" |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 11 | #include "frc971/control_loops/drivetrain/distance_spline.h" |
| 12 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 13 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
| 14 | #include "frc971/control_loops/drivetrain/spline.h" |
| 15 | #include "frc971/control_loops/drivetrain/trajectory.h" |
| 16 | |
| 17 | namespace frc971 { |
| 18 | namespace control_loops { |
| 19 | namespace drivetrain { |
| 20 | |
| 21 | class SplineDrivetrain { |
| 22 | public: |
| 23 | SplineDrivetrain(const DrivetrainConfig<double> &dt_config); |
| 24 | |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 25 | ~SplineDrivetrain() { |
| 26 | { |
| 27 | ::aos::MutexLocker locker(&mutex_); |
| 28 | run_ = false; |
| 29 | new_goal_.Signal(); |
| 30 | } |
| 31 | worker_thread_.join(); |
| 32 | } |
| 33 | |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 34 | void SetGoal(const ::frc971::control_loops::DrivetrainQueue::Goal &goal); |
| 35 | |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 36 | void Update(bool enabled, const ::Eigen::Matrix<double, 5, 1> &state); |
Alex Perry | a71badb | 2019-02-06 19:40:41 -0800 | [diff] [blame] | 37 | |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 38 | void SetOutput( |
| 39 | ::frc971::control_loops::DrivetrainQueue::Output *output); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 40 | void PopulateStatus( |
| 41 | ::frc971::control_loops::DrivetrainQueue::Status *status) const; |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 42 | |
| 43 | // Accessor for the current goal state, pretty much only present for debugging |
| 44 | // purposes. |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 45 | ::Eigen::Matrix<double, 5, 1> CurrentGoalState() const { |
| 46 | return current_trajectory_ |
| 47 | ? current_trajectory_->GoalState(current_xva_(0), |
| 48 | current_xva_(1)) |
| 49 | : ::Eigen::Matrix<double, 5, 1>::Zero(); |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | bool IsAtEnd() const { |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 53 | return current_trajectory_ |
| 54 | ? current_trajectory_->is_at_end(current_xva_.block<2, 1>(0, 0)) : |
| 55 | true; |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 56 | } |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 57 | |
| 58 | enum class PlanState : int8_t { |
| 59 | kNoPlan = 0, |
| 60 | kBuildingTrajectory = 1, |
| 61 | kPlanningTrajectory = 2, |
| 62 | kPlannedTrajectory = 3, |
| 63 | }; |
| 64 | |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 65 | private: |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 66 | void ComputeTrajectory(); |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 67 | void ScaleCapU(Eigen::Matrix<double, 2, 1> *U); |
| 68 | |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 69 | const DrivetrainConfig<double> dt_config_; |
| 70 | |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 71 | int32_t current_spline_handle_ = 0; // Current spline told to excecute. |
| 72 | int32_t current_spline_idx_ = 0; // Current executing spline. |
Alex Perry | 4b502a9 | 2019-04-06 22:00:38 -0700 | [diff] [blame] | 73 | bool has_started_execution_ = false; |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 74 | |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 75 | ::std::unique_ptr<DistanceSpline> current_distance_spline_; |
| 76 | ::std::unique_ptr<Trajectory> current_trajectory_; |
| 77 | |
| 78 | // State required to compute the next iteration's output. |
| 79 | ::Eigen::Matrix<double, 3, 1> current_xva_, next_xva_; |
| 80 | ::Eigen::Matrix<double, 2, 1> next_U_; |
| 81 | |
| 82 | // Information used for status message. |
| 83 | ::Eigen::Matrix<double, 2, 1> uncapped_U_; |
| 84 | bool enable_ = false; |
| 85 | bool output_was_capped_ = false; |
| 86 | |
| 87 | std::atomic<PlanState> plan_state_ = {PlanState::kNoPlan}; |
| 88 | |
| 89 | ::std::thread worker_thread_; |
| 90 | // mutex_ is held by the worker thread while it is doing work or by the main |
| 91 | // thread when it is sending work to the worker thread. |
| 92 | ::aos::Mutex mutex_; |
| 93 | // new_goal_ is used to signal to the worker thread that ther is work to do. |
| 94 | ::aos::Condition new_goal_; |
| 95 | // The following variables are guarded by mutex_. |
| 96 | bool run_ = true; |
| 97 | ::frc971::control_loops::DrivetrainQueue::Goal goal_; |
| 98 | ::std::unique_ptr<DistanceSpline> past_distance_spline_; |
| 99 | ::std::unique_ptr<DistanceSpline> future_distance_spline_; |
| 100 | ::std::unique_ptr<Trajectory> past_trajectory_; |
| 101 | ::std::unique_ptr<Trajectory> future_trajectory_; |
| 102 | int32_t future_spline_idx_ = 0; // Current spline being computed. |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 103 | ::std::atomic<int32_t> planning_spline_idx_{-1}; |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 104 | |
| 105 | // TODO(alex): pull this out of dt_config. |
| 106 | const ::Eigen::DiagonalMatrix<double, 5> Q = |
| 107 | (::Eigen::DiagonalMatrix<double, 5>().diagonal() |
Austin Schuh | 1104318 | 2019-03-23 22:29:12 -0700 | [diff] [blame] | 108 | << 1.0 / ::std::pow(0.07, 2), |
| 109 | 1.0 / ::std::pow(0.07, 2), 1.0 / ::std::pow(0.2, 2), |
| 110 | 1.0 / ::std::pow(1.5, 2), 1.0 / ::std::pow(1.5, 2)) |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 111 | .finished() |
| 112 | .asDiagonal(); |
| 113 | const ::Eigen::DiagonalMatrix<double, 2> R = |
| 114 | (::Eigen::DiagonalMatrix<double, 2>().diagonal() |
| 115 | << 1.0 / ::std::pow(12.0, 2), |
| 116 | 1.0 / ::std::pow(12.0, 2)) |
| 117 | .finished() |
| 118 | .asDiagonal(); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | } // namespace drivetrain |
| 122 | } // namespace control_loops |
| 123 | } // namespace frc971 |
| 124 | |
| 125 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_SPLINEDRIVETRAIN_H_ |