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 <SalGlasses.hh>
00047 #include "corbaUtil.h"
00048 #include "SalGlassesServer.h"
00049 #include "capture.h"
00050 #include "serial.h"
00051
00052 #define OBJECT_NS "saliency"
00053 #define OBJECT_NAME "SalGlasses"
00054 CORBA::ORB_var orb;
00055 CosNaming::Name objectName;
00056 int serialFd = -1;
00057
00058
00059
00060 static void* malloc_thunk(env_size_t n)
00061 {
00062 return malloc(n);
00063 }
00064
00065
00066
00067 static void assert_handler(const char* what, int custom_msg,
00068 const char* where, int line_no)
00069 __attribute__((noreturn));
00070
00071 static void assert_handler(const char* what, int custom_msg,
00072 const char* where, int line_no)
00073 {
00074 if (custom_msg)
00075 fprintf(stderr, "Assertion failed (%s:%d):\n\t%s\n\n",
00076 where, line_no, what);
00077 else
00078 fprintf(stderr, "Assertion failed (%s:%d):\n\texpected '%s'\n\n",
00079 where, line_no, what);
00080 abort();
00081 }
00082
00083
00084 struct status_data
00085 {
00086 int frame_number;
00087 };
00088
00089 static void print_chan_status(void* userdata,
00090 const char* tagName,
00091 const struct env_image* img)
00092 {
00093 struct status_data* p = (struct status_data*) userdata;
00094
00095 if (env_img_initialized(img))
00096 {
00097 intg32 mi, ma;
00098 env_c_get_min_max(env_img_pixels(img), env_img_size(img),
00099 &mi, &ma);
00100 fprintf(stderr,
00101 "frame %06d channel status: "
00102 "%20s: range [%ld .. %ld]\n",
00103 p->frame_number, tagName, (long) mi, (long) ma);
00104 }
00105 }
00106
00107
00108 void terminate(int s)
00109 {
00110 printf("*** INTERRUPT ***\n");
00111 unbindObject(orb, OBJECT_NS, OBJECT_NAME, objectName);
00112 orb->shutdown(0);
00113 }
00114
00115 void SalGlassesServer::shutdown() {
00116
00117 unbindObject(orb, OBJECT_NS, OBJECT_NAME, objectName);
00118 orb->shutdown(0);
00119 }
00120
00121
00122 SalGlassesServer::SalGlassesServer(int debug) :
00123 currentSpeed(0),
00124 currentSteering(0),
00125 itsDebug(debug)
00126 {
00127
00128
00129
00130
00131
00132 env_params_set_defaults(&itsEnvp);
00133
00134 itsEnvp.maxnorm_type = ENV_VCXNORM_MAXNORM;
00135 itsEnvp.scale_bits = 16;
00136 env_assert_set_handler(&assert_handler);
00137 env_allocation_init(&malloc_thunk, &free);
00138
00139 env_visual_cortex_init(&itsIvc, &itsEnvp);
00140
00141
00142 }
00143
00144
00145 SalGlassesServer::~SalGlassesServer() {
00146
00147
00148 env_visual_cortex_destroy(&itsIvc);
00149 env_allocation_cleanup();
00150 }
00151
00152
00153
00154 void SalGlassesServer::init(){
00155 }
00156
00157
00158 void SalGlassesServer::getImageSensorDims(short &w, short &h, const short i) {
00159 w = 10; h = 10;
00160 }
00161
00162 ImageOrb* SalGlassesServer::getImageSensor(const short i){
00163
00164
00165 frame* f = get_frame();
00166 int size = f->width*f->height*3;
00167
00168 ImageOrb *imageOrb = new ImageOrb;
00169 imageOrb->width = f->width;
00170 imageOrb->height = f->height;
00171 imageOrb->pix_size = 3;
00172
00173 CORBA::Octet *dat = new CORBA::Octet[size];
00174
00175 memcpy(dat, f->data, size);
00176 imageOrb->data.replace(size, size, dat, 1);
00177
00178 return imageOrb;
00179 }
00180
00181 ImageOrb* SalGlassesServer::getSaliencyMap(const short i){
00182
00183
00184 frame* f = get_frame();
00185 int size = f->width*f->height*3;
00186
00187 struct env_image ivcout = env_img_initializer;
00188 struct env_image intens = env_img_initializer;
00189 struct env_image color = env_img_initializer;
00190 struct env_image ori = env_img_initializer;
00191 #ifdef ENV_WITH_DYNAMIC_CHANNELS
00192 struct env_image flicker = env_img_initializer;
00193 struct env_image motion = env_img_initializer;
00194 #endif
00195
00196 struct status_data userdata;
00197 userdata.frame_number = 0;
00198
00199 struct env_dims indims;
00200 struct env_rgb_pixel *input;
00201 input = (env_rgb_pixel*)f->data;
00202 indims.w = f->width;
00203 indims.h = f->height;
00204
00205 env_mt_visual_cortex_input(0,
00206 &itsIvc, &itsEnvp,
00207 "visualcortex",
00208 input, 0, indims,
00209 &print_chan_status,
00210 &userdata,
00211 &ivcout,
00212 &intens, &color, &ori
00213 #ifdef ENV_WITH_DYNAMIC_CHANNELS
00214 , &flicker, &motion
00215 #endif
00216 );
00217
00218 env_visual_cortex_rescale_ranges(
00219 &ivcout, &intens, &color, &ori
00220 #ifdef ENV_WITH_DYNAMIC_CHANNELS
00221 , &flicker, &motion
00222 #endif
00223 );
00224
00225
00226
00227 ImageOrb *imageOrb = new ImageOrb;
00228 imageOrb->width = ivcout.dims.w;
00229 imageOrb->height = ivcout.dims.h;
00230 imageOrb->pix_size = 1;
00231
00232 CORBA::Octet *dat = new CORBA::Octet[size];
00233
00234 memcpy(dat, f->data, size);
00235 imageOrb->data.replace(size, size, dat, 1);
00236
00237 return imageOrb;
00238 }
00239
00240
00241 void SalGlassesServer::getWinner(short &x, short &y) {
00242 x = 10; y = 10;
00243 }
00244
00245 short SalGlassesServer::getSensorValue(const short i) {
00246 return -1;
00247 }
00248
00249
00250
00251
00252 int main(int argc, char **argv){
00253
00254 printf("Starting server\n");
00255
00256 try {
00257
00258 orb = CORBA::ORB_init(argc, argv);
00259
00260 if (true)
00261 {
00262 printf("Running as a daemon\n");
00263
00264
00265 pid_t pid = fork();
00266 if (pid < 0)
00267 {
00268 printf("Can not fork\n");
00269 exit(1);
00270 }
00271
00272 if (pid > 0)
00273 exit(0);
00274
00275
00276 umask(0);
00277
00278
00279 pid_t sid = setsid();
00280 if (sid < 0)
00281 {
00282 printf("Can not become independent\n");
00283 exit(1);
00284 }
00285
00286
00287
00288
00289
00290 }
00291
00292 signal(SIGHUP, terminate); signal(SIGINT, terminate);
00293 signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00294 signal(SIGALRM, terminate);
00295
00296
00297 open_device();
00298 init_device(0);
00299 start_capturing();
00300
00301
00302
00303
00304
00305
00306 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
00307 PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
00308
00309 SalGlassesServer* bcs = new SalGlassesServer(0);
00310
00311 PortableServer::ObjectId_var bcsID = poa->activate_object(bcs);
00312
00313
00314 obj = bcs->_this();
00315 CORBA::String_var sior(orb->object_to_string(obj));
00316 std::cerr << "'" << (char*)sior << "'" << "\n";
00317
00318 if( !bindObjectToName(orb, obj, OBJECT_NS, OBJECT_NAME, OBJECT_NAME, objectName) )
00319 return 1;
00320
00321 bcs->_remove_ref();
00322
00323 PortableServer::POAManager_var pman = poa->the_POAManager();
00324 pman->activate();
00325
00326 orb->run();
00327 }
00328
00329 catch(CORBA::SystemException& ex) {
00330 printf("Caught CORBA:: %s\n", ex._name());
00331 }
00332 catch(CORBA::Exception& ex) {
00333 printf("Caught CORBA::Exception: %s\n",ex._name());
00334 }
00335 catch(omniORB::fatalException& fe) {
00336 printf("Caught omniORB::fatalException:\n");
00337 printf(" file: \n", fe.file());
00338 printf(" line: \n", fe.line());
00339 printf(" mesg: \n", fe.errmsg());
00340 }
00341
00342 printf("Shutting down\n");
00343 stop_capturing();
00344 uninit_device();
00345 close_device();
00346 unbindObject(orb, OBJECT_NS, OBJECT_NAME, objectName);
00347
00348 return 0;
00349
00350 }
00351