Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_PROFILED_SUBSYSTEM_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_PROFILED_SUBSYSTEM_H_ |
| 3 | |
| 4 | #include <array> |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame^] | 5 | #include <chrono> |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 6 | #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 Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame^] | 13 | #include "frc971/control_loops/control_loops.q.h" |
| 14 | #include "frc971/control_loops/profiled_subsystem.q.h" |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 15 | #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 | |
| 19 | namespace frc971 { |
| 20 | namespace control_loops { |
| 21 | |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 22 | // TODO(Brian): Use a tuple instead of an array to support heterogeneous zeroing |
| 23 | // styles. |
| 24 | template <int number_of_states, int number_of_axes, |
| 25 | class ZeroingEstimator = |
| 26 | ::frc971::zeroing::PotAndIndexPulseZeroingEstimator> |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 27 | class ProfiledSubsystem { |
| 28 | public: |
| 29 | ProfiledSubsystem( |
| 30 | ::std::unique_ptr<::frc971::control_loops::SimpleCappedStateFeedbackLoop< |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 31 | number_of_states, number_of_axes, number_of_axes>> loop, |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 32 | ::std::array<ZeroingEstimator, number_of_axes> &&estimators) |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 33 | : 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 Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 56 | const StateFeedbackLoop<number_of_states, number_of_axes, number_of_axes> & |
| 57 | controller() const { |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 58 | 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 Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 119 | number_of_states, number_of_axes, number_of_axes>> loop_; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 120 | |
| 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 Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 126 | ::std::array<ZeroingEstimator, number_of_axes> estimators_; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 127 | |
| 128 | private: |
| 129 | ::std::array<bool, number_of_axes> zeroed_; |
| 130 | }; |
| 131 | |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 132 | template <class ZeroingEstimator = |
| 133 | ::frc971::zeroing::PotAndIndexPulseZeroingEstimator> |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 134 | class SingleDOFProfiledSubsystem |
| 135 | : public ::frc971::control_loops::ProfiledSubsystem<3, 1> { |
| 136 | public: |
| 137 | SingleDOFProfiledSubsystem( |
| 138 | ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop, |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 139 | const typename ZeroingEstimator::ZeroingConstants &zeroing_constants, |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 140 | const ::frc971::constants::Range &range, double default_angular_velocity, |
| 141 | double default_angular_acceleration); |
| 142 | |
| 143 | // Updates our estimator with the latest position. |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 144 | void Correct(typename ZeroingEstimator::Position position); |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 145 | // Runs the controller and profile generator for a cycle. |
| 146 | void Update(bool disabled); |
| 147 | |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame^] | 148 | // Fills out the ProfiledJointStatus structure with the current state. |
| 149 | void PopulateStatus(ProfiledJointStatus *status); |
| 150 | |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 151 | // 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 Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame^] | 157 | void AdjustProfile(const ::frc971::ProfileParameters &profile_parameters); |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 158 | 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 Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame^] | 164 | // Returns the requested voltage. |
| 165 | double voltage() const { return loop_->U(0, 0); } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 166 | |
| 167 | // Returns the current position. |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame^] | 168 | double position() const { return Y_(0, 0); } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 169 | |
| 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 Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame^] | 192 | |
| 193 | double last_position_ = 0; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 194 | }; |
| 195 | |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 196 | namespace internal { |
| 197 | |
| 198 | double UseUnlessZero(double target_value, double default_value); |
| 199 | |
| 200 | } // namespace internal |
| 201 | |
| 202 | template <class ZeroingEstimator> |
| 203 | SingleDOFProfiledSubsystem<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 | |
| 218 | template <class ZeroingEstimator> |
| 219 | void 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 Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame^] | 225 | last_position_ += doffset; |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 226 | 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 | |
| 234 | template <class ZeroingEstimator> |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame^] | 235 | void 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 | |
| 261 | template <class ZeroingEstimator> |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 262 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::Correct( |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame^] | 263 | typename ZeroingEstimator::Position new_position) { |
| 264 | estimators_[0].UpdateEstimate(new_position); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 265 | |
| 266 | if (estimators_[0].error()) { |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame^] | 267 | LOG(ERROR, "zeroing error\n"); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 268 | 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 Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame^] | 283 | last_position_ = position(); |
| 284 | Y_ << new_position.encoder; |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 285 | Y_ += offset_; |
| 286 | loop_->Correct(Y_); |
| 287 | } |
| 288 | |
| 289 | template <class ZeroingEstimator> |
| 290 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::CapGoal( |
| 291 | const char *name, Eigen::Matrix<double, 3, 1> *goal) { |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame^] | 292 | // Limit the goal to min/max allowable positions. |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 293 | if ((*goal)(0, 0) > range_.upper) { |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame^] | 294 | LOG(WARNING, "Goal %s above limit, %f > %f\n", name, (*goal)(0, 0), |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 295 | range_.upper); |
| 296 | (*goal)(0, 0) = range_.upper; |
| 297 | } |
| 298 | if ((*goal)(0, 0) < range_.lower) { |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame^] | 299 | LOG(WARNING, "Goal %s below limit, %f < %f\n", name, (*goal)(0, 0), |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 300 | range_.lower); |
| 301 | (*goal)(0, 0) = range_.lower; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | template <class ZeroingEstimator> |
| 306 | void 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 | |
| 314 | template <class ZeroingEstimator> |
| 315 | void 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 | |
| 323 | template <class ZeroingEstimator> |
| 324 | void 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 | |
| 342 | template <class ZeroingEstimator> |
| 343 | bool SingleDOFProfiledSubsystem<ZeroingEstimator>::CheckHardLimits() { |
| 344 | // Returns whether hard limits have been exceeded. |
| 345 | |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame^] | 346 | if (position() > range_.upper_hard || position() < range_.lower_hard) { |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 347 | LOG(ERROR, |
| 348 | "SingleDOFProfiledSubsystem at %f out of bounds [%f, %f], ESTOPing\n", |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame^] | 349 | position(), range_.lower_hard, range_.upper_hard); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 350 | return true; |
| 351 | } |
| 352 | |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | template <class ZeroingEstimator> |
| 357 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile( |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame^] | 358 | const ::frc971::ProfileParameters &profile_parameters) { |
| 359 | AdjustProfile(profile_parameters.max_velocity, |
| 360 | profile_parameters.max_acceleration); |
| 361 | } |
| 362 | |
| 363 | template <class ZeroingEstimator> |
| 364 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile( |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 365 | 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 Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 372 | } // namespace control_loops |
| 373 | } // namespace frc971 |
| 374 | |
| 375 | #endif // FRC971_CONTROL_LOOPS_PROFILED_SUBSYSTEM_H_ |