Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 1 | #ifndef Y2017_CONTROL_LOOPS_SUPERSTRUCTURE_COLUMN_COLUMN_H_ |
| 2 | #define Y2017_CONTROL_LOOPS_SUPERSTRUCTURE_COLUMN_COLUMN_H_ |
| 3 | |
| 4 | #include <array> |
| 5 | #include <chrono> |
| 6 | #include <memory> |
| 7 | #include <utility> |
| 8 | |
| 9 | #include "Eigen/Dense" |
| 10 | |
| 11 | #include "frc971/constants.h" |
| 12 | #include "frc971/control_loops/profiled_subsystem.h" |
| 13 | #include "frc971/control_loops/state_feedback_loop.h" |
| 14 | #include "y2017/constants.h" |
| 15 | #include "y2017/control_loops/superstructure/column/column_zeroing.h" |
| 16 | #include "y2017/control_loops/superstructure/intake/intake.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 17 | #include "y2017/control_loops/superstructure/superstructure_position_generated.h" |
| 18 | #include "y2017/control_loops/superstructure/superstructure_status_generated.h" |
Austin Schuh | ac76bb3 | 2017-03-22 22:34:26 -0700 | [diff] [blame] | 19 | #include "y2017/control_loops/superstructure/vision_time_adjuster.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 20 | #include "y2017/vision/vision_generated.h" |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 21 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 22 | namespace y2017::control_loops::superstructure::column { |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 23 | |
| 24 | class ColumnProfiledSubsystem |
| 25 | : public ::frc971::control_loops::ProfiledSubsystem< |
| 26 | 6, 1, ColumnZeroingEstimator, 2, 2> { |
| 27 | public: |
| 28 | ColumnProfiledSubsystem( |
| 29 | ::std::unique_ptr< |
| 30 | ::frc971::control_loops::SimpleCappedStateFeedbackLoop<6, 2, 2>> |
| 31 | loop, |
| 32 | const ::y2017::constants::Values::Column &zeroing_constants, |
| 33 | const ::frc971::constants::Range &range, double default_angular_velocity, |
| 34 | double default_angular_acceleration); |
| 35 | |
| 36 | // Updates our estimator with the latest position. |
| 37 | void Correct(const ColumnPosition &position); |
| 38 | // Runs the controller and profile generator for a cycle. |
| 39 | void Update(bool disabled); |
| 40 | |
| 41 | // Fills out the ProfiledJointStatus structure with the current state. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 42 | void PopulateTurretStatus( |
| 43 | TurretProfiledSubsystemStatus::Builder *status_builder, |
| 44 | flatbuffers::Offset<ColumnZeroingEstimator::State> |
| 45 | estimator_state_offset); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 46 | |
| 47 | // Forces the current goal to the provided goal, bypassing the profiler. |
| 48 | void ForceGoal(double goal_velocity, double goal); |
| 49 | // Sets the unprofiled goal. The profiler will generate a profile to go to |
| 50 | // this goal. |
| 51 | void set_indexer_unprofiled_goal(double goal_velocity); |
| 52 | void set_turret_unprofiled_goal(double unprofiled_goal); |
| 53 | void set_unprofiled_goal(double goal_velocity, double unprofiled_goal); |
| 54 | // Limits our profiles to a max velocity and acceleration for proper motion. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 55 | void AdjustProfile(const ::frc971::ProfileParameters *profile_parameters); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 56 | void AdjustProfile(double max_angular_velocity, |
| 57 | double max_angular_acceleration); |
| 58 | |
| 59 | // Returns true if we have exceeded any hard limits. |
| 60 | bool CheckHardLimits(); |
| 61 | |
| 62 | // Returns the requested voltage. |
| 63 | double indexer_voltage() const { return loop_->U(0, 0); } |
| 64 | double uncapped_indexer_voltage() const { return loop_->U_uncapped(0, 0); } |
| 65 | double turret_voltage() const { return loop_->U(1, 0); } |
| 66 | |
| 67 | // Returns the current Y. |
| 68 | const ::Eigen::Matrix<double, 2, 1> Y() const { return Y_; } |
| 69 | double Y(int i, int j) const { return Y_(i, j); } |
| 70 | |
| 71 | // Returns the current indexer position. |
| 72 | double indexer_position() const { return Y_(0, 0); } |
| 73 | |
| 74 | bool saturated() const { return saturated_; } |
| 75 | |
| 76 | // Returns the current turret position. |
| 77 | double turret_position() const { return Y_(1, 0) + Y_(0, 0); } |
| 78 | |
| 79 | // For testing: |
| 80 | // Triggers an estimator error. |
| 81 | void TriggerEstimatorError() { estimators_[0].TriggerError(); } |
| 82 | |
| 83 | const ::frc971::constants::Range &range() const { return range_; } |
| 84 | |
| 85 | bool IsIndexerStuck() const; |
| 86 | double IndexerStuckVoltage() const; |
| 87 | void PartialIndexerReset(); |
| 88 | void PartialTurretReset(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 89 | void PopulateIndexerStatus(IndexerStatus::Builder *status_builder); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 90 | |
| 91 | void AddOffset(double indexer_offset_delta, double turret_offset_delta); |
| 92 | |
| 93 | protected: |
| 94 | // Limits the provided goal to the soft limits. Prints "name" when it fails |
| 95 | // to aid debugging. |
| 96 | virtual void CapGoal(const char *name, Eigen::Matrix<double, 6, 1> *goal); |
| 97 | |
| 98 | private: |
| 99 | void UpdateOffset(double indexer_offset, double turret_offset); |
| 100 | |
| 101 | ::std::unique_ptr<StateFeedbackLoop<6, 2, 2>> stuck_indexer_detector_; |
| 102 | |
| 103 | // History array for calculating a filtered angular velocity. |
| 104 | static constexpr int kHistoryLength = 5; |
| 105 | ::std::array<double, kHistoryLength> indexer_history_; |
| 106 | ptrdiff_t indexer_history_position_ = 0; |
| 107 | |
| 108 | double indexer_error_ = 0.0; |
| 109 | double indexer_dt_velocity_ = 0.0; |
| 110 | double indexer_last_position_ = 0.0; |
| 111 | double indexer_average_angular_velocity_ = 0.0; |
| 112 | double indexer_position_error_ = 0.0; |
| 113 | bool indexer_ready_ = false; |
| 114 | |
| 115 | bool saturated_ = false; |
| 116 | |
| 117 | Eigen::Matrix<double, 6, 1> X_hat_current_; |
| 118 | Eigen::Matrix<double, 6, 1> stuck_indexer_X_hat_current_; |
| 119 | |
| 120 | aos::util::TrapezoidProfile profile_; |
| 121 | |
| 122 | // Current measurement. |
| 123 | Eigen::Matrix<double, 2, 1> Y_; |
| 124 | // Current offset. Y_ = offset_ + raw_sensor; |
| 125 | Eigen::Matrix<double, 2, 1> offset_; |
| 126 | |
| 127 | const ::frc971::constants::Range range_; |
| 128 | |
| 129 | const double default_velocity_; |
| 130 | const double default_acceleration_; |
| 131 | |
| 132 | double turret_last_position_ = 0; |
| 133 | }; |
| 134 | |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 135 | class Column { |
| 136 | public: |
Austin Schuh | b6c5c85 | 2019-05-19 20:13:31 -0700 | [diff] [blame] | 137 | Column(::aos::EventLoop *event_loop); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 138 | double goal(int row, int col) const { |
| 139 | return profiled_subsystem_.goal(row, col); |
| 140 | } |
| 141 | |
| 142 | double turret_position() const { |
| 143 | return profiled_subsystem_.turret_position(); |
| 144 | } |
| 145 | |
| 146 | void set_freeze(bool freeze) { freeze_ = freeze; } |
| 147 | |
| 148 | // The zeroing and operating voltages. |
| 149 | static constexpr double kZeroingVoltage = 5.0; |
| 150 | static constexpr double kOperatingVoltage = 12.0; |
| 151 | static constexpr double kIntakeZeroingMinDistance = 0.08; |
| 152 | static constexpr double kIntakeTolerance = 0.005; |
| 153 | static constexpr double kStuckZeroingTrackingError = 0.02; |
Austin Schuh | 3ae4743 | 2017-04-16 19:15:46 -0700 | [diff] [blame] | 154 | static constexpr double kTurretMin = -0.1; |
| 155 | static constexpr double kTurretMax = M_PI / 2.0 + 0.1; |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 156 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 157 | std::pair<flatbuffers::Offset<IndexerStatus>, |
| 158 | flatbuffers::Offset<TurretProfiledSubsystemStatus>> |
| 159 | Iterate(::aos::monotonic_clock::time_point monotonic_now, |
| 160 | const IndexerGoalT *unsafe_indexer_goal, |
| 161 | const TurretGoal *unsafe_turret_goal, const ColumnPosition *position, |
| 162 | const vision::VisionStatus *vision_status, double *indexer_output, |
| 163 | double *turret_output, flatbuffers::FlatBufferBuilder *fbb, |
| 164 | // IndexerStatus *indexer_status, |
| 165 | // TurretProfiledSubsystemStatus *turret_status, |
| 166 | intake::Intake *intake); |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 167 | |
| 168 | void Reset(); |
| 169 | |
| 170 | enum class State : int32_t { |
| 171 | UNINITIALIZED = 0, |
| 172 | ZEROING_UNINITIALIZED = 1, |
| 173 | ZEROING_POSITIVE = 2, |
| 174 | ZEROING_NEGATIVE = 3, |
| 175 | RUNNING = 4, |
| 176 | ESTOP = 5, |
| 177 | }; |
| 178 | |
| 179 | enum class IndexerState : int32_t { |
| 180 | // The system is running correctly, no stuck condition detected. |
| 181 | RUNNING = 0, |
| 182 | // The system is reversing to unjam. |
| 183 | REVERSING = 1 |
| 184 | }; |
| 185 | |
| 186 | State state() const { return state_; } |
| 187 | IndexerState indexer_state() const { return indexer_state_; } |
| 188 | |
| 189 | private: |
| 190 | State state_ = State::UNINITIALIZED; |
| 191 | |
| 192 | IndexerState indexer_state_ = IndexerState::RUNNING; |
| 193 | |
| 194 | bool freeze_ = false; |
| 195 | |
Austin Schuh | ac76bb3 | 2017-03-22 22:34:26 -0700 | [diff] [blame] | 196 | VisionTimeAdjuster vision_time_adjuster_; |
| 197 | |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 198 | // The last time that we transitioned from RUNNING to REVERSING or the |
| 199 | // reverse. Used to implement the timeouts. |
| 200 | ::aos::monotonic_clock::time_point last_transition_time_ = |
| 201 | ::aos::monotonic_clock::min_time; |
| 202 | |
| 203 | ColumnProfiledSubsystem profiled_subsystem_; |
Austin Schuh | 25db126 | 2017-04-05 19:39:55 -0700 | [diff] [blame] | 204 | |
| 205 | const double vision_error_; |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 206 | }; |
| 207 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 208 | } // namespace y2017::control_loops::superstructure::column |
Austin Schuh | d5ccb86 | 2017-03-11 22:06:36 -0800 | [diff] [blame] | 209 | |
| 210 | #endif // Y2017_CONTROL_LOOPS_SUPERSTRUCTURE_COLUMN_COLUMN_H_ |