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 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 11 | #include "aos/controls/control_loop.h" |
| 12 | #include "aos/util/trapezoid_profile.h" |
Tyler Chatow | d3afdef | 2019-04-06 22:15:26 -0700 | [diff] [blame] | 13 | #include "frc971/constants.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 14 | #include "frc971/control_loops/control_loops_generated.h" |
| 15 | #include "frc971/control_loops/profiled_subsystem_generated.h" |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 16 | #include "frc971/control_loops/simple_capped_state_feedback_loop.h" |
| 17 | #include "frc971/control_loops/state_feedback_loop.h" |
| 18 | #include "frc971/zeroing/zeroing.h" |
| 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 | } |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 58 | should_reset_ = true; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // Returns the controller. |
Tyler Chatow | d3afdef | 2019-04-06 22:15:26 -0700 | [diff] [blame] | 62 | const StateFeedbackLoop<number_of_states, number_of_inputs, number_of_outputs> |
| 63 | &controller() const { |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 64 | return *loop_; |
| 65 | } |
| 66 | |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 67 | int controller_index() const { return loop_->index(); } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 68 | |
Tyler Chatow | d3afdef | 2019-04-06 22:15:26 -0700 | [diff] [blame] | 69 | void set_controller_index(int index) { loop_->set_index(index); } |
| 70 | |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 71 | // Returns whether the estimators have been initialized and zeroed. |
| 72 | bool initialized() const { return initialized_; } |
| 73 | |
| 74 | bool zeroed() const { |
| 75 | for (int i = 0; i < number_of_axes; ++i) { |
| 76 | if (!zeroed_[i]) { |
| 77 | return false; |
| 78 | } |
| 79 | } |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | bool zeroed(int index) const { return zeroed_[index]; }; |
| 84 | |
| 85 | // Returns the filtered goal. |
| 86 | const Eigen::Matrix<double, number_of_states, 1> &goal() const { |
| 87 | return loop_->R(); |
| 88 | } |
| 89 | double goal(int row, int col) const { return loop_->R(row, col); } |
| 90 | |
| 91 | // Returns the unprofiled goal. |
| 92 | const Eigen::Matrix<double, number_of_states, 1> &unprofiled_goal() const { |
| 93 | return unprofiled_goal_; |
| 94 | } |
| 95 | double unprofiled_goal(int row, int col) const { |
| 96 | return unprofiled_goal_(row, col); |
| 97 | } |
| 98 | |
| 99 | // Returns the current state estimate. |
| 100 | const Eigen::Matrix<double, number_of_states, 1> &X_hat() const { |
| 101 | return loop_->X_hat(); |
| 102 | } |
| 103 | double X_hat(int row, int col) const { return loop_->X_hat(row, col); } |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 104 | double &mutable_X_hat(int row, int col) const { |
| 105 | return loop_->mutable_X_hat(row, col); |
| 106 | } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 107 | |
| 108 | // Returns the current internal estimator state for logging. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 109 | flatbuffers::Offset<typename ZeroingEstimator::State> EstimatorState( |
| 110 | flatbuffers::FlatBufferBuilder *fbb, int index) { |
| 111 | return estimators_[index].GetEstimatorState(fbb); |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // Sets the maximum voltage that will be commanded by the loop. |
Austin Schuh | 08d9ecf | 2017-03-05 00:58:48 -0800 | [diff] [blame] | 115 | void set_max_voltage(::std::array<double, number_of_inputs> voltages) { |
| 116 | for (int i = 0; i < number_of_inputs; ++i) { |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 117 | loop_->set_max_voltage(i, voltages[i]); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | protected: |
| 122 | void set_zeroed(int index, bool val) { zeroed_[index] = val; } |
| 123 | |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 124 | ::std::unique_ptr<::frc971::control_loops::SimpleCappedStateFeedbackLoop< |
Austin Schuh | 08d9ecf | 2017-03-05 00:58:48 -0800 | [diff] [blame] | 125 | number_of_states, number_of_inputs, number_of_outputs>> |
| 126 | loop_; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 127 | |
| 128 | // The goal that the profile tries to reach. |
| 129 | Eigen::Matrix<double, number_of_states, 1> unprofiled_goal_; |
| 130 | |
| 131 | bool initialized_ = false; |
| 132 | |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 133 | // If true, the subclass should reset in Update. It should then clear this |
| 134 | // flag. |
| 135 | bool should_reset_ = true; |
| 136 | |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 137 | ::std::array<ZeroingEstimator, number_of_axes> estimators_; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 138 | |
| 139 | private: |
| 140 | ::std::array<bool, number_of_axes> zeroed_; |
| 141 | }; |
| 142 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 143 | template <typename ZeroingEstimator = |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 144 | ::frc971::zeroing::PotAndIndexPulseZeroingEstimator> |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 145 | class SingleDOFProfiledSubsystem |
Tyler Chatow | d3afdef | 2019-04-06 22:15:26 -0700 | [diff] [blame] | 146 | : public ::frc971::control_loops::ProfiledSubsystem<3, 1, |
| 147 | ZeroingEstimator> { |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 148 | public: |
| 149 | SingleDOFProfiledSubsystem( |
| 150 | ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop, |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 151 | const typename ZeroingEstimator::ZeroingConstants &zeroing_constants, |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 152 | const ::frc971::constants::Range &range, double default_angular_velocity, |
| 153 | double default_angular_acceleration); |
| 154 | |
| 155 | // Updates our estimator with the latest position. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 156 | void Correct(const typename ZeroingEstimator::Position &position); |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 157 | // Runs the controller and profile generator for a cycle. |
| 158 | void Update(bool disabled); |
| 159 | |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 160 | // Fills out the ProfiledJointStatus structure with the current state. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 161 | template <class StatusTypeBuilder> |
| 162 | StatusTypeBuilder BuildStatus( |
| 163 | flatbuffers::FlatBufferBuilder *fbb); |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 164 | |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 165 | // Forces the current goal to the provided goal, bypassing the profiler. |
| 166 | void ForceGoal(double goal); |
| 167 | // Sets the unprofiled goal. The profiler will generate a profile to go to |
| 168 | // this goal. |
| 169 | void set_unprofiled_goal(double unprofiled_goal); |
| 170 | // Limits our profiles to a max velocity and acceleration for proper motion. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 171 | void AdjustProfile(const ::frc971::ProfileParameters *profile_parameters); |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 172 | void AdjustProfile(double max_angular_velocity, |
| 173 | double max_angular_acceleration); |
| 174 | |
| 175 | // Returns true if we have exceeded any hard limits. |
| 176 | bool CheckHardLimits(); |
| 177 | |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 178 | // Returns the requested voltage. |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 179 | double voltage() const { return this->loop_->U(0, 0); } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 180 | |
| 181 | // Returns the current position. |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 182 | double position() const { return this->Y_(0, 0); } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 183 | |
| 184 | // For testing: |
| 185 | // Triggers an estimator error. |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 186 | void TriggerEstimatorError() { this->estimators_[0].TriggerError(); } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 187 | |
Austin Schuh | 00be3a8 | 2017-02-05 19:01:40 -0800 | [diff] [blame] | 188 | const ::frc971::constants::Range &range() const { return range_; } |
| 189 | |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 190 | protected: |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 191 | // Limits the provided goal to the soft limits. Prints "name" when it fails |
| 192 | // to aid debugging. |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 193 | virtual void CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal); |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 194 | |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 195 | private: |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 196 | void UpdateOffset(double offset); |
| 197 | |
| 198 | aos::util::TrapezoidProfile profile_; |
| 199 | |
| 200 | // Current measurement. |
| 201 | Eigen::Matrix<double, 1, 1> Y_; |
| 202 | // Current offset. Y_ = offset_ + raw_sensor; |
| 203 | Eigen::Matrix<double, 1, 1> offset_; |
| 204 | |
| 205 | const ::frc971::constants::Range range_; |
| 206 | |
| 207 | const double default_velocity_; |
| 208 | const double default_acceleration_; |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 209 | |
| 210 | double last_position_ = 0; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 211 | }; |
| 212 | |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 213 | namespace internal { |
| 214 | |
| 215 | double UseUnlessZero(double target_value, double default_value); |
| 216 | |
| 217 | } // namespace internal |
| 218 | |
| 219 | template <class ZeroingEstimator> |
| 220 | SingleDOFProfiledSubsystem<ZeroingEstimator>::SingleDOFProfiledSubsystem( |
| 221 | ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop, |
| 222 | const typename ZeroingEstimator::ZeroingConstants &zeroing_constants, |
| 223 | const ::frc971::constants::Range &range, double default_velocity, |
| 224 | double default_acceleration) |
Campbell Crowley | 36e93e9 | 2017-12-23 14:21:43 -0800 | [diff] [blame] | 225 | : ProfiledSubsystem<3, 1, ZeroingEstimator>( |
| 226 | ::std::move(loop), {{ZeroingEstimator(zeroing_constants)}}), |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 227 | profile_(::aos::controls::kLoopFrequency), |
| 228 | range_(range), |
| 229 | default_velocity_(default_velocity), |
| 230 | default_acceleration_(default_acceleration) { |
| 231 | Y_.setZero(); |
| 232 | offset_.setZero(); |
| 233 | AdjustProfile(0.0, 0.0); |
| 234 | } |
| 235 | |
| 236 | template <class ZeroingEstimator> |
| 237 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::UpdateOffset(double offset) { |
| 238 | const double doffset = offset - offset_(0, 0); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 239 | AOS_LOG(INFO, "Adjusting offset from %f to %f\n", offset_(0, 0), offset); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 240 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 241 | this->loop_->mutable_X_hat()(0, 0) += doffset; |
| 242 | this->Y_(0, 0) += doffset; |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 243 | last_position_ += doffset; |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 244 | this->loop_->mutable_R(0, 0) += doffset; |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 245 | |
| 246 | profile_.MoveGoal(doffset); |
| 247 | offset_(0, 0) = offset; |
| 248 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 249 | CapGoal("R", &this->loop_->mutable_R()); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | template <class ZeroingEstimator> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 253 | template <class StatusTypeBuilder> |
| 254 | StatusTypeBuilder SingleDOFProfiledSubsystem<ZeroingEstimator>::BuildStatus( |
| 255 | flatbuffers::FlatBufferBuilder *fbb) { |
| 256 | flatbuffers::Offset<typename ZeroingEstimator::State> estimator_state = |
| 257 | this->EstimatorState(fbb, 0); |
| 258 | |
| 259 | StatusTypeBuilder builder(*fbb); |
| 260 | |
| 261 | builder.add_zeroed(this->zeroed()); |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 262 | // We don't know, so default to the bad case. |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 263 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 264 | builder.add_position(this->X_hat(0, 0)); |
| 265 | builder.add_velocity(this->X_hat(1, 0)); |
| 266 | builder.add_goal_position(this->goal(0, 0)); |
| 267 | builder.add_goal_velocity(this->goal(1, 0)); |
| 268 | builder.add_unprofiled_goal_position(this->unprofiled_goal(0, 0)); |
| 269 | builder.add_unprofiled_goal_velocity(this->unprofiled_goal(1, 0)); |
| 270 | builder.add_voltage_error(this->X_hat(2, 0)); |
| 271 | builder.add_calculated_velocity( |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 272 | (position() - last_position_) / |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 273 | ::aos::time::DurationInSeconds(::aos::controls::kLoopFrequency)); |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 274 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 275 | builder.add_estimator_state(estimator_state); |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 276 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 277 | Eigen::Matrix<double, 3, 1> error = this->controller().error(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 278 | builder.add_position_power( |
| 279 | this->controller().controller().K(0, 0) * error(0, 0)); |
| 280 | builder.add_velocity_power( |
| 281 | this->controller().controller().K(0, 1) * error(1, 0)); |
| 282 | return builder; |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | template <class ZeroingEstimator> |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 286 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::Correct( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 287 | const typename ZeroingEstimator::Position &new_position) { |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 288 | this->estimators_[0].UpdateEstimate(new_position); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 289 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 290 | if (this->estimators_[0].error()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 291 | AOS_LOG(ERROR, "zeroing error\n"); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 292 | return; |
| 293 | } |
| 294 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 295 | if (!this->initialized_) { |
| 296 | if (this->estimators_[0].offset_ready()) { |
| 297 | UpdateOffset(this->estimators_[0].offset()); |
| 298 | this->initialized_ = true; |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 302 | if (!this->zeroed(0) && this->estimators_[0].zeroed()) { |
| 303 | UpdateOffset(this->estimators_[0].offset()); |
| 304 | this->set_zeroed(0, true); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 307 | last_position_ = position(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 308 | this->Y_ << new_position.encoder(); |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 309 | this->Y_ += this->offset_; |
| 310 | this->loop_->Correct(Y_); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | template <class ZeroingEstimator> |
| 314 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::CapGoal( |
| 315 | const char *name, Eigen::Matrix<double, 3, 1> *goal) { |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 316 | // Limit the goal to min/max allowable positions. |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 317 | if ((*goal)(0, 0) > range_.upper) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 318 | AOS_LOG(WARNING, "Goal %s above limit, %f > %f\n", name, (*goal)(0, 0), |
| 319 | range_.upper); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 320 | (*goal)(0, 0) = range_.upper; |
| 321 | } |
| 322 | if ((*goal)(0, 0) < range_.lower) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 323 | AOS_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] | 324 | range_.lower); |
| 325 | (*goal)(0, 0) = range_.lower; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | template <class ZeroingEstimator> |
| 330 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::ForceGoal(double goal) { |
| 331 | set_unprofiled_goal(goal); |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 332 | this->loop_->mutable_R() = this->unprofiled_goal_; |
| 333 | this->loop_->mutable_next_R() = this->loop_->R(); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 334 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 335 | const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R(); |
| 336 | this->profile_.MoveCurrentState(R.block<2, 1>(0, 0)); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | template <class ZeroingEstimator> |
| 340 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::set_unprofiled_goal( |
| 341 | double unprofiled_goal) { |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 342 | this->unprofiled_goal_(0, 0) = unprofiled_goal; |
| 343 | this->unprofiled_goal_(1, 0) = 0.0; |
| 344 | this->unprofiled_goal_(2, 0) = 0.0; |
| 345 | CapGoal("unprofiled R", &this->unprofiled_goal_); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | template <class ZeroingEstimator> |
| 349 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::Update(bool disable) { |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 350 | // TODO(austin): What do we want to do with the profile on reset? Also, we |
| 351 | // should probably reset R, the offset, the profile, etc. |
| 352 | if (this->should_reset_) { |
| 353 | this->loop_->mutable_X_hat(0, 0) = Y_(0, 0); |
| 354 | this->loop_->mutable_X_hat(1, 0) = 0.0; |
| 355 | this->loop_->mutable_X_hat(2, 0) = 0.0; |
| 356 | this->should_reset_ = false; |
| 357 | } |
| 358 | |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 359 | if (!disable) { |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 360 | ::Eigen::Matrix<double, 2, 1> goal_state = profile_.Update( |
| 361 | this->unprofiled_goal_(0, 0), this->unprofiled_goal_(1, 0)); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 362 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 363 | this->loop_->mutable_next_R(0, 0) = goal_state(0, 0); |
| 364 | this->loop_->mutable_next_R(1, 0) = goal_state(1, 0); |
| 365 | this->loop_->mutable_next_R(2, 0) = 0.0; |
| 366 | CapGoal("next R", &this->loop_->mutable_next_R()); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 369 | this->loop_->Update(disable); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 370 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 371 | if (!disable && this->loop_->U(0, 0) != this->loop_->U_uncapped(0, 0)) { |
| 372 | const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R(); |
| 373 | profile_.MoveCurrentState(R.block<2, 1>(0, 0)); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
| 377 | template <class ZeroingEstimator> |
| 378 | bool SingleDOFProfiledSubsystem<ZeroingEstimator>::CheckHardLimits() { |
| 379 | // Returns whether hard limits have been exceeded. |
| 380 | |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 381 | if (position() > range_.upper_hard || position() < range_.lower_hard) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 382 | AOS_LOG( |
| 383 | ERROR, |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 384 | "SingleDOFProfiledSubsystem at %f out of bounds [%f, %f], ESTOPing\n", |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 385 | position(), range_.lower_hard, range_.upper_hard); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 386 | return true; |
| 387 | } |
| 388 | |
| 389 | return false; |
| 390 | } |
| 391 | |
| 392 | template <class ZeroingEstimator> |
| 393 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 394 | const ::frc971::ProfileParameters *profile_parameters) { |
| 395 | AdjustProfile( |
| 396 | profile_parameters != nullptr ? profile_parameters->max_velocity() : 0.0, |
| 397 | profile_parameters != nullptr ? profile_parameters->max_acceleration() |
| 398 | : 0.0); |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | template <class ZeroingEstimator> |
| 402 | void SingleDOFProfiledSubsystem<ZeroingEstimator>::AdjustProfile( |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 403 | double max_angular_velocity, double max_angular_acceleration) { |
| 404 | profile_.set_maximum_velocity( |
| 405 | internal::UseUnlessZero(max_angular_velocity, default_velocity_)); |
| 406 | profile_.set_maximum_acceleration( |
| 407 | internal::UseUnlessZero(max_angular_acceleration, default_acceleration_)); |
| 408 | } |
| 409 | |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 410 | } // namespace control_loops |
| 411 | } // namespace frc971 |
| 412 | |
| 413 | #endif // FRC971_CONTROL_LOOPS_PROFILED_SUBSYSTEM_H_ |