blob: 6e53138ca35ad94fb6beeb55b667d2dbac6b87d7 [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:
James Kuszmaul055fe762023-03-03 21:14:01 -080018 typedef RobotSide Side;
James Kuszmaulc29f4572023-02-25 17:02:33 -080019 virtual ~TargetSelectorInterface() {}
James Kuszmaul5bc6fc92019-03-01 21:50:06 -080020 // Take the state as [x, y, theta, left_vel, right_vel]
21 // If unable to determine what target to go for, returns false. If a viable
22 // target is selected, then returns true and sets target_pose.
James Kuszmaulc4bd1612019-03-10 11:35:06 -070023 // command_speed is the goal speed of the current drivetrain, generally
24 // generated from the throttle and meant to signify driver intent.
James Kuszmaul5bc6fc92019-03-01 21:50:06 -080025 // TODO(james): Some implementations may also want a drivetrain goal so that
26 // driver intent can be divined more directly.
James Kuszmaulc4bd1612019-03-10 11:35:06 -070027 virtual bool UpdateSelection(const ::Eigen::Matrix<double, 5, 1> &state,
28 double command_speed) = 0;
James Kuszmaul5bc6fc92019-03-01 21:50:06 -080029 // Gets the current target pose. Should only be called if UpdateSelection has
30 // returned true.
31 virtual TypedPose<double> TargetPose() const = 0;
James Kuszmaule093f512019-03-20 06:14:05 -070032 // The "radius" of the target--for y2019, we wanted to drive in so that a disc
33 // with radius r would hit the plane of the target at an offset of exactly r
34 // from the TargetPose--this is distinct from wanting the center of the
35 // robot to project straight onto the center of the target.
36 virtual double TargetRadius() const = 0;
James Kuszmaul055fe762023-03-03 21:14:01 -080037 // Which direction we want the robot to drive to get to the target.
38 virtual Side DriveDirection() const = 0;
James Kuszmaulc0163792023-03-04 14:13:31 -080039 // Indicates that the line following *must* drive to the currently selected
40 // target, regardless of any hysteresis we try to use to protect the driver.
41 virtual bool ForceReselectTarget() const = 0;
James Kuszmaul5bc6fc92019-03-01 21:50:06 -080042};
43
James Kuszmaul3431d622019-02-17 17:07:44 -080044// Defines an interface for classes that provide field-global localization.
45class LocalizerInterface {
James Kuszmaul5398fae2020-02-17 16:44:03 -080046 public:
James Kuszmaul2db5d882020-02-16 16:49:26 -080047 typedef HybridEkf<double> Ekf;
48 typedef typename Ekf::StateIdx StateIdx;
James Kuszmaul5398fae2020-02-17 16:44:03 -080049
James Kuszmaul531609d2020-02-18 17:12:23 -080050 virtual ~LocalizerInterface() {}
51
James Kuszmaul3431d622019-02-17 17:07:44 -080052 // Perform a single step of the filter, using the information that is
53 // available on every drivetrain iteration.
54 // The user should pass in the U that the real system experienced from the
55 // previous timestep until now; internally, any filters will first perform a
56 // prediction step to get the estimate at time now, and then will apply
57 // corrections based on the encoder/gyro/accelerometer values from time now.
58 // TODO(james): Consider letting implementations subscribe to the sensor
59 // values themselves, and then only passing in U. This requires more
60 // coordination on timing, however.
61 virtual void Update(const ::Eigen::Matrix<double, 2, 1> &U,
62 ::aos::monotonic_clock::time_point now,
63 double left_encoder, double right_encoder,
James Kuszmaul3c5b4d32020-02-11 17:22:14 -080064 double gyro_rate, const Eigen::Vector3d &accel) = 0;
James Kuszmaulbcd96fc2020-10-12 20:29:32 -070065 virtual void Reset(aos::monotonic_clock::time_point t,
66 const Ekf::State &state) = 0;
James Kuszmaulef428a02019-03-02 22:19:41 -080067 // Reset the absolute position of the estimator.
James Kuszmaul074429e2019-03-23 16:01:49 -070068 virtual void ResetPosition(::aos::monotonic_clock::time_point t, double x,
James Kuszmaul518640d2019-04-13 15:50:50 -070069 double y, double theta, double theta_uncertainty,
70 bool reset_theta) = 0;
James Kuszmaul3c5b4d32020-02-11 17:22:14 -080071 flatbuffers::Offset<LocalizerState> PopulateStatus(
72 flatbuffers::FlatBufferBuilder *fbb) {
73 LocalizerState::Builder builder(*fbb);
74 builder.add_x(x());
75 builder.add_y(y());
76 builder.add_theta(theta());
77 builder.add_left_velocity(left_velocity());
78 builder.add_right_velocity(right_velocity());
79 builder.add_left_encoder(left_encoder());
80 builder.add_right_encoder(right_encoder());
81 builder.add_left_voltage_error(left_voltage_error());
82 builder.add_right_voltage_error(right_voltage_error());
83 builder.add_angular_error(angular_error());
84 builder.add_longitudinal_velocity_offset(longitudinal_velocity_offset());
85 builder.add_lateral_velocity(lateral_velocity());
86 return builder.Finish();
87 }
James Kuszmaul2db5d882020-02-16 16:49:26 -080088 virtual Ekf::State Xhat() const = 0;
James Kuszmaul3431d622019-02-17 17:07:44 -080089 // There are several subtly different norms floating around for state
James Kuszmaul3c5b4d32020-02-11 17:22:14 -080090 // matrices. In order to avoid that mess, we just provide direct accessors for
James Kuszmaul3431d622019-02-17 17:07:44 -080091 // the values that most people care about.
James Kuszmaul2db5d882020-02-16 16:49:26 -080092 double x() const { return Xhat()(StateIdx::kX); }
93 double y() const { return Xhat()(StateIdx::kY); }
94 double theta() const { return Xhat()(StateIdx::kTheta); }
95 double left_velocity() const { return Xhat()(StateIdx::kLeftVelocity); }
96 double right_velocity() const { return Xhat()(StateIdx::kRightVelocity); }
97 double left_encoder() const { return Xhat()(StateIdx::kLeftEncoder); }
98 double right_encoder() const { return Xhat()(StateIdx::kRightEncoder); }
99 double left_voltage_error() const {
100 return Xhat()(StateIdx::kLeftVoltageError);
101 }
102 double right_voltage_error() const {
103 return Xhat()(StateIdx::kRightVoltageError);
104 }
105 double angular_error() const { return Xhat()(StateIdx::kAngularError); }
106 double longitudinal_velocity_offset() const {
107 return Xhat()(StateIdx::kLongitudinalVelocityOffset);
108 }
109 double lateral_velocity() const { return Xhat()(StateIdx::kLateralVelocity); }
110
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800111 virtual TargetSelectorInterface *target_selector() = 0;
112};
113
114// A target selector, primarily for testing purposes, that just lets a user
115// manually set the target selector state.
116class TrivialTargetSelector : public TargetSelectorInterface {
117 public:
James Kuszmaulc29f4572023-02-25 17:02:33 -0800118 virtual ~TrivialTargetSelector() {}
James Kuszmaulc4bd1612019-03-10 11:35:06 -0700119 bool UpdateSelection(const ::Eigen::Matrix<double, 5, 1> &, double) override {
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800120 return has_target_;
121 }
122 TypedPose<double> TargetPose() const override { return pose_; }
James Kuszmaule093f512019-03-20 06:14:05 -0700123 double TargetRadius() const override { return target_radius_; }
James Kuszmaul055fe762023-03-03 21:14:01 -0800124 Side DriveDirection() const override { return drive_direction_; }
James Kuszmaulc0163792023-03-04 14:13:31 -0800125 bool ForceReselectTarget() const override { return force_reselect_; }
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800126
127 void set_pose(const TypedPose<double> &pose) { pose_ = pose; }
James Kuszmaule093f512019-03-20 06:14:05 -0700128 void set_target_radius(double radius) { target_radius_ = radius; }
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800129 void set_has_target(bool has_target) { has_target_ = has_target; }
James Kuszmaul055fe762023-03-03 21:14:01 -0800130 void set_drive_direction(Side side) { drive_direction_ = side; }
James Kuszmaulc0163792023-03-04 14:13:31 -0800131 void set_force_reselect(bool force_reselect) {
132 force_reselect_ = force_reselect;
133 }
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800134 bool has_target() const { return has_target_; }
135
136 private:
137 bool has_target_ = true;
James Kuszmaulc0163792023-03-04 14:13:31 -0800138 bool force_reselect_ = false;
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800139 TypedPose<double> pose_;
James Kuszmaule093f512019-03-20 06:14:05 -0700140 double target_radius_ = 0.0;
James Kuszmaul055fe762023-03-03 21:14:01 -0800141 Side drive_direction_ = Side::DONT_CARE;
James Kuszmaul3431d622019-02-17 17:07:44 -0800142};
143
144// Uses the generic HybridEkf implementation to provide a basic field estimator.
145// This provides no method for using cameras or the such to get global
146// measurements and just assumes that you can dead-reckon perfectly.
147class DeadReckonEkf : public LocalizerInterface {
James Kuszmaul3431d622019-02-17 17:07:44 -0800148 public:
Austin Schuh9fe68f72019-08-10 19:32:03 -0700149 DeadReckonEkf(::aos::EventLoop *event_loop,
150 const DrivetrainConfig<double> &dt_config)
151 : ekf_(dt_config) {
152 event_loop->OnRun([this, event_loop]() {
153 ekf_.ResetInitialState(event_loop->monotonic_now(), Ekf::State::Zero(),
154 ekf_.P());
155 });
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800156 target_selector_.set_has_target(false);
James Kuszmaul3431d622019-02-17 17:07:44 -0800157 }
158
James Kuszmaul531609d2020-02-18 17:12:23 -0800159 virtual ~DeadReckonEkf() {}
160
James Kuszmaul3431d622019-02-17 17:07:44 -0800161 void Update(const ::Eigen::Matrix<double, 2, 1> &U,
Austin Schuh9fe68f72019-08-10 19:32:03 -0700162 ::aos::monotonic_clock::time_point now, double left_encoder,
163 double right_encoder, double gyro_rate,
James Kuszmaul3c5b4d32020-02-11 17:22:14 -0800164 const Eigen::Vector3d &accel) override {
165 ekf_.UpdateEncodersAndGyro(left_encoder, right_encoder, gyro_rate, U, accel,
166 now);
James Kuszmaul3431d622019-02-17 17:07:44 -0800167 }
168
James Kuszmaulbcd96fc2020-10-12 20:29:32 -0700169 void Reset(aos::monotonic_clock::time_point t,
170 const HybridEkf<double>::State &state) override {
171 ekf_.ResetInitialState(t, state, ekf_.P());
172 }
173
James Kuszmaul074429e2019-03-23 16:01:49 -0700174 void ResetPosition(::aos::monotonic_clock::time_point t, double x, double y,
James Kuszmaul518640d2019-04-13 15:50:50 -0700175 double theta, double /*theta_override*/,
176 bool /*reset_theta*/) override {
James Kuszmaulfedc4612019-03-10 11:24:51 -0700177 const double left_encoder = ekf_.X_hat(StateIdx::kLeftEncoder);
178 const double right_encoder = ekf_.X_hat(StateIdx::kRightEncoder);
James Kuszmaul3c5b4d32020-02-11 17:22:14 -0800179 ekf_.ResetInitialState(t,
180 (Ekf::State() << x, y, theta, left_encoder, 0,
181 right_encoder, 0, 0, 0, 0, 0, 0)
182 .finished(),
James Kuszmaul074429e2019-03-23 16:01:49 -0700183 ekf_.P());
James Kuszmaul2db5d882020-02-16 16:49:26 -0800184 }
James Kuszmaulef428a02019-03-02 22:19:41 -0800185
James Kuszmaul2db5d882020-02-16 16:49:26 -0800186 Ekf::State Xhat() const override { return ekf_.X_hat(); }
James Kuszmaul3431d622019-02-17 17:07:44 -0800187
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800188 TrivialTargetSelector *target_selector() override {
189 return &target_selector_;
190 }
191
James Kuszmaul3431d622019-02-17 17:07:44 -0800192 private:
193 Ekf ekf_;
James Kuszmaul5bc6fc92019-03-01 21:50:06 -0800194 TrivialTargetSelector target_selector_;
James Kuszmaul3431d622019-02-17 17:07:44 -0800195};
196
197} // namespace drivetrain
198} // namespace control_loops
199} // namespace frc971
200
201#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_FIELD_ESTIMATOR_H_