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