blob: 13437703b92cbec0b8a2824416489c8a8c9ccf3a [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"
John Park33858a32018-09-28 23:05:48 -070010#include "aos/util/trapezoid_profile.h"
Tyler Chatowd3afdef2019-04-06 22:15:26 -070011#include "frc971/constants.h"
Austin Schuh482a9142022-02-23 16:54:39 -080012#include "frc971/control_loops/control_loop.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070013#include "frc971/control_loops/control_loops_generated.h"
14#include "frc971/control_loops/profiled_subsystem_generated.h"
Austin Schuh473a5652017-02-05 01:30:42 -080015#include "frc971/control_loops/simple_capped_state_feedback_loop.h"
16#include "frc971/control_loops/state_feedback_loop.h"
17#include "frc971/zeroing/zeroing.h"
18
19namespace frc971 {
20namespace control_loops {
21
Brian Silvermanab0b6772017-02-05 16:16:21 -080022// TODO(Brian): Use a tuple instead of an array to support heterogeneous zeroing
23// styles.
24template <int number_of_states, int number_of_axes,
25 class ZeroingEstimator =
Austin Schuh08d9ecf2017-03-05 00:58:48 -080026 ::frc971::zeroing::PotAndIndexPulseZeroingEstimator,
27 int number_of_inputs = number_of_axes,
28 int number_of_outputs = number_of_axes>
Austin Schuh473a5652017-02-05 01:30:42 -080029class ProfiledSubsystem {
30 public:
31 ProfiledSubsystem(
32 ::std::unique_ptr<::frc971::control_loops::SimpleCappedStateFeedbackLoop<
Austin Schuh08d9ecf2017-03-05 00:58:48 -080033 number_of_states, number_of_inputs, number_of_outputs>>
34 loop,
Brian Silvermanab0b6772017-02-05 16:16:21 -080035 ::std::array<ZeroingEstimator, number_of_axes> &&estimators)
Austin Schuh473a5652017-02-05 01:30:42 -080036 : loop_(::std::move(loop)), estimators_(::std::move(estimators)) {
37 zeroed_.fill(false);
38 unprofiled_goal_.setZero();
Austin Schuh2669ece2022-03-11 18:30:57 -080039 X_hat_.setZero();
Austin Schuh473a5652017-02-05 01:30:42 -080040 }
41
42 // Returns whether an error has occured
43 bool error() const {
44 for (const auto &estimator : estimators_) {
45 if (estimator.error()) {
46 return true;
47 }
48 }
49 return false;
50 }
51
52 void Reset() {
53 zeroed_.fill(false);
Austin Schuh3a81d5c2017-02-05 23:13:47 -080054 initialized_ = false;
Austin Schuh473a5652017-02-05 01:30:42 -080055 for (auto &estimator : estimators_) {
56 estimator.Reset();
57 }
Austin Schuhd5ccb862017-03-11 22:06:36 -080058 should_reset_ = true;
Austin Schuh473a5652017-02-05 01:30:42 -080059 }
60
61 // Returns the controller.
Tyler Chatowd3afdef2019-04-06 22:15:26 -070062 const StateFeedbackLoop<number_of_states, number_of_inputs, number_of_outputs>
63 &controller() const {
Austin Schuh473a5652017-02-05 01:30:42 -080064 return *loop_;
65 }
66
Austin Schuhc5fceb82017-02-25 16:24:12 -080067 int controller_index() const { return loop_->index(); }
Austin Schuh473a5652017-02-05 01:30:42 -080068
Tyler Chatowd3afdef2019-04-06 22:15:26 -070069 void set_controller_index(int index) { loop_->set_index(index); }
70
Austin Schuh473a5652017-02-05 01:30:42 -080071 // Returns whether the estimators have been initialized and zeroed.
72 bool initialized() const { return initialized_; }
73
74 bool zeroed() const {
75 for (int i = 0; i < number_of_axes; ++i) {
76 if (!zeroed_[i]) {
77 return false;
78 }
79 }
80 return true;
81 }
82
83 bool zeroed(int index) const { return zeroed_[index]; };
84
85 // Returns the filtered goal.
86 const Eigen::Matrix<double, number_of_states, 1> &goal() const {
87 return loop_->R();
88 }
89 double goal(int row, int col) const { return loop_->R(row, col); }
90
91 // Returns the unprofiled goal.
92 const Eigen::Matrix<double, number_of_states, 1> &unprofiled_goal() const {
93 return unprofiled_goal_;
94 }
95 double unprofiled_goal(int row, int col) const {
96 return unprofiled_goal_(row, col);
97 }
98
Austin Schuh2669ece2022-03-11 18:30:57 -080099 // Returns the current state estimate after the most recent Correct. This
100 // does not change when Predict is run.
Austin Schuh473a5652017-02-05 01:30:42 -0800101 const Eigen::Matrix<double, number_of_states, 1> &X_hat() const {
Austin Schuh2669ece2022-03-11 18:30:57 -0800102 return X_hat_;
Austin Schuh473a5652017-02-05 01:30:42 -0800103 }
Austin Schuh2669ece2022-03-11 18:30:57 -0800104 double X_hat(int row, int col) const { return X_hat()(row, col); }
105 // Returns a mutable reference to the current state of the actual kalman
106 // filter state. Note: changing this won't change X_hat() immediately.
Austin Schuhd5ccb862017-03-11 22:06:36 -0800107 double &mutable_X_hat(int row, int col) const {
108 return loop_->mutable_X_hat(row, col);
109 }
Austin Schuh473a5652017-02-05 01:30:42 -0800110
111 // Returns the current internal estimator state for logging.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700112 flatbuffers::Offset<typename ZeroingEstimator::State> EstimatorState(
113 flatbuffers::FlatBufferBuilder *fbb, int index) {
114 return estimators_[index].GetEstimatorState(fbb);
Austin Schuh473a5652017-02-05 01:30:42 -0800115 }
116
117 // Sets the maximum voltage that will be commanded by the loop.
Austin Schuh08d9ecf2017-03-05 00:58:48 -0800118 void set_max_voltage(::std::array<double, number_of_inputs> voltages) {
119 for (int i = 0; i < number_of_inputs; ++i) {
Austin Schuh473a5652017-02-05 01:30:42 -0800120 loop_->set_max_voltage(i, voltages[i]);
121 }
122 }
123
124 protected:
125 void set_zeroed(int index, bool val) { zeroed_[index] = val; }
126
Austin Schuh473a5652017-02-05 01:30:42 -0800127 ::std::unique_ptr<::frc971::control_loops::SimpleCappedStateFeedbackLoop<
Austin Schuh08d9ecf2017-03-05 00:58:48 -0800128 number_of_states, number_of_inputs, number_of_outputs>>
129 loop_;
Austin Schuh473a5652017-02-05 01:30:42 -0800130
131 // The goal that the profile tries to reach.
132 Eigen::Matrix<double, number_of_states, 1> unprofiled_goal_;
133
Austin Schuh2669ece2022-03-11 18:30:57 -0800134 Eigen::Matrix<double, number_of_states, 1> X_hat_;
135
Austin Schuh473a5652017-02-05 01:30:42 -0800136 bool initialized_ = false;
137
Austin Schuhd5ccb862017-03-11 22:06:36 -0800138 // If true, the subclass should reset in Update. It should then clear this
139 // flag.
140 bool should_reset_ = true;
141
Brian Silvermanab0b6772017-02-05 16:16:21 -0800142 ::std::array<ZeroingEstimator, number_of_axes> estimators_;
Austin Schuh473a5652017-02-05 01:30:42 -0800143
144 private:
145 ::std::array<bool, number_of_axes> zeroed_;
146};
147
Adam Snaider79900c22017-02-08 20:23:15 -0800148template <typename ZeroingEstimator =
Brian Silvermanab0b6772017-02-05 16:16:21 -0800149 ::frc971::zeroing::PotAndIndexPulseZeroingEstimator>
Austin Schuh473a5652017-02-05 01:30:42 -0800150class SingleDOFProfiledSubsystem
Tyler Chatowd3afdef2019-04-06 22:15:26 -0700151 : public ::frc971::control_loops::ProfiledSubsystem<3, 1,
152 ZeroingEstimator> {
Austin Schuh473a5652017-02-05 01:30:42 -0800153 public:
154 SingleDOFProfiledSubsystem(
155 ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop,
Brian Silvermanab0b6772017-02-05 16:16:21 -0800156 const typename ZeroingEstimator::ZeroingConstants &zeroing_constants,
Austin Schuh473a5652017-02-05 01:30:42 -0800157 const ::frc971::constants::Range &range, double default_angular_velocity,
158 double default_angular_acceleration);
159
160 // Updates our estimator with the latest position.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700161 void Correct(const typename ZeroingEstimator::Position &position);
Austin Schuh482a9142022-02-23 16:54:39 -0800162 // Runs the controller and profile generator for a cycle. This is equivilent
163 // to calling UpdateObserver(UpdateController()) with the rest of the syntax
164 // actually right.
165 double Update(bool disabled);
166 // Just computes the controller and pushes the feed forwards forwards 1 step.
167 double UpdateController(bool disabled);
168 // Updates the observer with the computed U.
169 // Note: if this is the only method called, ForceGoal should also be called to
170 // move the state to match.
171 void UpdateObserver(double voltage);
Austin Schuh473a5652017-02-05 01:30:42 -0800172
Austin Schuh3634ed32017-02-05 16:28:49 -0800173 // Fills out the ProfiledJointStatus structure with the current state.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700174 template <class StatusTypeBuilder>
Austin Schuh482a9142022-02-23 16:54:39 -0800175 StatusTypeBuilder BuildStatus(flatbuffers::FlatBufferBuilder *fbb);
Austin Schuh3634ed32017-02-05 16:28:49 -0800176
Austin Schuh473a5652017-02-05 01:30:42 -0800177 // Forces the current goal to the provided goal, bypassing the profiler.
Austin Schuh482a9142022-02-23 16:54:39 -0800178 void ForceGoal(double goal, double goal_velocity = 0.0);
James Kuszmaul4fb29762020-02-20 19:37:41 -0800179 // Sets whether to use the trapezoidal profiler or whether to just bypass it
180 // and pass the unprofiled goal through directly.
181 void set_enable_profile(bool enable) { enable_profile_ = enable; }
Austin Schuh473a5652017-02-05 01:30:42 -0800182 // Sets the unprofiled goal. The profiler will generate a profile to go to
183 // this goal.
James Kuszmaul4fb29762020-02-20 19:37:41 -0800184 void set_unprofiled_goal(double unprofiled_goal,
Sabina Davis0e484f92020-02-23 17:47:53 -0800185 double unprofiled_goal_velocity = 0.0,
186 bool print = true);
Austin Schuh473a5652017-02-05 01:30:42 -0800187 // Limits our profiles to a max velocity and acceleration for proper motion.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700188 void AdjustProfile(const ::frc971::ProfileParameters *profile_parameters);
Austin Schuh473a5652017-02-05 01:30:42 -0800189 void AdjustProfile(double max_angular_velocity,
190 double max_angular_acceleration);
191
192 // Returns true if we have exceeded any hard limits.
193 bool CheckHardLimits();
194
Austin Schuh3634ed32017-02-05 16:28:49 -0800195 // Returns the requested voltage.
Adam Snaider79900c22017-02-08 20:23:15 -0800196 double voltage() const { return this->loop_->U(0, 0); }
Austin Schuh473a5652017-02-05 01:30:42 -0800197
Austin Schuh482a9142022-02-23 16:54:39 -0800198 // Returns the current position or velocity.
Adam Snaider79900c22017-02-08 20:23:15 -0800199 double position() const { return this->Y_(0, 0); }
Austin Schuh473a5652017-02-05 01:30:42 -0800200
201 // For testing:
202 // Triggers an estimator error.
Adam Snaider79900c22017-02-08 20:23:15 -0800203 void TriggerEstimatorError() { this->estimators_[0].TriggerError(); }
Austin Schuh473a5652017-02-05 01:30:42 -0800204
Austin Schuh00be3a82017-02-05 19:01:40 -0800205 const ::frc971::constants::Range &range() const { return range_; }
206
Austin Schuh93ddcb42021-10-25 21:54:11 -0700207 double default_velocity() const { return default_velocity_; }
208 double default_acceleration() const { return default_acceleration_; }
209
Austin Schuh6a90cd92017-02-19 20:55:33 -0800210 protected:
Austin Schuh473a5652017-02-05 01:30:42 -0800211 // Limits the provided goal to the soft limits. Prints "name" when it fails
212 // to aid debugging.
Sabina Davis0e484f92020-02-23 17:47:53 -0800213 virtual void CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal,
214 bool print = true);
Austin Schuh473a5652017-02-05 01:30:42 -0800215
Austin Schuh6a90cd92017-02-19 20:55:33 -0800216 private:
Austin Schuh473a5652017-02-05 01:30:42 -0800217 void UpdateOffset(double offset);
218
219 aos::util::TrapezoidProfile profile_;
James Kuszmaul4fb29762020-02-20 19:37:41 -0800220 bool enable_profile_ = true;
Austin Schuh473a5652017-02-05 01:30:42 -0800221
222 // Current measurement.
223 Eigen::Matrix<double, 1, 1> Y_;
224 // Current offset. Y_ = offset_ + raw_sensor;
225 Eigen::Matrix<double, 1, 1> offset_;
226
227 const ::frc971::constants::Range range_;
228
229 const double default_velocity_;
230 const double default_acceleration_;
Austin Schuh3634ed32017-02-05 16:28:49 -0800231
232 double last_position_ = 0;
Austin Schuh473a5652017-02-05 01:30:42 -0800233};
234
Brian Silvermanab0b6772017-02-05 16:16:21 -0800235namespace internal {
236
237double UseUnlessZero(double target_value, double default_value);
238
239} // namespace internal
240
241template <class ZeroingEstimator>
242SingleDOFProfiledSubsystem<ZeroingEstimator>::SingleDOFProfiledSubsystem(
243 ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop,
244 const typename ZeroingEstimator::ZeroingConstants &zeroing_constants,
245 const ::frc971::constants::Range &range, double default_velocity,
246 double default_acceleration)
Campbell Crowley36e93e92017-12-23 14:21:43 -0800247 : ProfiledSubsystem<3, 1, ZeroingEstimator>(
248 ::std::move(loop), {{ZeroingEstimator(zeroing_constants)}}),
Sabina Davis1184cc52020-02-22 18:29:25 -0800249 profile_(this->loop_->plant().coefficients().dt),
Brian Silvermanab0b6772017-02-05 16:16:21 -0800250 range_(range),
251 default_velocity_(default_velocity),
252 default_acceleration_(default_acceleration) {
253 Y_.setZero();
254 offset_.setZero();
255 AdjustProfile(0.0, 0.0);
256}
257
258template <class ZeroingEstimator>
259void SingleDOFProfiledSubsystem<ZeroingEstimator>::UpdateOffset(double offset) {
260 const double doffset = offset - offset_(0, 0);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700261 AOS_LOG(INFO, "Adjusting offset from %f to %f\n", offset_(0, 0), offset);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800262
Adam Snaider79900c22017-02-08 20:23:15 -0800263 this->loop_->mutable_X_hat()(0, 0) += doffset;
264 this->Y_(0, 0) += doffset;
Austin Schuh3634ed32017-02-05 16:28:49 -0800265 last_position_ += doffset;
Adam Snaider79900c22017-02-08 20:23:15 -0800266 this->loop_->mutable_R(0, 0) += doffset;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800267
268 profile_.MoveGoal(doffset);
269 offset_(0, 0) = offset;
270
Adam Snaider79900c22017-02-08 20:23:15 -0800271 CapGoal("R", &this->loop_->mutable_R());
Brian Silvermanab0b6772017-02-05 16:16:21 -0800272}
273
274template <class ZeroingEstimator>
Alex Perrycb7da4b2019-08-28 19:35:56 -0700275template <class StatusTypeBuilder>
276StatusTypeBuilder SingleDOFProfiledSubsystem<ZeroingEstimator>::BuildStatus(
277 flatbuffers::FlatBufferBuilder *fbb) {
278 flatbuffers::Offset<typename ZeroingEstimator::State> estimator_state =
279 this->EstimatorState(fbb, 0);
280
281 StatusTypeBuilder builder(*fbb);
282
283 builder.add_zeroed(this->zeroed());
Austin Schuh3634ed32017-02-05 16:28:49 -0800284 // We don't know, so default to the bad case.
Austin Schuh3634ed32017-02-05 16:28:49 -0800285
Austin Schuh2669ece2022-03-11 18:30:57 -0800286 builder.add_position(this->X_hat_(0, 0));
287 builder.add_velocity(this->X_hat_(1, 0));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700288 builder.add_goal_position(this->goal(0, 0));
289 builder.add_goal_velocity(this->goal(1, 0));
290 builder.add_unprofiled_goal_position(this->unprofiled_goal(0, 0));
291 builder.add_unprofiled_goal_velocity(this->unprofiled_goal(1, 0));
Austin Schuh2669ece2022-03-11 18:30:57 -0800292 builder.add_voltage_error(this->X_hat_(2, 0));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700293 builder.add_calculated_velocity(
Austin Schuh3634ed32017-02-05 16:28:49 -0800294 (position() - last_position_) /
Sabina Davis1184cc52020-02-22 18:29:25 -0800295 ::aos::time::DurationInSeconds(this->loop_->plant().coefficients().dt));
Austin Schuh3634ed32017-02-05 16:28:49 -0800296
Alex Perrycb7da4b2019-08-28 19:35:56 -0700297 builder.add_estimator_state(estimator_state);
Austin Schuh3634ed32017-02-05 16:28:49 -0800298
Adam Snaider79900c22017-02-08 20:23:15 -0800299 Eigen::Matrix<double, 3, 1> error = this->controller().error();
Austin Schuh482a9142022-02-23 16:54:39 -0800300 builder.add_position_power(this->controller().controller().K(0, 0) *
301 error(0, 0));
302 builder.add_velocity_power(this->controller().controller().K(0, 1) *
303 error(1, 0));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700304 return builder;
Austin Schuh3634ed32017-02-05 16:28:49 -0800305}
306
307template <class ZeroingEstimator>
Brian Silvermanab0b6772017-02-05 16:16:21 -0800308void SingleDOFProfiledSubsystem<ZeroingEstimator>::Correct(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700309 const typename ZeroingEstimator::Position &new_position) {
Adam Snaider79900c22017-02-08 20:23:15 -0800310 this->estimators_[0].UpdateEstimate(new_position);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800311
Adam Snaider79900c22017-02-08 20:23:15 -0800312 if (this->estimators_[0].error()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700313 AOS_LOG(ERROR, "zeroing error\n");
Austin Schuh2669ece2022-03-11 18:30:57 -0800314 this->X_hat_ = this->loop_->X_hat();
Brian Silvermanab0b6772017-02-05 16:16:21 -0800315 return;
316 }
317
Adam Snaider79900c22017-02-08 20:23:15 -0800318 if (!this->initialized_) {
319 if (this->estimators_[0].offset_ready()) {
320 UpdateOffset(this->estimators_[0].offset());
321 this->initialized_ = true;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800322 }
323 }
324
Adam Snaider79900c22017-02-08 20:23:15 -0800325 if (!this->zeroed(0) && this->estimators_[0].zeroed()) {
326 UpdateOffset(this->estimators_[0].offset());
327 this->set_zeroed(0, true);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800328 }
329
Austin Schuh3634ed32017-02-05 16:28:49 -0800330 last_position_ = position();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700331 this->Y_ << new_position.encoder();
Adam Snaider79900c22017-02-08 20:23:15 -0800332 this->Y_ += this->offset_;
333 this->loop_->Correct(Y_);
Austin Schuh2669ece2022-03-11 18:30:57 -0800334 this->X_hat_ = this->loop_->X_hat();
Brian Silvermanab0b6772017-02-05 16:16:21 -0800335}
336
337template <class ZeroingEstimator>
338void SingleDOFProfiledSubsystem<ZeroingEstimator>::CapGoal(
Sabina Davis0e484f92020-02-23 17:47:53 -0800339 const char *name, Eigen::Matrix<double, 3, 1> *goal, bool print) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800340 // Limit the goal to min/max allowable positions.
Brian Silvermanab0b6772017-02-05 16:16:21 -0800341 if ((*goal)(0, 0) > range_.upper) {
Sabina Davis0e484f92020-02-23 17:47:53 -0800342 if (print) {
343 AOS_LOG(WARNING, "Goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
344 range_.upper);
345 }
Brian Silvermanab0b6772017-02-05 16:16:21 -0800346 (*goal)(0, 0) = range_.upper;
347 }
348 if ((*goal)(0, 0) < range_.lower) {
Sabina Davis0e484f92020-02-23 17:47:53 -0800349 if (print) {
350 AOS_LOG(WARNING, "Goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
351 range_.lower);
352 }
Brian Silvermanab0b6772017-02-05 16:16:21 -0800353 (*goal)(0, 0) = range_.lower;
354 }
355}
356
357template <class ZeroingEstimator>
Austin Schuh482a9142022-02-23 16:54:39 -0800358void SingleDOFProfiledSubsystem<ZeroingEstimator>::ForceGoal(
359 double goal, double goal_velocity) {
360 set_unprofiled_goal(goal, goal_velocity, false);
Adam Snaider79900c22017-02-08 20:23:15 -0800361 this->loop_->mutable_R() = this->unprofiled_goal_;
362 this->loop_->mutable_next_R() = this->loop_->R();
Brian Silvermanab0b6772017-02-05 16:16:21 -0800363
Adam Snaider79900c22017-02-08 20:23:15 -0800364 const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R();
365 this->profile_.MoveCurrentState(R.block<2, 1>(0, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800366}
367
368template <class ZeroingEstimator>
369void SingleDOFProfiledSubsystem<ZeroingEstimator>::set_unprofiled_goal(
Sabina Davis0e484f92020-02-23 17:47:53 -0800370 double unprofiled_goal, double unprofiled_goal_velocity, bool print) {
Adam Snaider79900c22017-02-08 20:23:15 -0800371 this->unprofiled_goal_(0, 0) = unprofiled_goal;
James Kuszmaul4fb29762020-02-20 19:37:41 -0800372 this->unprofiled_goal_(1, 0) = unprofiled_goal_velocity;
Adam Snaider79900c22017-02-08 20:23:15 -0800373 this->unprofiled_goal_(2, 0) = 0.0;
Sabina Davis0e484f92020-02-23 17:47:53 -0800374 CapGoal("unprofiled R", &this->unprofiled_goal_, print);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800375}
376
377template <class ZeroingEstimator>
Austin Schuh482a9142022-02-23 16:54:39 -0800378double SingleDOFProfiledSubsystem<ZeroingEstimator>::UpdateController(
379 bool disable) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800380 // TODO(austin): What do we want to do with the profile on reset? Also, we
381 // should probably reset R, the offset, the profile, etc.
382 if (this->should_reset_) {
383 this->loop_->mutable_X_hat(0, 0) = Y_(0, 0);
384 this->loop_->mutable_X_hat(1, 0) = 0.0;
385 this->loop_->mutable_X_hat(2, 0) = 0.0;
Austin Schuh2669ece2022-03-11 18:30:57 -0800386 this->X_hat_.setZero();
Austin Schuhd5ccb862017-03-11 22:06:36 -0800387 this->should_reset_ = false;
388 }
389
Brian Silvermanab0b6772017-02-05 16:16:21 -0800390 if (!disable) {
James Kuszmaul4fb29762020-02-20 19:37:41 -0800391 if (enable_profile_) {
392 ::Eigen::Matrix<double, 2, 1> goal_state = profile_.Update(
393 this->unprofiled_goal_(0, 0), this->unprofiled_goal_(1, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800394
James Kuszmaul4fb29762020-02-20 19:37:41 -0800395 this->loop_->mutable_next_R(0, 0) = goal_state(0, 0);
396 this->loop_->mutable_next_R(1, 0) = goal_state(1, 0);
397 this->loop_->mutable_next_R(2, 0) = 0.0;
398 } else {
399 this->loop_->mutable_R() = this->unprofiled_goal_;
400 this->loop_->mutable_next_R() = this->unprofiled_goal_;
401 this->loop_->mutable_next_R(0, 0) +=
402 this->unprofiled_goal_(1) *
403 aos::time::DurationInSeconds(this->loop_->plant().coefficients().dt);
404 CapGoal("R", &this->loop_->mutable_R());
405 }
Adam Snaider79900c22017-02-08 20:23:15 -0800406 CapGoal("next R", &this->loop_->mutable_next_R());
Brian Silvermanab0b6772017-02-05 16:16:21 -0800407 }
408
Austin Schuh482a9142022-02-23 16:54:39 -0800409 this->loop_->UpdateController(disable);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800410
Adam Snaider79900c22017-02-08 20:23:15 -0800411 if (!disable && this->loop_->U(0, 0) != this->loop_->U_uncapped(0, 0)) {
412 const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R();
413 profile_.MoveCurrentState(R.block<2, 1>(0, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800414 }
Austin Schuh482a9142022-02-23 16:54:39 -0800415
416 return this->loop_->U(0, 0);
417}
418
419template <class ZeroingEstimator>
420void SingleDOFProfiledSubsystem<ZeroingEstimator>::UpdateObserver(
421 double voltage) {
422 this->loop_->mutable_U(0, 0) = voltage;
423 this->loop_->UpdateObserver(this->loop_->U(), this->loop_->plant().dt());
424}
425
426template <class ZeroingEstimator>
427double SingleDOFProfiledSubsystem<ZeroingEstimator>::Update(bool disable) {
428 const double voltage = UpdateController(disable);
429 UpdateObserver(voltage);
430 return voltage;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800431}
432
433template <class ZeroingEstimator>
434bool SingleDOFProfiledSubsystem<ZeroingEstimator>::CheckHardLimits() {
435 // Returns whether hard limits have been exceeded.
436
Austin Schuh3634ed32017-02-05 16:28:49 -0800437 if (position() > range_.upper_hard || position() < range_.lower_hard) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700438 AOS_LOG(
439 ERROR,
Brian Silvermanab0b6772017-02-05 16:16:21 -0800440 "SingleDOFProfiledSubsystem at %f out of bounds [%f, %f], ESTOPing\n",
Austin Schuh3634ed32017-02-05 16:28:49 -0800441 position(), range_.lower_hard, range_.upper_hard);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800442 return true;
443 }
444
445 return false;
446}
447
448template <class ZeroingEstimator>
449void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700450 const ::frc971::ProfileParameters *profile_parameters) {
451 AdjustProfile(
452 profile_parameters != nullptr ? profile_parameters->max_velocity() : 0.0,
453 profile_parameters != nullptr ? profile_parameters->max_acceleration()
454 : 0.0);
Austin Schuh3634ed32017-02-05 16:28:49 -0800455}
456
457template <class ZeroingEstimator>
458void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Brian Silvermanab0b6772017-02-05 16:16:21 -0800459 double max_angular_velocity, double max_angular_acceleration) {
460 profile_.set_maximum_velocity(
461 internal::UseUnlessZero(max_angular_velocity, default_velocity_));
462 profile_.set_maximum_acceleration(
463 internal::UseUnlessZero(max_angular_acceleration, default_acceleration_));
464}
465
Austin Schuh473a5652017-02-05 01:30:42 -0800466} // namespace control_loops
467} // namespace frc971
468
469#endif // FRC971_CONTROL_LOOPS_PROFILED_SUBSYSTEM_H_