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" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 10 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 11 | #include "aos/util/trapezoid_profile.h" |
Tyler Chatow | d3afdef | 2019-04-06 22:15:26 -0700 | [diff] [blame] | 12 | #include "frc971/constants.h" |
Austin Schuh | 482a914 | 2022-02-23 16:54:39 -0800 | [diff] [blame] | 13 | #include "frc971/control_loops/control_loop.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" |
James Kuszmaul | ec635d2 | 2023-08-12 18:39:24 -0700 | [diff] [blame] | 18 | #include "frc971/zeroing/pot_and_index.h" |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 19 | #include "frc971/zeroing/zeroing.h" |
| 20 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 21 | namespace frc971::control_loops { |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 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(); |
Austin Schuh | 2669ece | 2022-03-11 18:30:57 -0800 | [diff] [blame] | 40 | X_hat_.setZero(); |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // Returns whether an error has occured |
| 44 | bool error() const { |
| 45 | for (const auto &estimator : estimators_) { |
| 46 | if (estimator.error()) { |
| 47 | return true; |
| 48 | } |
| 49 | } |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | void Reset() { |
| 54 | zeroed_.fill(false); |
Austin Schuh | 3a81d5c | 2017-02-05 23:13:47 -0800 | [diff] [blame] | 55 | initialized_ = false; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 56 | for (auto &estimator : estimators_) { |
| 57 | estimator.Reset(); |
| 58 | } |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 59 | should_reset_ = true; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // Returns the controller. |
Austin Schuh | 50e3dca | 2023-07-23 14:34:27 -0700 | [diff] [blame] | 63 | const StateFeedbackLoop<number_of_states, number_of_inputs, |
| 64 | number_of_outputs> & |
| 65 | controller() const { |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 66 | return *loop_; |
| 67 | } |
| 68 | |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 69 | int controller_index() const { return loop_->index(); } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 70 | |
Tyler Chatow | d3afdef | 2019-04-06 22:15:26 -0700 | [diff] [blame] | 71 | void set_controller_index(int index) { loop_->set_index(index); } |
| 72 | |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 73 | // Returns whether the estimators have been initialized and zeroed. |
| 74 | bool initialized() const { return initialized_; } |
| 75 | |
| 76 | bool zeroed() const { |
| 77 | for (int i = 0; i < number_of_axes; ++i) { |
| 78 | if (!zeroed_[i]) { |
| 79 | return false; |
| 80 | } |
| 81 | } |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | bool zeroed(int index) const { return zeroed_[index]; }; |
| 86 | |
| 87 | // Returns the filtered goal. |
| 88 | const Eigen::Matrix<double, number_of_states, 1> &goal() const { |
| 89 | return loop_->R(); |
| 90 | } |
| 91 | double goal(int row, int col) const { return loop_->R(row, col); } |
| 92 | |
| 93 | // Returns the unprofiled goal. |
| 94 | const Eigen::Matrix<double, number_of_states, 1> &unprofiled_goal() const { |
| 95 | return unprofiled_goal_; |
| 96 | } |
| 97 | double unprofiled_goal(int row, int col) const { |
| 98 | return unprofiled_goal_(row, col); |
| 99 | } |
| 100 | |
Austin Schuh | 2669ece | 2022-03-11 18:30:57 -0800 | [diff] [blame] | 101 | // Returns the current state estimate after the most recent Correct. This |
| 102 | // does not change when Predict is run. |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 103 | const Eigen::Matrix<double, number_of_states, 1> &X_hat() const { |
Austin Schuh | 2669ece | 2022-03-11 18:30:57 -0800 | [diff] [blame] | 104 | return X_hat_; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 105 | } |
Austin Schuh | 2669ece | 2022-03-11 18:30:57 -0800 | [diff] [blame] | 106 | double X_hat(int row, int col) const { return X_hat()(row, col); } |
| 107 | // Returns a mutable reference to the current state of the actual kalman |
| 108 | // filter state. Note: changing this won't change X_hat() immediately. |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 109 | double &mutable_X_hat(int row, int col) const { |
| 110 | return loop_->mutable_X_hat(row, col); |
| 111 | } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 112 | |
| 113 | // Returns the current internal estimator state for logging. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 114 | flatbuffers::Offset<typename ZeroingEstimator::State> EstimatorState( |
| 115 | flatbuffers::FlatBufferBuilder *fbb, int index) { |
| 116 | return estimators_[index].GetEstimatorState(fbb); |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Sets the maximum voltage that will be commanded by the loop. |
Austin Schuh | 08d9ecf | 2017-03-05 00:58:48 -0800 | [diff] [blame] | 120 | void set_max_voltage(::std::array<double, number_of_inputs> voltages) { |
| 121 | for (int i = 0; i < number_of_inputs; ++i) { |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 122 | loop_->set_max_voltage(i, voltages[i]); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | protected: |
| 127 | void set_zeroed(int index, bool val) { zeroed_[index] = val; } |
| 128 | |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 129 | ::std::unique_ptr<::frc971::control_loops::SimpleCappedStateFeedbackLoop< |
Austin Schuh | 08d9ecf | 2017-03-05 00:58:48 -0800 | [diff] [blame] | 130 | number_of_states, number_of_inputs, number_of_outputs>> |
| 131 | loop_; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 132 | |
| 133 | // The goal that the profile tries to reach. |
| 134 | Eigen::Matrix<double, number_of_states, 1> unprofiled_goal_; |
| 135 | |
Austin Schuh | 2669ece | 2022-03-11 18:30:57 -0800 | [diff] [blame] | 136 | Eigen::Matrix<double, number_of_states, 1> X_hat_; |
| 137 | |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 138 | bool initialized_ = false; |
| 139 | |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 140 | // If true, the subclass should reset in Update. It should then clear this |
| 141 | // flag. |
| 142 | bool should_reset_ = true; |
| 143 | |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 144 | ::std::array<ZeroingEstimator, number_of_axes> estimators_; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 145 | |
| 146 | private: |
| 147 | ::std::array<bool, number_of_axes> zeroed_; |
| 148 | }; |
| 149 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 150 | template <typename ZeroingEstimator = |
Austin Schuh | ea83443 | 2024-02-20 21:23:13 -0800 | [diff] [blame] | 151 | ::frc971::zeroing::PotAndIndexPulseZeroingEstimator, |
| 152 | class Profile = aos::util::TrapezoidProfile> |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 153 | class SingleDOFProfiledSubsystem |
Tyler Chatow | d3afdef | 2019-04-06 22:15:26 -0700 | [diff] [blame] | 154 | : public ::frc971::control_loops::ProfiledSubsystem<3, 1, |
| 155 | ZeroingEstimator> { |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 156 | public: |
| 157 | SingleDOFProfiledSubsystem( |
| 158 | ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop, |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 159 | const typename ZeroingEstimator::ZeroingConstants &zeroing_constants, |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 160 | const ::frc971::constants::Range &range, double default_angular_velocity, |
| 161 | double default_angular_acceleration); |
| 162 | |
| 163 | // Updates our estimator with the latest position. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 164 | void Correct(const typename ZeroingEstimator::Position &position); |
Austin Schuh | 482a914 | 2022-02-23 16:54:39 -0800 | [diff] [blame] | 165 | // Runs the controller and profile generator for a cycle. This is equivilent |
| 166 | // to calling UpdateObserver(UpdateController()) with the rest of the syntax |
| 167 | // actually right. |
| 168 | double Update(bool disabled); |
| 169 | // Just computes the controller and pushes the feed forwards forwards 1 step. |
| 170 | double UpdateController(bool disabled); |
| 171 | // Updates the observer with the computed U. |
| 172 | // Note: if this is the only method called, ForceGoal should also be called to |
| 173 | // move the state to match. |
| 174 | void UpdateObserver(double voltage); |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 175 | |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 176 | // Fills out the ProfiledJointStatus structure with the current state. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 177 | template <class StatusTypeBuilder> |
Austin Schuh | 482a914 | 2022-02-23 16:54:39 -0800 | [diff] [blame] | 178 | StatusTypeBuilder BuildStatus(flatbuffers::FlatBufferBuilder *fbb); |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 179 | |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 180 | // Forces the current goal to the provided goal, bypassing the profiler. |
Austin Schuh | 482a914 | 2022-02-23 16:54:39 -0800 | [diff] [blame] | 181 | void ForceGoal(double goal, double goal_velocity = 0.0); |
James Kuszmaul | 4fb2976 | 2020-02-20 19:37:41 -0800 | [diff] [blame] | 182 | // Sets whether to use the trapezoidal profiler or whether to just bypass it |
| 183 | // and pass the unprofiled goal through directly. |
| 184 | void set_enable_profile(bool enable) { enable_profile_ = enable; } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 185 | // Sets the unprofiled goal. The profiler will generate a profile to go to |
| 186 | // this goal. |
James Kuszmaul | 4fb2976 | 2020-02-20 19:37:41 -0800 | [diff] [blame] | 187 | void set_unprofiled_goal(double unprofiled_goal, |
Sabina Davis | 0e484f9 | 2020-02-23 17:47:53 -0800 | [diff] [blame] | 188 | double unprofiled_goal_velocity = 0.0, |
James Kuszmaul | fc4e3f5 | 2024-04-05 17:32:58 -0700 | [diff] [blame] | 189 | bool print = false); |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 190 | // Limits our profiles to a max velocity and acceleration for proper motion. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 191 | void AdjustProfile(const ::frc971::ProfileParameters *profile_parameters); |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 192 | void AdjustProfile(double max_angular_velocity, |
| 193 | double max_angular_acceleration); |
| 194 | |
| 195 | // Returns true if we have exceeded any hard limits. |
| 196 | bool CheckHardLimits(); |
| 197 | |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 198 | // Returns the requested voltage. |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 199 | double voltage() const { return this->loop_->U(0, 0); } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 200 | |
Austin Schuh | 482a914 | 2022-02-23 16:54:39 -0800 | [diff] [blame] | 201 | // Returns the current position or velocity. |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 202 | double position() const { return this->Y_(0, 0); } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 203 | |
| 204 | // For testing: |
| 205 | // Triggers an estimator error. |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 206 | void TriggerEstimatorError() { this->estimators_[0].TriggerError(); } |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 207 | |
Austin Schuh | 00be3a8 | 2017-02-05 19:01:40 -0800 | [diff] [blame] | 208 | const ::frc971::constants::Range &range() const { return range_; } |
| 209 | |
Austin Schuh | 93ddcb4 | 2021-10-25 21:54:11 -0700 | [diff] [blame] | 210 | double default_velocity() const { return default_velocity_; } |
| 211 | double default_acceleration() const { return default_acceleration_; } |
| 212 | |
Austin Schuh | ea83443 | 2024-02-20 21:23:13 -0800 | [diff] [blame] | 213 | // Returns a pointer to the profile in use. |
| 214 | Profile *mutable_profile() { return &profile_; } |
| 215 | |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 216 | protected: |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 217 | // Limits the provided goal to the soft limits. Prints "name" when it fails |
| 218 | // to aid debugging. |
Sabina Davis | 0e484f9 | 2020-02-23 17:47:53 -0800 | [diff] [blame] | 219 | virtual void CapGoal(const char *name, Eigen::Matrix<double, 3, 1> *goal, |
James Kuszmaul | fc4e3f5 | 2024-04-05 17:32:58 -0700 | [diff] [blame] | 220 | bool print = false); |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 221 | |
Austin Schuh | 6a90cd9 | 2017-02-19 20:55:33 -0800 | [diff] [blame] | 222 | private: |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 223 | void UpdateOffset(double offset); |
| 224 | |
Austin Schuh | ea83443 | 2024-02-20 21:23:13 -0800 | [diff] [blame] | 225 | Profile profile_; |
James Kuszmaul | 4fb2976 | 2020-02-20 19:37:41 -0800 | [diff] [blame] | 226 | bool enable_profile_ = true; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 227 | |
| 228 | // Current measurement. |
| 229 | Eigen::Matrix<double, 1, 1> Y_; |
| 230 | // Current offset. Y_ = offset_ + raw_sensor; |
| 231 | Eigen::Matrix<double, 1, 1> offset_; |
| 232 | |
| 233 | const ::frc971::constants::Range range_; |
| 234 | |
| 235 | const double default_velocity_; |
| 236 | const double default_acceleration_; |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 237 | |
| 238 | double last_position_ = 0; |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 239 | }; |
| 240 | |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 241 | namespace internal { |
| 242 | |
| 243 | double UseUnlessZero(double target_value, double default_value); |
| 244 | |
| 245 | } // namespace internal |
| 246 | |
Austin Schuh | ea83443 | 2024-02-20 21:23:13 -0800 | [diff] [blame] | 247 | template <class ZeroingEstimator, class Profile> |
| 248 | SingleDOFProfiledSubsystem<ZeroingEstimator, Profile>:: |
| 249 | SingleDOFProfiledSubsystem( |
| 250 | ::std::unique_ptr<SimpleCappedStateFeedbackLoop<3, 1, 1>> loop, |
| 251 | const typename ZeroingEstimator::ZeroingConstants &zeroing_constants, |
| 252 | const ::frc971::constants::Range &range, double default_velocity, |
| 253 | double default_acceleration) |
Campbell Crowley | 36e93e9 | 2017-12-23 14:21:43 -0800 | [diff] [blame] | 254 | : ProfiledSubsystem<3, 1, ZeroingEstimator>( |
| 255 | ::std::move(loop), {{ZeroingEstimator(zeroing_constants)}}), |
Sabina Davis | 1184cc5 | 2020-02-22 18:29:25 -0800 | [diff] [blame] | 256 | profile_(this->loop_->plant().coefficients().dt), |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 257 | range_(range), |
| 258 | default_velocity_(default_velocity), |
| 259 | default_acceleration_(default_acceleration) { |
| 260 | Y_.setZero(); |
| 261 | offset_.setZero(); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Austin Schuh | ea83443 | 2024-02-20 21:23:13 -0800 | [diff] [blame] | 264 | template <class ZeroingEstimator, class Profile> |
| 265 | void SingleDOFProfiledSubsystem<ZeroingEstimator, Profile>::UpdateOffset( |
| 266 | double offset) { |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 267 | const double doffset = offset - offset_(0, 0); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 268 | 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] | 269 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 270 | this->loop_->mutable_X_hat()(0, 0) += doffset; |
| 271 | this->Y_(0, 0) += doffset; |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 272 | last_position_ += doffset; |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 273 | this->loop_->mutable_R(0, 0) += doffset; |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 274 | |
| 275 | profile_.MoveGoal(doffset); |
| 276 | offset_(0, 0) = offset; |
| 277 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 278 | CapGoal("R", &this->loop_->mutable_R()); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 279 | } |
| 280 | |
Austin Schuh | ea83443 | 2024-02-20 21:23:13 -0800 | [diff] [blame] | 281 | template <class ZeroingEstimator, class Profile> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 282 | template <class StatusTypeBuilder> |
Austin Schuh | ea83443 | 2024-02-20 21:23:13 -0800 | [diff] [blame] | 283 | StatusTypeBuilder |
| 284 | SingleDOFProfiledSubsystem<ZeroingEstimator, Profile>::BuildStatus( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 285 | flatbuffers::FlatBufferBuilder *fbb) { |
| 286 | flatbuffers::Offset<typename ZeroingEstimator::State> estimator_state = |
| 287 | this->EstimatorState(fbb, 0); |
| 288 | |
| 289 | StatusTypeBuilder builder(*fbb); |
| 290 | |
| 291 | builder.add_zeroed(this->zeroed()); |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 292 | // We don't know, so default to the bad case. |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 293 | |
Austin Schuh | 2669ece | 2022-03-11 18:30:57 -0800 | [diff] [blame] | 294 | builder.add_position(this->X_hat_(0, 0)); |
| 295 | builder.add_velocity(this->X_hat_(1, 0)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 296 | builder.add_goal_position(this->goal(0, 0)); |
| 297 | builder.add_goal_velocity(this->goal(1, 0)); |
| 298 | builder.add_unprofiled_goal_position(this->unprofiled_goal(0, 0)); |
| 299 | builder.add_unprofiled_goal_velocity(this->unprofiled_goal(1, 0)); |
Austin Schuh | 2669ece | 2022-03-11 18:30:57 -0800 | [diff] [blame] | 300 | builder.add_voltage_error(this->X_hat_(2, 0)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 301 | builder.add_calculated_velocity( |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 302 | (position() - last_position_) / |
Sabina Davis | 1184cc5 | 2020-02-22 18:29:25 -0800 | [diff] [blame] | 303 | ::aos::time::DurationInSeconds(this->loop_->plant().coefficients().dt)); |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 304 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 305 | builder.add_estimator_state(estimator_state); |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 306 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 307 | Eigen::Matrix<double, 3, 1> error = this->controller().error(); |
Austin Schuh | 482a914 | 2022-02-23 16:54:39 -0800 | [diff] [blame] | 308 | builder.add_position_power(this->controller().controller().K(0, 0) * |
| 309 | error(0, 0)); |
| 310 | builder.add_velocity_power(this->controller().controller().K(0, 1) * |
| 311 | error(1, 0)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 312 | return builder; |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Austin Schuh | ea83443 | 2024-02-20 21:23:13 -0800 | [diff] [blame] | 315 | template <class ZeroingEstimator, class Profile> |
| 316 | void SingleDOFProfiledSubsystem<ZeroingEstimator, Profile>::Correct( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 317 | const typename ZeroingEstimator::Position &new_position) { |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 318 | this->estimators_[0].UpdateEstimate(new_position); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 319 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 320 | if (this->estimators_[0].error()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 321 | AOS_LOG(ERROR, "zeroing error\n"); |
Austin Schuh | 2669ece | 2022-03-11 18:30:57 -0800 | [diff] [blame] | 322 | this->X_hat_ = this->loop_->X_hat(); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 323 | return; |
| 324 | } |
| 325 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 326 | if (!this->initialized_) { |
| 327 | if (this->estimators_[0].offset_ready()) { |
| 328 | UpdateOffset(this->estimators_[0].offset()); |
| 329 | this->initialized_ = true; |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 333 | if (!this->zeroed(0) && this->estimators_[0].zeroed()) { |
| 334 | UpdateOffset(this->estimators_[0].offset()); |
| 335 | this->set_zeroed(0, true); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 338 | last_position_ = position(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 339 | this->Y_ << new_position.encoder(); |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 340 | this->Y_ += this->offset_; |
| 341 | this->loop_->Correct(Y_); |
Austin Schuh | 2669ece | 2022-03-11 18:30:57 -0800 | [diff] [blame] | 342 | this->X_hat_ = this->loop_->X_hat(); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Austin Schuh | ea83443 | 2024-02-20 21:23:13 -0800 | [diff] [blame] | 345 | template <class ZeroingEstimator, class Profile> |
| 346 | void SingleDOFProfiledSubsystem<ZeroingEstimator, Profile>::CapGoal( |
Sabina Davis | 0e484f9 | 2020-02-23 17:47:53 -0800 | [diff] [blame] | 347 | const char *name, Eigen::Matrix<double, 3, 1> *goal, bool print) { |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 348 | // Limit the goal to min/max allowable positions. |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 349 | if ((*goal)(0, 0) > range_.upper) { |
Sabina Davis | 0e484f9 | 2020-02-23 17:47:53 -0800 | [diff] [blame] | 350 | if (print) { |
| 351 | AOS_LOG(WARNING, "Goal %s above limit, %f > %f\n", name, (*goal)(0, 0), |
| 352 | range_.upper); |
| 353 | } |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 354 | (*goal)(0, 0) = range_.upper; |
| 355 | } |
| 356 | if ((*goal)(0, 0) < range_.lower) { |
Sabina Davis | 0e484f9 | 2020-02-23 17:47:53 -0800 | [diff] [blame] | 357 | if (print) { |
| 358 | AOS_LOG(WARNING, "Goal %s below limit, %f < %f\n", name, (*goal)(0, 0), |
| 359 | range_.lower); |
| 360 | } |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 361 | (*goal)(0, 0) = range_.lower; |
| 362 | } |
| 363 | } |
| 364 | |
Austin Schuh | ea83443 | 2024-02-20 21:23:13 -0800 | [diff] [blame] | 365 | template <class ZeroingEstimator, class Profile> |
| 366 | void SingleDOFProfiledSubsystem<ZeroingEstimator, Profile>::ForceGoal( |
Austin Schuh | 482a914 | 2022-02-23 16:54:39 -0800 | [diff] [blame] | 367 | double goal, double goal_velocity) { |
| 368 | set_unprofiled_goal(goal, goal_velocity, false); |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 369 | this->loop_->mutable_R() = this->unprofiled_goal_; |
| 370 | this->loop_->mutable_next_R() = this->loop_->R(); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 371 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 372 | const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R(); |
| 373 | this->profile_.MoveCurrentState(R.block<2, 1>(0, 0)); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Austin Schuh | ea83443 | 2024-02-20 21:23:13 -0800 | [diff] [blame] | 376 | template <class ZeroingEstimator, class Profile> |
| 377 | void SingleDOFProfiledSubsystem<ZeroingEstimator, Profile>::set_unprofiled_goal( |
Sabina Davis | 0e484f9 | 2020-02-23 17:47:53 -0800 | [diff] [blame] | 378 | double unprofiled_goal, double unprofiled_goal_velocity, bool print) { |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 379 | this->unprofiled_goal_(0, 0) = unprofiled_goal; |
James Kuszmaul | 4fb2976 | 2020-02-20 19:37:41 -0800 | [diff] [blame] | 380 | this->unprofiled_goal_(1, 0) = unprofiled_goal_velocity; |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 381 | this->unprofiled_goal_(2, 0) = 0.0; |
Sabina Davis | 0e484f9 | 2020-02-23 17:47:53 -0800 | [diff] [blame] | 382 | CapGoal("unprofiled R", &this->unprofiled_goal_, print); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 383 | } |
| 384 | |
Austin Schuh | ea83443 | 2024-02-20 21:23:13 -0800 | [diff] [blame] | 385 | template <class ZeroingEstimator, class Profile> |
| 386 | double SingleDOFProfiledSubsystem<ZeroingEstimator, Profile>::UpdateController( |
Austin Schuh | 482a914 | 2022-02-23 16:54:39 -0800 | [diff] [blame] | 387 | bool disable) { |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 388 | // TODO(austin): What do we want to do with the profile on reset? Also, we |
| 389 | // should probably reset R, the offset, the profile, etc. |
| 390 | if (this->should_reset_) { |
| 391 | this->loop_->mutable_X_hat(0, 0) = Y_(0, 0); |
| 392 | this->loop_->mutable_X_hat(1, 0) = 0.0; |
| 393 | this->loop_->mutable_X_hat(2, 0) = 0.0; |
Austin Schuh | 2669ece | 2022-03-11 18:30:57 -0800 | [diff] [blame] | 394 | this->X_hat_.setZero(); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 395 | this->should_reset_ = false; |
| 396 | } |
| 397 | |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 398 | if (!disable) { |
James Kuszmaul | 4fb2976 | 2020-02-20 19:37:41 -0800 | [diff] [blame] | 399 | if (enable_profile_) { |
| 400 | ::Eigen::Matrix<double, 2, 1> goal_state = profile_.Update( |
| 401 | this->unprofiled_goal_(0, 0), this->unprofiled_goal_(1, 0)); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 402 | |
James Kuszmaul | 4fb2976 | 2020-02-20 19:37:41 -0800 | [diff] [blame] | 403 | this->loop_->mutable_next_R(0, 0) = goal_state(0, 0); |
| 404 | this->loop_->mutable_next_R(1, 0) = goal_state(1, 0); |
| 405 | this->loop_->mutable_next_R(2, 0) = 0.0; |
| 406 | } else { |
| 407 | this->loop_->mutable_R() = this->unprofiled_goal_; |
| 408 | this->loop_->mutable_next_R() = this->unprofiled_goal_; |
| 409 | this->loop_->mutable_next_R(0, 0) += |
| 410 | this->unprofiled_goal_(1) * |
| 411 | aos::time::DurationInSeconds(this->loop_->plant().coefficients().dt); |
| 412 | CapGoal("R", &this->loop_->mutable_R()); |
| 413 | } |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 414 | CapGoal("next R", &this->loop_->mutable_next_R()); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Austin Schuh | 482a914 | 2022-02-23 16:54:39 -0800 | [diff] [blame] | 417 | this->loop_->UpdateController(disable); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 418 | |
Adam Snaider | 79900c2 | 2017-02-08 20:23:15 -0800 | [diff] [blame] | 419 | if (!disable && this->loop_->U(0, 0) != this->loop_->U_uncapped(0, 0)) { |
| 420 | const ::Eigen::Matrix<double, 3, 1> &R = this->loop_->R(); |
| 421 | profile_.MoveCurrentState(R.block<2, 1>(0, 0)); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 422 | } |
Austin Schuh | 482a914 | 2022-02-23 16:54:39 -0800 | [diff] [blame] | 423 | |
| 424 | return this->loop_->U(0, 0); |
| 425 | } |
| 426 | |
Austin Schuh | ea83443 | 2024-02-20 21:23:13 -0800 | [diff] [blame] | 427 | template <class ZeroingEstimator, class Profile> |
| 428 | void SingleDOFProfiledSubsystem<ZeroingEstimator, Profile>::UpdateObserver( |
Austin Schuh | 482a914 | 2022-02-23 16:54:39 -0800 | [diff] [blame] | 429 | double voltage) { |
| 430 | this->loop_->mutable_U(0, 0) = voltage; |
| 431 | this->loop_->UpdateObserver(this->loop_->U(), this->loop_->plant().dt()); |
| 432 | } |
| 433 | |
Austin Schuh | ea83443 | 2024-02-20 21:23:13 -0800 | [diff] [blame] | 434 | template <class ZeroingEstimator, class Profile> |
| 435 | double SingleDOFProfiledSubsystem<ZeroingEstimator, Profile>::Update( |
| 436 | bool disable) { |
Austin Schuh | 482a914 | 2022-02-23 16:54:39 -0800 | [diff] [blame] | 437 | const double voltage = UpdateController(disable); |
| 438 | UpdateObserver(voltage); |
| 439 | return voltage; |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Austin Schuh | ea83443 | 2024-02-20 21:23:13 -0800 | [diff] [blame] | 442 | template <class ZeroingEstimator, class Profile> |
| 443 | bool SingleDOFProfiledSubsystem<ZeroingEstimator, Profile>::CheckHardLimits() { |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 444 | // Returns whether hard limits have been exceeded. |
| 445 | |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 446 | if (position() > range_.upper_hard || position() < range_.lower_hard) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 447 | AOS_LOG( |
| 448 | ERROR, |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 449 | "SingleDOFProfiledSubsystem at %f out of bounds [%f, %f], ESTOPing\n", |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 450 | position(), range_.lower_hard, range_.upper_hard); |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 451 | return true; |
| 452 | } |
| 453 | |
| 454 | return false; |
| 455 | } |
| 456 | |
Austin Schuh | ea83443 | 2024-02-20 21:23:13 -0800 | [diff] [blame] | 457 | template <class ZeroingEstimator, class Profile> |
| 458 | void SingleDOFProfiledSubsystem<ZeroingEstimator, Profile>::AdjustProfile( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 459 | const ::frc971::ProfileParameters *profile_parameters) { |
| 460 | AdjustProfile( |
| 461 | profile_parameters != nullptr ? profile_parameters->max_velocity() : 0.0, |
| 462 | profile_parameters != nullptr ? profile_parameters->max_acceleration() |
| 463 | : 0.0); |
Austin Schuh | 3634ed3 | 2017-02-05 16:28:49 -0800 | [diff] [blame] | 464 | } |
| 465 | |
Austin Schuh | ea83443 | 2024-02-20 21:23:13 -0800 | [diff] [blame] | 466 | template <class ZeroingEstimator, class Profile> |
| 467 | void SingleDOFProfiledSubsystem<ZeroingEstimator, Profile>::AdjustProfile( |
Brian Silverman | ab0b677 | 2017-02-05 16:16:21 -0800 | [diff] [blame] | 468 | double max_angular_velocity, double max_angular_acceleration) { |
| 469 | profile_.set_maximum_velocity( |
| 470 | internal::UseUnlessZero(max_angular_velocity, default_velocity_)); |
| 471 | profile_.set_maximum_acceleration( |
| 472 | internal::UseUnlessZero(max_angular_acceleration, default_acceleration_)); |
| 473 | } |
| 474 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 475 | } // namespace frc971::control_loops |
Austin Schuh | 473a565 | 2017-02-05 01:30:42 -0800 | [diff] [blame] | 476 | |
| 477 | #endif // FRC971_CONTROL_LOOPS_PROFILED_SUBSYSTEM_H_ |