RemoteKhepera.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 
00030 
00031 
00032 #ifndef __REMOTE_KHEPERA_H_
00033 #define __REMOTE_KHEPERA_H_
00034 
00035 #include <socketUDP.h>
00036 #include <CommonOptions.h>
00037 
00038 #include "remKhepComm.h"
00039 #include "URG04LX.h"
00040 #include "MobileRob.h"
00041 #include "RemoteRangeSens.h"
00042 #include <SsUtils.h>
00043 #include <deque>
00044 
00045 
00046 
00047 #ifdef LINK_ROS
00048  #define CONNECTTOROS
00049 #endif
00050 
00051 
00052 
00053 
00054 // #include <mutex>
00055 
00056 #ifdef CONNECTTOROS
00057   #include <ROSNode.h>
00058 #endif
00059 
00060 namespace MipResources{
00062  /* @{ */
00063  
00065 extern "C" void RKWork(void* p);
00066 
00068 extern "C" void RKClean(void* p);
00069 
00073 class RemoteKheperaPar : public MobileRobPar{
00074  private:
00075  public:
00077   RemoteKheperaPar(){};
00079   ~RemoteKheperaPar();
00080 };
00081 
00082 
00086 class RemoteKheperaVar : public MobileRobVar{
00087   
00088  private:
00089   bool           _lastScan1IsLast;
00090   
00091   pthread_mutex_t     _tStateLogMutex;
00092   
00093   MotionModuleTStateLog  _tStateLog;
00094   
00095  public:
00096 
00097   Scan*          _lastScan1;
00098   Scan*          _lastScan2;
00099 //   Scan _lastScan;
00100   Time           _lastScanTime;
00101   Timer          _scanTimer;
00102   int           _lastScanNum;
00103   int           _lastStateNumber;
00104   
00105   // Variables to check if a message of the same type is already in the list
00106   #ifdef CONNECTTOROS
00107    bool          _alreadyArrivedLaser;
00108 //    bool          _alreadyArrivedTState;
00109   #endif
00110   
00113   bool setLastScan(Scan& newScan);
00114   
00118   bool getLastScan(Scan& scan, Time timeout);
00119   
00120   
00124   bool getOldScan(Scan& scan, Time timeout);
00125   
00126   
00128   RemoteKheperaVar();
00129   
00131   ~RemoteKheperaVar(){};
00132   
00133   pthread_mutex_t* ptStateLogMutex(){
00134    return &_tStateLogMutex;
00135   }
00136   
00138   void lockTStateLog(){
00139    pthread_mutex_lock(&_tStateLogMutex);
00140   }
00141   
00143   void unlockTStateLog(){
00144    pthread_mutex_unlock(&_tStateLogMutex);
00145   }
00146   
00149   MotionModuleTStateLog* ptrTStateLog(){
00150    return &_tStateLog;
00151   }
00152 };
00153 
00157 class RemoteKheperaOptions : public MobileRobOptions{
00158  public:
00159   StringOption* baseName;
00160   StringOption* topicName;
00161   StringOption* nodeName;
00163   RemoteKheperaOptions();
00164   
00167   string getObjectName() const {
00168    return "RemoteKheperaOptions";
00169   }
00170   
00171   OptionGroupsType getGroup();
00172 };
00173 
00174 
00178 class RemoteKhepera : public MobileRob{
00179  private:
00180   static const ResourcePlate _plate = MR_REM_KHP_RES;
00181   
00182   URG04LXPar _urg04lxPar;
00183   
00184   RemoteKheperaVar *_remoteVar; 
00185   // onboard sensors
00186   RemoteRangeSens  *_remoteRangeSens;   
00187   
00188   bool _correctLandingStates;
00189   Pose _stateToCorrect;
00190   Pose _correctedState;
00191   
00192   stringstream _ssWarning;
00193   stringstream _ssDebug;
00194   
00195   Timer _timer;
00196   int _receivedScan ;
00197   
00198   Thread _RRThread;
00199   SenderUDP*  _sender;
00200   ListenerUDP* _listener;
00201   int   _sendPort;
00202   int   _listPort;
00203   string _addr;
00204   bool  _unconnected;
00205 
00207   void _startConnection();
00208   
00210   void _startConnectionROS();
00211   
00213   void _closeConnection();
00214   
00218   void _parse(string &msg,deque<MotionModuleTState>* itListTState=NULL);
00219   
00220   void _sendString(string s);
00221   void _getRangeSensPar();
00222   
00223   EnhancedMutEx count_mutex;
00224   
00225   // ROS adding: Marco Cognetti
00226 //   #if defined USE_ROS && defined FOUND_ROS_LIBS
00227   #ifdef CONNECTTOROS
00228    ROSNode*              _rosNode;
00229    int                 _topicSpecifier;
00230    RemoteKheperaOptions*        _options;
00231    bool                _firstOdomMsg;
00232    Decimal               _lasTimeTStateIn;
00233    Decimal               _tempTimeTStateIn;
00234    int                 _numOdoMsgAdded;
00235    list<MotionModuleTState>::iterator _itPTrTStateLog;
00236    Decimal               _lasTimeLaser;
00237   #endif
00238  public:
00240   RemoteKhepera(int argc, const char* argv[]);
00241   
00243   ~RemoteKhepera();
00244 
00246   ResourcePlate getPlate() const {
00247    return _plate;
00248   }
00249   
00252   Scan scan(){
00253    Scan s;
00254    return s;
00255   };
00256   
00257   /* Scan */
00259   void startScan(unsigned int num);
00260   
00262   void stopScan();
00263   
00264   /* Motion Module */
00266   UnicyclePar* unicyclePar();
00267   
00269   void velCmd(Decimal drive, Decimal turnrate);
00270   
00274   void getVel(Decimal& drive, Decimal& turnrate);
00275   
00277   Pose getPose(void);
00278   
00280   MotionModuleTState getMotionState();
00281   
00283   void setPose(Pose p);
00284   
00286   bool setMotionState(MotionModuleState state, bool setPastTimeStamp=false, Time pastTimeStamp=Time());
00287   
00289   bool storedScan(Scan& s,Time timeout);
00291   bool updatedScan(Scan& s);
00293   bool updatedScan(Scan& s, MotionModuleTState& state);
00294   
00298   bool updatedState(MotionModuleTState& state, Time &timeStamp);
00299   
00302   bool updatedState(MotionModuleTState& state);
00303   
00310   bool updatedLastState(MotionModuleTState& state, Time& timeStamp, const Decimal &delta);
00311   
00318   bool updatedLastState(MotionModuleTState& state, const Decimal &delta);
00319   
00322   void receive();
00323   
00325   void receiveUDP();
00326   
00328   void receiveROS();
00329   
00334   void _syncroTime();
00335   
00338   RangeSensPar* rangeSensPar();
00339 };
00340 
00341  /* @} */
00342  
00343 };// end namespace MipResources{
00344 
00345 #endif
00346 
00347 
00348 
00349 
00350 

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