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 = |
Austin Schuh | 08d9ecf | 2017-03-05 00:58:48 -0800 | [diff] [blame] | 27 | ::frc971::zeroing::PotAndIndexPulseZeroingEstimator, |
| 28 | int number_of_inputs = number_of_axes, |
| 29 | int number_of_outputs = number_of_axes> |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 30 | class ProfiledSubsystem { |
| 31 | public: |
| 32 | ProfiledSubsystem( |
| 33 | ::std::unique_ptr<::frc971::control_loops::SimpleCappedStateFeedbackLoop< |
Austin Schuh | 08d9ecf | 2017-03-05 00:58:48 -0800 | [diff] [blame] | 34 | number_of_states, number_of_inputs, number_of_outputs>> |
| 35 | loop, |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 36 | ::std::array<ZeroingEstimator, number_of_axes> &&estimators) |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 37 | : loop_(::std::move(loop)), estimators_(::std::move(estimators)) { |
| 38 | zeroed_.fill(false); |
| 39 | unprofiled_goal_.setZero(); |
| 40 | } |
| 41 | |
| 42 | // Returns whether an error has occured |
| 43 | bool error() const { |
| 44 | for (const auto &estimator : estimators_) { |
| 45 | if (estimator.error()) { |
| 46 | return true; |
| 47 | } |
| 48 | } |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | void Reset() { |
| 53 | zeroed_.fill(false); |
Austin Schuh | 3a81d5c | 2017-02-05 23:13:47 -0800 | [diff] [blame] | 54 | initialized_ = false; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 55 | for (auto &estimator : estimators_) { |
| 56 | estimator.Reset(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // Returns the controller. |
Austin Schuh | 08d9ecf | 2017-03-05 00:58:48 -0800 | [diff] [blame] | 61 | const StateFeedbackLoop<number_of_states, number_of_inputs, number_of_outputs> & |
Tyler Chatow | f8f0311 | 2017-02-05 14:31:34 -0800 | [diff] [blame] | 62 | controller() const { |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 63 | return *loop_; |
| 64 | } |
| 65 | |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 66 | int controller_index() const { return loop_->index(); } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 67 | |
| 68 | // Returns whether the estimators have been initialized and zeroed. |
| 69 | bool initialized() const { return initialized_; } |
| 70 | |
| 71 | bool zeroed() const { |
| 72 | for (int i = 0; i < number_of_axes; ++i) { |
| 73 | if (!zeroed_[i]) { |
| 74 | return false; |
| 75 | } |
| 76 | } |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | bool zeroed(int index) const { return zeroed_[index]; }; |
| 81 | |
| 82 | // Returns the filtered goal. |
| 83 | const Eigen::Matrix<double, number_of_states, 1> &goal() const { |
| 84 | return loop_->R(); |
| 85 | } |
| 86 | double goal(int row, int col) const { return loop_->R(row, col); } |
| 87 | |
| 88 | // Returns the unprofiled goal. |
| 89 | const Eigen::Matrix<double, number_of_states, 1> &unprofiled_goal() const { |
| 90 | return unprofiled_goal_; |
| 91 | } |
| 92 | double unprofiled_goal(int row, int col) const { |
| 93 | return unprofiled_goal_(row, col); |
| 94 | } |
| 95 | |
| 96 | // Returns the current state estimate. |
| 97 | const Eigen::Matrix<double, number_of_states, 1> &X_hat() const { |
| 98 | return loop_->X_hat(); |
| 99 | } |
| 100 | double X_hat(int row, int col) const { return loop_->X_hat(row, col); } |
| 101 | |
| 102 | // Returns the current internal estimator state for logging. |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 103 | typename ZeroingEstimator::State EstimatorState(int index) { |
Brian Silverman | 4f2e2ce | 2017-02-19 17:49:47 -0800 | [diff] [blame] | 104 | return estimators_[index].GetEstimatorState(); |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // Sets the maximum voltage that will be commanded by the loop. |
Austin Schuh | 08d9ecf | 2017-03-05 00:58:48 -0800 | [diff] [blame] | 108 | void set_max_voltage(::std::array<double, number_of_inputs> voltages) { |
| 109 | for (int i = 0; i < number_of_inputs; ++i) { |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 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< |
Austin Schuh | 08d9ecf | 2017-03-05 00:58:48 -0800 | [diff] [blame] | 120 | number_of_states, number_of_inputs, number_of_outputs>> |
| 121 | loop_; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 122 | |
| 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 Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 128 | ::std::array<ZeroingEstimator, number_of_axes> estimators_; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 129 | |
| 130 | private: |
| 131 | ::std::array<bool, number_of_axes> zeroed_; |
| 132 | }; |
| 133 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 134 | template <typename ZeroingEstimator = |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 135 | ::frc971::zeroing::PotAndIndexPulseZeroingEstimator> |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 136 | class SingleDOFProfiledSubsystem |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 137 | : public ::frc971::control_loops::ProfiledSubsystem<3, 1, ZeroingEstimator> { |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 138 | public: |
| 139 | SingleDOFProfiledSubsystem( |
| 140 | ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop, |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 141 | const typename ZeroingEstimator::ZeroingConstants &zeroing_constants, |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 142 | const ::frc971::constants::Range &range, double default_angular_velocity, |
| 143 | double default_angular_acceleration); |
| 144 | |
| 145 | // Updates our estimator with the latest position. |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 146 | void Correct(typename ZeroingEstimator::Position position); |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 147 | // Runs the controller and profile generator for a cycle. |
| 148 | void Update(bool disabled); |
| 149 | |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 150 | // Fills out the ProfiledJointStatus structure with the current state. |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 151 | template <class StatusType> |
| 152 | void PopulateStatus(StatusType *status); |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 153 | |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 154 | // Forces the current goal to the provided goal, bypassing the profiler. |
| 155 | void ForceGoal(double goal); |
| 156 | // Sets the unprofiled goal. The profiler will generate a profile to go to |
| 157 | // this goal. |
| 158 | void set_unprofiled_goal(double unprofiled_goal); |
| 159 | // Limits our profiles to a max velocity and acceleration for proper motion. |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 160 | void AdjustProfile(const ::frc971::ProfileParameters &profile_parameters); |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 161 | void AdjustProfile(double max_angular_velocity, |
| 162 | double max_angular_acceleration); |
| 163 | |
| 164 | // Returns true if we have exceeded any hard limits. |
| 165 | bool CheckHardLimits(); |
| 166 | |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 167 | // Returns the requested voltage. |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 168 | double voltage() const { return this->loop_->U(0, 0); } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 169 | |
| 170 | // Returns the current position. |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 171 | double position() const { return this->Y_(0, 0); } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 172 | |
| 173 | // For testing: |
| 174 | // Triggers an estimator error. |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 175 | void TriggerEstimatorError() { this->estimators_[0].TriggerError(); } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 176 | |
Austin Schuh | 00be3a8 | 2017-02-05 19:01:40 -0800 | [diff] [blame] | 177 | const ::frc971::constants::Range &range() const { return range_; } |
| 178 | |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 179 | protected: |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 180 | // Limits the provided goal to the soft limits. Prints "name" when it fails |
| 181 | // to aid debugging. |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 182 | virtual void CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal); |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 183 | |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 184 | private: |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 185 | void UpdateOffset(double offset); |
| 186 | |
| 187 | aos::util::TrapezoidProfile profile_; |
| 188 | |
| 189 | // Current measurement. |
| 190 | Eigen::Matrix<double, 1, 1> Y_; |
| 191 | // Current offset. Y_ = offset_ + raw_sensor; |
| 192 | Eigen::Matrix<double, 1, 1> offset_; |
| 193 | |
| 194 | const ::frc971::constants::Range range_; |
| 195 | |
| 196 | const double default_velocity_; |
| 197 | const double default_acceleration_; |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 198 | |
| 199 | double last_position_ = 0; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 200 | }; |
| 201 | |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 202 | namespace internal { |
| 203 | |
| 204 | double UseUnlessZero(double target_value, double default_value); |
| 205 | |
| 206 | } // namespace internal |
| 207 | |
| 208 | template <class ZeroingEstimator> |
| 209 | SingleDOFProfiledSubsystem<ZeroingEstimator>::SingleDOFProfiledSubsystem( |
| 210 | ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop, |
| 211 | const typename ZeroingEstimator::ZeroingConstants &zeroing_constants, |
| 212 | const ::frc971::constants::Range &range, double default_velocity, |
| 213 | double default_acceleration) |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 214 | : ProfiledSubsystem<3, 1, ZeroingEstimator>(::std::move(loop), |
| 215 | {{zeroing_constants}}), |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 216 | profile_(::aos::controls::kLoopFrequency), |
| 217 | range_(range), |
| 218 | default_velocity_(default_velocity), |
| 219 | default_acceleration_(default_acceleration) { |
| 220 | Y_.setZero(); |
| 221 | offset_.setZero(); |
| 222 | AdjustProfile(0.0, 0.0); |
| 223 | } |
| 224 | |
| 225 | template <class ZeroingEstimator> |
| 226 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::UpdateOffset(double offset) { |
| 227 | const double doffset = offset - offset_(0, 0); |
| 228 | LOG(INFO, "Adjusting offset from %f to %f\n", offset_(0, 0), offset); |
| 229 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 230 | this->loop_->mutable_X_hat()(0, 0) += doffset; |
| 231 | this->Y_(0, 0) += doffset; |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 232 | last_position_ += doffset; |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 233 | this->loop_->mutable_R(0, 0) += doffset; |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 234 | |
| 235 | profile_.MoveGoal(doffset); |
| 236 | offset_(0, 0) = offset; |
| 237 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 238 | CapGoal("R", &this->loop_->mutable_R()); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | template <class ZeroingEstimator> |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 242 | template <class StatusType> |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 243 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::PopulateStatus( |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 244 | StatusType *status) { |
| 245 | status->zeroed = this->zeroed(); |
Austin Schuh | 00be3a8 | 2017-02-05 19:01:40 -0800 | [diff] [blame] | 246 | status->state = -1; |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 247 | // We don't know, so default to the bad case. |
| 248 | status->estopped = true; |
| 249 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 250 | status->position = this->X_hat(0, 0); |
| 251 | status->velocity = this->X_hat(1, 0); |
| 252 | status->goal_position = this->goal(0, 0); |
| 253 | status->goal_velocity = this->goal(1, 0); |
| 254 | status->unprofiled_goal_position = this->unprofiled_goal(0, 0); |
| 255 | status->unprofiled_goal_velocity = this->unprofiled_goal(1, 0); |
| 256 | status->voltage_error = this->X_hat(2, 0); |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 257 | status->calculated_velocity = |
| 258 | (position() - last_position_) / |
| 259 | ::std::chrono::duration_cast<::std::chrono::duration<double>>( |
| 260 | ::aos::controls::kLoopFrequency) |
| 261 | .count(); |
| 262 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 263 | status->estimator_state = this->EstimatorState(0); |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 264 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 265 | Eigen::Matrix<double, 3, 1> error = this->controller().error(); |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 266 | status->position_power = |
| 267 | this->controller().controller().K(0, 0) * error(0, 0); |
| 268 | status->velocity_power = |
| 269 | this->controller().controller().K(0, 1) * error(1, 0); |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | template <class ZeroingEstimator> |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 273 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::Correct( |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 274 | typename ZeroingEstimator::Position new_position) { |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 275 | this->estimators_[0].UpdateEstimate(new_position); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 276 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 277 | if (this->estimators_[0].error()) { |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 278 | LOG(ERROR, "zeroing error\n"); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 279 | return; |
| 280 | } |
| 281 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 282 | if (!this->initialized_) { |
| 283 | if (this->estimators_[0].offset_ready()) { |
| 284 | UpdateOffset(this->estimators_[0].offset()); |
| 285 | this->initialized_ = true; |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 289 | if (!this->zeroed(0) && this->estimators_[0].zeroed()) { |
| 290 | UpdateOffset(this->estimators_[0].offset()); |
| 291 | this->set_zeroed(0, true); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 294 | last_position_ = position(); |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 295 | this->Y_ << new_position.encoder; |
| 296 | this->Y_ += this->offset_; |
| 297 | this->loop_->Correct(Y_); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | template <class ZeroingEstimator> |
| 301 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::CapGoal( |
| 302 | const char *name, Eigen::Matrix<double, 3, 1> *goal) { |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 303 | // Limit the goal to min/max allowable positions. |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 304 | if ((*goal)(0, 0) > range_.upper) { |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 305 | 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] | 306 | range_.upper); |
| 307 | (*goal)(0, 0) = range_.upper; |
| 308 | } |
| 309 | if ((*goal)(0, 0) < range_.lower) { |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 310 | 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] | 311 | range_.lower); |
| 312 | (*goal)(0, 0) = range_.lower; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | template <class ZeroingEstimator> |
| 317 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::ForceGoal(double goal) { |
| 318 | set_unprofiled_goal(goal); |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 319 | this->loop_->mutable_R() = this->unprofiled_goal_; |
| 320 | this->loop_->mutable_next_R() = this->loop_->R(); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 321 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 322 | const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R(); |
| 323 | this->profile_.MoveCurrentState(R.block<2, 1>(0, 0)); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | template <class ZeroingEstimator> |
| 327 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::set_unprofiled_goal( |
| 328 | double unprofiled_goal) { |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 329 | this->unprofiled_goal_(0, 0) = unprofiled_goal; |
| 330 | this->unprofiled_goal_(1, 0) = 0.0; |
| 331 | this->unprofiled_goal_(2, 0) = 0.0; |
| 332 | CapGoal("unprofiled R", &this->unprofiled_goal_); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | template <class ZeroingEstimator> |
| 336 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::Update(bool disable) { |
| 337 | if (!disable) { |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 338 | ::Eigen::Matrix<double, 2, 1> goal_state = profile_.Update( |
| 339 | this->unprofiled_goal_(0, 0), this->unprofiled_goal_(1, 0)); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 340 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 341 | this->loop_->mutable_next_R(0, 0) = goal_state(0, 0); |
| 342 | this->loop_->mutable_next_R(1, 0) = goal_state(1, 0); |
| 343 | this->loop_->mutable_next_R(2, 0) = 0.0; |
| 344 | CapGoal("next R", &this->loop_->mutable_next_R()); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 347 | this->loop_->Update(disable); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 348 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 349 | if (!disable && this->loop_->U(0, 0) != this->loop_->U_uncapped(0, 0)) { |
| 350 | const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R(); |
| 351 | profile_.MoveCurrentState(R.block<2, 1>(0, 0)); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
| 355 | template <class ZeroingEstimator> |
| 356 | bool SingleDOFProfiledSubsystem<ZeroingEstimator>::CheckHardLimits() { |
| 357 | // Returns whether hard limits have been exceeded. |
| 358 | |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 359 | if (position() > range_.upper_hard || position() < range_.lower_hard) { |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 360 | LOG(ERROR, |
| 361 | "SingleDOFProfiledSubsystem at %f out of bounds [%f, %f], ESTOPing\n", |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 362 | position(), range_.lower_hard, range_.upper_hard); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 363 | return true; |
| 364 | } |
| 365 | |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | template <class ZeroingEstimator> |
| 370 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile( |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 371 | const ::frc971::ProfileParameters &profile_parameters) { |
| 372 | AdjustProfile(profile_parameters.max_velocity, |
| 373 | profile_parameters.max_acceleration); |
| 374 | } |
| 375 | |
| 376 | template <class ZeroingEstimator> |
| 377 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile( |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 378 | double max_angular_velocity, double max_angular_acceleration) { |
| 379 | profile_.set_maximum_velocity( |
| 380 | internal::UseUnlessZero(max_angular_velocity, default_velocity_)); |
| 381 | profile_.set_maximum_acceleration( |
| 382 | internal::UseUnlessZero(max_angular_acceleration, default_acceleration_)); |
| 383 | } |
| 384 | |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 385 | } // namespace control_loops |
| 386 | } // namespace frc971 |
| 387 | |
| 388 | #endif // FRC971_CONTROL_LOOPS_PROFILED_SUBSYSTEM_H_ |