blob: 379cdc915eb5aa6ffe57deba7574e2775a381c48 [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"
18
19namespace frc971 {
20namespace control_loops {
21
Brian Silvermanab0b6772017-02-05 16:16:21 -080022// TODO(Brian): Use a tuple instead of an array to support heterogeneous zeroing
23// styles.
24template <int number_of_states, int number_of_axes,
25 class ZeroingEstimator =
26 ::frc971::zeroing::PotAndIndexPulseZeroingEstimator>
Austin Schuh473a5652017-02-05 01:30:42 -080027class ProfiledSubsystem {
28 public:
29 ProfiledSubsystem(
30 ::std::unique_ptr<::frc971::control_loops::SimpleCappedStateFeedbackLoop<
Tyler Chatowf8f03112017-02-05 14:31:34 -080031 number_of_states, number_of_axes, number_of_axes>> loop,
Brian Silvermanab0b6772017-02-05 16:16:21 -080032 ::std::array<ZeroingEstimator, number_of_axes> &&estimators)
Austin Schuh473a5652017-02-05 01:30:42 -080033 : loop_(::std::move(loop)), estimators_(::std::move(estimators)) {
34 zeroed_.fill(false);
35 unprofiled_goal_.setZero();
36 }
37
38 // Returns whether an error has occured
39 bool error() const {
40 for (const auto &estimator : estimators_) {
41 if (estimator.error()) {
42 return true;
43 }
44 }
45 return false;
46 }
47
48 void Reset() {
49 zeroed_.fill(false);
50 for (auto &estimator : estimators_) {
51 estimator.Reset();
52 }
53 }
54
55 // Returns the controller.
Tyler Chatowf8f03112017-02-05 14:31:34 -080056 const StateFeedbackLoop<number_of_states, number_of_axes, number_of_axes> &
57 controller() const {
Austin Schuh473a5652017-02-05 01:30:42 -080058 return *loop_;
59 }
60
61 int controller_index() const { return loop_->controller_index(); }
62
63 // Returns whether the estimators have been initialized and zeroed.
64 bool initialized() const { return initialized_; }
65
66 bool zeroed() const {
67 for (int i = 0; i < number_of_axes; ++i) {
68 if (!zeroed_[i]) {
69 return false;
70 }
71 }
72 return true;
73 }
74
75 bool zeroed(int index) const { return zeroed_[index]; };
76
77 // Returns the filtered goal.
78 const Eigen::Matrix<double, number_of_states, 1> &goal() const {
79 return loop_->R();
80 }
81 double goal(int row, int col) const { return loop_->R(row, col); }
82
83 // Returns the unprofiled goal.
84 const Eigen::Matrix<double, number_of_states, 1> &unprofiled_goal() const {
85 return unprofiled_goal_;
86 }
87 double unprofiled_goal(int row, int col) const {
88 return unprofiled_goal_(row, col);
89 }
90
91 // Returns the current state estimate.
92 const Eigen::Matrix<double, number_of_states, 1> &X_hat() const {
93 return loop_->X_hat();
94 }
95 double X_hat(int row, int col) const { return loop_->X_hat(row, col); }
96
97 // Returns the current internal estimator state for logging.
98 ::frc971::EstimatorState EstimatorState(int index) {
99 ::frc971::EstimatorState estimator_state;
100 ::frc971::zeroing::PopulateEstimatorState(estimators_[index],
101 &estimator_state);
102
103 return estimator_state;
104 }
105
106 // Sets the maximum voltage that will be commanded by the loop.
107 void set_max_voltage(::std::array<double, number_of_axes> voltages) {
108 for (int i = 0; i < number_of_axes; ++i) {
109 loop_->set_max_voltage(i, voltages[i]);
110 }
111 }
112
113 protected:
114 void set_zeroed(int index, bool val) { zeroed_[index] = val; }
115
116 // TODO(austin): It's a bold assumption to assume that we will have the same
117 // number of sensors as axes. So far, that's been fine.
118 ::std::unique_ptr<::frc971::control_loops::SimpleCappedStateFeedbackLoop<
Tyler Chatowf8f03112017-02-05 14:31:34 -0800119 number_of_states, number_of_axes, number_of_axes>> loop_;
Austin Schuh473a5652017-02-05 01:30:42 -0800120
121 // The goal that the profile tries to reach.
122 Eigen::Matrix<double, number_of_states, 1> unprofiled_goal_;
123
124 bool initialized_ = false;
125
Brian Silvermanab0b6772017-02-05 16:16:21 -0800126 ::std::array<ZeroingEstimator, number_of_axes> estimators_;
Austin Schuh473a5652017-02-05 01:30:42 -0800127
128 private:
129 ::std::array<bool, number_of_axes> zeroed_;
130};
131
Brian Silvermanab0b6772017-02-05 16:16:21 -0800132template <class ZeroingEstimator =
133 ::frc971::zeroing::PotAndIndexPulseZeroingEstimator>
Austin Schuh473a5652017-02-05 01:30:42 -0800134class SingleDOFProfiledSubsystem
135 : public ::frc971::control_loops::ProfiledSubsystem<3, 1> {
136 public:
137 SingleDOFProfiledSubsystem(
138 ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop,
Brian Silvermanab0b6772017-02-05 16:16:21 -0800139 const typename ZeroingEstimator::ZeroingConstants &zeroing_constants,
Austin Schuh473a5652017-02-05 01:30:42 -0800140 const ::frc971::constants::Range &range, double default_angular_velocity,
141 double default_angular_acceleration);
142
143 // Updates our estimator with the latest position.
Brian Silvermanab0b6772017-02-05 16:16:21 -0800144 void Correct(typename ZeroingEstimator::Position position);
Austin Schuh473a5652017-02-05 01:30:42 -0800145 // Runs the controller and profile generator for a cycle.
146 void Update(bool disabled);
147
Austin Schuh3634ed32017-02-05 16:28:49 -0800148 // Fills out the ProfiledJointStatus structure with the current state.
149 void PopulateStatus(ProfiledJointStatus *status);
150
Austin Schuh473a5652017-02-05 01:30:42 -0800151 // Forces the current goal to the provided goal, bypassing the profiler.
152 void ForceGoal(double goal);
153 // Sets the unprofiled goal. The profiler will generate a profile to go to
154 // this goal.
155 void set_unprofiled_goal(double unprofiled_goal);
156 // Limits our profiles to a max velocity and acceleration for proper motion.
Austin Schuh3634ed32017-02-05 16:28:49 -0800157 void AdjustProfile(const ::frc971::ProfileParameters &profile_parameters);
Austin Schuh473a5652017-02-05 01:30:42 -0800158 void AdjustProfile(double max_angular_velocity,
159 double max_angular_acceleration);
160
161 // Returns true if we have exceeded any hard limits.
162 bool CheckHardLimits();
163
Austin Schuh3634ed32017-02-05 16:28:49 -0800164 // Returns the requested voltage.
165 double voltage() const { return loop_->U(0, 0); }
Austin Schuh473a5652017-02-05 01:30:42 -0800166
167 // Returns the current position.
Austin Schuh3634ed32017-02-05 16:28:49 -0800168 double position() const { return Y_(0, 0); }
Austin Schuh473a5652017-02-05 01:30:42 -0800169
170 // For testing:
171 // Triggers an estimator error.
172 void TriggerEstimatorError() { estimators_[0].TriggerError(); }
173
174 private:
175 // Limits the provided goal to the soft limits. Prints "name" when it fails
176 // to aid debugging.
177 void CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal);
178
179 void UpdateOffset(double offset);
180
181 aos::util::TrapezoidProfile profile_;
182
183 // Current measurement.
184 Eigen::Matrix<double, 1, 1> Y_;
185 // Current offset. Y_ = offset_ + raw_sensor;
186 Eigen::Matrix<double, 1, 1> offset_;
187
188 const ::frc971::constants::Range range_;
189
190 const double default_velocity_;
191 const double default_acceleration_;
Austin Schuh3634ed32017-02-05 16:28:49 -0800192
193 double last_position_ = 0;
Austin Schuh473a5652017-02-05 01:30:42 -0800194};
195
Brian Silvermanab0b6772017-02-05 16:16:21 -0800196namespace internal {
197
198double UseUnlessZero(double target_value, double default_value);
199
200} // namespace internal
201
202template <class ZeroingEstimator>
203SingleDOFProfiledSubsystem<ZeroingEstimator>::SingleDOFProfiledSubsystem(
204 ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop,
205 const typename ZeroingEstimator::ZeroingConstants &zeroing_constants,
206 const ::frc971::constants::Range &range, double default_velocity,
207 double default_acceleration)
208 : ProfiledSubsystem(::std::move(loop), {{zeroing_constants}}),
209 profile_(::aos::controls::kLoopFrequency),
210 range_(range),
211 default_velocity_(default_velocity),
212 default_acceleration_(default_acceleration) {
213 Y_.setZero();
214 offset_.setZero();
215 AdjustProfile(0.0, 0.0);
216}
217
218template <class ZeroingEstimator>
219void SingleDOFProfiledSubsystem<ZeroingEstimator>::UpdateOffset(double offset) {
220 const double doffset = offset - offset_(0, 0);
221 LOG(INFO, "Adjusting offset from %f to %f\n", offset_(0, 0), offset);
222
223 loop_->mutable_X_hat()(0, 0) += doffset;
224 Y_(0, 0) += doffset;
Austin Schuh3634ed32017-02-05 16:28:49 -0800225 last_position_ += doffset;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800226 loop_->mutable_R(0, 0) += doffset;
227
228 profile_.MoveGoal(doffset);
229 offset_(0, 0) = offset;
230
231 CapGoal("R", &loop_->mutable_R());
232}
233
234template <class ZeroingEstimator>
Austin Schuh3634ed32017-02-05 16:28:49 -0800235void SingleDOFProfiledSubsystem<ZeroingEstimator>::PopulateStatus(
236 ProfiledJointStatus *status) {
237 status->zeroed = zeroed();
238 // We don't know, so default to the bad case.
239 status->estopped = true;
240
241 status->position = X_hat(0, 0);
242 status->velocity = X_hat(1, 0);
243 status->goal_position = goal(0, 0);
244 status->goal_velocity = goal(1, 0);
245 status->unprofiled_goal_position = unprofiled_goal(0, 0);
246 status->unprofiled_goal_velocity = unprofiled_goal(1, 0);
247 status->voltage_error = X_hat(2, 0);
248 status->calculated_velocity =
249 (position() - last_position_) /
250 ::std::chrono::duration_cast<::std::chrono::duration<double>>(
251 ::aos::controls::kLoopFrequency)
252 .count();
253
254 status->estimator_state = EstimatorState(0);
255
256 Eigen::Matrix<double, 3, 1> error = controller().error();
257 status->position_power = controller().K(0, 0) * error(0, 0);
258 status->velocity_power = controller().K(0, 1) * error(1, 0);
259}
260
261template <class ZeroingEstimator>
Brian Silvermanab0b6772017-02-05 16:16:21 -0800262void SingleDOFProfiledSubsystem<ZeroingEstimator>::Correct(
Austin Schuh3634ed32017-02-05 16:28:49 -0800263 typename ZeroingEstimator::Position new_position) {
264 estimators_[0].UpdateEstimate(new_position);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800265
266 if (estimators_[0].error()) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800267 LOG(ERROR, "zeroing error\n");
Brian Silvermanab0b6772017-02-05 16:16:21 -0800268 return;
269 }
270
271 if (!initialized_) {
272 if (estimators_[0].offset_ready()) {
273 UpdateOffset(estimators_[0].offset());
274 initialized_ = true;
275 }
276 }
277
278 if (!zeroed(0) && estimators_[0].zeroed()) {
279 UpdateOffset(estimators_[0].offset());
280 set_zeroed(0, true);
281 }
282
Austin Schuh3634ed32017-02-05 16:28:49 -0800283 last_position_ = position();
284 Y_ << new_position.encoder;
Brian Silvermanab0b6772017-02-05 16:16:21 -0800285 Y_ += offset_;
286 loop_->Correct(Y_);
287}
288
289template <class ZeroingEstimator>
290void SingleDOFProfiledSubsystem<ZeroingEstimator>::CapGoal(
291 const char *name, Eigen::Matrix<double, 3, 1> *goal) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800292 // Limit the goal to min/max allowable positions.
Brian Silvermanab0b6772017-02-05 16:16:21 -0800293 if ((*goal)(0, 0) > range_.upper) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800294 LOG(WARNING, "Goal %s above limit, %f > %f\n", name, (*goal)(0, 0),
Brian Silvermanab0b6772017-02-05 16:16:21 -0800295 range_.upper);
296 (*goal)(0, 0) = range_.upper;
297 }
298 if ((*goal)(0, 0) < range_.lower) {
Austin Schuh3634ed32017-02-05 16:28:49 -0800299 LOG(WARNING, "Goal %s below limit, %f < %f\n", name, (*goal)(0, 0),
Brian Silvermanab0b6772017-02-05 16:16:21 -0800300 range_.lower);
301 (*goal)(0, 0) = range_.lower;
302 }
303}
304
305template <class ZeroingEstimator>
306void SingleDOFProfiledSubsystem<ZeroingEstimator>::ForceGoal(double goal) {
307 set_unprofiled_goal(goal);
308 loop_->mutable_R() = unprofiled_goal_;
309 loop_->mutable_next_R() = loop_->R();
310
311 profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
312}
313
314template <class ZeroingEstimator>
315void SingleDOFProfiledSubsystem<ZeroingEstimator>::set_unprofiled_goal(
316 double unprofiled_goal) {
317 unprofiled_goal_(0, 0) = unprofiled_goal;
318 unprofiled_goal_(1, 0) = 0.0;
319 unprofiled_goal_(2, 0) = 0.0;
320 CapGoal("unprofiled R", &unprofiled_goal_);
321}
322
323template <class ZeroingEstimator>
324void SingleDOFProfiledSubsystem<ZeroingEstimator>::Update(bool disable) {
325 if (!disable) {
326 ::Eigen::Matrix<double, 2, 1> goal_state =
327 profile_.Update(unprofiled_goal_(0, 0), unprofiled_goal_(1, 0));
328
329 loop_->mutable_next_R(0, 0) = goal_state(0, 0);
330 loop_->mutable_next_R(1, 0) = goal_state(1, 0);
331 loop_->mutable_next_R(2, 0) = 0.0;
332 CapGoal("next R", &loop_->mutable_next_R());
333 }
334
335 loop_->Update(disable);
336
337 if (!disable && loop_->U(0, 0) != loop_->U_uncapped(0, 0)) {
338 profile_.MoveCurrentState(loop_->R().block<2, 1>(0, 0));
339 }
340}
341
342template <class ZeroingEstimator>
343bool SingleDOFProfiledSubsystem<ZeroingEstimator>::CheckHardLimits() {
344 // Returns whether hard limits have been exceeded.
345
Austin Schuh3634ed32017-02-05 16:28:49 -0800346 if (position() > range_.upper_hard || position() < range_.lower_hard) {
Brian Silvermanab0b6772017-02-05 16:16:21 -0800347 LOG(ERROR,
348 "SingleDOFProfiledSubsystem at %f out of bounds [%f, %f], ESTOPing\n",
Austin Schuh3634ed32017-02-05 16:28:49 -0800349 position(), range_.lower_hard, range_.upper_hard);
Brian Silvermanab0b6772017-02-05 16:16:21 -0800350 return true;
351 }
352
353 return false;
354}
355
356template <class ZeroingEstimator>
357void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Austin Schuh3634ed32017-02-05 16:28:49 -0800358 const ::frc971::ProfileParameters &profile_parameters) {
359 AdjustProfile(profile_parameters.max_velocity,
360 profile_parameters.max_acceleration);
361}
362
363template <class ZeroingEstimator>
364void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile(
Brian Silvermanab0b6772017-02-05 16:16:21 -0800365 double max_angular_velocity, double max_angular_acceleration) {
366 profile_.set_maximum_velocity(
367 internal::UseUnlessZero(max_angular_velocity, default_velocity_));
368 profile_.set_maximum_acceleration(
369 internal::UseUnlessZero(max_angular_acceleration, default_acceleration_));
370}
371
Austin Schuh473a5652017-02-05 01:30:42 -0800372} // namespace control_loops
373} // namespace frc971
374
375#endif // FRC971_CONTROL_LOOPS_PROFILED_SUBSYSTEM_H_