blob: ef5ffde02d363eb146abdb5998b7cf21bb0066ec [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"
Austin Schuh3634ed32017-02-05 16:28:49 -080014#include "frc971/control_loops/control_loops.q.h"
15#include "frc971/control_loops/profiled_subsystem.q.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.
Adam Snaider79900c22017-02-08 20:23:15 -0800109 typename ZeroingEstimator::State EstimatorState(int index) {
Brian Silverman4f2e2ce2017-02-19 17:49:47 -0800110 return estimators_[index].GetEstimatorState();
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.
Brian Silvermanab0b6772017-02-05 16:16:21 -0800155 void Correct(typename ZeroingEstimator::Position position);
Austin Schuh473a5652017-02-05 01:30:42 -0800156 // Runs the controller and profile generator for a cycle.
157 void Update(bool disabled);
158
Austin Schuh3634ed32017-02-05 16:28:49 -0800159 // Fills out the ProfiledJointStatus structure with the current state.
Adam Snaider79900c22017-02-08 20:23:15 -0800160 template <class StatusType>
161 void PopulateStatus(StatusType *status);
Austin Schuh3634ed32017-02-05 16:28:49 -0800162
Austin Schuh473a5652017-02-05 01:30:42 -0800163 // Forces the current goal to the provided goal, bypassing the profiler.
164 void ForceGoal(double goal);
165 // Sets the unprofiled goal. The profiler will generate a profile to go to
166 // this goal.
167 void set_unprofiled_goal(double unprofiled_goal);
168 // Limits our profiles to a max velocity and acceleration for proper motion.
Austin Schuh3634ed32017-02-05 16:28:49 -0800169 void AdjustProfile(const ::frc971::ProfileParameters &profile_parameters);
Austin Schuh473a5652017-02-05 01:30:42 -0800170 void AdjustProfile(double max_angular_velocity,
171 double max_angular_acceleration);
172
173 // Returns true if we have exceeded any hard limits.
174 bool CheckHardLimits();
175
Austin Schuh3634ed32017-02-05 16:28:49 -0800176 // Returns the requested voltage.
Adam Snaider79900c22017-02-08 20:23:15 -0800177 double voltage() const { return this->loop_->U(0, 0); }
Austin Schuh473a5652017-02-05 01:30:42 -0800178
179 // Returns the current position.
Adam Snaider79900c22017-02-08 20:23:15 -0800180 double position() const { return this->Y_(0, 0); }
Austin Schuh473a5652017-02-05 01:30:42 -0800181
182 // For testing:
183 // Triggers an estimator error.
Adam Snaider79900c22017-02-08 20:23:15 -0800184 void TriggerEstimatorError() { this->estimators_[0].TriggerError(); }
Austin Schuh473a5652017-02-05 01:30:42 -0800185
Austin Schuh00be3a82017-02-05 19:01:40 -0800186 const ::frc971::constants::Range &range() const { return range_; }
187
Austin Schuh6a90cd92017-02-19 20:55:33 -0800188 protected:
Austin Schuh473a5652017-02-05 01:30:42 -0800189 // Limits the provided goal to the soft limits. Prints "name" when it fails
190 // to aid debugging.
Austin Schuh6a90cd92017-02-19 20:55:33 -0800191 virtual void CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal);
Austin Schuh473a5652017-02-05 01:30:42 -0800192
Austin Schuh6a90cd92017-02-19 20:55:33 -0800193 private:
Austin Schuh473a5652017-02-05 01:30:42 -0800194 void UpdateOffset(double offset);
195
196 aos::util::TrapezoidProfile profile_;
197
198 // Current measurement.
199 Eigen::Matrix<double, 1, 1> Y_;
200 // Current offset. Y_ = offset_ + raw_sensor;
201 Eigen::Matrix<double, 1, 1> offset_;
202
203 const ::frc971::constants::Range range_;
204
205 const double default_velocity_;
206 const double default_acceleration_;
Austin Schuh3634ed32017-02-05 16:28:49 -0800207
208 double last_position_ = 0;
Austin Schuh473a5652017-02-05 01:30:42 -0800209};
210
Brian Silvermanab0b6772017-02-05 16:16:21 -0800211namespace internal {
212
213double UseUnlessZero(double target_value, double default_value);
214
215} // namespace internal
216
217template <class ZeroingEstimator>
218SingleDOFProfiledSubsystem<ZeroingEstimator>::SingleDOFProfiledSubsystem(
219 ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop,
220 const typename ZeroingEstimator::ZeroingConstants &zeroing_constants,
221 const ::frc971::constants::Range &range, double default_velocity,
222 double default_acceleration)
Campbell Crowley36e93e92017-12-23 14:21:43 -0800223 : ProfiledSubsystem<3, 1, ZeroingEstimator>(
224 ::std::move(loop), {{ZeroingEstimator(zeroing_constants)}}),
Brian Silvermanab0b6772017-02-05 16:16:21 -0800225 profile_(::aos::controls::kLoopFrequency),
226 range_(range),
227 default_velocity_(default_velocity),
228 default_acceleration_(default_acceleration) {
229 Y_.setZero();
230 offset_.setZero();
231 AdjustProfile(0.0, 0.0);
232}
233
234template <class ZeroingEstimator>
235void SingleDOFProfiledSubsystem<ZeroingEstimator>::UpdateOffset(double offset) {
236 const double doffset = offset - offset_(0, 0);
237 LOG(INFO, "Adjusting offset from %f to %f\n", offset_(0, 0), offset);
238
Adam Snaider79900c22017-02-08 20:23:15 -0800239 this->loop_->mutable_X_hat()(0, 0) += doffset;
240 this->Y_(0, 0) += doffset;
Austin Schuh3634ed32017-02-05 16:28:49 -0800241 last_position_ += doffset;
Adam Snaider79900c22017-02-08 20:23:15 -0800242 this->loop_->mutable_R(0, 0) += doffset;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800243
244 profile_.MoveGoal(doffset);
245 offset_(0, 0) = offset;
246
Adam Snaider79900c22017-02-08 20:23:15 -0800247 CapGoal("R", &this->loop_->mutable_R());
Brian Silvermanab0b6772017-02-05 16:16:21 -0800248}
249
250template <class ZeroingEstimator>
Adam Snaider79900c22017-02-08 20:23:15 -0800251template <class StatusType>
Austin Schuh3634ed32017-02-05 16:28:49 -0800252void SingleDOFProfiledSubsystem<ZeroingEstimator>::PopulateStatus(
Adam Snaider79900c22017-02-08 20:23:15 -0800253 StatusType *status) {
254 status->zeroed = this->zeroed();
Austin Schuh00be3a82017-02-05 19:01:40 -0800255 status->state = -1;
Austin Schuh3634ed32017-02-05 16:28:49 -0800256 // We don't know, so default to the bad case.
257 status->estopped = true;
258
Adam Snaider79900c22017-02-08 20:23:15 -0800259 status->position = this->X_hat(0, 0);
260 status->velocity = this->X_hat(1, 0);
261 status->goal_position = this->goal(0, 0);
262 status->goal_velocity = this->goal(1, 0);
263 status->unprofiled_goal_position = this->unprofiled_goal(0, 0);
264 status->unprofiled_goal_velocity = this->unprofiled_goal(1, 0);
265 status->voltage_error = this->X_hat(2, 0);
Austin Schuh3634ed32017-02-05 16:28:49 -0800266 status->calculated_velocity =
267 (position() - last_position_) /
268 ::std::chrono::duration_cast<::std::chrono::duration<double>>(
269 ::aos::controls::kLoopFrequency)
270 .count();
271
Adam Snaider79900c22017-02-08 20:23:15 -0800272 status->estimator_state = this->EstimatorState(0);
Austin Schuh3634ed32017-02-05 16:28:49 -0800273
Adam Snaider79900c22017-02-08 20:23:15 -0800274 Eigen::Matrix<double, 3, 1> error = this->controller().error();
Austin Schuh32501832017-02-25 18:32:56 -0800275 status->position_power =
276 this->controller().controller().K(0, 0) * error(0, 0);
277 status->velocity_power =
278 this->controller().controller().K(0, 1) * error(1, 0);
Austin Schuh3634ed32017-02-05 16:28:49 -0800279}
280
281template <class ZeroingEstimator>
Brian Silvermanab0b6772017-02-05 16:16:21 -0800282void SingleDOFProfiledSubsystem<ZeroingEstimator>::Correct(
Austin Schuh3634ed32017-02-05 16:28:49 -0800283 typename ZeroingEstimator::Position new_position) {
Adam Snaider79900c22017-02-08 20:23:15 -0800284 this->estimators_[0].UpdateEstimate(new_position);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800285
Adam Snaider79900c22017-02-08 20:23:15 -0800286 if (this->estimators_[0].error()) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800287 LOG(ERROR, "zeroing error\n");
Brian Silvermanab0b6772017-02-05 16:16:21 -0800288 return;
289 }
290
Adam Snaider79900c22017-02-08 20:23:15 -0800291 if (!this->initialized_) {
292 if (this->estimators_[0].offset_ready()) {
293 UpdateOffset(this->estimators_[0].offset());
294 this->initialized_ = true;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800295 }
296 }
297
Adam Snaider79900c22017-02-08 20:23:15 -0800298 if (!this->zeroed(0) && this->estimators_[0].zeroed()) {
299 UpdateOffset(this->estimators_[0].offset());
300 this->set_zeroed(0, true);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800301 }
302
Austin Schuh3634ed32017-02-05 16:28:49 -0800303 last_position_ = position();
Adam Snaider79900c22017-02-08 20:23:15 -0800304 this->Y_ << new_position.encoder;
305 this->Y_ += this->offset_;
306 this->loop_->Correct(Y_);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800307}
308
309template <class ZeroingEstimator>
310void SingleDOFProfiledSubsystem<ZeroingEstimator>::CapGoal(
311 const char *name, Eigen::Matrix<double, 3, 1> *goal) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800312 // Limit the goal to min/max allowable positions.
Brian Silvermanab0b6772017-02-05 16:16:21 -0800313 if ((*goal)(0, 0) > range_.upper) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800314 LOG(WARNING, "Goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
Brian Silvermanab0b6772017-02-05 16:16:21 -0800315 range_.upper);
316 (*goal)(0, 0) = range_.upper;
317 }
318 if ((*goal)(0, 0) < range_.lower) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800319 LOG(WARNING, "Goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
Brian Silvermanab0b6772017-02-05 16:16:21 -0800320 range_.lower);
321 (*goal)(0, 0) = range_.lower;
322 }
323}
324
325template <class ZeroingEstimator>
326void SingleDOFProfiledSubsystem<ZeroingEstimator>::ForceGoal(double goal) {
327 set_unprofiled_goal(goal);
Adam Snaider79900c22017-02-08 20:23:15 -0800328 this->loop_->mutable_R() = this->unprofiled_goal_;
329 this->loop_->mutable_next_R() = this->loop_->R();
Brian Silvermanab0b6772017-02-05 16:16:21 -0800330
Adam Snaider79900c22017-02-08 20:23:15 -0800331 const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R();
332 this->profile_.MoveCurrentState(R.block<2, 1>(0, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800333}
334
335template <class ZeroingEstimator>
336void SingleDOFProfiledSubsystem<ZeroingEstimator>::set_unprofiled_goal(
337 double unprofiled_goal) {
Adam Snaider79900c22017-02-08 20:23:15 -0800338 this->unprofiled_goal_(0, 0) = unprofiled_goal;
339 this->unprofiled_goal_(1, 0) = 0.0;
340 this->unprofiled_goal_(2, 0) = 0.0;
341 CapGoal("unprofiled R", &this->unprofiled_goal_);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800342}
343
344template <class ZeroingEstimator>
345void SingleDOFProfiledSubsystem<ZeroingEstimator>::Update(bool disable) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800346 // TODO(austin): What do we want to do with the profile on reset? Also, we
347 // should probably reset R, the offset, the profile, etc.
348 if (this->should_reset_) {
349 this->loop_->mutable_X_hat(0, 0) = Y_(0, 0);
350 this->loop_->mutable_X_hat(1, 0) = 0.0;
351 this->loop_->mutable_X_hat(2, 0) = 0.0;
352 this->should_reset_ = false;
353 }
354
Brian Silvermanab0b6772017-02-05 16:16:21 -0800355 if (!disable) {
Adam Snaider79900c22017-02-08 20:23:15 -0800356 ::Eigen::Matrix<double, 2, 1> goal_state = profile_.Update(
357 this->unprofiled_goal_(0, 0), this->unprofiled_goal_(1, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800358
Adam Snaider79900c22017-02-08 20:23:15 -0800359 this->loop_->mutable_next_R(0, 0) = goal_state(0, 0);
360 this->loop_->mutable_next_R(1, 0) = goal_state(1, 0);
361 this->loop_->mutable_next_R(2, 0) = 0.0;
362 CapGoal("next R", &this->loop_->mutable_next_R());
Brian Silvermanab0b6772017-02-05 16:16:21 -0800363 }
364
Adam Snaider79900c22017-02-08 20:23:15 -0800365 this->loop_->Update(disable);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800366
Adam Snaider79900c22017-02-08 20:23:15 -0800367 if (!disable && this->loop_->U(0, 0) != this->loop_->U_uncapped(0, 0)) {
368 const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R();
369 profile_.MoveCurrentState(R.block<2, 1>(0, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800370 }
371}
372
373template <class ZeroingEstimator>
374bool SingleDOFProfiledSubsystem<ZeroingEstimator>::CheckHardLimits() {
375 // Returns whether hard limits have been exceeded.
376
Austin Schuh3634ed32017-02-05 16:28:49 -0800377 if (position() > range_.upper_hard || position() < range_.lower_hard) {
Brian Silvermanab0b6772017-02-05 16:16:21 -0800378 LOG(ERROR,
379 "SingleDOFProfiledSubsystem at %f out of bounds [%f, %f], ESTOPing\n",
Austin Schuh3634ed32017-02-05 16:28:49 -0800380 position(), range_.lower_hard, range_.upper_hard);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800381 return true;
382 }
383
384 return false;
385}
386
387template <class ZeroingEstimator>
388void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Austin Schuh3634ed32017-02-05 16:28:49 -0800389 const ::frc971::ProfileParameters &profile_parameters) {
390 AdjustProfile(profile_parameters.max_velocity,
391 profile_parameters.max_acceleration);
392}
393
394template <class ZeroingEstimator>
395void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Brian Silvermanab0b6772017-02-05 16:16:21 -0800396 double max_angular_velocity, double max_angular_acceleration) {
397 profile_.set_maximum_velocity(
398 internal::UseUnlessZero(max_angular_velocity, default_velocity_));
399 profile_.set_maximum_acceleration(
400 internal::UseUnlessZero(max_angular_acceleration, default_acceleration_));
401}
402
Austin Schuh473a5652017-02-05 01:30:42 -0800403} // namespace control_loops
404} // namespace frc971
405
406#endif // FRC971_CONTROL_LOOPS_PROFILED_SUBSYSTEM_H_