RemKhepServerROS.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 
00029 
00030 #ifndef __REM_KHEP_SERVER_ROS_H_
00031 #define __REM_KHEP_SERVER_ROS_H_
00032 
00033 // MIP include
00034 #include <IPCommModule.h>
00035 #include <CommonOptions.h>
00036 #include <remKhepComm.h>
00037 #include <MotionModule.h>
00038 #include <KorebotROS.h>
00039 
00040 // ROS include
00041 #include <ROSNode.h>
00042 #include <sensor_msgs/LaserScan.h>
00043 #ifdef ROS_ELECTRIC
00044  #include <gazebo/Joint.hh>
00045 #endif
00046 
00047 #ifdef ROS_FUERTE
00048  #include <physics/Joint.hh>
00049 #endif
00050 
00051 #include <ros/ros.h>
00052 
00053 // Boost include
00054 #include <boost/bind.hpp>
00055 
00056 // MACROS
00057 // this is 22500 but means 25000 :)
00058 // #define SAMPLE_TIME_USEC 49500
00059 #define SAMPLE_TIME_USEC 30000
00060 #define WAIT_TIME_USEC 1000
00061 
00062 // DEBUG FLAGS
00063 #define KHS_COMM_DEBUG 0
00064 #define KHS_DEBUG 0
00065 #define KHS_EXECUTE_DEBUG 0
00066 
00067 using namespace std;
00068 
00069 // DEBUG DEFINE
00070 #define KH3_DEBUG(debLevel,_staticLevel,message) \
00071 { \
00072   if(debLevel <= _staticLevel) { \
00073     stringstream KH3_DEBUG_ss; \
00074     KH3_DEBUG_ss << "\033[01;31m" << "[KH3DEBUG] " << "\033[0m" << message; \
00075     std::cout << KH3_DEBUG_ss.str() << std::endl; \
00076   } \
00077 }
00078 
00079 namespace MipResources{
00080 enum{
00081  RIGHT, 
00082  LEFT,
00083  NUM_WHEELS
00084 };
00085 
00086 enum CommRateTypes {
00087  FIXED,
00088  VARIABLE,
00089  NUM_COMM_RATE_TYPES
00090 };
00091 
00092 struct laserScanStr{
00093   MipBaselib::Time _time;
00094  vector<Decimal> _ranges;
00095 };
00096 
00100 class KheperaServerROSOptions : public Options{
00101  public:
00102   IntOption*   urg04lxActive;
00103   IntOption*   korebotActive;
00104   StringOption* commRateType;
00105   
00106   // Name of the laser topic
00107   StringOption* laserTopicName;
00108   // Name of the topic where the khepera will publish data
00109   StringOption* kh3TopicName;
00110   // Name of the ROS node assigned to the robot
00111   StringOption* kh3NodeName;
00112   
00113   string getObjectName() const{
00114    return "KheperaServerROSOptions";
00115   }
00116   
00117   KheperaServerROSOptions();
00118 };
00119 
00123 class KheperaServerROSPars{
00124  private:
00125   // Name of the robot (base for topic)
00126   string      _robotName;
00127   // Actual level of debug
00128   int       _debugLevel;
00129   // Check if the laser is active or not
00130   int       _urg04lxActive;
00131   // Name of the topic where laser data are publishing (e.g. /base_scan/scan)
00132   string      _laserTopicName;
00133   // Name of the topic where one wants to publish and subscribe data (e.g. /msg)
00134   string     _topicName;
00135   // Name of the ROS node created by the resource ROSNode
00136   string     _nodeName;
00137   // Joints
00138   GAZEBO_JOINT_PTR _joints[2];
00139   // Wheel separation
00140   Decimal     _wheelSep;
00141   // Wheel diameter
00142   Decimal     _wheelDiam;
00143   // Integration method of odometry
00144   string     _intMethodOdo;
00145   // Communication type: it might be set to 'fixed' or 'variable'
00146   string     _commType;
00147   // Maximum torque to be applied to joints
00148   Decimal     _torqueMax;
00149   // Encoder resolution
00150   Decimal     _encRes;
00151 
00152  public:
00154   KheperaServerROSPars();
00155   
00157   KheperaServerROSPars(const string &robotName, const int &debugLevel, const int &urgAct, const string &ltn, const string &tn, const string& nn, const string &imo, GAZEBO_JOINT_PTR jtr, GAZEBO_JOINT_PTR jtl, const Decimal &ws, const Decimal &wd, const string &ct, const Decimal &tm, const Decimal &er);
00158   
00160   KheperaServerROSPars(const KheperaServerROSPars &A);
00161   
00163   KheperaServerROSPars operator= (KheperaServerROSPars A);
00164   
00167   string getRobotName() const;
00168   
00171   int getDebugLevel() const;
00172   
00175   int getUrg04lxActive() const;
00176   
00179   string getLaserTopicName() const;
00180   
00183   string getTopicName() const;
00184   
00187   string getNodeName() const;
00188   
00191   GAZEBO_JOINT_PTR getLeftWheel();
00192   
00195   GAZEBO_JOINT_PTR getRightWheel();
00196   
00199   Decimal getWheelSeparation() const;
00200   
00203   Decimal getWheelDiameter() const;
00204   
00207   string getIntMethodOdo() const;
00208   
00211   string getCommunicationType() const;
00212   
00215   Decimal getMaxTorque() const;
00216   
00219   Decimal getEncoderResolution() const;
00220 };
00221 
00222 
00226 class KheperaServerROS : public MIPObject/*, public gazebo::Controller*/{
00227  
00228  private:
00229   
00230   // In order to emulate the real laser, the first and last 15 degrees are erased
00231   static const int ticksToErase = 44;
00232   
00233   // Resource KorebotROS
00234   KorebotROS*        _korebot;
00235   
00236   KheperaServerROSOptions*  _options;
00237   
00238   bool            _newTState;
00239   bool            _newVelocity;
00240   Decimal          _driveToBeSet;
00241   Decimal          _turnrateToBeSet;
00242   MotionModuleTState     _motionModuleTStateToBeSet;
00243   MotionModuleTState     _lastMotionModuleTState;
00244   
00245   // Copy of the most important parameters
00246   string           _robotName;
00247   int            _debugLevel;
00248   int            _urg04lxActive;
00249   GAZEBO_JOINT_PTR     _leftJoint;
00250   GAZEBO_JOINT_PTR     _rightJoint;
00251   
00252   // Subscriber that take data from topic
00253   ros::Subscriber      _subscriberURG04LXmm;
00254   // The real package to send to ROS (this is the struct data that will be send to robot)
00255   laserScanStr        _laserScan;
00256   
00257   // ROSNode to manage all the ROS connections
00258   ROSNode*         _rosNode;
00259   // Topic specifier. It will be 0 since it is the first topic on the topic list in ROSNode
00260   int            topicSpecifier;
00261   
00262   // It will include:
00263   deque<string>       _incomingPackets;
00264   vector<string>       _outcomingPackets;
00265   
00266   char            _stateBuffer[70];
00267   int            _stateBufferCounter;
00268   int            _numSentState;
00269   SimTimer         _execCmdsTimer;
00270   
00271   // Laser message
00272   char           _laserBuffer[5048];
00273   int            _laserBufferCounter;
00274   int            _numSentScan;
00275   
00276   CommRateTypes       _commRateType;
00277   
00278   pthread_mutex_t      _laserMutex;
00279   
00280   string getObjectName() const {
00281    return "KheperaServerROS";
00282   }
00283   
00284   // Callback of the laser scan subscriber
00285   void laserCallback(const sensor_msgs::LaserScan::ConstPtr& msg);
00286   
00287  public:
00288   
00292   KheperaServerROS(int argc, const char* argv[]);
00293   
00295   KheperaServerROS(KheperaServerROSPars * khPars);
00296   
00298   ~KheperaServerROS();
00299   
00301   bool searchConnection();
00302   
00304   void receiveCommands();
00305   
00308   int parseCommands();
00309   
00311   void executeCommands();
00312   
00315   ROSNode* getRosNode();
00316   
00319   KorebotROS* getKorebot();
00320   
00324   void getVelocityCommand(Decimal &xVel, Decimal &omega);
00325   
00328   void getTStateCommand(MotionModuleTState &state);
00329   
00331   void lockLaserMutex();
00332   
00334   void unlockLaserMutex();
00335 };
00336 
00337 } // End of namespace
00338 
00339 #endif

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