blob: d16c17da6d244810042b7d7e800dbebe1832e277 [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
John Park33858a32018-09-28 23:05:48 -070011#include "aos/controls/control_loop.h"
12#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 Schuh6a90cd92017-02-19 20:55:33 -0800195 protected:
Austin Schuh473a5652017-02-05 01:30:42 -0800196 // Limits the provided goal to the soft limits. Prints "name" when it fails
197 // to aid debugging.
Sabina Davis0e484f92020-02-23 17:47:53 -0800198 virtual void CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal,
199 bool print = true);
Austin Schuh473a5652017-02-05 01:30:42 -0800200
Austin Schuh6a90cd92017-02-19 20:55:33 -0800201 private:
Austin Schuh473a5652017-02-05 01:30:42 -0800202 void UpdateOffset(double offset);
203
204 aos::util::TrapezoidProfile profile_;
James Kuszmaul4fb29762020-02-20 19:37:41 -0800205 bool enable_profile_ = true;
Austin Schuh473a5652017-02-05 01:30:42 -0800206
207 // Current measurement.
208 Eigen::Matrix<double, 1, 1> Y_;
209 // Current offset. Y_ = offset_ + raw_sensor;
210 Eigen::Matrix<double, 1, 1> offset_;
211
212 const ::frc971::constants::Range range_;
213
214 const double default_velocity_;
215 const double default_acceleration_;
Austin Schuh3634ed32017-02-05 16:28:49 -0800216
217 double last_position_ = 0;
Austin Schuh473a5652017-02-05 01:30:42 -0800218};
219
Brian Silvermanab0b6772017-02-05 16:16:21 -0800220namespace internal {
221
222double UseUnlessZero(double target_value, double default_value);
223
224} // namespace internal
225
226template <class ZeroingEstimator>
227SingleDOFProfiledSubsystem<ZeroingEstimator>::SingleDOFProfiledSubsystem(
228 ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop,
229 const typename ZeroingEstimator::ZeroingConstants &zeroing_constants,
230 const ::frc971::constants::Range &range, double default_velocity,
231 double default_acceleration)
Campbell Crowley36e93e92017-12-23 14:21:43 -0800232 : ProfiledSubsystem<3, 1, ZeroingEstimator>(
233 ::std::move(loop), {{ZeroingEstimator(zeroing_constants)}}),
Sabina Davis1184cc52020-02-22 18:29:25 -0800234 profile_(this->loop_->plant().coefficients().dt),
Brian Silvermanab0b6772017-02-05 16:16:21 -0800235 range_(range),
236 default_velocity_(default_velocity),
237 default_acceleration_(default_acceleration) {
238 Y_.setZero();
239 offset_.setZero();
240 AdjustProfile(0.0, 0.0);
241}
242
243template <class ZeroingEstimator>
244void SingleDOFProfiledSubsystem<ZeroingEstimator>::UpdateOffset(double offset) {
245 const double doffset = offset - offset_(0, 0);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700246 AOS_LOG(INFO, "Adjusting offset from %f to %f\n", offset_(0, 0), offset);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800247
Adam Snaider79900c22017-02-08 20:23:15 -0800248 this->loop_->mutable_X_hat()(0, 0) += doffset;
249 this->Y_(0, 0) += doffset;
Austin Schuh3634ed32017-02-05 16:28:49 -0800250 last_position_ += doffset;
Adam Snaider79900c22017-02-08 20:23:15 -0800251 this->loop_->mutable_R(0, 0) += doffset;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800252
253 profile_.MoveGoal(doffset);
254 offset_(0, 0) = offset;
255
Adam Snaider79900c22017-02-08 20:23:15 -0800256 CapGoal("R", &this->loop_->mutable_R());
Brian Silvermanab0b6772017-02-05 16:16:21 -0800257}
258
259template <class ZeroingEstimator>
Alex Perrycb7da4b2019-08-28 19:35:56 -0700260template <class StatusTypeBuilder>
261StatusTypeBuilder SingleDOFProfiledSubsystem<ZeroingEstimator>::BuildStatus(
262 flatbuffers::FlatBufferBuilder *fbb) {
263 flatbuffers::Offset<typename ZeroingEstimator::State> estimator_state =
264 this->EstimatorState(fbb, 0);
265
266 StatusTypeBuilder builder(*fbb);
267
268 builder.add_zeroed(this->zeroed());
Austin Schuh3634ed32017-02-05 16:28:49 -0800269 // We don't know, so default to the bad case.
Austin Schuh3634ed32017-02-05 16:28:49 -0800270
Alex Perrycb7da4b2019-08-28 19:35:56 -0700271 builder.add_position(this->X_hat(0, 0));
272 builder.add_velocity(this->X_hat(1, 0));
273 builder.add_goal_position(this->goal(0, 0));
274 builder.add_goal_velocity(this->goal(1, 0));
275 builder.add_unprofiled_goal_position(this->unprofiled_goal(0, 0));
276 builder.add_unprofiled_goal_velocity(this->unprofiled_goal(1, 0));
277 builder.add_voltage_error(this->X_hat(2, 0));
278 builder.add_calculated_velocity(
Austin Schuh3634ed32017-02-05 16:28:49 -0800279 (position() - last_position_) /
Sabina Davis1184cc52020-02-22 18:29:25 -0800280 ::aos::time::DurationInSeconds(this->loop_->plant().coefficients().dt));
Austin Schuh3634ed32017-02-05 16:28:49 -0800281
Alex Perrycb7da4b2019-08-28 19:35:56 -0700282 builder.add_estimator_state(estimator_state);
Austin Schuh3634ed32017-02-05 16:28:49 -0800283
Adam Snaider79900c22017-02-08 20:23:15 -0800284 Eigen::Matrix<double, 3, 1> error = this->controller().error();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700285 builder.add_position_power(
286 this->controller().controller().K(0, 0) * error(0, 0));
287 builder.add_velocity_power(
288 this->controller().controller().K(0, 1) * error(1, 0));
289 return builder;
Austin Schuh3634ed32017-02-05 16:28:49 -0800290}
291
292template <class ZeroingEstimator>
Brian Silvermanab0b6772017-02-05 16:16:21 -0800293void SingleDOFProfiledSubsystem<ZeroingEstimator>::Correct(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700294 const typename ZeroingEstimator::Position &new_position) {
Adam Snaider79900c22017-02-08 20:23:15 -0800295 this->estimators_[0].UpdateEstimate(new_position);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800296
Adam Snaider79900c22017-02-08 20:23:15 -0800297 if (this->estimators_[0].error()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700298 AOS_LOG(ERROR, "zeroing error\n");
Brian Silvermanab0b6772017-02-05 16:16:21 -0800299 return;
300 }
301
Adam Snaider79900c22017-02-08 20:23:15 -0800302 if (!this->initialized_) {
303 if (this->estimators_[0].offset_ready()) {
304 UpdateOffset(this->estimators_[0].offset());
305 this->initialized_ = true;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800306 }
307 }
308
Adam Snaider79900c22017-02-08 20:23:15 -0800309 if (!this->zeroed(0) && this->estimators_[0].zeroed()) {
310 UpdateOffset(this->estimators_[0].offset());
311 this->set_zeroed(0, true);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800312 }
313
Austin Schuh3634ed32017-02-05 16:28:49 -0800314 last_position_ = position();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700315 this->Y_ << new_position.encoder();
Adam Snaider79900c22017-02-08 20:23:15 -0800316 this->Y_ += this->offset_;
317 this->loop_->Correct(Y_);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800318}
319
320template <class ZeroingEstimator>
321void SingleDOFProfiledSubsystem<ZeroingEstimator>::CapGoal(
Sabina Davis0e484f92020-02-23 17:47:53 -0800322 const char *name, Eigen::Matrix<double, 3, 1> *goal, bool print) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800323 // Limit the goal to min/max allowable positions.
Brian Silvermanab0b6772017-02-05 16:16:21 -0800324 if ((*goal)(0, 0) > range_.upper) {
Sabina Davis0e484f92020-02-23 17:47:53 -0800325 if (print) {
326 AOS_LOG(WARNING, "Goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
327 range_.upper);
328 }
Brian Silvermanab0b6772017-02-05 16:16:21 -0800329 (*goal)(0, 0) = range_.upper;
330 }
331 if ((*goal)(0, 0) < range_.lower) {
Sabina Davis0e484f92020-02-23 17:47:53 -0800332 if (print) {
333 AOS_LOG(WARNING, "Goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
334 range_.lower);
335 }
Brian Silvermanab0b6772017-02-05 16:16:21 -0800336 (*goal)(0, 0) = range_.lower;
337 }
338}
339
340template <class ZeroingEstimator>
341void SingleDOFProfiledSubsystem<ZeroingEstimator>::ForceGoal(double goal) {
Sabina Davis0e484f92020-02-23 17:47:53 -0800342 set_unprofiled_goal(goal, 0.0, false);
Adam Snaider79900c22017-02-08 20:23:15 -0800343 this->loop_->mutable_R() = this->unprofiled_goal_;
344 this->loop_->mutable_next_R() = this->loop_->R();
Brian Silvermanab0b6772017-02-05 16:16:21 -0800345
Adam Snaider79900c22017-02-08 20:23:15 -0800346 const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R();
347 this->profile_.MoveCurrentState(R.block<2, 1>(0, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800348}
349
350template <class ZeroingEstimator>
351void SingleDOFProfiledSubsystem<ZeroingEstimator>::set_unprofiled_goal(
Sabina Davis0e484f92020-02-23 17:47:53 -0800352 double unprofiled_goal, double unprofiled_goal_velocity, bool print) {
Adam Snaider79900c22017-02-08 20:23:15 -0800353 this->unprofiled_goal_(0, 0) = unprofiled_goal;
James Kuszmaul4fb29762020-02-20 19:37:41 -0800354 this->unprofiled_goal_(1, 0) = unprofiled_goal_velocity;
Adam Snaider79900c22017-02-08 20:23:15 -0800355 this->unprofiled_goal_(2, 0) = 0.0;
Sabina Davis0e484f92020-02-23 17:47:53 -0800356 CapGoal("unprofiled R", &this->unprofiled_goal_, print);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800357}
358
359template <class ZeroingEstimator>
360void SingleDOFProfiledSubsystem<ZeroingEstimator>::Update(bool disable) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800361 // TODO(austin): What do we want to do with the profile on reset? Also, we
362 // should probably reset R, the offset, the profile, etc.
363 if (this->should_reset_) {
364 this->loop_->mutable_X_hat(0, 0) = Y_(0, 0);
365 this->loop_->mutable_X_hat(1, 0) = 0.0;
366 this->loop_->mutable_X_hat(2, 0) = 0.0;
367 this->should_reset_ = false;
368 }
369
Brian Silvermanab0b6772017-02-05 16:16:21 -0800370 if (!disable) {
James Kuszmaul4fb29762020-02-20 19:37:41 -0800371 if (enable_profile_) {
372 ::Eigen::Matrix<double, 2, 1> goal_state = profile_.Update(
373 this->unprofiled_goal_(0, 0), this->unprofiled_goal_(1, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800374
James Kuszmaul4fb29762020-02-20 19:37:41 -0800375 this->loop_->mutable_next_R(0, 0) = goal_state(0, 0);
376 this->loop_->mutable_next_R(1, 0) = goal_state(1, 0);
377 this->loop_->mutable_next_R(2, 0) = 0.0;
378 } else {
379 this->loop_->mutable_R() = this->unprofiled_goal_;
380 this->loop_->mutable_next_R() = this->unprofiled_goal_;
381 this->loop_->mutable_next_R(0, 0) +=
382 this->unprofiled_goal_(1) *
383 aos::time::DurationInSeconds(this->loop_->plant().coefficients().dt);
384 CapGoal("R", &this->loop_->mutable_R());
385 }
Adam Snaider79900c22017-02-08 20:23:15 -0800386 CapGoal("next R", &this->loop_->mutable_next_R());
Brian Silvermanab0b6772017-02-05 16:16:21 -0800387 }
388
Adam Snaider79900c22017-02-08 20:23:15 -0800389 this->loop_->Update(disable);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800390
Adam Snaider79900c22017-02-08 20:23:15 -0800391 if (!disable && this->loop_->U(0, 0) != this->loop_->U_uncapped(0, 0)) {
392 const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R();
393 profile_.MoveCurrentState(R.block<2, 1>(0, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800394 }
395}
396
397template <class ZeroingEstimator>
398bool SingleDOFProfiledSubsystem<ZeroingEstimator>::CheckHardLimits() {
399 // Returns whether hard limits have been exceeded.
400
Austin Schuh3634ed32017-02-05 16:28:49 -0800401 if (position() > range_.upper_hard || position() < range_.lower_hard) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700402 AOS_LOG(
403 ERROR,
Brian Silvermanab0b6772017-02-05 16:16:21 -0800404 "SingleDOFProfiledSubsystem at %f out of bounds [%f, %f], ESTOPing\n",
Austin Schuh3634ed32017-02-05 16:28:49 -0800405 position(), range_.lower_hard, range_.upper_hard);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800406 return true;
407 }
408
409 return false;
410}
411
412template <class ZeroingEstimator>
413void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Alex Perrycb7da4b2019-08-28 19:35:56 -0700414 const ::frc971::ProfileParameters *profile_parameters) {
415 AdjustProfile(
416 profile_parameters != nullptr ? profile_parameters->max_velocity() : 0.0,
417 profile_parameters != nullptr ? profile_parameters->max_acceleration()
418 : 0.0);
Austin Schuh3634ed32017-02-05 16:28:49 -0800419}
420
421template <class ZeroingEstimator>
422void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Brian Silvermanab0b6772017-02-05 16:16:21 -0800423 double max_angular_velocity, double max_angular_acceleration) {
424 profile_.set_maximum_velocity(
425 internal::UseUnlessZero(max_angular_velocity, default_velocity_));
426 profile_.set_maximum_acceleration(
427 internal::UseUnlessZero(max_angular_acceleration, default_acceleration_));
428}
429
Austin Schuh473a5652017-02-05 01:30:42 -0800430} // namespace control_loops
431} // namespace frc971
432
433#endif // FRC971_CONTROL_LOOPS_PROFILED_SUBSYSTEM_H_