James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 1 | #ifndef Y2019_CONTROL_LOOPS_DRIVETRAIN_LOCALIZATER_H_ |
| 2 | #define Y2019_CONTROL_LOOPS_DRIVETRAIN_LOCALIZATER_H_ |
| 3 | |
| 4 | #include <cmath> |
| 5 | #include <memory> |
| 6 | |
| 7 | #include "frc971/control_loops/pose.h" |
James Kuszmaul | f4ede20 | 2020-02-14 08:47:40 -0800 | [diff] [blame^] | 8 | #include "frc971/control_loops/drivetrain/camera.h" |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 9 | #include "frc971/control_loops/drivetrain/hybrid_ekf.h" |
| 10 | |
| 11 | namespace y2019 { |
| 12 | namespace control_loops { |
| 13 | |
| 14 | template <int num_cameras, int num_targets, int num_obstacles, |
| 15 | int max_targets_per_frame, typename Scalar = double> |
| 16 | class TypedLocalizer |
| 17 | : public ::frc971::control_loops::drivetrain::HybridEkf<Scalar> { |
| 18 | public: |
James Kuszmaul | f4ede20 | 2020-02-14 08:47:40 -0800 | [diff] [blame^] | 19 | typedef frc971::control_loops::TypedCamera<num_targets, num_obstacles, Scalar> |
| 20 | Camera; |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 21 | typedef typename Camera::TargetView TargetView; |
| 22 | typedef typename Camera::Pose Pose; |
James Kuszmaul | f4ede20 | 2020-02-14 08:47:40 -0800 | [diff] [blame^] | 23 | typedef typename frc971::control_loops::Target Target; |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 24 | typedef ::frc971::control_loops::drivetrain::HybridEkf<Scalar> HybridEkf; |
| 25 | typedef typename HybridEkf::State State; |
| 26 | typedef typename HybridEkf::StateSquare StateSquare; |
| 27 | typedef typename HybridEkf::Input Input; |
| 28 | typedef typename HybridEkf::Output Output; |
| 29 | using HybridEkf::kNInputs; |
| 30 | using HybridEkf::kNOutputs; |
| 31 | using HybridEkf::kNStates; |
| 32 | |
| 33 | // robot_pose should be the object that is used by the cameras, such that when |
| 34 | // we update robot_pose, the cameras will change what they report the relative |
| 35 | // position of the targets as. |
| 36 | // Note that the parameters for the cameras should be set to allow slightly |
| 37 | // larger fields of view and slightly longer range than the true cameras so |
| 38 | // that we can identify potential matches for targets even when we have slight |
| 39 | // modelling errors. |
| 40 | TypedLocalizer( |
| 41 | const ::frc971::control_loops::drivetrain::DrivetrainConfig<Scalar> |
| 42 | &dt_config, |
| 43 | Pose *robot_pose) |
| 44 | : ::frc971::control_loops::drivetrain::HybridEkf<Scalar>(dt_config), |
| 45 | robot_pose_(robot_pose) {} |
| 46 | |
| 47 | // Performs a kalman filter correction with a single camera frame, consisting |
| 48 | // of up to max_targets_per_frame targets and taken at time t. |
| 49 | // camera is the Camera used to take the images. |
| 50 | void UpdateTargets( |
| 51 | const Camera &camera, |
| 52 | const ::aos::SizedArray<TargetView, max_targets_per_frame> &targets, |
| 53 | ::aos::monotonic_clock::time_point t) { |
| 54 | if (targets.empty()) { |
| 55 | return; |
| 56 | } |
| 57 | |
James Kuszmaul | 6f941b7 | 2019-03-08 18:12:25 -0800 | [diff] [blame] | 58 | if (!SanitizeTargets(targets)) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 59 | AOS_LOG(ERROR, "Throwing out targets due to in insane values.\n"); |
James Kuszmaul | 6f941b7 | 2019-03-08 18:12:25 -0800 | [diff] [blame] | 60 | return; |
| 61 | } |
| 62 | |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 63 | if (t > HybridEkf::latest_t()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 64 | AOS_LOG(ERROR, |
| 65 | "target observations must be older than most recent encoder/gyro " |
| 66 | "update.\n"); |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 67 | return; |
| 68 | } |
| 69 | |
| 70 | Output z; |
| 71 | Eigen::Matrix<Scalar, kNOutputs, kNOutputs> R; |
| 72 | TargetViewToMatrices(targets[0], &z, &R); |
| 73 | |
| 74 | // In order to perform the correction steps for the targets, we will |
| 75 | // separately perform a Correct step for each following target. |
| 76 | // This way, we can have the first correction figure out the mappings |
| 77 | // between targets in the image and targets on the field, and then re-use |
| 78 | // those mappings for all the remaining corrections. |
| 79 | // As such, we need to store the EKF functions that the remaining targets |
| 80 | // will need in arrays: |
| 81 | ::aos::SizedArray<::std::function<Output(const State &, const Input &)>, |
| 82 | max_targets_per_frame> h_functions; |
| 83 | ::aos::SizedArray<::std::function<Eigen::Matrix<Scalar, kNOutputs, |
| 84 | kNStates>(const State &)>, |
| 85 | max_targets_per_frame> dhdx_functions; |
| 86 | HybridEkf::Correct( |
| 87 | z, nullptr, |
| 88 | ::std::bind(&TypedLocalizer::MakeH, this, camera, targets, &h_functions, |
| 89 | &dhdx_functions, ::std::placeholders::_1, |
| 90 | ::std::placeholders::_2, ::std::placeholders::_3, |
| 91 | ::std::placeholders::_4), |
| 92 | {}, {}, R, t); |
| 93 | // Fetch cache: |
| 94 | for (size_t ii = 1; ii < targets.size(); ++ii) { |
| 95 | TargetViewToMatrices(targets[ii], &z, &R); |
| 96 | HybridEkf::Correct(z, nullptr, {}, h_functions[ii], dhdx_functions[ii], R, |
| 97 | t); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | // The threshold to use for completely rejecting potentially bad target |
| 103 | // matches. |
| 104 | // TODO(james): Tune |
Austin Schuh | 113a85d | 2019-03-28 17:18:08 -0700 | [diff] [blame] | 105 | static constexpr Scalar kRejectionScore = 1.0; |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 106 | |
James Kuszmaul | 6f941b7 | 2019-03-08 18:12:25 -0800 | [diff] [blame] | 107 | // Checks that the targets coming in make some sense--mostly to prevent NaNs |
| 108 | // or the such from propagating. |
| 109 | bool SanitizeTargets( |
| 110 | const ::aos::SizedArray<TargetView, max_targets_per_frame> &targets) { |
| 111 | for (const TargetView &view : targets) { |
| 112 | const typename TargetView::Reading reading = view.reading; |
| 113 | if (!(::std::isfinite(reading.heading) && |
| 114 | ::std::isfinite(reading.distance) && |
| 115 | ::std::isfinite(reading.skew) && ::std::isfinite(reading.height))) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 116 | AOS_LOG(ERROR, "Got non-finite values in target.\n"); |
James Kuszmaul | 6f941b7 | 2019-03-08 18:12:25 -0800 | [diff] [blame] | 117 | return false; |
| 118 | } |
| 119 | if (reading.distance < 0) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 120 | AOS_LOG(ERROR, "Got negative distance.\n"); |
James Kuszmaul | 6f941b7 | 2019-03-08 18:12:25 -0800 | [diff] [blame] | 121 | return false; |
| 122 | } |
| 123 | if (::std::abs(::aos::math::NormalizeAngle(reading.skew)) > M_PI_2) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 124 | AOS_LOG(ERROR, "Got skew > pi / 2.\n"); |
James Kuszmaul | 6f941b7 | 2019-03-08 18:12:25 -0800 | [diff] [blame] | 125 | return false; |
| 126 | } |
| 127 | } |
| 128 | return true; |
| 129 | } |
| 130 | |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 131 | // Computes the measurement (z) and noise covariance (R) matrices for a given |
| 132 | // TargetView. |
| 133 | void TargetViewToMatrices(const TargetView &view, Output *z, |
| 134 | Eigen::Matrix<Scalar, kNOutputs, kNOutputs> *R) { |
James Kuszmaul | 6f941b7 | 2019-03-08 18:12:25 -0800 | [diff] [blame] | 135 | *z << view.reading.heading, view.reading.distance, |
| 136 | ::aos::math::NormalizeAngle(view.reading.skew); |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 137 | // TODO(james): R should account as well for our confidence in the target |
| 138 | // matching. However, handling that properly requires thing a lot more about |
| 139 | // the probabilities. |
| 140 | R->setZero(); |
| 141 | R->diagonal() << ::std::pow(view.noise.heading, 2), |
| 142 | ::std::pow(view.noise.distance, 2), ::std::pow(view.noise.skew, 2); |
| 143 | } |
| 144 | |
| 145 | // This is the function that will be called once the Ekf has inserted the |
| 146 | // measurement into the right spot in the measurement queue and needs the |
| 147 | // output functions to actually perform the corrections. |
| 148 | // Specifically, this will take the estimate of the state at that time and |
| 149 | // figure out how the targets seen by the camera best map onto the actual |
| 150 | // targets on the field. |
| 151 | // It then fills in the h and dhdx functions that are called by the Ekf. |
| 152 | void MakeH( |
| 153 | const Camera &camera, |
| 154 | const ::aos::SizedArray<TargetView, max_targets_per_frame> &target_views, |
| 155 | ::aos::SizedArray<::std::function<Output(const State &, const Input &)>, |
| 156 | max_targets_per_frame> *h_functions, |
| 157 | ::aos::SizedArray<::std::function<Eigen::Matrix<Scalar, kNOutputs, |
| 158 | kNStates>(const State &)>, |
| 159 | max_targets_per_frame> *dhdx_functions, |
| 160 | const State &X_hat, const StateSquare &P, |
| 161 | ::std::function<Output(const State &, const Input &)> *h, |
| 162 | ::std::function< |
| 163 | Eigen::Matrix<Scalar, kNOutputs, kNStates>(const State &)> *dhdx) { |
| 164 | // Because we need to match camera targets ("views") to actual field |
| 165 | // targets, and because we want to take advantage of the correlations |
| 166 | // between the targets (i.e., if we see two targets in the image, they |
| 167 | // probably correspond to different on-field targets), the matching problem |
| 168 | // here is somewhat non-trivial. Some of the methods we use only work |
| 169 | // because we are dealing with very small N (e.g., handling the correlations |
| 170 | // between multiple views has combinatoric complexity, but since N = 3, |
| 171 | // it's not an issue). |
| 172 | // |
| 173 | // High-level steps: |
| 174 | // 1) Set the base robot pose for the cameras to the Pose implied by X_hat. |
| 175 | // 2) Fetch all the expected target views from the camera. |
| 176 | // 3) Determine the "magnitude" of the Kalman correction from each potential |
| 177 | // view/target pair. |
| 178 | // 4) Match based on the combination of targets with the smallest |
| 179 | // corrections. |
| 180 | // 5) Calculate h and dhdx for each pair of targets. |
| 181 | // |
| 182 | // For the "magnitude" of the correction, we do not directly use the |
| 183 | // standard Kalman correction formula. Instead, we calculate the correction |
| 184 | // we would get from each component of the measurement and take the L2 norm |
| 185 | // of those. This prevents situations where a target matches very poorly but |
| 186 | // produces an overall correction of near-zero. |
| 187 | // TODO(james): I do not know if this is strictly the correct method to |
| 188 | // minimize likely error, but should be reasonable. |
| 189 | // |
| 190 | // For the matching, we do the following (see MatchFrames): |
| 191 | // 1. Compute the best max_targets_per_frame matches for each view. |
| 192 | // 2. Exhaust every possible combination of view/target pairs and |
| 193 | // choose the best one. |
| 194 | // When we don't think the camera should be able to see as many targets as |
| 195 | // we actually got in the frame, then we do permit doubling/tripling/etc. |
| 196 | // up on potential targets once we've exhausted all the targets we think |
| 197 | // we can see. |
| 198 | |
| 199 | // Set the current robot pose so that the cameras know where they are |
| 200 | // (all the cameras have robot_pose_ as their base): |
| 201 | *robot_pose_->mutable_pos() << X_hat(0, 0), X_hat(1, 0), 0.0; |
| 202 | robot_pose_->set_theta(X_hat(2, 0)); |
| 203 | |
| 204 | // Compute the things we *think* the camera should be seeing. |
| 205 | // Note: Because we will not try to match to any targets that are not |
| 206 | // returned by this function, we generally want the modelled camera to have |
| 207 | // a slightly larger field of view than the real camera, and be able to see |
| 208 | // slightly smaller targets. |
| 209 | const ::aos::SizedArray<TargetView, num_targets> camera_views = |
| 210 | camera.target_views(); |
| 211 | |
| 212 | // Each row contains the scores for each pair of target view and camera |
| 213 | // target view. Values in each row will not be populated past |
| 214 | // camera.target_views().size(); of the rows, only the first |
| 215 | // target_views.size() shall be populated. |
| 216 | // Higher scores imply a worse match. Zero implies a perfect match. |
| 217 | Eigen::Matrix<Scalar, max_targets_per_frame, num_targets> scores; |
| 218 | scores.setConstant(::std::numeric_limits<Scalar>::infinity()); |
| 219 | // Each row contains the indices of the best matches per view, where |
| 220 | // index 0 is the best, 1 the second best, and 2 the third, etc. |
| 221 | // -1 indicates an unfilled field. |
| 222 | Eigen::Matrix<int, max_targets_per_frame, max_targets_per_frame> |
| 223 | best_matches; |
| 224 | best_matches.setConstant(-1); |
| 225 | // The H matrices for each potential matching. This has the same structure |
| 226 | // as the scores matrix. |
| 227 | ::std::array<::std::array<Eigen::Matrix<Scalar, kNOutputs, kNStates>, |
| 228 | max_targets_per_frame>, |
| 229 | num_targets> all_H_matrices; |
| 230 | |
| 231 | // Iterate through and fill out the scores for each potential pairing: |
| 232 | for (size_t ii = 0; ii < target_views.size(); ++ii) { |
| 233 | const TargetView &target_view = target_views[ii]; |
| 234 | Output z; |
| 235 | Eigen::Matrix<Scalar, kNOutputs, kNOutputs> R; |
| 236 | TargetViewToMatrices(target_view, &z, &R); |
| 237 | |
| 238 | for (size_t jj = 0; jj < camera_views.size(); ++jj) { |
| 239 | // Compute the ckalman update for this step: |
| 240 | const TargetView &view = camera_views[jj]; |
| 241 | const Eigen::Matrix<Scalar, kNOutputs, kNStates> H = |
James Kuszmaul | 46f3a21 | 2019-03-10 10:14:24 -0700 | [diff] [blame] | 242 | HMatrix(*view.target, camera.pose()); |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 243 | const Eigen::Matrix<Scalar, kNStates, kNOutputs> PH = P * H.transpose(); |
| 244 | const Eigen::Matrix<Scalar, kNOutputs, kNOutputs> S = H * PH + R; |
| 245 | // Note: The inverse here should be very cheap so long as kNOutputs = 3. |
| 246 | const Eigen::Matrix<Scalar, kNStates, kNOutputs> K = PH * S.inverse(); |
| 247 | const Output err = z - Output(view.reading.heading, |
| 248 | view.reading.distance, view.reading.skew); |
| 249 | // In order to compute the actual score, we want to consider each |
| 250 | // component of the error separately, as well as considering the impacts |
| 251 | // on the each of the states separately. As such, we calculate what |
| 252 | // the separate updates from each error component would be, and sum |
| 253 | // the impacts on the states. |
| 254 | Output scorer; |
| 255 | for (size_t kk = 0; kk < kNOutputs; ++kk) { |
| 256 | // TODO(james): squaredNorm or norm or L1-norm? Do we care about the |
| 257 | // square root? Do we prefer a quadratic or linear response? |
| 258 | scorer(kk, 0) = (K.col(kk) * err(kk, 0)).squaredNorm(); |
| 259 | } |
| 260 | // Compute the overall score--note that we add in a term for the height, |
| 261 | // scaled by a manual fudge-factor. The height is not accounted for |
| 262 | // in the Kalman update because we are not trying to estimate the height |
| 263 | // of the robot directly. |
| 264 | Scalar score = |
| 265 | scorer.squaredNorm() + |
| 266 | ::std::pow((view.reading.height - target_view.reading.height) / |
| 267 | target_view.noise.height / 20.0, |
| 268 | 2); |
| 269 | scores(ii, jj) = score; |
| 270 | all_H_matrices[ii][jj] = H; |
| 271 | |
| 272 | // Update the best_matches matrix: |
| 273 | int insert_target = jj; |
| 274 | for (size_t kk = 0; kk < max_targets_per_frame; ++kk) { |
| 275 | int idx = best_matches(ii, kk); |
| 276 | // Note that -1 indicates an unfilled value. |
| 277 | if (idx == -1 || scores(ii, idx) > scores(ii, insert_target)) { |
| 278 | best_matches(ii, kk) = insert_target; |
| 279 | insert_target = idx; |
| 280 | if (idx == -1) { |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | if (camera_views.size() == 0) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 289 | AOS_LOG(DEBUG, "Unable to identify potential target matches.\n"); |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 290 | // If we can't get a match, provide H = zero, which will make this |
| 291 | // correction step a nop. |
| 292 | *h = [](const State &, const Input &) { return Output::Zero(); }; |
| 293 | *dhdx = [](const State &) { |
| 294 | return Eigen::Matrix<Scalar, kNOutputs, kNStates>::Zero(); |
| 295 | }; |
| 296 | for (size_t ii = 0; ii < target_views.size(); ++ii) { |
| 297 | h_functions->push_back(*h); |
| 298 | dhdx_functions->push_back(*dhdx); |
| 299 | } |
| 300 | } else { |
| 301 | // Go through and brute force the issue of what the best combination of |
| 302 | // target matches are. The worst case for this algorithm will be |
| 303 | // max_targets_per_frame!, which is awful for any N > ~4, but since |
| 304 | // max_targets_per_frame = 3, I'm not really worried. |
| 305 | ::std::array<int, max_targets_per_frame> best_frames = |
| 306 | MatchFrames(scores, best_matches, target_views.size()); |
| 307 | for (size_t ii = 0; ii < target_views.size(); ++ii) { |
James Kuszmaul | 6f941b7 | 2019-03-08 18:12:25 -0800 | [diff] [blame] | 308 | size_t view_idx = best_frames[ii]; |
| 309 | if (view_idx < 0 || view_idx >= camera_views.size()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 310 | AOS_LOG(ERROR, "Somehow, the view scorer failed.\n"); |
James Kuszmaul | 074429e | 2019-03-23 16:01:49 -0700 | [diff] [blame] | 311 | h_functions->push_back( |
| 312 | [](const State &, const Input &) { return Output::Zero(); }); |
| 313 | dhdx_functions->push_back([](const State &) { |
| 314 | return Eigen::Matrix<Scalar, kNOutputs, kNStates>::Zero(); |
| 315 | }); |
James Kuszmaul | 6f941b7 | 2019-03-08 18:12:25 -0800 | [diff] [blame] | 316 | continue; |
| 317 | } |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 318 | const Eigen::Matrix<Scalar, kNOutputs, kNStates> best_H = |
| 319 | all_H_matrices[ii][view_idx]; |
| 320 | const TargetView best_view = camera_views[view_idx]; |
| 321 | const TargetView target_view = target_views[ii]; |
| 322 | const Scalar match_score = scores(ii, view_idx); |
| 323 | if (match_score > kRejectionScore) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 324 | AOS_LOG(DEBUG, |
| 325 | "Rejecting target at (%f, %f, %f, %f) due to high score.\n", |
| 326 | target_view.reading.heading, target_view.reading.distance, |
| 327 | target_view.reading.skew, target_view.reading.height); |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 328 | h_functions->push_back( |
| 329 | [](const State &, const Input &) { return Output::Zero(); }); |
| 330 | dhdx_functions->push_back([](const State &) { |
| 331 | return Eigen::Matrix<Scalar, kNOutputs, kNStates>::Zero(); |
| 332 | }); |
| 333 | } else { |
| 334 | h_functions->push_back([this, &camera, best_view, target_view]( |
| 335 | const State &X, const Input &) { |
| 336 | // This function actually handles determining what the Output should |
| 337 | // be at a given state, now that we have chosen the target that |
| 338 | // we want to match to. |
| 339 | *robot_pose_->mutable_pos() << X(0, 0), X(1, 0), 0.0; |
| 340 | robot_pose_->set_theta(X(2, 0)); |
| 341 | const Pose relative_pose = |
| 342 | best_view.target->pose().Rebase(&camera.pose()); |
| 343 | const Scalar heading = relative_pose.heading(); |
| 344 | const Scalar distance = relative_pose.xy_norm(); |
James Kuszmaul | 289756f | 2019-03-05 21:52:10 -0800 | [diff] [blame] | 345 | const Scalar skew = ::aos::math::NormalizeAngle( |
| 346 | relative_pose.rel_theta() - heading); |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 347 | return Output(heading, distance, skew); |
| 348 | }); |
| 349 | |
| 350 | // TODO(james): Experiment to better understand whether we want to |
| 351 | // recalculate H or not. |
| 352 | dhdx_functions->push_back( |
| 353 | [best_H](const Eigen::Matrix<Scalar, kNStates, 1> &) { |
| 354 | return best_H; |
| 355 | }); |
| 356 | } |
| 357 | } |
| 358 | *h = h_functions->at(0); |
| 359 | *dhdx = dhdx_functions->at(0); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | Eigen::Matrix<Scalar, kNOutputs, kNStates> HMatrix( |
James Kuszmaul | 46f3a21 | 2019-03-10 10:14:24 -0700 | [diff] [blame] | 364 | const Target &target, const Pose &camera_pose) { |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 365 | // To calculate dheading/d{x,y,theta}: |
| 366 | // heading = arctan2(target_pos - camera_pos) - camera_theta |
| 367 | Eigen::Matrix<Scalar, 3, 1> target_pos = target.pose().abs_pos(); |
James Kuszmaul | 46f3a21 | 2019-03-10 10:14:24 -0700 | [diff] [blame] | 368 | Eigen::Matrix<Scalar, 3, 1> camera_pos = camera_pose.abs_pos(); |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 369 | Scalar diffx = target_pos.x() - camera_pos.x(); |
| 370 | Scalar diffy = target_pos.y() - camera_pos.y(); |
| 371 | Scalar norm2 = diffx * diffx + diffy * diffy; |
| 372 | Scalar dheadingdx = diffy / norm2; |
| 373 | Scalar dheadingdy = -diffx / norm2; |
| 374 | Scalar dheadingdtheta = -1.0; |
| 375 | |
| 376 | // To calculate ddistance/d{x,y}: |
| 377 | // distance = sqrt(diffx^2 + diffy^2) |
| 378 | Scalar distance = ::std::sqrt(norm2); |
| 379 | Scalar ddistdx = -diffx / distance; |
| 380 | Scalar ddistdy = -diffy / distance; |
| 381 | |
James Kuszmaul | 289756f | 2019-03-05 21:52:10 -0800 | [diff] [blame] | 382 | // Skew = target.theta - camera.theta - heading |
| 383 | // = target.theta - arctan2(target_pos - camera_pos) |
| 384 | Scalar dskewdx = -dheadingdx; |
| 385 | Scalar dskewdy = -dheadingdy; |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 386 | Eigen::Matrix<Scalar, kNOutputs, kNStates> H; |
| 387 | H.setZero(); |
| 388 | H(0, 0) = dheadingdx; |
| 389 | H(0, 1) = dheadingdy; |
| 390 | H(0, 2) = dheadingdtheta; |
| 391 | H(1, 0) = ddistdx; |
| 392 | H(1, 1) = ddistdy; |
James Kuszmaul | 289756f | 2019-03-05 21:52:10 -0800 | [diff] [blame] | 393 | H(2, 0) = dskewdx; |
| 394 | H(2, 1) = dskewdy; |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 395 | return H; |
| 396 | } |
| 397 | |
| 398 | // A helper function for the fuller version of MatchFrames; this just |
| 399 | // removes some of the arguments that are only needed during the recursion. |
| 400 | // n_views is the number of targets actually seen in the camera image (i.e., |
| 401 | // the number of rows in scores/best_matches that are actually populated). |
| 402 | ::std::array<int, max_targets_per_frame> MatchFrames( |
| 403 | const Eigen::Matrix<Scalar, max_targets_per_frame, num_targets> &scores, |
| 404 | const Eigen::Matrix<int, max_targets_per_frame, max_targets_per_frame> & |
| 405 | best_matches, |
| 406 | int n_views) { |
| 407 | ::std::array<int, max_targets_per_frame> best_set; |
James Kuszmaul | 6f941b7 | 2019-03-08 18:12:25 -0800 | [diff] [blame] | 408 | best_set.fill(-1); |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 409 | Scalar best_score; |
| 410 | // We start out without having "used" any views/targets: |
| 411 | ::aos::SizedArray<bool, max_targets_per_frame> used_views; |
| 412 | for (int ii = 0; ii < n_views; ++ii) { |
| 413 | used_views.push_back(false); |
| 414 | } |
| 415 | MatchFrames(scores, best_matches, used_views, {{false}}, &best_set, |
| 416 | &best_score); |
| 417 | return best_set; |
| 418 | } |
| 419 | |
| 420 | // Recursively iterates over every plausible combination of targets/views |
| 421 | // that there is and determines the lowest-scoring combination. |
| 422 | // used_views and used_targets indicate which rows/columns of the |
| 423 | // scores/best_matches matrices should be ignored. When used_views is all |
| 424 | // true, that means that we are done recursing. |
| 425 | void MatchFrames( |
| 426 | const Eigen::Matrix<Scalar, max_targets_per_frame, num_targets> &scores, |
| 427 | const Eigen::Matrix<int, max_targets_per_frame, max_targets_per_frame> & |
| 428 | best_matches, |
| 429 | const ::aos::SizedArray<bool, max_targets_per_frame> &used_views, |
| 430 | const ::std::array<bool, num_targets> &used_targets, |
| 431 | ::std::array<int, max_targets_per_frame> *best_set, Scalar *best_score) { |
| 432 | *best_score = ::std::numeric_limits<Scalar>::infinity(); |
| 433 | // Iterate by letting each target in the camera frame (that isn't in |
| 434 | // used_views) choose it's best match that isn't already taken. We then set |
| 435 | // the appropriate flags in used_views and used_targets and call MatchFrames |
| 436 | // to let all the other views sort themselves out. |
| 437 | for (size_t ii = 0; ii < used_views.size(); ++ii) { |
| 438 | if (used_views[ii]) { |
| 439 | continue; |
| 440 | } |
| 441 | int best_match = -1; |
| 442 | for (size_t jj = 0; jj < max_targets_per_frame; ++jj) { |
| 443 | if (best_matches(ii, jj) == -1) { |
| 444 | // If we run out of potential targets from the camera, then there |
| 445 | // are more targets in the frame than we think there should be. |
| 446 | // In this case, we are going to be doubling/tripling/etc. up |
| 447 | // anyhow. So we just give everyone their top choice: |
| 448 | // TODO(james): If we ever are dealing with larger numbers of |
| 449 | // targets per frame, do something to minimize doubling-up. |
| 450 | best_match = best_matches(ii, 0); |
| 451 | break; |
| 452 | } |
| 453 | best_match = best_matches(ii, jj); |
| 454 | if (!used_targets[best_match]) { |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | // If we reach here and best_match = -1, that means that no potential |
| 459 | // targets were generated by the camera, and we should never have gotten |
| 460 | // here. |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 461 | AOS_CHECK(best_match != -1); |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 462 | ::aos::SizedArray<bool, max_targets_per_frame> sub_views = used_views; |
| 463 | sub_views[ii] = true; |
| 464 | ::std::array<bool, num_targets> sub_targets = used_targets; |
| 465 | sub_targets[best_match] = true; |
| 466 | ::std::array<int, max_targets_per_frame> sub_best_set; |
| 467 | Scalar score; |
| 468 | MatchFrames(scores, best_matches, sub_views, sub_targets, &sub_best_set, |
| 469 | &score); |
| 470 | score += scores(ii, best_match); |
| 471 | sub_best_set[ii] = best_match; |
| 472 | if (score < *best_score) { |
| 473 | *best_score = score; |
| 474 | *best_set = sub_best_set; |
| 475 | } |
| 476 | } |
| 477 | // best_score will be infinite if we did not find a result due to there |
| 478 | // being no targets that weren't set in used_vies; this is the |
| 479 | // base case of the recursion and so we set best_score to zero: |
| 480 | if (!::std::isfinite(*best_score)) { |
| 481 | *best_score = 0.0; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | // The pose that is used by the cameras to determine the location of the robot |
| 486 | // and thus the expected view of the targets. |
| 487 | Pose *robot_pose_; |
| 488 | }; // class TypedLocalizer |
| 489 | |
| 490 | } // namespace control_loops |
| 491 | } // namespace y2019 |
| 492 | |
| 493 | #endif // Y2019_CONTROL_LOOPS_DRIVETRAIN_LOCALIZATER_H_ |