blob: 78a9a76b89d043e9c5618df3f3b69bdfd01ecdc5 [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.
100 ::frc971::EstimatorState EstimatorState(int index) {
101 ::frc971::EstimatorState estimator_state;
102 ::frc971::zeroing::PopulateEstimatorState(estimators_[index],
103 &estimator_state);
104
105 return estimator_state;
106 }
107
108 // Sets the maximum voltage that will be commanded by the loop.
109 void set_max_voltage(::std::array<double, number_of_axes> voltages) {
110 for (int i = 0; i < number_of_axes; ++i) {
111 loop_->set_max_voltage(i, voltages[i]);
112 }
113 }
114
115 protected:
116 void set_zeroed(int index, bool val) { zeroed_[index] = val; }
117
118 // TODO(austin): It's a bold assumption to assume that we will have the same
119 // number of sensors as axes. So far, that's been fine.
120 ::std::unique_ptr<::frc971::control_loops::SimpleCappedStateFeedbackLoop<
Tyler Chatowf8f03112017-02-05 14:31:34 -0800121 number_of_states, number_of_axes, number_of_axes>> loop_;
Austin Schuh473a5652017-02-05 01:30:42 -0800122
123 // The goal that the profile tries to reach.
124 Eigen::Matrix<double, number_of_states, 1> unprofiled_goal_;
125
126 bool initialized_ = false;
127
Brian Silvermanab0b6772017-02-05 16:16:21 -0800128 ::std::array<ZeroingEstimator, number_of_axes> estimators_;
Austin Schuh473a5652017-02-05 01:30:42 -0800129
130 private:
131 ::std::array<bool, number_of_axes> zeroed_;
132};
133
Brian Silvermanab0b6772017-02-05 16:16:21 -0800134template <class ZeroingEstimator =
135 ::frc971::zeroing::PotAndIndexPulseZeroingEstimator>
Austin Schuh473a5652017-02-05 01:30:42 -0800136class SingleDOFProfiledSubsystem
137 : public ::frc971::control_loops::ProfiledSubsystem<3, 1> {
138 public:
139 SingleDOFProfiledSubsystem(
140 ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop,
Brian Silvermanab0b6772017-02-05 16:16:21 -0800141 const typename ZeroingEstimator::ZeroingConstants &zeroing_constants,
Austin Schuh473a5652017-02-05 01:30:42 -0800142 const ::frc971::constants::Range &range, double default_angular_velocity,
143 double default_angular_acceleration);
144
145 // Updates our estimator with the latest position.
Brian Silvermanab0b6772017-02-05 16:16:21 -0800146 void Correct(typename ZeroingEstimator::Position position);
Austin Schuh473a5652017-02-05 01:30:42 -0800147 // Runs the controller and profile generator for a cycle.
148 void Update(bool disabled);
149
Austin Schuh3634ed32017-02-05 16:28:49 -0800150 // Fills out the ProfiledJointStatus structure with the current state.
151 void PopulateStatus(ProfiledJointStatus *status);
152
Austin Schuh473a5652017-02-05 01:30:42 -0800153 // Forces the current goal to the provided goal, bypassing the profiler.
154 void ForceGoal(double goal);
155 // Sets the unprofiled goal. The profiler will generate a profile to go to
156 // this goal.
157 void set_unprofiled_goal(double unprofiled_goal);
158 // Limits our profiles to a max velocity and acceleration for proper motion.
Austin Schuh3634ed32017-02-05 16:28:49 -0800159 void AdjustProfile(const ::frc971::ProfileParameters &profile_parameters);
Austin Schuh473a5652017-02-05 01:30:42 -0800160 void AdjustProfile(double max_angular_velocity,
161 double max_angular_acceleration);
162
163 // Returns true if we have exceeded any hard limits.
164 bool CheckHardLimits();
165
Austin Schuh3634ed32017-02-05 16:28:49 -0800166 // Returns the requested voltage.
167 double voltage() const { return loop_->U(0, 0); }
Austin Schuh473a5652017-02-05 01:30:42 -0800168
169 // Returns the current position.
Austin Schuh3634ed32017-02-05 16:28:49 -0800170 double position() const { return Y_(0, 0); }
Austin Schuh473a5652017-02-05 01:30:42 -0800171
172 // For testing:
173 // Triggers an estimator error.
174 void TriggerEstimatorError() { estimators_[0].TriggerError(); }
175
Austin Schuh00be3a82017-02-05 19:01:40 -0800176 const ::frc971::constants::Range &range() const { return range_; }
177
Austin Schuh473a5652017-02-05 01:30:42 -0800178 private:
179 // Limits the provided goal to the soft limits. Prints "name" when it fails
180 // to aid debugging.
181 void CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal);
182
183 void UpdateOffset(double offset);
184
185 aos::util::TrapezoidProfile profile_;
186
187 // Current measurement.
188 Eigen::Matrix<double, 1, 1> Y_;
189 // Current offset. Y_ = offset_ + raw_sensor;
190 Eigen::Matrix<double, 1, 1> offset_;
191
192 const ::frc971::constants::Range range_;
193
194 const double default_velocity_;
195 const double default_acceleration_;
Austin Schuh3634ed32017-02-05 16:28:49 -0800196
197 double last_position_ = 0;
Austin Schuh473a5652017-02-05 01:30:42 -0800198};
199
Brian Silvermanab0b6772017-02-05 16:16:21 -0800200namespace internal {
201
202double UseUnlessZero(double target_value, double default_value);
203
204} // namespace internal
205
206template <class ZeroingEstimator>
207SingleDOFProfiledSubsystem<ZeroingEstimator>::SingleDOFProfiledSubsystem(
208 ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop,
209 const typename ZeroingEstimator::ZeroingConstants &zeroing_constants,
210 const ::frc971::constants::Range &range, double default_velocity,
211 double default_acceleration)
212 : ProfiledSubsystem(::std::move(loop), {{zeroing_constants}}),
213 profile_(::aos::controls::kLoopFrequency),
214 range_(range),
215 default_velocity_(default_velocity),
216 default_acceleration_(default_acceleration) {
217 Y_.setZero();
218 offset_.setZero();
219 AdjustProfile(0.0, 0.0);
220}
221
222template <class ZeroingEstimator>
223void SingleDOFProfiledSubsystem<ZeroingEstimator>::UpdateOffset(double offset) {
224 const double doffset = offset - offset_(0, 0);
225 LOG(INFO, "Adjusting offset from %f to %f\n", offset_(0, 0), offset);
226
227 loop_->mutable_X_hat()(0, 0) += doffset;
228 Y_(0, 0) += doffset;
Austin Schuh3634ed32017-02-05 16:28:49 -0800229 last_position_ += doffset;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800230 loop_->mutable_R(0, 0) += doffset;
231
232 profile_.MoveGoal(doffset);
233 offset_(0, 0) = offset;
234
235 CapGoal("R", &loop_->mutable_R());
236}
237
238template <class ZeroingEstimator>
Austin Schuh3634ed32017-02-05 16:28:49 -0800239void SingleDOFProfiledSubsystem<ZeroingEstimator>::PopulateStatus(
240 ProfiledJointStatus *status) {
241 status->zeroed = zeroed();
Austin Schuh00be3a82017-02-05 19:01:40 -0800242 status->state = -1;
Austin Schuh3634ed32017-02-05 16:28:49 -0800243 // We don't know, so default to the bad case.
244 status->estopped = true;
245
246 status->position = X_hat(0, 0);
247 status->velocity = X_hat(1, 0);
248 status->goal_position = goal(0, 0);
249 status->goal_velocity = goal(1, 0);
250 status->unprofiled_goal_position = unprofiled_goal(0, 0);
251 status->unprofiled_goal_velocity = unprofiled_goal(1, 0);
252 status->voltage_error = X_hat(2, 0);
253 status->calculated_velocity =
254 (position() - last_position_) /
255 ::std::chrono::duration_cast<::std::chrono::duration<double>>(
256 ::aos::controls::kLoopFrequency)
257 .count();
258
259 status->estimator_state = EstimatorState(0);
260
261 Eigen::Matrix<double, 3, 1> error = controller().error();
262 status->position_power = controller().K(0, 0) * error(0, 0);
263 status->velocity_power = controller().K(0, 1) * error(1, 0);
264}
265
266template <class ZeroingEstimator>
Brian Silvermanab0b6772017-02-05 16:16:21 -0800267void SingleDOFProfiledSubsystem<ZeroingEstimator>::Correct(
Austin Schuh3634ed32017-02-05 16:28:49 -0800268 typename ZeroingEstimator::Position new_position) {
269 estimators_[0].UpdateEstimate(new_position);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800270
271 if (estimators_[0].error()) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800272 LOG(ERROR, "zeroing error\n");
Brian Silvermanab0b6772017-02-05 16:16:21 -0800273 return;
274 }
275
276 if (!initialized_) {
277 if (estimators_[0].offset_ready()) {
278 UpdateOffset(estimators_[0].offset());
279 initialized_ = true;
280 }
281 }
282
283 if (!zeroed(0) && estimators_[0].zeroed()) {
284 UpdateOffset(estimators_[0].offset());
285 set_zeroed(0, true);
286 }
287
Austin Schuh3634ed32017-02-05 16:28:49 -0800288 last_position_ = position();
289 Y_ << new_position.encoder;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800290 Y_ += offset_;
291 loop_->Correct(Y_);
292}
293
294template <class ZeroingEstimator>
295void SingleDOFProfiledSubsystem<ZeroingEstimator>::CapGoal(
296 const char *name, Eigen::Matrix<double, 3, 1> *goal) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800297 // Limit the goal to min/max allowable positions.
Brian Silvermanab0b6772017-02-05 16:16:21 -0800298 if ((*goal)(0, 0) > range_.upper) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800299 LOG(WARNING, "Goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
Brian Silvermanab0b6772017-02-05 16:16:21 -0800300 range_.upper);
301 (*goal)(0, 0) = range_.upper;
302 }
303 if ((*goal)(0, 0) < range_.lower) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800304 LOG(WARNING, "Goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
Brian Silvermanab0b6772017-02-05 16:16:21 -0800305 range_.lower);
306 (*goal)(0, 0) = range_.lower;
307 }
308}
309
310template <class ZeroingEstimator>
311void SingleDOFProfiledSubsystem<ZeroingEstimator>::ForceGoal(double goal) {
312 set_unprofiled_goal(goal);
313 loop_->mutable_R() = unprofiled_goal_;
314 loop_->mutable_next_R() = loop_->R();
315
316 profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
317}
318
319template <class ZeroingEstimator>
320void SingleDOFProfiledSubsystem<ZeroingEstimator>::set_unprofiled_goal(
321 double unprofiled_goal) {
322 unprofiled_goal_(0, 0) = unprofiled_goal;
323 unprofiled_goal_(1, 0) = 0.0;
324 unprofiled_goal_(2, 0) = 0.0;
325 CapGoal("unprofiled R", &unprofiled_goal_);
326}
327
328template <class ZeroingEstimator>
329void SingleDOFProfiledSubsystem<ZeroingEstimator>::Update(bool disable) {
330 if (!disable) {
331 ::Eigen::Matrix<double, 2, 1> goal_state =
332 profile_.Update(unprofiled_goal_(0, 0), unprofiled_goal_(1, 0));
333
334 loop_->mutable_next_R(0, 0) = goal_state(0, 0);
335 loop_->mutable_next_R(1, 0) = goal_state(1, 0);
336 loop_->mutable_next_R(2, 0) = 0.0;
337 CapGoal("next R", &loop_->mutable_next_R());
338 }
339
340 loop_->Update(disable);
341
342 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
343 profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
344 }
345}
346
347template <class ZeroingEstimator>
348bool SingleDOFProfiledSubsystem<ZeroingEstimator>::CheckHardLimits() {
349 // Returns whether hard limits have been exceeded.
350
Austin Schuh3634ed32017-02-05 16:28:49 -0800351 if (position() > range_.upper_hard || position() < range_.lower_hard) {
Brian Silvermanab0b6772017-02-05 16:16:21 -0800352 LOG(ERROR,
353 "SingleDOFProfiledSubsystem at %f out of bounds [%f, %f], ESTOPing\n",
Austin Schuh3634ed32017-02-05 16:28:49 -0800354 position(), range_.lower_hard, range_.upper_hard);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800355 return true;
356 }
357
358 return false;
359}
360
361template <class ZeroingEstimator>
362void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Austin Schuh3634ed32017-02-05 16:28:49 -0800363 const ::frc971::ProfileParameters &profile_parameters) {
364 AdjustProfile(profile_parameters.max_velocity,
365 profile_parameters.max_acceleration);
366}
367
368template <class ZeroingEstimator>
369void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Brian Silvermanab0b6772017-02-05 16:16:21 -0800370 double max_angular_velocity, double max_angular_acceleration) {
371 profile_.set_maximum_velocity(
372 internal::UseUnlessZero(max_angular_velocity, default_velocity_));
373 profile_.set_maximum_acceleration(
374 internal::UseUnlessZero(max_angular_acceleration, default_acceleration_));
375}
376
Austin Schuh473a5652017-02-05 01:30:42 -0800377} // namespace control_loops
378} // namespace frc971
379
380#endif // FRC971_CONTROL_LOOPS_PROFILED_SUBSYSTEM_H_