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