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