00001
00013 #ifndef SIMULATOR_HPP_
00014 #define SIMULATOR_HPP_
00015
00016 #include "kernel/IdFactory.hpp"
00017 #include "jmath/jblas.hpp"
00018
00019 #include "rtslam/quatTools.hpp"
00020 #include "rtslam/simulatorObjects.hpp"
00021
00022 namespace jafar {
00023 namespace rtslam {
00024 namespace simu {
00025
00026
00030 class AdhocSimulator
00031 {
00032 private:
00033 std::map<size_t,simu::Robot*> robots;
00034 std::map<size_t,simu::Landmark*> landmarks;
00035 IdFactory lmkIdFactory;
00036
00037
00038 protected:
00039 bool getSenGlobPose(size_t robId, size_t senId, jblas::vec7 &senGlobPose, std::map<size_t,simu::Sensor*>::const_iterator &itSen, double t) const
00040 {
00041 std::map<size_t,simu::Robot*>::const_iterator itRob = robots.find(robId);
00042 if (itRob == robots.end()) return false;
00043 itSen = itRob->second->sensors.find(senId);
00044 if (itSen == itRob->second->sensors.end()) return false;
00045
00046 jblas::vec6 robpose_e = itRob->second->getPose(t); std::swap(robpose_e(3), robpose_e(5));
00047 jblas::vec7 robpose_q = quaternion::e2q_frame(robpose_e);
00048 jblas::vec6 senpose_e = itSen->second->getPose(t); std::swap(senpose_e(3), senpose_e(5));
00049 jblas::vec7 senpose_q = quaternion::e2q_frame(senpose_e);
00050 senGlobPose = quaternion::composeFrames(robpose_q, senpose_q);
00051 return true;
00052 }
00053
00054 bool getObservationPose(jblas::vec &obsPose, const jblas::vec7 &senGlobPose, const std::map<size_t,simu::Sensor*>::const_iterator &itSen, size_t lmkId, double t) const
00055 {
00056 std::map<size_t,simu::Landmark*>::const_iterator itLmk = landmarks.find(lmkId);
00057 if (itLmk == landmarks.end()) return false;
00058 std::map<LandmarkAbstract::geometry_t, ObservationModelAbstract*>::const_iterator itMod = itSen->second->obsModels.find(itLmk->second->type);
00059 if (itMod == itSen->second->obsModels.end()) return false;
00060
00061 jblas::vec lmkPose = itLmk->second->getPose(t);
00062 jblas::vec nobs;
00063 itMod->second->project_func(senGlobPose, lmkPose, obsPose, nobs);
00064 return itMod->second->predictVisibility_func(obsPose, nobs);
00065 }
00066
00067 public:
00068 AdhocSimulator(const std::string & configFile);
00069 AdhocSimulator() {}
00070 ~AdhocSimulator()
00071 {
00072 for(std::map<size_t,simu::Robot*>::iterator it = robots.begin(); it != robots.end(); ++it) delete it->second;
00073 for(std::map<size_t,simu::Landmark*>::iterator it = landmarks.begin(); it != landmarks.end(); ++it) delete it->second;
00074 }
00075
00076 void addRobot(simu::Robot *robot) { robots[robot->id] = robot; }
00077 void addSensor(size_t robId, simu::Sensor *sensor)
00078 {
00079 std::map<size_t,simu::Robot*>::iterator it = robots.find(robId);
00080 if (it != robots.end()) return it->second->addSensor(sensor);
00081 }
00082 void addLandmark(simu::Landmark *landmark) { landmark->id = lmkIdFactory.getId(); landmarks[landmark->id] = landmark; }
00083 bool addObservationModel(size_t robId, size_t senId, LandmarkAbstract::geometry_t lmkType, ObservationModelAbstract *obsModel)
00084 {
00085 std::map<size_t,simu::Robot*>::iterator itRob = robots.find(robId);
00086 if (itRob == robots.end()) return false;
00087 std::map<size_t,simu::Sensor*>::const_iterator itSen = itRob->second->sensors.find(senId);
00088 if (itSen == itRob->second->sensors.end()) return false;
00089 itSen->second->addObservationModel(lmkType, obsModel);
00090 return true;
00091 }
00092
00093 bool hasEnded(size_t robId, size_t senId, double t) const
00094 {
00095 std::map<size_t,simu::Robot*>::const_iterator itRob = robots.find(robId);
00096 if (itRob == robots.end()) return true;
00097 std::map<size_t,simu::Sensor*>::const_iterator itSen = itRob->second->sensors.find(senId);
00098 if (itSen == itRob->second->sensors.end()) return true;
00099 return (itRob->second->hasEnded(t) && itSen->second->hasEnded(t));
00100 }
00101
00102 jblas::vec getRobotPose(size_t id, double t) const
00103 {
00104 std::map<size_t,simu::Robot*>::const_iterator it = robots.find(id);
00105 if (it != robots.end()) return it->second->getPose(t); else return jblas::vec();
00106 }
00107
00111 jblas::vec getRobotInertial(size_t id, double t) const
00112 {
00113 std::map<size_t,simu::Robot*>::const_iterator it = robots.find(id);
00114 if (it != robots.end())
00115 {
00116 jblas::vec imu(6);
00117 ublas::subrange(imu, 0, 3) = ublas::subrange(it->second->getAcc(t), 0, 3);
00118 ublas::subrange(imu, 3, 6) = ublas::subrange(it->second->getSpeed(t), 3, 6);
00119 return imu;
00120 }
00121 else return jblas::vec();
00122 }
00123
00124 jblas::vec getSensorPose(size_t robId, size_t senId, double t) const
00125 {
00126 std::map<size_t,simu::Robot*>::const_iterator itRob = robots.find(robId);
00127 if (itRob == robots.end()) return jblas::vec();
00128 return itRob->second->getSensorPose(senId, t);
00129 }
00130
00131 jblas::vec getLandmarkPose(size_t id, double t) const
00132 {
00133 std::map<size_t,simu::Landmark*>::const_iterator it = landmarks.find(id);
00134 if (it != landmarks.end()) return it->second->getPose(t); else return jblas::vec();
00135 }
00136 LandmarkAbstract::geometry_t getLandmarkType(size_t id) const
00137 {
00138 std::map<size_t,simu::Landmark*>::const_iterator it = landmarks.find(id);
00139 if (it != landmarks.end()) return it->second->type; else return (LandmarkAbstract::geometry_t)-1;
00140 }
00141
00142
00143 bool getObservationPose(jblas::vec &obsPose, size_t robId, size_t senId, size_t lmkId, double t) const
00144 {
00145 std::map<size_t,simu::Sensor*>::const_iterator itSen;
00146 jblas::vec7 senGlobPose;
00147 if (!getSenGlobPose(robId, senId, senGlobPose, itSen, t)) return false;
00148 return getObservationPose(obsPose, senGlobPose, itSen, lmkId, t);
00149 }
00150
00151
00152 raw_ptr_t getRaw(size_t robId, size_t senId, double t)
00153 {
00154 boost::shared_ptr<simu::RawSimu> raw(new simu::RawSimu());
00155 raw->timestamp = t;
00156
00157 std::map<size_t,simu::Sensor*>::const_iterator itSen;
00158 jblas::vec7 senGlobPose;
00159 if (!getSenGlobPose(robId, senId, senGlobPose, itSen, t)) return raw;
00160
00161 jblas::vec pose;
00162 for(std::map<size_t,simu::Landmark*>::const_iterator it = landmarks.begin(); it != landmarks.end(); ++it)
00163 {
00164 size_t lmkId = it->second->id;
00165 if (getObservationPose(pose, senGlobPose, itSen, lmkId, t))
00166 raw->obs[lmkId] = featuresimu_ptr_t(new FeatureSimu(pose, it->second->type, lmkId));
00167 }
00168 std::cout << "simulation has generated a raw at time " << t << " with " << raw->obs.size() << " obs ; robot pose " << robots[1]->getPose(t) << std::endl;
00169 return raw;
00170 }
00171 };
00172
00173
00174 }}}
00175
00176 #endif
00177