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" |
Austin Schuh | 00be3a8 | 2017-02-05 19:01:40 -0800 | [diff] [blame] | 18 | #include "frc971/constants.h" |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 19 | |
| 20 | namespace frc971 { |
| 21 | namespace control_loops { |
| 22 | |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 23 | // TODO(Brian): Use a tuple instead of an array to support heterogeneous zeroing |
| 24 | // styles. |
| 25 | template <int number_of_states, int number_of_axes, |
| 26 | class ZeroingEstimator = |
| 27 | ::frc971::zeroing::PotAndIndexPulseZeroingEstimator> |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 28 | class ProfiledSubsystem { |
| 29 | public: |
| 30 | ProfiledSubsystem( |
| 31 | ::std::unique_ptr<::frc971::control_loops::SimpleCappedStateFeedbackLoop< |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 32 | number_of_states, number_of_axes, number_of_axes>> loop, |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 33 | ::std::array<ZeroingEstimator, number_of_axes> &&estimators) |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 34 | : 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 Schuh | 3a81d5c | 2017-02-05 23:13:47 -0800 | [diff] [blame] | 51 | initialized_ = false; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 52 | for (auto &estimator : estimators_) { |
| 53 | estimator.Reset(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Returns the controller. |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 58 | const StateFeedbackLoop<number_of_states, number_of_axes, number_of_axes> & |
| 59 | controller() const { |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 60 | return *loop_; |
| 61 | } |
| 62 | |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 63 | int controller_index() const { return loop_->index(); } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 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 Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 100 | typename ZeroingEstimator::State EstimatorState(int index) { |
Brian Silverman | 4f2e2ce | 2017-02-19 17:49:47 -0800 | [diff] [blame] | 101 | return estimators_[index].GetEstimatorState(); |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 102 | } |
| 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 Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 117 | number_of_states, number_of_axes, number_of_axes>> loop_; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 118 | |
| 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 Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 124 | ::std::array<ZeroingEstimator, number_of_axes> estimators_; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 125 | |
| 126 | private: |
| 127 | ::std::array<bool, number_of_axes> zeroed_; |
| 128 | }; |
| 129 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 130 | template <typename ZeroingEstimator = |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 131 | ::frc971::zeroing::PotAndIndexPulseZeroingEstimator> |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 132 | class SingleDOFProfiledSubsystem |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 133 | : public ::frc971::control_loops::ProfiledSubsystem<3, 1, ZeroingEstimator> { |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 134 | public: |
| 135 | SingleDOFProfiledSubsystem( |
| 136 | ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop, |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 137 | const typename ZeroingEstimator::ZeroingConstants &zeroing_constants, |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 138 | const ::frc971::constants::Range &range, double default_angular_velocity, |
| 139 | double default_angular_acceleration); |
| 140 | |
| 141 | // Updates our estimator with the latest position. |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 142 | void Correct(typename ZeroingEstimator::Position position); |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 143 | // Runs the controller and profile generator for a cycle. |
| 144 | void Update(bool disabled); |
| 145 | |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 146 | // Fills out the ProfiledJointStatus structure with the current state. |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 147 | template <class StatusType> |
| 148 | void PopulateStatus(StatusType *status); |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 149 | |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 150 | // 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 Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 156 | void AdjustProfile(const ::frc971::ProfileParameters &profile_parameters); |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 157 | 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 Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 163 | // Returns the requested voltage. |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 164 | double voltage() const { return this->loop_->U(0, 0); } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 165 | |
| 166 | // Returns the current position. |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 167 | double position() const { return this->Y_(0, 0); } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 168 | |
| 169 | // For testing: |
| 170 | // Triggers an estimator error. |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 171 | void TriggerEstimatorError() { this->estimators_[0].TriggerError(); } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 172 | |
Austin Schuh | 00be3a8 | 2017-02-05 19:01:40 -0800 | [diff] [blame] | 173 | const ::frc971::constants::Range &range() const { return range_; } |
| 174 | |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 175 | protected: |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 176 | // Limits the provided goal to the soft limits. Prints "name" when it fails |
| 177 | // to aid debugging. |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 178 | virtual void CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal); |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 179 | |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 180 | private: |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 181 | void UpdateOffset(double offset); |
| 182 | |
| 183 | aos::util::TrapezoidProfile profile_; |
| 184 | |
| 185 | // Current measurement. |
| 186 | Eigen::Matrix<double, 1, 1> Y_; |
| 187 | // Current offset. Y_ = offset_ + raw_sensor; |
| 188 | Eigen::Matrix<double, 1, 1> offset_; |
| 189 | |
| 190 | const ::frc971::constants::Range range_; |
| 191 | |
| 192 | const double default_velocity_; |
| 193 | const double default_acceleration_; |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 194 | |
| 195 | double last_position_ = 0; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 196 | }; |
| 197 | |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 198 | namespace internal { |
| 199 | |
| 200 | double UseUnlessZero(double target_value, double default_value); |
| 201 | |
| 202 | } // namespace internal |
| 203 | |
| 204 | template <class ZeroingEstimator> |
| 205 | SingleDOFProfiledSubsystem<ZeroingEstimator>::SingleDOFProfiledSubsystem( |
| 206 | ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop, |
| 207 | const typename ZeroingEstimator::ZeroingConstants &zeroing_constants, |
| 208 | const ::frc971::constants::Range &range, double default_velocity, |
| 209 | double default_acceleration) |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 210 | : ProfiledSubsystem<3, 1, ZeroingEstimator>(::std::move(loop), |
| 211 | {{zeroing_constants}}), |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 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 | |
| 221 | template <class ZeroingEstimator> |
| 222 | void 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 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 226 | this->loop_->mutable_X_hat()(0, 0) += doffset; |
| 227 | this->Y_(0, 0) += doffset; |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 228 | last_position_ += doffset; |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 229 | this->loop_->mutable_R(0, 0) += doffset; |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 230 | |
| 231 | profile_.MoveGoal(doffset); |
| 232 | offset_(0, 0) = offset; |
| 233 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 234 | CapGoal("R", &this->loop_->mutable_R()); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | template <class ZeroingEstimator> |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 238 | template <class StatusType> |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 239 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::PopulateStatus( |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 240 | StatusType *status) { |
| 241 | status->zeroed = this->zeroed(); |
Austin Schuh | 00be3a8 | 2017-02-05 19:01:40 -0800 | [diff] [blame] | 242 | status->state = -1; |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 243 | // We don't know, so default to the bad case. |
| 244 | status->estopped = true; |
| 245 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 246 | status->position = this->X_hat(0, 0); |
| 247 | status->velocity = this->X_hat(1, 0); |
| 248 | status->goal_position = this->goal(0, 0); |
| 249 | status->goal_velocity = this->goal(1, 0); |
| 250 | status->unprofiled_goal_position = this->unprofiled_goal(0, 0); |
| 251 | status->unprofiled_goal_velocity = this->unprofiled_goal(1, 0); |
| 252 | status->voltage_error = this->X_hat(2, 0); |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 253 | status->calculated_velocity = |
| 254 | (position() - last_position_) / |
| 255 | ::std::chrono::duration_cast<::std::chrono::duration<double>>( |
| 256 | ::aos::controls::kLoopFrequency) |
| 257 | .count(); |
| 258 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 259 | status->estimator_state = this->EstimatorState(0); |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 260 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 261 | Eigen::Matrix<double, 3, 1> error = this->controller().error(); |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 262 | status->position_power = |
| 263 | this->controller().controller().K(0, 0) * error(0, 0); |
| 264 | status->velocity_power = |
| 265 | this->controller().controller().K(0, 1) * error(1, 0); |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | template <class ZeroingEstimator> |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 269 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::Correct( |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 270 | typename ZeroingEstimator::Position new_position) { |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 271 | this->estimators_[0].UpdateEstimate(new_position); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 272 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 273 | if (this->estimators_[0].error()) { |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 274 | LOG(ERROR, "zeroing error\n"); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 275 | return; |
| 276 | } |
| 277 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 278 | if (!this->initialized_) { |
| 279 | if (this->estimators_[0].offset_ready()) { |
| 280 | UpdateOffset(this->estimators_[0].offset()); |
| 281 | this->initialized_ = true; |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 285 | if (!this->zeroed(0) && this->estimators_[0].zeroed()) { |
| 286 | UpdateOffset(this->estimators_[0].offset()); |
| 287 | this->set_zeroed(0, true); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 288 | } |
| 289 | |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 290 | last_position_ = position(); |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 291 | this->Y_ << new_position.encoder; |
| 292 | this->Y_ += this->offset_; |
| 293 | this->loop_->Correct(Y_); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | template <class ZeroingEstimator> |
| 297 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::CapGoal( |
| 298 | const char *name, Eigen::Matrix<double, 3, 1> *goal) { |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 299 | // Limit the goal to min/max allowable positions. |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 300 | if ((*goal)(0, 0) > range_.upper) { |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 301 | 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] | 302 | range_.upper); |
| 303 | (*goal)(0, 0) = range_.upper; |
| 304 | } |
| 305 | if ((*goal)(0, 0) < range_.lower) { |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 306 | 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] | 307 | range_.lower); |
| 308 | (*goal)(0, 0) = range_.lower; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | template <class ZeroingEstimator> |
| 313 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::ForceGoal(double goal) { |
| 314 | set_unprofiled_goal(goal); |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 315 | this->loop_->mutable_R() = this->unprofiled_goal_; |
| 316 | this->loop_->mutable_next_R() = this->loop_->R(); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 317 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 318 | const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R(); |
| 319 | this->profile_.MoveCurrentState(R.block<2, 1>(0, 0)); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | template <class ZeroingEstimator> |
| 323 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::set_unprofiled_goal( |
| 324 | double unprofiled_goal) { |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 325 | this->unprofiled_goal_(0, 0) = unprofiled_goal; |
| 326 | this->unprofiled_goal_(1, 0) = 0.0; |
| 327 | this->unprofiled_goal_(2, 0) = 0.0; |
| 328 | CapGoal("unprofiled R", &this->unprofiled_goal_); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | template <class ZeroingEstimator> |
| 332 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::Update(bool disable) { |
| 333 | if (!disable) { |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 334 | ::Eigen::Matrix<double, 2, 1> goal_state = profile_.Update( |
| 335 | this->unprofiled_goal_(0, 0), this->unprofiled_goal_(1, 0)); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 336 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 337 | this->loop_->mutable_next_R(0, 0) = goal_state(0, 0); |
| 338 | this->loop_->mutable_next_R(1, 0) = goal_state(1, 0); |
| 339 | this->loop_->mutable_next_R(2, 0) = 0.0; |
| 340 | CapGoal("next R", &this->loop_->mutable_next_R()); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 343 | this->loop_->Update(disable); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 344 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 345 | if (!disable && this->loop_->U(0, 0) != this->loop_->U_uncapped(0, 0)) { |
| 346 | const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R(); |
| 347 | profile_.MoveCurrentState(R.block<2, 1>(0, 0)); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
| 351 | template <class ZeroingEstimator> |
| 352 | bool SingleDOFProfiledSubsystem<ZeroingEstimator>::CheckHardLimits() { |
| 353 | // Returns whether hard limits have been exceeded. |
| 354 | |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 355 | if (position() > range_.upper_hard || position() < range_.lower_hard) { |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 356 | LOG(ERROR, |
| 357 | "SingleDOFProfiledSubsystem at %f out of bounds [%f, %f], ESTOPing\n", |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 358 | position(), range_.lower_hard, range_.upper_hard); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 359 | return true; |
| 360 | } |
| 361 | |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | template <class ZeroingEstimator> |
| 366 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile( |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 367 | const ::frc971::ProfileParameters &profile_parameters) { |
| 368 | AdjustProfile(profile_parameters.max_velocity, |
| 369 | profile_parameters.max_acceleration); |
| 370 | } |
| 371 | |
| 372 | template <class ZeroingEstimator> |
| 373 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile( |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 374 | double max_angular_velocity, double max_angular_acceleration) { |
| 375 | profile_.set_maximum_velocity( |
| 376 | internal::UseUnlessZero(max_angular_velocity, default_velocity_)); |
| 377 | profile_.set_maximum_acceleration( |
| 378 | internal::UseUnlessZero(max_angular_acceleration, default_acceleration_)); |
| 379 | } |
| 380 | |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 381 | } // namespace control_loops |
| 382 | } // namespace frc971 |
| 383 | |
| 384 | #endif // FRC971_CONTROL_LOOPS_PROFILED_SUBSYSTEM_H_ |