Jafar
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
test_slam_multi.hpp
00001 
00002 #ifndef DDF_TEST_SLAM_MULTI_HPP
00003 #define DDF_TEST_SLAM_MULTI_HPP
00004 
00005 #include <iostream>
00006 #include <fstream>
00007 #include <BayesFilter/bayesFlt.hpp>
00008 #include <BayesFilter/matSup.hpp>
00009 
00010 #include "CANode.hpp"
00011 #include "CSNode.hpp"
00012 #include "upd_const_pose.hpp"
00013 #include "upd_targets.hpp"
00014 #include "robotsimu.hpp"
00015 #include "eventsimu.hpp"
00016 #include "velocitysimu.hpp"
00017 #include "trackersimu.hpp"
00018 #include "gdheclient.hpp"
00019 #include "multirobsimu.hpp"
00020 #include "trackrobot.hpp"
00021 #include "ddf/debugging.hpp"
00022 
00023 namespace jafar
00024 {
00025   namespace ddfsimu
00026   {
00027     namespace demo
00028     {
00029 
00030       using namespace Bayesian_filter_matrix;
00031       using namespace Bayesian_filter;
00032       using namespace jafar::ddf;
00033 
00034       typedef SensorNodeIPC<ParametersGeneric>        CLNodeIPC;
00035       typedef SensorNodePeriodic<ParametersGeneric>   CLNode;
00036 
00037       const double h = 1.0;         // integration step for converting into var vel and var acc
00038       const double ts_pose = 0.05;  // initial uncertainty about the position +/- ts_pose (two sigma)
00039       const double ts_pose_targets = 0.15;  // 0.2
00040       const unsigned short CST_ACC_MODEL_DIM = 3;
00041       const unsigned short CST_SPD_MODEL_DIM = 3;
00042       const unsigned short BIASES_SIZE=3;
00043 
00044       const unsigned short PROF_SIZE_R1 = 9;
00045       double accProfR1[PROF_SIZE_R1][4] = {
00046         {0.0   ,0.   ,0.,-0.02},
00047         {10.0  ,0.   ,0.1,-0.02},
00048         {12.50,0.12  ,0.1,-0.02},
00049         {13.50,0.17  ,-0.1,0.02},
00050         {18.00,-0.12,-0.1,0.02},
00051         {18.50,-0.17,-0.15,0.0},
00052         {20.   ,-0.07,-0.15,-0.02},
00053         {25.0  ,-0.01,0.,0.0},
00054         {35.0  ,0.   ,0.,0.0}};
00055 
00056       const unsigned short PROF_SIZE_R2 = 6;
00057       double accProfR2[PROF_SIZE_R1][4] = {
00058         {0.0 ,0.   ,0.   ,0.0},
00059         {5.0 ,0.06 ,0.09 ,0.0},
00060         {12.0   ,-0.05,-0.03,0.0},
00061         {20. ,-0.04,-0.05 ,0.0},
00062         {25.0   ,0.0  ,0.00 ,0.0},
00063         {35.0   ,0.   ,0.   ,0.0}};
00064 
00065       const unsigned short NUM_TARGETS = 7;
00066       double targetPose[NUM_TARGETS][3] = {
00067         {-0.5,0.5,0.25},
00068         {5,4.5,1.0},
00069         {-1,3,0},
00070         {2,-2,0.5},
00071         {5.5,-0.5,0},
00072         {1,7,1.5},
00073         {1.5,0.75,0}
00074       };
00075 
00076    
00077       // Prediction model for the tracker nodes
00078       class CstPosePredictModelFactory : public PredictModelFactoryBase
00079       {
00080         unsigned short m_sv_size;
00081         VEC            m_q;
00082    
00083       public:
00084         CstPosePredictModelFactory(unsigned short sv_size, VEC const& var):
00085           m_sv_size(sv_size), m_q(sv_size)
00086         {   JFR_PRECOND(var.size() == sv_size, "bad sizes");   for(int i = 0; i < sv_size; i++) m_q[i] = var[i]; }
00087          
00088         virtual ~CstPosePredictModelFactory() {}
00089          
00090         PredictionEngineBase* CreatePredictionModel() { return new Upd_const_pose(m_sv_size,m_q); }
00091       };
00092 
00093       class TargetsPredictModelFactory : public PredictModelFactoryBase
00094       {
00095         unsigned short m_sv_size;
00096         unsigned short m_num_robots;
00097         unsigned short m_rob_id;
00098         VEC            m_q;
00099    
00100       public:
00101         TargetsPredictModelFactory(unsigned short sv_size, unsigned short num_robots, unsigned short rob_id, VEC const& var):
00102           m_sv_size(sv_size), m_num_robots(num_robots), m_rob_id(rob_id), m_q(var)
00103         { }
00104          
00105         virtual ~TargetsPredictModelFactory() {}
00106          
00107         PredictionEngineBase* CreatePredictionModel() { return new Upd_targets(m_sv_size,m_num_robots,m_rob_id,m_q.size(),m_q); }
00108       };
00109 
00110       //typedef CstAccPredictModelFactory       CANode_PredModFactory;
00111    
00112       // Associate a sensor node and a sensor
00113       template<typename SNODE_CLASS>
00114       class  SensorNodeCompoundGen
00115       {
00116         typedef std::vector<SensorBase* >   SensorArray;
00117             
00118       public:
00119          
00120         SNODE_CLASS  *snode;     // Pointer on the sensor node associated to sensor
00121         SensorArray  sensor;     // Pointer on the sensors
00122         EventSimu    *trigger;   // Pointer on the sensor that triggers the sensor node
00123         time         T_sync;
00124         StopWatch    m_sw_check; // Period checker in case of real_time execution
00125         bool         m_is_tracker;
00126 
00127       
00128         SensorNodeCompoundGen(): snode(NULL), trigger(NULL), T_sync(time::null()),m_is_tracker(false) {}
00129         SensorNodeCompoundGen(SensorNodeCompoundGen const& other) {
00130           snode   = other.snode;    sensor = other.sensor;
00131           trigger = other.trigger;  T_sync = other.T_sync;
00132           m_is_tracker = other.m_is_tracker;
00133         }
00134         void AddSensor(SensorBase * new_sens) { sensor.push_back(new_sens); }
00135         bool IsTrackerNode() const { return m_is_tracker; }
00136         void SetIsTrackerNode() { m_is_tracker = true; }
00137         unsigned short GetSensorNr() const { return sensor.size(); }
00138         bool SensorBelongsToNodebyID(unsigned short id) {
00139           typename SensorArray::iterator it;
00140   
00141           for(it = sensor.begin(); it != sensor.end(); it++) {
00142             if((*it)->GetID() == id) {
00143               return true;
00144             }
00145           }
00146           if(trigger->GetID() == id) {
00147             return true;
00148           }
00149           return false;
00150         }
00151          
00152         ~SensorNodeCompoundGen() {}
00153       };
00154 
00155       template<typename SNODE_CLASS>
00156       class Test_Dump
00157       {
00158         typedef std::vector<SensorNodeCompoundGen<SNODE_CLASS> > SNodesArray;
00159       
00160         SNodesArray    *m_psens_nodes;
00161         std::ofstream  m_dump_file;
00162         unsigned short m_nodes_nr;
00163         double         *m_snerror;
00164         unsigned int   m_row;
00165       
00166       public:
00167         Test_Dump(unsigned short nodes_nr, SNodesArray   *nodes_array, std::string fname)
00168           : m_psens_nodes(nodes_array), m_dump_file(fname.c_str(), std::ios::out|std::ios::trunc), m_nodes_nr(nodes_nr) {
00169           m_dump_file << std::scientific;
00170           DumpHeader();
00171           m_snerror = new double[m_nodes_nr];
00172           m_row = 0;
00173           for(int i = 0; i < m_nodes_nr; i++) { m_snerror[i] = 0.0; }
00174         }
00175          
00176         ~Test_Dump() {
00177           for(unsigned int j = 0; j < m_psens_nodes->size(); j++) JFR_DEBUG( "Rob: " << m_snerror[j]);
00178           m_dump_file << "\n"; Flush(); m_dump_file.close(); delete [] m_snerror;
00179         }
00180 
00181         void DumpStateDiag(InfoContainer const& info)
00182         {
00183           m_dump_file << info.Getinfo()[AccelMotion::SV_X] << LOGSEP << info.GetInfoMat()(AccelMotion::SV_X,AccelMotion::SV_X) << LOGSEP;
00184         }
00185 
00186         void DumpStateDiag(InfoContainer const& info, unsigned short robid)
00187         {
00188           m_dump_file << info.Getinfo()[AccelMotion::SV_X+6*robid] << LOGSEP << info.GetInfoMat()(AccelMotion::SV_X+6*robid,AccelMotion::SV_X+6*robid) << LOGSEP;
00189         }
00190 
00191         VEC DumpTrueState(double const& t, RobotSimu const& rob)
00192         {
00193           VEC  acc(3), vel(3), pos(3);
00194             
00195           rob.ComputeVelocityAndPos(t, vel, pos);
00196           acc = rob.GetAccelAtTimeT(t);
00197               
00198           m_dump_file << t << LOGSEP << pos[0] << LOGSEP << vel[0] << LOGSEP <<  acc[0] << LOGSEP;
00199 
00200           return pos;
00201         }
00202   
00203         void DumpCurrentState(time const& origin, time const& stamp, RobotSimu const& rob, unsigned int POM_node_id)
00204         {
00205           int  j = 0;
00206           VEC  tpose(3);
00207   
00208               
00209           typename SNodesArray::iterator it, it_pom;
00210           double dtmp;
00211     
00212           m_dump_file << std::endl;
00213           m_dump_file << m_row++ << LOGSEP << rob.GetCurrentSensorID() << LOGSEP << stamp.to_double()  << LOGSEP;
00214             
00215           for(it = m_psens_nodes->begin(); it != m_psens_nodes->end(); it++, j++)
00216             {
00217               dtmp = ((*it).snode->GetLocalInfoTime() - origin).to_double();
00218               if(dtmp > rob.GetMaxTime()) break;
00219               tpose = DumpTrueState(dtmp,rob);
00220               if(!(*it).IsTrackerNode()){
00221                 DumpStateDiag((*it).snode->GetLocalInfo());
00222                 DumpStateDiag((*it).snode->GetLocalState());
00223                 dtmp = tpose[0] - (*it).snode->GetLocalState().Getinfo()[0];
00224               }
00225               else {
00226                 DumpStateDiag((*it).snode->GetLocalInfo(), rob.GetRobID());
00227                 DumpStateDiag((*it).snode->GetLocalState(), rob.GetRobID());
00228                 dtmp = tpose[0] - (*it).snode->GetLocalState().Getinfo()[6*rob.GetRobID()];
00229               }
00230                   
00231               m_snerror[j] += (dtmp * dtmp);
00232               m_dump_file << m_snerror[j] << LOGSEP;
00233     
00234               if((*it).snode->GetID() == POM_node_id) it_pom = it;
00235             }
00236     
00237           DumpAllSv((*it_pom).snode->GetLocalState());
00238         }
00239     
00240         void DumpAllSv(InfoContainer const& info)
00241         {
00242           m_dump_file << info.GetSize() << LOGSEP;
00243           for(unsigned int i = 0; i < info.GetSize(); i++) m_dump_file << info.Getinfo()[i] << LOGSEP;
00244         }
00245     
00246         void DumpCustom(VEC const& data) {
00247           for(unsigned int i = 0; i < data.size(); i++) m_dump_file << data[i] << LOGSEP;
00248         }
00249     
00250         void Flush() { m_dump_file << std::flush; }
00251             
00252       private:
00253         void DumpHeader()
00254         {
00255           m_dump_file << "# log file for " << m_nodes_nr << " nodes" << std::endl;
00256           m_dump_file << "# 1=row number | 2=sensor id | 3=time stamp(available). Then a blocks for each sensor node\n";
00257           m_dump_file << "# o0=local filter time o1,2,3=true state at time o0 (px,vx,ax) o4,o5=info vec px, info px o6,o7=state px,var px o8=sum of squared error | then the size of the state vector of POM and its content | finally custom user data\n";
00258         }
00259       };
00260     
00261         // Associate a robot with an array of sensor nodes/sensors
00262       template<typename SNODE_CLASS>
00263       class RobotSimuNodesGen : public RobotSimu
00264       {
00265         typedef std::vector<SensorNodeCompoundGen<SNODE_CLASS> > SNodesArray;
00266         typedef std::vector<VEC>                                 TrajRob;
00267 
00268       public:
00269         SNodesArray           m_nodes;
00270         ParametersGeneric     m_params;
00271         SNODE_CLASS           *m_POM_node;         // the prefered node to get state from
00272         SNODE_CLASS           *m_Tracker_node;
00273         TrajRob               m_traj;
00274         unsigned short        m_traj_id;           // for GDHE
00275         unsigned short        m_true_traj_id;
00276         std::vector<unsigned char>     m_color;    // color of traj
00277         Test_Dump<SNODE_CLASS>   *m_pDump;
00278         InfoContainer         m_CurInfo;           // temp variables
00279         bool                  m_just_perceive;     // for gdhe
00280         bool                  m_first;
00281         TrackerSimuUncorr     *m_tracker_sensor;
00282         unsigned short        m_gdhe_persist;
00283             
00284       public:
00285         RobotSimuNodesGen(unsigned short sv_size, std::string name, unsigned short rob_id, bool use_only_vel, bool use_biases = false)
00286           : RobotSimu(sv_size, name, use_only_vel, use_biases),
00287             m_POM_node(NULL),
00288             m_Tracker_node(NULL),
00289             m_traj_id(0),
00290             m_true_traj_id(0),
00291             m_color(3),
00292             m_pDump(NULL),
00293             m_CurInfo(sv_size),
00294             m_just_perceive(false),
00295             m_first(true),
00296             m_tracker_sensor(NULL),
00297             m_gdhe_persist(10) {m_rob_id = rob_id;}
00298     
00299         ~RobotSimuNodesGen() {
00300           typename SNodesArray::iterator  it;
00301           for(it = m_nodes.begin(); it != m_nodes.end(); it++)
00302             if ((*it).snode != NULL) delete (*it).snode;
00303           if (m_pDump != NULL) delete m_pDump;
00304         }
00305     
00306         int GetTrackerID() { return static_cast<SensorBase*>(m_tracker_sensor)->GetID(); }
00307         void SetTrackerPtr(TrackerSimuUncorr *tracker_sensor) {
00308           m_tracker_sensor = tracker_sensor;
00309         }
00310     
00311         void AddTrajPoint(VEC const& point) {
00312           if(m_traj.empty() || !vec_compare(point,m_traj.back())) m_traj.push_back(point);
00313         }
00314     
00315         void CreateDumpFile(std::string const& fname)
00316         {
00317           JFR_PRECOND (m_pDump == NULL, "not NULL");
00318           m_pDump = new Test_Dump<SNODE_CLASS>(m_nodes.size(), &m_nodes, fname);
00319         }
00320     
00321         void DumpCurrentState(time const& rel_time)
00322         {
00323           m_pDump->DumpCurrentState(this->GetMinTime(), rel_time, *this, this->m_POM_node->GetID());
00324         }
00325     
00326         void DumpCustom(VEC const& data) {
00327           m_pDump->DumpCustom(data);
00328         }
00329     
00330         void ClearTraj()
00331         {
00332           m_traj.clear();
00333           m_traj_id = 0;
00334           m_true_traj_id = 0;
00335         }
00336     
00337         void AddTrajPoint(double x, double y, double z) {
00338           VEC point(3);
00339           point[0] = x; point[1] = y; point[2] = z;
00340           AddTrajPoint(point);
00341         }
00342     
00343         void SetColor(unsigned char r, unsigned char g, unsigned char b) {
00344           m_color[0] = r;  m_color[1] = g;   m_color[2] = b;
00345         }
00346       };
00347     
00348       class Target
00349       {
00350         VEC            m_position;    // True position of the target
00351         unsigned short m_gdhe_id;     // id of the ellipsoid
00352         std::vector<unsigned short>   m_gdhe_seg_ids;
00353         unsigned short m_num_robots;
00354           
00355       public:
00356         Target(VEC const& pose, unsigned short num_rob): m_position(pose), m_gdhe_id(0)/*,m_gdhe_id_segment(0)*/
00357         { for(int i = 0; i < num_rob; i++)m_gdhe_seg_ids.push_back(0);  }
00358         void SetGDHEID(unsigned short id) {m_gdhe_id = id; }
00359         void SetGDHEIDSegment(unsigned short id, unsigned short rob_num) {m_gdhe_seg_ids[rob_num] = id; }
00360         unsigned short GetGDHEID() const { return m_gdhe_id; }
00361         unsigned short GetGDHEIDSegment(unsigned short rob_num) {return m_gdhe_seg_ids[rob_num]; }
00362         VEC const& GetPose() const { return m_position; }
00363       };
00364     
00365       template<typename SNODE_CLASS>
00366       class test_SimuSensorNode
00367         {
00368         typedef std::vector<RobotSimuNodesGen<SNODE_CLASS> * >   RobotsArray;
00369         typedef std::vector<Target * >                           TargetsArray;     // useless make array of x,y,z and m_gdhe_id
00370         typedef SensorNodeCompoundGen<SNODE_CLASS>               SensorNodeCompound;
00371         typedef std::vector<SensorNodeCompoundGen<SNODE_CLASS> > SNodesArray;
00372         typedef std::vector<unsigned short>                      SVsizeArray;
00373           
00374       private:
00375         unsigned short m_rob_num;
00376         unsigned short m_num_targets;
00377         unsigned short m_targets_sv_size;
00378         unsigned short m_targets_q_size;
00379         unsigned short m_obj_to_track;
00380         bool           m_real_time;
00381         bool           m_use_biases;
00382         bool           m_track_other_robot;
00383         RobotsArray    m_rob_array;
00384         TargetsArray   m_targets;
00385         GdheClient     m_gdhecl;
00386         MultiRobSimu   m_multi_rob;
00387   
00388       public:
00389           
00390         test_SimuSensorNode(unsigned short num_robots, bool real_time, bool track_other_robot)
00391           :   m_rob_num(num_robots),
00392               m_num_targets(NUM_TARGETS),
00393               m_targets_sv_size((m_rob_num*2+m_num_targets)*3),
00394               m_targets_q_size((m_rob_num+m_num_targets)*3),
00395               m_real_time(real_time),
00396               m_use_biases(true),
00397               m_track_other_robot(track_other_robot),
00398               m_gdhecl((char *)"localhost")
00399         {
00400           JFR_PRECOND(num_robots <= 2, "more robot not supported yet");
00401           m_obj_to_track = m_num_targets + (m_track_other_robot ? (1):(0));
00402           CreateGDHEWorld();
00403           InitTargets(targetPose, m_num_targets);
00404           CreateGDHETargets();
00405         }
00406           
00407         virtual ~test_SimuSensorNode() {
00408           typename RobotsArray::iterator  it;
00409           typename TargetsArray::iterator itt;
00410           
00411           for(it = m_rob_array.begin(); it != m_rob_array.end(); it++)
00412             delete *it;
00413         
00414           for(itt = m_targets.begin(); itt != m_targets.end(); itt++)
00415             delete *itt;
00416           
00417           ClearGDHEWorld();
00418         }
00419   
00420         void test_Simu()
00421         {
00422           if(m_rob_num == 1)
00423           {
00424             run_Simu(false);
00425             run_Simu(true);
00426           }
00427           else
00428           {
00429             if(!m_real_time)
00430             {      
00431               run_Simu_Multi(true);
00432             }
00433             else
00434               JFR_DEBUG("Not yet implemented");
00435           }
00436         }
00437 
00438         // *** Print functions ***
00439         void PrintNodes(RobotSimuNodesGen<SNODE_CLASS> & rob)
00440         {
00441           std::ostringstream stream;
00442 
00443           typename SNodesArray::iterator it;
00444           for(it = rob.m_nodes.begin(); it != rob.m_nodes.end(); it++){
00445             stream << "\n** Node id: " << (*it).snode->GetID() << "\n";
00446             stream << "Assoc. sensor ids: ";
00447             for(int i = 0; i < (*it).GetSensorNr(); i++) stream << (*it).sensor[i]->GetID() << " ";
00448             if(!m_real_time) stream << "\nAssoc. trigger id: " << (*it).trigger->GetID() << "\n";
00449             stream << "Next sync: " << (*it).snode->GetNextHorizon() << "\nT_sync: " << (*it).T_sync << "\n----\n";
00450           }
00451 
00452           JFR_DEBUG(stream.str());
00453         }
00454         
00455         void PrintRobots()
00456         {
00457           typename RobotsArray::iterator  it;
00458           for(it = m_rob_array.begin(); it != m_rob_array.end(); it++) {
00459             (*it)->PrintRobot();
00460             PrintNodes(*(*it));
00461           }
00462         }
00463     
00464         unsigned short GetNumRobots() { return m_rob_num; }
00465   
00466       private:
00467   
00468         void PrepareSimu(bool delayed, bool multi)
00469         {
00470           for(int i = 0; i < GetNumRobots(); i++) InitRobotSensors(i, delayed);
00471           PrintRobots();
00472     
00473           ClearAllMeasurements();
00474           SimulatePropPerception(delayed, multi);
00475   
00476           CreateGDHERobots();
00477           SetGDHERobotsPos();
00478           DrawGDHETrueTraj();
00479         }
00480     
00481         void ClearSimu()
00482         {
00483           DestroyAllSensorNodes();
00484           ClearRobotsAccelProfile();
00485           RemoveGDHETraj();
00486           RemoveGDHETargetsSegment();
00487           DestroyRobots();
00488         }
00489     
00490         void run_Simu_Multi(bool delayed)
00491         {
00492           typename RobotsArray::iterator  it;
00493           
00494           PrepareSimu(delayed, true);
00495           m_multi_rob.ResetCounter();
00496           m_multi_rob.ClearBuffer();
00497             
00498           for(it = m_rob_array.begin(); it != m_rob_array.end(); it++)
00499               m_multi_rob.AddRobotMeasurements(*(*it));
00500     
00501           if(delayed)
00502             m_multi_rob.DumpAllMeasures("sim_multi_delayed.dat");
00503           else
00504             m_multi_rob.DumpAllMeasures("sim_multi.dat");
00505     
00506           // complete TrackerSimu if needed
00507           if(m_track_other_robot) {
00508             m_rob_array[0]->m_tracker_sensor->AddRobotToTracker(m_rob_array[1]);
00509             m_rob_array[1]->m_tracker_sensor->AddRobotToTracker(m_rob_array[0]);
00510           }
00511             
00512           m_rob_array[0]->m_nodes[2].snode->RequestConnection(m_rob_array[1]->m_nodes[2].snode->GetID());
00513     
00514           FilterMultiPerceptionSimulation(delayed);
00515     
00516           ClearSimu();
00517         }
00518           
00519         void run_Simu(bool delayed)
00520         {
00521           typename RobotsArray::iterator  it;
00522           
00523           PrepareSimu(delayed, false);
00524             
00525           // Main loop
00526           if (m_real_time) {
00527             for(it = m_rob_array.begin(); it != m_rob_array.end(); it++)
00528               FilterPropPerceptionRealTime(*(*it));
00529           }
00530           else {
00531             for(it = m_rob_array.begin(); it != m_rob_array.end(); it++){
00532               (*it)->PrintRobot();
00533               PrintNodes(*(*it));
00534               FilterPropPerceptionSimulation(*(*it),delayed);
00535             }
00536           }
00537             
00538           ClearSimu();
00539         }
00540         
00541         // *** Initialization functions ***
00542         
00543         // pm_pose is the initial uncertainty of the position expressed with two sigma (same for all the dimensions)
00544         void InitState(bool use_biases, bool use_vel_model, double ts_position, VEC const& x0, VEC const& v0, VEC const& a0, double h, VEC & y_init, MSYM & Y_init)
00545         {
00546           unsigned short sv_x, sv_vx, sv_ax = 0;
00547           unsigned short sv_y, sv_vy, sv_ay = 0;
00548           unsigned short sv_z, sv_vz, sv_az = 0;
00549     
00550           sv_x = AccelMotion::SV_X;  sv_vx = AccelMotion::SV_VX;
00551           if(use_vel_model) {
00552             sv_y = 2; sv_vy = 3;
00553             sv_z = 4; sv_vz = 5;
00554           }
00555           else {
00556             sv_ax = AccelMotion::SV_AX;   sv_ay = AccelMotion::SV_AY;   sv_az = AccelMotion::SV_AZ;
00557             sv_y  = AccelMotion::SV_Y;    sv_vy = AccelMotion::SV_VY;
00558             sv_z  = AccelMotion::SV_Z;    sv_vz = AccelMotion::SV_VZ;
00559           }
00560           
00561           VEC  x_init(y_init.size());
00562           MSYM X_init(y_init.size(),y_init.size());
00563         
00564           y_init.clear();  Y_init.clear();   x_init.clear(); X_init.clear();
00565           
00566           x_init[sv_x]  = x0[AXIS_X];  x_init[sv_vx] = v0[AXIS_X];
00567           x_init[sv_y]  = x0[AXIS_Y];  x_init[sv_vy] = v0[AXIS_Y];
00568           x_init[sv_z]  = x0[AXIS_Z];  x_init[sv_vz] = v0[AXIS_Z];
00569           
00570           X_init(sv_x,sv_x)   = TWOSIGMA_TO_VAR(ts_position);
00571           X_init(sv_vx,sv_vx) = VARPOS_TO_VARVEL(TWOSIGMA_TO_VAR(ts_position), h);
00572     
00573           X_init(sv_y,sv_y)   = TWOSIGMA_TO_VAR(ts_position);
00574           X_init(sv_vy,sv_vy) = VARPOS_TO_VARVEL(TWOSIGMA_TO_VAR(ts_position), h);
00575           
00576           X_init(sv_z,sv_z)   = TWOSIGMA_TO_VAR(ts_position);
00577           X_init(sv_vz,sv_vz) = VARPOS_TO_VARVEL(TWOSIGMA_TO_VAR(ts_position), h);
00578           
00579     
00580           if(!use_vel_model) {
00581             X_init(sv_ax,sv_ax) = VARPOS_TO_VARACC(TWOSIGMA_TO_VAR(ts_position), h);
00582             X_init(sv_ay,sv_ay) = VARPOS_TO_VARACC(TWOSIGMA_TO_VAR(ts_position), h);
00583             X_init(sv_az,sv_az) = VARPOS_TO_VARACC(TWOSIGMA_TO_VAR(ts_position), h);
00584             x_init[sv_ax] = a0[AXIS_X];   x_init[sv_ay] = a0[AXIS_Y];   x_init[sv_az] = a0[AXIS_Z];
00585     
00586             if (use_biases)
00587               {
00588               X_init(AccelMotion::SV_BAX,AccelMotion::SV_BAX) = TWOSIGMA_TO_VAR(0.08);
00589               X_init(AccelMotion::SV_BAY,AccelMotion::SV_BAY) = TWOSIGMA_TO_VAR(0.08);
00590               X_init(AccelMotion::SV_BAZ,AccelMotion::SV_BAZ) = TWOSIGMA_TO_VAR(0.08);
00591               }
00592           }
00593     
00594           
00595           UdUinversePD (Y_init, X_init);
00596           Float rcond = UdUinversePD (Y_init, X_init);
00597           Numerical_rcond  rclimit;
00598           rclimit.check_PD(rcond, "Initial X not PD");
00599           noalias(y_init) = prod(Y_init,x_init);
00600         }
00601     
00602           
00603         void InitRobotSensors(unsigned short robnr, bool delayed)
00604         {
00605           std::ostringstream     rob_name;
00606           CANode_CommFactory*    pCommFact;
00607           SNODE_CLASS*           pNode;
00608           EventSimu*             pEventSimu;
00609           RobotSimuNodesGen<SNODE_CLASS>*       rob;// = m_rob_array[robnr];
00610           TargetsPredictModelFactory*  pPredModeFact_targets;
00611           VEC                    var_tar(3*m_obj_to_track), var_sv_targets(m_targets_q_size), y_targ_init(m_targets_sv_size);
00612           MSYM                   Y_targ_init(m_targets_sv_size,m_targets_sv_size);
00613           int                    i,j;
00614     
00615           var_sv_targets.clear();
00616     
00617           rob_name << "R" << robnr;
00618             
00619           switch (robnr)
00620             {
00621             case 0 :
00622               {
00623               // Create R0
00624               // The robot has 4 sensors: 1 accelerometer, 1 velocimeter, 1 GPS and a 1 tracker
00625               // (exteroceptive sensor for tracking targets in the scene).
00626               // The accelerometer and the velocimeter feed the same sensor node 0
00627               // The GPS and the tracker have their own node (respectively 1 and 2)
00628               // This architecture has been chosen because:
00629               // 1) we separate sensors without delay (computational time neglectable) and the sensors with delay generally with a much longer period
00630               //    this allows to limit the size of the history buffer and thus, makes the insertion of asequent data with less computation time
00631               // 2) node 0 and 1 share the same state vector whereas 2 estimates another state vector
00632               // A constant acceleration prediction model has been chosen for node 0 and 1 because the acceleration is available whereas
00633               // a simpler model (constant speed has been chosen for node 2): the update rate is slow and the acceleration cannot be estimated
00634               // properly (using the position only)
00635               // The state vector of node 0 and 1 constains position, velocity, acceleration for each axis (dimension 9)
00636               // and the state vector of the node 2 has a dimension of 6+num_targets*3 (position, velocity for each axis and position of each target)
00637               // the prediction model of the target is static (constant position)
00638               // The node chosen as POM is node 0 because it updates the state more frequently.
00639   
00640               // Add a new robot to the list
00641               if(m_use_biases)
00642                 rob = new RobotSimuNodesGen<SNODE_CLASS>(3*CST_ACC_MODEL_DIM+BIASES_SIZE, rob_name.str(), robnr, false);
00643               else
00644                 rob = new RobotSimuNodesGen<SNODE_CLASS>(3*CST_ACC_MODEL_DIM, rob_name.str(), robnr, false);
00645                       
00646               m_rob_array.push_back(rob);
00647     
00648               // Setup initial conditions
00649               const unsigned short  num_sensors = 4;
00650               const unsigned short  num_nodes = 3;
00651               enum {NODE_POM, NODE_GPS, NODE_TRACK} ;
00652               CANode_PredModFactory*   pPredModeFact_accel;
00653               SensorNodeCompound       compound[num_nodes];
00654               VEC                      v3(CST_ACC_MODEL_DIM), y_init(rob->GetSvSize());
00655               VEC                      x_init(CST_ACC_MODEL_DIM), v_init(CST_ACC_MODEL_DIM);
00656               VEC                      v6(6);
00657               MSYM                     Y_init(rob->GetSvSize(),rob->GetSvSize());
00658               time                     t_orig;
00659                   
00660               x_init[0] = 0.0;  x_init[1] = 0.0; x_init[2] = 4.0;
00661               v_init[0] = 0.03; v_init[1] = 0.05;  v_init[2] = 0.15;
00662     
00663               //v_init[0] = 0.0; v_init[1] = 0.0; v_init[2] = 0.0;
00664     
00665               rob->FillProfile(PROF_SIZE_R1,accProfR1);
00666               rob->SetInitialConditions(v_init, x_init);
00667               t_orig = rob->GetMinTime();
00668                   
00669               // Create accelerometer
00670               AcceleroSimuUncorr *accelero = new AcceleroSimuUncorr(rob->GetSvSize(), CST_ACC_MODEL_DIM, m_use_biases);
00671               v3[0] = TWOSIGMA_TO_VAR(0.04);  v3[1] = v3[0];      v3[2] = v3[0];
00672               accelero->SetSeed(0);          accelero->SetSensorSimVariance(v3);
00673               v3[0] = TWOSIGMA_TO_VAR(0.041); v3[1] = v3[0];      v3[2] = v3[0];
00674               accelero->SetSensorModelVar(v3);
00675               accelero->SetPeriod(time::from_ms(16));
00676               if(m_use_biases) {
00677                 VEC v(3);
00678                 v.clear(); v[0]= 0.04; v[1] = 0.01;
00679     
00680                 v.clear();
00681                 accelero->SetBiases(v);
00682               }
00683               //accelero->PrintObservationModel();
00684     
00685               // Create GPS
00686               GPSSimuUncorr *gps = new GPSSimuUncorr(rob->GetSvSize(), CST_ACC_MODEL_DIM);
00687               v3[0] = TWOSIGMA_TO_VAR(0.05); v3[1] = v3[0];    v3[2] = v3[0];
00688               gps->SetSeed(1);                  gps->SetSensorSimVariance(v3);
00689               v3[0] = TWOSIGMA_TO_VAR(0.051);   v3[1] = v3[0];    v3[2] = v3[0];
00690               gps->SetSensorModelVar(v3);
00691               gps->SetPeriod(time::from_ms(250));
00692               if(delayed) gps->SetProcTime(time::from_ms(100));   // 100
00693               else gps->SetProcTime(time::null());
00694     
00695               // Create velocimeter
00696               VelocitySimuUncorr *vel = new VelocitySimuUncorr(rob->GetSvSize(), CST_ACC_MODEL_DIM);
00697               v3[0] = TWOSIGMA_TO_VAR(0.03); v3[1] = v3[0];    v3[2] = v3[0];
00698               vel->SetSeed(2);                  vel->SetSensorSimVariance(v3);
00699               v3[0] = TWOSIGMA_TO_VAR(0.031);   v3[1] = v3[0];    v3[2] = v3[0];
00700               vel->SetSensorModelVar(v3);
00701               vel->SetPeriod(time::from_ms(80));
00702     
00703               // Create tracker
00704               TrackerSimuUncorr  *tracker = new TrackerSimuUncorr(m_targets_sv_size,0, m_num_targets, rob->UseOnlyVel(),m_track_other_robot ? 1:0);
00705               for(i = 0; i < m_obj_to_track*3; i++) var_tar[i] = TWOSIGMA_TO_VAR(0.30);
00706               tracker->SetSeed(3);        tracker->SetSensorSimVariance(var_tar);
00707               for(i = 0; i < m_obj_to_track*3; i++) var_tar[i] = TWOSIGMA_TO_VAR(0.31);
00708               
00709               tracker->SetSensorModelVar(var_tar);
00710               tracker->SetPeriod(time::from_ms(1000));
00711               tracker->SetTimeOffset(time::from_ms(1250));
00712               if(delayed) tracker->SetProcTime(time::from_ms(700));
00713               else tracker->SetProcTime(time::null());
00714               InitSensorTargets(tracker);
00715               rob->SetTrackerPtr(tracker);
00716     
00717               // Add a trigger for feeding the tracker node with POM data
00718              /* pEventSimu = new EventSimu(0);
00719               pEventSimu->SetSeed(18);
00720               pEventSimu->SetPeriod(time::from_ms(60000));
00721               pEventSimu->SetUpdateTrackerWithPOM(true);*/
00722     
00723               // Add sensors to the robot
00724               rob->AddSensor(accelero);   compound[NODE_POM].AddSensor(accelero);
00725               rob->AddSensor(vel);        compound[NODE_POM].AddSensor(vel);
00726               rob->AddSensor(gps);        compound[NODE_GPS].AddSensor(gps);
00727               rob->AddSensor(tracker);    compound[NODE_TRACK].AddSensor(tracker);
00728     
00729               time now = time::from_ms(400);
00730                   
00731               //pEventSimu->SetTimeOffset(time::from_ms(400));
00732               //rob->AddSensor(pEventSimu);compound[NODE_TRACK].AddSensor(pEventSimu);
00733     
00734               for(i = 0; i < num_nodes; i++)
00735                 {
00736     
00737                   if(i != NODE_TRACK){
00738                     compound[i].T_sync = time::from_ms(125);
00739     
00740                     if(i == NODE_POM)
00741                       for(j = 0; j < CST_ACC_MODEL_DIM; j++) v3[j] = TWOSIGMA_TO_VAR(0.2);  // 0.2
00742                     else
00743                       for(j = 0; j < CST_ACC_MODEL_DIM; j++) v3[j] = TWOSIGMA_TO_VAR(0.2); /* 2e-2prediction more uncertain than NODE_POM
00744                                                                           because not direct measure of acceleration and longer period */
00745                         
00746                     pCommFact      = new CANode_CommFactory(rob->GetSvSize());
00747                     if(m_use_biases) {
00748                             
00749                     for(j = 0; j < CST_ACC_MODEL_DIM; j++) v6[j] = v3[j];
00750                     if(i == NODE_POM)
00751                       for(j = 3; j < 2*CST_ACC_MODEL_DIM; j++) v6[j] = TWOSIGMA_TO_VAR(0.01); // for the biases 1e-1
00752                     else
00753                       for(j = 3; j < 2*CST_ACC_MODEL_DIM; j++) v6[j] = TWOSIGMA_TO_VAR(0.01);   //
00754                             
00755                     pPredModeFact_accel   = new CANode_PredModFactory(CST_ACC_MODEL_DIM, rob->GetSvSize(), 2*CST_ACC_MODEL_DIM, v6, m_use_biases);
00756                     pNode = new  SNODE_CLASS(rob->GetSvSize(), compound[i].T_sync, pPredModeFact_accel, pCommFact, delayed);
00757                     //pNode->PrintPredModel();
00758                     }
00759                     else {
00760                       pPredModeFact_accel   = new CANode_PredModFactory(CST_ACC_MODEL_DIM, rob->GetSvSize(), CST_ACC_MODEL_DIM, v3, m_use_biases);
00761                       pNode = new  SNODE_CLASS(rob->GetSvSize(), compound[i].T_sync, pPredModeFact_accel, pCommFact, delayed);
00762                     }
00763                     InitState(m_use_biases, false, ts_pose, rob->GetInitialPosition(), rob->GetInitialVelocity(), rob->GetInitialAccel(), h, y_init, Y_init);
00764                     pNode->SetInitWithInfo(y_init, Y_init);
00765                   }
00766                   else {
00767                     compound[i].T_sync = time::from_ms(500);
00768                     compound[i].SetIsTrackerNode();
00769                         
00770                     // uncertainty for the robot's pose (11)
00771                     if(delayed) {
00772                       for(j = 0; j < 3; j++) var_sv_targets[j] = TWOSIGMA_TO_VAR(0.8);   // 0.8
00773                       for(j = 3; j < 3*m_rob_num; j++) var_sv_targets[j] = TWOSIGMA_TO_VAR(0.8); // 0.8 ?
00774                     }
00775                     else {
00776                       for(j = 0; j < 3; j++) var_sv_targets[j] = TWOSIGMA_TO_VAR(0.5); // 11, 6
00777                       for(j = 3; j < 3*m_rob_num; j++) var_sv_targets[j] = TWOSIGMA_TO_VAR(0.5);
00778                     }
00779                         
00780                     // 0.01 -> 2 sigma = 5 cm for a period of 1s (this is the uncertainty for the state prediction of the target)
00781                     for(j = 3*m_rob_num; j < m_targets_q_size; j++) var_sv_targets[j] = TWOSIGMA_TO_VAR(0.0001); // 0.0001
00782     
00783                     pCommFact      = new CANode_CommFactory(m_targets_sv_size);
00784                     pPredModeFact_targets   = new TargetsPredictModelFactory(m_targets_sv_size, m_rob_num, robnr, var_sv_targets);
00785                     pNode = new SNODE_CLASS(m_targets_sv_size, compound[i].T_sync, pPredModeFact_targets, pCommFact, delayed);
00786                     InitTargetState(ts_pose_targets, ts_pose, rob->GetRobID(), rob->GetInitialPosition(), rob->GetInitialVelocity(), y_targ_init, Y_targ_init);
00787                     pNode->SetInitWithInfo(y_targ_init, Y_targ_init);
00788                   }
00789     
00790                   pNode->SetEpsilon(time::from_us(1));
00791                   pNode->SetHistoryLength(time(2.0));
00792                   pNode->SetNow(t_orig);
00793                   compound[i].snode = pNode;
00794     
00795                   if (!m_real_time)
00796                   {
00797                     pEventSimu = new EventSimu(pNode->GetID());
00798                     if(i == NODE_TRACK) pEventSimu->SetSyncTracker(true);
00799                     
00800                     pEventSimu->SetTimeOffset(now);
00801                     pNode->SetNextHorizon(now);
00802                         
00803                     pEventSimu->SetSeed(num_sensors+i);
00804                     pEventSimu->SetPeriod(compound[i].T_sync);
00805                         
00806                     compound[i].trigger = pEventSimu;
00807                     rob->AddSensor(pEventSimu);
00808                     pNode->SetLogChanFilters(true);
00809                   }
00810                   else pNode->InitNode();
00811                       
00812                   rob->m_nodes.push_back(compound[i]);
00813                       
00814                   now = now + time::from_ms(compound[i].T_sync.ms()/num_nodes);
00815                 }
00816     
00817               // Add same time stamping jitter for all sensors
00818               rob->InitStampJitter(time::from_ms(1));
00819     
00820               // Connect nodes together
00821               rob->m_nodes[NODE_POM].snode->RequestConnection(rob->m_nodes[NODE_GPS].snode->GetID());
00822               rob->m_POM_node = rob->m_nodes[NODE_POM].snode;
00823               rob->m_Tracker_node = rob->m_nodes[NODE_TRACK].snode;
00824     
00825               // Set robot's color
00826               rob->SetColor(255,0,0);
00827               }
00828               break;
00829                   
00830             case 1 :
00831               {
00832               // Create R1
00833               // The robot has 3 sensors: 1 velocimeter (sensor node 0), 1 GPS (sensor node 1) and a 1 tracker (sensor node 2)
00834               // A constant velocity prediction model has been chosen for all nodes (the acceleration is not measured)
00835               // The state vector of node 0 and 1 constains position, velocity for each axis (dimension 6)
00836               // Node 2 has the same caracteristics as node 2 of R0
00837               // The node chosen as POM is node 0 because it updates the state more frequently
00838     
00839               // Create a new robot and add to the list
00840               rob = new RobotSimuNodesGen<SNODE_CLASS>(2*CST_SPD_MODEL_DIM, rob_name.str(), robnr, true);
00841               m_rob_array.push_back(rob);
00842     
00843               // Setup initial condition and acceleration profile
00844               const unsigned short  num_sensors = 4;
00845               enum {NODE_POM, NODE_GPS, NODE_TRACK, NODE_TRACK_ROB};
00846               CSNode_PredModFactory*   pPredModeFact;
00847               SensorNodeCompound    compound[num_sensors];
00848               VEC                      v3(CST_SPD_MODEL_DIM), y_init(rob->GetSvSize());
00849               VEC                      x_init(CST_SPD_MODEL_DIM), v_init(CST_SPD_MODEL_DIM);
00850               MSYM                     Y_init(rob->GetSvSize(),rob->GetSvSize());
00851               time                     t_orig;
00852                   
00853               x_init[0] = 3.0;   x_init[1] = -1.0;    x_init[2] = 0.0;
00854               v_init[0] = 0.05;  v_init[1] = -0.02;   v_init[2] = 0.0;
00855     
00856               rob->FillProfile(PROF_SIZE_R2,accProfR2);
00857               rob->SetInitialConditions(v_init, x_init);
00858               t_orig = rob->GetMinTime();
00859     
00860     
00861               // Create GPS
00862               GPSSimuUncorr *gps = new GPSSimuUncorr(rob->GetSvSize(), CST_SPD_MODEL_DIM, true);
00863               v3[0] = TWOSIGMA_TO_VAR(0.06); v3[1] = v3[0];    v3[2] = v3[0];
00864               gps->SetSeed(4);                  gps->SetSensorSimVariance(v3);
00865               v3[0] = TWOSIGMA_TO_VAR(0.061);   v3[1] = v3[0];    v3[2] = v3[0];
00866               gps->SetSensorModelVar(v3);
00867               gps->SetPeriod(time::from_ms(200));
00868               if(delayed) gps->SetProcTime(time::from_ms(120));
00869               else gps->SetProcTime(time::null());
00870     
00871               // Create velocimeter
00872               VelocitySimuUncorr *vel = new VelocitySimuUncorr(rob->GetSvSize(), CST_SPD_MODEL_DIM, true);
00873               v3[0] = TWOSIGMA_TO_VAR(0.02); v3[1] = v3[0];    v3[2] = v3[0];
00874               vel->SetSeed(5);                  vel->SetSensorSimVariance(v3);
00875               v3[0] = TWOSIGMA_TO_VAR(0.021);   v3[1] = v3[0];    v3[2] = v3[0];
00876               vel->SetSensorModelVar(v3);
00877               vel->SetPeriod(time::from_ms(20));
00878     
00879               // Create tracker
00880               TrackerSimuUncorr  *tracker = new TrackerSimuUncorr(m_targets_sv_size,robnr, m_num_targets, rob->UseOnlyVel(),m_track_other_robot ? 1:0);
00881               for(i = 0; i < m_obj_to_track*3; i++) var_tar[i] = TWOSIGMA_TO_VAR(0.25);
00882               tracker->SetSeed(6);        tracker->SetSensorSimVariance(var_tar);
00883               for(i = 0; i < m_obj_to_track*3; i++) var_tar[i] = TWOSIGMA_TO_VAR(0.26);
00884               tracker->SetSensorModelVar(var_tar);
00885               tracker->SetPeriod(time::from_ms(1000));
00886               tracker->SetTimeOffset(time::from_ms(1000));
00887               if(delayed) tracker->SetProcTime(time::from_ms(900));
00888               else tracker->SetProcTime(time::null());
00889               InitSensorTargets(tracker);
00890               tracker->PrintTargets();
00891               rob->SetTrackerPtr(tracker);
00892     
00893               rob->AddSensor(gps);     compound[NODE_GPS].AddSensor(gps);
00894               rob->AddSensor(vel);     compound[NODE_POM].AddSensor(vel);
00895               rob->AddSensor(tracker); compound[NODE_TRACK].AddSensor(tracker);
00896     
00897               // Add triggers
00898               time now = time::from_ms(450);
00899     
00900              /* pEventSimu = new EventSimu(0);
00901               pEventSimu->SetSeed(19);
00902               pEventSimu->SetPeriod(time::from_ms(60000));
00903               pEventSimu->SetUpdateTrackerWithPOM(true);
00904     
00905               pEventSimu->SetTimeOffset(time::from_ms(400));
00906               //rob->AddSensor(pEventSimu);compound[NODE_TRACK].AddSensor(pEventSimu);*/
00907                   
00908     
00909               // Configure sensor nodes
00910               for(i = 0; i < num_sensors; i++)
00911                 {
00912                   if(i == NODE_TRACK_ROB) break;
00913                   if (i != NODE_TRACK) {
00914                     compound[i].T_sync = time::from_ms(125);
00915     
00916                     if(i == NODE_POM)
00917                     for(j = 0; j < CST_ACC_MODEL_DIM; j++) v3[j] = TWOSIGMA_TO_VAR(0.7); // 0.7
00918                     else
00919                     for(j = 0; j < CST_ACC_MODEL_DIM; j++) v3[j] = TWOSIGMA_TO_VAR(0.7); // 0.7
00920     
00921                     pCommFact      = new CANode_CommFactory(rob->GetSvSize());
00922                     pPredModeFact  = new CSNode_PredModFactory(CST_SPD_MODEL_DIM, rob->GetSvSize(), CST_SPD_MODEL_DIM, v3);
00923                     pNode       = new SNODE_CLASS(rob->GetSvSize(), compound[i].T_sync, pPredModeFact, pCommFact, delayed);
00924                     InitState(false, true, ts_pose, rob->GetInitialPosition(), rob->GetInitialVelocity(), rob->GetInitialAccel(), h, y_init, Y_init);
00925                     pNode->SetInitWithInfo(y_init, Y_init);
00926                   }
00927                   else {
00928                     compound[i].T_sync = time::from_ms(500);
00929                     compound[i].SetIsTrackerNode();
00930     
00931                     if(delayed){
00932                     for(j = 3; j < 3*m_rob_num; j++) var_sv_targets[j] = TWOSIGMA_TO_VAR(0.23); // 0.23
00933                     for(j = 0; j < 3; j++) var_sv_targets[j] = TWOSIGMA_TO_VAR(0.23);  // 0.23
00934                     }
00935                     else {
00936                     for(j = 3; j < 3*m_rob_num; j++) var_sv_targets[j] = TWOSIGMA_TO_VAR(3); // 5e3
00937                     for(j = 0; j < 3; j++) var_sv_targets[j] = TWOSIGMA_TO_VAR(3);
00938                     }
00939                             
00940                     for(j = 3*m_rob_num; j < m_targets_q_size; j++) var_sv_targets[j] = TWOSIGMA_TO_VAR(0.0001); // 0.0001
00941                         
00942                     pCommFact      = new CANode_CommFactory(m_targets_sv_size);
00943                     pPredModeFact_targets   = new TargetsPredictModelFactory(m_targets_sv_size, m_rob_num,1, var_sv_targets);
00944                     pNode = new SNODE_CLASS(m_targets_sv_size, compound[i].T_sync, pPredModeFact_targets, pCommFact, delayed);
00945                     //pNode->PrintPredModel();
00946                     InitTargetState(ts_pose_targets, ts_pose, rob->GetRobID(), rob->GetInitialPosition(), rob->GetInitialVelocity(), y_targ_init, Y_targ_init);
00947                     pNode->SetInitWithInfo(y_targ_init, Y_targ_init);
00948                   }
00949     
00950                   pNode->SetEpsilon(time::from_us(1));
00951                   pNode->SetHistoryLength(time(1.5));
00952                   pNode->SetNow(t_orig);
00953                   compound[i].snode = pNode;
00954                       
00955                   if (!m_real_time) {
00956                     pEventSimu = new EventSimu(pNode->GetID());
00957                     if(i == NODE_TRACK) pEventSimu->SetSyncTracker(true);
00958                     pEventSimu->SetSeed(num_sensors+i);
00959                     pEventSimu->SetPeriod(compound[i].T_sync);
00960                     if(i == NODE_TRACK){
00961                     pEventSimu->SetSyncTracker(true);
00962                     //pEventSimu->SetTimeOffset(time::from_ms(2050));
00963                     //pNode->SetNextHorizon(time::from_ms(2050));
00964                     }
00965                     else
00966                           {
00967                             pEventSimu->SetTimeOffset(now);
00968                             pNode->SetNextHorizon(now);
00969                           }
00970                         
00971                         
00972                     compound[i].trigger = pEventSimu;
00973                     rob->AddSensor(pEventSimu);
00974                     pNode->SetLogChanFilters(true);
00975                   }
00976                   else pNode->InitNode();
00977                       
00978                   
00979                   //pNode->SetLastSyncTime(now);
00980                   now = now + time::from_ms(compound[i].T_sync.ms()/num_sensors);
00981                   rob->m_nodes.push_back(compound[i]);
00982                 }
00983     
00984               // Same jitter for all sensors
00985               rob->InitStampJitter(time::from_ms(1));
00986     
00987               // Connect nodes together (accelerometer in the middle)
00988               rob->m_nodes[NODE_POM].snode->RequestConnection(rob->m_nodes[NODE_GPS].snode->GetID());
00989               rob->m_POM_node = rob->m_nodes[NODE_POM].snode;
00990               rob->m_Tracker_node = rob->m_nodes[NODE_TRACK].snode;
00991     
00992               // Setcolor
00993               rob->SetColor(0,255,0);
00994               }
00995               break;
00996     
00997             default: JFR_DEBUG( "Internal error!\n"); exit(EXIT_FAILURE);
00998             }
00999     
01000         }
01001     
01002         void InitSensorTargets(TrackerSimuUncorr * tracker)
01003         {
01004           for(int i = 0; i < tracker->GetNumTargets(); i++){
01005             tracker->AddTarget(m_targets[i]->GetPose());
01006           }
01007         }
01008     
01009         void InitTargetState(double ts_position, double ts_pose_robot, unsigned short rob_num, VEC const& p0, VEC const& v0, VEC & y_init, MSYM & Y_init)
01010         {
01011           int i,j,k;
01012           VEC  x_init(y_init.size()),v3(3);
01013           MSYM X_init(y_init.size(),y_init.size());
01014           
01015           y_init.clear();  Y_init.clear();   x_init.clear(); X_init.clear();
01016     
01017             // Init state vector
01018           k = rob_num * 6;
01019           for (i = 0; i < 3; i++) {
01020             x_init[k++] = p0[i];
01021             x_init[k++] = v0[i];
01022           }
01023     
01024           k = m_rob_num * 6;
01025           for(i = 0; i < m_num_targets; i++) {
01026             v3 = m_targets[i]->GetPose();
01027             for(j = 0; j < 3; j++) x_init[k++] = v3[j];
01028           }
01029     
01030             // Init cov matrix
01031           for(i = 3*m_rob_num; i < m_targets_sv_size; i++) X_init(i,i) = TWOSIGMA_TO_VAR(ts_position);
01032             
01033           for(i = 0; i < 6*m_rob_num; i += 2) {
01034             X_init(i,i)     = TWOSIGMA_TO_VAR(ts_pose_robot);
01035             X_init(i+1,i+1) = VARPOS_TO_VARVEL(TWOSIGMA_TO_VAR(ts_pose_robot), 1.0);
01036           }
01037     
01038           
01039           UdUinversePD (Y_init, X_init);
01040           Float rcond = UdUinversePD (Y_init, X_init);
01041           Numerical_rcond  rclimit;
01042           rclimit.check_PD(rcond, "Initial X not PD");
01043           noalias(y_init) = prod(Y_init,x_init);
01044         }
01045     
01046         void InitTargets(double tpose[][3] , unsigned short num_targets)
01047         {
01048           Target     *target;
01049           VEC p0(3);
01050           int i,j;
01051             
01052           p0.clear();
01053             
01054           for(i = 0; i < num_targets; i++)
01055             {
01056               for(j=0;j<3;j++) p0[j] = tpose[i][j];
01057               target = new Target(p0, m_rob_num);
01058               m_targets.push_back(target);
01059             }
01060         }
01061     
01062         void SetLocalInfoTime(RobotSimuNodesGen<SNODE_CLASS> & rob, time const& t)
01063         {
01064           typename SNodesArray::iterator it;
01065           for(it = rob.m_nodes.begin(); it != rob.m_nodes.end(); it++)
01066             (*it).snode->SetLocalInfoTime(t);
01067         }
01068     
01069         void StartNodesRealTime(RobotSimuNodesGen<SNODE_CLASS> & rob, time const& now)
01070         {
01071           typename SNodesArray::iterator it;
01072     
01073           for(it = rob.m_nodes.begin(); it != rob.m_nodes.end(); it++)
01074             {
01075               (*it).snode->SetLocalInfoTime(now);
01076               (*it).snode->SetNow(now);
01077               (*it).snode->Start(now+(*it).snode->GetNextHorizon());
01078             }
01079         }
01080     
01081         void ResetTimers(RobotSimuNodesGen<SNODE_CLASS> & rob)
01082         {
01083           typename SNodesArray::iterator it;
01084     
01085           for(it = rob.m_nodes.begin(); it != rob.m_nodes.end(); it++)
01086             (*it).m_sw_check.Restart();
01087         }
01088     
01089           // *** Simulation functions ***
01090         void SimulatePropPerception(RobotSimuNodesGen<SNODE_CLASS> *rob, bool delayed, bool multi)
01091         {
01092           std::ostringstream      rob_name, sensor_name;
01093           rob->SimulatePropPerception();
01094           std::string smulti = multi ? "multi_":"";
01095       
01096     
01097           if(delayed) {
01098             rob->FillUpMeasurementSet(RobotSimu::SORT_STAMP_AVAILABLE);
01099             rob_name << "sim_all_delayed_" << smulti << rob->GetName() << ".dat";
01100             rob->DumpAllMeasures((char *) rob_name.str().c_str());
01101             for(int i = 0; i < rob->GetNumSensors(); i++) {
01102               sensor_name.str("");
01103               sensor_name << rob->GetSensorName(i) << "_delayed_" <<  smulti << rob->GetName() << ".dat";
01104               rob->DumpSensor(sensor_name.str().c_str(),i);
01105             }
01106           }
01107           else {
01108             rob->FillUpMeasurementSet(RobotSimu::SORT_STAMP_SENSED);
01109             rob_name << "sim_all_not_delayed_" <<  smulti << rob->GetName() << ".dat";
01110             rob->DumpAllMeasures((char *) rob_name.str().c_str());
01111             for(int i = 0; i < rob->GetNumSensors(); i++) {
01112               sensor_name.str("");
01113               sensor_name << rob->GetSensorName(i) << "_not_delayed_" << smulti << rob->GetName() << ".dat";
01114               rob->DumpSensor(sensor_name.str().c_str(),i);
01115             }
01116           }
01117 
01118           rob_name.str("");
01119           rob_name << "true_state_" << smulti << rob->GetName() << ".dat";
01120           rob->DumpTruth(rob_name.str().c_str());
01121         }
01122     
01123         void SimulatePropPerception(bool delayed, bool multi)
01124         {
01125           typename RobotsArray::iterator  it;
01126           for(it = m_rob_array.begin(); it != m_rob_array.end(); it++) SimulatePropPerception(*it, delayed, multi);
01127         }
01128     
01129         void FilterMultiPerceptionSimulation(bool delayed)
01130         {
01131           unsigned short     PERSIST = 20;
01132           unsigned int       i,m;
01133           time               rel_time, node_time;
01134           InfoContainer      iI_tracker(m_targets_sv_size);
01135           InfoContainer      iI_POM(m_targets_sv_size);
01136           SensorNodeCompound compound, compound_other;
01137           ParametersGeneric  params;
01138           VEC                targets_sv(m_num_targets * 3);    // true x, estim x, var x
01139           VEC                targets_var(m_num_targets * 3);
01140           VEC                pose_robot(3);
01141           RobotSimuNodesGen<SNODE_CLASS>  *rob;
01142     
01143           targets_var.clear();
01144           targets_sv.clear();
01145             
01146             // Create dump files
01147           for(i = 0; i < m_rob_num; i++)
01148           {
01149             rob = m_rob_array[i];
01150             rob->CreateDumpFile(delayed ? ("sim_multi_"+rob->GetName()+"_delayed.log"):("sim_multi_"+rob->GetName()+".log"));
01151             rob->ResetSensorsGetNext();
01152           }
01153     
01154           m_multi_rob.ResetCounter();
01155           StopWatch sw;
01156             
01157           for (m = 0; m < m_multi_rob.GetNrMeas(); m++)
01158           {
01159             rob = GetRobByID(m_multi_rob.GetCurRobID());
01160             rel_time = m_multi_rob.GetCurRelTime();
01161 
01162             if(m_multi_rob.GetCurSensorType() != SENSOR_EVENT)    // Normal information fusion
01163             {
01164               compound = GetCompoundBySensorID(*rob, m_multi_rob.GetCurSensorID());
01165               node_time = compound.snode->GetLocalInfoTime();
01166               params.h = rel_time - node_time;
01167 
01168               if(m_multi_rob.GetCurSensorType() != SENSOR_TRACK)
01169               {
01170                 m_multi_rob.GetCurInfo(rob->m_CurInfo);
01171                 compound.snode->FeedWithSensorData(rob->m_CurInfo, params);
01172               }
01173               else
01174               {
01175                 m_multi_rob.GetCurInfo(iI_tracker);
01176 
01177                 if (rob->m_first) {
01178                   compound.snode->SetLocalInfoTime(iI_tracker.GetTime());
01179                   //compound.snode->SetLocalInfoTime(rob->m_CurInfo.GetTime());
01180                   params.h = time::null();
01181                   rob->m_first = false;
01182                 }
01183                 //else
01184                 {
01185 
01186                   // FIXME use covariance intersect instead
01187                   rob->m_CurInfo = rob->m_POM_node->GetLocalInfo(); // in case of delayed data then recall history of POM !!!
01188                   FillInfoTargets(rob->GetRobID(), rob->UseOnlyVel(), rob->m_CurInfo, iI_POM);
01189                   iI_POM.SetTime(rob->m_CurInfo.GetTime());
01190 
01191 
01192                   if(iI_POM.GetTime() > iI_tracker.GetTime()){
01193                     compound.snode->FeedWithSensorData(iI_tracker, params);
01194                     params.h = iI_POM.GetTime() - iI_tracker.GetTime();
01195                     compound.snode->FeedWithSensorData(iI_POM, params);
01196                   }
01197                   else {
01198                     params.h = node_time - iI_POM.GetTime();
01199                     compound.snode->FeedWithSensorData(iI_POM, params);
01200                     params.h = iI_tracker.GetTime() - iI_POM.GetTime();
01201                     compound.snode->FeedWithSensorData(iI_tracker, params);
01202                   }
01203                 }
01204                 pose_robot = GetPose(rob->m_POM_node->GetLocalState().m_info, rob->UseOnlyVel());
01205                 rob->m_gdhe_persist = PERSIST;
01206               }
01207             }
01208             else // Node sync
01209             {
01210               compound = GetCompoundBySNodeID(*rob, m_multi_rob.GetCurTriggerNodeID());
01211               compound.snode->UpdateWithChannelFilters(rel_time);
01212               if(m_multi_rob.IsCurTriggerSyncTracker())
01213               {
01214                 if(rob->GetRobID() == 0)
01215                   m_rob_array[1]->m_Tracker_node->DbgWaitInfoFromNode(compound.snode->GetID());
01216                 else
01217                   m_rob_array[0]->m_Tracker_node->DbgWaitInfoFromNode(compound.snode->GetID());
01218               }
01219               else DbgWaitOtherNodes(*rob, compound.snode->GetID());
01220 
01221               compound.snode->SetNextHorizon(rel_time + compound.snode->GetSyncPeriod());
01222             }
01223 
01224             if(rob->GetTrackerID() != -1) FillTargetSV(*rob, rob->GetTrackerID(), targets_sv, targets_var);
01225 
01226             if(m % 30 == 0) {
01227               SetGDHERobotsPos();
01228               DrawGDHERobotsTraj();
01229               m_gdhecl.Redraw();
01230             }
01231 
01232             if(rob->GetTrackerID() != -1)
01233             {
01234               if(rob->m_gdhe_persist == PERSIST) {
01235                 SetGDHERobotsPos();
01236                 DrawGDHERobotsTraj();
01237                 DrawGDHEPerception(pose_robot, rob->GetRobID());
01238                 ChangeGDHEAllEllipsoids(targets_var);
01239                 m_gdhecl.Redraw();
01240               }
01241 
01242               if(rob->m_gdhe_persist == 0) {
01243                 SetGDHERobotsPos();
01244                 DrawGDHERobotsTraj();
01245                 RemoveGDHETargetsSegment(rob->GetRobID());
01246                 m_gdhecl.Redraw();
01247               }
01248 
01249               rob->m_gdhe_persist--;
01250             }
01251 
01252             rob->DumpCurrentState(rel_time);
01253             rob->DumpCustom(targets_sv);
01254 
01255             m_multi_rob.IncCounter();
01256             
01257           } // end for
01258     
01259           JFR_DEBUG( "Simulation effective time: " << sw.ElapsedSec() << "\n");
01260         }
01261     
01262         void FilterPropPerceptionSimulation(RobotSimuNodesGen<SNODE_CLASS> & rob, bool delayed)
01263         {
01264           int m;
01265           Test_Dump<SNODE_CLASS>    dump(rob.m_nodes.size(), &rob.m_nodes, (delayed ? "sim_"+rob.GetName()+"_delayed.log":"sim_"+rob.GetName()+".log"));
01266           time                      rel_time, node_time, max_time;
01267           InfoContainer             iI(rob.GetSvSize());
01268           InfoContainer             iI_tracker(m_targets_sv_size);
01269           InfoContainer             iI_POM(m_targets_sv_size);
01270           ParametersGeneric         params;
01271           SensorNodeCompound        compound;
01272           time                      now = rob.GetMinTime(), save;
01273           VEC                       targets_sv(m_num_targets * 3); // true x, estim x, var x
01274           VEC                       targets_var(m_num_targets * 3);  // variances vx_t0 vy_to vz_to etc...
01275           VEC                       pose_robot(3);
01276           bool                      just_perceived = false, first = true;
01277           int                       tracker_id = -1; // id of the sensor tracker -1 if no tracker
01278     
01279           tracker_id = rob.GetSensorID(SENSOR_TRACK,0);
01280   
01281           //std::cout << "toto\n";
01282     
01283           pose_robot.clear();
01284           rob.ResetCounter();
01285           max_time = time(rob.GetMaxTime());
01286           StopWatch sw;
01287     
01288           for (m = 0; m < rob.GetNrMeas(); m++)
01289             {
01290               rel_time = rob.GetCurrentRelTime();
01291     
01292               if(rob.GetCurrentSensorType() != SENSOR_EVENT)  // Normal information fusion
01293               {
01294                 if(rob.GetCurrentSensorType() != SENSOR_TRACK) rob.GetCurrentInfo(iI);
01295                 else {
01296                   rob.GetCurrentInfo(iI_tracker);
01297                 }
01298                 compound = GetCompoundBySensorID(rob, rob.GetCurrentSensorID());
01299                 /*if(rob.GetCurrentSensorID() == 0 || rob.GetCurrentSensorID() == 2)
01300                       {
01301                       JFR_DEBUG( "S id " << rob.GetCurrentSensorID() << " node " << compound.snode->GetID() << "\n");
01302                   }*/
01303                 node_time = compound.snode->GetLocalInfoTime();
01304                 params.h = rel_time - node_time;
01305                 if(rob.GetCurrentSensorType() != SENSOR_TRACK) compound.snode->FeedWithSensorData(iI, params);
01306                 else {
01307                   // Extract info from pom (considered as a sensor)
01308                   // !!! delayed data not ready
01309                       
01310                   if (first) {
01311                     compound.snode->SetLocalInfoTime(iI_tracker.GetTime());
01312                     params.h = time::null();//time(1,0);
01313                     first = false;
01314                   }
01315                       
01316                   iI = rob.m_POM_node->GetLocalInfo(); // in case of delayed data then recall history of POM !!!
01317                       
01318                   FillInfoTargets(rob.GetRobID(), rob.UseOnlyVel(), iI, iI_POM);
01319                   //PrettyPrintMatrix(iI.m_InfoMat, "iI");
01320                   /*PrettyPrintMatrix(iI_POM.m_InfoMat, "iI_POM");
01321                         PrettyPrintVector(iI_POM.m_info, "info POM");
01322                         PrettyPrintMatrix(iI_multi.m_InfoMat, "iI_multi");
01323                         PrettyPrintVector(iI_multi.m_info, "iI_multi info");*/
01324                   //PrettyPrintVector(iI.m_info, "info");
01325                       
01326                   // Add to the tracker info
01327                   //JFR_DEBUG( "Time pom: " << iI.GetTime() << " tracker: " << iI_multi.GetTime() << "\n");
01328                       
01329                   //iI_POM.SetTime(iI_tracker.GetTime());
01330                   //iI_multi =  iI_multi + iI_POM;
01331                   /* PrettyPrintMatrix(iI_multi.m_InfoMat, "sum");
01332                     PrettyPrintVector(iI_multi.m_info, "sum info");*/
01333     
01334                   // Feed the node with POM and iI_track
01335                   //exit(-1);
01336                       
01337                   iI_POM.SetTime(iI.GetTime());
01338     
01339                   if(iI_POM.GetTime() > iI_tracker.GetTime()){
01340                     compound.snode->FeedWithSensorData(iI_tracker, params);
01341                     params.h = iI_POM.GetTime() - iI_tracker.GetTime();
01342                     compound.snode->FeedWithSensorData(iI_POM, params);
01343                   }
01344                   else {
01345                     compound.snode->FeedWithSensorData(iI_POM, params);
01346                     params.h = iI_tracker.GetTime() - iI_POM.GetTime();
01347                     compound.snode->FeedWithSensorData(iI_tracker, params);
01348                   }
01349     
01350                   pose_robot = GetPose(rob.m_POM_node->GetLocalState().m_info, rob.UseOnlyVel());
01351                       
01352                   just_perceived = true;
01353                 }
01354               }
01355               else {   // Event triggers sensor node sync
01356               if(rob.IsCurrentTriggerUpdatePOM())
01357                 {
01358                   JFR_DEBUG( "Update tracker with POM caused by trigger nr: " << rob.GetCurrentSensorID() << "\n");
01359                   compound = GetCompoundBySensorID(rob, rob.GetCurrentSensorID());
01360                   node_time = compound.snode->GetLocalInfoTime();
01361                   params.h = rel_time - node_time;
01362                   JFR_DEBUG( "h: " << params.h << "\n");
01363                       
01364                   iI = rob.m_POM_node->GetLocalInfo(); // in case of delayed data then recall history of POM !!!
01365                   FillInfoTargets(rob.GetRobID(), rob.UseOnlyVel(), iI, iI_POM);
01366                   iI_POM.SetTime(iI.GetTime());
01367                   compound.snode->FeedWithSensorData(iI_POM, params);
01368                 }
01369               else {
01370                 compound = GetCompoundBySNodeID(rob, rob.GetCurrentTriggerNodeID());
01371                 if(compound.T_sync > time(35,0)) { JFR_DEBUG( "Sync tracker\n"); }
01372                 else {
01373                   compound.snode->UpdateWithChannelFilters(rel_time);
01374                   DbgWaitOtherNodes(rob, compound.snode->GetID());
01375                   compound.snode->SetNextHorizon(rel_time + compound.snode->GetSyncPeriod());
01376                 }
01377               }
01378               }
01379     
01380               if(tracker_id != -1) FillTargetSV(rob, tracker_id, targets_sv, targets_var);
01381                 
01382               if(m % 30 == 0)
01383               {
01384                 SetGDHERobotsPos();
01385                 DrawGDHERobotsTraj();
01386     
01387                 if(tracker_id != -1) {
01388                   ChangeGDHEAllEllipsoids(targets_var);
01389                   if (just_perceived) {
01390                     DrawGDHEPerception(pose_robot, rob.GetRobID());
01391                     just_perceived = false;
01392                   }
01393                   else RemoveGDHETargetsSegment();
01394                 }
01395                   
01396                 m_gdhecl.Redraw();
01397               }
01398     
01399               dump.DumpCurrentState(now, rel_time, rob, rob.m_POM_node->GetID());
01400               dump.DumpCustom(targets_sv);
01401               rob.IncCounter();
01402             }
01403             std::ostringstream stream;
01404 
01405             
01406             stream << "Simulation effective time: " << sw.ElapsedSec() << "\n";
01407             stream << "Simulation time: " << rob.GetTimeSpan() << "\n";
01408             stream <<"Total measurements: " << rob.GetNrMeas() << "\n";
01409             JFR_DEBUG(stream.str());
01410           
01411         }
01412     
01413         void FilterPropPerceptionRealTime(RobotSimuNodesGen<SNODE_CLASS> & rob)
01414         {
01415           int m;
01416           Test_Dump<SNODE_CLASS>    dump(rob.m_nodes.size(), &rob.m_nodes, "rt_"+rob.GetName()+"_delayed.log");
01417           InfoContainer                iI(rob.GetSvSize());
01418           ParametersGeneric            params;
01419           double                    refresh = 0.0, elapsed_sec;
01420           time                      now, rel_time, stamp_time, node_time, elapsed;
01421     
01422           rel_time = time(3.5);
01423           JFR_DEBUG( "Wait connection ...");
01424           rel_time.sleep_period();
01425           JFR_DEBUG( "Done!\n");
01426             
01427             
01428           now = time::current();
01429           JFR_DEBUG( "Time origin: " << now << "\n");
01430       
01431           rob.ResetCounter();
01432           ResetTimers(rob);
01433             
01434           StartNodesRealTime(rob, now);
01435           JFR_DEBUG( "Nodes are running... \n");
01436     
01437           StopWatch sw;
01438             
01439           for (m = 0; m < rob.GetNrMeas() ; m++)
01440             {
01441               rel_time = rob.GetCurrentRelTime();    // sorted chronologically by sensed or available
01442               stamp_time = now + rel_time;
01443               stamp_time.sleep_until();
01444     
01445               if(rob.GetCurrentSensorType() == SENSOR_TRACK) {
01446               rob.IncCounter();
01447               continue;
01448               }
01449     
01450               SensorNodeCompound   &compound = GetCompoundBySensorID(rob, rob.GetCurrentSensorID());
01451               elapsed = compound.m_sw_check.ElapsedAndReset();
01452               node_time = compound.snode->GetLocalInfoTime();
01453     
01454               if (rel_time != time::null() && elapsed > rob.GetCurrentSensorPeriod() + rob.GetCurrentSensorPeriod())
01455               {
01456                 JFR_DEBUG( "Big delay! elapsed: " << elapsed << "\n");
01457                 //break;
01458               }
01459                 
01460               rob.GetCurrentInfo(iI);          // comes with the relative sensed time stamp
01461               iI.SetTime(now + iI.GetTime());
01462     
01463               if(node_time > stamp_time) params.h = time::null();
01464               else params.h = stamp_time - node_time;
01465               compound.snode->FeedWithSensorData(iI, params);
01466                 
01467                 // We are running in real time -> don't dump everything
01468               if(m % 10 == 0) {
01469               dump.DumpCurrentState(now, rel_time, rob, rob.m_POM_node->GetID());
01470               }
01471                 
01472               if((elapsed_sec = sw.Elapsed().to_double()) > refresh) {
01473               refresh = elapsed_sec;
01474               //compound.snode->ResizeHistory();
01475               /*SetGDHERobotsPos();
01476                 m_gdhecl.Redraw();
01477                 DrawGDHERobotsTraj();*/
01478               }
01479               rob.IncCounter();
01480             }
01481 
01482             std::ostringstream stream;
01483             stream << "Simulation time: " << rob.GetTimeSpan() << "\n";
01484             stream << "Real time: " << sw.Elapsed() << "\n";
01485             JFR_DEBUG(stream.str());
01486             KillNodes(rob);
01487         }
01488     
01489           // *** GDHE related functions ***
01490         void CreateGDHEWorld() {
01491           m_gdhecl.CleanAll();
01492           m_gdhecl.SetBkGroundColor(180,180,180);
01493           m_gdhecl.CreateGrid(-3., -3., 8., 8., 1.0, 255,160,160,"GRID0");
01494           m_gdhecl.CreateFrame(1.5,0,200,0,"FRAME0");
01495           m_gdhecl.Redraw();
01496         }
01497     
01498         void ClearGDHEWorld() {
01499             /*m_gdhecl.DeleteObject("GRID0");
01500             m_gdhecl.DeleteObject("FRAME0");*/
01501           m_gdhecl.CleanAll();
01502         }
01503     
01504         void CreateGDHERobots()
01505         {
01506           typename RobotsArray::iterator  it;
01507           char str[255];
01508             
01509           for(it = m_rob_array.begin(); it != m_rob_array.end(); it++){
01510             (*it)->GetName(str);
01511             m_gdhecl.CreateSphere(0.2, (*it)->m_color[0], (*it)->m_color[1], (*it)->m_color[2], str);
01512           }
01513         }
01514     
01515         void SetGDHERobotsPos()
01516         {
01517           typename RobotsArray::iterator  it;
01518           char str[255];
01519           VEC  pos(3);
01520             
01521           for(it = m_rob_array.begin(); it != m_rob_array.end(); it++){
01522             (*it)->GetName(str);
01523             InfoContainer const& state = (*it)->m_POM_node->GetLocalState();
01524             pos = GetPose(state.m_info, (*it)->UseOnlyVel());
01525             m_gdhecl.SetPos(str, pos[0], pos[1], pos[2]);
01526             (*it)->AddTrajPoint(pos[0], pos[1], pos[2]);
01527           }
01528         }
01529     
01530         void DrawGDHERobotsTraj()
01531         {
01532           typename RobotsArray::iterator  it;
01533             
01534           for(it = m_rob_array.begin(); it != m_rob_array.end(); it++){
01535             if((*it)->m_traj_id != 0) m_gdhecl.DeleteObject((*it)->m_traj_id);
01536             (*it)->m_traj_id = m_gdhecl.CreatePolyline((*it)->m_traj,(*it)->m_traj.size(),(*it)->m_color[0],(*it)->m_color[1],(*it)->m_color[2]);
01537           }
01538         }
01539     
01540         void DrawGDHESegment(unsigned short id, unsigned short rob_id, VEC const& origin, VEC const& destination)
01541         {
01542           unsigned int gd_id = m_targets[id]->GetGDHEIDSegment(rob_id);
01543           if(gd_id != 0) m_gdhecl.DeleteObject(gd_id);
01544           std::vector<VEC> poly;
01545           poly.push_back(origin);
01546           poly.push_back(destination);
01547           m_targets[id]->SetGDHEIDSegment(m_gdhecl.CreatePolyline(poly,2,0,255,255), rob_id);
01548         }
01549     
01550         void DrawGDHEPerception(VEC const& pos_rob, unsigned short rob_id)
01551         {
01552           for(int i = 0; i < m_num_targets; i++) {
01553             DrawGDHESegment(i,rob_id,pos_rob, m_targets[i]->GetPose());
01554           }
01555         }
01556     
01557         void DrawGDHETrueTraj()
01558         {
01559           typename RobotsArray::iterator  it;
01560           const int NPTS = 100;
01561           float traj[NPTS][3];
01562             
01563           for(it = m_rob_array.begin(); it != m_rob_array.end(); it++){
01564             if((*it)->m_true_traj_id != 0) m_gdhecl.DeleteObject((*it)->m_true_traj_id);
01565             (*it)->FillGdheTrajectory(NPTS, traj);
01566             (*it)->m_true_traj_id = m_gdhecl.CreatePolyline(traj,NPTS,255,255,255);
01567           }
01568         }
01569     
01570         void CreateGDHETargets()
01571         {
01572           typename TargetsArray::iterator it;
01573           unsigned short id;
01574             
01575           for(it = m_targets.begin(); it != m_targets.end(); it++){
01576             id = m_gdhecl.CreateEllipsoid(ts_pose_targets, ts_pose_targets, ts_pose_targets, 0, 255, 255);
01577             (*it)->SetGDHEID(id);
01578             VEC const& p0 = (*it)->GetPose();
01579             m_gdhecl.SetPos((*it)->GetGDHEID(), p0[0], p0[1], p0[2]);
01580           }
01581         }
01582     
01583         void ChangeGDHEEllipsoid(unsigned short target_nr, double vx, double vy, double vz) {
01584           m_gdhecl.DeleteObject(m_targets[target_nr]->GetGDHEID());
01585           m_targets[target_nr]->SetGDHEID(m_gdhecl.CreateEllipsoid(vx, vy, vz, 0, 255, 255));
01586           VEC const& p0 = m_targets[target_nr]->GetPose();
01587           m_gdhecl.SetPos(m_targets[target_nr]->GetGDHEID(), p0[0], p0[1], p0[2]);
01588         }
01589   
01590         void ChangeGDHEAllEllipsoids(VEC const& targets_var) {
01591           for(int i = 0, j = 0; i < m_num_targets; i++, j+=3)
01592             ChangeGDHEEllipsoid(i, 3.0 * sqrt(targets_var[j]), 3.0 * sqrt(targets_var[j+1]), 3.0 * sqrt( targets_var[j+2]));
01593         }
01594     
01595         void RemoveGDHETraj()
01596         {
01597           typename RobotsArray::iterator  it;
01598             
01599           for(it = m_rob_array.begin(); it != m_rob_array.end(); it++){
01600             if((*it)->m_traj_id != 0) {
01601               m_gdhecl.DeleteObject((*it)->m_traj_id);
01602               m_gdhecl.DeleteObject((*it)->m_true_traj_id);
01603             }
01604             (*it)->ClearTraj();
01605           }
01606         }
01607     
01608         void RemoveGDHETargetsSegment(unsigned short rob_num)
01609         {
01610           for(int i = 0; i < m_num_targets; i++) {
01611             if (m_targets[i]->GetGDHEIDSegment(rob_num) != 0) {
01612               m_gdhecl.DeleteObject(m_targets[i]->GetGDHEIDSegment(rob_num));
01613               m_targets[i]->SetGDHEIDSegment(0,rob_num);
01614             }
01615           }
01616         }
01617     
01618         void RemoveGDHETargetsSegment()
01619         {
01620           for(int i = 0; i < m_rob_num; i++) {
01621             RemoveGDHETargetsSegment(i);
01622           }
01623         }
01624     
01625         void RemoveGDHETargets()
01626         {
01627           typename TargetsArray::iterator it;
01628             
01629           for(it = m_targets.begin(); it != m_targets.end(); it++)
01630             m_gdhecl.DeleteObject((*it)->GetGDHEID());
01631         }
01632     
01633           // *** Misc functions ***
01634         VEC GetPose(VEC const& sv, bool use_vel_model)
01635         {
01636           VEC  p(3);
01637     
01638           p[0] = sv[AccelMotion::SV_X];
01639           if(use_vel_model){
01640             p[1] = sv[2];
01641             p[2] = sv[4];
01642           }
01643           else {
01644             p[1] = sv[AccelMotion::SV_Y];
01645             p[2] = sv[AccelMotion::SV_Z];
01646           }
01647           return p;
01648         }
01649     
01650         VEC GetVel(VEC const& sv, bool use_vel_model)
01651         {
01652           VEC  p(3);
01653     
01654           p[0] = sv[AccelMotion::SV_VX];
01655           if(use_vel_model){
01656             p[1] = sv[3];
01657             p[2] = sv[5];
01658           }
01659           else {
01660             p[1] = sv[AccelMotion::SV_VY];
01661             p[2] = sv[AccelMotion::SV_VZ];
01662           }
01663           return p;
01664         }
01665     
01666         SensorNodeCompound & GetCompoundBySensorID(RobotSimuNodesGen<SNODE_CLASS> & rob, unsigned short sensor_id)
01667         {
01668           typename SNodesArray::iterator it;
01669           it = rob.m_nodes.begin();
01670           while(it != rob.m_nodes.end() && !(*it).SensorBelongsToNodebyID(sensor_id)) it++;
01671     
01672           JFR_PRECOND (it != rob.m_nodes.end(), "end of buffer");
01673           return ((*it));
01674         }
01675     
01676         SensorNodeCompound const& GetCompoundBySNodeID(RobotSimuNodesGen<SNODE_CLASS> & rob, unsigned short node_id)
01677         {
01678           typename SNodesArray::iterator it;
01679           it = rob.m_nodes.begin();
01680           while(it != rob.m_nodes.end() && (*it).snode->GetID() != node_id) it++;
01681     
01682           JFR_PRECOND (it != rob.m_nodes.end(), "end of buffer");
01683           return ((*it));
01684         }
01685     
01686         RobotSimuNodesGen<SNODE_CLASS> * GetRobByID(unsigned short robid)
01687         {
01688           for(int i = 0; i < m_rob_num; i++) if(m_rob_array[i]->GetRobID() == robid) return m_rob_array[i];
01689           JFR_POSTCOND(0, "robot not found");
01690           return NULL;
01691         }
01692     
01693         void DbgWaitOtherNodes(RobotSimuNodesGen<SNODE_CLASS> & rob, unsigned short node_sending)
01694         {
01695           typename SNodesArray::iterator it;
01696           for (it = rob.m_nodes.begin(); it != rob.m_nodes.end(); it++)
01697             if((*it).snode->GetID() != node_sending) (*it).snode->DbgWaitInfoFromNode(node_sending);
01698         }
01699     
01700         void FillTargetSV(RobotSimuNodesGen<SNODE_CLASS> & rob, unsigned short sensor_id, VEC & targets_sv, VEC & targets_var) {
01701             // Get node containing target
01702           SensorNodeCompound        compound;
01703           unsigned short sv_size,offset,target_id;
01704     
01705           compound = GetCompoundBySensorID(rob, sensor_id);
01706           sv_size = compound.snode->GetSvSize();
01707           offset = sv_size-m_num_targets*3;
01708           InfoContainer const& iCont = compound.snode->GetLocalState();
01709           for(unsigned int i = 0, j = 0; i < targets_sv.size(); i = i+3,j = j + 3){
01710             target_id = i/3;
01711             targets_sv[i]     = m_targets[target_id]->GetPose()[0];
01712             targets_sv[i+1]   = iCont.m_info[offset+j];
01713             targets_sv[i+2]   = iCont.m_InfoMat(offset+j,offset+j);
01714             targets_var[i]    = iCont.m_InfoMat(offset+j,offset+j);
01715             targets_var[i+1]  = iCont.m_InfoMat(offset+j+1,offset+j+1);
01716             targets_var[i+2]  = iCont.m_InfoMat(offset+j+2,offset+j+2);
01717           }
01718         }
01719           
01720         void FillInfoTargets(unsigned short rob_id, bool use_only_vel, InfoContainer const& iI, InfoContainer & iI_POM)
01721         {
01722           unsigned short pad,i,j,offset;
01723     
01724           if(use_only_vel) pad = 2;
01725           else pad = 3;
01726     
01727           offset = 6*rob_id;
01728     
01729           iI_POM.ClearAll();
01730             
01731             // Pic x vx,y vy,z vz cov mat
01732           for(i = 0; i < 3; i++)
01733             for(j = i; j < 3; j++)
01734               {
01735               iI_POM.m_InfoMat(offset+2*i,offset+2*j) = iI.m_InfoMat(i*pad,j*pad);
01736               iI_POM.m_InfoMat(offset+2*i,offset+2*j+1) = iI.m_InfoMat(i*pad,j*pad+1);
01737               iI_POM.m_InfoMat(offset+2*i+1,offset+2*j+1) = iI.m_InfoMat(i*pad+1,j*pad+1);
01738               }
01739     
01740             // Copy x vx, y vy, z vz
01741           for(i = 0,j = 0; i < 3; i++, j = j+2) {
01742             iI_POM.m_info(offset+j) = iI.m_info(i*pad);
01743             iI_POM.m_info(offset+j+1) = iI.m_info(i*pad+1);
01744           }
01745         }
01746     
01747     
01748           // *** Destroy functions ***
01749         void DestroySensorNodes(RobotSimuNodesGen<SNODE_CLASS> *rob)
01750         {
01751           typename SNodesArray::iterator  it;
01752           for(it = rob->m_nodes.begin(); it != rob->m_nodes.end(); it++) {
01753             delete (*it).snode;
01754             (*it).snode = NULL;
01755           }
01756     
01757           rob->m_nodes.clear();
01758           rob->RemoveAllSensors();
01759         }
01760     
01761         void DestroyAllSensorNodes()
01762         {
01763           typename RobotsArray::iterator  it;
01764           for(it = m_rob_array.begin(); it != m_rob_array.end(); it++) {
01765             DestroySensorNodes((*it));
01766           }
01767         }
01768     
01769         void DestroyRobots()
01770         {
01771           typename RobotsArray::iterator  it;
01772           for(it = m_rob_array.begin(); it != m_rob_array.end(); it++) {
01773             delete(*it);
01774           }
01775           m_rob_array.clear();
01776         }
01777     
01778         void KillNodes(RobotSimuNodesGen<SNODE_CLASS> & rob)
01779         {
01780           typename SNodesArray::iterator it;
01781           for(it = rob.m_nodes.begin(); it != rob.m_nodes.end(); it++)
01782             (dynamic_cast<CANode*>((*it).snode))->KillNode();
01783         }
01784     
01785         void ClearRobotsAccelProfile()
01786         {
01787           typename RobotsArray::iterator  it;
01788           for(it = m_rob_array.begin(); it != m_rob_array.end(); it++) (*it)->ClearProfile();
01789         }
01790     
01791         void ClearAllMeasurements()
01792         {
01793           typename RobotsArray::iterator  it;
01794           for(it = m_rob_array.begin(); it != m_rob_array.end(); it++) {
01795             (*it)->ClearUpMeasurementSet();
01796             (*it)->ClearAllSensorsMeasurements();
01797           }
01798         }
01799       };
01800 
01801 
01802     } // namespace test_nodes
01803 
01804   } // namespace ddfsimu
01805 } // namespace jafar
01806 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Wed Oct 15 2014 00:37:18 for Jafar by doxygen 1.7.6.1
LAAS-CNRS