Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 1 | #ifndef Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_ARM_H_ |
| 2 | #define Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_ARM_H_ |
| 3 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 4 | #include "aos/time/time.h" |
Maxwell Henderson | 8f0e07f | 2023-02-08 21:10:58 -0800 | [diff] [blame] | 5 | #include "frc971/control_loops/double_jointed_arm/dynamics.h" |
| 6 | #include "frc971/control_loops/double_jointed_arm/ekf.h" |
Maxwell Henderson | 8f0e07f | 2023-02-08 21:10:58 -0800 | [diff] [blame] | 7 | #include "frc971/control_loops/double_jointed_arm/graph.h" |
| 8 | #include "frc971/control_loops/double_jointed_arm/trajectory.h" |
James Kuszmaul | ec635d2 | 2023-08-12 18:39:24 -0700 | [diff] [blame^] | 9 | #include "frc971/zeroing/pot_and_absolute_encoder.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 10 | #include "frc971/zeroing/zeroing.h" |
| 11 | #include "y2018/constants.h" |
| 12 | #include "y2018/control_loops/superstructure/arm/generated_graph.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 13 | #include "y2018/control_loops/superstructure/superstructure_position_generated.h" |
| 14 | #include "y2018/control_loops/superstructure/superstructure_status_generated.h" |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 15 | |
Maxwell Henderson | 8f0e07f | 2023-02-08 21:10:58 -0800 | [diff] [blame] | 16 | using frc971::control_loops::arm::EKF; |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 17 | using frc971::control_loops::arm::TrajectoryFollower; |
Maxwell Henderson | 8f0e07f | 2023-02-08 21:10:58 -0800 | [diff] [blame] | 18 | |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 19 | namespace y2018 { |
| 20 | namespace control_loops { |
| 21 | namespace superstructure { |
| 22 | namespace arm { |
| 23 | |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 24 | class Arm { |
| 25 | public: |
| 26 | Arm(); |
| 27 | |
Austin Schuh | 7a09040 | 2018-03-04 01:16:45 -0800 | [diff] [blame] | 28 | // If true, tune down all the constants for testing. |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 29 | static constexpr bool kGrannyMode() { return false; } |
Austin Schuh | 7a09040 | 2018-03-04 01:16:45 -0800 | [diff] [blame] | 30 | |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 31 | // The operating voltage. |
Austin Schuh | 7a09040 | 2018-03-04 01:16:45 -0800 | [diff] [blame] | 32 | static constexpr double kOperatingVoltage() { |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame] | 33 | return kGrannyMode() ? 5.0 : 12.0; |
Austin Schuh | 7a09040 | 2018-03-04 01:16:45 -0800 | [diff] [blame] | 34 | } |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 35 | static constexpr double kDt() { return 0.00505; } |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame] | 36 | static constexpr double kAlpha0Max() { return kGrannyMode() ? 5.0 : 15.0; } |
| 37 | static constexpr double kAlpha1Max() { return kGrannyMode() ? 5.0 : 15.0; } |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 38 | |
| 39 | static constexpr double kVMax() { return kGrannyMode() ? 5.0 : 11.5; } |
| 40 | static constexpr double kPathlessVMax() { return 5.0; } |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame] | 41 | static constexpr double kGotoPathVMax() { return 6.0; } |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 42 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 43 | flatbuffers::Offset<superstructure::ArmStatus> Iterate( |
| 44 | const ::aos::monotonic_clock::time_point monotonic_now, |
| 45 | const uint32_t *unsafe_goal, bool grab_box, bool open_claw, |
| 46 | bool close_claw, const superstructure::ArmPosition *position, |
| 47 | const bool claw_beambreak_triggered, |
| 48 | const bool box_back_beambreak_triggered, const bool intake_clear_of_box, |
| 49 | bool suicide, bool trajectory_override, double *proximal_output, |
| 50 | double *distal_output, bool *release_arm_brake, bool *claw_closed, |
| 51 | flatbuffers::FlatBufferBuilder *fbb); |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 52 | |
| 53 | void Reset(); |
| 54 | |
| 55 | enum class State : int32_t { |
| 56 | UNINITIALIZED, |
| 57 | ZEROING, |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 58 | DISABLED, |
| 59 | GOTO_PATH, |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 60 | RUNNING, |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 61 | PREP_CLIMB, |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 62 | ESTOP, |
| 63 | }; |
| 64 | |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 65 | enum class GrabState : int32_t { |
Austin Schuh | ede4732 | 2018-07-08 16:04:36 -0700 | [diff] [blame] | 66 | NORMAL = 0, |
| 67 | WAIT_FOR_BOX = 1, |
| 68 | TALL_BOX = 2, |
| 69 | SHORT_BOX = 3, |
| 70 | CLAW_CLOSE = 4, |
| 71 | OPEN_INTAKE = 5, |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 74 | State state() const { return state_; } |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 75 | GrabState grab_state() const { return grab_state_; } |
| 76 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 77 | bool estopped() const { return state_ == State::ESTOP; } |
| 78 | bool zeroed() const { |
| 79 | return (proximal_zeroing_estimator_.zeroed() && |
| 80 | distal_zeroing_estimator_.zeroed()); |
| 81 | } |
| 82 | |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 83 | // Returns the maximum position for the intake. This is used to override the |
| 84 | // intake position to release the box when the state machine is lifting. |
| 85 | double max_intake_override() const { return max_intake_override_; } |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 86 | |
Austin Schuh | d76546a | 2018-07-08 16:05:14 -0700 | [diff] [blame] | 87 | uint32_t current_node() const { return current_node_; } |
| 88 | |
| 89 | double path_distance_to_go() { return follower_.path_distance_to_go(); } |
| 90 | |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 91 | private: |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 92 | bool AtState(uint32_t state) const { return current_node_ == state; } |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 93 | bool NearEnd(double threshold = 0.03) const { |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 94 | return ::std::abs(arm_ekf_.X_hat(0) - follower_.theta(0)) <= threshold && |
| 95 | ::std::abs(arm_ekf_.X_hat(2) - follower_.theta(1)) <= threshold && |
| 96 | follower_.path_distance_to_go() < 1e-3; |
| 97 | } |
| 98 | |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 99 | State state_ = State::UNINITIALIZED; |
| 100 | |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 101 | GrabState grab_state_ = GrabState::NORMAL; |
| 102 | |
| 103 | ::aos::monotonic_clock::time_point claw_close_start_time_ = |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 104 | ::aos::monotonic_clock::min_time; |
| 105 | |
Austin Schuh | 72db9a1 | 2019-01-21 18:02:51 -0800 | [diff] [blame] | 106 | ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator |
| 107 | proximal_zeroing_estimator_; |
| 108 | ::frc971::zeroing::PotAndAbsoluteEncoderZeroingEstimator |
| 109 | distal_zeroing_estimator_; |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 110 | |
| 111 | double proximal_offset_ = 0.0; |
| 112 | double distal_offset_ = 0.0; |
| 113 | |
Austin Schuh | 9634153 | 2018-03-09 21:17:24 -0800 | [diff] [blame] | 114 | bool claw_closed_ = true; |
| 115 | double max_intake_override_ = 1000.0; |
| 116 | |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 117 | const ::Eigen::Matrix<double, 2, 2> alpha_unitizer_; |
| 118 | |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame] | 119 | double vmax_ = kVMax(); |
| 120 | |
Maxwell Henderson | 8f0e07f | 2023-02-08 21:10:58 -0800 | [diff] [blame] | 121 | frc971::control_loops::arm::Dynamics dynamics_; |
| 122 | |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame] | 123 | ::std::vector<TrajectoryAndParams> trajectories_; |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 124 | SearchGraph search_graph_; |
| 125 | |
| 126 | bool close_enough_for_full_power_ = false; |
| 127 | |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 128 | int32_t climb_count_ = 0; |
Austin Schuh | 7afcc23 | 2018-09-16 16:33:47 -0700 | [diff] [blame] | 129 | int32_t brownout_count_ = 0; |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 130 | |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 131 | EKF arm_ekf_; |
| 132 | TrajectoryFollower follower_; |
| 133 | |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 134 | const ::std::vector<::Eigen::Matrix<double, 2, 1>> points_; |
| 135 | |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 136 | // Start at the 0th index. |
| 137 | uint32_t current_node_ = 0; |
| 138 | |
Neil Balch | ba9cbba | 2018-04-06 22:26:38 -0700 | [diff] [blame] | 139 | uint32_t claw_closed_count_ = 0; |
| 140 | |
Austin Schuh | cb09171 | 2018-02-21 20:01:55 -0800 | [diff] [blame] | 141 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW; |
| 142 | }; |
| 143 | |
| 144 | } // namespace arm |
| 145 | } // namespace superstructure |
| 146 | } // namespace control_loops |
| 147 | } // namespace y2018 |
| 148 | |
| 149 | #endif // Y2018_CONTROL_LOOPS_SUPERSTRUCTURE_ARM_ARM_H_ |