James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 1 | #include "y2024/localizer/localizer.h" |
| 2 | |
| 3 | #include "gflags/gflags.h" |
| 4 | |
| 5 | #include "aos/containers/sized_array.h" |
| 6 | #include "frc971/control_loops/drivetrain/localizer_generated.h" |
| 7 | #include "frc971/control_loops/pose.h" |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 8 | #include "frc971/math/flatbuffers_matrix.h" |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 9 | #include "frc971/vision/target_map_utils.h" |
| 10 | #include "y2024/constants.h" |
| 11 | |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 12 | DEFINE_double(max_pose_error, 1e-5, |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 13 | "Throw out target poses with a higher pose error than this"); |
Austin Schuh | 98b732d | 2024-03-22 19:59:09 -0700 | [diff] [blame] | 14 | DEFINE_double(max_distortion, 1000.0, ""); |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 15 | DEFINE_double( |
| 16 | max_pose_error_ratio, 0.4, |
| 17 | "Throw out target poses with a higher pose error ratio than this"); |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 18 | DEFINE_double(distortion_noise_scalar, 4.0, |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 19 | "Scale the target pose distortion factor by this when computing " |
| 20 | "the noise."); |
| 21 | DEFINE_double( |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 22 | max_implied_yaw_error, 5.0, |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 23 | "Reject target poses that imply a robot yaw of more than this many degrees " |
| 24 | "off from our estimate."); |
| 25 | DEFINE_double( |
| 26 | max_implied_teleop_yaw_error, 30.0, |
| 27 | "Reject target poses that imply a robot yaw of more than this many degrees " |
| 28 | "off from our estimate."); |
| 29 | DEFINE_double(max_distance_to_target, 5.0, |
| 30 | "Reject target poses that have a 3d distance of more than this " |
| 31 | "many meters."); |
James Kuszmaul | 592055e | 2024-03-23 20:12:59 -0700 | [diff] [blame] | 32 | DEFINE_double(max_auto_image_robot_speed, 5.0, |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 33 | "Reject target poses when the robot is travelling faster than " |
| 34 | "this speed in auto."); |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 35 | DEFINE_bool( |
James Kuszmaul | 592055e | 2024-03-23 20:12:59 -0700 | [diff] [blame] | 36 | do_xytheta_corrections, false, |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 37 | "If set, uses the x/y/theta corrector rather than a heading/distance/skew " |
| 38 | "one. This is better conditioned currently, but is theoretically worse due " |
| 39 | "to not capturing noise effectively."); |
| 40 | DEFINE_bool( |
| 41 | always_use_extra_tags, true, |
| 42 | "If set, we will use the \"deweighted\" tags even in auto mode (this " |
| 43 | "affects april tags whose field positions we do not trust as much)."); |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 44 | |
| 45 | namespace y2024::localizer { |
| 46 | namespace { |
| 47 | constexpr std::array<std::string_view, Localizer::kNumCameras> |
Maxwell Henderson | 3279bc5 | 2024-03-01 09:50:53 -0800 | [diff] [blame] | 48 | kDetectionChannels{"/orin1/camera0", "/orin1/camera1", "/imu/camera0", |
| 49 | "/imu/camera1"}; |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 50 | |
| 51 | size_t CameraIndexForName(std::string_view name) { |
| 52 | for (size_t index = 0; index < kDetectionChannels.size(); ++index) { |
| 53 | if (name == kDetectionChannels.at(index)) { |
| 54 | return index; |
| 55 | } |
| 56 | } |
| 57 | LOG(FATAL) << "No camera channel named " << name; |
| 58 | } |
| 59 | |
| 60 | std::map<uint64_t, Localizer::Transform> GetTargetLocations( |
| 61 | const Constants &constants) { |
| 62 | CHECK(constants.has_common()); |
| 63 | CHECK(constants.common()->has_target_map()); |
| 64 | CHECK(constants.common()->target_map()->has_target_poses()); |
| 65 | std::map<uint64_t, Localizer::Transform> transforms; |
| 66 | for (const frc971::vision::TargetPoseFbs *target : |
| 67 | *constants.common()->target_map()->target_poses()) { |
| 68 | CHECK(target->has_id()); |
| 69 | CHECK(target->has_position()); |
| 70 | CHECK(target->has_orientation()); |
| 71 | CHECK_EQ(0u, transforms.count(target->id())); |
| 72 | transforms[target->id()] = PoseToTransform(target); |
| 73 | } |
| 74 | return transforms; |
| 75 | } |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 76 | |
| 77 | // Returns the "nominal" covariance of localizer---i.e., the values to which it |
| 78 | // tends to converge during normal operation. By initializing the localizer's |
| 79 | // covariance this way, we reduce the likelihood that the first few corrections |
| 80 | // we receive will result in insane jumps in robot state. |
| 81 | Eigen::Matrix<double, Localizer::HybridEkf::kNStates, |
| 82 | Localizer::HybridEkf::kNStates> |
| 83 | NominalCovariance() { |
| 84 | Eigen::Matrix<double, Localizer::HybridEkf::kNStates, |
| 85 | Localizer::HybridEkf::kNStates> |
| 86 | P_transpose; |
| 87 | // Grabbed from when the robot was in a steady-state. |
James Kuszmaul | 592055e | 2024-03-23 20:12:59 -0700 | [diff] [blame] | 88 | P_transpose << 0.00478504226469438, 0.000253940126278529, |
| 89 | -0.000162526741742492, 2.25403185759796e-09, 0.0101734987442698, |
| 90 | 2.25403195618803e-09, 0.0101734987442698, 0.0253922208811703, |
| 91 | 0.0253922210268363, -2.21692792749728e-10, 1.30552506376491e-05, |
| 92 | 8.24314992005184e-07, 0.000253940126278532, 0.00189751717312843, |
| 93 | 0.000513974713526466, 2.03445653416419e-10, 0.00091777414692514, |
| 94 | 2.03445505573468e-10, 0.00091777414692514, 0.002190445323373, |
| 95 | 0.00219044511582939, 3.32473307499143e-10, 1.45178014834701e-06, |
| 96 | 1.71788107973058e-05, -0.000162526741742491, 0.000513974713526467, |
| 97 | 0.000241235997378754, -2.30353529071927e-12, -1.03627077991455e-05, |
| 98 | -2.30350039899681e-12, -1.03627077991157e-05, -6.36337811958761e-06, |
| 99 | -6.62065263890835e-06, 1.24447423005307e-09, -1.228397466134e-07, |
| 100 | 2.45695800192927e-06, 2.25403185760077e-09, 2.0344565341686e-10, |
| 101 | -2.30353529071687e-12, 4.99964876555835e-09, 4.09452976434092e-08, |
| 102 | -1.11086080247582e-15, 4.09452976433419e-08, 1.61945884581856e-07, |
| 103 | 1.61950413812579e-07, 4.58556491207338e-08, -1.0257731581937e-12, |
| 104 | 3.0118336328036e-13, 0.0101734987442698, 0.000917774146925141, |
| 105 | -1.03627077991456e-05, 4.09452976433247e-08, 0.186711669156372, |
| 106 | 4.09452978206736e-08, 0.186711669156351, 0.747606782854604, |
| 107 | 0.747606783311591, -3.98476625129118e-10, 4.53292935526394e-05, |
| 108 | 1.34809505728832e-06, 2.2540319561823e-09, 2.03445505573217e-10, |
| 109 | -2.30350039893596e-12, -1.11086077014401e-15, 4.09452978206229e-08, |
| 110 | 4.99964876557609e-09, 4.0945297820556e-08, 1.61950414014674e-07, |
| 111 | 1.61945884988215e-07, -4.58556492982177e-08, -1.02577287448067e-12, |
| 112 | 3.01180296453645e-13, 0.0101734987442698, 0.000917774146925141, |
| 113 | -1.03627077991158e-05, 4.09452976433153e-08, 0.186711669156351, |
| 114 | 4.09452978206613e-08, 0.186711669156372, 0.747606782852986, |
| 115 | 0.747606783313209, -3.98476449084643e-10, 4.53292935526394e-05, |
| 116 | 1.34809505728832e-06, 0.0253922208811701, 0.00219044532337299, |
| 117 | -6.36337811958279e-06, 1.61945884583411e-07, 0.747606782854602, |
| 118 | 1.61950414014798e-07, 0.747606782852984, 4.36530695987946, |
| 119 | 4.17234874741425, 7.37989263565032e-07, 0.000112905097332305, |
| 120 | 1.0761727407346e-06, 0.025392221026836, 0.00219044511582942, |
| 121 | -6.62065263891535e-06, 1.61950413812353e-07, 0.747606783311594, |
| 122 | 1.61945884987625e-07, 0.747606783313212, 4.17234874741427, |
| 123 | 4.36530696204959, -7.39350829913324e-07, 0.000112905097765367, |
| 124 | 1.07616825738023e-06, -2.21692793550929e-10, 3.32473307500738e-10, |
| 125 | 1.24447423005688e-09, 4.58556491207295e-08, -3.98476620685685e-10, |
| 126 | -4.58556492984426e-08, -3.98476445724907e-10, 7.3798926341289e-07, |
| 127 | -7.39350829974392e-07, 0.212257282137077, -6.38021734059486e-13, |
| 128 | 6.89673203238e-12, 1.30552506376492e-05, 1.451780148347e-06, |
| 129 | -1.22839746613403e-07, -1.02577315819363e-12, 4.53292935526395e-05, |
| 130 | -1.02577287448185e-12, 4.53292935526395e-05, 0.000112905097332305, |
| 131 | 0.000112905097765368, -6.38021733687597e-13, 4.99487202342848e-05, |
| 132 | 7.45706935797857e-09, 8.24314992005172e-07, 1.7178810797306e-05, |
| 133 | 2.45695800192931e-06, 3.01183363281006e-13, 1.34809505728833e-06, |
| 134 | 3.01180296453493e-13, 1.34809505728833e-06, 1.07617274073465e-06, |
| 135 | 1.07616825738027e-06, 6.89673203233812e-12, 7.45706935797858e-09, |
| 136 | 4.97065161286885e-05; |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 137 | return P_transpose.transpose(); |
| 138 | } |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 139 | } // namespace |
| 140 | |
| 141 | std::array<Localizer::CameraState, Localizer::kNumCameras> |
| 142 | Localizer::MakeCameras(const Constants &constants, aos::EventLoop *event_loop) { |
| 143 | CHECK(constants.has_cameras()); |
| 144 | std::array<Localizer::CameraState, Localizer::kNumCameras> cameras; |
| 145 | for (const CameraConfiguration *camera : *constants.cameras()) { |
| 146 | CHECK(camera->has_calibration()); |
| 147 | const frc971::vision::calibration::CameraCalibration *calibration = |
| 148 | camera->calibration(); |
| 149 | CHECK(!calibration->has_turret_extrinsics()) |
| 150 | << "The 2024 robot does not have cameras on a turret."; |
| 151 | CHECK(calibration->has_node_name()); |
| 152 | const std::string channel_name = |
| 153 | absl::StrFormat("/%s/camera%d", calibration->node_name()->string_view(), |
| 154 | calibration->camera_number()); |
| 155 | const size_t index = CameraIndexForName(channel_name); |
| 156 | // We default-construct the extrinsics matrix to all-zeros; use that to |
| 157 | // sanity-check whether we have populated the matrix yet or not. |
| 158 | CHECK(cameras.at(index).extrinsics.norm() == 0) |
| 159 | << "Got multiple calibrations for " |
| 160 | << calibration->node_name()->string_view(); |
| 161 | CHECK(calibration->has_fixed_extrinsics()); |
| 162 | cameras.at(index).extrinsics = |
| 163 | frc971::control_loops::drivetrain::FlatbufferToTransformationMatrix( |
| 164 | *calibration->fixed_extrinsics()); |
| 165 | cameras.at(index).debug_sender = |
| 166 | event_loop->MakeSender<VisualizationStatic>(channel_name); |
| 167 | } |
| 168 | for (const CameraState &camera : cameras) { |
| 169 | CHECK(camera.extrinsics.norm() != 0) << "Missing a camera calibration."; |
| 170 | } |
| 171 | return cameras; |
| 172 | } |
| 173 | |
| 174 | Localizer::Localizer(aos::EventLoop *event_loop) |
| 175 | : event_loop_(event_loop), |
| 176 | constants_fetcher_(event_loop), |
| 177 | dt_config_( |
| 178 | frc971::control_loops::drivetrain::DrivetrainConfig<double>:: |
| 179 | FromFlatbuffer(*CHECK_NOTNULL( |
| 180 | constants_fetcher_.constants().common()->drivetrain()))), |
| 181 | cameras_(MakeCameras(constants_fetcher_.constants(), event_loop)), |
| 182 | target_poses_(GetTargetLocations(constants_fetcher_.constants())), |
| 183 | down_estimator_(dt_config_), |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 184 | // Force the dt to 1 ms (the nominal IMU frequency) since we have observed |
| 185 | // issues with timing on the orins. |
| 186 | // TODO(james): Ostensibly, we should be able to use the timestamps from |
| 187 | // the IMU board itself for exactly this; however, I am currently worried |
| 188 | // about the impacts of clock drift in using that. |
| 189 | ekf_(dt_config_, std::chrono::milliseconds(1)), |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 190 | observations_(&ekf_), |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 191 | xyz_observations_(&ekf_), |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 192 | imu_watcher_(event_loop, dt_config_, |
| 193 | y2024::constants::Values::DrivetrainEncoderToMeters(1), |
| 194 | std::bind(&Localizer::HandleImu, this, std::placeholders::_1, |
| 195 | std::placeholders::_2, std::placeholders::_3, |
| 196 | std::placeholders::_4, std::placeholders::_5), |
| 197 | frc971::controls::ImuWatcher::TimestampSource::kPi), |
| 198 | utils_(event_loop), |
| 199 | status_sender_(event_loop->MakeSender<Status>("/localizer")), |
| 200 | output_sender_(event_loop->MakeSender<frc971::controls::LocalizerOutput>( |
| 201 | "/localizer")), |
| 202 | server_statistics_fetcher_( |
| 203 | event_loop_->MakeFetcher<aos::message_bridge::ServerStatistics>( |
| 204 | "/aos")), |
| 205 | client_statistics_fetcher_( |
| 206 | event_loop_->MakeFetcher<aos::message_bridge::ClientStatistics>( |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 207 | "/aos")), |
| 208 | control_fetcher_(event_loop_->MakeFetcher< |
| 209 | frc971::control_loops::drivetrain::LocalizerControl>( |
| 210 | "/drivetrain")) { |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 211 | if (dt_config_.is_simulated) { |
| 212 | down_estimator_.assume_perfect_gravity(); |
| 213 | } |
| 214 | |
| 215 | for (size_t camera_index = 0; camera_index < kNumCameras; ++camera_index) { |
| 216 | const std::string_view channel_name = kDetectionChannels.at(camera_index); |
| 217 | const aos::Channel *const channel = CHECK_NOTNULL( |
| 218 | event_loop->GetChannel<frc971::vision::TargetMap>(channel_name)); |
| 219 | event_loop->MakeWatcher( |
| 220 | channel_name, [this, channel, |
| 221 | camera_index](const frc971::vision::TargetMap &targets) { |
| 222 | CHECK(targets.has_target_poses()); |
| 223 | CHECK(targets.has_monotonic_timestamp_ns()); |
| 224 | const std::optional<aos::monotonic_clock::duration> clock_offset = |
| 225 | utils_.ClockOffset(channel->source_node()->string_view()); |
| 226 | if (!clock_offset.has_value()) { |
| 227 | VLOG(1) << "Rejecting image due to disconnected message bridge at " |
| 228 | << event_loop_->monotonic_now(); |
| 229 | cameras_.at(camera_index) |
| 230 | .rejection_counter.IncrementError( |
| 231 | RejectionReason::MESSAGE_BRIDGE_DISCONNECTED); |
| 232 | return; |
| 233 | } |
| 234 | const aos::monotonic_clock::time_point orin_capture_time( |
| 235 | std::chrono::nanoseconds(targets.monotonic_timestamp_ns()) - |
| 236 | clock_offset.value()); |
| 237 | if (orin_capture_time > event_loop_->context().monotonic_event_time) { |
| 238 | VLOG(1) << "Rejecting image due to being from future at " |
| 239 | << event_loop_->monotonic_now() << " with timestamp of " |
| 240 | << orin_capture_time << " and event time pf " |
| 241 | << event_loop_->context().monotonic_event_time; |
| 242 | cameras_.at(camera_index) |
| 243 | .rejection_counter.IncrementError( |
| 244 | RejectionReason::IMAGE_FROM_FUTURE); |
| 245 | return; |
| 246 | } |
| 247 | auto debug_builder = |
| 248 | cameras_.at(camera_index).debug_sender.MakeStaticBuilder(); |
| 249 | auto target_debug_list = debug_builder->add_targets(); |
| 250 | // The static_length should already be 20. |
| 251 | CHECK(target_debug_list->reserve(20)); |
| 252 | for (const frc971::vision::TargetPoseFbs *target : |
| 253 | *targets.target_poses()) { |
| 254 | VLOG(1) << "Handling target from " << camera_index; |
| 255 | HandleTarget(camera_index, orin_capture_time, *target, |
| 256 | target_debug_list->emplace_back()); |
| 257 | } |
| 258 | StatisticsForCamera(cameras_.at(camera_index), |
| 259 | debug_builder->add_statistics()); |
| 260 | debug_builder.CheckOk(debug_builder.Send()); |
| 261 | SendStatus(); |
| 262 | }); |
| 263 | } |
| 264 | |
| 265 | event_loop_->AddPhasedLoop([this](int) { SendOutput(); }, |
| 266 | std::chrono::milliseconds(20)); |
| 267 | |
| 268 | event_loop_->MakeWatcher( |
| 269 | "/drivetrain", |
| 270 | [this]( |
| 271 | const frc971::control_loops::drivetrain::LocalizerControl &control) { |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 272 | HandleControl(control); |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 273 | }); |
| 274 | |
| 275 | ekf_.set_ignore_accel(true); |
| 276 | // Priority should be lower than the imu reading process, but non-zero. |
| 277 | event_loop->SetRuntimeRealtimePriority(10); |
| 278 | event_loop->OnRun([this, event_loop]() { |
| 279 | ekf_.ResetInitialState(event_loop->monotonic_now(), |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 280 | HybridEkf::State::Zero(), NominalCovariance()); |
| 281 | if (control_fetcher_.Fetch()) { |
| 282 | HandleControl(*control_fetcher_.get()); |
| 283 | } |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 284 | }); |
| 285 | } |
| 286 | |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 287 | void Localizer::HandleControl( |
| 288 | const frc971::control_loops::drivetrain::LocalizerControl &control) { |
| 289 | // This is triggered whenever we need to force the X/Y/(maybe theta) |
| 290 | // position of the robot to a particular point---e.g., during pre-match |
| 291 | // setup, or when commanded by a button on the driverstation. |
| 292 | |
| 293 | // For some forms of reset, we choose to keep our current yaw estimate |
| 294 | // rather than overriding it from the control message. |
| 295 | const double theta = control.keep_current_theta() |
| 296 | ? ekf_.X_hat(StateIdx::kTheta) |
| 297 | : control.theta(); |
| 298 | // Encoder values need to be reset based on the current values to ensure |
| 299 | // that we don't get weird corrections on the next encoder update. |
| 300 | const double left_encoder = ekf_.X_hat(StateIdx::kLeftEncoder); |
| 301 | const double right_encoder = ekf_.X_hat(StateIdx::kRightEncoder); |
| 302 | ekf_.ResetInitialState(t_, |
| 303 | (HybridEkf::State() << control.x(), control.y(), theta, |
| 304 | left_encoder, 0, right_encoder, 0, 0, 0, 0, 0, 0) |
| 305 | .finished(), |
| 306 | NominalCovariance()); |
| 307 | VLOG(1) << "Reset state"; |
| 308 | } |
| 309 | |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 310 | void Localizer::HandleImu(aos::monotonic_clock::time_point /*sample_time_pico*/, |
| 311 | aos::monotonic_clock::time_point sample_time_orin, |
| 312 | std::optional<Eigen::Vector2d> /*encoders*/, |
| 313 | Eigen::Vector3d gyro, Eigen::Vector3d accel) { |
| 314 | std::optional<Eigen::Vector2d> encoders = utils_.Encoders(sample_time_orin); |
| 315 | last_encoder_readings_ = encoders; |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 316 | VLOG(1) << "Got encoders"; |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 317 | if (t_ == aos::monotonic_clock::min_time) { |
| 318 | t_ = sample_time_orin; |
| 319 | } |
| 320 | if (t_ + 10 * frc971::controls::ImuWatcher::kNominalDt < sample_time_orin) { |
| 321 | t_ = sample_time_orin; |
| 322 | ++clock_resets_; |
| 323 | } |
| 324 | const aos::monotonic_clock::duration dt = sample_time_orin - t_; |
| 325 | t_ = sample_time_orin; |
| 326 | // We don't actually use the down estimator currently, but it's really |
| 327 | // convenient for debugging. |
| 328 | down_estimator_.Predict(gyro, accel, dt); |
| 329 | const double yaw_rate = (dt_config_.imu_transform * gyro)(2); |
James Kuszmaul | 2700e0f | 2024-03-16 16:45:48 -0700 | [diff] [blame] | 330 | ekf_.UpdateEncodersAndGyro( |
| 331 | encoders.has_value() ? std::make_optional<double>(encoders.value()(0)) |
| 332 | : std::nullopt, |
| 333 | encoders.has_value() ? std::make_optional<double>(encoders.value()(1)) |
| 334 | : std::nullopt, |
| 335 | yaw_rate, utils_.VoltageOrZero(sample_time_orin), accel, t_); |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 336 | SendStatus(); |
| 337 | } |
| 338 | |
| 339 | void Localizer::RejectImage(int camera_index, RejectionReason reason, |
| 340 | TargetEstimateDebugStatic *builder) { |
| 341 | if (builder != nullptr) { |
| 342 | builder->set_accepted(false); |
| 343 | builder->set_rejection_reason(reason); |
| 344 | } |
| 345 | cameras_.at(camera_index).rejection_counter.IncrementError(reason); |
| 346 | } |
| 347 | |
| 348 | // Only use april tags present in the target map; this method has also been used |
| 349 | // (in the past) for ignoring april tags that tend to produce problematic |
| 350 | // readings. |
| 351 | bool Localizer::UseAprilTag(uint64_t target_id) { |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 352 | if (target_poses_.count(target_id) == 0) { |
| 353 | return false; |
| 354 | } |
| 355 | return true; |
| 356 | } |
| 357 | |
| 358 | bool Localizer::DeweightAprilTag(uint64_t target_id) { |
| 359 | const flatbuffers::Vector<uint64_t> *ignore_tags = nullptr; |
| 360 | |
| 361 | switch (utils_.Alliance()) { |
| 362 | case aos::Alliance::kRed: |
| 363 | ignore_tags = CHECK_NOTNULL( |
| 364 | constants_fetcher_.constants().common()->ignore_targets()->red()); |
| 365 | break; |
| 366 | case aos::Alliance::kBlue: |
| 367 | ignore_tags = CHECK_NOTNULL( |
| 368 | constants_fetcher_.constants().common()->ignore_targets()->blue()); |
| 369 | break; |
| 370 | case aos::Alliance::kInvalid: |
| 371 | return false; |
| 372 | } |
| 373 | return std::find(ignore_tags->begin(), ignore_tags->end(), target_id) != |
| 374 | ignore_tags->end(); |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | namespace { |
| 378 | // Converts a camera transformation matrix from treating the +Z axis from |
| 379 | // pointing straight out the lens to having the +X pointing straight out the |
| 380 | // lens, with +Z going "up" (i.e., -Y in the normal convention) and +Y going |
| 381 | // leftwards (i.e., -X in the normal convention). |
| 382 | Localizer::Transform ZToXCamera(const Localizer::Transform &transform) { |
| 383 | return transform * |
| 384 | Eigen::Matrix4d{ |
| 385 | {0, -1, 0, 0}, {0, 0, -1, 0}, {1, 0, 0, 0}, {0, 0, 0, 1}}; |
| 386 | } |
| 387 | } // namespace |
| 388 | |
| 389 | void Localizer::HandleTarget( |
| 390 | int camera_index, const aos::monotonic_clock::time_point capture_time, |
| 391 | const frc971::vision::TargetPoseFbs &target, |
| 392 | TargetEstimateDebugStatic *debug_builder) { |
| 393 | ++total_candidate_targets_; |
| 394 | ++cameras_.at(camera_index).total_candidate_targets; |
| 395 | const uint64_t target_id = target.id(); |
| 396 | |
| 397 | if (debug_builder == nullptr) { |
| 398 | AOS_LOG(ERROR, "Dropped message from debug vector."); |
| 399 | } else { |
| 400 | debug_builder->set_camera(camera_index); |
| 401 | debug_builder->set_image_age_sec(aos::time::DurationInSeconds( |
| 402 | event_loop_->monotonic_now() - capture_time)); |
| 403 | debug_builder->set_image_monotonic_timestamp_ns( |
| 404 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 405 | capture_time.time_since_epoch()) |
| 406 | .count()); |
| 407 | debug_builder->set_april_tag(target_id); |
| 408 | } |
| 409 | VLOG(2) << aos::FlatbufferToJson(&target); |
| 410 | if (!UseAprilTag(target_id)) { |
| 411 | VLOG(1) << "Rejecting target due to invalid ID " << target_id; |
| 412 | RejectImage(camera_index, RejectionReason::NO_SUCH_TARGET, debug_builder); |
| 413 | return; |
| 414 | } |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 415 | double april_tag_noise_scalar = 1.0; |
| 416 | if (DeweightAprilTag(target_id)) { |
| 417 | if (!FLAGS_always_use_extra_tags && utils_.MaybeInAutonomous()) { |
| 418 | VLOG(1) << "Rejecting target due to auto invalid ID " << target_id; |
| 419 | RejectImage(camera_index, RejectionReason::NO_SUCH_TARGET, debug_builder); |
| 420 | return; |
| 421 | } else { |
James Kuszmaul | 592055e | 2024-03-23 20:12:59 -0700 | [diff] [blame] | 422 | if (utils_.MaybeInAutonomous()) { |
| 423 | april_tag_noise_scalar = 2.0; |
| 424 | } else { |
| 425 | april_tag_noise_scalar = 10.0; |
| 426 | } |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 427 | } |
| 428 | } |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 429 | |
| 430 | const Transform &H_field_target = target_poses_.at(target_id); |
| 431 | const Transform &H_robot_camera = cameras_.at(camera_index).extrinsics; |
| 432 | |
| 433 | const Transform H_camera_target = PoseToTransform(&target); |
| 434 | |
| 435 | // In order to do the EKF correction, we determine the expected state based |
| 436 | // on the state at the time the image was captured; however, we insert the |
| 437 | // correction update itself at the current time. This is technically not |
| 438 | // quite correct, but saves substantial CPU usage & code complexity by |
| 439 | // making it so that we don't have to constantly rewind the entire EKF |
| 440 | // history. |
| 441 | const std::optional<State> state_at_capture = |
| 442 | ekf_.LastStateBeforeTime(capture_time); |
| 443 | |
| 444 | if (!state_at_capture.has_value()) { |
| 445 | VLOG(1) << "Rejecting image due to being too old."; |
| 446 | return RejectImage(camera_index, RejectionReason::IMAGE_TOO_OLD, |
| 447 | debug_builder); |
| 448 | } else if (target.pose_error() > FLAGS_max_pose_error) { |
| 449 | VLOG(1) << "Rejecting target due to high pose error " |
| 450 | << target.pose_error(); |
| 451 | return RejectImage(camera_index, RejectionReason::HIGH_POSE_ERROR, |
| 452 | debug_builder); |
| 453 | } else if (target.pose_error_ratio() > FLAGS_max_pose_error_ratio) { |
| 454 | VLOG(1) << "Rejecting target due to high pose error ratio " |
| 455 | << target.pose_error_ratio(); |
| 456 | return RejectImage(camera_index, RejectionReason::HIGH_POSE_ERROR_RATIO, |
| 457 | debug_builder); |
| 458 | } |
| 459 | |
James Kuszmaul | 592055e | 2024-03-23 20:12:59 -0700 | [diff] [blame] | 460 | const double robot_speed = |
| 461 | (state_at_capture.value()(StateIdx::kLeftVelocity) + |
| 462 | state_at_capture.value()(StateIdx::kRightVelocity)) / |
| 463 | 2.0; |
| 464 | |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 465 | Corrector corrector(state_at_capture.value(), H_field_target, H_robot_camera, |
| 466 | H_camera_target); |
| 467 | const double distance_to_target = corrector.observed()(Corrector::kDistance); |
| 468 | |
| 469 | // Heading, distance, skew at 1 meter. |
James Kuszmaul | 592055e | 2024-03-23 20:12:59 -0700 | [diff] [blame] | 470 | Eigen::Matrix<double, 3, 1> noises(0.03, 0.25, 0.15); |
| 471 | noises *= 2.0; |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 472 | const double distance_noise_scalar = |
| 473 | std::min(1.0, std::pow(distance_to_target, 2.0)); |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 474 | noises(Corrector::kDistance) *= distance_noise_scalar; |
| 475 | noises(Corrector::kSkew) *= distance_noise_scalar; |
| 476 | // TODO(james): This is leftover from last year; figure out if we want it. |
| 477 | // Scale noise by the distortion factor for this detection |
| 478 | noises *= (1.0 + FLAGS_distortion_noise_scalar * target.distortion_factor()); |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 479 | noises *= april_tag_noise_scalar; |
James Kuszmaul | 592055e | 2024-03-23 20:12:59 -0700 | [diff] [blame] | 480 | noises *= (1.0 + std::abs(robot_speed)); |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 481 | |
| 482 | Eigen::Matrix3d R = Eigen::Matrix3d::Zero(); |
| 483 | R.diagonal() = noises.cwiseAbs2(); |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 484 | const Eigen::Vector3d camera_position = |
| 485 | corrector.observed_camera_pose().abs_pos(); |
| 486 | // Calculate the camera-to-robot transformation matrix ignoring the |
| 487 | // pitch/roll of the camera. |
| 488 | const Transform H_camera_robot_stripped = |
| 489 | frc971::control_loops::Pose(ZToXCamera(H_robot_camera)) |
| 490 | .AsTransformationMatrix() |
| 491 | .inverse(); |
| 492 | const frc971::control_loops::Pose measured_pose( |
| 493 | corrector.observed_camera_pose().AsTransformationMatrix() * |
| 494 | H_camera_robot_stripped); |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 495 | if (debug_builder != nullptr) { |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 496 | debug_builder->set_camera_x(camera_position.x()); |
| 497 | debug_builder->set_camera_y(camera_position.y()); |
| 498 | debug_builder->set_camera_theta( |
| 499 | corrector.observed_camera_pose().abs_theta()); |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 500 | debug_builder->set_implied_robot_x(measured_pose.rel_pos().x()); |
| 501 | debug_builder->set_implied_robot_y(measured_pose.rel_pos().y()); |
| 502 | debug_builder->set_implied_robot_theta(measured_pose.rel_theta()); |
| 503 | |
| 504 | Corrector::PopulateMeasurement(corrector.expected(), |
| 505 | debug_builder->add_expected_observation()); |
| 506 | Corrector::PopulateMeasurement(corrector.observed(), |
| 507 | debug_builder->add_actual_observation()); |
| 508 | Corrector::PopulateMeasurement(noises, debug_builder->add_modeled_noise()); |
| 509 | } |
| 510 | |
| 511 | const double camera_yaw_error = |
| 512 | aos::math::NormalizeAngle(corrector.expected_camera_pose().abs_theta() - |
| 513 | corrector.observed_camera_pose().abs_theta()); |
| 514 | constexpr double kDegToRad = M_PI / 180.0; |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 515 | const double yaw_threshold = |
| 516 | (utils_.MaybeInAutonomous() ? FLAGS_max_implied_yaw_error |
| 517 | : FLAGS_max_implied_teleop_yaw_error) * |
| 518 | kDegToRad; |
| 519 | |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 520 | if (target.distortion_factor() > FLAGS_max_distortion) { |
| 521 | VLOG(1) << "Rejecting target due to high distortion."; |
| 522 | return RejectImage(camera_index, RejectionReason::HIGH_DISTORTION, |
| 523 | debug_builder); |
| 524 | } else if (utils_.MaybeInAutonomous() && |
| 525 | (std::abs(robot_speed) > FLAGS_max_auto_image_robot_speed)) { |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 526 | return RejectImage(camera_index, RejectionReason::ROBOT_TOO_FAST, |
| 527 | debug_builder); |
| 528 | } else if (std::abs(camera_yaw_error) > yaw_threshold) { |
| 529 | return RejectImage(camera_index, RejectionReason::HIGH_IMPLIED_YAW_ERROR, |
| 530 | debug_builder); |
| 531 | } else if (distance_to_target > FLAGS_max_distance_to_target) { |
| 532 | return RejectImage(camera_index, RejectionReason::HIGH_DISTANCE_TO_TARGET, |
| 533 | debug_builder); |
| 534 | } |
| 535 | |
| 536 | const Input U = ekf_.MostRecentInput(); |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 537 | VLOG(1) << "previous state " << ekf_.X_hat().transpose(); |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 538 | const State prior_state = ekf_.X_hat(); |
| 539 | // For the correction step, instead of passing in the measurement directly, |
| 540 | // we pass in (0, 0, 0) as the measurement and then for the expected |
| 541 | // measurement (Zhat) we calculate the error between the pose implied by |
| 542 | // the camera measurement and the current estimate of the |
| 543 | // pose. This doesn't affect any of the math, it just makes the code a bit |
| 544 | // more convenient to write given the Correct() interface we already have. |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 545 | if (FLAGS_do_xytheta_corrections) { |
| 546 | Eigen::Vector3d Z(measured_pose.rel_pos().x(), measured_pose.rel_pos().y(), |
| 547 | measured_pose.rel_theta()); |
| 548 | Eigen::Matrix<double, 3, 1> xyz_noises(0.2, 0.2, 0.5); |
| 549 | xyz_noises *= distance_noise_scalar; |
| 550 | xyz_noises *= april_tag_noise_scalar; |
| 551 | // Scale noise by the distortion factor for this detection |
| 552 | xyz_noises *= |
| 553 | (1.0 + FLAGS_distortion_noise_scalar * target.distortion_factor()); |
| 554 | |
| 555 | Eigen::Matrix3d R_xyz = Eigen::Matrix3d::Zero(); |
| 556 | R_xyz.diagonal() = xyz_noises.cwiseAbs2(); |
| 557 | xyz_observations_.CorrectKnownH(Eigen::Vector3d::Zero(), &U, |
| 558 | XyzCorrector(state_at_capture.value(), Z), |
| 559 | R_xyz, t_); |
| 560 | } else { |
| 561 | observations_.CorrectKnownH(Eigen::Vector3d::Zero(), &U, corrector, R, t_); |
| 562 | } |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 563 | ++total_accepted_targets_; |
| 564 | ++cameras_.at(camera_index).total_accepted_targets; |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 565 | VLOG(1) << "new state " << ekf_.X_hat().transpose(); |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 566 | if (debug_builder != nullptr) { |
| 567 | debug_builder->set_correction_x(ekf_.X_hat()(StateIdx::kX) - |
| 568 | prior_state(StateIdx::kX)); |
| 569 | debug_builder->set_correction_y(ekf_.X_hat()(StateIdx::kY) - |
| 570 | prior_state(StateIdx::kY)); |
| 571 | debug_builder->set_correction_theta(ekf_.X_hat()(StateIdx::kTheta) - |
| 572 | prior_state(StateIdx::kTheta)); |
| 573 | debug_builder->set_accepted(true); |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 574 | debug_builder->set_expected_robot_x(ekf_.X_hat()(StateIdx::kX)); |
| 575 | debug_builder->set_expected_robot_y(ekf_.X_hat()(StateIdx::kY)); |
James Kuszmaul | 81d5f2d | 2024-04-05 21:57:14 -0700 | [diff] [blame^] | 576 | debug_builder->set_expected_robot_theta( |
| 577 | aos::math::NormalizeAngle(ekf_.X_hat()(StateIdx::kTheta))); |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
| 581 | void Localizer::SendOutput() { |
| 582 | auto builder = output_sender_.MakeBuilder(); |
| 583 | frc971::controls::LocalizerOutput::Builder output_builder = |
| 584 | builder.MakeBuilder<frc971::controls::LocalizerOutput>(); |
| 585 | output_builder.add_monotonic_timestamp_ns( |
| 586 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 587 | event_loop_->context().monotonic_event_time.time_since_epoch()) |
| 588 | .count()); |
| 589 | output_builder.add_x(ekf_.X_hat(StateIdx::kX)); |
| 590 | output_builder.add_y(ekf_.X_hat(StateIdx::kY)); |
| 591 | output_builder.add_theta(ekf_.X_hat(StateIdx::kTheta)); |
| 592 | output_builder.add_zeroed(imu_watcher_.zeroer().Zeroed()); |
| 593 | output_builder.add_image_accepted_count(total_accepted_targets_); |
| 594 | const Eigen::Quaterniond &orientation = |
| 595 | Eigen::AngleAxis<double>(ekf_.X_hat(StateIdx::kTheta), |
| 596 | Eigen::Vector3d::UnitZ()) * |
| 597 | down_estimator_.X_hat(); |
| 598 | frc971::controls::Quaternion quaternion; |
| 599 | quaternion.mutate_x(orientation.x()); |
| 600 | quaternion.mutate_y(orientation.y()); |
| 601 | quaternion.mutate_z(orientation.z()); |
| 602 | quaternion.mutate_w(orientation.w()); |
| 603 | output_builder.add_orientation(&quaternion); |
| 604 | server_statistics_fetcher_.Fetch(); |
| 605 | client_statistics_fetcher_.Fetch(); |
| 606 | |
| 607 | bool orins_connected = true; |
| 608 | |
| 609 | if (server_statistics_fetcher_.get()) { |
| 610 | for (const auto *orin_server_status : |
| 611 | *server_statistics_fetcher_->connections()) { |
| 612 | if (orin_server_status->state() == |
| 613 | aos::message_bridge::State::DISCONNECTED) { |
| 614 | orins_connected = false; |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | if (client_statistics_fetcher_.get()) { |
| 620 | for (const auto *pi_client_status : |
| 621 | *client_statistics_fetcher_->connections()) { |
| 622 | if (pi_client_status->state() == |
| 623 | aos::message_bridge::State::DISCONNECTED) { |
| 624 | orins_connected = false; |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | // The output message is year-agnostic, and retains "pi" naming for histrocial |
| 630 | // reasons. |
| 631 | output_builder.add_all_pis_connected(orins_connected); |
| 632 | builder.CheckOk(builder.Send(output_builder.Finish())); |
| 633 | } |
| 634 | |
| 635 | flatbuffers::Offset<frc971::control_loops::drivetrain::LocalizerState> |
| 636 | Localizer::PopulateState(const State &X_hat, |
| 637 | flatbuffers::FlatBufferBuilder *fbb) { |
| 638 | frc971::control_loops::drivetrain::LocalizerState::Builder builder(*fbb); |
| 639 | builder.add_x(X_hat(StateIdx::kX)); |
| 640 | builder.add_y(X_hat(StateIdx::kY)); |
Stephan Pleines | 78df16d | 2024-04-03 20:45:26 -0700 | [diff] [blame] | 641 | builder.add_theta(aos::math::NormalizeAngle(X_hat(StateIdx::kTheta))); |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 642 | builder.add_left_velocity(X_hat(StateIdx::kLeftVelocity)); |
| 643 | builder.add_right_velocity(X_hat(StateIdx::kRightVelocity)); |
| 644 | builder.add_left_encoder(X_hat(StateIdx::kLeftEncoder)); |
| 645 | builder.add_right_encoder(X_hat(StateIdx::kRightEncoder)); |
| 646 | builder.add_left_voltage_error(X_hat(StateIdx::kLeftVoltageError)); |
| 647 | builder.add_right_voltage_error(X_hat(StateIdx::kRightVoltageError)); |
| 648 | builder.add_angular_error(X_hat(StateIdx::kAngularError)); |
| 649 | builder.add_longitudinal_velocity_offset( |
| 650 | X_hat(StateIdx::kLongitudinalVelocityOffset)); |
| 651 | builder.add_lateral_velocity(X_hat(StateIdx::kLateralVelocity)); |
| 652 | return builder.Finish(); |
| 653 | } |
| 654 | |
| 655 | flatbuffers::Offset<ImuStatus> Localizer::PopulateImu( |
| 656 | flatbuffers::FlatBufferBuilder *fbb) const { |
| 657 | const auto zeroer_offset = imu_watcher_.zeroer().PopulateStatus(fbb); |
| 658 | const auto failures_offset = imu_watcher_.PopulateImuFailures(fbb); |
| 659 | ImuStatus::Builder builder(*fbb); |
| 660 | builder.add_zeroed(imu_watcher_.zeroer().Zeroed()); |
| 661 | builder.add_faulted_zero(imu_watcher_.zeroer().Faulted()); |
| 662 | builder.add_zeroing(zeroer_offset); |
| 663 | if (imu_watcher_.pico_offset().has_value()) { |
| 664 | builder.add_board_offset_ns(imu_watcher_.pico_offset().value().count()); |
| 665 | builder.add_board_offset_error_ns(imu_watcher_.pico_offset_error().count()); |
| 666 | } |
| 667 | if (last_encoder_readings_.has_value()) { |
| 668 | builder.add_left_encoder(last_encoder_readings_.value()(0)); |
| 669 | builder.add_right_encoder(last_encoder_readings_.value()(1)); |
| 670 | } |
| 671 | builder.add_imu_failures(failures_offset); |
| 672 | return builder.Finish(); |
| 673 | } |
| 674 | |
| 675 | flatbuffers::Offset<CumulativeStatistics> Localizer::StatisticsForCamera( |
| 676 | const CameraState &camera, flatbuffers::FlatBufferBuilder *fbb) { |
| 677 | const auto counts_offset = camera.rejection_counter.PopulateCounts(fbb); |
| 678 | CumulativeStatistics::Builder stats_builder(*fbb); |
| 679 | stats_builder.add_total_accepted(camera.total_accepted_targets); |
| 680 | stats_builder.add_total_candidates(camera.total_candidate_targets); |
| 681 | stats_builder.add_rejection_reasons(counts_offset); |
| 682 | return stats_builder.Finish(); |
| 683 | } |
| 684 | |
| 685 | void Localizer::StatisticsForCamera(const CameraState &camera, |
| 686 | CumulativeStatisticsStatic *builder) { |
| 687 | camera.rejection_counter.PopulateCountsStaticFbs( |
| 688 | builder->add_rejection_reasons()); |
| 689 | builder->set_total_accepted(camera.total_accepted_targets); |
| 690 | builder->set_total_candidates(camera.total_candidate_targets); |
| 691 | } |
| 692 | |
| 693 | void Localizer::SendStatus() { |
| 694 | auto builder = status_sender_.MakeBuilder(); |
| 695 | std::array<flatbuffers::Offset<CumulativeStatistics>, kNumCameras> |
| 696 | stats_offsets; |
| 697 | for (size_t ii = 0; ii < kNumCameras; ++ii) { |
| 698 | stats_offsets.at(ii) = StatisticsForCamera(cameras_.at(ii), builder.fbb()); |
| 699 | } |
| 700 | auto stats_offset = |
| 701 | builder.fbb()->CreateVector(stats_offsets.data(), stats_offsets.size()); |
| 702 | auto down_estimator_offset = |
| 703 | down_estimator_.PopulateStatus(builder.fbb(), t_); |
| 704 | auto imu_offset = PopulateImu(builder.fbb()); |
| 705 | auto state_offset = PopulateState(ekf_.X_hat(), builder.fbb()); |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 706 | // covariance is a square; we use the number of rows in the state as the rows |
| 707 | // and cols of the covariance. |
| 708 | auto covariance_offset = |
| 709 | frc971::FromEigen<State::RowsAtCompileTime, State::RowsAtCompileTime>( |
| 710 | ekf_.P(), builder.fbb()); |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 711 | Status::Builder status_builder = builder.MakeBuilder<Status>(); |
| 712 | status_builder.add_state(state_offset); |
| 713 | status_builder.add_down_estimator(down_estimator_offset); |
| 714 | status_builder.add_imu(imu_offset); |
| 715 | status_builder.add_statistics(stats_offset); |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 716 | status_builder.add_ekf_covariance(covariance_offset); |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 717 | builder.CheckOk(builder.Send(status_builder.Finish())); |
| 718 | } |
| 719 | |
| 720 | Eigen::Vector3d Localizer::Corrector::HeadingDistanceSkew( |
| 721 | const Pose &relative_pose) { |
| 722 | const double heading = relative_pose.heading(); |
| 723 | const double distance = relative_pose.xy_norm(); |
| 724 | const double skew = |
| 725 | ::aos::math::NormalizeAngle(relative_pose.rel_theta() - heading); |
| 726 | return {heading, distance, skew}; |
| 727 | } |
| 728 | |
| 729 | Localizer::Corrector Localizer::Corrector::CalculateHeadingDistanceSkewH( |
| 730 | const State &state_at_capture, const Transform &H_field_target, |
| 731 | const Transform &H_robot_camera, const Transform &H_camera_target) { |
| 732 | const Transform H_field_camera = H_field_target * H_camera_target.inverse(); |
| 733 | const Pose expected_robot_pose( |
| 734 | {state_at_capture(StateIdx::kX), state_at_capture(StateIdx::kY), 0.0}, |
| 735 | state_at_capture(StateIdx::kTheta)); |
| 736 | // Observed position on the field, reduced to just the 2-D pose. |
| 737 | const Pose observed_camera(ZToXCamera(H_field_camera)); |
| 738 | const Pose expected_camera(expected_robot_pose.AsTransformationMatrix() * |
| 739 | ZToXCamera(H_robot_camera)); |
| 740 | const Pose nominal_target(ZToXCamera(H_field_target)); |
| 741 | const Pose observed_target = nominal_target.Rebase(&observed_camera); |
| 742 | const Pose expected_target = nominal_target.Rebase(&expected_camera); |
| 743 | return Localizer::Corrector{ |
| 744 | expected_robot_pose, |
| 745 | observed_camera, |
| 746 | expected_camera, |
| 747 | HeadingDistanceSkew(expected_target), |
| 748 | HeadingDistanceSkew(observed_target), |
| 749 | frc971::control_loops::drivetrain::HMatrixForCameraHeadingDistanceSkew( |
| 750 | nominal_target, observed_camera)}; |
| 751 | } |
| 752 | |
| 753 | Localizer::Corrector::Corrector(const State &state_at_capture, |
| 754 | const Transform &H_field_target, |
| 755 | const Transform &H_robot_camera, |
| 756 | const Transform &H_camera_target) |
| 757 | : Corrector(CalculateHeadingDistanceSkewH( |
| 758 | state_at_capture, H_field_target, H_robot_camera, H_camera_target)) {} |
| 759 | |
| 760 | Localizer::Output Localizer::Corrector::H(const State &, const Input &) { |
| 761 | return expected_ - observed_; |
| 762 | } |
| 763 | |
James Kuszmaul | 86116c2 | 2024-03-15 22:50:34 -0700 | [diff] [blame] | 764 | Localizer::Output Localizer::XyzCorrector::H(const State &, const Input &) { |
| 765 | CHECK(Z_.allFinite()); |
| 766 | Eigen::Vector3d Zhat = H_ * state_at_capture_ - Z_; |
| 767 | // Rewrap angle difference to put it back in range. |
| 768 | Zhat(2) = aos::math::NormalizeAngle(Zhat(2)); |
| 769 | VLOG(1) << "Zhat " << Zhat.transpose() << " Z_ " << Z_.transpose() |
| 770 | << " state " << (H_ * state_at_capture_).transpose(); |
| 771 | return Zhat; |
| 772 | } |
| 773 | |
James Kuszmaul | 313e9ce | 2024-02-11 17:47:33 -0800 | [diff] [blame] | 774 | } // namespace y2024::localizer |