Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

minisim.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_minisim_h_
00003 #define INCLUDED_minisim_h_
00004 
00005 #ifdef TK_ENABLE_CONFIG
00006 #  include "Shared/Config.h"
00007 #endif
00008 
00009 #ifdef TK_ENABLE_EROUTER
00010 #  include "Events/EventRouter.h"
00011 #endif
00012 
00013 #ifdef TK_ENABLE_WIRELESS
00014 #  include "Wireless/Wireless.h"
00015 #  define TK_ENABLE_THREADING
00016 #endif
00017 #ifdef TK_ENABLE_SOUTSERR
00018 #  include "Wireless/Socket.h"
00019 #endif
00020 
00021 #ifdef TK_ENABLE_KINEMATICS
00022 #  include <iostream>
00023 #  include "Shared/Config.h"
00024 #  include "Motion/Kinematics.h"
00025 #endif
00026 
00027 #ifdef TK_ENABLE_THREADING
00028 #  include "IPC/Thread.h"
00029 #endif
00030 
00031 #include <signal.h>
00032 
00033 #define TK_cstr(s) TK_str(s)
00034 #define TK_str(s) #s
00035 
00036 //! provides initialization and teardown of Tekkotsu globals, provide easy access to framework from command line tools or other executables
00037 /*! See the minisim.h file documentation for a list of macros
00038  *  to control which framework features will be initialized for use. */
00039 namespace minisim {
00040   //! call this one time before using framework features
00041   void initialize();
00042   //! call this when done using framework features
00043   void destruct();
00044   
00045   //! a class which relies on its constructor/destructor pair to ensure destruct() is called
00046   /*! This is convenient if your main function has many error returns, or throws exceptions. */
00047   struct AutoScopeInit {
00048     AutoScopeInit() { minisim::initialize(); } //!< constructor calls minisim::initialize()
00049     ~AutoScopeInit() { minisim::destruct(); } //!< destructor calls minisim::destructor();
00050   };
00051   
00052 #ifdef TK_ENABLE_WIRELESS
00053   class WirelessThread : public Thread {
00054   public:
00055     //! constructor
00056     WirelessThread() : Thread() {}
00057     //! destructor -- stop thread
00058     virtual ~WirelessThread() {
00059       if(isStarted()) {
00060         stop();
00061         join();
00062       }
00063     }
00064     virtual unsigned int runloop() {
00065       // initial setup done by wireless's own constructor
00066       wireless->pollTest(NULL); // by passing NULL, we wait indefinitely, so no need to usleep in the Thread code
00067       wireless->pollProcess();
00068       wireless->pollSetup(); // reinitialize for next test
00069       return 0; //no sleep time because pollTest blocks
00070     }
00071     virtual Thread& stop() {
00072       Thread::stop();
00073       wireless->wakeup();
00074       return *this;
00075     }
00076   } * wireless_thread;
00077 #endif
00078   
00079   void initialize() {
00080     signal(SIGPIPE, SIG_IGN);
00081     
00082 #ifdef TK_ENABLE_CONFIG
00083     if(config==NULL) {
00084       ::config = new Config();
00085       ::config->setFileSystemRoot("ms");
00086       std::string configfile=TK_cstr(TK_ENABLE_CONFIG);
00087       if(configfile.size()!=0) {
00088         if(::config->loadFile(configfile.c_str())==0)
00089           std::cerr << std::endl << " *** ERROR: Could not load configuration file " << configfile << " *** " << std::endl << std::endl;
00090       } else {
00091         if(::config->loadFile("config/tekkotsu.xml")==0) {
00092           if(::config->loadFile("config/tekkotsu.cfg")==0)
00093             std::cerr << std::endl << " *** ERROR: Could not load configuration file config/tekkotsu.xml *** " << std::endl << std::endl;
00094           else
00095             std::cerr << "Successfully imported settings from old-format tekkotsu.cfg" << std::endl;
00096         }
00097         if(::config->loadFile("config/sim_ovrd.xml")==0)
00098           if(::config->loadFile("config/sim_ovrd.cfg")!=0)
00099             std::cerr << "Successfully imported settings from old-format simulator.cfg" << std::endl;
00100       }
00101     }
00102 #endif
00103     
00104 #ifdef TK_ENABLE_EROUTER
00105     erouter=new EventRouter;
00106 #endif
00107     
00108 #ifdef TK_ENABLE_THREADING
00109     Thread::initMainThread();
00110 #endif
00111 
00112 #ifdef TK_ENABLE_WIRELESS
00113     wireless_thread = new WirelessThread();
00114     wireless = new Wireless();
00115 #endif
00116 #ifdef TK_ENABLE_SOUTSERR
00117 #  ifdef TK_ENABLE_WIRELESS
00118     sout=wireless->socket(Socket::SOCK_STREAM,Wireless::WIRELESS_DEF_RECV_SIZE,Wireless::WIRELESS_DEF_SEND_SIZE*12);
00119     serr=wireless->socket(Socket::SOCK_STREAM,Wireless::WIRELESS_DEF_RECV_SIZE,Wireless::WIRELESS_DEF_SEND_SIZE*4);
00120     wireless->setDaemon(sout);
00121     wireless->setDaemon(serr);
00122 #  else
00123     sout=new Socket(0);
00124     serr=new Socket(1);
00125 #  endif
00126     serr->setFlushType(Socket::FLUSH_BLOCKING);
00127     sout->setTextForward();
00128     serr->setForward(sout);
00129 #  ifdef TK_ENABLE_WIRELESS
00130 #    ifdef TK_ENABLE_CONFIG
00131     wireless->listen(sout, if(config!=NULL) ? 10000 : config->main.console_port);
00132     wireless->listen(serr, if(config!=NULL) ? 10001 : config->main.stderr_port);
00133 #    else
00134     wireless->listen(sout, 10000 );
00135     wireless->listen(serr, 10001 );
00136 #    endif
00137 #  endif
00138 #else //no sout/serr
00139 #  ifdef TK_ENABLE_WIRELESS //but if wireless is enabled, might as well define them
00140     sout=new Socket(-1);
00141     serr=new Socket(-1);
00142     serr->setFlushType(Socket::FLUSH_BLOCKING);
00143     sout->setTextForward();
00144     serr->setForward(sout);
00145 #  endif
00146 #endif //sout and serr
00147     
00148 #ifdef TK_ENABLE_KINEMATICS
00149     if(config==NULL) {
00150       std::cerr << "ERROR: Kinematics depends on Config -- either define TK_ENABLE_CONFIG,\n"
00151                   "  or manually create a Config, either way providing values for:\n"
00152                   "    motion_config::kinematics - location of the kinematics spec file\n"
00153                   "    motion_config::kinematic_chains - name(s) of chain(s) to load" << std::endl;
00154       exit(1);
00155     }
00156     if(config->motion.kinematics.size()==0 || config->motion.kinematic_chains.size()==0) {
00157       std::cerr << "ERROR: The specified configuration does not provide values for:\n"
00158                   "    motion_config::kinematics - location of the kinematics spec file\n"
00159                   "    motion_config::kinematic_chains - name(s) of chain(s) to load\n"
00160                   "  These values are required for Kinematics initialization." << std::endl;
00161       exit(1);
00162     }
00163 #ifndef TK_ENABLE_SOUTSERR
00164 #  warning If using Kinematics, it is wise to enable sout & serr in case errors are enountered.
00165 #endif
00166     kine = new Kinematics();
00167 #endif
00168     
00169 #ifdef TK_ENABLE_WIRELESS
00170     wireless_thread->start();
00171 #endif
00172   }
00173 
00174   
00175   void destruct() {
00176 #ifdef TK_ENABLE_WIRELESS
00177     wireless_thread->stop().join();
00178     delete wireless_thread;
00179     wireless_thread=NULL;
00180 #endif
00181     
00182 #ifdef TK_ENABLE_KINEMATICS
00183     delete kine;
00184     kine=NULL;
00185 #endif
00186     
00187 
00188 #if defined(TK_ENABLE_WIRELESS) || defined(TK_ENABLE_SOUTSERR)
00189     if(serr!=NULL) {
00190 #  if defined(TK_ENABLE_WIRELESS) && defined(TK_ENABLE_SOUTSERR)
00191       wireless->setDaemon(serr,false);
00192       wireless->close(serr);
00193 #  else
00194       delete serr;
00195 #  endif
00196       serr=NULL;
00197     }
00198     
00199     if(sout!=NULL) {
00200 #  if defined(TK_ENABLE_WIRELESS) && defined(TK_ENABLE_SOUTSERR)
00201       wireless->setDaemon(sout,false);
00202       wireless->close(sout);
00203 #  else
00204       delete sout;
00205 #  endif
00206       sout=NULL;
00207     }
00208 
00209 #  ifdef TK_ENABLE_WIRELESS
00210     delete wireless;
00211     wireless=NULL;
00212 #  endif
00213 #endif
00214     
00215 #ifdef TK_ENABLE_EROUTER
00216     delete erouter;
00217     erouter=NULL;
00218 #endif
00219     
00220 #ifdef TK_ENABLE_CONFIG
00221     delete config;
00222     config=NULL;
00223 #endif
00224   }
00225   
00226 };
00227 
00228 /*! @file
00229  * @brief 
00230  * @author Ethan Tira-Thompson (ejt) (Creator)
00231  */
00232 
00233 #endif

Tekkotsu Hardware Abstraction Layer 5.1CVS
Generated Mon May 9 05:01:38 2016 by Doxygen 1.6.3