00001
00002 #ifndef DDF_TEST_DELAYED_HPP
00003 #define DDF_TEST_DELAYED_HPP
00004
00005 #include <iostream>
00006 #include <fstream>
00007 #include <BayesFilter/bayesFlt.hpp>
00008 #include <BayesFilter/infFlt.hpp>
00009
00010 #include "jmath/ublasExtra.hpp"
00011 #include "sensor.hpp"
00012 #include "velocitysimu.hpp"
00013 #include "gpssimu.hpp"
00014 #include "robotsimu.hpp"
00015 #include "upd_const_speed.hpp"
00016 #include "ddf/tools.hpp"
00017
00018 namespace jafar
00019 {
00020 namespace ddfsimu
00021 {
00022
00023 namespace demo
00024 {
00025
00026 using namespace Bayesian_filter;
00027 using namespace Bayesian_filter_matrix;
00028
00029
00030
00031
00032 class Const_speed_predict_model : public Linear_invertable_predict_model
00033 {
00034 public:
00035 static const double DELTA_T;
00036 static const unsigned int SV_SIZE;
00037
00038 public:
00039 Const_speed_predict_model() : Linear_invertable_predict_model(Const_speed_predict_model::SV_SIZE,3)
00040 {
00041 Fx.clear();
00042 Fx(0,0) = 1.; Fx(1,1) = 1.;
00043 Fx(2,2) = 1.; Fx(3,3) = 1.;
00044 Fx(4,4) = 1.; Fx(5,5) = 1.;
00045
00046 Fx(0,1) = DELTA_T; Fx(2,3) = DELTA_T; Fx(4,5) = DELTA_T;
00047
00048 jafar::jmath::ublasExtra::lu_inv(Fx, static_cast<detail::BaseDenseColMatrix &> (inv.Fx));
00049
00050 G.clear();
00051 double tmp = DELTA_T * DELTA_T / 2.0;
00052
00053 G(0,0) = tmp; G(1,0) = DELTA_T;
00054 G(2,1) = tmp; G(3,1) = DELTA_T;
00055 G(4,2) = tmp; G(5,2) = DELTA_T;
00056
00057 q[0] = 0.5; q[1] = 0.5; q[2] = 0.5;
00058 }
00059
00060 void Print_Prediction_Model()
00061 {
00062 PrettyPrintMatrix(Fx,"Fx");
00063 PrettyPrintMatrix(G,"G");
00064 PrettyPrintVector(q,"q");
00065 }
00066 };
00067
00068 class FSStorage : public FSStorageBase<int>
00069 {
00070 public:
00071 InfoContainer m_StateCntr;
00072
00073 public:
00074 FSStorage(unsigned short sv_size, FS_STORE_TYPE fs_type): FSStorageBase<int>(sv_size, fs_type,0), m_StateCntr(sv_size){}
00075 FSStorage(FSStorage const& other): FSStorageBase<int>(other), m_StateCntr(other.GetSize()) {
00076 if (this == &other) return;
00077 this->m_StateCntr = other.m_StateCntr;
00078 }
00079 };
00080
00081 void FillStorage(Upd_const_speed & mPred, FS_STORE_TYPE fs_type, std::vector<FSStorage> &m_storage)
00082 {
00083 FSStorage mStore(mPred.GetSvSize(), fs_type);
00084
00085 mStore.m_MkIik = mPred.GetPrediction();
00086 mStore.m_StateCntr = mPred.GetInversePrediction();
00087 m_storage.push_back(mStore);
00088 }
00089
00090
00091 void DumpStore(std::string const& fname, std::vector<FSStorage> &m_storage)
00092 {
00093 std::vector<FSStorage>::iterator it;
00094 int nb = 0;
00095
00096 std::ofstream dump_file(fname.c_str(), std::ios::out|std::ios::trunc);
00097 dump_file << std::scientific;
00098
00099 dump_file << "# nb | k | Y(k) | y(k) | P(k) | x(k) | v(k)" << std::endl;
00100
00101 for(it = m_storage.begin(); it != m_storage.end(); it++, nb++)
00102 {
00103 dump_file << nb << LOGSEP << ((*it).GetTime()).to_double() << LOGSEP << (*it).m_MkIik.GetInfoMat()(0,0) << LOGSEP\
00104 << (*it).m_MkIik.Getinfo()(0) << LOGSEP << (*it).m_StateCntr.GetInfoMat()(0,0) << LOGSEP\
00105 << (*it).m_StateCntr.Getinfo()(0) << LOGSEP << (*it).m_StateCntr.Getinfo()(1) << LOGSEP;
00106 dump_file << std::endl;
00107 }
00108 dump_file.close();
00109 m_storage.clear();
00110 }
00111
00112 void DumpHistory(std::string const& fname, Upd_const_speed & predMod, std::vector<FSStorage> &m_storage)
00113 {
00114 unsigned short buf_size = predMod.GetHistorySize();
00115 unsigned short nb = 0;
00116 ParametersGeneric tmp;
00117 FSStorageBase<ParametersGeneric> store(predMod.GetSvSize(), FS_UPDATE,tmp);
00118
00119 std::ofstream dump_file(fname.c_str(), std::ios::out|std::ios::trunc);
00120 dump_file << std::scientific;
00121
00122 dump_file << "# nb | time | Yx | yx | Yv | yv | type" << std::endl;
00123
00124 for(int i = 0; i < buf_size; i++, nb++)
00125 {
00126 store = predMod.RecallStoredState(i);
00127 dump_file << nb << LOGSEP << store.GetTime().to_double() << LOGSEP << store.m_MkIik.GetInfoMat()(0,0) << LOGSEP\
00128 << store.m_MkIik.Getinfo()(0) << LOGSEP << store.m_MkIik.GetInfoMat()(1,1) << LOGSEP << store.m_MkIik.Getinfo()(1) << LOGSEP\
00129 << store.GetType();
00130 dump_file << std::endl;
00131 }
00132 predMod.ClearHistory();
00133 dump_file.close();
00134 }
00135
00136
00137
00138
00139 void test_out_of_sequence()
00140 {
00141 const unsigned short cx = 2;
00142 const unsigned short dim = cx/2;
00143
00144 const unsigned short PROF_SIZE_R1 = 2;
00145 double accProfR1[PROF_SIZE_R1][4] = {{0.0,0.,0.,0.},
00146 {5.0,0.,0.,0.}};
00147
00148 std::vector<FSStorage> m_storage;
00149
00150 VEC v1(1);
00151 VEC v2(2);
00152 VEC v3_1(3), v3_2(3);
00153 MSYM Y0(cx, cx);
00154 time now = time::null(), horizon = time::null();
00155 InfoContainer iCont(cx);
00156
00157
00158 VelocitySimuUncorr *vel_r1 = new VelocitySimuUncorr(cx, dim);
00159 v1[0] = TWOSIGMA_TO_VAR(0.03); vel_r1->SetSensorSimVariance(v1);
00160 v1[0] = TWOSIGMA_TO_VAR(0.031); vel_r1->SetSensorModelVar(v1);
00161 vel_r1->SetPeriod(0.1);
00162 vel_r1->PrintObservationModel();
00163
00164 GPSSimuUncorr *gps_r1 = new GPSSimuUncorr(cx, dim);
00165 v1[0] = TWOSIGMA_TO_VAR(0.15); gps_r1->SetSensorSimVariance(v1);
00166 v1[0] = TWOSIGMA_TO_VAR(0.17); gps_r1->SetSensorModelVar(v1);
00167 gps_r1->SetPeriod(0.5);
00168 gps_r1->SetProcTime(time::from_ms(300));
00169 gps_r1->SetTimeOffset(time::from_ms(5));
00170 gps_r1->PrintObservationModel();
00171
00172
00173 RobotSimu robsim1(cx, "R1D1", true);
00174 robsim1.FillProfile(PROF_SIZE_R1, accProfR1);
00175
00176 v3_1.clear(); v3_2.clear();
00177 v3_1[0] = 0.4;
00178 robsim1.SetInitialConditions(v3_1, v3_2);
00179 robsim1.AddSensor(vel_r1);
00180 robsim1.AddSensor(gps_r1);
00181
00182 robsim1.SimulatePropPerception();
00183 robsim1.FillUpMeasurementSet(RobotSimu::SORT_STAMP_SENSED);
00184 robsim1.PrintRobot();
00185 robsim1.DumpAllMeasures("delay_meas_all_sensed.log");
00186 robsim1.DumpTruth("delay_truth.log");
00187 vel_r1->DumpMeasurements("delay_meas_vel.log");
00188 gps_r1->DumpMeasurements("delay_meas_pos.log");
00189 robsim1.ResetCounter();
00190
00191
00192 Upd_const_speed myPredict(dim,2*dim,dim);
00193 FSStorageBase<ParametersGeneric> store(Const_speed_predict_model::SV_SIZE, FS_UPDATE);
00194
00195 myPredict.StoreStates(true);
00196 v1[0] = 0.002;
00197 myPredict.Updateq(v1);
00198 v2[0] = 0.0; v2[1] = v3_1[0]+0.2;
00199 Y0.clear(); Y0(0,0) = 0.4; Y0(1,1) = 0.3333;
00200 myPredict.InitInformation(v2, Y0);
00201 myPredict.SetNeedUpdate(NEED_UPD_Q, false);
00202
00203
00204 now = robsim1.GetMinTime();
00205
00206 while(!robsim1.EndBuf())
00207 {
00208 MeasureItem const& mItem = robsim1.GetCurrentMeasure();
00209
00210
00211 horizon = mItem.GetSensedTime();
00212 myPredict.SetModelParams(now, horizon);
00213 myPredict.Predict(horizon);
00214 FillStorage(myPredict, FS_PREDICT, m_storage);
00215
00216 now = horizon;
00217
00218
00219 do
00220 {
00221 robsim1.GetCurrentInfo(iCont);
00222 myPredict.UpdatePredictionWith(iCont);
00223 FillStorage(myPredict, FS_UPDATE, m_storage);
00224 robsim1.IncCounter();
00225 }
00226 while(!robsim1.EndBuf() && robsim1.GetCurrentRelTime() == now);
00227 }
00228
00229
00230 DumpStore(std::string("delay_histo_sensed.log"), m_storage);
00231 myPredict.PrintHistory();
00232 DumpHistory(std::string("delay_histo_filter_sens.log"), myPredict, m_storage);
00233
00234
00235
00236 myPredict.InitInformation(v2, Y0);
00237
00238 robsim1.ClearUpMeasurementSet();
00239 robsim1.FillUpMeasurementSet(RobotSimu::SORT_STAMP_AVAILABLE);
00240 robsim1.DumpAllMeasures("delay_meas_all_available.log");
00241
00242 now = robsim1.GetMinTime();
00243 robsim1.ResetCounter();
00244
00245 while(!robsim1.EndBuf())
00246 {
00247 MeasureItem const& mItem = robsim1.GetCurrentMeasure();
00248
00249 if(mItem.GetSensedTime() < mItem.GetAvailableTime()) {
00250 robsim1.GetCurrentInfo(iCont);
00251 JFR_VDEBUG(iCont << "-- InsertDelayedMeasure\n");
00252 myPredict.InsertDelayedMeasure(iCont, mItem.GetAvailableTime());
00253 FillStorage(myPredict, FS_UPDATE, m_storage);
00254 robsim1.IncCounter();
00255 }
00256 else {
00257
00258
00259 horizon = mItem.GetSensedTime();
00260 myPredict.SetModelParams(now, horizon);
00261 myPredict.Predict(horizon);
00262 FillStorage(myPredict, FS_PREDICT, m_storage);
00263
00264 now = horizon;
00265
00266
00267 do
00268 {
00269 robsim1.GetCurrentInfo(iCont);
00270 myPredict.UpdatePredictionWith(iCont);
00271 FillStorage(myPredict, FS_UPDATE, m_storage);
00272 robsim1.IncCounter();
00273 }
00274 while(!robsim1.EndBuf() && robsim1.GetCurrentRelTime() == now);
00275
00276 }
00277 }
00278
00279 DumpStore(std::string("delay_histo_available.log"),m_storage);
00280 myPredict.PrintHistory();
00281 DumpHistory(std::string("delay_histo_filter_avail.log"), myPredict, m_storage);
00282 }
00283 }
00284 }
00285 }
00286 #endif