James Kuszmaul | 1798c07 | 2022-02-13 15:32:11 -0800 | [diff] [blame] | 1 | #ifndef Y2022_CONTROL_LOOPS_DRIVETRAIN_LOCALIZER_H_ |
| 2 | #define Y2022_CONTROL_LOOPS_DRIVETRAIN_LOCALIZER_H_ |
| 3 | |
| 4 | #include <string_view> |
| 5 | |
| 6 | #include "aos/events/event_loop.h" |
| 7 | #include "frc971/control_loops/drivetrain/hybrid_ekf.h" |
| 8 | #include "frc971/control_loops/drivetrain/localizer.h" |
James Kuszmaul | 51fa1ae | 2022-02-26 00:49:57 -0800 | [diff] [blame] | 9 | #include "y2022/localizer/localizer_output_generated.h" |
James Kuszmaul | 1798c07 | 2022-02-13 15:32:11 -0800 | [diff] [blame] | 10 | #include "aos/network/message_bridge_server_generated.h" |
| 11 | |
| 12 | namespace y2022 { |
| 13 | namespace control_loops { |
| 14 | namespace drivetrain { |
| 15 | |
| 16 | // This class handles the localization for the 2022 robot. Rather than actually |
| 17 | // doing any work on the roborio, we farm all the localization out to a |
| 18 | // raspberry pi and it then sends out LocalizerOutput messages that we treat as |
James Kuszmaul | 51fa1ae | 2022-02-26 00:49:57 -0800 | [diff] [blame] | 19 | // measurement updates. See //y2022/localizer. |
James Kuszmaul | 1798c07 | 2022-02-13 15:32:11 -0800 | [diff] [blame] | 20 | // TODO(james): Needs tests. Should refactor out some of the code from the 2020 |
| 21 | // localizer test. |
| 22 | class Localizer : public frc971::control_loops::drivetrain::LocalizerInterface { |
| 23 | public: |
| 24 | typedef frc971::control_loops::TypedPose<float> Pose; |
| 25 | typedef frc971::control_loops::drivetrain::HybridEkf<float> HybridEkf; |
| 26 | typedef typename HybridEkf::State State; |
| 27 | typedef typename HybridEkf::StateIdx StateIdx; |
| 28 | typedef typename HybridEkf::StateSquare StateSquare; |
| 29 | typedef typename HybridEkf::Input Input; |
| 30 | typedef typename HybridEkf::Output Output; |
| 31 | Localizer(aos::EventLoop *event_loop, |
| 32 | const frc971::control_loops::drivetrain::DrivetrainConfig<double> |
| 33 | &dt_config); |
| 34 | frc971::control_loops::drivetrain::HybridEkf<double>::State Xhat() |
| 35 | const override { |
| 36 | return ekf_.X_hat().cast<double>(); |
| 37 | } |
| 38 | frc971::control_loops::drivetrain::TrivialTargetSelector *target_selector() |
| 39 | override { |
| 40 | return &target_selector_; |
| 41 | } |
| 42 | |
| 43 | void Update(const ::Eigen::Matrix<double, 2, 1> &U, |
| 44 | aos::monotonic_clock::time_point now, double left_encoder, |
| 45 | double right_encoder, double gyro_rate, |
| 46 | const Eigen::Vector3d &accel) override; |
| 47 | |
| 48 | void Reset(aos::monotonic_clock::time_point t, |
| 49 | const frc971::control_loops::drivetrain::HybridEkf<double>::State |
| 50 | &state) override; |
| 51 | |
| 52 | void ResetPosition(aos::monotonic_clock::time_point t, double x, double y, |
| 53 | double theta, double /*theta_override*/, |
| 54 | bool /*reset_theta*/) override { |
| 55 | const double left_encoder = ekf_.X_hat(StateIdx::kLeftEncoder); |
| 56 | const double right_encoder = ekf_.X_hat(StateIdx::kRightEncoder); |
| 57 | ekf_.ResetInitialState(t, |
| 58 | (HybridEkf::State() << x, y, theta, left_encoder, 0, |
| 59 | right_encoder, 0, 0, 0, 0, 0, 0) |
| 60 | .finished(), |
| 61 | ekf_.P()); |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | aos::EventLoop *const event_loop_; |
| 66 | const frc971::control_loops::drivetrain::DrivetrainConfig<double> dt_config_; |
| 67 | HybridEkf ekf_; |
| 68 | |
| 69 | aos::Fetcher<frc971::controls::LocalizerOutput> localizer_output_fetcher_; |
| 70 | aos::Fetcher<aos::message_bridge::ServerStatistics> clock_offset_fetcher_; |
| 71 | |
| 72 | // Target selector to allow us to satisfy the LocalizerInterface requirements. |
| 73 | frc971::control_loops::drivetrain::TrivialTargetSelector target_selector_; |
| 74 | }; |
| 75 | |
| 76 | } // namespace drivetrain |
| 77 | } // namespace control_loops |
| 78 | } // namespace y2022 |
| 79 | |
| 80 | #endif // Y2022_CONTROL_LOOPS_DRIVETRAIN_LOCALIZER_H_ |