ClearingMobileRobot.h

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------------
00002 //
00003 // $Id$
00004 //
00005 // Copyright 2008, 2009, 2010, 2011, 2012  Antonio Franchi and Paolo Stegagno    
00006 //
00007 // This file is part of MIP.
00008 //
00009 // MIP is free software: you can redistribute it and/or modify
00010 // it under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation, either version 3 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // MIP is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with MIP. If not, see <http://www.gnu.org/licenses/>.
00021 //
00022 // Contact info: franchi@diag.uniroma1.it stegagno@diag.uniroma1.it
00023 //
00024 // ----------------------------------------------------------------------------
00025 
00026 #ifndef __CLEARING_MOBILE_ROBOT_
00027 #define __CLEARING_MOBILE_ROBOT_
00028 
00029 
00030 #include <libplayerc++/playerc++.h>
00031 #include <vector>
00032 #include <list>
00033 #include <unistd.h>
00034 #include <math.h>
00035 #include <cstdlib>
00036 #include <assert.h>
00037 #include <iostream>
00038 #include <fstream>
00039 
00040 // MIP
00041 #include <R2.h>
00042 #include <Grid.h>
00043 #include <Scan.h>
00044 #include <MobileRob.h>
00045 #include <PlayerRob.h>
00046 
00047 // #include "args.h"
00048 // #include "utils.h"
00049 
00050 
00051 using namespace PlayerCc;
00052 using namespace std;
00053 using namespace boost;
00054 using namespace MipBaselib;
00055 using namespace MipResources;
00056 
00058 static void _motionWork ( void* p );
00059 
00061 static void _motionClean ( void* p );
00062 
00063 enum BehaviorTypes
00064 {
00065     EXPAND,
00066     GUARD,
00067     FOLLOW,
00068     WANDER,
00069     NO_BEHAVIOR
00070 };
00071 
00072 enum KeepColor
00073 {
00074     KEEP_UNKNOWN,
00075     KEEP_ORANGE,
00076     KEEP_PURPLE
00077 };
00078 
00079 class ClearingMobileRobot;
00080 
00081 typedef uint IdentityCL;
00082 typedef std::vector<Position > PathCl;
00083 // typedef std::vector<ClearingMobileRobot* > AgentVector;
00084 // typedef std::vector<Position > ViewPoints;
00085 
00086 class ClearingAgent;
00087 
00091 typedef std::vector<ClearingAgent> AgentVector;
00092 
00093 
00094 
00098 class ClearingAgent{
00099  Position _pos;
00100  bool _isLeader;
00101  IdentityCL _ID;
00102  BehaviorTypes _behavior;
00103  
00104  
00105  public:
00106   ClearingAgent(const ClearingAgent &a){
00107   }
00108   void operator=(const ClearingAgent& a){
00109   }
00110   /*###############*/
00111   /* Set functions */
00112   /*###############*/
00115   void setID(IdentityCL i){
00116    _ID = i;
00117   }
00118   
00121   void setPosition(Position p){
00122    _pos = p;
00123   }
00124   
00126   void setIsLeader(){
00127    _isLeader = true;
00128   }
00129   
00131   void unsetIsLeader(){
00132    _isLeader = false;
00133   }
00134   
00136   void setBehavior(BehaviorTypes b){
00137    _behavior = b;
00138   }
00139   
00140   /*###############*/
00141   /* Get functions */
00142   /*###############*/
00145   IdentityCL getID(){
00146    return _ID;
00147   }
00148   
00151   Position getPosition(){
00152    return _pos;
00153   }
00154   
00157   bool isLeader(){
00158    return _isLeader;
00159   }
00160   
00162   BehaviorTypes getBehavior(){
00163    return _behavior;
00164   }
00165   
00166   /*#################*/
00167   /* Other functions */
00168   /*#################*/
00171   void goTo(Position pos){
00173   }
00174   
00175   int getNumFollowers(){
00177    return 0;
00178   }
00179   
00180   bool isWaitingForAnyAgent(){
00182    return false;
00183   }
00184   
00185   void setToFollow(){
00186   }
00187   
00188   Position getLSRPosition(){
00189    return Position();
00190   }
00191   
00192   Decimal getSensorRange(){
00193    return 0.0;
00194   }
00195   
00196   Frontier getFrontier(){return Frontier();}
00197   Cell* getCell(DubInt c){return NULL;}
00198   Position getCellPosition(DubInt c){return Position();}
00199   bool isWaitingForAgent(IdentityCL id){return false;}
00200   void clearWaitingForAgent(){}
00201   void setFrontier(Frontier& f){}
00202   void setToWander(){}
00203   void setToExpand(PathCl &pth, AgentVector& agnt){}
00204   void setToNoBehavior(){}
00205   void logData(){}
00206   void addFollower(ClearingAgent& cla){}
00207   void addFollower(ClearingMobileRobot* clmr){}
00208 };
00209 
00210 
00214 class ClearingMobileRobot{
00215   
00216  protected :
00217   
00218   // protected variables
00219   MobileRob* _mobileRob;    
00220   IdentityCL m_iID;        
00221   
00222   Position goal;        
00223   
00224   Thread _motionThread;     
00225   
00226   BehaviorTypes m_eBehavior;
00227   Frontier m_frontier;
00228   Frontier m_freeArcs;
00229   Frontier m_prevFrontier;
00230   Grid m_LSR;
00231   Grid m_prevLSR;
00232   bool m_bFrontierInitialized;
00233   PathCl m_path;
00234   ClearingAgent* m_pWaitForAgent;
00235   AgentVector m_followers;
00236   
00237   // Agent action functions
00238   void Expand( AgentVector neighbors );
00239   void Guard( AgentVector neighbors );
00240   void processFrontier( AgentVector neighbors );
00241   void Wander( AgentVector neighbors );
00242   
00243   void recordLSR();
00244   //bool isCanExpandAlone();
00245   bool isCellCovered( DubInt iViewPoint, DubInt iCell );
00246  
00247  //
00248  // These next functions are the two main functions for the algorithm
00249  //
00250  
00251  // Pick a viewpoint on current frontier to expand
00252  Position pickBestViewPoint(int *iNumViewPoints, bool bDebugDisplay = false);
00253  
00254  // Update frontier after arriving at new viewpoint, involves clearing
00255  // some of neighbors frontier arcs and classifying LSR boundary
00256  void updateFrontier( AgentVector neighbors, bool bDebugDisplay = false );
00257  
00258  
00259  public :
00262   ClearingMobileRobot( IdentityCL ID, int argc, const char* argv[]);
00263   
00266   ClearingMobileRobot( IdentityCL ID, int argc, const char* argv[], MobileRob* rob);
00267   
00269   ~ClearingMobileRobot( );
00270   
00272   IdentityCL getID(){
00273    return m_iID;
00274   }
00275   
00277   BehaviorTypes getBehavior(){
00278    return m_eBehavior;
00279   }
00280   
00282   Grid getLSR(){
00283    return ((m_bFrontierInitialized) ? m_LSR : m_prevLSR);
00284   }
00285   
00286   LRR getLRR(){
00287    return ((m_bFrontierInitialized) ? m_LSR._lrr : m_prevLSR._lrr);
00288   }
00289   
00290   Frontier getFrontier(){
00291    return ((m_bFrontierInitialized) ? m_frontier : m_prevFrontier);
00292   }
00293   
00294   void velCmd(Decimal drive, Decimal turnrate){
00295    _mobileRob->velCmd(drive,turnrate);
00296   }
00297   
00298   Position getGoal(){
00299    return goal;
00300   }
00301   
00302   int getNumFrontierSegments(){
00303    return ((m_bFrontierInitialized) ? m_frontier.size() : m_prevFrontier.size());
00304   }
00305   
00306   int getTotalFrontierCells();
00307   
00308   void setFrontier(Frontier frontier, bool bForce = false );
00309   
00310   PathCl getPathCl(){
00311    return m_path;
00312   }
00313   
00314   Pose getPose(){
00315    return _mobileRob->getPose();;
00316   }
00317   
00318   Position getPosition(){
00319    return getPose().pos();
00320   }
00321   
00322   Position getViewpoint();
00323   
00324   Scan getScan();
00325   
00326   ExplorationParams getExplParams();
00327   
00328   double getSensorRange(){ 
00329 //         cout << "*****************************************" << endl;
00330 //         cout << (_mobileRob->rangeSensPar()->linRangeMax()) << endl;
00331    return (double)(_mobileRob->rangeSensPar()->linRangeMax());
00332   }
00333   
00334   double getCoverRange();
00335   
00336   double getWidth(){
00337    return _mobileRob->robPar()->radius();
00338   }
00339   
00340   int getLSRSize(){
00341    return ((m_bFrontierInitialized) ? m_LSR.columns()*m_LSR.rows() : m_prevLSR.columns()*m_prevLSR.rows());
00342   }
00343   
00344   vector<Cell* > getRaster( DubInt iCell1, DubInt iCell2 );
00345   
00346   double getLSRResolution(){
00347    return ((m_bFrontierInitialized) ? m_LSR.resolution() : m_prevLSR.resolution());
00348   }
00349   
00350   Position getLSRPosition();
00351   
00352   LSRBound getLSRBound(){
00353    return ((m_bFrontierInitialized) ? m_LSR.lsrBound() : m_prevLSR.lsrBound());
00354   }
00355   
00356   Cell* getCell( DubInt indices ){
00357    return ((m_bFrontierInitialized) ? m_LSR.getCell(indices) : m_prevLSR.getCell(indices));
00358   }
00359   
00360   Cell* getCell( Position pos ){
00361    return ((m_bFrontierInitialized) ? m_LSR.getCell(pos) : m_prevLSR.getCell(pos));
00362   }
00363   
00364   Cell* getCell( int i ){
00365    return ((m_bFrontierInitialized) ? m_LSR.getCell(i) : m_prevLSR.getCell(i));
00366   }
00367   
00368   Position getCellPosition( DubInt indices ){
00369    return getCell(indices)->point();
00370   }
00371   
00372   int getNumFollowers(){
00373    return m_followers.size();
00374   }
00375   
00376   Position getFollowerPosition();
00377     
00378     // State change functions
00379     void setToExpand( PathCl path, AgentVector neighbors, bool bDoExpand = true );
00380     void setToGuard( AgentVector neighbors );
00381     void setToFollow( );
00382     void setToWander( );
00383     void setToNoBehavior( );
00384     
00385     void goTo( Position pos );
00386     void addFollower( ClearingAgent* newFollower, bool bMove = true );
00387     
00388     // This function is called whenever agent is active
00389     void doCurrentBehavior( AgentVector neighbors );
00390     
00391     bool isLeader();
00392     bool isReady();
00393     bool isWaitingForAnyAgent();
00394     bool isWaitingForAgent(int iID);
00395     void clearWaitingForAgent();
00396     bool isTraveling();
00397     
00398     void drawBoundary( Graphics2dProxy &gp );
00399     void drawFrontier( Graphics2dProxy &gp );
00400     void drawLSR( Graphics2dProxy &gp, bool bOnlyLSR );
00401     
00402     bool canDetectTargetPosition( Position targetPos );
00403 };
00404 
00405 
00406 #endif    //TEST_FRONTIER_PURSUIT_H

Generated on Mon Feb 20 07:01:07 2017 for MIP by  doxygen 1.5.6