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 | |
| 4 | #include "aos/containers/ring_buffer.h" |
| 5 | #include "aos/events/event_loop.h" |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame^] | 6 | #include "aos/network/message_bridge_server_generated.h" |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 7 | #include "frc971/control_loops/drivetrain/hybrid_ekf.h" |
| 8 | #include "frc971/control_loops/drivetrain/localizer.h" |
| 9 | #include "y2020/control_loops/superstructure/superstructure_status_generated.h" |
| 10 | #include "y2020/vision/sift/sift_generated.h" |
| 11 | |
| 12 | namespace y2020 { |
| 13 | namespace control_loops { |
| 14 | namespace drivetrain { |
| 15 | |
| 16 | // This class handles the localization for the 2020 robot. In order to handle |
| 17 | // camera updates, we get the ImageMatchResult message from the cameras and then |
| 18 | // project the result onto the 2-D X/Y plane and use the implied robot |
| 19 | // position/heading from that as the measurement. This is distinct from 2019, |
| 20 | // when we used a heading/distance/skew measurement update. This is because |
| 21 | // updating with x/y/theta directly seems to be better conditioned (even if it |
| 22 | // may not reflect the measurement noise quite as accurately). The poor |
| 23 | // conditioning seemed to work in 2019, but due to the addition of a couple of |
| 24 | // velocity offset states that allow us to use the accelerometer more |
| 25 | // effectively, things started to become unstable. |
| 26 | class Localizer : public frc971::control_loops::drivetrain::LocalizerInterface { |
| 27 | public: |
| 28 | typedef frc971::control_loops::TypedPose<double> Pose; |
| 29 | typedef frc971::control_loops::drivetrain::HybridEkf<double> HybridEkf; |
| 30 | typedef typename HybridEkf::State State; |
| 31 | typedef typename HybridEkf::StateIdx StateIdx; |
| 32 | typedef typename HybridEkf::StateSquare StateSquare; |
| 33 | typedef typename HybridEkf::Input Input; |
| 34 | typedef typename HybridEkf::Output Output; |
| 35 | Localizer(aos::EventLoop *event_loop, |
| 36 | const frc971::control_loops::drivetrain::DrivetrainConfig<double> |
| 37 | &dt_config); |
| 38 | State Xhat() const override { return ekf_.X_hat(); } |
| 39 | frc971::control_loops::drivetrain::TrivialTargetSelector *target_selector() |
| 40 | override { |
| 41 | return &target_selector_; |
| 42 | } |
| 43 | |
| 44 | void Update(const ::Eigen::Matrix<double, 2, 1> &U, |
| 45 | aos::monotonic_clock::time_point now, double left_encoder, |
| 46 | double right_encoder, double gyro_rate, |
| 47 | const Eigen::Vector3d &accel) override; |
| 48 | |
| 49 | void Reset(aos::monotonic_clock::time_point t, const State &state) { |
| 50 | ekf_.ResetInitialState(t, state, ekf_.P()); |
| 51 | } |
| 52 | |
| 53 | void ResetPosition(aos::monotonic_clock::time_point t, double x, double y, |
| 54 | double theta, double /*theta_override*/, |
| 55 | bool /*reset_theta*/) override { |
| 56 | const double left_encoder = ekf_.X_hat(StateIdx::kLeftEncoder); |
| 57 | const double right_encoder = ekf_.X_hat(StateIdx::kRightEncoder); |
| 58 | ekf_.ResetInitialState(t, (Ekf::State() << x, y, theta, left_encoder, 0, |
| 59 | right_encoder, 0, 0, 0, 0, 0, 0).finished(), |
| 60 | ekf_.P()); |
| 61 | }; |
| 62 | |
| 63 | private: |
| 64 | // Storage for a single turret position data point. |
| 65 | struct TurretData { |
| 66 | aos::monotonic_clock::time_point receive_time = |
| 67 | aos::monotonic_clock::min_time; |
| 68 | double position = 0.0; // rad |
| 69 | double velocity = 0.0; // rad/sec |
| 70 | }; |
| 71 | |
| 72 | // Processes new image data and updates the EKF. |
| 73 | void HandleImageMatch(const frc971::vision::sift::ImageMatchResult &result); |
| 74 | |
| 75 | // Processes the most recent turret position and stores it in the turret_data_ |
| 76 | // buffer. |
| 77 | void HandleSuperstructureStatus( |
| 78 | const y2020::control_loops::superstructure::Status &status); |
| 79 | |
| 80 | // Retrieves the turret data closest to the provided time. |
| 81 | TurretData GetTurretDataForTime(aos::monotonic_clock::time_point time); |
| 82 | |
| 83 | aos::EventLoop *const event_loop_; |
| 84 | const frc971::control_loops::drivetrain::DrivetrainConfig<double> dt_config_; |
| 85 | HybridEkf ekf_; |
| 86 | |
| 87 | std::vector<aos::Fetcher<frc971::vision::sift::ImageMatchResult>> |
| 88 | image_fetchers_; |
| 89 | |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame^] | 90 | aos::Fetcher<aos::message_bridge::ServerStatistics> clock_offset_fetcher_; |
| 91 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 92 | // Buffer of recent turret data--this is used so that when we receive a camera |
| 93 | // frame from the turret, we can back out what the turret angle was at that |
| 94 | // time. |
| 95 | aos::RingBuffer<TurretData, 200> turret_data_; |
| 96 | |
| 97 | // Target selector to allow us to satisfy the LocalizerInterface requirements. |
| 98 | frc971::control_loops::drivetrain::TrivialTargetSelector target_selector_; |
| 99 | }; |
| 100 | |
| 101 | } // namespace drivetrain |
| 102 | } // namespace control_loops |
| 103 | } // namespace y2020 |
| 104 | |
| 105 | #endif // Y2020_CONTROL_LOOPS_DRIVETRAIN_LOCALIZER_H_ |