James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 1 | #include "y2020/control_loops/drivetrain/localizer.h" |
| 2 | |
| 3 | #include "y2020/constants.h" |
| 4 | |
| 5 | namespace y2020 { |
| 6 | namespace control_loops { |
| 7 | namespace drivetrain { |
| 8 | |
| 9 | namespace { |
| 10 | // Converts a flatbuffer TransformationMatrix to an Eigen matrix. Technically, |
| 11 | // this should be able to do a single memcpy, but the extra verbosity here seems |
| 12 | // appropriate. |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 13 | Eigen::Matrix<float, 4, 4> FlatbufferToTransformationMatrix( |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 14 | const frc971::vision::sift::TransformationMatrix &flatbuffer) { |
| 15 | CHECK_EQ(16u, CHECK_NOTNULL(flatbuffer.data())->size()); |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 16 | Eigen::Matrix<float, 4, 4> result; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 17 | result.setIdentity(); |
| 18 | for (int row = 0; row < 4; ++row) { |
| 19 | for (int col = 0; col < 4; ++col) { |
| 20 | result(row, col) = (*flatbuffer.data())[row * 4 + col]; |
| 21 | } |
| 22 | } |
| 23 | return result; |
| 24 | } |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 25 | |
James Kuszmaul | c6723cf | 2020-03-01 14:45:59 -0800 | [diff] [blame] | 26 | // Indices of the pis to use. |
| 27 | const std::array<std::string, 3> kPisToUse{"pi1", "pi2", "pi3"}; |
| 28 | |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 29 | // Calculates the pose implied by the camera target, just based on |
| 30 | // distance/heading components. |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 31 | Eigen::Vector3f CalculateImpliedPose(const Localizer::State &X, |
| 32 | const Eigen::Matrix4f &H_field_target, |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 33 | const Localizer::Pose &pose_robot_target) { |
| 34 | // This code overrides the pose sent directly from the camera code and |
| 35 | // effectively distills it down to just a distance + heading estimate, on |
| 36 | // the presumption that these signals will tend to be much lower noise and |
| 37 | // better-conditioned than other portions of the robot pose. |
| 38 | // As such, this code assumes that the current estimate of the robot |
| 39 | // heading is correct and then, given the heading from the camera to the |
| 40 | // target and the distance from the camera to the target, calculates the |
| 41 | // position that the robot would have to be at to make the current camera |
| 42 | // heading + distance correct. This X/Y implied robot position is then |
| 43 | // used as the measurement in the EKF, rather than the X/Y that is |
| 44 | // directly returned from the vision processing. This means that |
| 45 | // the cameras will not correct any drift in the robot heading estimate |
| 46 | // but will compensate for X/Y position in a way that prioritizes keeping |
| 47 | // an accurate distance + heading to the goal. |
| 48 | |
| 49 | // Calculate the heading to the robot in the target's coordinate frame. |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 50 | const float implied_heading_from_target = aos::math::NormalizeAngle( |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 51 | pose_robot_target.heading() + M_PI + X(Localizer::StateIdx::kTheta)); |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 52 | const float implied_distance = pose_robot_target.xy_norm(); |
| 53 | const Eigen::Vector4f robot_pose_in_target_frame( |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 54 | implied_distance * std::cos(implied_heading_from_target), |
| 55 | implied_distance * std::sin(implied_heading_from_target), 0, 1); |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 56 | const Eigen::Vector4f implied_pose = |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 57 | H_field_target * robot_pose_in_target_frame; |
| 58 | return implied_pose.topRows<3>(); |
| 59 | } |
| 60 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 61 | } // namespace |
| 62 | |
| 63 | Localizer::Localizer( |
| 64 | aos::EventLoop *event_loop, |
| 65 | const frc971::control_loops::drivetrain::DrivetrainConfig<double> |
| 66 | &dt_config) |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 67 | : event_loop_(event_loop), |
| 68 | dt_config_(dt_config), |
| 69 | ekf_(dt_config), |
| 70 | clock_offset_fetcher_( |
| 71 | event_loop_->MakeFetcher<aos::message_bridge::ServerStatistics>( |
| 72 | "/aos")) { |
James Kuszmaul | 91aa0cf | 2021-02-13 13:15:06 -0800 | [diff] [blame^] | 73 | // TODO(james): The down estimator has trouble handling situations where the |
| 74 | // robot is constantly wiggling but not actually moving much, and can cause |
| 75 | // drift when using accelerometer readings. |
| 76 | ekf_.set_ignore_accel(true); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 77 | // TODO(james): This doesn't really need to be a watcher; we could just use a |
| 78 | // fetcher for the superstructure status. |
| 79 | // This probably should be a Fetcher instead of a Watcher, but this |
| 80 | // seems simpler for the time being (although technically it should be |
| 81 | // possible to do everything we need to using just a Fetcher without |
| 82 | // even maintaining a separate buffer, but that seems overly cute). |
| 83 | event_loop_->MakeWatcher("/superstructure", |
| 84 | [this](const superstructure::Status &status) { |
| 85 | HandleSuperstructureStatus(status); |
| 86 | }); |
| 87 | |
James Kuszmaul | 286b428 | 2020-02-26 20:29:32 -0800 | [diff] [blame] | 88 | event_loop->OnRun([this, event_loop]() { |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 89 | ekf_.ResetInitialState(event_loop->monotonic_now(), |
| 90 | HybridEkf::State::Zero(), ekf_.P()); |
James Kuszmaul | 286b428 | 2020-02-26 20:29:32 -0800 | [diff] [blame] | 91 | }); |
| 92 | |
James Kuszmaul | c6723cf | 2020-03-01 14:45:59 -0800 | [diff] [blame] | 93 | for (const auto &pi : kPisToUse) { |
| 94 | image_fetchers_.emplace_back( |
| 95 | event_loop_->MakeFetcher<frc971::vision::sift::ImageMatchResult>( |
| 96 | "/" + pi + "/camera")); |
| 97 | } |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 98 | |
| 99 | target_selector_.set_has_target(false); |
| 100 | } |
| 101 | |
James Kuszmaul | bcd96fc | 2020-10-12 20:29:32 -0700 | [diff] [blame] | 102 | void Localizer::Reset( |
| 103 | aos::monotonic_clock::time_point t, |
| 104 | const frc971::control_loops::drivetrain::HybridEkf<double>::State &state) { |
| 105 | // Go through and clear out all of the fetchers so that we don't get behind. |
| 106 | for (auto &fetcher : image_fetchers_) { |
| 107 | fetcher.Fetch(); |
| 108 | } |
| 109 | ekf_.ResetInitialState(t, state.cast<float>(), ekf_.P()); |
| 110 | } |
| 111 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 112 | void Localizer::HandleSuperstructureStatus( |
| 113 | const y2020::control_loops::superstructure::Status &status) { |
| 114 | CHECK(status.has_turret()); |
| 115 | turret_data_.Push({event_loop_->monotonic_now(), status.turret()->position(), |
| 116 | status.turret()->velocity()}); |
| 117 | } |
| 118 | |
| 119 | Localizer::TurretData Localizer::GetTurretDataForTime( |
| 120 | aos::monotonic_clock::time_point time) { |
| 121 | if (turret_data_.empty()) { |
| 122 | return {}; |
| 123 | } |
| 124 | |
| 125 | aos::monotonic_clock::duration lowest_time_error = |
| 126 | aos::monotonic_clock::duration::max(); |
| 127 | TurretData best_data_match; |
| 128 | for (const auto &sample : turret_data_) { |
| 129 | const aos::monotonic_clock::duration time_error = |
| 130 | std::chrono::abs(sample.receive_time - time); |
| 131 | if (time_error < lowest_time_error) { |
| 132 | lowest_time_error = time_error; |
| 133 | best_data_match = sample; |
| 134 | } |
| 135 | } |
| 136 | return best_data_match; |
| 137 | } |
| 138 | |
James Kuszmaul | 06257f4 | 2020-05-09 15:40:09 -0700 | [diff] [blame] | 139 | void Localizer::Update(const Eigen::Matrix<double, 2, 1> &U, |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 140 | aos::monotonic_clock::time_point now, |
| 141 | double left_encoder, double right_encoder, |
| 142 | double gyro_rate, const Eigen::Vector3d &accel) { |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 143 | ekf_.UpdateEncodersAndGyro(left_encoder, right_encoder, gyro_rate, |
| 144 | U.cast<float>(), accel.cast<float>(), now); |
James Kuszmaul | c6723cf | 2020-03-01 14:45:59 -0800 | [diff] [blame] | 145 | for (size_t ii = 0; ii < kPisToUse.size(); ++ii) { |
| 146 | auto &image_fetcher = image_fetchers_[ii]; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 147 | while (image_fetcher.FetchNext()) { |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 148 | HandleImageMatch(kPisToUse[ii], *image_fetcher, now); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 149 | } |
| 150 | } |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void Localizer::HandleImageMatch( |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 154 | std::string_view pi, const frc971::vision::sift::ImageMatchResult &result, |
| 155 | aos::monotonic_clock::time_point now) { |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 156 | std::chrono::nanoseconds monotonic_offset(0); |
| 157 | clock_offset_fetcher_.Fetch(); |
| 158 | if (clock_offset_fetcher_.get() != nullptr) { |
| 159 | for (const auto connection : *clock_offset_fetcher_->connections()) { |
| 160 | if (connection->has_node() && connection->node()->has_name() && |
James Kuszmaul | c6723cf | 2020-03-01 14:45:59 -0800 | [diff] [blame] | 161 | connection->node()->name()->string_view() == pi) { |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 162 | monotonic_offset = |
| 163 | std::chrono::nanoseconds(connection->monotonic_offset()); |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | } |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 168 | aos::monotonic_clock::time_point capture_time( |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 169 | std::chrono::nanoseconds(result.image_monotonic_timestamp_ns()) - |
| 170 | monotonic_offset); |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame] | 171 | VLOG(1) << "Got monotonic offset of " |
| 172 | << aos::time::DurationInSeconds(monotonic_offset) |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 173 | << " when at time of " << now << " and capture time estimate of " |
| 174 | << capture_time; |
| 175 | if (capture_time > now) { |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame] | 176 | LOG(WARNING) << "Got camera frame from the future."; |
| 177 | return; |
| 178 | } |
James Kuszmaul | bcd96fc | 2020-10-12 20:29:32 -0700 | [diff] [blame] | 179 | if (!result.has_camera_calibration()) { |
| 180 | LOG(WARNING) << "Got camera frame without calibration data."; |
| 181 | return; |
| 182 | } |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 183 | // Per the ImageMatchResult specification, we can actually determine whether |
| 184 | // the camera is the turret camera just from the presence of the |
| 185 | // turret_extrinsics member. |
| 186 | const bool is_turret = result.camera_calibration()->has_turret_extrinsics(); |
| 187 | const TurretData turret_data = GetTurretDataForTime(capture_time); |
| 188 | // Ignore readings when the turret is spinning too fast, on the assumption |
| 189 | // that the odds of screwing up the time compensation are higher. |
| 190 | // Note that the current number here is chosen pretty arbitrarily--1 rad / sec |
| 191 | // seems reasonable, but may be unnecessarily low or high. |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 192 | constexpr float kMaxTurretVelocity = 1.0; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 193 | if (is_turret && std::abs(turret_data.velocity) > kMaxTurretVelocity) { |
| 194 | return; |
| 195 | } |
| 196 | CHECK(result.camera_calibration()->has_fixed_extrinsics()); |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 197 | const Eigen::Matrix<float, 4, 4> fixed_extrinsics = |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 198 | FlatbufferToTransformationMatrix( |
| 199 | *result.camera_calibration()->fixed_extrinsics()); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 200 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 201 | // Calculate the pose of the camera relative to the robot origin. |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 202 | Eigen::Matrix<float, 4, 4> H_robot_camera = fixed_extrinsics; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 203 | if (is_turret) { |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 204 | H_robot_camera = H_robot_camera * |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 205 | frc971::control_loops::TransformationMatrixForYaw<float>( |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 206 | turret_data.position) * |
| 207 | FlatbufferToTransformationMatrix( |
| 208 | *result.camera_calibration()->turret_extrinsics()); |
| 209 | } |
| 210 | |
| 211 | if (!result.has_camera_poses()) { |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | for (const frc971::vision::sift::CameraPose *vision_result : |
| 216 | *result.camera_poses()) { |
| 217 | if (!vision_result->has_camera_to_target() || |
| 218 | !vision_result->has_field_to_target()) { |
| 219 | continue; |
| 220 | } |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 221 | const Eigen::Matrix<float, 4, 4> H_camera_target = |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 222 | FlatbufferToTransformationMatrix(*vision_result->camera_to_target()); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 223 | |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 224 | const Eigen::Matrix<float, 4, 4> H_field_target = |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 225 | FlatbufferToTransformationMatrix(*vision_result->field_to_target()); |
| 226 | // Back out the robot position that is implied by the current camera |
| 227 | // reading. |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 228 | const Pose measured_pose(H_field_target * |
| 229 | (H_robot_camera * H_camera_target).inverse()); |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 230 | // This "Z" is the robot pose directly implied by the camera results. |
| 231 | // Currently, we do not actually use this result directly. However, it is |
| 232 | // kept around in case we want to quickly re-enable it. |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 233 | const Eigen::Matrix<float, 3, 1> Z(measured_pose.rel_pos().x(), |
| 234 | measured_pose.rel_pos().y(), |
| 235 | measured_pose.rel_theta()); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 236 | // Pose of the target in the robot frame. |
| 237 | Pose pose_robot_target(H_robot_camera * H_camera_target); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 238 | // TODO(james): Figure out how to properly handle calculating the |
| 239 | // noise. Currently, the values are deliberately tuned so that image updates |
| 240 | // will not be trusted overly much. In theory, we should probably also be |
| 241 | // populating some cross-correlation terms. |
| 242 | // Note that these are the noise standard deviations (they are squared below |
| 243 | // to get variances). |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 244 | Eigen::Matrix<float, 3, 1> noises(2.0, 2.0, 0.2); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 245 | // Augment the noise by the approximate rotational speed of the |
| 246 | // camera. This should help account for the fact that, while we are |
| 247 | // spinning, slight timing errors in the camera/turret data will tend to |
| 248 | // have mutch more drastic effects on the results. |
| 249 | noises *= 1.0 + std::abs((right_velocity() - left_velocity()) / |
| 250 | (2.0 * dt_config_.robot_radius) + |
| 251 | (is_turret ? turret_data.velocity : 0.0)); |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 252 | Eigen::Matrix3f R = Eigen::Matrix3f::Zero(); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 253 | R.diagonal() = noises.cwiseAbs2(); |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 254 | Eigen::Matrix<float, HybridEkf::kNOutputs, HybridEkf::kNStates> H; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 255 | H.setZero(); |
| 256 | H(0, StateIdx::kX) = 1; |
| 257 | H(1, StateIdx::kY) = 1; |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 258 | // This is currently set to zero because we ignore the heading implied by |
| 259 | // the camera. |
| 260 | H(2, StateIdx::kTheta) = 0; |
| 261 | VLOG(1) << "Pose implied by target: " << Z.transpose() |
| 262 | << " and current pose " << x() << ", " << y() << ", " << theta() |
| 263 | << " Heading/dist/skew implied by target: " |
| 264 | << pose_robot_target.ToHeadingDistanceSkew().transpose(); |
James Kuszmaul | add40ca | 2020-03-01 14:10:50 -0800 | [diff] [blame] | 265 | // If the heading is off by too much, assume that we got a false-positive |
| 266 | // and don't use the correction. |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 267 | if (std::abs(aos::math::DiffAngle<float>(theta(), Z(2))) > M_PI_2) { |
James Kuszmaul | add40ca | 2020-03-01 14:10:50 -0800 | [diff] [blame] | 268 | AOS_LOG(WARNING, "Dropped image match due to heading mismatch.\n"); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 269 | continue; |
James Kuszmaul | add40ca | 2020-03-01 14:10:50 -0800 | [diff] [blame] | 270 | } |
James Kuszmaul | 06257f4 | 2020-05-09 15:40:09 -0700 | [diff] [blame] | 271 | // In order to do the EKF correction, we determine the expected state based |
| 272 | // on the state at the time the image was captured; however, we insert the |
| 273 | // correction update itself at the current time. This is technically not |
| 274 | // quite correct, but saves substantial CPU usage by making it so that we |
| 275 | // don't have to constantly rewind the entire EKF history. |
| 276 | const std::optional<State> state_at_capture = |
| 277 | ekf_.LastStateBeforeTime(capture_time); |
| 278 | if (!state_at_capture.has_value()) { |
| 279 | AOS_LOG(WARNING, "Dropped image match due to age of image.\n"); |
| 280 | continue; |
| 281 | } |
| 282 | const Input U = ekf_.MostRecentInput(); |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 283 | // For the correction step, instead of passing in the measurement directly, |
| 284 | // we pass in (0, 0, 0) as the measurement and then for the expected |
| 285 | // measurement (Zhat) we calculate the error between the implied and actual |
| 286 | // poses. This doesn't affect any of the math, it just makes the code a bit |
| 287 | // more convenient to write given the Correct() interface we already have. |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 288 | ekf_.Correct( |
James Kuszmaul | 06257f4 | 2020-05-09 15:40:09 -0700 | [diff] [blame] | 289 | Eigen::Vector3f::Zero(), &U, {}, |
| 290 | [H, H_field_target, pose_robot_target, state_at_capture]( |
| 291 | const State &, const Input &) -> Eigen::Vector3f { |
| 292 | const Eigen::Vector3f Z = CalculateImpliedPose( |
| 293 | state_at_capture.value(), H_field_target, pose_robot_target); |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 294 | // Just in case we ever do encounter any, drop measurements if they |
| 295 | // have non-finite numbers. |
| 296 | if (!Z.allFinite()) { |
| 297 | AOS_LOG(WARNING, "Got measurement with infinites or NaNs.\n"); |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 298 | return Eigen::Vector3f::Zero(); |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 299 | } |
James Kuszmaul | 06257f4 | 2020-05-09 15:40:09 -0700 | [diff] [blame] | 300 | Eigen::Vector3f Zhat = H * state_at_capture.value() - Z; |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 301 | // Rewrap angle difference to put it back in range. Note that this |
| 302 | // component of the error is currently ignored (see definition of H |
| 303 | // above). |
| 304 | Zhat(2) = aos::math::NormalizeAngle(Zhat(2)); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 305 | // If the measurement implies that we are too far from the current |
| 306 | // estimate, then ignore it. |
| 307 | // Note that I am not entirely sure how much effect this actually has, |
| 308 | // because I primarily introduced it to make sure that any grossly |
| 309 | // invalid measurements get thrown out. |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 310 | if (Zhat.squaredNorm() > std::pow(10.0, 2)) { |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 311 | return Eigen::Vector3f::Zero(); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 312 | } |
| 313 | return Zhat; |
| 314 | }, |
James Kuszmaul | 06257f4 | 2020-05-09 15:40:09 -0700 | [diff] [blame] | 315 | [H](const State &) { return H; }, R, now); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
| 319 | } // namespace drivetrain |
| 320 | } // namespace control_loops |
| 321 | } // namespace y2020 |