James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_DRIVETRAIN_FIELD_ESTIMATOR_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_DRIVETRAIN_FIELD_ESTIMATOR_H_ |
| 3 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 4 | #include "aos/events/event_loop.h" |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 5 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
James Kuszmaul | 3c5b4d3 | 2020-02-11 17:22:14 -0800 | [diff] [blame] | 6 | #include "frc971/control_loops/drivetrain/drivetrain_status_generated.h" |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 7 | #include "frc971/control_loops/drivetrain/hybrid_ekf.h" |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 8 | #include "frc971/control_loops/pose.h" |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 9 | |
| 10 | namespace frc971 { |
| 11 | namespace control_loops { |
| 12 | namespace drivetrain { |
| 13 | |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 14 | // 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. |
| 16 | class TargetSelectorInterface { |
| 17 | public: |
James Kuszmaul | c29f457 | 2023-02-25 17:02:33 -0800 | [diff] [blame] | 18 | virtual ~TargetSelectorInterface() {} |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 19 | // Take the state as [x, y, theta, left_vel, right_vel] |
| 20 | // If unable to determine what target to go for, returns false. If a viable |
| 21 | // target is selected, then returns true and sets target_pose. |
James Kuszmaul | c4bd161 | 2019-03-10 11:35:06 -0700 | [diff] [blame] | 22 | // command_speed is the goal speed of the current drivetrain, generally |
| 23 | // generated from the throttle and meant to signify driver intent. |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 24 | // TODO(james): Some implementations may also want a drivetrain goal so that |
| 25 | // driver intent can be divined more directly. |
James Kuszmaul | c4bd161 | 2019-03-10 11:35:06 -0700 | [diff] [blame] | 26 | virtual bool UpdateSelection(const ::Eigen::Matrix<double, 5, 1> &state, |
| 27 | double command_speed) = 0; |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 28 | // Gets the current target pose. Should only be called if UpdateSelection has |
| 29 | // returned true. |
| 30 | virtual TypedPose<double> TargetPose() const = 0; |
James Kuszmaul | e093f51 | 2019-03-20 06:14:05 -0700 | [diff] [blame] | 31 | // The "radius" of the target--for y2019, we wanted to drive in so that a disc |
| 32 | // with radius r would hit the plane of the target at an offset of exactly r |
| 33 | // from the TargetPose--this is distinct from wanting the center of the |
| 34 | // robot to project straight onto the center of the target. |
| 35 | virtual double TargetRadius() const = 0; |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 36 | }; |
| 37 | |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 38 | // Defines an interface for classes that provide field-global localization. |
| 39 | class LocalizerInterface { |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 40 | public: |
James Kuszmaul | 2db5d88 | 2020-02-16 16:49:26 -0800 | [diff] [blame] | 41 | typedef HybridEkf<double> Ekf; |
| 42 | typedef typename Ekf::StateIdx StateIdx; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 43 | |
James Kuszmaul | 531609d | 2020-02-18 17:12:23 -0800 | [diff] [blame] | 44 | virtual ~LocalizerInterface() {} |
| 45 | |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 46 | // Perform a single step of the filter, using the information that is |
| 47 | // available on every drivetrain iteration. |
| 48 | // The user should pass in the U that the real system experienced from the |
| 49 | // previous timestep until now; internally, any filters will first perform a |
| 50 | // prediction step to get the estimate at time now, and then will apply |
| 51 | // corrections based on the encoder/gyro/accelerometer values from time now. |
| 52 | // TODO(james): Consider letting implementations subscribe to the sensor |
| 53 | // values themselves, and then only passing in U. This requires more |
| 54 | // coordination on timing, however. |
| 55 | virtual void Update(const ::Eigen::Matrix<double, 2, 1> &U, |
| 56 | ::aos::monotonic_clock::time_point now, |
| 57 | double left_encoder, double right_encoder, |
James Kuszmaul | 3c5b4d3 | 2020-02-11 17:22:14 -0800 | [diff] [blame] | 58 | double gyro_rate, const Eigen::Vector3d &accel) = 0; |
James Kuszmaul | bcd96fc | 2020-10-12 20:29:32 -0700 | [diff] [blame] | 59 | virtual void Reset(aos::monotonic_clock::time_point t, |
| 60 | const Ekf::State &state) = 0; |
James Kuszmaul | ef428a0 | 2019-03-02 22:19:41 -0800 | [diff] [blame] | 61 | // Reset the absolute position of the estimator. |
James Kuszmaul | 074429e | 2019-03-23 16:01:49 -0700 | [diff] [blame] | 62 | virtual void ResetPosition(::aos::monotonic_clock::time_point t, double x, |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 63 | double y, double theta, double theta_uncertainty, |
| 64 | bool reset_theta) = 0; |
James Kuszmaul | 3c5b4d3 | 2020-02-11 17:22:14 -0800 | [diff] [blame] | 65 | flatbuffers::Offset<LocalizerState> PopulateStatus( |
| 66 | flatbuffers::FlatBufferBuilder *fbb) { |
| 67 | LocalizerState::Builder builder(*fbb); |
| 68 | builder.add_x(x()); |
| 69 | builder.add_y(y()); |
| 70 | builder.add_theta(theta()); |
| 71 | builder.add_left_velocity(left_velocity()); |
| 72 | builder.add_right_velocity(right_velocity()); |
| 73 | builder.add_left_encoder(left_encoder()); |
| 74 | builder.add_right_encoder(right_encoder()); |
| 75 | builder.add_left_voltage_error(left_voltage_error()); |
| 76 | builder.add_right_voltage_error(right_voltage_error()); |
| 77 | builder.add_angular_error(angular_error()); |
| 78 | builder.add_longitudinal_velocity_offset(longitudinal_velocity_offset()); |
| 79 | builder.add_lateral_velocity(lateral_velocity()); |
| 80 | return builder.Finish(); |
| 81 | } |
James Kuszmaul | 2db5d88 | 2020-02-16 16:49:26 -0800 | [diff] [blame] | 82 | virtual Ekf::State Xhat() const = 0; |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 83 | // There are several subtly different norms floating around for state |
James Kuszmaul | 3c5b4d3 | 2020-02-11 17:22:14 -0800 | [diff] [blame] | 84 | // matrices. In order to avoid that mess, we just provide direct accessors for |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 85 | // the values that most people care about. |
James Kuszmaul | 2db5d88 | 2020-02-16 16:49:26 -0800 | [diff] [blame] | 86 | double x() const { return Xhat()(StateIdx::kX); } |
| 87 | double y() const { return Xhat()(StateIdx::kY); } |
| 88 | double theta() const { return Xhat()(StateIdx::kTheta); } |
| 89 | double left_velocity() const { return Xhat()(StateIdx::kLeftVelocity); } |
| 90 | double right_velocity() const { return Xhat()(StateIdx::kRightVelocity); } |
| 91 | double left_encoder() const { return Xhat()(StateIdx::kLeftEncoder); } |
| 92 | double right_encoder() const { return Xhat()(StateIdx::kRightEncoder); } |
| 93 | double left_voltage_error() const { |
| 94 | return Xhat()(StateIdx::kLeftVoltageError); |
| 95 | } |
| 96 | double right_voltage_error() const { |
| 97 | return Xhat()(StateIdx::kRightVoltageError); |
| 98 | } |
| 99 | double angular_error() const { return Xhat()(StateIdx::kAngularError); } |
| 100 | double longitudinal_velocity_offset() const { |
| 101 | return Xhat()(StateIdx::kLongitudinalVelocityOffset); |
| 102 | } |
| 103 | double lateral_velocity() const { return Xhat()(StateIdx::kLateralVelocity); } |
| 104 | |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 105 | virtual TargetSelectorInterface *target_selector() = 0; |
| 106 | }; |
| 107 | |
| 108 | // A target selector, primarily for testing purposes, that just lets a user |
| 109 | // manually set the target selector state. |
| 110 | class TrivialTargetSelector : public TargetSelectorInterface { |
| 111 | public: |
James Kuszmaul | c29f457 | 2023-02-25 17:02:33 -0800 | [diff] [blame] | 112 | virtual ~TrivialTargetSelector() {} |
James Kuszmaul | c4bd161 | 2019-03-10 11:35:06 -0700 | [diff] [blame] | 113 | bool UpdateSelection(const ::Eigen::Matrix<double, 5, 1> &, double) override { |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 114 | return has_target_; |
| 115 | } |
| 116 | TypedPose<double> TargetPose() const override { return pose_; } |
James Kuszmaul | e093f51 | 2019-03-20 06:14:05 -0700 | [diff] [blame] | 117 | double TargetRadius() const override { return target_radius_; } |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 118 | |
| 119 | void set_pose(const TypedPose<double> &pose) { pose_ = pose; } |
James Kuszmaul | e093f51 | 2019-03-20 06:14:05 -0700 | [diff] [blame] | 120 | void set_target_radius(double radius) { target_radius_ = radius; } |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 121 | void set_has_target(bool has_target) { has_target_ = has_target; } |
| 122 | bool has_target() const { return has_target_; } |
| 123 | |
| 124 | private: |
| 125 | bool has_target_ = true; |
| 126 | TypedPose<double> pose_; |
James Kuszmaul | e093f51 | 2019-03-20 06:14:05 -0700 | [diff] [blame] | 127 | double target_radius_ = 0.0; |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | // Uses the generic HybridEkf implementation to provide a basic field estimator. |
| 131 | // This provides no method for using cameras or the such to get global |
| 132 | // measurements and just assumes that you can dead-reckon perfectly. |
| 133 | class DeadReckonEkf : public LocalizerInterface { |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 134 | public: |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 135 | DeadReckonEkf(::aos::EventLoop *event_loop, |
| 136 | const DrivetrainConfig<double> &dt_config) |
| 137 | : ekf_(dt_config) { |
| 138 | event_loop->OnRun([this, event_loop]() { |
| 139 | ekf_.ResetInitialState(event_loop->monotonic_now(), Ekf::State::Zero(), |
| 140 | ekf_.P()); |
| 141 | }); |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 142 | target_selector_.set_has_target(false); |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 143 | } |
| 144 | |
James Kuszmaul | 531609d | 2020-02-18 17:12:23 -0800 | [diff] [blame] | 145 | virtual ~DeadReckonEkf() {} |
| 146 | |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 147 | void Update(const ::Eigen::Matrix<double, 2, 1> &U, |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 148 | ::aos::monotonic_clock::time_point now, double left_encoder, |
| 149 | double right_encoder, double gyro_rate, |
James Kuszmaul | 3c5b4d3 | 2020-02-11 17:22:14 -0800 | [diff] [blame] | 150 | const Eigen::Vector3d &accel) override { |
| 151 | ekf_.UpdateEncodersAndGyro(left_encoder, right_encoder, gyro_rate, U, accel, |
| 152 | now); |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 153 | } |
| 154 | |
James Kuszmaul | bcd96fc | 2020-10-12 20:29:32 -0700 | [diff] [blame] | 155 | void Reset(aos::monotonic_clock::time_point t, |
| 156 | const HybridEkf<double>::State &state) override { |
| 157 | ekf_.ResetInitialState(t, state, ekf_.P()); |
| 158 | } |
| 159 | |
James Kuszmaul | 074429e | 2019-03-23 16:01:49 -0700 | [diff] [blame] | 160 | void ResetPosition(::aos::monotonic_clock::time_point t, double x, double y, |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 161 | double theta, double /*theta_override*/, |
| 162 | bool /*reset_theta*/) override { |
James Kuszmaul | fedc461 | 2019-03-10 11:24:51 -0700 | [diff] [blame] | 163 | const double left_encoder = ekf_.X_hat(StateIdx::kLeftEncoder); |
| 164 | const double right_encoder = ekf_.X_hat(StateIdx::kRightEncoder); |
James Kuszmaul | 3c5b4d3 | 2020-02-11 17:22:14 -0800 | [diff] [blame] | 165 | ekf_.ResetInitialState(t, |
| 166 | (Ekf::State() << x, y, theta, left_encoder, 0, |
| 167 | right_encoder, 0, 0, 0, 0, 0, 0) |
| 168 | .finished(), |
James Kuszmaul | 074429e | 2019-03-23 16:01:49 -0700 | [diff] [blame] | 169 | ekf_.P()); |
James Kuszmaul | 2db5d88 | 2020-02-16 16:49:26 -0800 | [diff] [blame] | 170 | } |
James Kuszmaul | ef428a0 | 2019-03-02 22:19:41 -0800 | [diff] [blame] | 171 | |
James Kuszmaul | 2db5d88 | 2020-02-16 16:49:26 -0800 | [diff] [blame] | 172 | Ekf::State Xhat() const override { return ekf_.X_hat(); } |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 173 | |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 174 | TrivialTargetSelector *target_selector() override { |
| 175 | return &target_selector_; |
| 176 | } |
| 177 | |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 178 | private: |
| 179 | Ekf ekf_; |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 180 | TrivialTargetSelector target_selector_; |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | } // namespace drivetrain |
| 184 | } // namespace control_loops |
| 185 | } // namespace frc971 |
| 186 | |
| 187 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_FIELD_ESTIMATOR_H_ |