blob: 54587975b9f39b66356d789886070acb05b2de2f [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 Kuszmaulf75ecd62021-10-23 14:33:46 -070081 static constexpr size_t kNumRejectionReasons =
82 static_cast<int>(RejectionReason::MAX) -
83 static_cast<int>(RejectionReason::MIN) + 1;
84
85 struct Statistics {
86 int total_accepted = 0;
87 int total_candidates = 0;
88 static_assert(0 == static_cast<int>(RejectionReason::MIN));
89 static_assert(
90 kNumRejectionReasons ==
91 sizeof(
92 std::invoke_result<decltype(EnumValuesRejectionReason)>::type) /
93 sizeof(RejectionReason),
94 "RejectionReason has non-contiguous error values.");
95 std::array<int, kNumRejectionReasons> rejection_counts;
96 };
97
James Kuszmaulc6723cf2020-03-01 14:45:59 -080098 // Processes new image data from the given pi and updates the EKF.
James Kuszmaul5ff8a862021-09-25 17:29:43 -070099 aos::SizedArray<flatbuffers::Offset<ImageMatchDebug>, 5> HandleImageMatch(
100 size_t camera_index, std::string_view pi,
101 const frc971::vision::sift::ImageMatchResult &result,
102 aos::monotonic_clock::time_point now,
103 flatbuffers::FlatBufferBuilder *fbb);
James Kuszmaul5398fae2020-02-17 16:44:03 -0800104
105 // Processes the most recent turret position and stores it in the turret_data_
106 // buffer.
107 void HandleSuperstructureStatus(
108 const y2020::control_loops::superstructure::Status &status);
109
110 // Retrieves the turret data closest to the provided time.
111 TurretData GetTurretDataForTime(aos::monotonic_clock::time_point time);
112
113 aos::EventLoop *const event_loop_;
114 const frc971::control_loops::drivetrain::DrivetrainConfig<double> dt_config_;
115 HybridEkf ekf_;
116
117 std::vector<aos::Fetcher<frc971::vision::sift::ImageMatchResult>>
118 image_fetchers_;
119
James Kuszmaul958b21e2020-02-26 21:51:40 -0800120 aos::Fetcher<aos::message_bridge::ServerStatistics> clock_offset_fetcher_;
121
James Kuszmaul5ff8a862021-09-25 17:29:43 -0700122 aos::Sender<y2020::control_loops::drivetrain::LocalizerDebug> debug_sender_;
123
James Kuszmaul5398fae2020-02-17 16:44:03 -0800124 // Buffer of recent turret data--this is used so that when we receive a camera
125 // frame from the turret, we can back out what the turret angle was at that
126 // time.
127 aos::RingBuffer<TurretData, 200> turret_data_;
128
129 // Target selector to allow us to satisfy the LocalizerInterface requirements.
130 frc971::control_loops::drivetrain::TrivialTargetSelector target_selector_;
James Kuszmaulf75ecd62021-10-23 14:33:46 -0700131
132 Statistics statistics_;
James Kuszmaul5398fae2020-02-17 16:44:03 -0800133};
134
135} // namespace drivetrain
136} // namespace control_loops
137} // namespace y2020
138
139#endif // Y2020_CONTROL_LOOPS_DRIVETRAIN_LOCALIZER_H_