blob: cd712bc375050b86038412ae6b4248e9cd9fc63f [file] [log] [blame]
Alex Perry731b4602019-02-02 22:13:01 -08001#ifndef FRC971_CONTROL_LOOPS_DRIVETRAIN_SPLINEDRIVETRAIN_H_
2#define FRC971_CONTROL_LOOPS_DRIVETRAIN_SPLINEDRIVETRAIN_H_
3
Alex Perrycc3ee4c2019-02-09 21:20:41 -08004#include <atomic>
5#include <thread>
6
Alex Perrya71badb2019-02-06 19:40:41 -08007#include "Eigen/Dense"
Alex Perrycc3ee4c2019-02-09 21:20:41 -08008#include "aos/condition.h"
9#include "aos/mutex/mutex.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070010#include "frc971/control_loops/control_loops_generated.h"
Alex Perry731b4602019-02-02 22:13:01 -080011#include "frc971/control_loops/drivetrain/distance_spline.h"
Alex Perry731b4602019-02-02 22:13:01 -080012#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070013#include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h"
14#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
15#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
Alex Perry731b4602019-02-02 22:13:01 -080016#include "frc971/control_loops/drivetrain/spline.h"
17#include "frc971/control_loops/drivetrain/trajectory.h"
18
19namespace frc971 {
20namespace control_loops {
21namespace drivetrain {
22
23class SplineDrivetrain {
24 public:
James Kuszmauldc534432023-02-05 14:51:11 -080025 static constexpr size_t kMaxTrajectories = 5;
Alex Perry731b4602019-02-02 22:13:01 -080026 SplineDrivetrain(const DrivetrainConfig<double> &dt_config);
27
Alex Perrycb7da4b2019-08-28 19:35:56 -070028 void SetGoal(const ::frc971::control_loops::drivetrain::Goal *goal);
James Kuszmaul75a18c52021-03-10 22:02:07 -080029 // 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 Kuszmaul99af8b52021-03-28 10:50:15 -070035 bool IsCurrentTrajectory(const fb::Trajectory *trajectory) const;
36 void DeleteTrajectory(const fb::Trajectory *trajectory);
Alex Perry731b4602019-02-02 22:13:01 -080037
James Kuszmaulaa2499d2020-06-02 21:31:19 -070038 void Update(bool enabled, const ::Eigen::Matrix<double, 5, 1> &state,
39 const ::Eigen::Matrix<double, 2, 1> &voltage_error);
Alex Perrya71badb2019-02-06 19:40:41 -080040
Alex Perrycb7da4b2019-08-28 19:35:56 -070041 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 Schuhd749d932020-12-30 21:38:40 -080047 void PopulateStatus(drivetrain::Status::Builder *status) const;
James Kuszmaul1057ce82019-02-09 17:58:24 -080048
49 // Accessor for the current goal state, pretty much only present for debugging
50 // purposes.
Alex Perrycc3ee4c2019-02-09 21:20:41 -080051 ::Eigen::Matrix<double, 5, 1> CurrentGoalState() const {
James Kuszmauldc534432023-02-05 14:51:11 -080052 return executing_spline_ ? CHECK_NOTNULL(current_trajectory())
53 ->GoalState(current_xva_(0), current_xva_(1))
James Kuszmaul75a18c52021-03-10 22:02:07 -080054 : ::Eigen::Matrix<double, 5, 1>::Zero();
James Kuszmaul1057ce82019-02-09 17:58:24 -080055 }
56
57 bool IsAtEnd() const {
James Kuszmauldc534432023-02-05 14:51:11 -080058 return executing_spline_ ? CHECK_NOTNULL(current_trajectory())
59 ->is_at_end(current_xva_.block<2, 1>(0, 0))
60 : true;
James Kuszmaul1057ce82019-02-09 17:58:24 -080061 }
Alex Perrycc3ee4c2019-02-09 21:20:41 -080062
James Kuszmaul99af8b52021-03-28 10:50:15 -070063 size_t trajectory_count() const { return trajectories_.size(); }
64
Alex Perrycb7da4b2019-08-28 19:35:56 -070065 // Returns true if the splinedrivetrain is enabled.
66 bool enable() const { return enable_; }
67
Alex Perry731b4602019-02-02 22:13:01 -080068 private:
Alex Perrye32eabc2019-02-08 19:51:19 -080069 void ScaleCapU(Eigen::Matrix<double, 2, 1> *U);
70
James Kuszmaul75a18c52021-03-10 22:02:07 -080071 // 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
James Kuszmauldc534432023-02-05 14:51:11 -080073 // commanded_spline to determine whether we are being commanded to run a
James Kuszmaul75a18c52021-03-10 22:02:07 -080074 // spline, and if there is any trajectory in the list of trajectories matching
James Kuszmauldc534432023-02-05 14:51:11 -080075 // the command, we begin/continue executing that spline. If commanded_spline
James Kuszmaul75a18c52021-03-10 22:02:07 -080076 // 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.
James Kuszmauldc534432023-02-05 14:51:11 -080080 void UpdateSplineHandles(std::optional<int> commanded_spline);
James Kuszmaul75a18c52021-03-10 22:02:07 -080081
82 // Deletes the currently executing trajectory.
83 void DeleteCurrentSpline();
84
James Kuszmauldc534432023-02-05 14:51:11 -080085 FinishedTrajectory *current_trajectory();
86 const FinishedTrajectory *current_trajectory() const;
James Kuszmaul75a18c52021-03-10 22:02:07 -080087
Alex Perry731b4602019-02-02 22:13:01 -080088 const DrivetrainConfig<double> dt_config_;
89
Austin Schuhf7c65202022-11-04 21:28:20 -070090 std::shared_ptr<
91 StateFeedbackLoop<2, 2, 2, double, StateFeedbackHybridPlant<2, 2, 2>,
92 HybridKalman<2, 2, 2>>>
93 velocity_drivetrain_;
94
James Kuszmaul75a18c52021-03-10 22:02:07 -080095 bool executing_spline_ = false;
Alex Perrye32eabc2019-02-08 19:51:19 -080096
James Kuszmaul75a18c52021-03-10 22:02:07 -080097 // TODO(james): Sort out construction to avoid so much dynamic memory
98 // allocation...
James Kuszmauldc534432023-02-05 14:51:11 -080099 aos::SizedArray<FinishedTrajectory, kMaxTrajectories> trajectories_;
James Kuszmaul75a18c52021-03-10 22:02:07 -0800100
101 std::optional<int> commanded_spline_;
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800102
103 // State required to compute the next iteration's output.
104 ::Eigen::Matrix<double, 3, 1> current_xva_, next_xva_;
105 ::Eigen::Matrix<double, 2, 1> next_U_;
106
107 // Information used for status message.
108 ::Eigen::Matrix<double, 2, 1> uncapped_U_;
109 bool enable_ = false;
110 bool output_was_capped_ = false;
Alex Perry731b4602019-02-02 22:13:01 -0800111};
112
113} // namespace drivetrain
114} // namespace control_loops
115} // namespace frc971
116
117#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_SPLINEDRIVETRAIN_H_