blob: 5a450ecdcc439497685d0eaa43482d778669fc9d [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();
39 }
40
41 // Returns whether an error has occured
42 bool error() const {
43 for (const auto &estimator : estimators_) {
44 if (estimator.error()) {
45 return true;
46 }
47 }
48 return false;
49 }
50
51 void Reset() {
52 zeroed_.fill(false);
Austin Schuh3a81d5c2017-02-05 23:13:47 -080053 initialized_ = false;
Austin Schuh473a5652017-02-05 01:30:42 -080054 for (auto &estimator : estimators_) {
55 estimator.Reset();
56 }
Austin Schuhd5ccb862017-03-11 22:06:36 -080057 should_reset_ = true;
Austin Schuh473a5652017-02-05 01:30:42 -080058 }
59
60 // Returns the controller.
Tyler Chatowd3afdef2019-04-06 22:15:26 -070061 const StateFeedbackLoop<number_of_states, number_of_inputs, number_of_outputs>
62 &controller() const {
Austin Schuh473a5652017-02-05 01:30:42 -080063 return *loop_;
64 }
65
Austin Schuhc5fceb82017-02-25 16:24:12 -080066 int controller_index() const { return loop_->index(); }
Austin Schuh473a5652017-02-05 01:30:42 -080067
Tyler Chatowd3afdef2019-04-06 22:15:26 -070068 void set_controller_index(int index) { loop_->set_index(index); }
69
Austin Schuh473a5652017-02-05 01:30:42 -080070 // Returns whether the estimators have been initialized and zeroed.
71 bool initialized() const { return initialized_; }
72
73 bool zeroed() const {
74 for (int i = 0; i < number_of_axes; ++i) {
75 if (!zeroed_[i]) {
76 return false;
77 }
78 }
79 return true;
80 }
81
82 bool zeroed(int index) const { return zeroed_[index]; };
83
84 // Returns the filtered goal.
85 const Eigen::Matrix<double, number_of_states, 1> &goal() const {
86 return loop_->R();
87 }
88 double goal(int row, int col) const { return loop_->R(row, col); }
89
90 // Returns the unprofiled goal.
91 const Eigen::Matrix<double, number_of_states, 1> &unprofiled_goal() const {
92 return unprofiled_goal_;
93 }
94 double unprofiled_goal(int row, int col) const {
95 return unprofiled_goal_(row, col);
96 }
97
98 // Returns the current state estimate.
99 const Eigen::Matrix<double, number_of_states, 1> &X_hat() const {
100 return loop_->X_hat();
101 }
102 double X_hat(int row, int col) const { return loop_->X_hat(row, col); }
Austin Schuhd5ccb862017-03-11 22:06:36 -0800103 double &mutable_X_hat(int row, int col) const {
104 return loop_->mutable_X_hat(row, col);
105 }
Austin Schuh473a5652017-02-05 01:30:42 -0800106
107 // Returns the current internal estimator state for logging.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700108 flatbuffers::Offset<typename ZeroingEstimator::State> EstimatorState(
109 flatbuffers::FlatBufferBuilder *fbb, int index) {
110 return estimators_[index].GetEstimatorState(fbb);
Austin Schuh473a5652017-02-05 01:30:42 -0800111 }
112
113 // Sets the maximum voltage that will be commanded by the loop.
Austin Schuh08d9ecf2017-03-05 00:58:48 -0800114 void set_max_voltage(::std::array<double, number_of_inputs> voltages) {
115 for (int i = 0; i < number_of_inputs; ++i) {
Austin Schuh473a5652017-02-05 01:30:42 -0800116 loop_->set_max_voltage(i, voltages[i]);
117 }
118 }
119
120 protected:
121 void set_zeroed(int index, bool val) { zeroed_[index] = val; }
122
Austin Schuh473a5652017-02-05 01:30:42 -0800123 ::std::unique_ptr<::frc971::control_loops::SimpleCappedStateFeedbackLoop<
Austin Schuh08d9ecf2017-03-05 00:58:48 -0800124 number_of_states, number_of_inputs, number_of_outputs>>
125 loop_;
Austin Schuh473a5652017-02-05 01:30:42 -0800126
127 // The goal that the profile tries to reach.
128 Eigen::Matrix<double, number_of_states, 1> unprofiled_goal_;
129
130 bool initialized_ = false;
131
Austin Schuhd5ccb862017-03-11 22:06:36 -0800132 // If true, the subclass should reset in Update. It should then clear this
133 // flag.
134 bool should_reset_ = true;
135
Brian Silvermanab0b6772017-02-05 16:16:21 -0800136 ::std::array<ZeroingEstimator, number_of_axes> estimators_;
Austin Schuh473a5652017-02-05 01:30:42 -0800137
138 private:
139 ::std::array<bool, number_of_axes> zeroed_;
140};
141
Adam Snaider79900c22017-02-08 20:23:15 -0800142template <typename ZeroingEstimator =
Brian Silvermanab0b6772017-02-05 16:16:21 -0800143 ::frc971::zeroing::PotAndIndexPulseZeroingEstimator>
Austin Schuh473a5652017-02-05 01:30:42 -0800144class SingleDOFProfiledSubsystem
Tyler Chatowd3afdef2019-04-06 22:15:26 -0700145 : public ::frc971::control_loops::ProfiledSubsystem<3, 1,
146 ZeroingEstimator> {
Austin Schuh473a5652017-02-05 01:30:42 -0800147 public:
148 SingleDOFProfiledSubsystem(
149 ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop,
Brian Silvermanab0b6772017-02-05 16:16:21 -0800150 const typename ZeroingEstimator::ZeroingConstants &zeroing_constants,
Austin Schuh473a5652017-02-05 01:30:42 -0800151 const ::frc971::constants::Range &range, double default_angular_velocity,
152 double default_angular_acceleration);
153
154 // Updates our estimator with the latest position.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700155 void Correct(const typename ZeroingEstimator::Position &position);
Austin Schuh482a9142022-02-23 16:54:39 -0800156 // Runs the controller and profile generator for a cycle. This is equivilent
157 // to calling UpdateObserver(UpdateController()) with the rest of the syntax
158 // actually right.
159 double Update(bool disabled);
160 // Just computes the controller and pushes the feed forwards forwards 1 step.
161 double UpdateController(bool disabled);
162 // Updates the observer with the computed U.
163 // Note: if this is the only method called, ForceGoal should also be called to
164 // move the state to match.
165 void UpdateObserver(double voltage);
Austin Schuh473a5652017-02-05 01:30:42 -0800166
Austin Schuh3634ed32017-02-05 16:28:49 -0800167 // Fills out the ProfiledJointStatus structure with the current state.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700168 template <class StatusTypeBuilder>
Austin Schuh482a9142022-02-23 16:54:39 -0800169 StatusTypeBuilder BuildStatus(flatbuffers::FlatBufferBuilder *fbb);
Austin Schuh3634ed32017-02-05 16:28:49 -0800170
Austin Schuh473a5652017-02-05 01:30:42 -0800171 // Forces the current goal to the provided goal, bypassing the profiler.
Austin Schuh482a9142022-02-23 16:54:39 -0800172 void ForceGoal(double goal, double goal_velocity = 0.0);
James Kuszmaul4fb29762020-02-20 19:37:41 -0800173 // Sets whether to use the trapezoidal profiler or whether to just bypass it
174 // and pass the unprofiled goal through directly.
175 void set_enable_profile(bool enable) { enable_profile_ = enable; }
Austin Schuh473a5652017-02-05 01:30:42 -0800176 // Sets the unprofiled goal. The profiler will generate a profile to go to
177 // this goal.
James Kuszmaul4fb29762020-02-20 19:37:41 -0800178 void set_unprofiled_goal(double unprofiled_goal,
Sabina Davis0e484f92020-02-23 17:47:53 -0800179 double unprofiled_goal_velocity = 0.0,
180 bool print = true);
Austin Schuh473a5652017-02-05 01:30:42 -0800181 // Limits our profiles to a max velocity and acceleration for proper motion.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700182 void AdjustProfile(const ::frc971::ProfileParameters *profile_parameters);
Austin Schuh473a5652017-02-05 01:30:42 -0800183 void AdjustProfile(double max_angular_velocity,
184 double max_angular_acceleration);
185
186 // Returns true if we have exceeded any hard limits.
187 bool CheckHardLimits();
188
Austin Schuh3634ed32017-02-05 16:28:49 -0800189 // Returns the requested voltage.
Adam Snaider79900c22017-02-08 20:23:15 -0800190 double voltage() const { return this->loop_->U(0, 0); }
Austin Schuh473a5652017-02-05 01:30:42 -0800191
Austin Schuh482a9142022-02-23 16:54:39 -0800192 // Returns the current position or velocity.
Adam Snaider79900c22017-02-08 20:23:15 -0800193 double position() const { return this->Y_(0, 0); }
Austin Schuh473a5652017-02-05 01:30:42 -0800194
195 // For testing:
196 // Triggers an estimator error.
Adam Snaider79900c22017-02-08 20:23:15 -0800197 void TriggerEstimatorError() { this->estimators_[0].TriggerError(); }
Austin Schuh473a5652017-02-05 01:30:42 -0800198
Austin Schuh00be3a82017-02-05 19:01:40 -0800199 const ::frc971::constants::Range &range() const { return range_; }
200
Austin Schuh93ddcb42021-10-25 21:54:11 -0700201 double default_velocity() const { return default_velocity_; }
202 double default_acceleration() const { return default_acceleration_; }
203
Austin Schuh6a90cd92017-02-19 20:55:33 -0800204 protected:
Austin Schuh473a5652017-02-05 01:30:42 -0800205 // Limits the provided goal to the soft limits. Prints "name" when it fails
206 // to aid debugging.
Sabina Davis0e484f92020-02-23 17:47:53 -0800207 virtual void CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal,
208 bool print = true);
Austin Schuh473a5652017-02-05 01:30:42 -0800209
Austin Schuh6a90cd92017-02-19 20:55:33 -0800210 private:
Austin Schuh473a5652017-02-05 01:30:42 -0800211 void UpdateOffset(double offset);
212
213 aos::util::TrapezoidProfile profile_;
James Kuszmaul4fb29762020-02-20 19:37:41 -0800214 bool enable_profile_ = true;
Austin Schuh473a5652017-02-05 01:30:42 -0800215
216 // Current measurement.
217 Eigen::Matrix<double, 1, 1> Y_;
218 // Current offset. Y_ = offset_ + raw_sensor;
219 Eigen::Matrix<double, 1, 1> offset_;
220
221 const ::frc971::constants::Range range_;
222
223 const double default_velocity_;
224 const double default_acceleration_;
Austin Schuh3634ed32017-02-05 16:28:49 -0800225
226 double last_position_ = 0;
Austin Schuh473a5652017-02-05 01:30:42 -0800227};
228
Brian Silvermanab0b6772017-02-05 16:16:21 -0800229namespace internal {
230
231double UseUnlessZero(double target_value, double default_value);
232
233} // namespace internal
234
235template <class ZeroingEstimator>
236SingleDOFProfiledSubsystem<ZeroingEstimator>::SingleDOFProfiledSubsystem(
237 ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop,
238 const typename ZeroingEstimator::ZeroingConstants &zeroing_constants,
239 const ::frc971::constants::Range &range, double default_velocity,
240 double default_acceleration)
Campbell Crowley36e93e92017-12-23 14:21:43 -0800241 : ProfiledSubsystem<3, 1, ZeroingEstimator>(
242 ::std::move(loop), {{ZeroingEstimator(zeroing_constants)}}),
Sabina Davis1184cc52020-02-22 18:29:25 -0800243 profile_(this->loop_->plant().coefficients().dt),
Brian Silvermanab0b6772017-02-05 16:16:21 -0800244 range_(range),
245 default_velocity_(default_velocity),
246 default_acceleration_(default_acceleration) {
247 Y_.setZero();
248 offset_.setZero();
249 AdjustProfile(0.0, 0.0);
250}
251
252template <class ZeroingEstimator>
253void SingleDOFProfiledSubsystem<ZeroingEstimator>::UpdateOffset(double offset) {
254 const double doffset = offset - offset_(0, 0);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700255 AOS_LOG(INFO, "Adjusting offset from %f to %f\n", offset_(0, 0), offset);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800256
Adam Snaider79900c22017-02-08 20:23:15 -0800257 this->loop_->mutable_X_hat()(0, 0) += doffset;
258 this->Y_(0, 0) += doffset;
Austin Schuh3634ed32017-02-05 16:28:49 -0800259 last_position_ += doffset;
Adam Snaider79900c22017-02-08 20:23:15 -0800260 this->loop_->mutable_R(0, 0) += doffset;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800261
262 profile_.MoveGoal(doffset);
263 offset_(0, 0) = offset;
264
Adam Snaider79900c22017-02-08 20:23:15 -0800265 CapGoal("R", &this->loop_->mutable_R());
Brian Silvermanab0b6772017-02-05 16:16:21 -0800266}
267
268template <class ZeroingEstimator>
Alex Perrycb7da4b2019-08-28 19:35:56 -0700269template <class StatusTypeBuilder>
270StatusTypeBuilder SingleDOFProfiledSubsystem<ZeroingEstimator>::BuildStatus(
271 flatbuffers::FlatBufferBuilder *fbb) {
272 flatbuffers::Offset<typename ZeroingEstimator::State> estimator_state =
273 this->EstimatorState(fbb, 0);
274
275 StatusTypeBuilder builder(*fbb);
276
277 builder.add_zeroed(this->zeroed());
Austin Schuh3634ed32017-02-05 16:28:49 -0800278 // We don't know, so default to the bad case.
Austin Schuh3634ed32017-02-05 16:28:49 -0800279
Alex Perrycb7da4b2019-08-28 19:35:56 -0700280 builder.add_position(this->X_hat(0, 0));
281 builder.add_velocity(this->X_hat(1, 0));
282 builder.add_goal_position(this->goal(0, 0));
283 builder.add_goal_velocity(this->goal(1, 0));
284 builder.add_unprofiled_goal_position(this->unprofiled_goal(0, 0));
285 builder.add_unprofiled_goal_velocity(this->unprofiled_goal(1, 0));
286 builder.add_voltage_error(this->X_hat(2, 0));
287 builder.add_calculated_velocity(
Austin Schuh3634ed32017-02-05 16:28:49 -0800288 (position() - last_position_) /
Sabina Davis1184cc52020-02-22 18:29:25 -0800289 ::aos::time::DurationInSeconds(this->loop_->plant().coefficients().dt));
Austin Schuh3634ed32017-02-05 16:28:49 -0800290
Alex Perrycb7da4b2019-08-28 19:35:56 -0700291 builder.add_estimator_state(estimator_state);
Austin Schuh3634ed32017-02-05 16:28:49 -0800292
Adam Snaider79900c22017-02-08 20:23:15 -0800293 Eigen::Matrix<double, 3, 1> error = this->controller().error();
Austin Schuh482a9142022-02-23 16:54:39 -0800294 builder.add_position_power(this->controller().controller().K(0, 0) *
295 error(0, 0));
296 builder.add_velocity_power(this->controller().controller().K(0, 1) *
297 error(1, 0));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700298 return builder;
Austin Schuh3634ed32017-02-05 16:28:49 -0800299}
300
301template <class ZeroingEstimator>
Brian Silvermanab0b6772017-02-05 16:16:21 -0800302void SingleDOFProfiledSubsystem<ZeroingEstimator>::Correct(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700303 const typename ZeroingEstimator::Position &new_position) {
Adam Snaider79900c22017-02-08 20:23:15 -0800304 this->estimators_[0].UpdateEstimate(new_position);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800305
Adam Snaider79900c22017-02-08 20:23:15 -0800306 if (this->estimators_[0].error()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700307 AOS_LOG(ERROR, "zeroing error\n");
Brian Silvermanab0b6772017-02-05 16:16:21 -0800308 return;
309 }
310
Adam Snaider79900c22017-02-08 20:23:15 -0800311 if (!this->initialized_) {
312 if (this->estimators_[0].offset_ready()) {
313 UpdateOffset(this->estimators_[0].offset());
314 this->initialized_ = true;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800315 }
316 }
317
Adam Snaider79900c22017-02-08 20:23:15 -0800318 if (!this->zeroed(0) && this->estimators_[0].zeroed()) {
319 UpdateOffset(this->estimators_[0].offset());
320 this->set_zeroed(0, true);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800321 }
322
Austin Schuh3634ed32017-02-05 16:28:49 -0800323 last_position_ = position();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700324 this->Y_ << new_position.encoder();
Adam Snaider79900c22017-02-08 20:23:15 -0800325 this->Y_ += this->offset_;
326 this->loop_->Correct(Y_);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800327}
328
329template <class ZeroingEstimator>
330void SingleDOFProfiledSubsystem<ZeroingEstimator>::CapGoal(
Sabina Davis0e484f92020-02-23 17:47:53 -0800331 const char *name, Eigen::Matrix<double, 3, 1> *goal, bool print) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800332 // Limit the goal to min/max allowable positions.
Brian Silvermanab0b6772017-02-05 16:16:21 -0800333 if ((*goal)(0, 0) > range_.upper) {
Sabina Davis0e484f92020-02-23 17:47:53 -0800334 if (print) {
335 AOS_LOG(WARNING, "Goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
336 range_.upper);
337 }
Brian Silvermanab0b6772017-02-05 16:16:21 -0800338 (*goal)(0, 0) = range_.upper;
339 }
340 if ((*goal)(0, 0) < range_.lower) {
Sabina Davis0e484f92020-02-23 17:47:53 -0800341 if (print) {
342 AOS_LOG(WARNING, "Goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
343 range_.lower);
344 }
Brian Silvermanab0b6772017-02-05 16:16:21 -0800345 (*goal)(0, 0) = range_.lower;
346 }
347}
348
349template <class ZeroingEstimator>
Austin Schuh482a9142022-02-23 16:54:39 -0800350void SingleDOFProfiledSubsystem<ZeroingEstimator>::ForceGoal(
351 double goal, double goal_velocity) {
352 set_unprofiled_goal(goal, goal_velocity, false);
Adam Snaider79900c22017-02-08 20:23:15 -0800353 this->loop_->mutable_R() = this->unprofiled_goal_;
354 this->loop_->mutable_next_R() = this->loop_->R();
Brian Silvermanab0b6772017-02-05 16:16:21 -0800355
Adam Snaider79900c22017-02-08 20:23:15 -0800356 const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R();
357 this->profile_.MoveCurrentState(R.block<2, 1>(0, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800358}
359
360template <class ZeroingEstimator>
361void SingleDOFProfiledSubsystem<ZeroingEstimator>::set_unprofiled_goal(
Sabina Davis0e484f92020-02-23 17:47:53 -0800362 double unprofiled_goal, double unprofiled_goal_velocity, bool print) {
Adam Snaider79900c22017-02-08 20:23:15 -0800363 this->unprofiled_goal_(0, 0) = unprofiled_goal;
James Kuszmaul4fb29762020-02-20 19:37:41 -0800364 this->unprofiled_goal_(1, 0) = unprofiled_goal_velocity;
Adam Snaider79900c22017-02-08 20:23:15 -0800365 this->unprofiled_goal_(2, 0) = 0.0;
Sabina Davis0e484f92020-02-23 17:47:53 -0800366 CapGoal("unprofiled R", &this->unprofiled_goal_, print);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800367}
368
369template <class ZeroingEstimator>
Austin Schuh482a9142022-02-23 16:54:39 -0800370double SingleDOFProfiledSubsystem<ZeroingEstimator>::UpdateController(
371 bool disable) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800372 // TODO(austin): What do we want to do with the profile on reset? Also, we
373 // should probably reset R, the offset, the profile, etc.
374 if (this->should_reset_) {
375 this->loop_->mutable_X_hat(0, 0) = Y_(0, 0);
376 this->loop_->mutable_X_hat(1, 0) = 0.0;
377 this->loop_->mutable_X_hat(2, 0) = 0.0;
378 this->should_reset_ = false;
379 }
380
Brian Silvermanab0b6772017-02-05 16:16:21 -0800381 if (!disable) {
James Kuszmaul4fb29762020-02-20 19:37:41 -0800382 if (enable_profile_) {
383 ::Eigen::Matrix<double, 2, 1> goal_state = profile_.Update(
384 this->unprofiled_goal_(0, 0), this->unprofiled_goal_(1, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800385
James Kuszmaul4fb29762020-02-20 19:37:41 -0800386 this->loop_->mutable_next_R(0, 0) = goal_state(0, 0);
387 this->loop_->mutable_next_R(1, 0) = goal_state(1, 0);
388 this->loop_->mutable_next_R(2, 0) = 0.0;
389 } else {
390 this->loop_->mutable_R() = this->unprofiled_goal_;
391 this->loop_->mutable_next_R() = this->unprofiled_goal_;
392 this->loop_->mutable_next_R(0, 0) +=
393 this->unprofiled_goal_(1) *
394 aos::time::DurationInSeconds(this->loop_->plant().coefficients().dt);
395 CapGoal("R", &this->loop_->mutable_R());
396 }
Adam Snaider79900c22017-02-08 20:23:15 -0800397 CapGoal("next R", &this->loop_->mutable_next_R());
Brian Silvermanab0b6772017-02-05 16:16:21 -0800398 }
399
Austin Schuh482a9142022-02-23 16:54:39 -0800400 this->loop_->UpdateController(disable);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800401
Adam Snaider79900c22017-02-08 20:23:15 -0800402 if (!disable && this->loop_->U(0, 0) != this->loop_->U_uncapped(0, 0)) {
403 const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R();
404 profile_.MoveCurrentState(R.block<2, 1>(0, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800405 }
Austin Schuh482a9142022-02-23 16:54:39 -0800406
407 return this->loop_->U(0, 0);
408}
409
410template <class ZeroingEstimator>
411void SingleDOFProfiledSubsystem<ZeroingEstimator>::UpdateObserver(
412 double voltage) {
413 this->loop_->mutable_U(0, 0) = voltage;
414 this->loop_->UpdateObserver(this->loop_->U(), this->loop_->plant().dt());
415}
416
417template <class ZeroingEstimator>
418double SingleDOFProfiledSubsystem<ZeroingEstimator>::Update(bool disable) {
419 const double voltage = UpdateController(disable);
420 UpdateObserver(voltage);
421 return voltage;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800422}
423
424template <class ZeroingEstimator>
425bool SingleDOFProfiledSubsystem<ZeroingEstimator>::CheckHardLimits() {
426 // Returns whether hard limits have been exceeded.
427
Austin Schuh3634ed32017-02-05 16:28:49 -0800428 if (position() > range_.upper_hard || position() < range_.lower_hard) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700429 AOS_LOG(
430 ERROR,
Brian Silvermanab0b6772017-02-05 16:16:21 -0800431 "SingleDOFProfiledSubsystem at %f out of bounds [%f, %f], ESTOPing\n",
Austin Schuh3634ed32017-02-05 16:28:49 -0800432 position(), range_.lower_hard, range_.upper_hard);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800433 return true;
434 }
435
436 return false;
437}
438
439template <class ZeroingEstimator>
440void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700441 const ::frc971::ProfileParameters *profile_parameters) {
442 AdjustProfile(
443 profile_parameters != nullptr ? profile_parameters->max_velocity() : 0.0,
444 profile_parameters != nullptr ? profile_parameters->max_acceleration()
445 : 0.0);
Austin Schuh3634ed32017-02-05 16:28:49 -0800446}
447
448template <class ZeroingEstimator>
449void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Brian Silvermanab0b6772017-02-05 16:16:21 -0800450 double max_angular_velocity, double max_angular_acceleration) {
451 profile_.set_maximum_velocity(
452 internal::UseUnlessZero(max_angular_velocity, default_velocity_));
453 profile_.set_maximum_acceleration(
454 internal::UseUnlessZero(max_angular_acceleration, default_acceleration_));
455}
456
Austin Schuh473a5652017-02-05 01:30:42 -0800457} // namespace control_loops
458} // namespace frc971
459
460#endif // FRC971_CONTROL_LOOPS_PROFILED_SUBSYSTEM_H_