blob: 5dbad0209f6441605154edc7cf2116d69248e988 [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:
James Kuszmauld478f872020-03-16 20:54:27 -070030 typedef frc971::control_loops::TypedPose<float> Pose;
31 typedef frc971::control_loops::drivetrain::HybridEkf<float> HybridEkf;
James Kuszmaul5398fae2020-02-17 16:44:03 -080032 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);
James Kuszmauld478f872020-03-16 20:54:27 -070040 frc971::control_loops::drivetrain::HybridEkf<double>::State Xhat()
41 const override {
42 return ekf_.X_hat().cast<double>();
43 }
James Kuszmaul5398fae2020-02-17 16:44:03 -080044 frc971::control_loops::drivetrain::TrivialTargetSelector *target_selector()
45 override {
46 return &target_selector_;
47 }
48
49 void Update(const ::Eigen::Matrix<double, 2, 1> &U,
50 aos::monotonic_clock::time_point now, double left_encoder,
51 double right_encoder, double gyro_rate,
52 const Eigen::Vector3d &accel) override;
53
James Kuszmaulbcd96fc2020-10-12 20:29:32 -070054 void Reset(aos::monotonic_clock::time_point t,
55 const frc971::control_loops::drivetrain::HybridEkf<double>::State
56 &state) override;
James Kuszmaul5398fae2020-02-17 16:44:03 -080057
58 void ResetPosition(aos::monotonic_clock::time_point t, double x, double y,
59 double theta, double /*theta_override*/,
60 bool /*reset_theta*/) override {
61 const double left_encoder = ekf_.X_hat(StateIdx::kLeftEncoder);
62 const double right_encoder = ekf_.X_hat(StateIdx::kRightEncoder);
James Kuszmauld478f872020-03-16 20:54:27 -070063 ekf_.ResetInitialState(t,
64 (HybridEkf::State() << x, y, theta, left_encoder, 0,
65 right_encoder, 0, 0, 0, 0, 0, 0)
66 .finished(),
James Kuszmaul5398fae2020-02-17 16:44:03 -080067 ekf_.P());
68 };
69
70 private:
71 // Storage for a single turret position data point.
72 struct TurretData {
73 aos::monotonic_clock::time_point receive_time =
74 aos::monotonic_clock::min_time;
75 double position = 0.0; // rad
76 double velocity = 0.0; // rad/sec
77 };
78
James Kuszmaulc6723cf2020-03-01 14:45:59 -080079 // Processes new image data from the given pi and updates the EKF.
80 void HandleImageMatch(std::string_view pi,
James Kuszmaul58cb1fe2020-03-07 16:18:59 -080081 const frc971::vision::sift::ImageMatchResult &result,
82 aos::monotonic_clock::time_point now);
James Kuszmaul5398fae2020-02-17 16:44:03 -080083
84 // Processes the most recent turret position and stores it in the turret_data_
85 // buffer.
86 void HandleSuperstructureStatus(
87 const y2020::control_loops::superstructure::Status &status);
88
89 // Retrieves the turret data closest to the provided time.
90 TurretData GetTurretDataForTime(aos::monotonic_clock::time_point time);
91
92 aos::EventLoop *const event_loop_;
93 const frc971::control_loops::drivetrain::DrivetrainConfig<double> dt_config_;
94 HybridEkf ekf_;
95
96 std::vector<aos::Fetcher<frc971::vision::sift::ImageMatchResult>>
97 image_fetchers_;
98
James Kuszmaul958b21e2020-02-26 21:51:40 -080099 aos::Fetcher<aos::message_bridge::ServerStatistics> clock_offset_fetcher_;
100
James Kuszmaul5398fae2020-02-17 16:44:03 -0800101 // Buffer of recent turret data--this is used so that when we receive a camera
102 // frame from the turret, we can back out what the turret angle was at that
103 // time.
104 aos::RingBuffer<TurretData, 200> turret_data_;
105
106 // Target selector to allow us to satisfy the LocalizerInterface requirements.
107 frc971::control_loops::drivetrain::TrivialTargetSelector target_selector_;
108};
109
110} // namespace drivetrain
111} // namespace control_loops
112} // namespace y2020
113
114#endif // Y2020_CONTROL_LOOPS_DRIVETRAIN_LOCALIZER_H_