blob: 19ee4ad497b4f0549b85f433d5fb2110e3efaa40 [file] [log] [blame]
James Kuszmaul5398fae2020-02-17 16:44:03 -08001#ifndef Y2020_CONTROL_LOOPS_DRIVETRAIN_LOCALIZER_H_
2#define Y2020_CONTROL_LOOPS_DRIVETRAIN_LOCALIZER_H_
3
James Kuszmaulc6723cf2020-03-01 14:45:59 -08004#include <string_view>
5
James Kuszmaul5398fae2020-02-17 16:44:03 -08006#include "aos/containers/ring_buffer.h"
James Kuszmaul5ff8a862021-09-25 17:29:43 -07007#include "aos/containers/sized_array.h"
James Kuszmaul5398fae2020-02-17 16:44:03 -08008#include "aos/events/event_loop.h"
James Kuszmaul958b21e2020-02-26 21:51:40 -08009#include "aos/network/message_bridge_server_generated.h"
James Kuszmaul5398fae2020-02-17 16:44:03 -080010#include "frc971/control_loops/drivetrain/hybrid_ekf.h"
11#include "frc971/control_loops/drivetrain/localizer.h"
12#include "y2020/control_loops/superstructure/superstructure_status_generated.h"
James Kuszmaul5ff8a862021-09-25 17:29:43 -070013#include "y2020/control_loops/drivetrain/localizer_debug_generated.h"
James Kuszmaul5398fae2020-02-17 16:44:03 -080014#include "y2020/vision/sift/sift_generated.h"
15
16namespace y2020 {
17namespace control_loops {
18namespace drivetrain {
19
20// This class handles the localization for the 2020 robot. In order to handle
21// camera updates, we get the ImageMatchResult message from the cameras and then
22// project the result onto the 2-D X/Y plane and use the implied robot
23// position/heading from that as the measurement. This is distinct from 2019,
24// when we used a heading/distance/skew measurement update. This is because
25// updating with x/y/theta directly seems to be better conditioned (even if it
26// may not reflect the measurement noise quite as accurately). The poor
27// conditioning seemed to work in 2019, but due to the addition of a couple of
28// velocity offset states that allow us to use the accelerometer more
29// effectively, things started to become unstable.
30class Localizer : public frc971::control_loops::drivetrain::LocalizerInterface {
31 public:
James Kuszmauld478f872020-03-16 20:54:27 -070032 typedef frc971::control_loops::TypedPose<float> Pose;
33 typedef frc971::control_loops::drivetrain::HybridEkf<float> HybridEkf;
James Kuszmaul5398fae2020-02-17 16:44:03 -080034 typedef typename HybridEkf::State State;
35 typedef typename HybridEkf::StateIdx StateIdx;
36 typedef typename HybridEkf::StateSquare StateSquare;
37 typedef typename HybridEkf::Input Input;
38 typedef typename HybridEkf::Output Output;
39 Localizer(aos::EventLoop *event_loop,
40 const frc971::control_loops::drivetrain::DrivetrainConfig<double>
41 &dt_config);
James Kuszmauld478f872020-03-16 20:54:27 -070042 frc971::control_loops::drivetrain::HybridEkf<double>::State Xhat()
43 const override {
44 return ekf_.X_hat().cast<double>();
45 }
James Kuszmaul5398fae2020-02-17 16:44:03 -080046 frc971::control_loops::drivetrain::TrivialTargetSelector *target_selector()
47 override {
48 return &target_selector_;
49 }
50
51 void Update(const ::Eigen::Matrix<double, 2, 1> &U,
52 aos::monotonic_clock::time_point now, double left_encoder,
53 double right_encoder, double gyro_rate,
54 const Eigen::Vector3d &accel) override;
55
James Kuszmaulbcd96fc2020-10-12 20:29:32 -070056 void Reset(aos::monotonic_clock::time_point t,
57 const frc971::control_loops::drivetrain::HybridEkf<double>::State
58 &state) override;
James Kuszmaul5398fae2020-02-17 16:44:03 -080059
60 void ResetPosition(aos::monotonic_clock::time_point t, double x, double y,
61 double theta, double /*theta_override*/,
62 bool /*reset_theta*/) override {
63 const double left_encoder = ekf_.X_hat(StateIdx::kLeftEncoder);
64 const double right_encoder = ekf_.X_hat(StateIdx::kRightEncoder);
James Kuszmauld478f872020-03-16 20:54:27 -070065 ekf_.ResetInitialState(t,
66 (HybridEkf::State() << x, y, theta, left_encoder, 0,
67 right_encoder, 0, 0, 0, 0, 0, 0)
68 .finished(),
James Kuszmaul5398fae2020-02-17 16:44:03 -080069 ekf_.P());
James Kuszmaul9c128122021-03-22 22:24:36 -070070 }
James Kuszmaul5398fae2020-02-17 16:44:03 -080071
72 private:
73 // Storage for a single turret position data point.
74 struct TurretData {
75 aos::monotonic_clock::time_point receive_time =
76 aos::monotonic_clock::min_time;
77 double position = 0.0; // rad
78 double velocity = 0.0; // rad/sec
79 };
80
James Kuszmaulc6723cf2020-03-01 14:45:59 -080081 // Processes new image data from the given pi and updates the EKF.
James Kuszmaul5ff8a862021-09-25 17:29:43 -070082 aos::SizedArray<flatbuffers::Offset<ImageMatchDebug>, 5> HandleImageMatch(
83 size_t camera_index, std::string_view pi,
84 const frc971::vision::sift::ImageMatchResult &result,
85 aos::monotonic_clock::time_point now,
86 flatbuffers::FlatBufferBuilder *fbb);
James Kuszmaul5398fae2020-02-17 16:44:03 -080087
88 // Processes the most recent turret position and stores it in the turret_data_
89 // buffer.
90 void HandleSuperstructureStatus(
91 const y2020::control_loops::superstructure::Status &status);
92
93 // Retrieves the turret data closest to the provided time.
94 TurretData GetTurretDataForTime(aos::monotonic_clock::time_point time);
95
96 aos::EventLoop *const event_loop_;
97 const frc971::control_loops::drivetrain::DrivetrainConfig<double> dt_config_;
98 HybridEkf ekf_;
99
100 std::vector<aos::Fetcher<frc971::vision::sift::ImageMatchResult>>
101 image_fetchers_;
102
James Kuszmaul958b21e2020-02-26 21:51:40 -0800103 aos::Fetcher<aos::message_bridge::ServerStatistics> clock_offset_fetcher_;
104
James Kuszmaul5ff8a862021-09-25 17:29:43 -0700105 aos::Sender<y2020::control_loops::drivetrain::LocalizerDebug> debug_sender_;
106
James Kuszmaul5398fae2020-02-17 16:44:03 -0800107 // Buffer of recent turret data--this is used so that when we receive a camera
108 // frame from the turret, we can back out what the turret angle was at that
109 // time.
110 aos::RingBuffer<TurretData, 200> turret_data_;
111
112 // Target selector to allow us to satisfy the LocalizerInterface requirements.
113 frc971::control_loops::drivetrain::TrivialTargetSelector target_selector_;
114};
115
116} // namespace drivetrain
117} // namespace control_loops
118} // namespace y2020
119
120#endif // Y2020_CONTROL_LOOPS_DRIVETRAIN_LOCALIZER_H_