blob: 1d6f8161b39b81ddd9d674d68e1683b0bcef2d08 [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"
7#include "aos/events/event_loop.h"
James Kuszmaul958b21e2020-02-26 21:51:40 -08008#include "aos/network/message_bridge_server_generated.h"
James Kuszmaul5398fae2020-02-17 16:44:03 -08009#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
14namespace y2020 {
15namespace control_loops {
16namespace 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.
28class Localizer : public frc971::control_loops::drivetrain::LocalizerInterface {
29 public:
30 typedef frc971::control_loops::TypedPose<double> Pose;
31 typedef frc971::control_loops::drivetrain::HybridEkf<double> HybridEkf;
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);
40 State Xhat() const override { return ekf_.X_hat(); }
41 frc971::control_loops::drivetrain::TrivialTargetSelector *target_selector()
42 override {
43 return &target_selector_;
44 }
45
46 void Update(const ::Eigen::Matrix<double, 2, 1> &U,
47 aos::monotonic_clock::time_point now, double left_encoder,
48 double right_encoder, double gyro_rate,
49 const Eigen::Vector3d &accel) override;
50
51 void Reset(aos::monotonic_clock::time_point t, const State &state) {
52 ekf_.ResetInitialState(t, state, ekf_.P());
53 }
54
55 void ResetPosition(aos::monotonic_clock::time_point t, double x, double y,
56 double theta, double /*theta_override*/,
57 bool /*reset_theta*/) override {
58 const double left_encoder = ekf_.X_hat(StateIdx::kLeftEncoder);
59 const double right_encoder = ekf_.X_hat(StateIdx::kRightEncoder);
60 ekf_.ResetInitialState(t, (Ekf::State() << x, y, theta, left_encoder, 0,
61 right_encoder, 0, 0, 0, 0, 0, 0).finished(),
62 ekf_.P());
63 };
64
65 private:
66 // Storage for a single turret position data point.
67 struct TurretData {
68 aos::monotonic_clock::time_point receive_time =
69 aos::monotonic_clock::min_time;
70 double position = 0.0; // rad
71 double velocity = 0.0; // rad/sec
72 };
73
James Kuszmaulc6723cf2020-03-01 14:45:59 -080074 // Processes new image data from the given pi and updates the EKF.
75 void HandleImageMatch(std::string_view pi,
76 const frc971::vision::sift::ImageMatchResult &result);
James Kuszmaul5398fae2020-02-17 16:44:03 -080077
78 // Processes the most recent turret position and stores it in the turret_data_
79 // buffer.
80 void HandleSuperstructureStatus(
81 const y2020::control_loops::superstructure::Status &status);
82
83 // Retrieves the turret data closest to the provided time.
84 TurretData GetTurretDataForTime(aos::monotonic_clock::time_point time);
85
86 aos::EventLoop *const event_loop_;
87 const frc971::control_loops::drivetrain::DrivetrainConfig<double> dt_config_;
88 HybridEkf ekf_;
89
90 std::vector<aos::Fetcher<frc971::vision::sift::ImageMatchResult>>
91 image_fetchers_;
92
James Kuszmaul958b21e2020-02-26 21:51:40 -080093 aos::Fetcher<aos::message_bridge::ServerStatistics> clock_offset_fetcher_;
94
James Kuszmaul5398fae2020-02-17 16:44:03 -080095 // Buffer of recent turret data--this is used so that when we receive a camera
96 // frame from the turret, we can back out what the turret angle was at that
97 // time.
98 aos::RingBuffer<TurretData, 200> turret_data_;
99
100 // Target selector to allow us to satisfy the LocalizerInterface requirements.
101 frc971::control_loops::drivetrain::TrivialTargetSelector target_selector_;
102};
103
104} // namespace drivetrain
105} // namespace control_loops
106} // namespace y2020
107
108#endif // Y2020_CONTROL_LOOPS_DRIVETRAIN_LOCALIZER_H_