MobileRob.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: antonio.franchi@tuebingen.mpg.de stegagno@diag.uniroma1.it
00023 //
00024 // ----------------------------------------------------------------------------
00025 
00026 
00033 
00037 
00038 #ifndef __MOB_ROB_H_
00039 #define __MOB_ROB_H_
00040 
00041 #ifdef MIP_HOST_APPLE
00042 #include <applePatch.h>
00043 #endif
00044 
00045 #include <stdlib.h>
00046 #include <string>
00047 #include <sstream>
00048 #include <vector>
00049 #include <assert.h>
00050 
00051 #include <CommonOptions.h>
00052 
00053 #include <baselib.h>
00054 #include <RangeSens.h>
00055 #include <GroundSens.h>
00056 #include <MotionModule.h>
00057 #include <PositionDetector.h>
00058 
00059 #include <Resource.h>
00060 
00061 
00062 namespace MipResources{
00064  /* @{ */
00065  
00066  
00068  static void _mobileRobMotionWork ( void* p );
00069  
00071  static void _mobileRobMotionClean ( void* p );
00072 
00073 
00077 class MobileRobOptions : public Options {
00078  public:
00079   IntOption* motionModule;
00080   IntOption* laser;
00081   IntOption* infrared;
00082   IntOption* sonar;
00083   IntOption* gps;
00084   IntOption* odoLoc;
00085   PoseOption *initialGoal;
00086   
00087   MobileRobOptions();
00088   string getObjectName() const {
00089    return "MobileRobOptions";
00090   }
00091   OptionGroupsType getGroup();
00092 };
00093 
00094 
00098 class MobileRobPar{
00099  private:
00100   int      _id;     
00101   Decimal  _radius; 
00102   string  _name;   
00104  protected:
00107   void setId(int i){
00108    assert(i>0);
00109    _id=i;
00110   }
00113   void setRadius(Decimal r){
00114    assert(r>0.0);
00115    _radius=r;
00116   }
00118   void setName(string n){
00119    _name=n;
00120   }
00121  public:
00123   MobileRobPar (){
00124    _id   = 0;
00125    _radius = 0.0;
00126    _name  = string();
00127   }
00129   MobileRobPar(const MobileRobPar &r){
00130    _id   = r._id;
00131    _radius = r._radius;
00132    _name  = r._name;
00133   }
00135   MobileRobPar& operator=(const MobileRobPar& r){
00136    if (this != &r){
00137     _id   = r._id;
00138     _radius = r._radius;
00139     _name  = r._name;
00140    }
00141    return *this;
00142   }
00144   int id(){
00145    return _id;
00146   }
00148   Decimal radius(){
00149    return _radius;
00150   }
00152   string name(){
00153    return _name;
00154   }
00156   string print(){
00157    stringstream s;
00158    s << "name " << name() << endl
00159     << "id " << _id << endl
00160     << "radius " << _radius << endl;
00161    return s.str();
00162   }
00163 };
00164 
00165 
00169 class MobileRobVar{
00170  protected:
00171   Pose   _pose;  
00172   Pose   _goal;  
00173   Position _goalVel; 
00175  public:
00177   MobileRobVar (){
00178   }
00180   MobileRobVar(const MobileRobVar &v){
00181    _goal = v._goal;
00182    _goalVel = v._goalVel;
00183   }
00185   MobileRobVar& operator=(const MobileRobVar& v){
00186    if (this != &v){
00187     _goal = v._goal;
00188     _goalVel = v._goalVel;
00189     }
00190    return *this;
00191   }
00193   void setPose(Pose p){
00194    _pose=p;
00195   }
00197   Pose pose(){
00198    return _pose;
00199   }
00201   void setGoal(Pose g){
00202    _goal=g;
00203   }
00205   Pose goal(){
00206    return _goal;
00207   }
00209   void setGoalVel(Position gv){
00210    _goalVel=gv;
00211   }
00213   Position goalVel(){
00214    return _goalVel;
00215   }
00217   string print(){
00218    stringstream s;
00219    s << "pose " << _pose.print() << "goal " << _goal.print() << ", goalVel " << _goalVel.print();
00220    return s.str();
00221   }
00222 };
00223 
00224 
00229 class MobileRob : public Resource{
00230  protected:
00231   MobileRobOptions options; 
00233   MobileRobPar   *_par;  
00234   MobileRobVar   *_var; 
00236   // onboard sensors and actuators
00237   RangeSens     *_rangeSens;     
00238   Unicycle     *_unicycle;      
00239   RangeSens     *_infrared;      
00240   GroundSens     *_groundInfrared;   
00241   RangeSens     *_sonar;       
00242   PositionDetector *_gps;        
00244   ScanTypes rangeSensorType;  
00245   ScanTypes infraredType;   
00247   bool rangeSensActive;  
00248   bool infraredActive;  
00249   bool unicycleActive;  
00250   bool sonarActive;    
00251   bool gpsActive;     
00253   bool useOdometryLocalization; 
00255   bool goalIsSet;
00256   
00257   Thread _motionThread;
00258   bool _motionThreadActive;
00259   bool _motionThreadPause;
00260   
00261   ofstream *outputFile;
00262   
00263   
00264  public:
00267   MobileRob(){
00268   }
00269   
00272   MobileRob(int argc, const char* argv[]);
00273   
00276   virtual MobileRobPar* robPar(){
00277    return _par;
00278   }
00279   
00282   virtual MobileRobVar* robVar(){
00283    return _var;
00284   }
00285   
00287   void activateMotionThread();
00288   
00290   void pauseMotionThread();
00291 
00293   void unpauseMotionThread();
00294 
00296   bool isPauseMotionThread();
00297   
00298   /* ### ### range scan ### ### */
00299   
00302   virtual RangeSensPar* rangeSensPar(){
00303    if (rangeSensActive){
00304     return _rangeSens->par();
00305    } else {
00306     cout << "MIP-WARNING (MobileRob): request for range sensor parameters that is not available." << endl;
00307     return NULL;
00308    }
00309   }
00310   
00311     
00314   virtual RangeSensmmPar* rangeSensmmPar(){
00315    if (rangeSensActive) {
00316     return _rangeSens->parmm();
00317    } else {
00318     warning("Request for RangeSensmmPar while RangeSens is not active.");
00319     return NULL;
00320    }
00321   }
00322   
00327   virtual bool updatedScan(Scan& s);
00328   
00334   virtual bool updatedScan(Scan& s,MotionModuleTState& state);
00335   
00339   virtual bool updatedState(MotionModuleTState& state, Time& timeStamp);
00340   
00343   virtual bool updatedState(MotionModuleTState& state);
00344   
00351   virtual bool updatedLastState(MotionModuleTState& state, Time& timeStamp, const Decimal &delta);
00352   
00358   virtual bool updatedLastState(MotionModuleTState& state, const Decimal &delta);
00359   
00363   virtual void startScan(unsigned int num){
00364    if (rangeSensActive) {
00365     _rangeSens->startScan(num);
00366    } else {
00367     warning("Request for startScan while RangeSens is not active.");
00368    }
00369   }
00370   
00373   virtual void stopScan(){
00374    if (rangeSensActive) {
00375     _rangeSens->stopScan();
00376    } else {
00377     warning("Request for stopScan while RangeSens is not active.");
00378    }
00379   }
00380   
00386   virtual bool storedScan(Scan& s,Time timeout){
00387    if (rangeSensActive) {
00388     return _rangeSens->storedScan(s,timeout);
00389    } else {
00390     warning("Request for storedScan while RangeSens is not active.");
00391     return false;
00392    }
00393   }
00394  
00395   /* ### ### unycycle ### ### */
00396   
00399   virtual UnicyclePar* unicyclePar(){
00400    return (UnicyclePar*) _unicycle->getPar();
00401   }
00402   
00406   virtual void velCmd(Decimal drive, Decimal turnrate){
00407    
00408    if (commonOptions->saveFiles->getValue()) {
00409     Time nowTime;
00410     (*outputFile) << nowTime.sec() << " " << nowTime.usec() << " " << drive << " " << turnrate << endl;
00411    }
00412    
00413    _unicycle->setCommands(drive, turnrate);
00414   }
00415   
00420   virtual void getVel(Decimal& linVel, Decimal& angVel){
00421    MotionModuleTState state = _unicycle->getState();
00422    linVel = state.linVel;
00423    angVel = state.angVel;
00424   }
00425   
00429   virtual Pose getPose(){
00430    return _unicycle->getState().pose;
00431   }
00432   
00435   virtual  MotionModuleTState getMotionState(){
00436    return _unicycle->getState();
00437   }
00438   
00442   void getCurvature(Decimal &c,bool &infinity){
00443    return _unicycle->getCurvature(c,infinity);
00444   }
00445   
00446 
00447   
00458   virtual bool setMotionState(MotionModuleState state, bool setPastTimeStamp=false, Time pastTimeStamp=Time()){
00459    _unicycle->setState(state,setPastTimeStamp,pastTimeStamp);
00460   }
00461   
00462   
00467   virtual void setPose(Pose p){
00468    MotionModuleTState state(p,0.0,0.0);
00469    _unicycle->setState(state);
00470   }
00471   
00472 
00473   /* ### ### Range infrared ### ### */
00474 
00479   virtual bool updatedIRScan(Scan& scan){
00480    if (infraredActive){
00481     _infrared->updatedScan(scan);
00482     return true;
00483    }
00484    return false;
00485   }
00486   
00487   /* ### ### Ground infrared ### ### */
00488 
00493   virtual bool updatedIRGroundScan(vector<int>& s){
00494    if (infraredActive){
00495     _groundInfrared->updatedScan(s);
00496     return true;
00497    }
00498    return false;
00499   }
00500   
00501   /* ### ### sonar ### ### */
00506   virtual bool getSonarMeasures(Scan &scan){
00507    if (sonarActive){
00508     _sonar->updatedScan(scan);
00509     return true;
00510    }
00511    return false;
00512   }
00513   
00514   /* ### ### gps ### ### */
00515   
00519   virtual bool getGPSMeasures(Position pos){
00520    if (gpsActive){
00521     pos = _gps->getMeasure();
00522     return true;
00523    }
00524    return false;
00525   }
00526   
00527   /* ### ### MobileRob ### ### */
00528   
00530   virtual void setGoal(Pose g){
00531    _var->setGoal(g);
00532    goalIsSet = true;
00533   }
00534   
00536   Pose getGoal(){
00537    if(goalIsSet){
00538     return _var->goal();
00539    }else{
00540     _var->setGoal(options.initialGoal->getValue());
00541     goalIsSet = true;
00542    }
00543   }
00544   
00546   void setGoalVel(Position gv);
00547   
00549   Position goalVel();
00550   
00552   bool getRangeSensActive();
00553 
00555                 void setRangeSensActive(bool set);
00556   
00558   bool getInfraredActive();
00559   
00561   bool getUnicycleActive();
00562   
00564   bool getSonarActive();
00565   
00567   bool getGpsActive();
00568   
00569   
00570   bool getRangeSensorType(ScanTypes& type){
00571    if (rangeSensActive) {
00572     type = rangeSensorType;
00573     return true;
00574    }else{
00575     warning("Request for getRangeSensorType while RangeSens is not active.");
00576     return false;
00577    }
00578   }
00579   
00580   /*Delete these unuseful methods*/
00581   
00583   virtual int totScans(int lastTakenScan){
00584    return lastTakenScan+1;
00585   }
00586   
00587 
00588   
00589   virtual void sendString(string s){}
00590   
00591   virtual void turnLaserOn(){}
00592   
00593   /*End Delete these unuseful methods*/
00594   
00595 };
00596 
00597  /* @} */
00598  
00599 };// end namespace MipResources{
00600 
00601 
00602 #endif
00603 
00604 
00605 
00606 
00607 

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