BotControlServer.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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
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
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
00151 int main(int argc, char **argv){
00152
00153 MYLOGVERB = LOG_INFO;
00154
00155
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
00169
00170 pid_t pid = fork();
00171 if (pid < 0)
00172 LFATAL("Can not fork");
00173
00174 if (pid > 0)
00175 exit(0);
00176
00177
00178 umask(0);
00179
00180
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
00191 signal(SIGHUP, terminate); signal(SIGINT, terminate);
00192 signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00193 signal(SIGALRM, terminate);
00194
00195
00196
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
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
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