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