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