James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 1 | #ifndef Y2020_CONTROL_LOOPS_DRIVETRAIN_LOCALIZER_H_ |
| 2 | #define Y2020_CONTROL_LOOPS_DRIVETRAIN_LOCALIZER_H_ |
| 3 | |
James Kuszmaul | c6723cf | 2020-03-01 14:45:59 -0800 | [diff] [blame] | 4 | #include <string_view> |
| 5 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 6 | #include "aos/containers/ring_buffer.h" |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 7 | #include "aos/containers/sized_array.h" |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 8 | #include "aos/events/event_loop.h" |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 9 | #include "aos/network/message_bridge_server_generated.h" |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 10 | #include "frc971/control_loops/drivetrain/hybrid_ekf.h" |
| 11 | #include "frc971/control_loops/drivetrain/localizer.h" |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 12 | #include "y2020/control_loops/drivetrain/localizer_debug_generated.h" |
James Kuszmaul | 2971b5a | 2023-01-29 15:49:32 -0800 | [diff] [blame] | 13 | #include "y2020/control_loops/superstructure/superstructure_status_generated.h" |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 14 | #include "y2020/vision/sift/sift_generated.h" |
| 15 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame^] | 16 | namespace y2020::control_loops::drivetrain { |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 17 | |
| 18 | // This class handles the localization for the 2020 robot. In order to handle |
| 19 | // camera updates, we get the ImageMatchResult message from the cameras and then |
| 20 | // project the result onto the 2-D X/Y plane and use the implied robot |
| 21 | // position/heading from that as the measurement. This is distinct from 2019, |
| 22 | // when we used a heading/distance/skew measurement update. This is because |
| 23 | // updating with x/y/theta directly seems to be better conditioned (even if it |
| 24 | // may not reflect the measurement noise quite as accurately). The poor |
| 25 | // conditioning seemed to work in 2019, but due to the addition of a couple of |
| 26 | // velocity offset states that allow us to use the accelerometer more |
| 27 | // effectively, things started to become unstable. |
| 28 | class Localizer : public frc971::control_loops::drivetrain::LocalizerInterface { |
| 29 | public: |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 30 | typedef frc971::control_loops::TypedPose<float> Pose; |
| 31 | typedef frc971::control_loops::drivetrain::HybridEkf<float> HybridEkf; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 32 | typedef typename HybridEkf::State State; |
| 33 | typedef typename HybridEkf::StateIdx StateIdx; |
| 34 | typedef typename HybridEkf::StateSquare StateSquare; |
| 35 | typedef typename HybridEkf::Input Input; |
| 36 | typedef typename HybridEkf::Output Output; |
| 37 | Localizer(aos::EventLoop *event_loop, |
| 38 | const frc971::control_loops::drivetrain::DrivetrainConfig<double> |
| 39 | &dt_config); |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 40 | frc971::control_loops::drivetrain::HybridEkf<double>::State Xhat() |
| 41 | const override { |
| 42 | return ekf_.X_hat().cast<double>(); |
| 43 | } |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 44 | frc971::control_loops::drivetrain::TrivialTargetSelector *target_selector() |
| 45 | override { |
| 46 | return &target_selector_; |
| 47 | } |
| 48 | |
| 49 | void Update(const ::Eigen::Matrix<double, 2, 1> &U, |
| 50 | aos::monotonic_clock::time_point now, double left_encoder, |
| 51 | double right_encoder, double gyro_rate, |
| 52 | const Eigen::Vector3d &accel) override; |
| 53 | |
James Kuszmaul | bcd96fc | 2020-10-12 20:29:32 -0700 | [diff] [blame] | 54 | void Reset(aos::monotonic_clock::time_point t, |
| 55 | const frc971::control_loops::drivetrain::HybridEkf<double>::State |
| 56 | &state) override; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 57 | |
| 58 | void ResetPosition(aos::monotonic_clock::time_point t, double x, double y, |
| 59 | double theta, double /*theta_override*/, |
| 60 | bool /*reset_theta*/) override { |
| 61 | const double left_encoder = ekf_.X_hat(StateIdx::kLeftEncoder); |
| 62 | const double right_encoder = ekf_.X_hat(StateIdx::kRightEncoder); |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 63 | ekf_.ResetInitialState(t, |
| 64 | (HybridEkf::State() << x, y, theta, left_encoder, 0, |
| 65 | right_encoder, 0, 0, 0, 0, 0, 0) |
| 66 | .finished(), |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 67 | ekf_.P()); |
James Kuszmaul | 9c12812 | 2021-03-22 22:24:36 -0700 | [diff] [blame] | 68 | } |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 69 | |
| 70 | private: |
| 71 | // Storage for a single turret position data point. |
| 72 | struct TurretData { |
| 73 | aos::monotonic_clock::time_point receive_time = |
| 74 | aos::monotonic_clock::min_time; |
| 75 | double position = 0.0; // rad |
| 76 | double velocity = 0.0; // rad/sec |
| 77 | }; |
| 78 | |
James Kuszmaul | f75ecd6 | 2021-10-23 14:33:46 -0700 | [diff] [blame] | 79 | static constexpr size_t kNumRejectionReasons = |
| 80 | static_cast<int>(RejectionReason::MAX) - |
| 81 | static_cast<int>(RejectionReason::MIN) + 1; |
| 82 | |
| 83 | struct Statistics { |
| 84 | int total_accepted = 0; |
| 85 | int total_candidates = 0; |
| 86 | static_assert(0 == static_cast<int>(RejectionReason::MIN)); |
| 87 | static_assert( |
| 88 | kNumRejectionReasons == |
| 89 | sizeof( |
| 90 | std::invoke_result<decltype(EnumValuesRejectionReason)>::type) / |
| 91 | sizeof(RejectionReason), |
| 92 | "RejectionReason has non-contiguous error values."); |
| 93 | std::array<int, kNumRejectionReasons> rejection_counts; |
| 94 | }; |
| 95 | |
James Kuszmaul | 2971b5a | 2023-01-29 15:49:32 -0800 | [diff] [blame] | 96 | class Corrector : public HybridEkf::ExpectedObservationFunctor { |
| 97 | public: |
| 98 | Corrector(const Eigen::Matrix<float, 4, 4> &H_field_target, |
| 99 | const Pose &pose_robot_target, const State &state_at_capture, |
| 100 | const Eigen::Vector3f &Z, |
| 101 | std::optional<RejectionReason> *correction_rejection) |
| 102 | : H_field_target_(H_field_target), |
| 103 | pose_robot_target_(pose_robot_target), |
| 104 | state_at_capture_(state_at_capture), |
| 105 | Z_(Z), |
| 106 | correction_rejection_(correction_rejection) { |
| 107 | H_.setZero(); |
| 108 | H_(0, StateIdx::kX) = 1; |
| 109 | H_(1, StateIdx::kY) = 1; |
| 110 | H_(2, StateIdx::kTheta) = 1; |
| 111 | } |
| 112 | Output H(const State &, const Input &) final; |
| 113 | Eigen::Matrix<float, HybridEkf::kNOutputs, HybridEkf::kNStates> DHDX( |
| 114 | const State &) final { |
| 115 | return H_; |
| 116 | } |
| 117 | |
| 118 | private: |
| 119 | Eigen::Matrix<float, HybridEkf::kNOutputs, HybridEkf::kNStates> H_; |
| 120 | const Eigen::Matrix<float, 4, 4> H_field_target_; |
| 121 | Pose pose_robot_target_; |
| 122 | const State state_at_capture_; |
| 123 | const Eigen::Vector3f &Z_; |
| 124 | std::optional<RejectionReason> *correction_rejection_; |
| 125 | }; |
| 126 | |
James Kuszmaul | c6723cf | 2020-03-01 14:45:59 -0800 | [diff] [blame] | 127 | // Processes new image data from the given pi and updates the EKF. |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 128 | aos::SizedArray<flatbuffers::Offset<ImageMatchDebug>, 5> HandleImageMatch( |
| 129 | size_t camera_index, std::string_view pi, |
| 130 | const frc971::vision::sift::ImageMatchResult &result, |
| 131 | aos::monotonic_clock::time_point now, |
| 132 | flatbuffers::FlatBufferBuilder *fbb); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 133 | |
| 134 | // Processes the most recent turret position and stores it in the turret_data_ |
| 135 | // buffer. |
| 136 | void HandleSuperstructureStatus( |
| 137 | const y2020::control_loops::superstructure::Status &status); |
| 138 | |
| 139 | // Retrieves the turret data closest to the provided time. |
| 140 | TurretData GetTurretDataForTime(aos::monotonic_clock::time_point time); |
| 141 | |
| 142 | aos::EventLoop *const event_loop_; |
| 143 | const frc971::control_loops::drivetrain::DrivetrainConfig<double> dt_config_; |
| 144 | HybridEkf ekf_; |
James Kuszmaul | 2971b5a | 2023-01-29 15:49:32 -0800 | [diff] [blame] | 145 | HybridEkf::ExpectedObservationAllocator<Corrector> observations_; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 146 | |
| 147 | std::vector<aos::Fetcher<frc971::vision::sift::ImageMatchResult>> |
| 148 | image_fetchers_; |
| 149 | |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 150 | aos::Fetcher<aos::message_bridge::ServerStatistics> clock_offset_fetcher_; |
| 151 | |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 152 | aos::Sender<y2020::control_loops::drivetrain::LocalizerDebug> debug_sender_; |
| 153 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 154 | // Buffer of recent turret data--this is used so that when we receive a camera |
| 155 | // frame from the turret, we can back out what the turret angle was at that |
| 156 | // time. |
| 157 | aos::RingBuffer<TurretData, 200> turret_data_; |
| 158 | |
| 159 | // Target selector to allow us to satisfy the LocalizerInterface requirements. |
| 160 | frc971::control_loops::drivetrain::TrivialTargetSelector target_selector_; |
James Kuszmaul | f75ecd6 | 2021-10-23 14:33:46 -0700 | [diff] [blame] | 161 | |
| 162 | Statistics statistics_; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 163 | }; |
| 164 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame^] | 165 | } // namespace y2020::control_loops::drivetrain |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 166 | |
| 167 | #endif // Y2020_CONTROL_LOOPS_DRIVETRAIN_LOCALIZER_H_ |