00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef CKalmanFilterCapable_H
00029 #define CKalmanFilterCapable_H
00030
00031 #include <mrpt/math/CMatrixFixedNumeric.h>
00032 #include <mrpt/math/CMatrixTemplateNumeric.h>
00033 #include <mrpt/math/CVectorTemplate.h>
00034 #include <mrpt/math/CArray.h>
00035 #include <mrpt/math/utils.h>
00036
00037 #include <mrpt/utils/CTimeLogger.h>
00038 #include <mrpt/utils/CLoadableOptions.h>
00039 #include <mrpt/utils/CDebugOutputCapable.h>
00040 #include <mrpt/utils/stl_extensions.h>
00041 #include <mrpt/system/os.h>
00042 #include <mrpt/utils/CTicTac.h>
00043 #include <mrpt/utils/CFileOutputStream.h>
00044
00045 #include <mrpt/bayes/link_pragmas.h>
00046
00047
00048 namespace mrpt
00049 {
00050 namespace bayes
00051 {
00052 using namespace mrpt::utils;
00053 using namespace mrpt::math;
00054 using namespace mrpt;
00055 using namespace std;
00056
00057
00058
00059
00060
00061 enum TKFMethod {
00062 kfEKFNaive = 0,
00063 kfEKFAlaDavison,
00064 kfIKFFull,
00065 kfIKF
00066 };
00067
00068
00069
00070 struct BAYES_IMPEXP TKF_options : public utils::CLoadableOptions
00071 {
00072 TKF_options();
00073
00074 void loadFromConfigFile(
00075 const mrpt::utils::CConfigFileBase &source,
00076 const std::string §ion);
00077
00078
00079 void dumpToTextStream(CStream &out) const;
00080
00081 TKFMethod method;
00082 bool verbose;
00083 int IKF_iterations;
00084 bool enable_profiler;
00085 bool use_analytic_transition_jacobian;
00086 bool use_analytic_observation_jacobian;
00087 bool debug_verify_analytic_jacobians;
00088 double debug_verify_analytic_jacobians_threshold;
00089 };
00090
00091
00092 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t FEAT_SIZE, size_t ACT_SIZE, typename KFTYPE> class CKalmanFilterCapable;
00093
00094
00095 namespace detail
00096 {
00097
00098 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t FEAT_SIZE, size_t ACT_SIZE, typename KFTYPE>
00099 void runOneKalmanIteration_addNewLandmarks(
00100 CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,FEAT_SIZE,ACT_SIZE,KFTYPE> &obj,
00101 std::vector<typename CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,FEAT_SIZE,ACT_SIZE,KFTYPE>::KFArray_OBS> Z,
00102 const mrpt::vector_int &data_association,
00103 const typename CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,FEAT_SIZE,ACT_SIZE,KFTYPE>::KFMatrix_OxO &R
00104 );
00105
00106 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t ACT_SIZE, typename KFTYPE>
00107 void runOneKalmanIteration_addNewLandmarks(
00108 CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,0 /* FEAT_SIZE=0 */,ACT_SIZE,KFTYPE> &obj,
00109 std::vector<typename CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,0 /* FEAT_SIZE=0 */,ACT_SIZE,KFTYPE>::KFArray_OBS> Z,
00110 const mrpt::vector_int &data_association,
00111 const typename CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,0 /* FEAT_SIZE=0 */,ACT_SIZE,KFTYPE>::KFMatrix_OxO &R
00112 );
00113
00114 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t FEAT_SIZE, size_t ACT_SIZE, typename KFTYPE>
00115 inline size_t getNumberOfLandmarksInMap(const CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,FEAT_SIZE,ACT_SIZE,KFTYPE> &obj);
00116
00117 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t ACT_SIZE, typename KFTYPE>
00118 inline size_t getNumberOfLandmarksInMap(const CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,0 /*FEAT_SIZE*/,ACT_SIZE,KFTYPE> &obj);
00119
00120 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t FEAT_SIZE, size_t ACT_SIZE, typename KFTYPE>
00121 inline bool isMapEmpty(const CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,FEAT_SIZE,ACT_SIZE,KFTYPE> &obj);
00122
00123 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t ACT_SIZE, typename KFTYPE>
00124 inline bool isMapEmpty(const CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,0 /*FEAT_SIZE*/,ACT_SIZE,KFTYPE> &obj);
00125 }
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t FEAT_SIZE, size_t ACT_SIZE, typename KFTYPE = double>
00155 class CKalmanFilterCapable : public mrpt::utils::CDebugOutputCapable
00156 {
00157 public:
00158 static inline size_t get_vehicle_size() { return VEH_SIZE; }
00159 static inline size_t get_observation_size() { return OBS_SIZE; }
00160 static inline size_t get_feature_size() { return FEAT_SIZE; }
00161 static inline size_t get_action_size() { return ACT_SIZE; }
00162 inline size_t getNumberOfLandmarksInTheMap() const { return detail::getNumberOfLandmarksInMap(*this); }
00163 inline bool isMapEmpty() const { return detail::isMapEmpty(*this); }
00164
00165
00166 typedef KFTYPE kftype;
00167 typedef CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,FEAT_SIZE,ACT_SIZE,KFTYPE> KFCLASS;
00168
00169
00170 typedef CVectorTemplate<KFTYPE> KFVector;
00171 typedef CMatrixTemplateNumeric<KFTYPE> KFMatrix;
00172
00173 typedef CMatrixFixedNumeric<KFTYPE,VEH_SIZE,VEH_SIZE> KFMatrix_VxV;
00174 typedef CMatrixFixedNumeric<KFTYPE,OBS_SIZE,OBS_SIZE> KFMatrix_OxO;
00175 typedef CMatrixFixedNumeric<KFTYPE,FEAT_SIZE,FEAT_SIZE> KFMatrix_FxF;
00176 typedef CMatrixFixedNumeric<KFTYPE,ACT_SIZE,ACT_SIZE> KFMatrix_AxA;
00177
00178 typedef CMatrixFixedNumeric<KFTYPE,VEH_SIZE,OBS_SIZE> KFMatrix_VxO;
00179 typedef CMatrixFixedNumeric<KFTYPE,VEH_SIZE,FEAT_SIZE> KFMatrix_VxF;
00180
00181 typedef CMatrixFixedNumeric<KFTYPE,FEAT_SIZE,VEH_SIZE> KFMatrix_FxV;
00182 typedef CMatrixFixedNumeric<KFTYPE,FEAT_SIZE,OBS_SIZE> KFMatrix_FxO;
00183
00184 typedef CMatrixFixedNumeric<KFTYPE,OBS_SIZE,FEAT_SIZE> KFMatrix_OxF;
00185 typedef CMatrixFixedNumeric<KFTYPE,OBS_SIZE,VEH_SIZE> KFMatrix_OxV;
00186
00187 typedef CArrayNumeric<KFTYPE,VEH_SIZE> KFArray_VEH;
00188 typedef CArrayNumeric<KFTYPE,ACT_SIZE> KFArray_ACT;
00189 typedef CArrayNumeric<KFTYPE,OBS_SIZE> KFArray_OBS;
00190 typedef CArrayNumeric<KFTYPE,FEAT_SIZE> KFArray_FEAT;
00191
00192 inline size_t getStateVectorLength() const { return m_xkk.size(); }
00193
00194
00195
00196
00197 inline void getLandmarkMean(size_t idx, KFArray_FEAT &feat ) const {
00198 ASSERT_(idx<getNumberOfLandmarksInTheMap())
00199 ::memcpy(&feat[0], &m_xkk[VEH_SIZE+idx*FEAT_SIZE], FEAT_SIZE*sizeof(m_xkk[0]));
00200 }
00201
00202
00203
00204 inline void getLandmarkCov(size_t idx, KFMatrix_FxF &feat_cov ) const {
00205 m_pkk.extractMatrix(VEH_SIZE+idx*FEAT_SIZE,VEH_SIZE+idx*FEAT_SIZE,feat_cov);
00206 }
00207
00208 protected:
00209
00210
00211
00212 KFVector m_xkk;
00213 KFMatrix m_pkk;
00214
00215
00216
00217 mrpt::utils::CTimeLogger m_timLogger;
00218
00219
00220
00221
00222
00223
00224
00225
00226 virtual void OnGetAction( KFArray_ACT &out_u ) const = 0;
00227
00228
00229
00230
00231
00232
00233
00234 virtual void OnTransitionModel(
00235 const KFArray_ACT &in_u,
00236 KFArray_VEH &inout_x,
00237 bool &out_skipPrediction
00238 ) const = 0;
00239
00240
00241
00242
00243
00244 virtual void OnTransitionJacobian( KFMatrix_VxV &out_F ) const
00245 {
00246 m_user_didnt_implement_jacobian=true;
00247 }
00248
00249
00250
00251 virtual void OnTransitionJacobianNumericGetIncrements(KFArray_VEH &out_increments) const
00252 {
00253 std::fill_n(&out_increments[0], VEH_SIZE, 1e-6);
00254 }
00255
00256
00257
00258
00259
00260 virtual void OnTransitionNoise( KFMatrix_VxV &out_Q ) const = 0;
00261
00262
00263
00264
00265
00266
00267
00268
00269 virtual void OnPreComputingPredictions(
00270 const std::vector<KFArray_OBS> &in_all_prediction_means,
00271 mrpt::vector_size_t &out_LM_indices_to_predict ) const
00272 {
00273
00274 const size_t N = this->getNumberOfLandmarksInTheMap();
00275 out_LM_indices_to_predict.resize(N);
00276 for (size_t i=0;i<N;i++) out_LM_indices_to_predict[i]=i;
00277 }
00278
00279
00280
00281
00282
00283 virtual void OnGetObservationNoise(KFMatrix_OxO &out_R) const = 0;
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 virtual void OnGetObservationsAndDataAssociation(
00297 std::vector<KFArray_OBS> &out_z,
00298 mrpt::vector_int &out_data_association,
00299 const std::vector<KFArray_OBS> &in_all_predictions,
00300 const KFMatrix &in_S,
00301 const vector_size_t &in_lm_indices_in_S,
00302 const KFMatrix_OxO &in_R
00303 ) = 0;
00304
00305
00306
00307
00308
00309 virtual void OnObservationModel(
00310 const mrpt::vector_size_t &idx_landmarks_to_predict,
00311 std::vector<KFArray_OBS> &out_predictions
00312 ) const = 0;
00313
00314
00315
00316
00317
00318
00319 virtual void OnObservationJacobians(
00320 const size_t &idx_landmark_to_predict,
00321 KFMatrix_OxV &Hx,
00322 KFMatrix_OxF &Hy
00323 ) const
00324 {
00325 m_user_didnt_implement_jacobian=true;
00326 }
00327
00328
00329
00330 virtual void OnObservationJacobiansNumericGetIncrements(
00331 KFArray_VEH &out_veh_increments,
00332 KFArray_FEAT &out_feat_increments ) const
00333 {
00334 std::fill_n(&out_veh_increments[0], VEH_SIZE, 1e-6);
00335 std::fill_n(&out_feat_increments[0], FEAT_SIZE, 1e-6);
00336 }
00337
00338
00339
00340 virtual void OnSubstractObservationVectors(KFArray_OBS &A, const KFArray_OBS &B) const
00341 {
00342 A -= B;
00343 }
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358 virtual void OnInverseObservationModel(
00359 const KFArray_OBS & in_z,
00360 KFArray_FEAT & out_yn,
00361 KFMatrix_FxV & out_dyn_dxv,
00362 KFMatrix_FxO & out_dyn_dhn ) const
00363 {
00364 MRPT_START
00365 THROW_EXCEPTION("Inverse sensor model required but not implemented in derived class.")
00366 MRPT_END
00367 }
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391 virtual void OnInverseObservationModel(
00392 const KFArray_OBS & in_z,
00393 KFArray_FEAT & out_yn,
00394 KFMatrix_FxV & out_dyn_dxv,
00395 KFMatrix_FxO & out_dyn_dhn,
00396 KFMatrix_FxF & out_dyn_dhn_R_dyn_dhnT,
00397 bool & out_use_dyn_dhn_jacobian
00398 ) const
00399 {
00400 MRPT_START
00401 OnInverseObservationModel(in_z,out_yn,out_dyn_dxv,out_dyn_dhn);
00402 out_use_dyn_dhn_jacobian=true;
00403 MRPT_END
00404 }
00405
00406
00407
00408
00409
00410
00411 virtual void OnNewLandmarkAddedToMap(
00412 const size_t in_obsIdx,
00413 const size_t in_idxNewFeat )
00414 {
00415
00416 }
00417
00418
00419
00420 virtual void OnNormalizeStateVector()
00421 {
00422
00423 }
00424
00425
00426
00427 virtual void OnPostIteration()
00428 {
00429
00430 }
00431
00432
00433
00434
00435 public:
00436 CKalmanFilterCapable() {}
00437 virtual ~CKalmanFilterCapable() {}
00438
00439 mrpt::utils::CTimeLogger &getProfiler() { return m_timLogger; }
00440
00441 TKF_options KF_options;
00442
00443
00444 private:
00445
00446
00447
00448 vector<KFArray_OBS> all_predictions;
00449 vector_size_t predictLMidxs;
00450 KFMatrix dh_dx;
00451 KFMatrix dh_dx_full;
00452 vector_size_t idxs;
00453 KFMatrix S;
00454 KFMatrix Pkk_subset;
00455 vector<KFArray_OBS> Z;
00456 KFMatrix K;
00457 KFMatrix S_1;
00458 KFMatrix dh_dx_full_obs;
00459 KFMatrix aux_K_dh_dx;
00460
00461 protected:
00462
00463
00464
00465
00466
00467 void runOneKalmanIteration()
00468 {
00469 MRPT_START
00470
00471 m_timLogger.enable(KF_options.enable_profiler || KF_options.verbose);
00472 m_timLogger.enter("KF:complete_step");
00473
00474 ASSERT_(m_xkk.size()==m_pkk.getColCount())
00475 ASSERT_(m_xkk.size()>=VEH_SIZE)
00476
00477
00478
00479
00480 KFArray_ACT u;
00481
00482 m_timLogger.enter("KF:1.OnGetAction");
00483 OnGetAction(u);
00484 m_timLogger.leave("KF:1.OnGetAction");
00485
00486
00487 if (FEAT_SIZE) ASSERTDEB_( (m_xkk.size() - VEH_SIZE) % FEAT_SIZE == 0 );
00488
00489
00490
00491
00492 m_timLogger.enter("KF:2.prediction stage");
00493
00494 const size_t N_map = getNumberOfLandmarksInTheMap();
00495
00496 KFArray_VEH xv( &m_xkk[0] );
00497
00498 bool skipPrediction=false;
00499
00500
00501
00502 OnTransitionModel(u, xv, skipPrediction);
00503
00504 if ( !skipPrediction )
00505 {
00506
00507
00508
00509
00510 KFMatrix_VxV dfv_dxv;
00511
00512
00513 m_user_didnt_implement_jacobian=false;
00514 if (KF_options.use_analytic_transition_jacobian)
00515 OnTransitionJacobian(dfv_dxv);
00516
00517 if (m_user_didnt_implement_jacobian || !KF_options.use_analytic_transition_jacobian || KF_options.debug_verify_analytic_jacobians)
00518 {
00519 KFArray_VEH xkk_vehicle( &m_xkk[0] );
00520 KFArray_VEH xkk_veh_increments;
00521 OnTransitionJacobianNumericGetIncrements(xkk_veh_increments);
00522
00523 mrpt::math::estimateJacobian(
00524 xkk_vehicle,
00525 &KF_aux_estimate_trans_jacobian,
00526 xkk_veh_increments,
00527 std::make_pair<KFCLASS*,KFArray_ACT>(this,u),
00528 dfv_dxv);
00529
00530 if (KF_options.debug_verify_analytic_jacobians)
00531 {
00532 KFMatrix_VxV dfv_dxv_gt(UNINITIALIZED_MATRIX);
00533 OnTransitionJacobian(dfv_dxv_gt);
00534 if ((dfv_dxv-dfv_dxv_gt).Abs().sumAll()>KF_options.debug_verify_analytic_jacobians_threshold)
00535 {
00536 std::cerr << "[KalmanFilter] ERROR: User analytical transition Jacobians are wrong: \n"
00537 << " Real dfv_dxv: \n" << dfv_dxv << "\n Analytical dfv_dxv:\n" << dfv_dxv_gt << "Diff:\n" << (dfv_dxv-dfv_dxv_gt) << "\n";
00538 THROW_EXCEPTION("ERROR: User analytical transition Jacobians are wrong (More details dumped to cerr)")
00539 }
00540 }
00541
00542 }
00543
00544
00545 KFMatrix_VxV Q;
00546 OnTransitionNoise(Q);
00547
00548
00549
00550
00551 KFMatrix_VxV Pkk_new = Q;
00552
00553
00554
00555 dfv_dxv.multiply_HCHt(
00556 m_pkk,
00557 Pkk_new,
00558 true,
00559 true
00560 );
00561
00562
00563 m_pkk.insertMatrix(0,0, Pkk_new );
00564
00565
00566
00567
00568
00569 KFMatrix_VxF aux;
00570 for (size_t i=0 ; i<N_map ; i++)
00571 {
00572
00573 dfv_dxv.multiply_subMatrix(
00574 m_pkk,
00575 aux,
00576 VEH_SIZE+i*FEAT_SIZE,
00577 0,
00578 FEAT_SIZE
00579 );
00580
00581 m_pkk.insertMatrix (0, VEH_SIZE+i*FEAT_SIZE, aux );
00582 m_pkk.insertMatrixTranspose(VEH_SIZE+i*FEAT_SIZE, 0 , aux );
00583 }
00584
00585
00586
00587
00588 for (size_t i=0;i<VEH_SIZE;i++)
00589 m_xkk[i]=xv[i];
00590
00591
00592 OnNormalizeStateVector();
00593
00594 }
00595
00596
00597 const double tim_pred = m_timLogger.leave("KF:2.prediction stage");
00598
00599
00600
00601
00602
00603 m_timLogger.enter("KF:3.predict all obs");
00604
00605 KFMatrix_OxO R;
00606 OnGetObservationNoise(R);
00607
00608
00609
00610 all_predictions.resize(N_map);
00611 OnObservationModel(
00612 mrpt::math::sequence<size_t,1>(0,N_map),
00613 all_predictions);
00614
00615 const double tim_pred_obs = m_timLogger.leave("KF:3.predict all obs");
00616
00617 m_timLogger.enter("KF:4.decide pred obs");
00618
00619
00620 predictLMidxs.clear();
00621 if (FEAT_SIZE==0)
00622
00623 predictLMidxs.assign(1,0);
00624 else
00625
00626 OnPreComputingPredictions(all_predictions, predictLMidxs);
00627
00628
00629 m_timLogger.leave("KF:4.decide pred obs");
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655 m_timLogger.enter("KF:5.build Jacobians");
00656
00657 const size_t N_pred = FEAT_SIZE==0 ?
00658 1 :
00659 predictLMidxs.size();
00660
00661 dh_dx.zeros(N_pred*OBS_SIZE, VEH_SIZE + FEAT_SIZE * N_pred );
00662 dh_dx_full.zeros(N_pred*OBS_SIZE, VEH_SIZE + FEAT_SIZE * N_map );
00663
00664
00665 idxs.clear();
00666 idxs.reserve(VEH_SIZE+N_pred*FEAT_SIZE);
00667
00668 for (size_t i=0;i<VEH_SIZE;i++) idxs.push_back(i);
00669
00670 for (size_t i=0;i<N_pred;++i)
00671 {
00672 const size_t lm_idx = FEAT_SIZE==0 ? 0 : predictLMidxs[i];
00673 KFMatrix_OxV Hx(UNINITIALIZED_MATRIX);
00674 KFMatrix_OxF Hy(UNINITIALIZED_MATRIX);
00675
00676
00677 m_user_didnt_implement_jacobian=false;
00678 if (KF_options.use_analytic_transition_jacobian)
00679 OnObservationJacobians(lm_idx,Hx,Hy);
00680
00681 if (m_user_didnt_implement_jacobian || !KF_options.use_analytic_observation_jacobian || KF_options.debug_verify_analytic_jacobians)
00682 {
00683 const size_t lm_idx_in_statevector = VEH_SIZE+lm_idx*FEAT_SIZE;
00684
00685 const KFArray_VEH x_vehicle( &m_xkk[0] );
00686 const KFArray_FEAT x_feat( &m_xkk[lm_idx_in_statevector] );
00687
00688 KFArray_VEH xkk_veh_increments;
00689 KFArray_FEAT feat_increments;
00690 OnObservationJacobiansNumericGetIncrements(xkk_veh_increments, feat_increments);
00691
00692 mrpt::math::estimateJacobian(
00693 x_vehicle,
00694 &KF_aux_estimate_obs_Hx_jacobian,
00695 xkk_veh_increments,
00696 std::make_pair<KFCLASS*,size_t>(this,lm_idx),
00697 Hx);
00698
00699 ::memcpy(&m_xkk[0],&x_vehicle[0],sizeof(m_xkk[0])*VEH_SIZE);
00700
00701 mrpt::math::estimateJacobian(
00702 x_feat,
00703 &KF_aux_estimate_obs_Hy_jacobian,
00704 feat_increments,
00705 std::make_pair<KFCLASS*,size_t>(this,lm_idx),
00706 Hy);
00707
00708 ::memcpy(&m_xkk[lm_idx_in_statevector],&x_feat[0],sizeof(m_xkk[0])*FEAT_SIZE);
00709
00710 if (KF_options.debug_verify_analytic_jacobians)
00711 {
00712 KFMatrix_OxV Hx_gt(UNINITIALIZED_MATRIX);
00713 KFMatrix_OxF Hy_gt(UNINITIALIZED_MATRIX);
00714 OnObservationJacobians(lm_idx,Hx_gt,Hy_gt);
00715 if ((Hx-Hx_gt).Abs().sumAll()>KF_options.debug_verify_analytic_jacobians_threshold) {
00716 std::cerr << "[KalmanFilter] ERROR: User analytical observation Hx Jacobians are wrong: \n"
00717 << " Real Hx: \n" << Hx << "\n Analytical Hx:\n" << Hx_gt << "Diff:\n" << Hx-Hx_gt << "\n";
00718 THROW_EXCEPTION("ERROR: User analytical observation Hx Jacobians are wrong (More details dumped to cerr)")
00719 }
00720 if ((Hy-Hy_gt).Abs().sumAll()>KF_options.debug_verify_analytic_jacobians_threshold) {
00721 std::cerr << "[KalmanFilter] ERROR: User analytical observation Hy Jacobians are wrong: \n"
00722 << " Real Hy: \n" << Hy << "\n Analytical Hx:\n" << Hy_gt << "Diff:\n" << Hy-Hy_gt << "\n";
00723 THROW_EXCEPTION("ERROR: User analytical observation Hy Jacobians are wrong (More details dumped to cerr)")
00724 }
00725 }
00726 }
00727
00728 dh_dx.insertMatrix(i*OBS_SIZE,0, Hx);
00729 if (FEAT_SIZE!=0)
00730 dh_dx.insertMatrix(i*OBS_SIZE,VEH_SIZE+i*FEAT_SIZE, Hy);
00731
00732 dh_dx_full.insertMatrix(i*OBS_SIZE,0, Hx);
00733 if (FEAT_SIZE!=0)
00734 {
00735 dh_dx_full.insertMatrix(i*OBS_SIZE,VEH_SIZE+lm_idx*FEAT_SIZE, Hy);
00736
00737 for (size_t k=0;k<FEAT_SIZE;k++)
00738 idxs.push_back(k+VEH_SIZE+FEAT_SIZE*lm_idx);
00739 }
00740 }
00741 m_timLogger.leave("KF:5.build Jacobians");
00742
00743
00744
00745
00746 m_timLogger.enter("KF:6.build S");
00747
00748 S.setSize(N_pred*OBS_SIZE,N_pred*OBS_SIZE);
00749
00750
00751
00752 m_pkk.extractSubmatrixSymmetrical(idxs,Pkk_subset);
00753
00754
00755 dh_dx.multiply_HCHt(Pkk_subset,S);
00756
00757
00758 if ( FEAT_SIZE>0 )
00759 {
00760 for (size_t i=0;i<N_pred;++i)
00761 {
00762 const size_t obs_idx_off = i*OBS_SIZE;
00763 for (size_t j=0;j<OBS_SIZE;j++)
00764 for (size_t k=0;k<OBS_SIZE;k++)
00765 S.get_unsafe(obs_idx_off+j,obs_idx_off+k) += R.get_unsafe(j,k);
00766 }
00767 }
00768 else
00769 {
00770 ASSERTDEB_(S.getColCount() == OBS_SIZE );
00771 S+=R;
00772 }
00773
00774 m_timLogger.leave("KF:6.build S");
00775
00776
00777 Z.clear();
00778 vector_int data_association;
00779
00780 m_timLogger.enter("KF:7.get obs & DA");
00781
00782
00783 OnGetObservationsAndDataAssociation(
00784 Z, data_association,
00785 all_predictions, S, predictLMidxs, R
00786 );
00787
00788 ASSERTDEB_(data_association.size()==Z.size() || (data_association.empty() && FEAT_SIZE==0));
00789
00790 const double tim_obs_DA = m_timLogger.leave("KF:7.get obs & DA");
00791
00792
00793
00794
00795
00796 if ( !Z.empty() )
00797 {
00798 m_timLogger.enter("KF:8.update stage");
00799
00800 switch (KF_options.method)
00801 {
00802
00803
00804
00805 case kfEKFNaive:
00806 case kfIKFFull:
00807 {
00808
00809
00810
00811 vector_int mapIndicesForKFUpdate(data_association.size());
00812 mapIndicesForKFUpdate.resize( std::distance(mapIndicesForKFUpdate.begin(),
00813 std::remove_copy_if(
00814 data_association.begin(),
00815 data_association.end(),
00816 mapIndicesForKFUpdate.begin(),
00817 binder1st<equal_to<int> >(equal_to<int>(),-1) ) ) );
00818
00819 const size_t N_upd = (FEAT_SIZE==0) ?
00820 1 :
00821 mapIndicesForKFUpdate.size();
00822
00823
00824 const size_t nKF_iterations = (KF_options.method==kfEKFNaive) ? 1 : KF_options.IKF_iterations;
00825
00826 const KFVector xkk_0 = m_xkk;
00827
00828
00829 if (N_upd>0)
00830 {
00831 for (size_t IKF_iteration=0;IKF_iteration<nKF_iterations;IKF_iteration++)
00832 {
00833
00834 KFVector ytilde;
00835 ytilde.reserve(OBS_SIZE*N_upd);
00836
00837
00838 dh_dx_full_obs.zeros(N_upd*OBS_SIZE, VEH_SIZE + FEAT_SIZE * N_map );
00839 KFMatrix S_observed;
00840
00841 if (FEAT_SIZE!=0)
00842 {
00843 vector_size_t S_idxs;
00844 S_idxs.reserve(OBS_SIZE*N_upd);
00845
00846 for (size_t i=0;i<data_association.size();++i)
00847 {
00848 if (data_association[i]<0) continue;
00849
00850 const size_t assoc_idx_in_map = static_cast<size_t>(data_association[i]);
00851 const size_t assoc_idx_in_pred = mrpt::utils::find_in_vector(assoc_idx_in_map, predictLMidxs);
00852 ASSERTMSG_(assoc_idx_in_pred!=string::npos, "OnPreComputingPredictions() didn't recommend the prediction of a landmark which has been actually observed!")
00853
00854
00855
00856 for (size_t k=0;k<OBS_SIZE;k++)
00857 {
00858 ::memcpy(
00859 dh_dx_full_obs.get_unsafe_row(S_idxs.size()),
00860 dh_dx_full.get_unsafe_row(assoc_idx_in_pred*OBS_SIZE + k ),
00861 sizeof(dh_dx_full(0,0))*(VEH_SIZE + FEAT_SIZE * N_map) );
00862 S_idxs.push_back(assoc_idx_in_pred*OBS_SIZE+k);
00863 }
00864
00865
00866 KFArray_OBS ytilde_i = Z[i];
00867 OnSubstractObservationVectors(ytilde_i,all_predictions[predictLMidxs[assoc_idx_in_pred]]);
00868 for (size_t k=0;k<OBS_SIZE;k++)
00869 ytilde.push_back( ytilde_i[k] );
00870 }
00871
00872 S.extractSubmatrixSymmetrical(S_idxs,S_observed);
00873 }
00874 else
00875 {
00876 ASSERT_(Z.size()==1 && all_predictions.size()==1)
00877
00878 dh_dx_full_obs = dh_dx_full;
00879 KFArray_OBS ytilde_i = Z[0];
00880 OnSubstractObservationVectors(ytilde_i,all_predictions[0]);
00881 for (size_t k=0;k<OBS_SIZE;k++)
00882 ytilde.push_back( ytilde_i[k] );
00883
00884 S_observed = S;
00885 }
00886
00887
00888
00889 m_timLogger.enter("KF:8.update stage:1.FULLKF:build K");
00890
00891 K.setSize(m_pkk.getRowCount(), S_observed.getColCount() );
00892
00893
00894 K.multiply_ABt(m_pkk, dh_dx_full_obs);
00895
00896
00897 S_observed.inv(S_1);
00898 K*=S_1;
00899
00900 m_timLogger.leave("KF:8.update stage:1.FULLKF:build K");
00901
00902
00903 if (nKF_iterations==1)
00904 {
00905 m_timLogger.enter("KF:8.update stage:2.FULLKF:update xkk");
00906
00907 K.multiply_Ab(
00908 ytilde,
00909 m_xkk,
00910 true
00911 );
00912
00913 m_timLogger.leave("KF:8.update stage:2.FULLKF:update xkk");
00914 }
00915 else
00916 {
00917 m_timLogger.enter("KF:8.update stage:2.FULLKF:iter.update xkk");
00918
00919 KFVector HAx_column;
00920 dh_dx_full_obs.multiply_Ab( m_xkk - xkk_0, HAx_column);
00921
00922 m_xkk = xkk_0;
00923 K.multiply_Ab(
00924 (ytilde-HAx_column),
00925 m_xkk,
00926 true
00927 );
00928
00929 m_timLogger.leave("KF:8.update stage:2.FULLKF:iter.update xkk");
00930 }
00931
00932
00933
00934 if (IKF_iteration == (nKF_iterations-1) )
00935 {
00936 m_timLogger.enter("KF:8.update stage:3.FULLKF:update Pkk");
00937
00938
00939
00940
00941
00942
00943 aux_K_dh_dx.multiply(K,dh_dx_full_obs);
00944
00945
00946 const size_t stat_len = aux_K_dh_dx.getColCount();
00947 for (size_t r=0;r<stat_len;r++)
00948 for (size_t c=0;c<stat_len;c++)
00949 if (r==c)
00950 aux_K_dh_dx.get_unsafe(r,c)=-aux_K_dh_dx.get_unsafe(r,c) + kftype(1);
00951 else aux_K_dh_dx.get_unsafe(r,c)=-aux_K_dh_dx.get_unsafe(r,c);
00952
00953 m_pkk.multiply_result_is_symmetric(aux_K_dh_dx, m_pkk );
00954
00955 m_timLogger.leave("KF:8.update stage:3.FULLKF:update Pkk");
00956 }
00957 }
00958 }
00959 }
00960 break;
00961
00962
00963
00964
00965 case kfEKFAlaDavison:
00966 {
00967
00968 for (size_t obsIdx=0;obsIdx<Z.size();obsIdx++)
00969 {
00970
00971 bool doit;
00972 size_t idxInTheFilter=0;
00973
00974 if (data_association.empty())
00975 {
00976 doit = true;
00977 }
00978 else
00979 {
00980 doit = data_association[obsIdx] >= 0;
00981 if (doit)
00982 idxInTheFilter = data_association[obsIdx];
00983 }
00984
00985 if ( doit )
00986 {
00987 m_timLogger.enter("KF:8.update stage:1.ScalarAtOnce.prepare");
00988
00989
00990 const size_t idx_off = VEH_SIZE + idxInTheFilter*FEAT_SIZE;
00991
00992
00993 vector<KFArray_OBS> pred_obs;
00994 OnObservationModel( vector_size_t(1,idxInTheFilter),pred_obs);
00995 ASSERTDEB_(pred_obs.size()==1);
00996
00997
00998 KFArray_OBS ytilde = Z[obsIdx];
00999 OnSubstractObservationVectors(ytilde, pred_obs[0]);
01000
01001
01002
01003
01004
01005 KFMatrix_OxV Hx(UNINITIALIZED_MATRIX);
01006 KFMatrix_OxF Hy(UNINITIALIZED_MATRIX);
01007 const size_t i_idx_in_preds = mrpt::utils::find_in_vector(idxInTheFilter,predictLMidxs);
01008 ASSERTMSG_(i_idx_in_preds!=string::npos, "OnPreComputingPredictions() didn't recommend the prediction of a landmark which has been actually observed!")
01009 dh_dx.extractMatrix(i_idx_in_preds*OBS_SIZE,0, Hx);
01010 dh_dx.extractMatrix(i_idx_in_preds*OBS_SIZE,VEH_SIZE+i_idx_in_preds*OBS_SIZE, Hy);
01011
01012
01013 m_timLogger.leave("KF:8.update stage:1.ScalarAtOnce.prepare");
01014
01015
01016 for (size_t j=0;j<OBS_SIZE;j++)
01017 {
01018 m_timLogger.enter("KF:8.update stage:2.ScalarAtOnce.update");
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032 #if defined(_DEBUG)
01033 {
01034
01035 for (size_t a=0;a<OBS_SIZE;a++)
01036 for (size_t b=0;b<OBS_SIZE;b++)
01037 if ( a!=b )
01038 if (R(a,b)!=0)
01039 THROW_EXCEPTION("This KF algorithm assumes independent noise components in the observation (matrix R). Select another KF algorithm.")
01040 }
01041 #endif
01042
01043 KFTYPE Sij = R.get_unsafe(j,j);
01044
01045
01046 for (size_t k=0;k<VEH_SIZE;k++)
01047 {
01048 KFTYPE accum = 0;
01049 for (size_t q=0;q<VEH_SIZE;q++)
01050 accum += Hx.get_unsafe(j,q) * m_pkk.get_unsafe(q,k);
01051 Sij+= Hx.get_unsafe(j,k) * accum;
01052 }
01053
01054
01055 KFTYPE term2=0;
01056 for (size_t k=0;k<VEH_SIZE;k++)
01057 {
01058 KFTYPE accum = 0;
01059 for (size_t q=0;q<FEAT_SIZE;q++)
01060 accum += Hy.get_unsafe(j,q) * m_pkk.get_unsafe(idx_off+q,k);
01061 term2+= Hx.get_unsafe(j,k) * accum;
01062 }
01063 Sij += 2 * term2;
01064
01065
01066 for (size_t k=0;k<FEAT_SIZE;k++)
01067 {
01068 KFTYPE accum = 0;
01069 for (size_t q=0;q<FEAT_SIZE;q++)
01070 accum += Hy.get_unsafe(j,q) * m_pkk.get_unsafe(idx_off+q,idx_off+k);
01071 Sij+= Hy.get_unsafe(j,k) * accum;
01072 }
01073
01074
01075
01076 size_t N = m_pkk.getColCount();
01077 vector<KFTYPE> Kij( N );
01078
01079 for (size_t k=0;k<N;k++)
01080 {
01081 KFTYPE K_tmp = 0;
01082
01083
01084 size_t q;
01085 for (q=0;q<VEH_SIZE;q++)
01086 K_tmp+= m_pkk.get_unsafe(k,q) * Hx.get_unsafe(j,q);
01087
01088
01089 for (q=0;q<FEAT_SIZE;q++)
01090 K_tmp+= m_pkk.get_unsafe(k,idx_off+q) * Hy.get_unsafe(j,q);
01091
01092 Kij[k] = K_tmp / Sij;
01093 }
01094
01095
01096
01097
01098 for (size_t k=0;k<N;k++)
01099 m_xkk[k] += Kij[k] * ytilde[j];
01100
01101
01102
01103 {
01104 for (size_t k=0;k<N;k++)
01105 {
01106 for (size_t q=k;q<N;q++)
01107 {
01108 m_pkk(k,q) -= Sij * Kij[k] * Kij[q];
01109
01110 m_pkk(q,k) = m_pkk(k,q);
01111 }
01112
01113 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG)
01114 if (m_pkk(k,k)<0)
01115 {
01116 m_pkk.saveToTextFile("Pkk_err.txt");
01117 mrpt::system::vectorToTextFile(Kij,"Kij.txt");
01118 ASSERT_(m_pkk(k,k)>0)
01119 }
01120 #endif
01121 }
01122 }
01123
01124
01125 m_timLogger.leave("KF:8.update stage:2.ScalarAtOnce.update");
01126 }
01127 }
01128 }
01129 }
01130 break;
01131
01132
01133
01134
01135 case kfIKF:
01136 {
01137 THROW_EXCEPTION("IKF scalar by scalar not implemented yet.");
01138 #if 0
01139 KFMatrix h,Hx,Hy;
01140
01141
01142 size_t nKF_iterations = KF_options.IKF_iterations;
01143
01144
01145 KFMatrix *saved_Pkk=NULL;
01146 if (nKF_iterations>1)
01147 {
01148
01149 saved_Pkk = new KFMatrix( m_pkk );
01150 }
01151
01152 KFVector xkk_0 = m_xkk;
01153 KFVector xkk_next_iter = m_xkk;
01154
01155
01156 for (size_t IKF_iteration=0;IKF_iteration<nKF_iterations;IKF_iteration++)
01157 {
01158
01159
01160
01161 if (IKF_iteration>0)
01162 {
01163 m_pkk = *saved_Pkk;
01164 xkk_next_iter = xkk_0;
01165 }
01166
01167
01168 for (size_t obsIdx=0;obsIdx<Z.getRowCount();obsIdx++)
01169 {
01170
01171 bool doit;
01172 size_t idxInTheFilter=0;
01173
01174 if (data_association.empty())
01175 {
01176 doit = true;
01177 }
01178 else
01179 {
01180 doit = data_association[obsIdx] >= 0;
01181 if (doit)
01182 idxInTheFilter = data_association[obsIdx];
01183 }
01184
01185 if ( doit )
01186 {
01187
01188 const size_t idx_off = VEH_SIZE + idxInTheFilter *FEAT_SIZE;
01189 const size_t R_row_offset = obsIdx*OBS_SIZE;
01190
01191
01192 KFVector ytilde;
01193 OnObservationModelAndJacobians(
01194 Z,
01195 data_association,
01196 false,
01197 (int)obsIdx,
01198 ytilde,
01199 Hx,
01200 Hy );
01201
01202 ASSERTDEB_(ytilde.size() == OBS_SIZE )
01203 ASSERTDEB_(Hx.getRowCount() == OBS_SIZE )
01204 ASSERTDEB_(Hx.getColCount() == VEH_SIZE )
01205
01206 if (FEAT_SIZE>0)
01207 {
01208 ASSERTDEB_(Hy.getRowCount() == OBS_SIZE )
01209 ASSERTDEB_(Hy.getColCount() == FEAT_SIZE )
01210 }
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223
01224
01225
01226
01227
01228
01229
01230
01231
01232 KFMatrix Si(OBS_SIZE,OBS_SIZE);
01233 R.extractMatrix(R_row_offset,0, Si);
01234
01235 size_t k;
01236 KFMatrix term(OBS_SIZE,OBS_SIZE);
01237
01238
01239 Hx.multiply_HCHt(
01240 m_pkk,
01241 Si,
01242 true,
01243 0,
01244 true
01245 );
01246
01247
01248
01249 KFMatrix Pyix( FEAT_SIZE, VEH_SIZE );
01250 m_pkk.extractMatrix(idx_off,0, Pyix);
01251
01252 term.multiply_ABCt( Hy, Pyix, Hx );
01253 Si.add_AAt( term );
01254
01255
01256 Hy.multiply_HCHt(
01257 m_pkk,
01258 Si,
01259 true,
01260 idx_off,
01261 true
01262 );
01263
01264
01265 KFMatrix Si_1(OBS_SIZE,OBS_SIZE);
01266
01267
01268
01269 size_t N = m_pkk.getColCount();
01270
01271 KFMatrix Ki( N, OBS_SIZE );
01272
01273 for (k=0;k<N;k++)
01274 {
01275 size_t q;
01276
01277 for (size_t c=0;c<OBS_SIZE;c++)
01278 {
01279 KFTYPE K_tmp = 0;
01280
01281
01282 for (q=0;q<VEH_SIZE;q++)
01283 K_tmp+= m_pkk.get_unsafe(k,q) * Hx.get_unsafe(c,q);
01284
01285
01286 for (q=0;q<FEAT_SIZE;q++)
01287 K_tmp+= m_pkk.get_unsafe(k,idx_off+q) * Hy.get_unsafe(c,q);
01288
01289 Ki.set_unsafe(k,c, K_tmp);
01290 }
01291 }
01292
01293 Ki.multiply(Ki, Si.inv() );
01294
01295
01296
01297 if (nKF_iterations==1)
01298 {
01299
01300
01301 for (k=0;k<N;k++)
01302 for (size_t q=0;q<OBS_SIZE;q++)
01303 m_xkk[k] += Ki.get_unsafe(k,q) * ytilde[q];
01304 }
01305 else
01306 {
01307
01308 std::vector<KFTYPE> HAx(OBS_SIZE);
01309 size_t o,q;
01310
01311 for (o=0;o<OBS_SIZE;o++)
01312 {
01313 KFTYPE tmp = 0;
01314 for (q=0;q<VEH_SIZE;q++)
01315 tmp += Hx.get_unsafe(o,q) * (xkk_0[q] - m_xkk[q]);
01316
01317 for (q=0;q<FEAT_SIZE;q++)
01318 tmp += Hy.get_unsafe(o,q) * (xkk_0[idx_off+q] - m_xkk[idx_off+q]);
01319
01320 HAx[o] = tmp;
01321 }
01322
01323
01324 for (o=0;o<OBS_SIZE;o++)
01325 HAx[o] = ytilde[o] - HAx[o];
01326
01327
01328
01329 for (k=0;k<N;k++)
01330 {
01331 KFTYPE tmp = xkk_next_iter[k];
01332
01333 for (o=0;o<OBS_SIZE;o++)
01334 tmp += Ki.get_unsafe(k,o) * HAx[o];
01335
01336 xkk_next_iter[k] = tmp;
01337 }
01338 }
01339
01340
01341
01342
01343 {
01344
01345 Ki.multiplyByMatrixAndByTransposeNonSymmetric(
01346 Si,
01347 m_pkk,
01348 true,
01349 true);
01350
01351 m_pkk.force_symmetry();
01352
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365
01366
01367
01368
01369
01370
01371
01372 }
01373
01374 }
01375
01376 }
01377
01378
01379 if (nKF_iterations>1)
01380 {
01381 #if 0
01382 cout << "IKF iter: " << IKF_iteration << " -> " << xkk_next_iter << endl;
01383 #endif
01384 m_xkk = xkk_next_iter;
01385 }
01386
01387 }
01388
01389
01390 if (saved_Pkk) delete saved_Pkk;
01391
01392 #endif
01393 }
01394 break;
01395
01396 default:
01397 THROW_EXCEPTION("Invalid value of options.KF_method");
01398 }
01399
01400 }
01401
01402 const double tim_update = m_timLogger.leave("KF:8.update stage");
01403
01404 OnNormalizeStateVector();
01405
01406
01407
01408
01409 if (!data_association.empty())
01410 {
01411 detail::runOneKalmanIteration_addNewLandmarks(*this, Z, data_association,R);
01412 }
01413
01414 if (KF_options.verbose)
01415 {
01416 printf_debug("[KF] %u LMs | Pr: %.2fms | Pr.Obs: %.2fms | Obs.DA: %.2fms | Upd: %.2fms\n",
01417 static_cast<unsigned int>(getNumberOfLandmarksInTheMap()),
01418 1e3*tim_pred,
01419 1e3*tim_pred_obs,
01420 1e3*tim_obs_DA,
01421 1e3*tim_update
01422 );
01423 }
01424
01425
01426 OnPostIteration();
01427
01428 m_timLogger.leave("KF:complete_step");
01429
01430 MRPT_END
01431
01432 }
01433
01434
01435
01436 private:
01437 mutable bool m_user_didnt_implement_jacobian;
01438
01439
01440 static void KF_aux_estimate_trans_jacobian(
01441 const KFArray_VEH &x,
01442 const std::pair<KFCLASS*,KFArray_ACT> &dat,
01443 KFArray_VEH &out_x)
01444 {
01445 bool dumm;
01446 out_x=x;
01447 dat.first->OnTransitionModel(dat.second,out_x, dumm);
01448 }
01449 static void KF_aux_estimate_obs_Hx_jacobian(
01450 const KFArray_VEH &x,
01451 const std::pair<KFCLASS*,size_t> &dat,
01452 KFArray_OBS &out_x)
01453 {
01454 vector_size_t idxs_to_predict(1,dat.second);
01455 std::vector<KFArray_OBS> prediction;
01456
01457 ::memcpy(&dat.first->m_xkk[0],&x[0],sizeof(x[0])*VEH_SIZE);
01458 dat.first->OnObservationModel(idxs_to_predict,prediction);
01459 ASSERTDEB_(prediction.size()==1);
01460 out_x=prediction[0];
01461 }
01462 static void KF_aux_estimate_obs_Hy_jacobian(
01463 const KFArray_FEAT &x,
01464 const std::pair<KFCLASS*,size_t> &dat,
01465 KFArray_OBS &out_x)
01466 {
01467 vector_size_t idxs_to_predict(1,dat.second);
01468 std::vector<KFArray_OBS> prediction;
01469 const size_t lm_idx_in_statevector = VEH_SIZE+FEAT_SIZE*dat.second;
01470
01471 ::memcpy(&dat.first->m_xkk[lm_idx_in_statevector],&x[0],sizeof(x[0])*FEAT_SIZE);
01472 dat.first->OnObservationModel(idxs_to_predict,prediction);
01473 ASSERTDEB_(prediction.size()==1);
01474 out_x=prediction[0];
01475 }
01476
01477
01478
01479 template <size_t _VEH_SIZE, size_t _OBS_SIZE, size_t _FEAT_SIZE, size_t _ACT_SIZE, typename _KFTYPE>
01480 friend void detail::runOneKalmanIteration_addNewLandmarks(
01481 CKalmanFilterCapable<_VEH_SIZE,_OBS_SIZE,_FEAT_SIZE,_ACT_SIZE,_KFTYPE> &obj,
01482 std::vector<typename CKalmanFilterCapable<_VEH_SIZE,_OBS_SIZE,_FEAT_SIZE,_ACT_SIZE,_KFTYPE>::KFArray_OBS> Z,
01483 const vector_int &data_association,
01484 const typename CKalmanFilterCapable<_VEH_SIZE,_OBS_SIZE,_FEAT_SIZE,_ACT_SIZE,_KFTYPE>::KFMatrix_OxO &R
01485 );
01486
01487 };
01488
01489 namespace detail
01490 {
01491
01492 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t FEAT_SIZE, size_t ACT_SIZE, typename KFTYPE>
01493 void runOneKalmanIteration_addNewLandmarks(
01494 CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,FEAT_SIZE,ACT_SIZE,KFTYPE> &obj,
01495 std::vector<typename CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,FEAT_SIZE,ACT_SIZE,KFTYPE>::KFArray_OBS> Z,
01496 const vector_int &data_association,
01497 const typename CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,FEAT_SIZE,ACT_SIZE,KFTYPE>::KFMatrix_OxO &R
01498 )
01499 {
01500 typedef CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,FEAT_SIZE,ACT_SIZE,KFTYPE> KF;
01501
01502 for (size_t idxObs=0;idxObs<Z.size();idxObs++)
01503 {
01504
01505 if ( data_association[idxObs] < 0 )
01506 {
01507 obj.m_timLogger.enter("KF:9.create new LMs");
01508
01509
01510
01511 ASSERTDEB_(FEAT_SIZE>0)
01512 ASSERTDEB_( 0 == ((obj.m_xkk.size() - VEH_SIZE) % FEAT_SIZE) )
01513
01514 const size_t newIndexInMap = (obj.m_xkk.size() - VEH_SIZE) / FEAT_SIZE;
01515
01516
01517 typename KF::KFArray_FEAT yn;
01518 typename KF::KFMatrix_FxV dyn_dxv;
01519 typename KF::KFMatrix_FxO dyn_dhn;
01520 typename KF::KFMatrix_FxF dyn_dhn_R_dyn_dhnT;
01521 bool use_dyn_dhn_jacobian=true;
01522
01523
01524 obj.OnInverseObservationModel(
01525 Z[idxObs],
01526 yn,
01527 dyn_dxv,
01528 dyn_dhn,
01529 dyn_dhn_R_dyn_dhnT,
01530 use_dyn_dhn_jacobian );
01531
01532
01533 obj.OnNewLandmarkAddedToMap(
01534 idxObs,
01535 newIndexInMap
01536 );
01537
01538 ASSERTDEB_( yn.size() == FEAT_SIZE )
01539
01540
01541 size_t q;
01542 size_t idx = obj.m_xkk.size();
01543 obj.m_xkk.resize( obj.m_xkk.size() + FEAT_SIZE );
01544
01545 for (q=0;q<FEAT_SIZE;q++)
01546 obj.m_xkk[idx+q] = yn[q];
01547
01548
01549
01550
01551 ASSERTDEB_( obj.m_pkk.getColCount()==idx && obj.m_pkk.getRowCount()==idx );
01552
01553 obj.m_pkk.setSize( idx+FEAT_SIZE,idx+FEAT_SIZE );
01554
01555
01556
01557 typename KF::KFMatrix_VxV Pxx;
01558 obj.m_pkk.extractMatrix(0,0,Pxx);
01559 typename KF::KFMatrix_FxV Pxyn;
01560 Pxyn.multiply( dyn_dxv, Pxx );
01561
01562 obj.m_pkk.insertMatrix( idx,0, Pxyn );
01563 obj.m_pkk.insertMatrixTranspose( 0,idx, Pxyn );
01564
01565
01566
01567 const size_t nLMs = (idx-VEH_SIZE)/FEAT_SIZE;
01568 for (q=0;q<nLMs;q++)
01569 {
01570 typename KF::KFMatrix_VxF P_x_yq(UNINITIALIZED_MATRIX);
01571 obj.m_pkk.extractMatrix(0,VEH_SIZE+q*FEAT_SIZE,P_x_yq) ;
01572
01573 typename KF::KFMatrix_FxF P_cross(UNINITIALIZED_MATRIX);
01574 P_cross.multiply(dyn_dxv, P_x_yq );
01575
01576 obj.m_pkk.insertMatrix(idx,VEH_SIZE+q*FEAT_SIZE, P_cross );
01577 obj.m_pkk.insertMatrixTranspose(VEH_SIZE+q*FEAT_SIZE,idx, P_cross );
01578 }
01579
01580
01581
01582
01583 typename KF::KFMatrix_FxF P_yn_yn(UNINITIALIZED_MATRIX);
01584 dyn_dxv.multiply_HCHt(Pxx, P_yn_yn);
01585 if (use_dyn_dhn_jacobian)
01586 dyn_dhn.multiply_HCHt(R, P_yn_yn, true);
01587 else P_yn_yn+=dyn_dhn_R_dyn_dhnT;
01588
01589 obj.m_pkk.insertMatrix(idx,idx, P_yn_yn );
01590
01591 obj.m_timLogger.leave("KF:9.create new LMs");
01592 }
01593 }
01594 }
01595
01596 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t ACT_SIZE, typename KFTYPE>
01597 void runOneKalmanIteration_addNewLandmarks(
01598 CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,0 /* FEAT_SIZE=0 */,ACT_SIZE,KFTYPE> &obj,
01599 std::vector<typename CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,0 /* FEAT_SIZE=0 */,ACT_SIZE,KFTYPE>::KFArray_OBS> Z,
01600 const vector_int &data_association,
01601 const typename CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,0 /* FEAT_SIZE=0 */,ACT_SIZE,KFTYPE>::KFMatrix_OxO &R
01602 )
01603 {
01604
01605 }
01606
01607 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t FEAT_SIZE, size_t ACT_SIZE, typename KFTYPE>
01608 inline size_t getNumberOfLandmarksInMap(const CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,FEAT_SIZE,ACT_SIZE,KFTYPE> &obj)
01609 {
01610 return (obj.getStateVectorLength()-VEH_SIZE)/FEAT_SIZE;
01611 }
01612
01613 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t ACT_SIZE, typename KFTYPE>
01614 inline size_t getNumberOfLandmarksInMap(const CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,0 /*FEAT_SIZE*/,ACT_SIZE,KFTYPE> &obj)
01615 {
01616 return 0;
01617 }
01618
01619 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t FEAT_SIZE, size_t ACT_SIZE, typename KFTYPE>
01620 inline bool isMapEmpty(const CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,FEAT_SIZE,ACT_SIZE,KFTYPE> &obj)
01621 {
01622 return (obj.getStateVectorLength()==VEH_SIZE);
01623 }
01624
01625 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t ACT_SIZE, typename KFTYPE>
01626 inline bool isMapEmpty (const CKalmanFilterCapable<VEH_SIZE,OBS_SIZE,0 /*FEAT_SIZE*/,ACT_SIZE,KFTYPE> &obj)
01627 {
01628 return true;
01629 }
01630
01631 }
01632
01633 }
01634 }
01635
01636 #endif