blob: d98b5c9bf538dc88445888c5cc99884db5497b99 [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"
Austin Schuh3634ed32017-02-05 16:28:49 -080013#include "frc971/control_loops/control_loops.q.h"
14#include "frc971/control_loops/profiled_subsystem.q.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"
Austin Schuh00be3a82017-02-05 19:01:40 -080018#include "frc971/constants.h"
Austin Schuh473a5652017-02-05 01:30:42 -080019
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.
Austin Schuh08d9ecf2017-03-05 00:58:48 -080062 const StateFeedbackLoop<number_of_states, number_of_inputs, number_of_outputs> &
Tyler Chatowf8f03112017-02-05 14:31:34 -080063 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
69 // Returns whether the estimators have been initialized and zeroed.
70 bool initialized() const { return initialized_; }
71
72 bool zeroed() const {
73 for (int i = 0; i < number_of_axes; ++i) {
74 if (!zeroed_[i]) {
75 return false;
76 }
77 }
78 return true;
79 }
80
81 bool zeroed(int index) const { return zeroed_[index]; };
82
83 // Returns the filtered goal.
84 const Eigen::Matrix<double, number_of_states, 1> &goal() const {
85 return loop_->R();
86 }
87 double goal(int row, int col) const { return loop_->R(row, col); }
88
89 // Returns the unprofiled goal.
90 const Eigen::Matrix<double, number_of_states, 1> &unprofiled_goal() const {
91 return unprofiled_goal_;
92 }
93 double unprofiled_goal(int row, int col) const {
94 return unprofiled_goal_(row, col);
95 }
96
97 // Returns the current state estimate.
98 const Eigen::Matrix<double, number_of_states, 1> &X_hat() const {
99 return loop_->X_hat();
100 }
101 double X_hat(int row, int col) const { return loop_->X_hat(row, col); }
Austin Schuhd5ccb862017-03-11 22:06:36 -0800102 double &mutable_X_hat(int row, int col) const {
103 return loop_->mutable_X_hat(row, col);
104 }
Austin Schuh473a5652017-02-05 01:30:42 -0800105
106 // Returns the current internal estimator state for logging.
Adam Snaider79900c22017-02-08 20:23:15 -0800107 typename ZeroingEstimator::State EstimatorState(int index) {
Brian Silverman4f2e2ce2017-02-19 17:49:47 -0800108 return estimators_[index].GetEstimatorState();
Austin Schuh473a5652017-02-05 01:30:42 -0800109 }
110
111 // Sets the maximum voltage that will be commanded by the loop.
Austin Schuh08d9ecf2017-03-05 00:58:48 -0800112 void set_max_voltage(::std::array<double, number_of_inputs> voltages) {
113 for (int i = 0; i < number_of_inputs; ++i) {
Austin Schuh473a5652017-02-05 01:30:42 -0800114 loop_->set_max_voltage(i, voltages[i]);
115 }
116 }
117
118 protected:
119 void set_zeroed(int index, bool val) { zeroed_[index] = val; }
120
Austin Schuh473a5652017-02-05 01:30:42 -0800121 ::std::unique_ptr<::frc971::control_loops::SimpleCappedStateFeedbackLoop<
Austin Schuh08d9ecf2017-03-05 00:58:48 -0800122 number_of_states, number_of_inputs, number_of_outputs>>
123 loop_;
Austin Schuh473a5652017-02-05 01:30:42 -0800124
125 // The goal that the profile tries to reach.
126 Eigen::Matrix<double, number_of_states, 1> unprofiled_goal_;
127
128 bool initialized_ = false;
129
Austin Schuhd5ccb862017-03-11 22:06:36 -0800130 // If true, the subclass should reset in Update. It should then clear this
131 // flag.
132 bool should_reset_ = true;
133
Brian Silvermanab0b6772017-02-05 16:16:21 -0800134 ::std::array<ZeroingEstimator, number_of_axes> estimators_;
Austin Schuh473a5652017-02-05 01:30:42 -0800135
136 private:
137 ::std::array<bool, number_of_axes> zeroed_;
138};
139
Adam Snaider79900c22017-02-08 20:23:15 -0800140template <typename ZeroingEstimator =
Brian Silvermanab0b6772017-02-05 16:16:21 -0800141 ::frc971::zeroing::PotAndIndexPulseZeroingEstimator>
Austin Schuh473a5652017-02-05 01:30:42 -0800142class SingleDOFProfiledSubsystem
Adam Snaider79900c22017-02-08 20:23:15 -0800143 : public ::frc971::control_loops::ProfiledSubsystem<3, 1, ZeroingEstimator> {
Austin Schuh473a5652017-02-05 01:30:42 -0800144 public:
145 SingleDOFProfiledSubsystem(
146 ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop,
Brian Silvermanab0b6772017-02-05 16:16:21 -0800147 const typename ZeroingEstimator::ZeroingConstants &zeroing_constants,
Austin Schuh473a5652017-02-05 01:30:42 -0800148 const ::frc971::constants::Range &range, double default_angular_velocity,
149 double default_angular_acceleration);
150
151 // Updates our estimator with the latest position.
Brian Silvermanab0b6772017-02-05 16:16:21 -0800152 void Correct(typename ZeroingEstimator::Position position);
Austin Schuh473a5652017-02-05 01:30:42 -0800153 // Runs the controller and profile generator for a cycle.
154 void Update(bool disabled);
155
Austin Schuh3634ed32017-02-05 16:28:49 -0800156 // Fills out the ProfiledJointStatus structure with the current state.
Adam Snaider79900c22017-02-08 20:23:15 -0800157 template <class StatusType>
158 void PopulateStatus(StatusType *status);
Austin Schuh3634ed32017-02-05 16:28:49 -0800159
Austin Schuh473a5652017-02-05 01:30:42 -0800160 // Forces the current goal to the provided goal, bypassing the profiler.
161 void ForceGoal(double goal);
162 // Sets the unprofiled goal. The profiler will generate a profile to go to
163 // this goal.
164 void set_unprofiled_goal(double unprofiled_goal);
165 // Limits our profiles to a max velocity and acceleration for proper motion.
Austin Schuh3634ed32017-02-05 16:28:49 -0800166 void AdjustProfile(const ::frc971::ProfileParameters &profile_parameters);
Austin Schuh473a5652017-02-05 01:30:42 -0800167 void AdjustProfile(double max_angular_velocity,
168 double max_angular_acceleration);
169
170 // Returns true if we have exceeded any hard limits.
171 bool CheckHardLimits();
172
Austin Schuh3634ed32017-02-05 16:28:49 -0800173 // Returns the requested voltage.
Adam Snaider79900c22017-02-08 20:23:15 -0800174 double voltage() const { return this->loop_->U(0, 0); }
Austin Schuh473a5652017-02-05 01:30:42 -0800175
176 // Returns the current position.
Adam Snaider79900c22017-02-08 20:23:15 -0800177 double position() const { return this->Y_(0, 0); }
Austin Schuh473a5652017-02-05 01:30:42 -0800178
179 // For testing:
180 // Triggers an estimator error.
Adam Snaider79900c22017-02-08 20:23:15 -0800181 void TriggerEstimatorError() { this->estimators_[0].TriggerError(); }
Austin Schuh473a5652017-02-05 01:30:42 -0800182
Austin Schuh00be3a82017-02-05 19:01:40 -0800183 const ::frc971::constants::Range &range() const { return range_; }
184
Austin Schuh6a90cd92017-02-19 20:55:33 -0800185 protected:
Austin Schuh473a5652017-02-05 01:30:42 -0800186 // Limits the provided goal to the soft limits. Prints "name" when it fails
187 // to aid debugging.
Austin Schuh6a90cd92017-02-19 20:55:33 -0800188 virtual void CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal);
Austin Schuh473a5652017-02-05 01:30:42 -0800189
Austin Schuh6a90cd92017-02-19 20:55:33 -0800190 private:
Austin Schuh473a5652017-02-05 01:30:42 -0800191 void UpdateOffset(double offset);
192
193 aos::util::TrapezoidProfile profile_;
194
195 // Current measurement.
196 Eigen::Matrix<double, 1, 1> Y_;
197 // Current offset. Y_ = offset_ + raw_sensor;
198 Eigen::Matrix<double, 1, 1> offset_;
199
200 const ::frc971::constants::Range range_;
201
202 const double default_velocity_;
203 const double default_acceleration_;
Austin Schuh3634ed32017-02-05 16:28:49 -0800204
205 double last_position_ = 0;
Austin Schuh473a5652017-02-05 01:30:42 -0800206};
207
Brian Silvermanab0b6772017-02-05 16:16:21 -0800208namespace internal {
209
210double UseUnlessZero(double target_value, double default_value);
211
212} // namespace internal
213
214template <class ZeroingEstimator>
215SingleDOFProfiledSubsystem<ZeroingEstimator>::SingleDOFProfiledSubsystem(
216 ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop,
217 const typename ZeroingEstimator::ZeroingConstants &zeroing_constants,
218 const ::frc971::constants::Range &range, double default_velocity,
219 double default_acceleration)
Campbell Crowley36e93e92017-12-23 14:21:43 -0800220 : ProfiledSubsystem<3, 1, ZeroingEstimator>(
221 ::std::move(loop), {{ZeroingEstimator(zeroing_constants)}}),
Brian Silvermanab0b6772017-02-05 16:16:21 -0800222 profile_(::aos::controls::kLoopFrequency),
223 range_(range),
224 default_velocity_(default_velocity),
225 default_acceleration_(default_acceleration) {
226 Y_.setZero();
227 offset_.setZero();
228 AdjustProfile(0.0, 0.0);
229}
230
231template <class ZeroingEstimator>
232void SingleDOFProfiledSubsystem<ZeroingEstimator>::UpdateOffset(double offset) {
233 const double doffset = offset - offset_(0, 0);
234 LOG(INFO, "Adjusting offset from %f to %f\n", offset_(0, 0), offset);
235
Adam Snaider79900c22017-02-08 20:23:15 -0800236 this->loop_->mutable_X_hat()(0, 0) += doffset;
237 this->Y_(0, 0) += doffset;
Austin Schuh3634ed32017-02-05 16:28:49 -0800238 last_position_ += doffset;
Adam Snaider79900c22017-02-08 20:23:15 -0800239 this->loop_->mutable_R(0, 0) += doffset;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800240
241 profile_.MoveGoal(doffset);
242 offset_(0, 0) = offset;
243
Adam Snaider79900c22017-02-08 20:23:15 -0800244 CapGoal("R", &this->loop_->mutable_R());
Brian Silvermanab0b6772017-02-05 16:16:21 -0800245}
246
247template <class ZeroingEstimator>
Adam Snaider79900c22017-02-08 20:23:15 -0800248template <class StatusType>
Austin Schuh3634ed32017-02-05 16:28:49 -0800249void SingleDOFProfiledSubsystem<ZeroingEstimator>::PopulateStatus(
Adam Snaider79900c22017-02-08 20:23:15 -0800250 StatusType *status) {
251 status->zeroed = this->zeroed();
Austin Schuh00be3a82017-02-05 19:01:40 -0800252 status->state = -1;
Austin Schuh3634ed32017-02-05 16:28:49 -0800253 // We don't know, so default to the bad case.
254 status->estopped = true;
255
Adam Snaider79900c22017-02-08 20:23:15 -0800256 status->position = this->X_hat(0, 0);
257 status->velocity = this->X_hat(1, 0);
258 status->goal_position = this->goal(0, 0);
259 status->goal_velocity = this->goal(1, 0);
260 status->unprofiled_goal_position = this->unprofiled_goal(0, 0);
261 status->unprofiled_goal_velocity = this->unprofiled_goal(1, 0);
262 status->voltage_error = this->X_hat(2, 0);
Austin Schuh3634ed32017-02-05 16:28:49 -0800263 status->calculated_velocity =
264 (position() - last_position_) /
265 ::std::chrono::duration_cast<::std::chrono::duration<double>>(
266 ::aos::controls::kLoopFrequency)
267 .count();
268
Adam Snaider79900c22017-02-08 20:23:15 -0800269 status->estimator_state = this->EstimatorState(0);
Austin Schuh3634ed32017-02-05 16:28:49 -0800270
Adam Snaider79900c22017-02-08 20:23:15 -0800271 Eigen::Matrix<double, 3, 1> error = this->controller().error();
Austin Schuh32501832017-02-25 18:32:56 -0800272 status->position_power =
273 this->controller().controller().K(0, 0) * error(0, 0);
274 status->velocity_power =
275 this->controller().controller().K(0, 1) * error(1, 0);
Austin Schuh3634ed32017-02-05 16:28:49 -0800276}
277
278template <class ZeroingEstimator>
Brian Silvermanab0b6772017-02-05 16:16:21 -0800279void SingleDOFProfiledSubsystem<ZeroingEstimator>::Correct(
Austin Schuh3634ed32017-02-05 16:28:49 -0800280 typename ZeroingEstimator::Position new_position) {
Adam Snaider79900c22017-02-08 20:23:15 -0800281 this->estimators_[0].UpdateEstimate(new_position);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800282
Adam Snaider79900c22017-02-08 20:23:15 -0800283 if (this->estimators_[0].error()) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800284 LOG(ERROR, "zeroing error\n");
Brian Silvermanab0b6772017-02-05 16:16:21 -0800285 return;
286 }
287
Adam Snaider79900c22017-02-08 20:23:15 -0800288 if (!this->initialized_) {
289 if (this->estimators_[0].offset_ready()) {
290 UpdateOffset(this->estimators_[0].offset());
291 this->initialized_ = true;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800292 }
293 }
294
Adam Snaider79900c22017-02-08 20:23:15 -0800295 if (!this->zeroed(0) && this->estimators_[0].zeroed()) {
296 UpdateOffset(this->estimators_[0].offset());
297 this->set_zeroed(0, true);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800298 }
299
Austin Schuh3634ed32017-02-05 16:28:49 -0800300 last_position_ = position();
Adam Snaider79900c22017-02-08 20:23:15 -0800301 this->Y_ << new_position.encoder;
302 this->Y_ += this->offset_;
303 this->loop_->Correct(Y_);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800304}
305
306template <class ZeroingEstimator>
307void SingleDOFProfiledSubsystem<ZeroingEstimator>::CapGoal(
308 const char *name, Eigen::Matrix<double, 3, 1> *goal) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800309 // Limit the goal to min/max allowable positions.
Brian Silvermanab0b6772017-02-05 16:16:21 -0800310 if ((*goal)(0, 0) > range_.upper) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800311 LOG(WARNING, "Goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
Brian Silvermanab0b6772017-02-05 16:16:21 -0800312 range_.upper);
313 (*goal)(0, 0) = range_.upper;
314 }
315 if ((*goal)(0, 0) < range_.lower) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800316 LOG(WARNING, "Goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
Brian Silvermanab0b6772017-02-05 16:16:21 -0800317 range_.lower);
318 (*goal)(0, 0) = range_.lower;
319 }
320}
321
322template <class ZeroingEstimator>
323void SingleDOFProfiledSubsystem<ZeroingEstimator>::ForceGoal(double goal) {
324 set_unprofiled_goal(goal);
Adam Snaider79900c22017-02-08 20:23:15 -0800325 this->loop_->mutable_R() = this->unprofiled_goal_;
326 this->loop_->mutable_next_R() = this->loop_->R();
Brian Silvermanab0b6772017-02-05 16:16:21 -0800327
Adam Snaider79900c22017-02-08 20:23:15 -0800328 const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R();
329 this->profile_.MoveCurrentState(R.block<2, 1>(0, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800330}
331
332template <class ZeroingEstimator>
333void SingleDOFProfiledSubsystem<ZeroingEstimator>::set_unprofiled_goal(
334 double unprofiled_goal) {
Adam Snaider79900c22017-02-08 20:23:15 -0800335 this->unprofiled_goal_(0, 0) = unprofiled_goal;
336 this->unprofiled_goal_(1, 0) = 0.0;
337 this->unprofiled_goal_(2, 0) = 0.0;
338 CapGoal("unprofiled R", &this->unprofiled_goal_);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800339}
340
341template <class ZeroingEstimator>
342void SingleDOFProfiledSubsystem<ZeroingEstimator>::Update(bool disable) {
Austin Schuhd5ccb862017-03-11 22:06:36 -0800343 // TODO(austin): What do we want to do with the profile on reset? Also, we
344 // should probably reset R, the offset, the profile, etc.
345 if (this->should_reset_) {
346 this->loop_->mutable_X_hat(0, 0) = Y_(0, 0);
347 this->loop_->mutable_X_hat(1, 0) = 0.0;
348 this->loop_->mutable_X_hat(2, 0) = 0.0;
349 this->should_reset_ = false;
350 }
351
Brian Silvermanab0b6772017-02-05 16:16:21 -0800352 if (!disable) {
Adam Snaider79900c22017-02-08 20:23:15 -0800353 ::Eigen::Matrix<double, 2, 1> goal_state = profile_.Update(
354 this->unprofiled_goal_(0, 0), this->unprofiled_goal_(1, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800355
Adam Snaider79900c22017-02-08 20:23:15 -0800356 this->loop_->mutable_next_R(0, 0) = goal_state(0, 0);
357 this->loop_->mutable_next_R(1, 0) = goal_state(1, 0);
358 this->loop_->mutable_next_R(2, 0) = 0.0;
359 CapGoal("next R", &this->loop_->mutable_next_R());
Brian Silvermanab0b6772017-02-05 16:16:21 -0800360 }
361
Adam Snaider79900c22017-02-08 20:23:15 -0800362 this->loop_->Update(disable);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800363
Adam Snaider79900c22017-02-08 20:23:15 -0800364 if (!disable && this->loop_->U(0, 0) != this->loop_->U_uncapped(0, 0)) {
365 const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R();
366 profile_.MoveCurrentState(R.block<2, 1>(0, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800367 }
368}
369
370template <class ZeroingEstimator>
371bool SingleDOFProfiledSubsystem<ZeroingEstimator>::CheckHardLimits() {
372 // Returns whether hard limits have been exceeded.
373
Austin Schuh3634ed32017-02-05 16:28:49 -0800374 if (position() > range_.upper_hard || position() < range_.lower_hard) {
Brian Silvermanab0b6772017-02-05 16:16:21 -0800375 LOG(ERROR,
376 "SingleDOFProfiledSubsystem at %f out of bounds [%f, %f], ESTOPing\n",
Austin Schuh3634ed32017-02-05 16:28:49 -0800377 position(), range_.lower_hard, range_.upper_hard);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800378 return true;
379 }
380
381 return false;
382}
383
384template <class ZeroingEstimator>
385void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Austin Schuh3634ed32017-02-05 16:28:49 -0800386 const ::frc971::ProfileParameters &profile_parameters) {
387 AdjustProfile(profile_parameters.max_velocity,
388 profile_parameters.max_acceleration);
389}
390
391template <class ZeroingEstimator>
392void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Brian Silvermanab0b6772017-02-05 16:16:21 -0800393 double max_angular_velocity, double max_angular_acceleration) {
394 profile_.set_maximum_velocity(
395 internal::UseUnlessZero(max_angular_velocity, default_velocity_));
396 profile_.set_maximum_acceleration(
397 internal::UseUnlessZero(max_angular_acceleration, default_acceleration_));
398}
399
Austin Schuh473a5652017-02-05 01:30:42 -0800400} // namespace control_loops
401} // namespace frc971
402
403#endif // FRC971_CONTROL_LOOPS_PROFILED_SUBSYSTEM_H_