blob: 7dbd1a8709b868d8aab45ec9643a5c316a2eb28b [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);
51 for (auto &estimator : estimators_) {
52 estimator.Reset();
53 }
54 }
55
56 // Returns the controller.
Tyler Chatowf8f03112017-02-05 14:31:34 -080057 const StateFeedbackLoop<number_of_states, number_of_axes, number_of_axes> &
58 controller() const {
Austin Schuh473a5652017-02-05 01:30:42 -080059 return *loop_;
60 }
61
62 int controller_index() const { return loop_->controller_index(); }
63
64 // Returns whether the estimators have been initialized and zeroed.
65 bool initialized() const { return initialized_; }
66
67 bool zeroed() const {
68 for (int i = 0; i < number_of_axes; ++i) {
69 if (!zeroed_[i]) {
70 return false;
71 }
72 }
73 return true;
74 }
75
76 bool zeroed(int index) const { return zeroed_[index]; };
77
78 // Returns the filtered goal.
79 const Eigen::Matrix<double, number_of_states, 1> &goal() const {
80 return loop_->R();
81 }
82 double goal(int row, int col) const { return loop_->R(row, col); }
83
84 // Returns the unprofiled goal.
85 const Eigen::Matrix<double, number_of_states, 1> &unprofiled_goal() const {
86 return unprofiled_goal_;
87 }
88 double unprofiled_goal(int row, int col) const {
89 return unprofiled_goal_(row, col);
90 }
91
92 // Returns the current state estimate.
93 const Eigen::Matrix<double, number_of_states, 1> &X_hat() const {
94 return loop_->X_hat();
95 }
96 double X_hat(int row, int col) const { return loop_->X_hat(row, col); }
97
98 // Returns the current internal estimator state for logging.
99 ::frc971::EstimatorState EstimatorState(int index) {
100 ::frc971::EstimatorState estimator_state;
101 ::frc971::zeroing::PopulateEstimatorState(estimators_[index],
102 &estimator_state);
103
104 return estimator_state;
105 }
106
107 // Sets the maximum voltage that will be commanded by the loop.
108 void set_max_voltage(::std::array<double, number_of_axes> voltages) {
109 for (int i = 0; i < number_of_axes; ++i) {
110 loop_->set_max_voltage(i, voltages[i]);
111 }
112 }
113
114 protected:
115 void set_zeroed(int index, bool val) { zeroed_[index] = val; }
116
117 // TODO(austin): It's a bold assumption to assume that we will have the same
118 // number of sensors as axes. So far, that's been fine.
119 ::std::unique_ptr<::frc971::control_loops::SimpleCappedStateFeedbackLoop<
Tyler Chatowf8f03112017-02-05 14:31:34 -0800120 number_of_states, number_of_axes, number_of_axes>> loop_;
Austin Schuh473a5652017-02-05 01:30:42 -0800121
122 // The goal that the profile tries to reach.
123 Eigen::Matrix<double, number_of_states, 1> unprofiled_goal_;
124
125 bool initialized_ = false;
126
Brian Silvermanab0b6772017-02-05 16:16:21 -0800127 ::std::array<ZeroingEstimator, number_of_axes> estimators_;
Austin Schuh473a5652017-02-05 01:30:42 -0800128
129 private:
130 ::std::array<bool, number_of_axes> zeroed_;
131};
132
Brian Silvermanab0b6772017-02-05 16:16:21 -0800133template <class ZeroingEstimator =
134 ::frc971::zeroing::PotAndIndexPulseZeroingEstimator>
Austin Schuh473a5652017-02-05 01:30:42 -0800135class SingleDOFProfiledSubsystem
136 : public ::frc971::control_loops::ProfiledSubsystem<3, 1> {
137 public:
138 SingleDOFProfiledSubsystem(
139 ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop,
Brian Silvermanab0b6772017-02-05 16:16:21 -0800140 const typename ZeroingEstimator::ZeroingConstants &zeroing_constants,
Austin Schuh473a5652017-02-05 01:30:42 -0800141 const ::frc971::constants::Range &range, double default_angular_velocity,
142 double default_angular_acceleration);
143
144 // Updates our estimator with the latest position.
Brian Silvermanab0b6772017-02-05 16:16:21 -0800145 void Correct(typename ZeroingEstimator::Position position);
Austin Schuh473a5652017-02-05 01:30:42 -0800146 // Runs the controller and profile generator for a cycle.
147 void Update(bool disabled);
148
Austin Schuh3634ed32017-02-05 16:28:49 -0800149 // Fills out the ProfiledJointStatus structure with the current state.
150 void PopulateStatus(ProfiledJointStatus *status);
151
Austin Schuh473a5652017-02-05 01:30:42 -0800152 // Forces the current goal to the provided goal, bypassing the profiler.
153 void ForceGoal(double goal);
154 // Sets the unprofiled goal. The profiler will generate a profile to go to
155 // this goal.
156 void set_unprofiled_goal(double unprofiled_goal);
157 // Limits our profiles to a max velocity and acceleration for proper motion.
Austin Schuh3634ed32017-02-05 16:28:49 -0800158 void AdjustProfile(const ::frc971::ProfileParameters &profile_parameters);
Austin Schuh473a5652017-02-05 01:30:42 -0800159 void AdjustProfile(double max_angular_velocity,
160 double max_angular_acceleration);
161
162 // Returns true if we have exceeded any hard limits.
163 bool CheckHardLimits();
164
Austin Schuh3634ed32017-02-05 16:28:49 -0800165 // Returns the requested voltage.
166 double voltage() const { return loop_->U(0, 0); }
Austin Schuh473a5652017-02-05 01:30:42 -0800167
168 // Returns the current position.
Austin Schuh3634ed32017-02-05 16:28:49 -0800169 double position() const { return Y_(0, 0); }
Austin Schuh473a5652017-02-05 01:30:42 -0800170
171 // For testing:
172 // Triggers an estimator error.
173 void TriggerEstimatorError() { estimators_[0].TriggerError(); }
174
Austin Schuh00be3a82017-02-05 19:01:40 -0800175 const ::frc971::constants::Range &range() const { return range_; }
176
Austin Schuh473a5652017-02-05 01:30:42 -0800177 private:
178 // Limits the provided goal to the soft limits. Prints "name" when it fails
179 // to aid debugging.
180 void CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal);
181
182 void UpdateOffset(double offset);
183
184 aos::util::TrapezoidProfile profile_;
185
186 // Current measurement.
187 Eigen::Matrix<double, 1, 1> Y_;
188 // Current offset. Y_ = offset_ + raw_sensor;
189 Eigen::Matrix<double, 1, 1> offset_;
190
191 const ::frc971::constants::Range range_;
192
193 const double default_velocity_;
194 const double default_acceleration_;
Austin Schuh3634ed32017-02-05 16:28:49 -0800195
196 double last_position_ = 0;
Austin Schuh473a5652017-02-05 01:30:42 -0800197};
198
Brian Silvermanab0b6772017-02-05 16:16:21 -0800199namespace internal {
200
201double UseUnlessZero(double target_value, double default_value);
202
203} // namespace internal
204
205template <class ZeroingEstimator>
206SingleDOFProfiledSubsystem<ZeroingEstimator>::SingleDOFProfiledSubsystem(
207 ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop,
208 const typename ZeroingEstimator::ZeroingConstants &zeroing_constants,
209 const ::frc971::constants::Range &range, double default_velocity,
210 double default_acceleration)
211 : ProfiledSubsystem(::std::move(loop), {{zeroing_constants}}),
212 profile_(::aos::controls::kLoopFrequency),
213 range_(range),
214 default_velocity_(default_velocity),
215 default_acceleration_(default_acceleration) {
216 Y_.setZero();
217 offset_.setZero();
218 AdjustProfile(0.0, 0.0);
219}
220
221template <class ZeroingEstimator>
222void SingleDOFProfiledSubsystem<ZeroingEstimator>::UpdateOffset(double offset) {
223 const double doffset = offset - offset_(0, 0);
224 LOG(INFO, "Adjusting offset from %f to %f\n", offset_(0, 0), offset);
225
226 loop_->mutable_X_hat()(0, 0) += doffset;
227 Y_(0, 0) += doffset;
Austin Schuh3634ed32017-02-05 16:28:49 -0800228 last_position_ += doffset;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800229 loop_->mutable_R(0, 0) += doffset;
230
231 profile_.MoveGoal(doffset);
232 offset_(0, 0) = offset;
233
234 CapGoal("R", &loop_->mutable_R());
235}
236
237template <class ZeroingEstimator>
Austin Schuh3634ed32017-02-05 16:28:49 -0800238void SingleDOFProfiledSubsystem<ZeroingEstimator>::PopulateStatus(
239 ProfiledJointStatus *status) {
240 status->zeroed = 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
245 status->position = X_hat(0, 0);
246 status->velocity = X_hat(1, 0);
247 status->goal_position = goal(0, 0);
248 status->goal_velocity = goal(1, 0);
249 status->unprofiled_goal_position = unprofiled_goal(0, 0);
250 status->unprofiled_goal_velocity = unprofiled_goal(1, 0);
251 status->voltage_error = X_hat(2, 0);
252 status->calculated_velocity =
253 (position() - last_position_) /
254 ::std::chrono::duration_cast<::std::chrono::duration<double>>(
255 ::aos::controls::kLoopFrequency)
256 .count();
257
258 status->estimator_state = EstimatorState(0);
259
260 Eigen::Matrix<double, 3, 1> error = controller().error();
261 status->position_power = controller().K(0, 0) * error(0, 0);
262 status->velocity_power = controller().K(0, 1) * error(1, 0);
263}
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) {
268 estimators_[0].UpdateEstimate(new_position);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800269
270 if (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
275 if (!initialized_) {
276 if (estimators_[0].offset_ready()) {
277 UpdateOffset(estimators_[0].offset());
278 initialized_ = true;
279 }
280 }
281
282 if (!zeroed(0) && estimators_[0].zeroed()) {
283 UpdateOffset(estimators_[0].offset());
284 set_zeroed(0, true);
285 }
286
Austin Schuh3634ed32017-02-05 16:28:49 -0800287 last_position_ = position();
288 Y_ << new_position.encoder;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800289 Y_ += offset_;
290 loop_->Correct(Y_);
291}
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);
312 loop_->mutable_R() = unprofiled_goal_;
313 loop_->mutable_next_R() = loop_->R();
314
315 profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
316}
317
318template <class ZeroingEstimator>
319void SingleDOFProfiledSubsystem<ZeroingEstimator>::set_unprofiled_goal(
320 double unprofiled_goal) {
321 unprofiled_goal_(0, 0) = unprofiled_goal;
322 unprofiled_goal_(1, 0) = 0.0;
323 unprofiled_goal_(2, 0) = 0.0;
324 CapGoal("unprofiled R", &unprofiled_goal_);
325}
326
327template <class ZeroingEstimator>
328void SingleDOFProfiledSubsystem<ZeroingEstimator>::Update(bool disable) {
329 if (!disable) {
330 ::Eigen::Matrix<double, 2, 1> goal_state =
331 profile_.Update(unprofiled_goal_(0, 0), unprofiled_goal_(1, 0));
332
333 loop_->mutable_next_R(0, 0) = goal_state(0, 0);
334 loop_->mutable_next_R(1, 0) = goal_state(1, 0);
335 loop_->mutable_next_R(2, 0) = 0.0;
336 CapGoal("next R", &loop_->mutable_next_R());
337 }
338
339 loop_->Update(disable);
340
341 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
342 profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
343 }
344}
345
346template <class ZeroingEstimator>
347bool SingleDOFProfiledSubsystem<ZeroingEstimator>::CheckHardLimits() {
348 // Returns whether hard limits have been exceeded.
349
Austin Schuh3634ed32017-02-05 16:28:49 -0800350 if (position() > range_.upper_hard || position() < range_.lower_hard) {
Brian Silvermanab0b6772017-02-05 16:16:21 -0800351 LOG(ERROR,
352 "SingleDOFProfiledSubsystem at %f out of bounds [%f, %f], ESTOPing\n",
Austin Schuh3634ed32017-02-05 16:28:49 -0800353 position(), range_.lower_hard, range_.upper_hard);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800354 return true;
355 }
356
357 return false;
358}
359
360template <class ZeroingEstimator>
361void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Austin Schuh3634ed32017-02-05 16:28:49 -0800362 const ::frc971::ProfileParameters &profile_parameters) {
363 AdjustProfile(profile_parameters.max_velocity,
364 profile_parameters.max_acceleration);
365}
366
367template <class ZeroingEstimator>
368void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Brian Silvermanab0b6772017-02-05 16:16:21 -0800369 double max_angular_velocity, double max_angular_acceleration) {
370 profile_.set_maximum_velocity(
371 internal::UseUnlessZero(max_angular_velocity, default_velocity_));
372 profile_.set_maximum_acceleration(
373 internal::UseUnlessZero(max_angular_acceleration, default_acceleration_));
374}
375
Austin Schuh473a5652017-02-05 01:30:42 -0800376} // namespace control_loops
377} // namespace frc971
378
379#endif // FRC971_CONTROL_LOOPS_PROFILED_SUBSYSTEM_H_