00001 /*!@file Corba/Objects/BotControlServer.C control a robot via corba */ 00002 00003 //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Lior Elazary <lelazary@yahoo.com> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Corba/Objects/BotControlServer.C $ 00035 // $Id: BotControlServer.C 10794 2009-02-08 06:21:09Z itti $ 00036 // 00037 00038 #include <stdlib.h> 00039 #include <time.h> 00040 #include <iostream> 00041 #include <unistd.h> 00042 #include <fcntl.h> 00043 #include <sys/types.h> 00044 #include <sys/stat.h> 00045 #include <signal.h> 00046 #include "Image/ColorOps.H" 00047 #include "Image/MathOps.H" 00048 #include "Image/Image.H" 00049 #include "Image/ImageSet.H" 00050 #include "Image/Pixels.H" 00051 #include "Image/PyramidOps.H" 00052 #include "Image/ShapeOps.H" 00053 #include "Image/Transforms.H" 00054 #include "Image/fancynorm.H" 00055 #include "Util/Assert.H" 00056 #include "Util/Timer.H" 00057 #include "Util/Types.H" 00058 #include "Component/ModelManager.H" 00059 #include "Corba/Objects/BotControlServerSK.hh" 00060 #include "Corba/ImageOrbUtil.H" 00061 #include "Corba/Objects/BotControlServer.H" 00062 #include "Corba/CorbaUtil.H" 00063 00064 CORBA::ORB_var orb; 00065 CosNaming::Name objectName; 00066 00067 bool Debug = false; 00068 00069 //! Signal handler (e.g., for control-C) 00070 void terminate(int s) 00071 { 00072 LERROR("*** INTERRUPT ***"); 00073 unbindObject(orb, "saliency", "BotControllers", objectName); 00074 orb->shutdown(0); 00075 } 00076 00077 void BotControlServer_i::shutdown() { 00078 // Shutdown the ORB 00079 unbindObject(orb, "saliency", "BotControllers", objectName); 00080 orb->shutdown(0); 00081 } 00082 00083 00084 BotControlServer_i::BotControlServer_i(OptionManager &mgr, bool debug) : 00085 BotControl(mgr, "Bot control", "BotControl", BotControl::SIMULATION) 00086 { 00087 } 00088 00089 00090 BotControlServer_i::~BotControlServer_i() { 00091 00092 } 00093 00094 00095 00096 void BotControlServer_i::init(){ 00097 BotControl::init(); 00098 00099 } 00100 00101 00102 float BotControlServer_i::getSpeed(){ 00103 return BotControl::getSpeed(); 00104 } 00105 00106 00107 short BotControlServer_i::setSpeed(const float speed){ 00108 return BotControl::setSpeed(speed); 00109 } 00110 00111 00112 float BotControlServer_i::getSteering(){ 00113 return BotControl::getSteering(); 00114 } 00115 00116 00117 short BotControlServer_i::setSteering(const float steeringPos){ 00118 return BotControl::setSteering(steeringPos); 00119 } 00120 00121 00122 ImageOrb* BotControlServer_i::getImageSensor(const short i){ 00123 00124 Image<PixRGB<byte> > image = BotControl::getImageSensor(i); 00125 00126 return image2Orb(image); 00127 } 00128 00129 00130 void BotControlServer_i::getImageSensorDims(short &w, short &h, const short i) { 00131 00132 BotControl::getImageSensorDims(w, h, i); 00133 } 00134 00135 void BotControlServer_i::setInfo(const char *info, const Point2DOrb& trackLoc, 00136 const Point2DOrb& recLoc){ 00137 BotControl::setInfo(info, Point2D<int>(trackLoc.i, trackLoc.j), Point2D<int>(recLoc.i, recLoc.j)); 00138 } 00139 00140 short BotControlServer_i::getUserInput(Point2DOrb &loc){ 00141 Point2D<int> location; 00142 00143 short key = BotControl::getUserInput(location); 00144 loc.i = location.i; 00145 loc.j = location.j; 00146 00147 return key; 00148 } 00149 00150 //start the class server 00151 int main(int argc, char **argv){ 00152 00153 MYLOGVERB = LOG_INFO; 00154 00155 // Instantiate a ModelManager: 00156 ModelManager manager("BotControlServer Corba Object"); 00157 00158 nub::ref<BotControlServer_i> botControl(new BotControlServer_i(manager, false)); 00159 manager.addSubComponent(botControl); 00160 00161 if (manager.parseCommandLine((const int)argc, (const char**)argv, "", 0, 0) == false) 00162 return(1); 00163 00164 if (manager.debugMode()){ 00165 Debug = true; 00166 } else { 00167 LINFO("Running as a daemon. Set --debug to see any errors."); 00168 //Become a daemon 00169 // fork off the parent process 00170 pid_t pid = fork(); 00171 if (pid < 0) 00172 LFATAL("Can not fork"); 00173 00174 if (pid > 0) 00175 exit(0); //exit the parent process 00176 00177 // Change the file mask 00178 umask(0); 00179 00180 //Create a new system id so that the kernel wont think we are an orphan. 00181 pid_t sid = setsid(); 00182 if (sid < 0) 00183 LFATAL("Can not become independent"); 00184 00185 fclose(stdin); 00186 fclose(stdout); 00187 fclose(stderr); 00188 00189 } 00190 // catch signals and redirect them to terminate for clean exit: 00191 signal(SIGHUP, terminate); signal(SIGINT, terminate); 00192 signal(SIGQUIT, terminate); signal(SIGTERM, terminate); 00193 signal(SIGALRM, terminate); 00194 00195 00196 //Create the object and run in 00197 orb = CORBA::ORB_init(argc, argv); 00198 00199 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA"); 00200 PortableServer::POA_var poa = PortableServer::POA::_narrow(obj); 00201 00202 BotControlServer_i* myobj = botControl.get(); 00203 00204 00205 PortableServer::ObjectId_var objID = poa->activate_object(myobj); 00206 00207 //get a ref string 00208 obj = myobj->_this(); 00209 CORBA::String_var sior(orb->object_to_string(obj)); 00210 std::cerr << "'" << (char*)sior << "'" << "\n"; 00211 00212 if( !bindObjectToName(orb, obj, "saliency", "BotControllers", "BotControlServer", objectName) ) 00213 return 1; 00214 myobj->_remove_ref(); 00215 00216 PortableServer::POAManager_var pman = poa->the_POAManager(); 00217 pman->activate(); 00218 00219 00220 try { 00221 manager.start(); 00222 //run the object untill shutdown or killed 00223 orb->run(); 00224 } catch (...) { 00225 LINFO("Error starting server"); 00226 } 00227 00228 00229 LINFO("Shutting down"); 00230 unbindObject(orb, "saliency", "BotControllers", objectName); 00231 00232 manager.stop(); 00233 00234 return 0; 00235 } 00236