00001
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 #define TK_cstr(s) TK_str(s)
00032 #define TK_str(s) #s
00033
00034
00035 namespace minisim {
00036 #ifdef TK_ENABLE_WIRELESS
00037 class WirelessThread : public Thread {
00038 public:
00039
00040 WirelessThread() : Thread() {}
00041
00042 virtual ~WirelessThread() {
00043 if(isStarted()) {
00044 stop();
00045 join();
00046 }
00047 }
00048 virtual unsigned int runloop() {
00049
00050 wireless->pollTest(NULL);
00051 wireless->pollProcess();
00052 wireless->pollSetup();
00053 return 0;
00054 }
00055 virtual void stop() {
00056 Thread::stop();
00057 wireless->wakeup();
00058 }
00059 } wireless_thread;
00060 #endif
00061
00062 void initialize() {
00063 #ifdef TK_ENABLE_CONFIG
00064 if(config==NULL) {
00065 ::config = new Config();
00066 ::config->setFileSystemRoot("ms");
00067 std::string configfile=TK_cstr(TK_ENABLE_CONFIG);
00068 if(configfile.size()!=0) {
00069 if(::config->loadFile(configfile.c_str())==0)
00070 std::cerr << std::endl << " *** ERROR: Could not load configuration file config/tekkotsu.xml *** " << std::endl << std::endl;
00071 } else {
00072 if(::config->loadFile("config/tekkotsu.xml")==0) {
00073 if(::config->loadFile("config/tekkotsu.cfg")==0)
00074 std::cerr << std::endl << " *** ERROR: Could not load configuration file config/tekkotsu.xml *** " << std::endl << std::endl;
00075 else
00076 std::cerr << "Successfully imported settings from old-format tekkotsu.cfg" << std::endl;
00077 }
00078 if(::config->loadFile("config/sim_ovrd.xml")==0)
00079 if(::config->loadFile("config/sim_ovrd.cfg")!=0)
00080 std::cerr << "Successfully imported settings from old-format simulator.cfg" << std::endl;
00081 }
00082 }
00083 #endif
00084
00085 #ifdef TK_ENABLE_EROUTER
00086 erouter=new EventRouter;
00087 #endif
00088
00089 #ifdef TK_ENABLE_THREADING
00090 Thread::initMainThread();
00091 #endif
00092 #ifdef TK_ENABLE_WIRELESS
00093 wireless = new Wireless();
00094 #endif
00095 #ifdef TK_ENABLE_SOUTSERR
00096 # ifdef TK_ENABLE_WIRELESS
00097 sout=wireless->socket(Socket::SOCK_STREAM,Wireless::WIRELESS_DEF_RECV_SIZE,Wireless::WIRELESS_DEF_SEND_SIZE*12);
00098 serr=wireless->socket(Socket::SOCK_STREAM,Wireless::WIRELESS_DEF_RECV_SIZE,Wireless::WIRELESS_DEF_SEND_SIZE*4);
00099 wireless->setDaemon(sout);
00100 wireless->setDaemon(serr);
00101 # else
00102 sout=new Socket(0);
00103 serr=new Socket(1);
00104 # endif
00105 serr->setFlushType(Socket::FLUSH_BLOCKING);
00106 sout->setTextForward();
00107 serr->setForward(sout);
00108 # ifdef TK_ENABLE_WIRELESS
00109 # ifdef TK_ENABLE_CONFIG
00110 wireless->listen(sout, if(config!=NULL) ? 10000 : config->main.console_port);
00111 wireless->listen(serr, if(config!=NULL) ? 10001 : config->main.stderr_port);
00112 # else
00113 wireless->listen(sout, 10000 );
00114 wireless->listen(serr, 10001 );
00115 # endif
00116 # endif
00117 #else //no sout/serr
00118 # ifdef TK_ENABLE_WIRELESS //but if wireless is enabled, might as well define them
00119 sout=new Socket(-1);
00120 serr=new Socket(-1);
00121 serr->setFlushType(Socket::FLUSH_BLOCKING);
00122 sout->setTextForward();
00123 serr->setForward(sout);
00124 # endif
00125 #endif //sout and serr
00126
00127 #ifdef TK_ENABLE_KINEMATICS
00128 if(config==NULL) {
00129 std::cerr << "ERROR: Kinematics depends on Config -- either define TK_ENABLE_CONFIG,\n"
00130 " or manually create a Config, either way providing values for:\n"
00131 " motion_config::kinematics - location of the kinematics spec file\n"
00132 " motion_config::kinematic_chains - name(s) of chain(s) to load" << std::endl;
00133 exit(1);
00134 }
00135 if(config->motion.kinematics.size()==0 || config->motion.kinematic_chains.size()==0) {
00136 std::cerr << "ERROR: The specified configuration does not provide values for:\n"
00137 " motion_config::kinematics - location of the kinematics spec file\n"
00138 " motion_config::kinematic_chains - name(s) of chain(s) to load\n"
00139 " These values are required for Kinematics initialization." << std::endl;
00140 exit(1);
00141 }
00142 #ifndef TK_ENABLE_SOUTSERR
00143 # warning If using Kinematics, it is wise to enable sout & serr in case errors are enountered.
00144 #endif
00145 kine = new Kinematics();
00146 #endif
00147
00148 #ifdef TK_ENABLE_WIRELESS
00149 wireless_thread.start();
00150 #endif
00151 }
00152
00153
00154 void destruct() {
00155 #ifdef TK_ENABLE_WIRELESS
00156 wireless_thread.stop();
00157 wireless_thread.join();
00158 #endif
00159
00160 #ifdef TK_ENABLE_KINEMATICS
00161 delete kine;
00162 #endif
00163
00164
00165 #if defined(TK_ENABLE_WIRELESS) || defined(TK_ENABLE_SOUTSERR)
00166 if(serr!=NULL) {
00167 # if defined(TK_ENABLE_WIRELESS) && defined(TK_ENABLE_SOUTSERR)
00168 wireless->setDaemon(serr,false);
00169 wireless->close(serr);
00170 # else
00171 delete serr;
00172 # endif
00173 serr=NULL;
00174 }
00175
00176 if(sout!=NULL) {
00177 # if defined(TK_ENABLE_WIRELESS) && defined(TK_ENABLE_SOUTSERR)
00178 wireless->setDaemon(sout,false);
00179 wireless->close(sout);
00180 # else
00181 delete sout;
00182 # endif
00183 sout=NULL;
00184 }
00185
00186 # ifdef TK_ENABLE_WIRELESS
00187 delete wireless;
00188 # endif
00189 #endif
00190
00191 #ifdef TK_ENABLE_EROUTER
00192 delete erouter;
00193 #endif
00194
00195 #ifdef TK_ENABLE_CONFIG
00196 delete config;
00197 #endif
00198 }
00199
00200 };
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 #endif