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" |
| 7 | #include "aos/events/event_loop.h" |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 8 | #include "aos/network/message_bridge_server_generated.h" |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 9 | #include "frc971/control_loops/drivetrain/hybrid_ekf.h" |
| 10 | #include "frc971/control_loops/drivetrain/localizer.h" |
| 11 | #include "y2020/control_loops/superstructure/superstructure_status_generated.h" |
| 12 | #include "y2020/vision/sift/sift_generated.h" |
| 13 | |
| 14 | namespace y2020 { |
| 15 | namespace control_loops { |
| 16 | namespace drivetrain { |
| 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 | |
| 54 | void Reset(aos::monotonic_clock::time_point t, const State &state) { |
| 55 | ekf_.ResetInitialState(t, state, ekf_.P()); |
| 56 | } |
| 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()); |
| 68 | }; |
| 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 | c6723cf | 2020-03-01 14:45:59 -0800 | [diff] [blame] | 79 | // Processes new image data from the given pi and updates the EKF. |
| 80 | void HandleImageMatch(std::string_view pi, |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 81 | const frc971::vision::sift::ImageMatchResult &result, |
| 82 | aos::monotonic_clock::time_point now); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 83 | |
| 84 | // Processes the most recent turret position and stores it in the turret_data_ |
| 85 | // buffer. |
| 86 | void HandleSuperstructureStatus( |
| 87 | const y2020::control_loops::superstructure::Status &status); |
| 88 | |
| 89 | // Retrieves the turret data closest to the provided time. |
| 90 | TurretData GetTurretDataForTime(aos::monotonic_clock::time_point time); |
| 91 | |
| 92 | aos::EventLoop *const event_loop_; |
| 93 | const frc971::control_loops::drivetrain::DrivetrainConfig<double> dt_config_; |
| 94 | HybridEkf ekf_; |
| 95 | |
| 96 | std::vector<aos::Fetcher<frc971::vision::sift::ImageMatchResult>> |
| 97 | image_fetchers_; |
| 98 | |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 99 | aos::Fetcher<aos::message_bridge::ServerStatistics> clock_offset_fetcher_; |
| 100 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 101 | // Buffer of recent turret data--this is used so that when we receive a camera |
| 102 | // frame from the turret, we can back out what the turret angle was at that |
| 103 | // time. |
| 104 | aos::RingBuffer<TurretData, 200> turret_data_; |
| 105 | |
| 106 | // Target selector to allow us to satisfy the LocalizerInterface requirements. |
| 107 | frc971::control_loops::drivetrain::TrivialTargetSelector target_selector_; |
| 108 | }; |
| 109 | |
| 110 | } // namespace drivetrain |
| 111 | } // namespace control_loops |
| 112 | } // namespace y2020 |
| 113 | |
| 114 | #endif // Y2020_CONTROL_LOOPS_DRIVETRAIN_LOCALIZER_H_ |