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 | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 11 | #include "frc971/control_loops/control_loops_generated.h" |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 12 | #include "frc971/control_loops/drivetrain/distance_spline.h" |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 13 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 14 | #include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h" |
| 15 | #include "frc971/control_loops/drivetrain/drivetrain_output_generated.h" |
| 16 | #include "frc971/control_loops/drivetrain/drivetrain_status_generated.h" |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 17 | #include "frc971/control_loops/drivetrain/spline.h" |
| 18 | #include "frc971/control_loops/drivetrain/trajectory.h" |
| 19 | |
| 20 | namespace frc971 { |
| 21 | namespace control_loops { |
| 22 | namespace drivetrain { |
| 23 | |
| 24 | class SplineDrivetrain { |
| 25 | public: |
| 26 | SplineDrivetrain(const DrivetrainConfig<double> &dt_config); |
| 27 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 28 | void SetGoal(const ::frc971::control_loops::drivetrain::Goal *goal); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 29 | // Note that the user maintains ownership of the trajectory flatbuffer for the |
| 30 | // entire time; once AddTrajectory() is called, the trajectory pointer must |
| 31 | // stay valid until the spline has finished executing and HasTrajectory() |
| 32 | // returns false. |
| 33 | bool HasTrajectory(const fb::Trajectory *trajectory) const; |
| 34 | void AddTrajectory(const fb::Trajectory *trajectory); |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 35 | bool IsCurrentTrajectory(const fb::Trajectory *trajectory) const; |
| 36 | void DeleteTrajectory(const fb::Trajectory *trajectory); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 37 | |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 38 | void Update(bool enabled, const ::Eigen::Matrix<double, 5, 1> &state, |
| 39 | const ::Eigen::Matrix<double, 2, 1> &voltage_error); |
Alex Perry | a71badb | 2019-02-06 19:40:41 -0800 | [diff] [blame] | 40 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 41 | void SetOutput(::frc971::control_loops::drivetrain::OutputT *output); |
| 42 | |
| 43 | flatbuffers::Offset<TrajectoryLogging> MakeTrajectoryLogging( |
| 44 | aos::Sender<drivetrain::Status>::Builder *builder) const; |
| 45 | flatbuffers::Offset<TrajectoryLogging> MakeTrajectoryLogging( |
| 46 | flatbuffers::FlatBufferBuilder *builder) const; |
Austin Schuh | d749d93 | 2020-12-30 21:38:40 -0800 | [diff] [blame] | 47 | void PopulateStatus(drivetrain::Status::Builder *status) const; |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 48 | |
| 49 | // Accessor for the current goal state, pretty much only present for debugging |
| 50 | // purposes. |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 51 | ::Eigen::Matrix<double, 5, 1> CurrentGoalState() const { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 52 | return executing_spline_ ? current_trajectory().GoalState(current_xva_(0), |
| 53 | current_xva_(1)) |
| 54 | : ::Eigen::Matrix<double, 5, 1>::Zero(); |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | bool IsAtEnd() const { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 58 | return executing_spline_ |
| 59 | ? current_trajectory().is_at_end(current_xva_.block<2, 1>(0, 0)) |
Austin Schuh | d749d93 | 2020-12-30 21:38:40 -0800 | [diff] [blame] | 60 | : true; |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 61 | } |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 62 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 63 | size_t trajectory_count() const { return trajectories_.size(); } |
| 64 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 65 | // Returns true if the splinedrivetrain is enabled. |
| 66 | bool enable() const { return enable_; } |
| 67 | |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 68 | private: |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 69 | void ScaleCapU(Eigen::Matrix<double, 2, 1> *U); |
| 70 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 71 | // This is called to update the internal state for managing all the splines. |
| 72 | // Calling it redundantly does not cause any issues. It checks the value of |
| 73 | // commanded_spline_ to determine whether we are being commanded to run a |
| 74 | // spline, and if there is any trajectory in the list of trajectories matching |
| 75 | // the command, we begin/continue executing that spline. If commanded_spline_ |
| 76 | // is empty or has changed, we stop executing the previous trajectory and |
| 77 | // remove it from trajectories_. Then, when the drivetrain code checks |
| 78 | // HasTrajectory() for the old trajectory, it will return false and the |
| 79 | // drivetrain can free up the fetcher to get the next trajectory. |
| 80 | void UpdateSplineHandles(); |
| 81 | |
| 82 | // Deletes the currently executing trajectory. |
| 83 | void DeleteCurrentSpline(); |
| 84 | |
| 85 | const FinishedTrajectory ¤t_trajectory() const { |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 86 | return *CHECK_NOTNULL(current_trajectory_); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 89 | const DrivetrainConfig<double> dt_config_; |
| 90 | |
Austin Schuh | f7c6520 | 2022-11-04 21:28:20 -0700 | [diff] [blame] | 91 | std::shared_ptr< |
| 92 | StateFeedbackLoop<2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>, |
| 93 | HybridKalman<2, 2, 2>>> |
| 94 | velocity_drivetrain_; |
| 95 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 96 | bool executing_spline_ = false; |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 97 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 98 | // TODO(james): Sort out construction to avoid so much dynamic memory |
| 99 | // allocation... |
| 100 | std::vector<std::unique_ptr<FinishedTrajectory>> trajectories_; |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 101 | const FinishedTrajectory *current_trajectory_ = nullptr; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 102 | |
| 103 | std::optional<int> commanded_spline_; |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 104 | |
| 105 | // State required to compute the next iteration's output. |
| 106 | ::Eigen::Matrix<double, 3, 1> current_xva_, next_xva_; |
| 107 | ::Eigen::Matrix<double, 2, 1> next_U_; |
| 108 | |
| 109 | // Information used for status message. |
| 110 | ::Eigen::Matrix<double, 2, 1> uncapped_U_; |
| 111 | bool enable_ = false; |
| 112 | bool output_was_capped_ = false; |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | } // namespace drivetrain |
| 116 | } // namespace control_loops |
| 117 | } // namespace frc971 |
| 118 | |
| 119 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_SPLINEDRIVETRAIN_H_ |