blob: 86e608c0ba92ed6c1336d08d98787c59a275e6a7 [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"
10
James Kuszmaul61750662021-06-21 21:32:33 -070011#include "frc971/control_loops/control_loop.h"
John Park33858a32018-09-28 23:05:48 -070012#include "aos/util/trapezoid_profile.h"
Tyler Chatowd3afdef2019-04-06 22:15:26 -070013#include "frc971/constants.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"
18#include "frc971/zeroing/zeroing.h"
19
20namespace frc971 {
21namespace control_loops {
22
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();
40 }
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
99 // Returns the current state estimate.
100 const Eigen::Matrix<double, number_of_states, 1> &X_hat() const {
101 return loop_->X_hat();
102 }
103 double X_hat(int row, int col) const { return loop_->X_hat(row, col); }
Austin Schuhd5ccb862017-03-11 22:06:36 -0800104 double &mutable_X_hat(int row, int col) const {
105 return loop_->mutable_X_hat(row, col);
106 }
Austin Schuh473a5652017-02-05 01:30:42 -0800107
108 // Returns the current internal estimator state for logging.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700109 flatbuffers::Offset<typename ZeroingEstimator::State> EstimatorState(
110 flatbuffers::FlatBufferBuilder *fbb, int index) {
111 return estimators_[index].GetEstimatorState(fbb);
Austin Schuh473a5652017-02-05 01:30:42 -0800112 }
113
114 // Sets the maximum voltage that will be commanded by the loop.
Austin Schuh08d9ecf2017-03-05 00:58:48 -0800115 void set_max_voltage(::std::array<double, number_of_inputs> voltages) {
116 for (int i = 0; i < number_of_inputs; ++i) {
Austin Schuh473a5652017-02-05 01:30:42 -0800117 loop_->set_max_voltage(i, voltages[i]);
118 }
119 }
120
121 protected:
122 void set_zeroed(int index, bool val) { zeroed_[index] = val; }
123
Austin Schuh473a5652017-02-05 01:30:42 -0800124 ::std::unique_ptr<::frc971::control_loops::SimpleCappedStateFeedbackLoop<
Austin Schuh08d9ecf2017-03-05 00:58:48 -0800125 number_of_states, number_of_inputs, number_of_outputs>>
126 loop_;
Austin Schuh473a5652017-02-05 01:30:42 -0800127
128 // The goal that the profile tries to reach.
129 Eigen::Matrix<double, number_of_states, 1> unprofiled_goal_;
130
131 bool initialized_ = false;
132
Austin Schuhd5ccb862017-03-11 22:06:36 -0800133 // If true, the subclass should reset in Update. It should then clear this
134 // flag.
135 bool should_reset_ = true;
136
Brian Silvermanab0b6772017-02-05 16:16:21 -0800137 ::std::array<ZeroingEstimator, number_of_axes> estimators_;
Austin Schuh473a5652017-02-05 01:30:42 -0800138
139 private:
140 ::std::array<bool, number_of_axes> zeroed_;
141};
142
Adam Snaider79900c22017-02-08 20:23:15 -0800143template <typename ZeroingEstimator =
Brian Silvermanab0b6772017-02-05 16:16:21 -0800144 ::frc971::zeroing::PotAndIndexPulseZeroingEstimator>
Austin Schuh473a5652017-02-05 01:30:42 -0800145class SingleDOFProfiledSubsystem
Tyler Chatowd3afdef2019-04-06 22:15:26 -0700146 : public ::frc971::control_loops::ProfiledSubsystem<3, 1,
147 ZeroingEstimator> {
Austin Schuh473a5652017-02-05 01:30:42 -0800148 public:
149 SingleDOFProfiledSubsystem(
150 ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop,
Brian Silvermanab0b6772017-02-05 16:16:21 -0800151 const typename ZeroingEstimator::ZeroingConstants &zeroing_constants,
Austin Schuh473a5652017-02-05 01:30:42 -0800152 const ::frc971::constants::Range &range, double default_angular_velocity,
153 double default_angular_acceleration);
154
155 // Updates our estimator with the latest position.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700156 void Correct(const typename ZeroingEstimator::Position &position);
Austin Schuh473a5652017-02-05 01:30:42 -0800157 // Runs the controller and profile generator for a cycle.
158 void Update(bool disabled);
159
Austin Schuh3634ed32017-02-05 16:28:49 -0800160 // Fills out the ProfiledJointStatus structure with the current state.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700161 template <class StatusTypeBuilder>
162 StatusTypeBuilder BuildStatus(
163 flatbuffers::FlatBufferBuilder *fbb);
Austin Schuh3634ed32017-02-05 16:28:49 -0800164
Austin Schuh473a5652017-02-05 01:30:42 -0800165 // Forces the current goal to the provided goal, bypassing the profiler.
166 void ForceGoal(double goal);
James Kuszmaul4fb29762020-02-20 19:37:41 -0800167 // Sets whether to use the trapezoidal profiler or whether to just bypass it
168 // and pass the unprofiled goal through directly.
169 void set_enable_profile(bool enable) { enable_profile_ = enable; }
Austin Schuh473a5652017-02-05 01:30:42 -0800170 // Sets the unprofiled goal. The profiler will generate a profile to go to
171 // this goal.
James Kuszmaul4fb29762020-02-20 19:37:41 -0800172 void set_unprofiled_goal(double unprofiled_goal,
Sabina Davis0e484f92020-02-23 17:47:53 -0800173 double unprofiled_goal_velocity = 0.0,
174 bool print = true);
Austin Schuh473a5652017-02-05 01:30:42 -0800175 // Limits our profiles to a max velocity and acceleration for proper motion.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700176 void AdjustProfile(const ::frc971::ProfileParameters *profile_parameters);
Austin Schuh473a5652017-02-05 01:30:42 -0800177 void AdjustProfile(double max_angular_velocity,
178 double max_angular_acceleration);
179
180 // Returns true if we have exceeded any hard limits.
181 bool CheckHardLimits();
182
Austin Schuh3634ed32017-02-05 16:28:49 -0800183 // Returns the requested voltage.
Adam Snaider79900c22017-02-08 20:23:15 -0800184 double voltage() const { return this->loop_->U(0, 0); }
Austin Schuh473a5652017-02-05 01:30:42 -0800185
186 // Returns the current position.
Adam Snaider79900c22017-02-08 20:23:15 -0800187 double position() const { return this->Y_(0, 0); }
Austin Schuh473a5652017-02-05 01:30:42 -0800188
189 // For testing:
190 // Triggers an estimator error.
Adam Snaider79900c22017-02-08 20:23:15 -0800191 void TriggerEstimatorError() { this->estimators_[0].TriggerError(); }
Austin Schuh473a5652017-02-05 01:30:42 -0800192
Austin Schuh00be3a82017-02-05 19:01:40 -0800193 const ::frc971::constants::Range &range() const { return range_; }
194
Austin Schuh93ddcb42021-10-25 21:54:11 -0700195 double default_velocity() const { return default_velocity_; }
196 double default_acceleration() const { return default_acceleration_; }
197
Austin Schuh6a90cd92017-02-19 20:55:33 -0800198 protected:
Austin Schuh473a5652017-02-05 01:30:42 -0800199 // Limits the provided goal to the soft limits. Prints "name" when it fails
200 // to aid debugging.
Sabina Davis0e484f92020-02-23 17:47:53 -0800201 virtual void CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal,
202 bool print = true);
Austin Schuh473a5652017-02-05 01:30:42 -0800203
Austin Schuh6a90cd92017-02-19 20:55:33 -0800204 private:
Austin Schuh473a5652017-02-05 01:30:42 -0800205 void UpdateOffset(double offset);
206
207 aos::util::TrapezoidProfile profile_;
James Kuszmaul4fb29762020-02-20 19:37:41 -0800208 bool enable_profile_ = true;
Austin Schuh473a5652017-02-05 01:30:42 -0800209
210 // Current measurement.
211 Eigen::Matrix<double, 1, 1> Y_;
212 // Current offset. Y_ = offset_ + raw_sensor;
213 Eigen::Matrix<double, 1, 1> offset_;
214
215 const ::frc971::constants::Range range_;
216
217 const double default_velocity_;
218 const double default_acceleration_;
Austin Schuh3634ed32017-02-05 16:28:49 -0800219
220 double last_position_ = 0;
Austin Schuh473a5652017-02-05 01:30:42 -0800221};
222
Brian Silvermanab0b6772017-02-05 16:16:21 -0800223namespace internal {
224
225double UseUnlessZero(double target_value, double default_value);
226
227} // namespace internal
228
229template <class ZeroingEstimator>
230SingleDOFProfiledSubsystem<ZeroingEstimator>::SingleDOFProfiledSubsystem(
231 ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop,
232 const typename ZeroingEstimator::ZeroingConstants &zeroing_constants,
233 const ::frc971::constants::Range &range, double default_velocity,
234 double default_acceleration)
Campbell Crowley36e93e92017-12-23 14:21:43 -0800235 : ProfiledSubsystem<3, 1, ZeroingEstimator>(
236 ::std::move(loop), {{ZeroingEstimator(zeroing_constants)}}),
Sabina Davis1184cc52020-02-22 18:29:25 -0800237 profile_(this->loop_->plant().coefficients().dt),
Brian Silvermanab0b6772017-02-05 16:16:21 -0800238 range_(range),
239 default_velocity_(default_velocity),
240 default_acceleration_(default_acceleration) {
241 Y_.setZero();
242 offset_.setZero();
243 AdjustProfile(0.0, 0.0);
244}
245
246template <class ZeroingEstimator>
247void SingleDOFProfiledSubsystem<ZeroingEstimator>::UpdateOffset(double offset) {
248 const double doffset = offset - offset_(0, 0);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700249 AOS_LOG(INFO, "Adjusting offset from %f to %f\n", offset_(0, 0), offset);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800250
Adam Snaider79900c22017-02-08 20:23:15 -0800251 this->loop_->mutable_X_hat()(0, 0) += doffset;
252 this->Y_(0, 0) += doffset;
Austin Schuh3634ed32017-02-05 16:28:49 -0800253 last_position_ += doffset;
Adam Snaider79900c22017-02-08 20:23:15 -0800254 this->loop_->mutable_R(0, 0) += doffset;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800255
256 profile_.MoveGoal(doffset);
257 offset_(0, 0) = offset;
258
Adam Snaider79900c22017-02-08 20:23:15 -0800259 CapGoal("R", &this->loop_->mutable_R());
Brian Silvermanab0b6772017-02-05 16:16:21 -0800260}
261
262template <class ZeroingEstimator>
Alex Perrycb7da4b2019-08-28 19:35:56 -0700263template <class StatusTypeBuilder>
264StatusTypeBuilder SingleDOFProfiledSubsystem<ZeroingEstimator>::BuildStatus(
265 flatbuffers::FlatBufferBuilder *fbb) {
266 flatbuffers::Offset<typename ZeroingEstimator::State> estimator_state =
267 this->EstimatorState(fbb, 0);
268
269 StatusTypeBuilder builder(*fbb);
270
271 builder.add_zeroed(this->zeroed());
Austin Schuh3634ed32017-02-05 16:28:49 -0800272 // We don't know, so default to the bad case.
Austin Schuh3634ed32017-02-05 16:28:49 -0800273
Alex Perrycb7da4b2019-08-28 19:35:56 -0700274 builder.add_position(this->X_hat(0, 0));
275 builder.add_velocity(this->X_hat(1, 0));
276 builder.add_goal_position(this->goal(0, 0));
277 builder.add_goal_velocity(this->goal(1, 0));
278 builder.add_unprofiled_goal_position(this->unprofiled_goal(0, 0));
279 builder.add_unprofiled_goal_velocity(this->unprofiled_goal(1, 0));
280 builder.add_voltage_error(this->X_hat(2, 0));
281 builder.add_calculated_velocity(
Austin Schuh3634ed32017-02-05 16:28:49 -0800282 (position() - last_position_) /
Sabina Davis1184cc52020-02-22 18:29:25 -0800283 ::aos::time::DurationInSeconds(this->loop_->plant().coefficients().dt));
Austin Schuh3634ed32017-02-05 16:28:49 -0800284
Alex Perrycb7da4b2019-08-28 19:35:56 -0700285 builder.add_estimator_state(estimator_state);
Austin Schuh3634ed32017-02-05 16:28:49 -0800286
Adam Snaider79900c22017-02-08 20:23:15 -0800287 Eigen::Matrix<double, 3, 1> error = this->controller().error();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700288 builder.add_position_power(
289 this->controller().controller().K(0, 0) * error(0, 0));
290 builder.add_velocity_power(
291 this->controller().controller().K(0, 1) * error(1, 0));
292 return builder;
Austin Schuh3634ed32017-02-05 16:28:49 -0800293}
294
295template <class ZeroingEstimator>
Brian Silvermanab0b6772017-02-05 16:16:21 -0800296void SingleDOFProfiledSubsystem<ZeroingEstimator>::Correct(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700297 const typename ZeroingEstimator::Position &new_position) {
Adam Snaider79900c22017-02-08 20:23:15 -0800298 this->estimators_[0].UpdateEstimate(new_position);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800299
Adam Snaider79900c22017-02-08 20:23:15 -0800300 if (this->estimators_[0].error()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700301 AOS_LOG(ERROR, "zeroing error\n");
Brian Silvermanab0b6772017-02-05 16:16:21 -0800302 return;
303 }
304
Adam Snaider79900c22017-02-08 20:23:15 -0800305 if (!this->initialized_) {
306 if (this->estimators_[0].offset_ready()) {
307 UpdateOffset(this->estimators_[0].offset());
308 this->initialized_ = true;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800309 }
310 }
311
Adam Snaider79900c22017-02-08 20:23:15 -0800312 if (!this->zeroed(0) && this->estimators_[0].zeroed()) {
313 UpdateOffset(this->estimators_[0].offset());
314 this->set_zeroed(0, true);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800315 }
316
Austin Schuh3634ed32017-02-05 16:28:49 -0800317 last_position_ = position();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700318 this->Y_ << new_position.encoder();
Adam Snaider79900c22017-02-08 20:23:15 -0800319 this->Y_ += this->offset_;
320 this->loop_->Correct(Y_);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800321}
322
323template <class ZeroingEstimator>
324void SingleDOFProfiledSubsystem<ZeroingEstimator>::CapGoal(
Sabina Davis0e484f92020-02-23 17:47:53 -0800325 const char *name, Eigen::Matrix<double, 3, 1> *goal, bool print) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800326 // Limit the goal to min/max allowable positions.
Brian Silvermanab0b6772017-02-05 16:16:21 -0800327 if ((*goal)(0, 0) > range_.upper) {
Sabina Davis0e484f92020-02-23 17:47:53 -0800328 if (print) {
329 AOS_LOG(WARNING, "Goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
330 range_.upper);
331 }
Brian Silvermanab0b6772017-02-05 16:16:21 -0800332 (*goal)(0, 0) = range_.upper;
333 }
334 if ((*goal)(0, 0) < range_.lower) {
Sabina Davis0e484f92020-02-23 17:47:53 -0800335 if (print) {
336 AOS_LOG(WARNING, "Goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
337 range_.lower);
338 }
Brian Silvermanab0b6772017-02-05 16:16:21 -0800339 (*goal)(0, 0) = range_.lower;
340 }
341}
342
343template <class ZeroingEstimator>
344void SingleDOFProfiledSubsystem<ZeroingEstimator>::ForceGoal(double goal) {
Sabina Davis0e484f92020-02-23 17:47:53 -0800345 set_unprofiled_goal(goal, 0.0, false);
Adam Snaider79900c22017-02-08 20:23:15 -0800346 this->loop_->mutable_R() = this->unprofiled_goal_;
347 this->loop_->mutable_next_R() = this->loop_->R();
Brian Silvermanab0b6772017-02-05 16:16:21 -0800348
Adam Snaider79900c22017-02-08 20:23:15 -0800349 const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R();
350 this->profile_.MoveCurrentState(R.block<2, 1>(0, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800351}
352
353template <class ZeroingEstimator>
354void SingleDOFProfiledSubsystem<ZeroingEstimator>::set_unprofiled_goal(
Sabina Davis0e484f92020-02-23 17:47:53 -0800355 double unprofiled_goal, double unprofiled_goal_velocity, bool print) {
Adam Snaider79900c22017-02-08 20:23:15 -0800356 this->unprofiled_goal_(0, 0) = unprofiled_goal;
James Kuszmaul4fb29762020-02-20 19:37:41 -0800357 this->unprofiled_goal_(1, 0) = unprofiled_goal_velocity;
Adam Snaider79900c22017-02-08 20:23:15 -0800358 this->unprofiled_goal_(2, 0) = 0.0;
Sabina Davis0e484f92020-02-23 17:47:53 -0800359 CapGoal("unprofiled R", &this->unprofiled_goal_, print);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800360}
361
362template <class ZeroingEstimator>
363void SingleDOFProfiledSubsystem<ZeroingEstimator>::Update(bool disable) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800364 // TODO(austin): What do we want to do with the profile on reset? Also, we
365 // should probably reset R, the offset, the profile, etc.
366 if (this->should_reset_) {
367 this->loop_->mutable_X_hat(0, 0) = Y_(0, 0);
368 this->loop_->mutable_X_hat(1, 0) = 0.0;
369 this->loop_->mutable_X_hat(2, 0) = 0.0;
370 this->should_reset_ = false;
371 }
372
Brian Silvermanab0b6772017-02-05 16:16:21 -0800373 if (!disable) {
James Kuszmaul4fb29762020-02-20 19:37:41 -0800374 if (enable_profile_) {
375 ::Eigen::Matrix<double, 2, 1> goal_state = profile_.Update(
376 this->unprofiled_goal_(0, 0), this->unprofiled_goal_(1, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800377
James Kuszmaul4fb29762020-02-20 19:37:41 -0800378 this->loop_->mutable_next_R(0, 0) = goal_state(0, 0);
379 this->loop_->mutable_next_R(1, 0) = goal_state(1, 0);
380 this->loop_->mutable_next_R(2, 0) = 0.0;
381 } else {
382 this->loop_->mutable_R() = this->unprofiled_goal_;
383 this->loop_->mutable_next_R() = this->unprofiled_goal_;
384 this->loop_->mutable_next_R(0, 0) +=
385 this->unprofiled_goal_(1) *
386 aos::time::DurationInSeconds(this->loop_->plant().coefficients().dt);
387 CapGoal("R", &this->loop_->mutable_R());
388 }
Adam Snaider79900c22017-02-08 20:23:15 -0800389 CapGoal("next R", &this->loop_->mutable_next_R());
Brian Silvermanab0b6772017-02-05 16:16:21 -0800390 }
391
Adam Snaider79900c22017-02-08 20:23:15 -0800392 this->loop_->Update(disable);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800393
Adam Snaider79900c22017-02-08 20:23:15 -0800394 if (!disable && this->loop_->U(0, 0) != this->loop_->U_uncapped(0, 0)) {
395 const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R();
396 profile_.MoveCurrentState(R.block<2, 1>(0, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800397 }
398}
399
400template <class ZeroingEstimator>
401bool SingleDOFProfiledSubsystem<ZeroingEstimator>::CheckHardLimits() {
402 // Returns whether hard limits have been exceeded.
403
Austin Schuh3634ed32017-02-05 16:28:49 -0800404 if (position() > range_.upper_hard || position() < range_.lower_hard) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700405 AOS_LOG(
406 ERROR,
Brian Silvermanab0b6772017-02-05 16:16:21 -0800407 "SingleDOFProfiledSubsystem at %f out of bounds [%f, %f], ESTOPing\n",
Austin Schuh3634ed32017-02-05 16:28:49 -0800408 position(), range_.lower_hard, range_.upper_hard);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800409 return true;
410 }
411
412 return false;
413}
414
415template <class ZeroingEstimator>
416void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700417 const ::frc971::ProfileParameters *profile_parameters) {
418 AdjustProfile(
419 profile_parameters != nullptr ? profile_parameters->max_velocity() : 0.0,
420 profile_parameters != nullptr ? profile_parameters->max_acceleration()
421 : 0.0);
Austin Schuh3634ed32017-02-05 16:28:49 -0800422}
423
424template <class ZeroingEstimator>
425void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Brian Silvermanab0b6772017-02-05 16:16:21 -0800426 double max_angular_velocity, double max_angular_acceleration) {
427 profile_.set_maximum_velocity(
428 internal::UseUnlessZero(max_angular_velocity, default_velocity_));
429 profile_.set_maximum_acceleration(
430 internal::UseUnlessZero(max_angular_acceleration, default_acceleration_));
431}
432
Austin Schuh473a5652017-02-05 01:30:42 -0800433} // namespace control_loops
434} // namespace frc971
435
436#endif // FRC971_CONTROL_LOOPS_PROFILED_SUBSYSTEM_H_