MRXT: The Multi-Robot eXploration Tool
Multi-Robot autonomous exploration and mapping simulator.
include/model/simulatedModel.h
00001 
00002 /*
00003 *
00004 * Author: Miguel Julia <mjulia@umh.es> 
00005 * 
00006 * Date:   2009
00007 * 
00008 * Class virtualWorld
00009 * 
00010 */
00011 
00012 #pragma once
00013 #ifndef __SIMULATED__MODEL__
00014 #define __SIMULATED__MODEL__
00015 
00016 #include <QObject>
00017 #include <QPixmap>
00018 #include <opencv2/opencv.hpp>
00019 #include "worldModelInterface.h"
00020 #include "gridMapInterface.h"
00021 #include "visualMap.h"
00022 
00023 /**
00024 * @brief Feature Struct. Contains the position and descriptor of a landmark
00025 *
00026 */
00027 typedef struct feature{
00028         pos3d pos;
00029         float* desc;
00030         int desclength;
00031         feature():desc(0),desclength(0){};
00032         feature(const pos3d& p, float* d, int l):pos(p),desclength(l){ 
00033                 desc = new float[desclength]; 
00034                 memcpy(desc,d,desclength*sizeof(float)); 
00035         };
00036         feature(const feature& f): pos(f.pos), desclength(f.desclength){
00037                 desc = new float[desclength]; 
00038                 memcpy(desc,f.desc,desclength*sizeof(float));
00039         };
00040         feature& operator=(const feature& f){
00041                 pos = f.pos;
00042                 if (desclength != f.desclength){
00043                         desclength = f.desclength;
00044                         if (desc) delete[] desc;
00045                         desc = new float[desclength]; 
00046                 }
00047                 memcpy(desc,f.desc,desclength*sizeof(float));
00048                 return *this;
00049         };
00050         ~feature(){if (desc) delete[] desc;};
00051 }feature;
00052 
00053 
00054 /**
00055 * @brief  This class implements a virtual world for robots where robots can move and observe the environment
00056 *
00057 */
00058 
00059 class simulatedModel:  public worldModelInterface{
00060 
00061         Q_OBJECT
00062 
00063 signals:
00064         
00065         //void redraw(const QPixmap scenario);
00066         void changedPositions(rposes poses);
00067 
00068 ///////////////////////////////////////////////////////////////////////
00069 //-------------------------  Attributes  ------------------------------
00070 ///////////////////////////////////////////////////////////////////////
00071 
00072 protected:
00073 
00074         void redrawing();
00075 
00076         int nBots;                                      // number of robots
00077         int maxNumRobots;
00078         int step;
00079 
00080         // current sensors data structures
00081         rangeSensorData* rsData;
00082         landmarksData* lmData;
00083         pose* odo;
00084         pose* gt;
00085         pose* lastodo;
00086         pose* lastgt;
00087         robotBase** rbase;
00088         bool* robotsEnabled;
00089 
00090         // actuators values
00091         speed* velo;
00092 
00093         char* logstr;
00094 
00095         float alfa1;                                    /// odometry params
00096         float alfa2;                                    /// odometry params
00097         float alfa3;                                    /// odometry params
00098         float alfa4;                                    /// odometry params
00099         float drifttrans;                               /// odometry params
00100 
00101         // Datos de calibracion
00102 
00103         int imgwidth;
00104         int imgheight;
00105 
00106         float sigma2f; 
00107         float sigmaf; 
00108         float f; 
00109         float Realf;
00110 
00111         float sigma2I; 
00112         float sigmaI; 
00113         float I; 
00114         float RealI;
00115 
00116         float sigma2Cx; 
00117         float sigmaCx; 
00118         float Cx;
00119         float RealCx;
00120 
00121         float sigma2Cy; 
00122         float sigmaCy; 
00123         float Cy;
00124         float RealCy;
00125 
00126         float sigma2Cxp; 
00127         float sigmaCxp; 
00128         float Cxp;
00129         float RealCxp;
00130 
00131         float sigmar; 
00132         float sigmac; 
00133         float sigmacp; 
00134         float sigmad;
00135         float sigma2r; 
00136         float sigma2c; 
00137         float sigma2cp; 
00138         float sigma2d;
00139 
00140         float betaMAX;
00141         float gammaMAX;
00142         float distMAX;
00143         float distMIN;
00144 
00145         double camx;                                            /// camera position
00146         double camy;                                            /// camera position
00147         double camz;                                            /// camera position
00148         double rx;                                              /// camera position
00149         double ry;                                              /// camera position
00150         double rz;                                              /// camera position
00151 
00152         int numSensors;                                         /// sonar parameters
00153         float devError;
00154         float realError;
00155         float gammamax;
00156         float aperture;
00157         float maxDist;
00158         float minDist;
00159 
00160         float sample_time;
00161         float elapsedTime;
00162 
00163         std::vector<double> robot_footprint;                                    
00164         std::vector< std::vector<line> > robots_lines;          /// wall / obstacles
00165 
00166         bool endth;                                             /// stop condition
00167         ClMutex closing;                                        /// syncronization mutex on closing
00168         ClMutex newIdsMutex;
00169         int idCounter;
00170 
00171         // map objects
00172         std::vector<pose> robotPose;                            /// robot positions
00173         std::vector<feature> landmarks;                         /// landmarks
00174         std::vector<line> mapWall;                              /// wall / obstacles
00175         int desclength;
00176 
00177         matrix Scam;                                            /// sensor covariance matrix
00178 
00179         float scalex;
00180         float scaley;
00181         float maxx, maxy, minx, miny;
00182         int width, height;
00183 
00184         bool showsimulation;
00185 
00186 //      bool groupInitialPoses;
00187 //      int maxInitialPoseDist;
00188 //      double minInitialPoseDist;
00189         int numfeat;
00190         bool mapLoaded;
00191 
00192         gridMapInterface* omap;
00193         
00194         char* omapfilename;
00195 
00196 
00197 
00198 //--------------------------------------------------------------------- 
00199 ///////////////////////////////////////////////////////////////////////
00200 
00201 ///////////////////////////////////////////////////////////////////////
00202 //-------------------------  Methods  ---------------------------------
00203 ///////////////////////////////////////////////////////////////////////
00204 public:
00205         /// default constructor
00206         simulatedModel();
00207         /// destructor
00208         virtual ~simulatedModel();
00209         /// constructor
00210         simulatedModel(int nBots, ConfigFile& configfile, float sampletime);
00211         
00212         /// initializes a virtual world with "nBots" robots in the scenario "scene" and initial poses given by "test" 
00213         void initialize(int nagents, ConfigFile& configfile, float sampletime); 
00214 
00215         void reset();
00216 
00217         /// synchronizer
00218         production synchronizer;
00219 
00220         /// load a map file
00221         void loadMapFile(const ConfigFile& config);
00222 
00223         robotBase* createNewPlatform();
00224         void disablePlatform(const robotBase& r);
00225 
00226         /// returns the range sensor current data
00227         rangeSensorData* getRangeSensorData(int robot);
00228         /// returns the landmarks data
00229         landmarksData* getLandmarksData(int robot);
00230         /// returns the robot odometry
00231         pose* getOdometry(int robot);
00232 
00233         const char* getOMapFileName() const;
00234 
00235         /// sets the linear and angular speed for a robot
00236         void setSpeed(int r, float v, float w);
00237         /// sets the name of the log file
00238         void setLogName(const char*);
00239 
00240         int fire(int robot);
00241 
00242         int getNumRobots();
00243         void setNumRobots(int num);
00244 
00245         QPixmap* getPixmap();
00246         float getTime() const;
00247         float getSampleTime() const;
00248         void randomPoses(bool group, double maxInitialPoseDist, double minInitialPoseDist);
00249         
00250         rposes getPoses() const;
00251 
00252         double evaluateVMap(visualMap& vm) const;
00253         
00254 private:
00255 
00256         IplImage* getMapImage();
00257         void drawRobots(IplImage*& img) ;
00258 
00259         /// set up for the execution thread
00260         int setup();                    
00261         /// primary execution loop
00262         void execute(); 
00263         /// called before stopping
00264         void onStop();  
00265         
00266         /// moves the robot with speed vel for time st
00267         void robotMotion(pose &pos, const speed& vel, float st);
00268         /// creates a new odometry reading
00269         void motionSample(const pose& lastPose, const pose& newPose, const pose& lastodo, pose& newodo);
00270         /// captures features from camPose
00271         void simCamAdquisition(const pose& robotpos, landmarksData& lmData, int nr);
00272         /// simulates sonar/laser readings
00273         void simRangeAdquisition(const pose& realPose, rangeSensorData& sonarData, int r); 
00274         /// auxiliary function to test when a feature is visible or not
00275         bool testVisibility(const pos3d& realPose, const pos3d& mark, int nr);
00276         /// returns the distance from ori to dest or the first wall in that direction
00277         float visibilityDistance(const pose& ori, const pose& dest, int nr);
00278 
00279         /// returns the pose of the camera
00280         pose3d camPose(const pose& robotpose);
00281 
00282         /// translates a position in the camera frame of reference to the robot's frame of reference
00283         pos3d cam2base(const pos3d& p);
00284         /// translates a position in the robot's frame of reference to the camera frame of reference
00285         pos3d base2cam(const pos3d& p);
00286 
00287         void update_footprint(int r);
00288 
00289 //--------------------------------------------------------------------- 
00290 ///////////////////////////////////////////////////////////////////////
00291 };
00292 
00293 inline rangeSensorData* simulatedModel::getRangeSensorData(int r)       {return new rangeSensorData(rsData[r]);}
00294 inline landmarksData* simulatedModel::getLandmarksData(int r)           {return new landmarksData(lmData[r]);}
00295 inline pose* simulatedModel::getOdometry(int r)                         {return new pose(odo[r]);}
00296 inline void simulatedModel::setSpeed(int r, float v, float w)           {if (r<nBots){velo[r].v=v;velo[r].w=w;}}
00297 inline int simulatedModel::getNumRobots()                               {return nBots;};
00298 inline const char* simulatedModel::getOMapFileName() const              {return omapfilename;};
00299 inline float simulatedModel::getTime() const                            {return elapsedTime;};
00300 inline float simulatedModel::getSampleTime() const                      {return sample_time;};
00301 #endif
00302 
00303 
00304 
 All Classes Functions Variables Typedefs