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 | ed6fca4 | 2021-09-25 17:58:30 -0700 | [diff] [blame^] | 26 | // Offset to add to the pi's yaw in its extrinsics, to account for issues in the |
| 27 | // calibrated extrinsics. |
| 28 | constexpr double kTurretPiOffset = 0.0; |
| 29 | |
James Kuszmaul | c6723cf | 2020-03-01 14:45:59 -0800 | [diff] [blame] | 30 | // Indices of the pis to use. |
James Kuszmaul | 9c12812 | 2021-03-22 22:24:36 -0700 | [diff] [blame] | 31 | const std::array<std::string, 5> kPisToUse{"pi1", "pi2", "pi3", "pi4", "pi5"}; |
James Kuszmaul | c6723cf | 2020-03-01 14:45:59 -0800 | [diff] [blame] | 32 | |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 33 | // Calculates the pose implied by the camera target, just based on |
| 34 | // distance/heading components. |
James Kuszmaul | 5a46c8d | 2021-09-03 19:33:48 -0700 | [diff] [blame] | 35 | Eigen::Vector3f CalculateImpliedPose(const Eigen::Matrix4f &H_field_target, |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 36 | const Localizer::Pose &pose_robot_target) { |
| 37 | // This code overrides the pose sent directly from the camera code and |
| 38 | // effectively distills it down to just a distance + heading estimate, on |
| 39 | // the presumption that these signals will tend to be much lower noise and |
| 40 | // better-conditioned than other portions of the robot pose. |
| 41 | // As such, this code assumes that the current estimate of the robot |
| 42 | // heading is correct and then, given the heading from the camera to the |
| 43 | // target and the distance from the camera to the target, calculates the |
| 44 | // position that the robot would have to be at to make the current camera |
| 45 | // heading + distance correct. This X/Y implied robot position is then |
| 46 | // used as the measurement in the EKF, rather than the X/Y that is |
| 47 | // directly returned from the vision processing. This means that |
| 48 | // the cameras will not correct any drift in the robot heading estimate |
| 49 | // but will compensate for X/Y position in a way that prioritizes keeping |
| 50 | // an accurate distance + heading to the goal. |
| 51 | |
| 52 | // Calculate the heading to the robot in the target's coordinate frame. |
James Kuszmaul | 5a46c8d | 2021-09-03 19:33:48 -0700 | [diff] [blame] | 53 | // Reminder on what the numbers mean: |
| 54 | // rel_theta: The orientation of the target in the robot frame. |
| 55 | // heading: heading from the robot to the target in the robot frame. I.e., |
| 56 | // atan2(y, x) for x/y of the target in the robot frame. |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 57 | const float implied_heading_from_target = aos::math::NormalizeAngle( |
James Kuszmaul | 5a46c8d | 2021-09-03 19:33:48 -0700 | [diff] [blame] | 58 | M_PI - pose_robot_target.rel_theta() + pose_robot_target.heading()); |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 59 | const float implied_distance = pose_robot_target.xy_norm(); |
| 60 | const Eigen::Vector4f robot_pose_in_target_frame( |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 61 | implied_distance * std::cos(implied_heading_from_target), |
| 62 | implied_distance * std::sin(implied_heading_from_target), 0, 1); |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 63 | const Eigen::Vector4f implied_pose = |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 64 | H_field_target * robot_pose_in_target_frame; |
| 65 | return implied_pose.topRows<3>(); |
| 66 | } |
| 67 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 68 | } // namespace |
| 69 | |
| 70 | Localizer::Localizer( |
| 71 | aos::EventLoop *event_loop, |
| 72 | const frc971::control_loops::drivetrain::DrivetrainConfig<double> |
| 73 | &dt_config) |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 74 | : event_loop_(event_loop), |
| 75 | dt_config_(dt_config), |
| 76 | ekf_(dt_config), |
| 77 | clock_offset_fetcher_( |
| 78 | event_loop_->MakeFetcher<aos::message_bridge::ServerStatistics>( |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 79 | "/aos")), |
| 80 | debug_sender_( |
| 81 | event_loop_ |
| 82 | ->MakeSender<y2020::control_loops::drivetrain::LocalizerDebug>( |
| 83 | "/drivetrain")) { |
James Kuszmaul | 91aa0cf | 2021-02-13 13:15:06 -0800 | [diff] [blame] | 84 | // TODO(james): The down estimator has trouble handling situations where the |
| 85 | // robot is constantly wiggling but not actually moving much, and can cause |
| 86 | // drift when using accelerometer readings. |
| 87 | ekf_.set_ignore_accel(true); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 88 | // TODO(james): This doesn't really need to be a watcher; we could just use a |
| 89 | // fetcher for the superstructure status. |
| 90 | // This probably should be a Fetcher instead of a Watcher, but this |
| 91 | // seems simpler for the time being (although technically it should be |
| 92 | // possible to do everything we need to using just a Fetcher without |
| 93 | // even maintaining a separate buffer, but that seems overly cute). |
| 94 | event_loop_->MakeWatcher("/superstructure", |
| 95 | [this](const superstructure::Status &status) { |
| 96 | HandleSuperstructureStatus(status); |
| 97 | }); |
| 98 | |
James Kuszmaul | 286b428 | 2020-02-26 20:29:32 -0800 | [diff] [blame] | 99 | event_loop->OnRun([this, event_loop]() { |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 100 | ekf_.ResetInitialState(event_loop->monotonic_now(), |
| 101 | HybridEkf::State::Zero(), ekf_.P()); |
James Kuszmaul | 286b428 | 2020-02-26 20:29:32 -0800 | [diff] [blame] | 102 | }); |
| 103 | |
James Kuszmaul | c6723cf | 2020-03-01 14:45:59 -0800 | [diff] [blame] | 104 | for (const auto &pi : kPisToUse) { |
| 105 | image_fetchers_.emplace_back( |
| 106 | event_loop_->MakeFetcher<frc971::vision::sift::ImageMatchResult>( |
| 107 | "/" + pi + "/camera")); |
| 108 | } |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 109 | |
| 110 | target_selector_.set_has_target(false); |
| 111 | } |
| 112 | |
James Kuszmaul | bcd96fc | 2020-10-12 20:29:32 -0700 | [diff] [blame] | 113 | void Localizer::Reset( |
| 114 | aos::monotonic_clock::time_point t, |
| 115 | const frc971::control_loops::drivetrain::HybridEkf<double>::State &state) { |
| 116 | // Go through and clear out all of the fetchers so that we don't get behind. |
| 117 | for (auto &fetcher : image_fetchers_) { |
| 118 | fetcher.Fetch(); |
| 119 | } |
| 120 | ekf_.ResetInitialState(t, state.cast<float>(), ekf_.P()); |
| 121 | } |
| 122 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 123 | void Localizer::HandleSuperstructureStatus( |
| 124 | const y2020::control_loops::superstructure::Status &status) { |
| 125 | CHECK(status.has_turret()); |
| 126 | turret_data_.Push({event_loop_->monotonic_now(), status.turret()->position(), |
| 127 | status.turret()->velocity()}); |
| 128 | } |
| 129 | |
| 130 | Localizer::TurretData Localizer::GetTurretDataForTime( |
| 131 | aos::monotonic_clock::time_point time) { |
| 132 | if (turret_data_.empty()) { |
| 133 | return {}; |
| 134 | } |
| 135 | |
| 136 | aos::monotonic_clock::duration lowest_time_error = |
| 137 | aos::monotonic_clock::duration::max(); |
| 138 | TurretData best_data_match; |
| 139 | for (const auto &sample : turret_data_) { |
| 140 | const aos::monotonic_clock::duration time_error = |
| 141 | std::chrono::abs(sample.receive_time - time); |
| 142 | if (time_error < lowest_time_error) { |
| 143 | lowest_time_error = time_error; |
| 144 | best_data_match = sample; |
| 145 | } |
| 146 | } |
| 147 | return best_data_match; |
| 148 | } |
| 149 | |
James Kuszmaul | 06257f4 | 2020-05-09 15:40:09 -0700 | [diff] [blame] | 150 | void Localizer::Update(const Eigen::Matrix<double, 2, 1> &U, |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 151 | aos::monotonic_clock::time_point now, |
| 152 | double left_encoder, double right_encoder, |
| 153 | double gyro_rate, const Eigen::Vector3d &accel) { |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 154 | ekf_.UpdateEncodersAndGyro(left_encoder, right_encoder, gyro_rate, |
| 155 | U.cast<float>(), accel.cast<float>(), now); |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 156 | auto builder = debug_sender_.MakeBuilder(); |
| 157 | aos::SizedArray<flatbuffers::Offset<ImageMatchDebug>, 25> debug_offsets; |
James Kuszmaul | c6723cf | 2020-03-01 14:45:59 -0800 | [diff] [blame] | 158 | for (size_t ii = 0; ii < kPisToUse.size(); ++ii) { |
| 159 | auto &image_fetcher = image_fetchers_[ii]; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 160 | while (image_fetcher.FetchNext()) { |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 161 | const auto offsets = HandleImageMatch(ii, kPisToUse[ii], *image_fetcher, |
| 162 | now, builder.fbb()); |
| 163 | for (const auto offset : offsets) { |
| 164 | debug_offsets.push_back(offset); |
| 165 | } |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 166 | } |
| 167 | } |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 168 | const auto vector_offset = |
| 169 | builder.fbb()->CreateVector(debug_offsets.data(), debug_offsets.size()); |
| 170 | LocalizerDebug::Builder debug_builder = builder.MakeBuilder<LocalizerDebug>(); |
| 171 | debug_builder.add_matches(vector_offset); |
| 172 | CHECK(builder.Send(debug_builder.Finish())); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 173 | } |
| 174 | |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 175 | aos::SizedArray<flatbuffers::Offset<ImageMatchDebug>, 5> |
| 176 | Localizer::HandleImageMatch( |
| 177 | size_t camera_index, std::string_view pi, |
| 178 | const frc971::vision::sift::ImageMatchResult &result, |
| 179 | aos::monotonic_clock::time_point now, flatbuffers::FlatBufferBuilder *fbb) { |
| 180 | aos::SizedArray<flatbuffers::Offset<ImageMatchDebug>, 5> debug_offsets; |
| 181 | |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 182 | std::chrono::nanoseconds monotonic_offset(0); |
| 183 | clock_offset_fetcher_.Fetch(); |
| 184 | if (clock_offset_fetcher_.get() != nullptr) { |
| 185 | for (const auto connection : *clock_offset_fetcher_->connections()) { |
| 186 | if (connection->has_node() && connection->node()->has_name() && |
James Kuszmaul | c6723cf | 2020-03-01 14:45:59 -0800 | [diff] [blame] | 187 | connection->node()->name()->string_view() == pi) { |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 188 | monotonic_offset = |
| 189 | std::chrono::nanoseconds(connection->monotonic_offset()); |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | } |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 194 | aos::monotonic_clock::time_point capture_time( |
James Kuszmaul | 958b21e | 2020-02-26 21:51:40 -0800 | [diff] [blame] | 195 | std::chrono::nanoseconds(result.image_monotonic_timestamp_ns()) - |
| 196 | monotonic_offset); |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame] | 197 | VLOG(1) << "Got monotonic offset of " |
| 198 | << aos::time::DurationInSeconds(monotonic_offset) |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 199 | << " when at time of " << now << " and capture time estimate of " |
| 200 | << capture_time; |
| 201 | if (capture_time > now) { |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame] | 202 | LOG(WARNING) << "Got camera frame from the future."; |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 203 | ImageMatchDebug::Builder builder(*fbb); |
| 204 | builder.add_camera(camera_index); |
| 205 | builder.add_accepted(false); |
| 206 | builder.add_rejection_reason(RejectionReason::IMAGE_FROM_FUTURE); |
| 207 | debug_offsets.push_back(builder.Finish()); |
| 208 | return debug_offsets; |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame] | 209 | } |
James Kuszmaul | bcd96fc | 2020-10-12 20:29:32 -0700 | [diff] [blame] | 210 | if (!result.has_camera_calibration()) { |
| 211 | LOG(WARNING) << "Got camera frame without calibration data."; |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 212 | ImageMatchDebug::Builder builder(*fbb); |
| 213 | builder.add_camera(camera_index); |
| 214 | builder.add_accepted(false); |
| 215 | builder.add_rejection_reason(RejectionReason::NO_CALIBRATION); |
| 216 | debug_offsets.push_back(builder.Finish()); |
| 217 | return debug_offsets; |
James Kuszmaul | bcd96fc | 2020-10-12 20:29:32 -0700 | [diff] [blame] | 218 | } |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 219 | // Per the ImageMatchResult specification, we can actually determine whether |
| 220 | // the camera is the turret camera just from the presence of the |
| 221 | // turret_extrinsics member. |
| 222 | const bool is_turret = result.camera_calibration()->has_turret_extrinsics(); |
| 223 | const TurretData turret_data = GetTurretDataForTime(capture_time); |
| 224 | // Ignore readings when the turret is spinning too fast, on the assumption |
| 225 | // that the odds of screwing up the time compensation are higher. |
| 226 | // Note that the current number here is chosen pretty arbitrarily--1 rad / sec |
| 227 | // seems reasonable, but may be unnecessarily low or high. |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 228 | constexpr float kMaxTurretVelocity = 1.0; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 229 | if (is_turret && std::abs(turret_data.velocity) > kMaxTurretVelocity) { |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 230 | ImageMatchDebug::Builder builder(*fbb); |
| 231 | builder.add_camera(camera_index); |
| 232 | builder.add_accepted(false); |
| 233 | builder.add_rejection_reason(RejectionReason::TURRET_TOO_FAST); |
| 234 | debug_offsets.push_back(builder.Finish()); |
| 235 | return debug_offsets; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 236 | } |
| 237 | CHECK(result.camera_calibration()->has_fixed_extrinsics()); |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 238 | const Eigen::Matrix<float, 4, 4> fixed_extrinsics = |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 239 | FlatbufferToTransformationMatrix( |
| 240 | *result.camera_calibration()->fixed_extrinsics()); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 241 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 242 | // Calculate the pose of the camera relative to the robot origin. |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 243 | Eigen::Matrix<float, 4, 4> H_robot_camera = fixed_extrinsics; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 244 | if (is_turret) { |
James Kuszmaul | c51dbfe | 2020-02-23 15:39:00 -0800 | [diff] [blame] | 245 | H_robot_camera = H_robot_camera * |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 246 | frc971::control_loops::TransformationMatrixForYaw<float>( |
James Kuszmaul | ed6fca4 | 2021-09-25 17:58:30 -0700 | [diff] [blame^] | 247 | turret_data.position + kTurretPiOffset) * |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 248 | FlatbufferToTransformationMatrix( |
| 249 | *result.camera_calibration()->turret_extrinsics()); |
| 250 | } |
| 251 | |
| 252 | if (!result.has_camera_poses()) { |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 253 | ImageMatchDebug::Builder builder(*fbb); |
| 254 | builder.add_camera(camera_index); |
| 255 | builder.add_accepted(false); |
| 256 | builder.add_rejection_reason(RejectionReason::NO_RESULTS); |
| 257 | debug_offsets.push_back(builder.Finish()); |
| 258 | return debug_offsets; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 259 | } |
| 260 | |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 261 | int index = -1; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 262 | for (const frc971::vision::sift::CameraPose *vision_result : |
| 263 | *result.camera_poses()) { |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 264 | ++index; |
| 265 | |
| 266 | ImageMatchDebug::Builder builder(*fbb); |
| 267 | builder.add_camera(camera_index); |
| 268 | builder.add_pose_index(index); |
| 269 | builder.add_local_image_capture_time_ns(result.image_monotonic_timestamp_ns()); |
| 270 | builder.add_roborio_image_capture_time_ns( |
| 271 | capture_time.time_since_epoch().count()); |
| 272 | builder.add_image_age_sec(aos::time::DurationInSeconds(now - capture_time)); |
| 273 | |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 274 | if (!vision_result->has_camera_to_target() || |
| 275 | !vision_result->has_field_to_target()) { |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 276 | builder.add_accepted(false); |
| 277 | builder.add_rejection_reason(RejectionReason::NO_TRANSFORMS); |
| 278 | debug_offsets.push_back(builder.Finish()); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 279 | continue; |
| 280 | } |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 281 | const Eigen::Matrix<float, 4, 4> H_camera_target = |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 282 | FlatbufferToTransformationMatrix(*vision_result->camera_to_target()); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 283 | |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 284 | const Eigen::Matrix<float, 4, 4> H_field_target = |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 285 | FlatbufferToTransformationMatrix(*vision_result->field_to_target()); |
| 286 | // Back out the robot position that is implied by the current camera |
James Kuszmaul | 715e793 | 2021-04-05 20:45:57 -0700 | [diff] [blame] | 287 | // reading. Note that the Pose object ignores any roll/pitch components, so |
| 288 | // if the camera's extrinsics for pitch/roll are off, this should just |
| 289 | // ignore it. |
James Kuszmaul | aca8878 | 2021-04-24 16:48:45 -0700 | [diff] [blame] | 290 | const Pose measured_camera_pose(H_field_target * H_camera_target.inverse()); |
| 291 | // Calculate the camera-to-robot transformation matrix ignoring the |
| 292 | // pitch/roll of the camera. |
| 293 | // TODO(james): This could probably be made a bit more efficient, but I |
| 294 | // don't think this is anywhere near our bottleneck currently. |
| 295 | const Eigen::Matrix<float, 4, 4> H_camera_robot_stripped = |
| 296 | Pose(H_robot_camera).AsTransformationMatrix().inverse(); |
| 297 | const Pose measured_pose(measured_camera_pose.AsTransformationMatrix() * |
| 298 | H_camera_robot_stripped); |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 299 | // This "Z" is the robot pose directly implied by the camera results. |
| 300 | // Currently, we do not actually use this result directly. However, it is |
| 301 | // kept around in case we want to quickly re-enable it. |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 302 | const Eigen::Matrix<float, 3, 1> Z(measured_pose.rel_pos().x(), |
| 303 | measured_pose.rel_pos().y(), |
| 304 | measured_pose.rel_theta()); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 305 | // Pose of the target in the robot frame. |
James Kuszmaul | 715e793 | 2021-04-05 20:45:57 -0700 | [diff] [blame] | 306 | // Note that we use measured_pose's transformation matrix rather than just |
| 307 | // doing H_robot_camera * H_camera_target because measured_pose ignores |
| 308 | // pitch/roll. |
| 309 | Pose pose_robot_target(measured_pose.AsTransformationMatrix().inverse() * |
| 310 | H_field_target); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 311 | // TODO(james): Figure out how to properly handle calculating the |
| 312 | // noise. Currently, the values are deliberately tuned so that image updates |
| 313 | // will not be trusted overly much. In theory, we should probably also be |
| 314 | // populating some cross-correlation terms. |
| 315 | // Note that these are the noise standard deviations (they are squared below |
| 316 | // to get variances). |
James Kuszmaul | ed6fca4 | 2021-09-25 17:58:30 -0700 | [diff] [blame^] | 317 | Eigen::Matrix<float, 3, 1> noises(2.0, 2.0, 0.5); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 318 | // Augment the noise by the approximate rotational speed of the |
| 319 | // camera. This should help account for the fact that, while we are |
| 320 | // spinning, slight timing errors in the camera/turret data will tend to |
| 321 | // have mutch more drastic effects on the results. |
| 322 | noises *= 1.0 + std::abs((right_velocity() - left_velocity()) / |
| 323 | (2.0 * dt_config_.robot_radius) + |
| 324 | (is_turret ? turret_data.velocity : 0.0)); |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 325 | Eigen::Matrix3f R = Eigen::Matrix3f::Zero(); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 326 | R.diagonal() = noises.cwiseAbs2(); |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 327 | Eigen::Matrix<float, HybridEkf::kNOutputs, HybridEkf::kNStates> H; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 328 | H.setZero(); |
| 329 | H(0, StateIdx::kX) = 1; |
| 330 | H(1, StateIdx::kY) = 1; |
James Kuszmaul | ed6fca4 | 2021-09-25 17:58:30 -0700 | [diff] [blame^] | 331 | H(2, StateIdx::kTheta) = 1; |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 332 | VLOG(1) << "Pose implied by target: " << Z.transpose() |
| 333 | << " and current pose " << x() << ", " << y() << ", " << theta() |
| 334 | << " Heading/dist/skew implied by target: " |
| 335 | << pose_robot_target.ToHeadingDistanceSkew().transpose(); |
James Kuszmaul | add40ca | 2020-03-01 14:10:50 -0800 | [diff] [blame] | 336 | // If the heading is off by too much, assume that we got a false-positive |
| 337 | // and don't use the correction. |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 338 | 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] | 339 | AOS_LOG(WARNING, "Dropped image match due to heading mismatch.\n"); |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 340 | builder.add_accepted(false); |
| 341 | builder.add_rejection_reason(RejectionReason::HIGH_THETA_DIFFERENCE); |
| 342 | debug_offsets.push_back(builder.Finish()); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 343 | continue; |
James Kuszmaul | add40ca | 2020-03-01 14:10:50 -0800 | [diff] [blame] | 344 | } |
James Kuszmaul | 06257f4 | 2020-05-09 15:40:09 -0700 | [diff] [blame] | 345 | // In order to do the EKF correction, we determine the expected state based |
| 346 | // on the state at the time the image was captured; however, we insert the |
| 347 | // correction update itself at the current time. This is technically not |
| 348 | // quite correct, but saves substantial CPU usage by making it so that we |
| 349 | // don't have to constantly rewind the entire EKF history. |
| 350 | const std::optional<State> state_at_capture = |
| 351 | ekf_.LastStateBeforeTime(capture_time); |
| 352 | if (!state_at_capture.has_value()) { |
| 353 | AOS_LOG(WARNING, "Dropped image match due to age of image.\n"); |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 354 | builder.add_accepted(false); |
| 355 | builder.add_rejection_reason(RejectionReason::IMAGE_TOO_OLD); |
| 356 | debug_offsets.push_back(builder.Finish()); |
James Kuszmaul | 06257f4 | 2020-05-09 15:40:09 -0700 | [diff] [blame] | 357 | continue; |
| 358 | } |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 359 | |
| 360 | builder.add_implied_robot_x(Z(0)); |
| 361 | builder.add_implied_robot_y(Z(1)); |
| 362 | builder.add_implied_robot_theta(Z(2)); |
| 363 | |
| 364 | // Turret is zero when pointed backwards. |
| 365 | builder.add_implied_turret_goal( |
| 366 | aos::math::NormalizeAngle(M_PI + pose_robot_target.heading())); |
| 367 | |
| 368 | std::optional<RejectionReason> correction_rejection; |
James Kuszmaul | 06257f4 | 2020-05-09 15:40:09 -0700 | [diff] [blame] | 369 | const Input U = ekf_.MostRecentInput(); |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 370 | // For the correction step, instead of passing in the measurement directly, |
| 371 | // we pass in (0, 0, 0) as the measurement and then for the expected |
| 372 | // measurement (Zhat) we calculate the error between the implied and actual |
| 373 | // poses. This doesn't affect any of the math, it just makes the code a bit |
| 374 | // more convenient to write given the Correct() interface we already have. |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 375 | // Note: If we start going back to doing back-in-time rewinds, then we can't |
| 376 | // get away with passing things by reference. |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 377 | ekf_.Correct( |
James Kuszmaul | 06257f4 | 2020-05-09 15:40:09 -0700 | [diff] [blame] | 378 | Eigen::Vector3f::Zero(), &U, {}, |
James Kuszmaul | ed6fca4 | 2021-09-25 17:58:30 -0700 | [diff] [blame^] | 379 | [H, H_field_target, pose_robot_target, state_at_capture, Z, &correction_rejection]( |
James Kuszmaul | 06257f4 | 2020-05-09 15:40:09 -0700 | [diff] [blame] | 380 | const State &, const Input &) -> Eigen::Vector3f { |
James Kuszmaul | ed6fca4 | 2021-09-25 17:58:30 -0700 | [diff] [blame^] | 381 | // TODO(james): Figure out how to use the implied pose for... |
| 382 | const Eigen::Vector3f Z_implied = |
James Kuszmaul | 5a46c8d | 2021-09-03 19:33:48 -0700 | [diff] [blame] | 383 | CalculateImpliedPose(H_field_target, pose_robot_target); |
James Kuszmaul | ed6fca4 | 2021-09-25 17:58:30 -0700 | [diff] [blame^] | 384 | (void)Z_implied; |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 385 | // Just in case we ever do encounter any, drop measurements if they |
| 386 | // have non-finite numbers. |
| 387 | if (!Z.allFinite()) { |
| 388 | AOS_LOG(WARNING, "Got measurement with infinites or NaNs.\n"); |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 389 | correction_rejection = RejectionReason::NONFINITE_MEASUREMENT; |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 390 | return Eigen::Vector3f::Zero(); |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 391 | } |
James Kuszmaul | 06257f4 | 2020-05-09 15:40:09 -0700 | [diff] [blame] | 392 | Eigen::Vector3f Zhat = H * state_at_capture.value() - Z; |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 393 | // Rewrap angle difference to put it back in range. Note that this |
| 394 | // component of the error is currently ignored (see definition of H |
| 395 | // above). |
| 396 | Zhat(2) = aos::math::NormalizeAngle(Zhat(2)); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 397 | // If the measurement implies that we are too far from the current |
| 398 | // estimate, then ignore it. |
| 399 | // Note that I am not entirely sure how much effect this actually has, |
| 400 | // because I primarily introduced it to make sure that any grossly |
| 401 | // invalid measurements get thrown out. |
James Kuszmaul | 66efe83 | 2020-03-16 19:38:33 -0700 | [diff] [blame] | 402 | if (Zhat.squaredNorm() > std::pow(10.0, 2)) { |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 403 | correction_rejection = RejectionReason::CORRECTION_TOO_LARGE; |
James Kuszmaul | d478f87 | 2020-03-16 20:54:27 -0700 | [diff] [blame] | 404 | return Eigen::Vector3f::Zero(); |
James Kuszmaul | 58cb1fe | 2020-03-07 16:18:59 -0800 | [diff] [blame] | 405 | } |
| 406 | return Zhat; |
| 407 | }, |
James Kuszmaul | 06257f4 | 2020-05-09 15:40:09 -0700 | [diff] [blame] | 408 | [H](const State &) { return H; }, R, now); |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 409 | if (correction_rejection) { |
| 410 | builder.add_accepted(false); |
| 411 | builder.add_rejection_reason(*correction_rejection); |
| 412 | } else { |
| 413 | builder.add_accepted(true); |
| 414 | } |
| 415 | debug_offsets.push_back(builder.Finish()); |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 416 | } |
James Kuszmaul | 5ff8a86 | 2021-09-25 17:29:43 -0700 | [diff] [blame] | 417 | return debug_offsets; |
James Kuszmaul | 5398fae | 2020-02-17 16:44:03 -0800 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | } // namespace drivetrain |
| 421 | } // namespace control_loops |
| 422 | } // namespace y2020 |