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 | 055fe76 | 2023-03-03 21:14:01 -0800 | [diff] [blame] | 18 | typedef RobotSide Side; |
James Kuszmaul | c29f457 | 2023-02-25 17:02:33 -0800 | [diff] [blame] | 19 | virtual ~TargetSelectorInterface() {} |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 20 | // 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 Kuszmaul | c4bd161 | 2019-03-10 11:35:06 -0700 | [diff] [blame] | 23 | // command_speed is the goal speed of the current drivetrain, generally |
| 24 | // generated from the throttle and meant to signify driver intent. |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 25 | // TODO(james): Some implementations may also want a drivetrain goal so that |
| 26 | // driver intent can be divined more directly. |
James Kuszmaul | c4bd161 | 2019-03-10 11:35:06 -0700 | [diff] [blame] | 27 | virtual bool UpdateSelection(const ::Eigen::Matrix<double, 5, 1> &state, |
| 28 | double command_speed) = 0; |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 29 | // Gets the current target pose. Should only be called if UpdateSelection has |
| 30 | // returned true. |
| 31 | virtual TypedPose<double> TargetPose() const = 0; |
James Kuszmaul | 4f05d79 | 2023-03-05 14:14:18 -0800 | [diff] [blame^] | 32 | // For the "radii" below, we have two possible modes: |
| 33 | // 1) Akin to 2019, we can place with either edge of the game piece, so |
| 34 | // the line following code will have to automatically detect which edge |
| 35 | // (right or left) to aim to have intersect the target. |
| 36 | // 2) As in 2023, the game piece itself is offset in the robot and so we care |
| 37 | // which of left vs. right we are using. |
| 38 | // In situation (1), SignedRadii() should return false and the *Radius() |
| 39 | // functions should return a non-negative number (technically I think the |
| 40 | // math may work for negative numbers, but may have weird implications |
| 41 | // physically...). For (2) SignedRadii() |
| 42 | // should return true and the sign of the *Radius() functions will be |
| 43 | // respected by the line following code. |
| 44 | virtual bool SignedRadii() const = 0; |
James Kuszmaul | e093f51 | 2019-03-20 06:14:05 -0700 | [diff] [blame] | 45 | // The "radius" of the target--for y2019, we wanted to drive in so that a disc |
| 46 | // with radius r would hit the plane of the target at an offset of exactly r |
| 47 | // from the TargetPose--this is distinct from wanting the center of the |
| 48 | // robot to project straight onto the center of the target. |
| 49 | virtual double TargetRadius() const = 0; |
James Kuszmaul | 4f05d79 | 2023-03-05 14:14:18 -0800 | [diff] [blame^] | 50 | // the "radius" of the robot/game piece to place. |
| 51 | virtual double GamePieceRadius() const = 0; |
James Kuszmaul | 055fe76 | 2023-03-03 21:14:01 -0800 | [diff] [blame] | 52 | // Which direction we want the robot to drive to get to the target. |
| 53 | virtual Side DriveDirection() const = 0; |
James Kuszmaul | c016379 | 2023-03-04 14:13:31 -0800 | [diff] [blame] | 54 | // Indicates that the line following *must* drive to the currently selected |
| 55 | // target, regardless of any hysteresis we try to use to protect the driver. |
| 56 | virtual bool ForceReselectTarget() const = 0; |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 57 | }; |
| 58 | |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 59 | // Defines an interface for classes that provide field-global localization. |
| 60 | class LocalizerInterface { |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 61 | public: |
James Kuszmaul | 2db5d88 | 2020-02-16 16:49:26 -0800 | [diff] [blame] | 62 | typedef HybridEkf<double> Ekf; |
| 63 | typedef typename Ekf::StateIdx StateIdx; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 64 | |
James Kuszmaul | 531609d | 2020-02-18 17:12:23 -0800 | [diff] [blame] | 65 | virtual ~LocalizerInterface() {} |
| 66 | |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 67 | // Perform a single step of the filter, using the information that is |
| 68 | // available on every drivetrain iteration. |
| 69 | // The user should pass in the U that the real system experienced from the |
| 70 | // previous timestep until now; internally, any filters will first perform a |
| 71 | // prediction step to get the estimate at time now, and then will apply |
| 72 | // corrections based on the encoder/gyro/accelerometer values from time now. |
| 73 | // TODO(james): Consider letting implementations subscribe to the sensor |
| 74 | // values themselves, and then only passing in U. This requires more |
| 75 | // coordination on timing, however. |
| 76 | virtual void Update(const ::Eigen::Matrix<double, 2, 1> &U, |
| 77 | ::aos::monotonic_clock::time_point now, |
| 78 | double left_encoder, double right_encoder, |
James Kuszmaul | 3c5b4d3 | 2020-02-11 17:22:14 -0800 | [diff] [blame] | 79 | double gyro_rate, const Eigen::Vector3d &accel) = 0; |
James Kuszmaul | bcd96fc | 2020-10-12 20:29:32 -0700 | [diff] [blame] | 80 | virtual void Reset(aos::monotonic_clock::time_point t, |
| 81 | const Ekf::State &state) = 0; |
James Kuszmaul | ef428a0 | 2019-03-02 22:19:41 -0800 | [diff] [blame] | 82 | // Reset the absolute position of the estimator. |
James Kuszmaul | 074429e | 2019-03-23 16:01:49 -0700 | [diff] [blame] | 83 | virtual void ResetPosition(::aos::monotonic_clock::time_point t, double x, |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 84 | double y, double theta, double theta_uncertainty, |
| 85 | bool reset_theta) = 0; |
James Kuszmaul | 3c5b4d3 | 2020-02-11 17:22:14 -0800 | [diff] [blame] | 86 | flatbuffers::Offset<LocalizerState> PopulateStatus( |
| 87 | flatbuffers::FlatBufferBuilder *fbb) { |
| 88 | LocalizerState::Builder builder(*fbb); |
| 89 | builder.add_x(x()); |
| 90 | builder.add_y(y()); |
| 91 | builder.add_theta(theta()); |
| 92 | builder.add_left_velocity(left_velocity()); |
| 93 | builder.add_right_velocity(right_velocity()); |
| 94 | builder.add_left_encoder(left_encoder()); |
| 95 | builder.add_right_encoder(right_encoder()); |
| 96 | builder.add_left_voltage_error(left_voltage_error()); |
| 97 | builder.add_right_voltage_error(right_voltage_error()); |
| 98 | builder.add_angular_error(angular_error()); |
| 99 | builder.add_longitudinal_velocity_offset(longitudinal_velocity_offset()); |
| 100 | builder.add_lateral_velocity(lateral_velocity()); |
| 101 | return builder.Finish(); |
| 102 | } |
James Kuszmaul | 2db5d88 | 2020-02-16 16:49:26 -0800 | [diff] [blame] | 103 | virtual Ekf::State Xhat() const = 0; |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 104 | // There are several subtly different norms floating around for state |
James Kuszmaul | 3c5b4d3 | 2020-02-11 17:22:14 -0800 | [diff] [blame] | 105 | // 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] | 106 | // the values that most people care about. |
James Kuszmaul | 2db5d88 | 2020-02-16 16:49:26 -0800 | [diff] [blame] | 107 | double x() const { return Xhat()(StateIdx::kX); } |
| 108 | double y() const { return Xhat()(StateIdx::kY); } |
| 109 | double theta() const { return Xhat()(StateIdx::kTheta); } |
| 110 | double left_velocity() const { return Xhat()(StateIdx::kLeftVelocity); } |
| 111 | double right_velocity() const { return Xhat()(StateIdx::kRightVelocity); } |
| 112 | double left_encoder() const { return Xhat()(StateIdx::kLeftEncoder); } |
| 113 | double right_encoder() const { return Xhat()(StateIdx::kRightEncoder); } |
| 114 | double left_voltage_error() const { |
| 115 | return Xhat()(StateIdx::kLeftVoltageError); |
| 116 | } |
| 117 | double right_voltage_error() const { |
| 118 | return Xhat()(StateIdx::kRightVoltageError); |
| 119 | } |
| 120 | double angular_error() const { return Xhat()(StateIdx::kAngularError); } |
| 121 | double longitudinal_velocity_offset() const { |
| 122 | return Xhat()(StateIdx::kLongitudinalVelocityOffset); |
| 123 | } |
| 124 | double lateral_velocity() const { return Xhat()(StateIdx::kLateralVelocity); } |
| 125 | |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 126 | virtual TargetSelectorInterface *target_selector() = 0; |
| 127 | }; |
| 128 | |
| 129 | // A target selector, primarily for testing purposes, that just lets a user |
| 130 | // manually set the target selector state. |
| 131 | class TrivialTargetSelector : public TargetSelectorInterface { |
| 132 | public: |
James Kuszmaul | c29f457 | 2023-02-25 17:02:33 -0800 | [diff] [blame] | 133 | virtual ~TrivialTargetSelector() {} |
James Kuszmaul | c4bd161 | 2019-03-10 11:35:06 -0700 | [diff] [blame] | 134 | bool UpdateSelection(const ::Eigen::Matrix<double, 5, 1> &, double) override { |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 135 | return has_target_; |
| 136 | } |
| 137 | TypedPose<double> TargetPose() const override { return pose_; } |
James Kuszmaul | 4f05d79 | 2023-03-05 14:14:18 -0800 | [diff] [blame^] | 138 | bool SignedRadii() const override { return signed_radii_; } |
James Kuszmaul | e093f51 | 2019-03-20 06:14:05 -0700 | [diff] [blame] | 139 | double TargetRadius() const override { return target_radius_; } |
James Kuszmaul | 4f05d79 | 2023-03-05 14:14:18 -0800 | [diff] [blame^] | 140 | double GamePieceRadius() const override { return game_piece_radius_; } |
James Kuszmaul | 055fe76 | 2023-03-03 21:14:01 -0800 | [diff] [blame] | 141 | Side DriveDirection() const override { return drive_direction_; } |
James Kuszmaul | c016379 | 2023-03-04 14:13:31 -0800 | [diff] [blame] | 142 | bool ForceReselectTarget() const override { return force_reselect_; } |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 143 | |
| 144 | void set_pose(const TypedPose<double> &pose) { pose_ = pose; } |
James Kuszmaul | e093f51 | 2019-03-20 06:14:05 -0700 | [diff] [blame] | 145 | void set_target_radius(double radius) { target_radius_ = radius; } |
James Kuszmaul | 4f05d79 | 2023-03-05 14:14:18 -0800 | [diff] [blame^] | 146 | void set_game_piece_radius(double radius) { game_piece_radius_ = radius; } |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 147 | void set_has_target(bool has_target) { has_target_ = has_target; } |
James Kuszmaul | 055fe76 | 2023-03-03 21:14:01 -0800 | [diff] [blame] | 148 | void set_drive_direction(Side side) { drive_direction_ = side; } |
James Kuszmaul | c016379 | 2023-03-04 14:13:31 -0800 | [diff] [blame] | 149 | void set_force_reselect(bool force_reselect) { |
| 150 | force_reselect_ = force_reselect; |
| 151 | } |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 152 | bool has_target() const { return has_target_; } |
| 153 | |
| 154 | private: |
| 155 | bool has_target_ = true; |
James Kuszmaul | c016379 | 2023-03-04 14:13:31 -0800 | [diff] [blame] | 156 | bool force_reselect_ = false; |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 157 | TypedPose<double> pose_; |
James Kuszmaul | 4f05d79 | 2023-03-05 14:14:18 -0800 | [diff] [blame^] | 158 | bool signed_radii_ = false; |
James Kuszmaul | e093f51 | 2019-03-20 06:14:05 -0700 | [diff] [blame] | 159 | double target_radius_ = 0.0; |
James Kuszmaul | 4f05d79 | 2023-03-05 14:14:18 -0800 | [diff] [blame^] | 160 | double game_piece_radius_ = 0.0; |
James Kuszmaul | 055fe76 | 2023-03-03 21:14:01 -0800 | [diff] [blame] | 161 | Side drive_direction_ = Side::DONT_CARE; |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | // Uses the generic HybridEkf implementation to provide a basic field estimator. |
| 165 | // This provides no method for using cameras or the such to get global |
| 166 | // measurements and just assumes that you can dead-reckon perfectly. |
| 167 | class DeadReckonEkf : public LocalizerInterface { |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 168 | public: |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 169 | DeadReckonEkf(::aos::EventLoop *event_loop, |
| 170 | const DrivetrainConfig<double> &dt_config) |
| 171 | : ekf_(dt_config) { |
| 172 | event_loop->OnRun([this, event_loop]() { |
| 173 | ekf_.ResetInitialState(event_loop->monotonic_now(), Ekf::State::Zero(), |
| 174 | ekf_.P()); |
| 175 | }); |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 176 | target_selector_.set_has_target(false); |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 177 | } |
| 178 | |
James Kuszmaul | 531609d | 2020-02-18 17:12:23 -0800 | [diff] [blame] | 179 | virtual ~DeadReckonEkf() {} |
| 180 | |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 181 | void Update(const ::Eigen::Matrix<double, 2, 1> &U, |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 182 | ::aos::monotonic_clock::time_point now, double left_encoder, |
| 183 | double right_encoder, double gyro_rate, |
James Kuszmaul | 3c5b4d3 | 2020-02-11 17:22:14 -0800 | [diff] [blame] | 184 | const Eigen::Vector3d &accel) override { |
| 185 | ekf_.UpdateEncodersAndGyro(left_encoder, right_encoder, gyro_rate, U, accel, |
| 186 | now); |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 187 | } |
| 188 | |
James Kuszmaul | bcd96fc | 2020-10-12 20:29:32 -0700 | [diff] [blame] | 189 | void Reset(aos::monotonic_clock::time_point t, |
| 190 | const HybridEkf<double>::State &state) override { |
| 191 | ekf_.ResetInitialState(t, state, ekf_.P()); |
| 192 | } |
| 193 | |
James Kuszmaul | 074429e | 2019-03-23 16:01:49 -0700 | [diff] [blame] | 194 | void ResetPosition(::aos::monotonic_clock::time_point t, double x, double y, |
James Kuszmaul | 518640d | 2019-04-13 15:50:50 -0700 | [diff] [blame] | 195 | double theta, double /*theta_override*/, |
| 196 | bool /*reset_theta*/) override { |
James Kuszmaul | fedc461 | 2019-03-10 11:24:51 -0700 | [diff] [blame] | 197 | const double left_encoder = ekf_.X_hat(StateIdx::kLeftEncoder); |
| 198 | const double right_encoder = ekf_.X_hat(StateIdx::kRightEncoder); |
James Kuszmaul | 3c5b4d3 | 2020-02-11 17:22:14 -0800 | [diff] [blame] | 199 | ekf_.ResetInitialState(t, |
| 200 | (Ekf::State() << x, y, theta, left_encoder, 0, |
| 201 | right_encoder, 0, 0, 0, 0, 0, 0) |
| 202 | .finished(), |
James Kuszmaul | 074429e | 2019-03-23 16:01:49 -0700 | [diff] [blame] | 203 | ekf_.P()); |
James Kuszmaul | 2db5d88 | 2020-02-16 16:49:26 -0800 | [diff] [blame] | 204 | } |
James Kuszmaul | ef428a0 | 2019-03-02 22:19:41 -0800 | [diff] [blame] | 205 | |
James Kuszmaul | 2db5d88 | 2020-02-16 16:49:26 -0800 | [diff] [blame] | 206 | Ekf::State Xhat() const override { return ekf_.X_hat(); } |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 207 | |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 208 | TrivialTargetSelector *target_selector() override { |
| 209 | return &target_selector_; |
| 210 | } |
| 211 | |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 212 | private: |
| 213 | Ekf ekf_; |
James Kuszmaul | 5bc6fc9 | 2019-03-01 21:50:06 -0800 | [diff] [blame] | 214 | TrivialTargetSelector target_selector_; |
James Kuszmaul | 3431d62 | 2019-02-17 17:07:44 -0800 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | } // namespace drivetrain |
| 218 | } // namespace control_loops |
| 219 | } // namespace frc971 |
| 220 | |
| 221 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_FIELD_ESTIMATOR_H_ |