blob: 77b29ebc42779bbf32177d6e9151e36c2daf1e64 [file] [log] [blame]
James Kuszmaul1798c072022-02-13 15:32:11 -08001#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"
James Kuszmaul2971b5a2023-01-29 15:49:32 -08007#include "aos/network/message_bridge_server_generated.h"
James Kuszmaul1798c072022-02-13 15:32:11 -08008#include "frc971/control_loops/drivetrain/hybrid_ekf.h"
9#include "frc971/control_loops/drivetrain/localizer.h"
James Kuszmaulc76c19b2022-03-16 22:41:44 -070010#include "frc971/input/joystick_state_generated.h"
James Kuszmaul2971b5a2023-01-29 15:49:32 -080011#include "y2022/localizer/localizer_output_generated.h"
James Kuszmaul1798c072022-02-13 15:32:11 -080012
13namespace y2022 {
14namespace control_loops {
15namespace drivetrain {
16
17// This class handles the localization for the 2022 robot. Rather than actually
18// doing any work on the roborio, we farm all the localization out to a
19// raspberry pi and it then sends out LocalizerOutput messages that we treat as
James Kuszmaul51fa1ae2022-02-26 00:49:57 -080020// measurement updates. See //y2022/localizer.
James Kuszmaul1798c072022-02-13 15:32:11 -080021// TODO(james): Needs tests. Should refactor out some of the code from the 2020
22// localizer test.
23class Localizer : public frc971::control_loops::drivetrain::LocalizerInterface {
24 public:
25 typedef frc971::control_loops::TypedPose<float> Pose;
26 typedef frc971::control_loops::drivetrain::HybridEkf<float> HybridEkf;
27 typedef typename HybridEkf::State State;
28 typedef typename HybridEkf::StateIdx StateIdx;
29 typedef typename HybridEkf::StateSquare StateSquare;
30 typedef typename HybridEkf::Input Input;
31 typedef typename HybridEkf::Output Output;
32 Localizer(aos::EventLoop *event_loop,
33 const frc971::control_loops::drivetrain::DrivetrainConfig<double>
34 &dt_config);
35 frc971::control_loops::drivetrain::HybridEkf<double>::State Xhat()
36 const override {
37 return ekf_.X_hat().cast<double>();
38 }
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,
50 const frc971::control_loops::drivetrain::HybridEkf<double>::State
51 &state) override;
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,
59 (HybridEkf::State() << x, y, theta, left_encoder, 0,
60 right_encoder, 0, 0, 0, 0, 0, 0)
61 .finished(),
62 ekf_.P());
63 }
64
65 private:
James Kuszmaul2971b5a2023-01-29 15:49:32 -080066 class Corrector : public HybridEkf::ExpectedObservationFunctor {
67 public:
68 Corrector(const State &state_at_capture, const Eigen::Vector3f &Z)
69 : state_at_capture_(state_at_capture), Z_(Z) {
70 H_.setZero();
71 H_(0, StateIdx::kX) = 1;
72 H_(1, StateIdx::kY) = 1;
73 H_(2, StateIdx::kTheta) = 1;
74 }
75 Output H(const State &, const Input &) final {
76 Eigen::Vector3f error = H_ * state_at_capture_ - Z_;
77 error(2) = aos::math::NormalizeAngle(error(2));
78 return error;
79 }
80 Eigen::Matrix<float, HybridEkf::kNOutputs, HybridEkf::kNStates> DHDX(
81 const State &) final {
82 return H_;
83 }
84
85 private:
86 Eigen::Matrix<float, HybridEkf::kNOutputs, HybridEkf::kNStates> H_;
87 State state_at_capture_;
88 Eigen::Vector3f Z_;
89 };
James Kuszmaul1798c072022-02-13 15:32:11 -080090 aos::EventLoop *const event_loop_;
91 const frc971::control_loops::drivetrain::DrivetrainConfig<double> dt_config_;
92 HybridEkf ekf_;
James Kuszmaul2971b5a2023-01-29 15:49:32 -080093 HybridEkf::ExpectedObservationAllocator<Corrector> observations_;
James Kuszmaul1798c072022-02-13 15:32:11 -080094
95 aos::Fetcher<frc971::controls::LocalizerOutput> localizer_output_fetcher_;
James Kuszmaulc76c19b2022-03-16 22:41:44 -070096 aos::Fetcher<aos::JoystickState> joystick_state_fetcher_;
James Kuszmaul1798c072022-02-13 15:32:11 -080097 aos::Fetcher<aos::message_bridge::ServerStatistics> clock_offset_fetcher_;
98
99 // Target selector to allow us to satisfy the LocalizerInterface requirements.
100 frc971::control_loops::drivetrain::TrivialTargetSelector target_selector_;
101};
102
103} // namespace drivetrain
104} // namespace control_loops
105} // namespace y2022
106
107#endif // Y2022_CONTROL_LOOPS_DRIVETRAIN_LOCALIZER_H_