blob: 45ef912b4e1960d3052b3b53b99cb3d3178e596a [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
11#include "aos/common/controls/control_loop.h"
12#include "aos/common/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 =
27 ::frc971::zeroing::PotAndIndexPulseZeroingEstimator>
Austin Schuh473a5652017-02-05 01:30:42 -080028class ProfiledSubsystem {
29 public:
30 ProfiledSubsystem(
31 ::std::unique_ptr<::frc971::control_loops::SimpleCappedStateFeedbackLoop<
Tyler Chatowf8f03112017-02-05 14:31:34 -080032 number_of_states, number_of_axes, number_of_axes>> loop,
Brian Silvermanab0b6772017-02-05 16:16:21 -080033 ::std::array<ZeroingEstimator, number_of_axes> &&estimators)
Austin Schuh473a5652017-02-05 01:30:42 -080034 : loop_(::std::move(loop)), estimators_(::std::move(estimators)) {
35 zeroed_.fill(false);
36 unprofiled_goal_.setZero();
37 }
38
39 // Returns whether an error has occured
40 bool error() const {
41 for (const auto &estimator : estimators_) {
42 if (estimator.error()) {
43 return true;
44 }
45 }
46 return false;
47 }
48
49 void Reset() {
50 zeroed_.fill(false);
Austin Schuh3a81d5c2017-02-05 23:13:47 -080051 initialized_ = false;
Austin Schuh473a5652017-02-05 01:30:42 -080052 for (auto &estimator : estimators_) {
53 estimator.Reset();
54 }
55 }
56
57 // Returns the controller.
Tyler Chatowf8f03112017-02-05 14:31:34 -080058 const StateFeedbackLoop<number_of_states, number_of_axes, number_of_axes> &
59 controller() const {
Austin Schuh473a5652017-02-05 01:30:42 -080060 return *loop_;
61 }
62
63 int controller_index() const { return loop_->controller_index(); }
64
65 // Returns whether the estimators have been initialized and zeroed.
66 bool initialized() const { return initialized_; }
67
68 bool zeroed() const {
69 for (int i = 0; i < number_of_axes; ++i) {
70 if (!zeroed_[i]) {
71 return false;
72 }
73 }
74 return true;
75 }
76
77 bool zeroed(int index) const { return zeroed_[index]; };
78
79 // Returns the filtered goal.
80 const Eigen::Matrix<double, number_of_states, 1> &goal() const {
81 return loop_->R();
82 }
83 double goal(int row, int col) const { return loop_->R(row, col); }
84
85 // Returns the unprofiled goal.
86 const Eigen::Matrix<double, number_of_states, 1> &unprofiled_goal() const {
87 return unprofiled_goal_;
88 }
89 double unprofiled_goal(int row, int col) const {
90 return unprofiled_goal_(row, col);
91 }
92
93 // Returns the current state estimate.
94 const Eigen::Matrix<double, number_of_states, 1> &X_hat() const {
95 return loop_->X_hat();
96 }
97 double X_hat(int row, int col) const { return loop_->X_hat(row, col); }
98
99 // Returns the current internal estimator state for logging.
Adam Snaider79900c22017-02-08 20:23:15 -0800100 typename ZeroingEstimator::State EstimatorState(int index) {
Brian Silverman4f2e2ce2017-02-19 17:49:47 -0800101 return estimators_[index].GetEstimatorState();
Austin Schuh473a5652017-02-05 01:30:42 -0800102 }
103
104 // Sets the maximum voltage that will be commanded by the loop.
105 void set_max_voltage(::std::array<double, number_of_axes> voltages) {
106 for (int i = 0; i < number_of_axes; ++i) {
107 loop_->set_max_voltage(i, voltages[i]);
108 }
109 }
110
111 protected:
112 void set_zeroed(int index, bool val) { zeroed_[index] = val; }
113
114 // TODO(austin): It's a bold assumption to assume that we will have the same
115 // number of sensors as axes. So far, that's been fine.
116 ::std::unique_ptr<::frc971::control_loops::SimpleCappedStateFeedbackLoop<
Tyler Chatowf8f03112017-02-05 14:31:34 -0800117 number_of_states, number_of_axes, number_of_axes>> loop_;
Austin Schuh473a5652017-02-05 01:30:42 -0800118
119 // The goal that the profile tries to reach.
120 Eigen::Matrix<double, number_of_states, 1> unprofiled_goal_;
121
122 bool initialized_ = false;
123
Brian Silvermanab0b6772017-02-05 16:16:21 -0800124 ::std::array<ZeroingEstimator, number_of_axes> estimators_;
Austin Schuh473a5652017-02-05 01:30:42 -0800125
126 private:
127 ::std::array<bool, number_of_axes> zeroed_;
128};
129
Adam Snaider79900c22017-02-08 20:23:15 -0800130template <typename ZeroingEstimator =
Brian Silvermanab0b6772017-02-05 16:16:21 -0800131 ::frc971::zeroing::PotAndIndexPulseZeroingEstimator>
Austin Schuh473a5652017-02-05 01:30:42 -0800132class SingleDOFProfiledSubsystem
Adam Snaider79900c22017-02-08 20:23:15 -0800133 : public ::frc971::control_loops::ProfiledSubsystem<3, 1, ZeroingEstimator> {
Austin Schuh473a5652017-02-05 01:30:42 -0800134 public:
135 SingleDOFProfiledSubsystem(
136 ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop,
Brian Silvermanab0b6772017-02-05 16:16:21 -0800137 const typename ZeroingEstimator::ZeroingConstants &zeroing_constants,
Austin Schuh473a5652017-02-05 01:30:42 -0800138 const ::frc971::constants::Range &range, double default_angular_velocity,
139 double default_angular_acceleration);
140
141 // Updates our estimator with the latest position.
Brian Silvermanab0b6772017-02-05 16:16:21 -0800142 void Correct(typename ZeroingEstimator::Position position);
Austin Schuh473a5652017-02-05 01:30:42 -0800143 // Runs the controller and profile generator for a cycle.
144 void Update(bool disabled);
145
Austin Schuh3634ed32017-02-05 16:28:49 -0800146 // Fills out the ProfiledJointStatus structure with the current state.
Adam Snaider79900c22017-02-08 20:23:15 -0800147 template <class StatusType>
148 void PopulateStatus(StatusType *status);
Austin Schuh3634ed32017-02-05 16:28:49 -0800149
Austin Schuh473a5652017-02-05 01:30:42 -0800150 // Forces the current goal to the provided goal, bypassing the profiler.
151 void ForceGoal(double goal);
152 // Sets the unprofiled goal. The profiler will generate a profile to go to
153 // this goal.
154 void set_unprofiled_goal(double unprofiled_goal);
155 // Limits our profiles to a max velocity and acceleration for proper motion.
Austin Schuh3634ed32017-02-05 16:28:49 -0800156 void AdjustProfile(const ::frc971::ProfileParameters &profile_parameters);
Austin Schuh473a5652017-02-05 01:30:42 -0800157 void AdjustProfile(double max_angular_velocity,
158 double max_angular_acceleration);
159
160 // Returns true if we have exceeded any hard limits.
161 bool CheckHardLimits();
162
Austin Schuh3634ed32017-02-05 16:28:49 -0800163 // Returns the requested voltage.
Adam Snaider79900c22017-02-08 20:23:15 -0800164 double voltage() const { return this->loop_->U(0, 0); }
Austin Schuh473a5652017-02-05 01:30:42 -0800165
166 // Returns the current position.
Adam Snaider79900c22017-02-08 20:23:15 -0800167 double position() const { return this->Y_(0, 0); }
Austin Schuh473a5652017-02-05 01:30:42 -0800168
169 // For testing:
170 // Triggers an estimator error.
Adam Snaider79900c22017-02-08 20:23:15 -0800171 void TriggerEstimatorError() { this->estimators_[0].TriggerError(); }
Austin Schuh473a5652017-02-05 01:30:42 -0800172
Austin Schuh00be3a82017-02-05 19:01:40 -0800173 const ::frc971::constants::Range &range() const { return range_; }
174
Austin Schuh473a5652017-02-05 01:30:42 -0800175 private:
176 // Limits the provided goal to the soft limits. Prints "name" when it fails
177 // to aid debugging.
178 void CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal);
179
180 void UpdateOffset(double offset);
181
182 aos::util::TrapezoidProfile profile_;
183
184 // Current measurement.
185 Eigen::Matrix<double, 1, 1> Y_;
186 // Current offset. Y_ = offset_ + raw_sensor;
187 Eigen::Matrix<double, 1, 1> offset_;
188
189 const ::frc971::constants::Range range_;
190
191 const double default_velocity_;
192 const double default_acceleration_;
Austin Schuh3634ed32017-02-05 16:28:49 -0800193
194 double last_position_ = 0;
Austin Schuh473a5652017-02-05 01:30:42 -0800195};
196
Brian Silvermanab0b6772017-02-05 16:16:21 -0800197namespace internal {
198
199double UseUnlessZero(double target_value, double default_value);
200
201} // namespace internal
202
203template <class ZeroingEstimator>
204SingleDOFProfiledSubsystem<ZeroingEstimator>::SingleDOFProfiledSubsystem(
205 ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop,
206 const typename ZeroingEstimator::ZeroingConstants &zeroing_constants,
207 const ::frc971::constants::Range &range, double default_velocity,
208 double default_acceleration)
Adam Snaider79900c22017-02-08 20:23:15 -0800209 : ProfiledSubsystem<3, 1, ZeroingEstimator>(::std::move(loop),
210 {{zeroing_constants}}),
Brian Silvermanab0b6772017-02-05 16:16:21 -0800211 profile_(::aos::controls::kLoopFrequency),
212 range_(range),
213 default_velocity_(default_velocity),
214 default_acceleration_(default_acceleration) {
215 Y_.setZero();
216 offset_.setZero();
217 AdjustProfile(0.0, 0.0);
218}
219
220template <class ZeroingEstimator>
221void SingleDOFProfiledSubsystem<ZeroingEstimator>::UpdateOffset(double offset) {
222 const double doffset = offset - offset_(0, 0);
223 LOG(INFO, "Adjusting offset from %f to %f\n", offset_(0, 0), offset);
224
Adam Snaider79900c22017-02-08 20:23:15 -0800225 this->loop_->mutable_X_hat()(0, 0) += doffset;
226 this->Y_(0, 0) += doffset;
Austin Schuh3634ed32017-02-05 16:28:49 -0800227 last_position_ += doffset;
Adam Snaider79900c22017-02-08 20:23:15 -0800228 this->loop_->mutable_R(0, 0) += doffset;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800229
230 profile_.MoveGoal(doffset);
231 offset_(0, 0) = offset;
232
Adam Snaider79900c22017-02-08 20:23:15 -0800233 CapGoal("R", &this->loop_->mutable_R());
Brian Silvermanab0b6772017-02-05 16:16:21 -0800234}
235
236template <class ZeroingEstimator>
Adam Snaider79900c22017-02-08 20:23:15 -0800237template <class StatusType>
Austin Schuh3634ed32017-02-05 16:28:49 -0800238void SingleDOFProfiledSubsystem<ZeroingEstimator>::PopulateStatus(
Adam Snaider79900c22017-02-08 20:23:15 -0800239 StatusType *status) {
240 status->zeroed = this->zeroed();
Austin Schuh00be3a82017-02-05 19:01:40 -0800241 status->state = -1;
Austin Schuh3634ed32017-02-05 16:28:49 -0800242 // We don't know, so default to the bad case.
243 status->estopped = true;
244
Adam Snaider79900c22017-02-08 20:23:15 -0800245 status->position = this->X_hat(0, 0);
246 status->velocity = this->X_hat(1, 0);
247 status->goal_position = this->goal(0, 0);
248 status->goal_velocity = this->goal(1, 0);
249 status->unprofiled_goal_position = this->unprofiled_goal(0, 0);
250 status->unprofiled_goal_velocity = this->unprofiled_goal(1, 0);
251 status->voltage_error = this->X_hat(2, 0);
Austin Schuh3634ed32017-02-05 16:28:49 -0800252 status->calculated_velocity =
253 (position() - last_position_) /
254 ::std::chrono::duration_cast<::std::chrono::duration<double>>(
255 ::aos::controls::kLoopFrequency)
256 .count();
257
Adam Snaider79900c22017-02-08 20:23:15 -0800258 status->estimator_state = this->EstimatorState(0);
Austin Schuh3634ed32017-02-05 16:28:49 -0800259
Adam Snaider79900c22017-02-08 20:23:15 -0800260 Eigen::Matrix<double, 3, 1> error = this->controller().error();
261 status->position_power = this->controller().K(0, 0) * error(0, 0);
262 status->velocity_power = this->controller().K(0, 1) * error(1, 0);
Austin Schuh3634ed32017-02-05 16:28:49 -0800263}
264
265template <class ZeroingEstimator>
Brian Silvermanab0b6772017-02-05 16:16:21 -0800266void SingleDOFProfiledSubsystem<ZeroingEstimator>::Correct(
Austin Schuh3634ed32017-02-05 16:28:49 -0800267 typename ZeroingEstimator::Position new_position) {
Adam Snaider79900c22017-02-08 20:23:15 -0800268 this->estimators_[0].UpdateEstimate(new_position);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800269
Adam Snaider79900c22017-02-08 20:23:15 -0800270 if (this->estimators_[0].error()) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800271 LOG(ERROR, "zeroing error\n");
Brian Silvermanab0b6772017-02-05 16:16:21 -0800272 return;
273 }
274
Adam Snaider79900c22017-02-08 20:23:15 -0800275 if (!this->initialized_) {
276 if (this->estimators_[0].offset_ready()) {
277 UpdateOffset(this->estimators_[0].offset());
278 this->initialized_ = true;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800279 }
280 }
281
Adam Snaider79900c22017-02-08 20:23:15 -0800282 if (!this->zeroed(0) && this->estimators_[0].zeroed()) {
283 UpdateOffset(this->estimators_[0].offset());
284 this->set_zeroed(0, true);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800285 }
286
Austin Schuh3634ed32017-02-05 16:28:49 -0800287 last_position_ = position();
Adam Snaider79900c22017-02-08 20:23:15 -0800288 this->Y_ << new_position.encoder;
289 this->Y_ += this->offset_;
290 this->loop_->Correct(Y_);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800291}
292
293template <class ZeroingEstimator>
294void SingleDOFProfiledSubsystem<ZeroingEstimator>::CapGoal(
295 const char *name, Eigen::Matrix<double, 3, 1> *goal) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800296 // Limit the goal to min/max allowable positions.
Brian Silvermanab0b6772017-02-05 16:16:21 -0800297 if ((*goal)(0, 0) > range_.upper) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800298 LOG(WARNING, "Goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
Brian Silvermanab0b6772017-02-05 16:16:21 -0800299 range_.upper);
300 (*goal)(0, 0) = range_.upper;
301 }
302 if ((*goal)(0, 0) < range_.lower) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800303 LOG(WARNING, "Goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
Brian Silvermanab0b6772017-02-05 16:16:21 -0800304 range_.lower);
305 (*goal)(0, 0) = range_.lower;
306 }
307}
308
309template <class ZeroingEstimator>
310void SingleDOFProfiledSubsystem<ZeroingEstimator>::ForceGoal(double goal) {
311 set_unprofiled_goal(goal);
Adam Snaider79900c22017-02-08 20:23:15 -0800312 this->loop_->mutable_R() = this->unprofiled_goal_;
313 this->loop_->mutable_next_R() = this->loop_->R();
Brian Silvermanab0b6772017-02-05 16:16:21 -0800314
Adam Snaider79900c22017-02-08 20:23:15 -0800315 const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R();
316 this->profile_.MoveCurrentState(R.block<2, 1>(0, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800317}
318
319template <class ZeroingEstimator>
320void SingleDOFProfiledSubsystem<ZeroingEstimator>::set_unprofiled_goal(
321 double unprofiled_goal) {
Adam Snaider79900c22017-02-08 20:23:15 -0800322 this->unprofiled_goal_(0, 0) = unprofiled_goal;
323 this->unprofiled_goal_(1, 0) = 0.0;
324 this->unprofiled_goal_(2, 0) = 0.0;
325 CapGoal("unprofiled R", &this->unprofiled_goal_);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800326}
327
328template <class ZeroingEstimator>
329void SingleDOFProfiledSubsystem<ZeroingEstimator>::Update(bool disable) {
330 if (!disable) {
Adam Snaider79900c22017-02-08 20:23:15 -0800331 ::Eigen::Matrix<double, 2, 1> goal_state = profile_.Update(
332 this->unprofiled_goal_(0, 0), this->unprofiled_goal_(1, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800333
Adam Snaider79900c22017-02-08 20:23:15 -0800334 this->loop_->mutable_next_R(0, 0) = goal_state(0, 0);
335 this->loop_->mutable_next_R(1, 0) = goal_state(1, 0);
336 this->loop_->mutable_next_R(2, 0) = 0.0;
337 CapGoal("next R", &this->loop_->mutable_next_R());
Brian Silvermanab0b6772017-02-05 16:16:21 -0800338 }
339
Adam Snaider79900c22017-02-08 20:23:15 -0800340 this->loop_->Update(disable);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800341
Adam Snaider79900c22017-02-08 20:23:15 -0800342 if (!disable && this->loop_->U(0, 0) != this->loop_->U_uncapped(0, 0)) {
343 const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R();
344 profile_.MoveCurrentState(R.block<2, 1>(0, 0));
Brian Silvermanab0b6772017-02-05 16:16:21 -0800345 }
346}
347
348template <class ZeroingEstimator>
349bool SingleDOFProfiledSubsystem<ZeroingEstimator>::CheckHardLimits() {
350 // Returns whether hard limits have been exceeded.
351
Austin Schuh3634ed32017-02-05 16:28:49 -0800352 if (position() > range_.upper_hard || position() < range_.lower_hard) {
Brian Silvermanab0b6772017-02-05 16:16:21 -0800353 LOG(ERROR,
354 "SingleDOFProfiledSubsystem at %f out of bounds [%f, %f], ESTOPing\n",
Austin Schuh3634ed32017-02-05 16:28:49 -0800355 position(), range_.lower_hard, range_.upper_hard);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800356 return true;
357 }
358
359 return false;
360}
361
362template <class ZeroingEstimator>
363void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Austin Schuh3634ed32017-02-05 16:28:49 -0800364 const ::frc971::ProfileParameters &profile_parameters) {
365 AdjustProfile(profile_parameters.max_velocity,
366 profile_parameters.max_acceleration);
367}
368
369template <class ZeroingEstimator>
370void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Brian Silvermanab0b6772017-02-05 16:16:21 -0800371 double max_angular_velocity, double max_angular_acceleration) {
372 profile_.set_maximum_velocity(
373 internal::UseUnlessZero(max_angular_velocity, default_velocity_));
374 profile_.set_maximum_acceleration(
375 internal::UseUnlessZero(max_angular_acceleration, default_acceleration_));
376}
377
Austin Schuh473a5652017-02-05 01:30:42 -0800378} // namespace control_loops
379} // namespace frc971
380
381#endif // FRC971_CONTROL_LOOPS_PROFILED_SUBSYSTEM_H_