blob: 7a0b7fa7323e037f541a614ea903ee446678be32 [file] [log] [blame]
Austin Schuh473a5652017-02-05 01:30:42 -08001#ifndef FRC971_CONTROL_LOOPS_PROFILED_SUBSYSTEM_H_
2#define FRC971_CONTROL_LOOPS_PROFILED_SUBSYSTEM_H_
3
4#include <array>
Austin Schuh3634ed32017-02-05 16:28:49 -08005#include <chrono>
Austin Schuh473a5652017-02-05 01:30:42 -08006#include <memory>
7#include <utility>
8
9#include "Eigen/Dense"
Philipp Schrader790cb542023-07-05 21:06:52 -070010
John Park33858a32018-09-28 23:05:48 -070011#include "aos/util/trapezoid_profile.h"
Tyler Chatowd3afdef2019-04-06 22:15:26 -070012#include "frc971/constants.h"
Austin Schuh482a9142022-02-23 16:54:39 -080013#include "frc971/control_loops/control_loop.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070014#include "frc971/control_loops/control_loops_generated.h"
15#include "frc971/control_loops/profiled_subsystem_generated.h"
Austin Schuh473a5652017-02-05 01:30:42 -080016#include "frc971/control_loops/simple_capped_state_feedback_loop.h"
17#include "frc971/control_loops/state_feedback_loop.h"
James Kuszmaulec635d22023-08-12 18:39:24 -070018#include "frc971/zeroing/pot_and_index.h"
Austin Schuh473a5652017-02-05 01:30:42 -080019#include "frc971/zeroing/zeroing.h"
20
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080021namespace frc971::control_loops {
Austin Schuh473a5652017-02-05 01:30:42 -080022
Brian Silvermanab0b6772017-02-05 16:16:21 -080023// TODO(Brian): Use a tuple instead of an array to support heterogeneous zeroing
24// styles.
25template <int number_of_states, int number_of_axes,
26 class ZeroingEstimator =
Austin Schuh08d9ecf2017-03-05 00:58:48 -080027 ::frc971::zeroing::PotAndIndexPulseZeroingEstimator,
28 int number_of_inputs = number_of_axes,
29 int number_of_outputs = number_of_axes>
Austin Schuh473a5652017-02-05 01:30:42 -080030class ProfiledSubsystem {
31 public:
32 ProfiledSubsystem(
33 ::std::unique_ptr<::frc971::control_loops::SimpleCappedStateFeedbackLoop<
Austin Schuh08d9ecf2017-03-05 00:58:48 -080034 number_of_states, number_of_inputs, number_of_outputs>>
35 loop,
Brian Silvermanab0b6772017-02-05 16:16:21 -080036 ::std::array<ZeroingEstimator, number_of_axes> &&estimators)
Austin Schuh473a5652017-02-05 01:30:42 -080037 : loop_(::std::move(loop)), estimators_(::std::move(estimators)) {
38 zeroed_.fill(false);
39 unprofiled_goal_.setZero();
Austin Schuh2669ece2022-03-11 18:30:57 -080040 X_hat_.setZero();
Austin Schuh473a5652017-02-05 01:30:42 -080041 }
42
43 // Returns whether an error has occured
44 bool error() const {
45 for (const auto &estimator : estimators_) {
46 if (estimator.error()) {
47 return true;
48 }
49 }
50 return false;
51 }
52
53 void Reset() {
54 zeroed_.fill(false);
Austin Schuh3a81d5c2017-02-05 23:13:47 -080055 initialized_ = false;
Austin Schuh473a5652017-02-05 01:30:42 -080056 for (auto &estimator : estimators_) {
57 estimator.Reset();
58 }
Austin Schuhd5ccb862017-03-11 22:06:36 -080059 should_reset_ = true;
Austin Schuh473a5652017-02-05 01:30:42 -080060 }
61
62 // Returns the controller.
Austin Schuh50e3dca2023-07-23 14:34:27 -070063 const StateFeedbackLoop<number_of_states, number_of_inputs,
64 number_of_outputs> &
65 controller() const {
Austin Schuh473a5652017-02-05 01:30:42 -080066 return *loop_;
67 }
68
Austin Schuhc5fceb82017-02-25 16:24:12 -080069 int controller_index() const { return loop_->index(); }
Austin Schuh473a5652017-02-05 01:30:42 -080070
Tyler Chatowd3afdef2019-04-06 22:15:26 -070071 void set_controller_index(int index) { loop_->set_index(index); }
72
Austin Schuh473a5652017-02-05 01:30:42 -080073 // Returns whether the estimators have been initialized and zeroed.
74 bool initialized() const { return initialized_; }
75
76 bool zeroed() const {
77 for (int i = 0; i < number_of_axes; ++i) {
78 if (!zeroed_[i]) {
79 return false;
80 }
81 }
82 return true;
83 }
84
85 bool zeroed(int index) const { return zeroed_[index]; };
86
87 // Returns the filtered goal.
88 const Eigen::Matrix<double, number_of_states, 1> &goal() const {
89 return loop_->R();
90 }
91 double goal(int row, int col) const { return loop_->R(row, col); }
92
93 // Returns the unprofiled goal.
94 const Eigen::Matrix<double, number_of_states, 1> &unprofiled_goal() const {
95 return unprofiled_goal_;
96 }
97 double unprofiled_goal(int row, int col) const {
98 return unprofiled_goal_(row, col);
99 }
100
Austin Schuh2669ece2022-03-11 18:30:57 -0800101 // Returns the current state estimate after the most recent Correct. This
102 // does not change when Predict is run.
Austin Schuh473a5652017-02-05 01:30:42 -0800103 const Eigen::Matrix<double, number_of_states, 1> &X_hat() const {
Austin Schuh2669ece2022-03-11 18:30:57 -0800104 return X_hat_;
Austin Schuh473a5652017-02-05 01:30:42 -0800105 }
Austin Schuh2669ece2022-03-11 18:30:57 -0800106 double X_hat(int row, int col) const { return X_hat()(row, col); }
107 // Returns a mutable reference to the current state of the actual kalman
108 // filter state. Note: changing this won't change X_hat() immediately.
Austin Schuhd5ccb862017-03-11 22:06:36 -0800109 double &mutable_X_hat(int row, int col) const {
110 return loop_->mutable_X_hat(row, col);
111 }
Austin Schuh473a5652017-02-05 01:30:42 -0800112
113 // Returns the current internal estimator state for logging.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700114 flatbuffers::Offset<typename ZeroingEstimator::State> EstimatorState(
115 flatbuffers::FlatBufferBuilder *fbb, int index) {
116 return estimators_[index].GetEstimatorState(fbb);
Austin Schuh473a5652017-02-05 01:30:42 -0800117 }
118
119 // Sets the maximum voltage that will be commanded by the loop.
Austin Schuh08d9ecf2017-03-05 00:58:48 -0800120 void set_max_voltage(::std::array<double, number_of_inputs> voltages) {
121 for (int i = 0; i < number_of_inputs; ++i) {
Austin Schuh473a5652017-02-05 01:30:42 -0800122 loop_->set_max_voltage(i, voltages[i]);
123 }
124 }
125
126 protected:
127 void set_zeroed(int index, bool val) { zeroed_[index] = val; }
128
Austin Schuh473a5652017-02-05 01:30:42 -0800129 ::std::unique_ptr<::frc971::control_loops::SimpleCappedStateFeedbackLoop<
Austin Schuh08d9ecf2017-03-05 00:58:48 -0800130 number_of_states, number_of_inputs, number_of_outputs>>
131 loop_;
Austin Schuh473a5652017-02-05 01:30:42 -0800132
133 // The goal that the profile tries to reach.
134 Eigen::Matrix<double, number_of_states, 1> unprofiled_goal_;
135
Austin Schuh2669ece2022-03-11 18:30:57 -0800136 Eigen::Matrix<double, number_of_states, 1> X_hat_;
137
Austin Schuh473a5652017-02-05 01:30:42 -0800138 bool initialized_ = false;
139
Austin Schuhd5ccb862017-03-11 22:06:36 -0800140 // If true, the subclass should reset in Update. It should then clear this
141 // flag.
142 bool should_reset_ = true;
143
Brian Silvermanab0b6772017-02-05 16:16:21 -0800144 ::std::array<ZeroingEstimator, number_of_axes> estimators_;
Austin Schuh473a5652017-02-05 01:30:42 -0800145
146 private:
147 ::std::array<bool, number_of_axes> zeroed_;
148};
149
Adam Snaider79900c22017-02-08 20:23:15 -0800150template <typename ZeroingEstimator =
Brian Silvermanab0b6772017-02-05 16:16:21 -0800151 ::frc971::zeroing::PotAndIndexPulseZeroingEstimator>
Austin Schuh473a5652017-02-05 01:30:42 -0800152class SingleDOFProfiledSubsystem
Tyler Chatowd3afdef2019-04-06 22:15:26 -0700153 : public ::frc971::control_loops::ProfiledSubsystem<3, 1,
154 ZeroingEstimator> {
Austin Schuh473a5652017-02-05 01:30:42 -0800155 public:
156 SingleDOFProfiledSubsystem(
157 ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop,
Brian Silvermanab0b6772017-02-05 16:16:21 -0800158 const typename ZeroingEstimator::ZeroingConstants &zeroing_constants,
Austin Schuh473a5652017-02-05 01:30:42 -0800159 const ::frc971::constants::Range &range, double default_angular_velocity,
160 double default_angular_acceleration);
161
162 // Updates our estimator with the latest position.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700163 void Correct(const typename ZeroingEstimator::Position &position);
Austin Schuh482a9142022-02-23 16:54:39 -0800164 // Runs the controller and profile generator for a cycle. This is equivilent
165 // to calling UpdateObserver(UpdateController()) with the rest of the syntax
166 // actually right.
167 double Update(bool disabled);
168 // Just computes the controller and pushes the feed forwards forwards 1 step.
169 double UpdateController(bool disabled);
170 // Updates the observer with the computed U.
171 // Note: if this is the only method called, ForceGoal should also be called to
172 // move the state to match.
173 void UpdateObserver(double voltage);
Austin Schuh473a5652017-02-05 01:30:42 -0800174
Austin Schuh3634ed32017-02-05 16:28:49 -0800175 // Fills out the ProfiledJointStatus structure with the current state.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700176 template <class StatusTypeBuilder>
Austin Schuh482a9142022-02-23 16:54:39 -0800177 StatusTypeBuilder BuildStatus(flatbuffers::FlatBufferBuilder *fbb);
Austin Schuh3634ed32017-02-05 16:28:49 -0800178
Austin Schuh473a5652017-02-05 01:30:42 -0800179 // Forces the current goal to the provided goal, bypassing the profiler.
Austin Schuh482a9142022-02-23 16:54:39 -0800180 void ForceGoal(double goal, double goal_velocity = 0.0);
James Kuszmaul4fb29762020-02-20 19:37:41 -0800181 // Sets whether to use the trapezoidal profiler or whether to just bypass it
182 // and pass the unprofiled goal through directly.
183 void set_enable_profile(bool enable) { enable_profile_ = enable; }
Austin Schuh473a5652017-02-05 01:30:42 -0800184 // Sets the unprofiled goal. The profiler will generate a profile to go to
185 // this goal.
James Kuszmaul4fb29762020-02-20 19:37:41 -0800186 void set_unprofiled_goal(double unprofiled_goal,
Sabina Davis0e484f92020-02-23 17:47:53 -0800187 double unprofiled_goal_velocity = 0.0,
188 bool print = true);
Austin Schuh473a5652017-02-05 01:30:42 -0800189 // Limits our profiles to a max velocity and acceleration for proper motion.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700190 void AdjustProfile(const ::frc971::ProfileParameters *profile_parameters);
Austin Schuh473a5652017-02-05 01:30:42 -0800191 void AdjustProfile(double max_angular_velocity,
192 double max_angular_acceleration);
193
194 // Returns true if we have exceeded any hard limits.
195 bool CheckHardLimits();
196
Austin Schuh3634ed32017-02-05 16:28:49 -0800197 // Returns the requested voltage.
Adam Snaider79900c22017-02-08 20:23:15 -0800198 double voltage() const { return this->loop_->U(0, 0); }
Austin Schuh473a5652017-02-05 01:30:42 -0800199
Austin Schuh482a9142022-02-23 16:54:39 -0800200 // Returns the current position or velocity.
Adam Snaider79900c22017-02-08 20:23:15 -0800201 double position() const { return this->Y_(0, 0); }
Austin Schuh473a5652017-02-05 01:30:42 -0800202
203 // For testing:
204 // Triggers an estimator error.
Adam Snaider79900c22017-02-08 20:23:15 -0800205 void TriggerEstimatorError() { this->estimators_[0].TriggerError(); }
Austin Schuh473a5652017-02-05 01:30:42 -0800206
Austin Schuh00be3a82017-02-05 19:01:40 -0800207 const ::frc971::constants::Range &range() const { return range_; }
208
Austin Schuh93ddcb42021-10-25 21:54:11 -0700209 double default_velocity() const { return default_velocity_; }
210 double default_acceleration() const { return default_acceleration_; }
211
Austin Schuh6a90cd92017-02-19 20:55:33 -0800212 protected:
Austin Schuh473a5652017-02-05 01:30:42 -0800213 // Limits the provided goal to the soft limits. Prints "name" when it fails
214 // to aid debugging.
Sabina Davis0e484f92020-02-23 17:47:53 -0800215 virtual void CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal,
216 bool print = true);
Austin Schuh473a5652017-02-05 01:30:42 -0800217
Austin Schuh6a90cd92017-02-19 20:55:33 -0800218 private:
Austin Schuh473a5652017-02-05 01:30:42 -0800219 void UpdateOffset(double offset);
220
221 aos::util::TrapezoidProfile profile_;
James Kuszmaul4fb29762020-02-20 19:37:41 -0800222 bool enable_profile_ = true;
Austin Schuh473a5652017-02-05 01:30:42 -0800223
224 // Current measurement.
225 Eigen::Matrix<double, 1, 1> Y_;
226 // Current offset. Y_ = offset_ + raw_sensor;
227 Eigen::Matrix<double, 1, 1> offset_;
228
229 const ::frc971::constants::Range range_;
230
231 const double default_velocity_;
232 const double default_acceleration_;
Austin Schuh3634ed32017-02-05 16:28:49 -0800233
234 double last_position_ = 0;
Austin Schuh473a5652017-02-05 01:30:42 -0800235};
236
Brian Silvermanab0b6772017-02-05 16:16:21 -0800237namespace internal {
238
239double UseUnlessZero(double target_value, double default_value);
240
241} // namespace internal
242
243template <class ZeroingEstimator>
244SingleDOFProfiledSubsystem<ZeroingEstimator>::SingleDOFProfiledSubsystem(
245 ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop,
246 const typename ZeroingEstimator::ZeroingConstants &zeroing_constants,
247 const ::frc971::constants::Range &range, double default_velocity,
248 double default_acceleration)
Campbell Crowley36e93e92017-12-23 14:21:43 -0800249 : ProfiledSubsystem<3, 1, ZeroingEstimator>(
250 ::std::move(loop), {{ZeroingEstimator(zeroing_constants)}}),
Sabina Davis1184cc52020-02-22 18:29:25 -0800251 profile_(this->loop_->plant().coefficients().dt),
Brian Silvermanab0b6772017-02-05 16:16:21 -0800252 range_(range),
253 default_velocity_(default_velocity),
254 default_acceleration_(default_acceleration) {
255 Y_.setZero();
256 offset_.setZero();
257 AdjustProfile(0.0, 0.0);
258}
259
260template <class ZeroingEstimator>
261void SingleDOFProfiledSubsystem<ZeroingEstimator>::UpdateOffset(double offset) {
262 const double doffset = offset - offset_(0, 0);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700263 AOS_LOG(INFO, "Adjusting offset from %f to %f\n", offset_(0, 0), offset);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800264
Adam Snaider79900c22017-02-08 20:23:15 -0800265 this->loop_->mutable_X_hat()(0, 0) += doffset;
266 this->Y_(0, 0) += doffset;
Austin Schuh3634ed32017-02-05 16:28:49 -0800267 last_position_ += doffset;
Adam Snaider79900c22017-02-08 20:23:15 -0800268 this->loop_->mutable_R(0, 0) += doffset;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800269
270 profile_.MoveGoal(doffset);
271 offset_(0, 0) = offset;
272
Adam Snaider79900c22017-02-08 20:23:15 -0800273 CapGoal("R", &this->loop_->mutable_R());
Brian Silvermanab0b6772017-02-05 16:16:21 -0800274}
275
276template <class ZeroingEstimator>
Alex Perrycb7da4b2019-08-28 19:35:56 -0700277template <class StatusTypeBuilder>
278StatusTypeBuilder SingleDOFProfiledSubsystem<ZeroingEstimator>::BuildStatus(
279 flatbuffers::FlatBufferBuilder *fbb) {
280 flatbuffers::Offset<typename ZeroingEstimator::State> estimator_state =
281 this->EstimatorState(fbb, 0);
282
283 StatusTypeBuilder builder(*fbb);
284
285 builder.add_zeroed(this->zeroed());
Austin Schuh3634ed32017-02-05 16:28:49 -0800286 // We don't know, so default to the bad case.
Austin Schuh3634ed32017-02-05 16:28:49 -0800287
Austin Schuh2669ece2022-03-11 18:30:57 -0800288 builder.add_position(this->X_hat_(0, 0));
289 builder.add_velocity(this->X_hat_(1, 0));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700290 builder.add_goal_position(this->goal(0, 0));
291 builder.add_goal_velocity(this->goal(1, 0));
292 builder.add_unprofiled_goal_position(this->unprofiled_goal(0, 0));
293 builder.add_unprofiled_goal_velocity(this->unprofiled_goal(1, 0));
Austin Schuh2669ece2022-03-11 18:30:57 -0800294 builder.add_voltage_error(this->X_hat_(2, 0));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700295 builder.add_calculated_velocity(
Austin Schuh3634ed32017-02-05 16:28:49 -0800296 (position() - last_position_) /
Sabina Davis1184cc52020-02-22 18:29:25 -0800297 ::aos::time::DurationInSeconds(this->loop_->plant().coefficients().dt));
Austin Schuh3634ed32017-02-05 16:28:49 -0800298
Alex Perrycb7da4b2019-08-28 19:35:56 -0700299 builder.add_estimator_state(estimator_state);
Austin Schuh3634ed32017-02-05 16:28:49 -0800300
Adam Snaider79900c22017-02-08 20:23:15 -0800301 Eigen::Matrix<double, 3, 1> error = this->controller().error();
Austin Schuh482a9142022-02-23 16:54:39 -0800302 builder.add_position_power(this->controller().controller().K(0, 0) *
303 error(0, 0));
304 builder.add_velocity_power(this->controller().controller().K(0, 1) *
305 error(1, 0));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700306 return builder;
Austin Schuh3634ed32017-02-05 16:28:49 -0800307}
308
309template <class ZeroingEstimator>
Brian Silvermanab0b6772017-02-05 16:16:21 -0800310void SingleDOFProfiledSubsystem<ZeroingEstimator>::Correct(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700311 const typename ZeroingEstimator::Position &new_position) {
Adam Snaider79900c22017-02-08 20:23:15 -0800312 this->estimators_[0].UpdateEstimate(new_position);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800313
Adam Snaider79900c22017-02-08 20:23:15 -0800314 if (this->estimators_[0].error()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700315 AOS_LOG(ERROR, "zeroing error\n");
Austin Schuh2669ece2022-03-11 18:30:57 -0800316 this->X_hat_ = this->loop_->X_hat();
Brian Silvermanab0b6772017-02-05 16:16:21 -0800317 return;
318 }
319
Adam Snaider79900c22017-02-08 20:23:15 -0800320 if (!this->initialized_) {
321 if (this->estimators_[0].offset_ready()) {
322 UpdateOffset(this->estimators_[0].offset());
323 this->initialized_ = true;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800324 }
325 }
326
Adam Snaider79900c22017-02-08 20:23:15 -0800327 if (!this->zeroed(0) && this->estimators_[0].zeroed()) {
328 UpdateOffset(this->estimators_[0].offset());
329 this->set_zeroed(0, true);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800330 }
331
Austin Schuh3634ed32017-02-05 16:28:49 -0800332 last_position_ = position();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700333 this->Y_ << new_position.encoder();
Adam Snaider79900c22017-02-08 20:23:15 -0800334 this->Y_ += this->offset_;
335 this->loop_->Correct(Y_);
Austin Schuh2669ece2022-03-11 18:30:57 -0800336 this->X_hat_ = this->loop_->X_hat();
Brian Silvermanab0b6772017-02-05 16:16:21 -0800337}
338
339template <class ZeroingEstimator>
340void SingleDOFProfiledSubsystem<ZeroingEstimator>::CapGoal(
Sabina Davis0e484f92020-02-23 17:47:53 -0800341 const char *name, Eigen::Matrix<double, 3, 1> *goal, bool print) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800342 // Limit the goal to min/max allowable positions.
Brian Silvermanab0b6772017-02-05 16:16:21 -0800343 if ((*goal)(0, 0) > range_.upper) {
Sabina Davis0e484f92020-02-23 17:47:53 -0800344 if (print) {
345 AOS_LOG(WARNING, "Goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
346 range_.upper);
347 }
Brian Silvermanab0b6772017-02-05 16:16:21 -0800348 (*goal)(0, 0) = range_.upper;
349 }
350 if ((*goal)(0, 0) < range_.lower) {
Sabina Davis0e484f92020-02-23 17:47:53 -0800351 if (print) {
352 AOS_LOG(WARNING, "Goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
353 range_.lower);
354 }
Brian Silvermanab0b6772017-02-05 16:16:21 -0800355 (*goal)(0, 0) = range_.lower;
356 }
357}
358
359template <class ZeroingEstimator>
Austin Schuh482a9142022-02-23 16:54:39 -0800360void SingleDOFProfiledSubsystem<ZeroingEstimator>::ForceGoal(
361 double goal, double goal_velocity) {
362 set_unprofiled_goal(goal, goal_velocity, false);
Adam Snaider79900c22017-02-08 20:23:15 -0800363 this->loop_->mutable_R() = this->unprofiled_goal_;
364 this->loop_->mutable_next_R() = this->loop_->R();
Brian Silvermanab0b6772017-02-05 16:16:21 -0800365
Adam Snaider79900c22017-02-08 20:23:15 -0800366 const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R();
367 this->profile_.MoveCurrentState(R.block<2, 1>(0, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800368}
369
370template <class ZeroingEstimator>
371void SingleDOFProfiledSubsystem<ZeroingEstimator>::set_unprofiled_goal(
Sabina Davis0e484f92020-02-23 17:47:53 -0800372 double unprofiled_goal, double unprofiled_goal_velocity, bool print) {
Adam Snaider79900c22017-02-08 20:23:15 -0800373 this->unprofiled_goal_(0, 0) = unprofiled_goal;
James Kuszmaul4fb29762020-02-20 19:37:41 -0800374 this->unprofiled_goal_(1, 0) = unprofiled_goal_velocity;
Adam Snaider79900c22017-02-08 20:23:15 -0800375 this->unprofiled_goal_(2, 0) = 0.0;
Sabina Davis0e484f92020-02-23 17:47:53 -0800376 CapGoal("unprofiled R", &this->unprofiled_goal_, print);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800377}
378
379template <class ZeroingEstimator>
Austin Schuh482a9142022-02-23 16:54:39 -0800380double SingleDOFProfiledSubsystem<ZeroingEstimator>::UpdateController(
381 bool disable) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800382 // TODO(austin): What do we want to do with the profile on reset? Also, we
383 // should probably reset R, the offset, the profile, etc.
384 if (this->should_reset_) {
385 this->loop_->mutable_X_hat(0, 0) = Y_(0, 0);
386 this->loop_->mutable_X_hat(1, 0) = 0.0;
387 this->loop_->mutable_X_hat(2, 0) = 0.0;
Austin Schuh2669ece2022-03-11 18:30:57 -0800388 this->X_hat_.setZero();
Austin Schuhd5ccb862017-03-11 22:06:36 -0800389 this->should_reset_ = false;
390 }
391
Brian Silvermanab0b6772017-02-05 16:16:21 -0800392 if (!disable) {
James Kuszmaul4fb29762020-02-20 19:37:41 -0800393 if (enable_profile_) {
394 ::Eigen::Matrix<double, 2, 1> goal_state = profile_.Update(
395 this->unprofiled_goal_(0, 0), this->unprofiled_goal_(1, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800396
James Kuszmaul4fb29762020-02-20 19:37:41 -0800397 this->loop_->mutable_next_R(0, 0) = goal_state(0, 0);
398 this->loop_->mutable_next_R(1, 0) = goal_state(1, 0);
399 this->loop_->mutable_next_R(2, 0) = 0.0;
400 } else {
401 this->loop_->mutable_R() = this->unprofiled_goal_;
402 this->loop_->mutable_next_R() = this->unprofiled_goal_;
403 this->loop_->mutable_next_R(0, 0) +=
404 this->unprofiled_goal_(1) *
405 aos::time::DurationInSeconds(this->loop_->plant().coefficients().dt);
406 CapGoal("R", &this->loop_->mutable_R());
407 }
Adam Snaider79900c22017-02-08 20:23:15 -0800408 CapGoal("next R", &this->loop_->mutable_next_R());
Brian Silvermanab0b6772017-02-05 16:16:21 -0800409 }
410
Austin Schuh482a9142022-02-23 16:54:39 -0800411 this->loop_->UpdateController(disable);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800412
Adam Snaider79900c22017-02-08 20:23:15 -0800413 if (!disable && this->loop_->U(0, 0) != this->loop_->U_uncapped(0, 0)) {
414 const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R();
415 profile_.MoveCurrentState(R.block<2, 1>(0, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800416 }
Austin Schuh482a9142022-02-23 16:54:39 -0800417
418 return this->loop_->U(0, 0);
419}
420
421template <class ZeroingEstimator>
422void SingleDOFProfiledSubsystem<ZeroingEstimator>::UpdateObserver(
423 double voltage) {
424 this->loop_->mutable_U(0, 0) = voltage;
425 this->loop_->UpdateObserver(this->loop_->U(), this->loop_->plant().dt());
426}
427
428template <class ZeroingEstimator>
429double SingleDOFProfiledSubsystem<ZeroingEstimator>::Update(bool disable) {
430 const double voltage = UpdateController(disable);
431 UpdateObserver(voltage);
432 return voltage;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800433}
434
435template <class ZeroingEstimator>
436bool SingleDOFProfiledSubsystem<ZeroingEstimator>::CheckHardLimits() {
437 // Returns whether hard limits have been exceeded.
438
Austin Schuh3634ed32017-02-05 16:28:49 -0800439 if (position() > range_.upper_hard || position() < range_.lower_hard) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700440 AOS_LOG(
441 ERROR,
Brian Silvermanab0b6772017-02-05 16:16:21 -0800442 "SingleDOFProfiledSubsystem at %f out of bounds [%f, %f], ESTOPing\n",
Austin Schuh3634ed32017-02-05 16:28:49 -0800443 position(), range_.lower_hard, range_.upper_hard);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800444 return true;
445 }
446
447 return false;
448}
449
450template <class ZeroingEstimator>
451void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700452 const ::frc971::ProfileParameters *profile_parameters) {
453 AdjustProfile(
454 profile_parameters != nullptr ? profile_parameters->max_velocity() : 0.0,
455 profile_parameters != nullptr ? profile_parameters->max_acceleration()
456 : 0.0);
Austin Schuh3634ed32017-02-05 16:28:49 -0800457}
458
459template <class ZeroingEstimator>
460void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Brian Silvermanab0b6772017-02-05 16:16:21 -0800461 double max_angular_velocity, double max_angular_acceleration) {
462 profile_.set_maximum_velocity(
463 internal::UseUnlessZero(max_angular_velocity, default_velocity_));
464 profile_.set_maximum_acceleration(
465 internal::UseUnlessZero(max_angular_acceleration, default_acceleration_));
466}
467
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800468} // namespace frc971::control_loops
Austin Schuh473a5652017-02-05 01:30:42 -0800469
470#endif // FRC971_CONTROL_LOOPS_PROFILED_SUBSYSTEM_H_