blob: 66771454db99c2cc761c357be089c0da7d89ebbb [file] [log] [blame]
James Kuszmaul3431d622019-02-17 17:07:44 -08001#ifndef FRC971_CONTROL_LOOPS_DRIVETRAIN_FIELD_ESTIMATOR_H_
2#define FRC971_CONTROL_LOOPS_DRIVETRAIN_FIELD_ESTIMATOR_H_
3
Alex Perrycb7da4b2019-08-28 19:35:56 -07004#include "aos/events/event_loop.h"
James Kuszmaul3431d622019-02-17 17:07:44 -08005#include "frc971/control_loops/drivetrain/drivetrain_config.h"
James Kuszmaul3c5b4d32020-02-11 17:22:14 -08006#include "frc971/control_loops/drivetrain/drivetrain_status_generated.h"
James Kuszmaul3431d622019-02-17 17:07:44 -08007#include "frc971/control_loops/drivetrain/hybrid_ekf.h"
James Kuszmaul5bc6fc92019-03-01 21:50:06 -08008#include "frc971/control_loops/pose.h"
James Kuszmaul3431d622019-02-17 17:07:44 -08009
10namespace frc971 {
11namespace control_loops {
12namespace drivetrain {
13
James Kuszmaul5bc6fc92019-03-01 21:50:06 -080014// An interface for target selection. This provides an object that will take in
15// state updates and then determine what poes we should be driving to.
16class TargetSelectorInterface {
17 public:
18 // Take the state as [x, y, theta, left_vel, right_vel]
19 // If unable to determine what target to go for, returns false. If a viable
20 // target is selected, then returns true and sets target_pose.
James Kuszmaulc4bd1612019-03-10 11:35:06 -070021 // command_speed is the goal speed of the current drivetrain, generally
22 // generated from the throttle and meant to signify driver intent.
James Kuszmaul5bc6fc92019-03-01 21:50:06 -080023 // TODO(james): Some implementations may also want a drivetrain goal so that
24 // driver intent can be divined more directly.
James Kuszmaulc4bd1612019-03-10 11:35:06 -070025 virtual bool UpdateSelection(const ::Eigen::Matrix<double, 5, 1> &state,
26 double command_speed) = 0;
James Kuszmaul5bc6fc92019-03-01 21:50:06 -080027 // Gets the current target pose. Should only be called if UpdateSelection has
28 // returned true.
29 virtual TypedPose<double> TargetPose() const = 0;
James Kuszmaule093f512019-03-20 06:14:05 -070030 // The "radius" of the target--for y2019, we wanted to drive in so that a disc
31 // with radius r would hit the plane of the target at an offset of exactly r
32 // from the TargetPose--this is distinct from wanting the center of the
33 // robot to project straight onto the center of the target.
34 virtual double TargetRadius() const = 0;
James Kuszmaul5bc6fc92019-03-01 21:50:06 -080035};
36
James Kuszmaul3431d622019-02-17 17:07:44 -080037// Defines an interface for classes that provide field-global localization.
38class LocalizerInterface {
James Kuszmaul5398fae2020-02-17 16:44:03 -080039 public:
James Kuszmaul2db5d882020-02-16 16:49:26 -080040 typedef HybridEkf<double> Ekf;
41 typedef typename Ekf::StateIdx StateIdx;
James Kuszmaul5398fae2020-02-17 16:44:03 -080042
James Kuszmaul531609d2020-02-18 17:12:23 -080043 virtual ~LocalizerInterface() {}
44
James Kuszmaul3431d622019-02-17 17:07:44 -080045 // Perform a single step of the filter, using the information that is
46 // available on every drivetrain iteration.
47 // The user should pass in the U that the real system experienced from the
48 // previous timestep until now; internally, any filters will first perform a
49 // prediction step to get the estimate at time now, and then will apply
50 // corrections based on the encoder/gyro/accelerometer values from time now.
51 // TODO(james): Consider letting implementations subscribe to the sensor
52 // values themselves, and then only passing in U. This requires more
53 // coordination on timing, however.
54 virtual void Update(const ::Eigen::Matrix<double, 2, 1> &U,
55 ::aos::monotonic_clock::time_point now,
56 double left_encoder, double right_encoder,
James Kuszmaul3c5b4d32020-02-11 17:22:14 -080057 double gyro_rate, const Eigen::Vector3d &accel) = 0;
James Kuszmaulef428a02019-03-02 22:19:41 -080058 // Reset the absolute position of the estimator.
James Kuszmaul074429e2019-03-23 16:01:49 -070059 virtual void ResetPosition(::aos::monotonic_clock::time_point t, double x,
James Kuszmaul518640d2019-04-13 15:50:50 -070060 double y, double theta, double theta_uncertainty,
61 bool reset_theta) = 0;
James Kuszmaul3c5b4d32020-02-11 17:22:14 -080062 flatbuffers::Offset<LocalizerState> PopulateStatus(
63 flatbuffers::FlatBufferBuilder *fbb) {
64 LocalizerState::Builder builder(*fbb);
65 builder.add_x(x());
66 builder.add_y(y());
67 builder.add_theta(theta());
68 builder.add_left_velocity(left_velocity());
69 builder.add_right_velocity(right_velocity());
70 builder.add_left_encoder(left_encoder());
71 builder.add_right_encoder(right_encoder());
72 builder.add_left_voltage_error(left_voltage_error());
73 builder.add_right_voltage_error(right_voltage_error());
74 builder.add_angular_error(angular_error());
75 builder.add_longitudinal_velocity_offset(longitudinal_velocity_offset());
76 builder.add_lateral_velocity(lateral_velocity());
77 return builder.Finish();
78 }
James Kuszmaul2db5d882020-02-16 16:49:26 -080079 virtual Ekf::State Xhat() const = 0;
James Kuszmaul3431d622019-02-17 17:07:44 -080080 // There are several subtly different norms floating around for state
James Kuszmaul3c5b4d32020-02-11 17:22:14 -080081 // matrices. In order to avoid that mess, we just provide direct accessors for
James Kuszmaul3431d622019-02-17 17:07:44 -080082 // the values that most people care about.
James Kuszmaul2db5d882020-02-16 16:49:26 -080083 double x() const { return Xhat()(StateIdx::kX); }
84 double y() const { return Xhat()(StateIdx::kY); }
85 double theta() const { return Xhat()(StateIdx::kTheta); }
86 double left_velocity() const { return Xhat()(StateIdx::kLeftVelocity); }
87 double right_velocity() const { return Xhat()(StateIdx::kRightVelocity); }
88 double left_encoder() const { return Xhat()(StateIdx::kLeftEncoder); }
89 double right_encoder() const { return Xhat()(StateIdx::kRightEncoder); }
90 double left_voltage_error() const {
91 return Xhat()(StateIdx::kLeftVoltageError);
92 }
93 double right_voltage_error() const {
94 return Xhat()(StateIdx::kRightVoltageError);
95 }
96 double angular_error() const { return Xhat()(StateIdx::kAngularError); }
97 double longitudinal_velocity_offset() const {
98 return Xhat()(StateIdx::kLongitudinalVelocityOffset);
99 }
100 double lateral_velocity() const { return Xhat()(StateIdx::kLateralVelocity); }
101
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800102 virtual TargetSelectorInterface *target_selector() = 0;
103};
104
105// A target selector, primarily for testing purposes, that just lets a user
106// manually set the target selector state.
107class TrivialTargetSelector : public TargetSelectorInterface {
108 public:
James Kuszmaulc4bd1612019-03-10 11:35:06 -0700109 bool UpdateSelection(const ::Eigen::Matrix<double, 5, 1> &, double) override {
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800110 return has_target_;
111 }
112 TypedPose<double> TargetPose() const override { return pose_; }
James Kuszmaule093f512019-03-20 06:14:05 -0700113 double TargetRadius() const override { return target_radius_; }
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800114
115 void set_pose(const TypedPose<double> &pose) { pose_ = pose; }
James Kuszmaule093f512019-03-20 06:14:05 -0700116 void set_target_radius(double radius) { target_radius_ = radius; }
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800117 void set_has_target(bool has_target) { has_target_ = has_target; }
118 bool has_target() const { return has_target_; }
119
120 private:
121 bool has_target_ = true;
122 TypedPose<double> pose_;
James Kuszmaule093f512019-03-20 06:14:05 -0700123 double target_radius_ = 0.0;
James Kuszmaul3431d622019-02-17 17:07:44 -0800124};
125
126// Uses the generic HybridEkf implementation to provide a basic field estimator.
127// This provides no method for using cameras or the such to get global
128// measurements and just assumes that you can dead-reckon perfectly.
129class DeadReckonEkf : public LocalizerInterface {
James Kuszmaul3431d622019-02-17 17:07:44 -0800130 public:
Austin Schuh9fe68f72019-08-10 19:32:03 -0700131 DeadReckonEkf(::aos::EventLoop *event_loop,
132 const DrivetrainConfig<double> &dt_config)
133 : ekf_(dt_config) {
134 event_loop->OnRun([this, event_loop]() {
135 ekf_.ResetInitialState(event_loop->monotonic_now(), Ekf::State::Zero(),
136 ekf_.P());
137 });
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800138 target_selector_.set_has_target(false);
James Kuszmaul3431d622019-02-17 17:07:44 -0800139 }
140
James Kuszmaul531609d2020-02-18 17:12:23 -0800141 virtual ~DeadReckonEkf() {}
142
James Kuszmaul3431d622019-02-17 17:07:44 -0800143 void Update(const ::Eigen::Matrix<double, 2, 1> &U,
Austin Schuh9fe68f72019-08-10 19:32:03 -0700144 ::aos::monotonic_clock::time_point now, double left_encoder,
145 double right_encoder, double gyro_rate,
James Kuszmaul3c5b4d32020-02-11 17:22:14 -0800146 const Eigen::Vector3d &accel) override {
147 ekf_.UpdateEncodersAndGyro(left_encoder, right_encoder, gyro_rate, U, accel,
148 now);
James Kuszmaul3431d622019-02-17 17:07:44 -0800149 }
150
James Kuszmaul074429e2019-03-23 16:01:49 -0700151 void ResetPosition(::aos::monotonic_clock::time_point t, double x, double y,
James Kuszmaul518640d2019-04-13 15:50:50 -0700152 double theta, double /*theta_override*/,
153 bool /*reset_theta*/) override {
James Kuszmaulfedc4612019-03-10 11:24:51 -0700154 const double left_encoder = ekf_.X_hat(StateIdx::kLeftEncoder);
155 const double right_encoder = ekf_.X_hat(StateIdx::kRightEncoder);
James Kuszmaul3c5b4d32020-02-11 17:22:14 -0800156 ekf_.ResetInitialState(t,
157 (Ekf::State() << x, y, theta, left_encoder, 0,
158 right_encoder, 0, 0, 0, 0, 0, 0)
159 .finished(),
James Kuszmaul074429e2019-03-23 16:01:49 -0700160 ekf_.P());
James Kuszmaul2db5d882020-02-16 16:49:26 -0800161 }
James Kuszmaulef428a02019-03-02 22:19:41 -0800162
James Kuszmaul2db5d882020-02-16 16:49:26 -0800163 Ekf::State Xhat() const override { return ekf_.X_hat(); }
James Kuszmaul3431d622019-02-17 17:07:44 -0800164
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800165 TrivialTargetSelector *target_selector() override {
166 return &target_selector_;
167 }
168
James Kuszmaul3431d622019-02-17 17:07:44 -0800169 private:
170 Ekf ekf_;
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800171 TrivialTargetSelector target_selector_;
James Kuszmaul3431d622019-02-17 17:07:44 -0800172};
173
174} // namespace drivetrain
175} // namespace control_loops
176} // namespace frc971
177
178#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_FIELD_ESTIMATOR_H_