Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

Config.cc

Go to the documentation of this file.
00001 #include "Shared/Config.h"
00002 #include "Shared/ProjectInterface.h"
00003 #include "Behaviors/Mon/RawCamBehavior.h"
00004 #include "Vision/RawCameraGenerator.h"
00005 #include "Vision/SegmentedColorGenerator.h"
00006 #include <stdio.h>
00007 #include <string>
00008 #include <cstring>
00009 #include <ctype.h>
00010 #include "Wireless/Socket.h"
00011 #ifdef PLATFORM_APERIOS
00012 #  include <OPENR/OPrimitiveControl.h>
00013 #  include <OPENR/OPENRAPI.h>
00014 #else
00015 #include <sys/param.h>
00016 #include <unistd.h>
00017 typedef unsigned int OSpeakerVolume;
00018 const OSpeakerVolume ospkvolinfdB = 0x8000;
00019 const OSpeakerVolume ospkvol25dB  = 0xe700;
00020 const OSpeakerVolume ospkvol18dB  = 0xee00;
00021 const OSpeakerVolume ospkvol10dB  = 0xf600;
00022 #endif
00023 
00024 using namespace std;
00025 
00026 Config* config=NULL;
00027 
00028 void Config::setFileSystemRoot(const std::string& fsr) {
00029 #ifdef PLATFORM_APERIOS
00030   if(fsr[0]=='/')
00031     fsRoot="/ms"+fsr;
00032   else
00033     fsRoot="/ms/"+fsr;
00034 #else
00035   char buf[MAXPATHLEN+2];
00036   if(getcwd(buf,MAXPATHLEN+2)==NULL)
00037     perror("Config::setFileSystemRoot(): getcwd");
00038   buf[MAXPATHLEN+1]='\0';
00039   std::string sbuf(buf);
00040   if(sbuf[sbuf.size()-1]!='/' && fsr[0]!='/')
00041     fsRoot=sbuf+'/'+fsr;
00042   else if(sbuf[sbuf.size()-1]=='/' && fsr[0]=='/')
00043     fsRoot=sbuf+fsr.substr(1);
00044   else
00045     fsRoot=sbuf+fsr;
00046 #endif
00047 }
00048 
00049 
00050 std::string Config::portPath(const std::string& path) const {
00051   if(fsRoot.size()==0)
00052     return path;
00053   if(path.size()==0)
00054     return fsRoot;
00055   if(path.substr(0,fsRoot.size())==fsRoot)
00056     return path;
00057   else if(fsRoot[fsRoot.size()-1]=='/') {
00058     if(path[0]!='/')
00059       return fsRoot+path;
00060     else
00061       return fsRoot+path.substr(1);
00062   } else {
00063     if(path[0]!='/')
00064       return fsRoot+'/'+path;
00065     else
00066       return fsRoot+path;
00067   }
00068 }
00069 
00070 void* Config::setValue(section_t section, const char *key, const char *value, bool /*updated*/) {
00071   switch (section) {
00072     case sec_wireless:
00073       if (strncasecmp(key,"id",29)==0) {
00074         wireless.id=atoi(value);
00075         return &wireless.id;
00076       }
00077       break;
00078     case sec_vision:
00079       if (strncasecmp(key,"white_balance",29)==0) {
00080         if (strncasecmp(value,"indoor",49)==0) {
00081           vision.white_balance=1;
00082         } else if (strncasecmp(value,"flourescent",49)==0) {
00083           std::cout << "Your tekkotsu.cfg file uses deprecated 'flourescent' (misspelled) -- use 'fluorescent' instead" << std::endl;
00084           vision.white_balance=3;
00085         } else if (strncasecmp(value,"fluorescent",49)==0) {
00086           vision.white_balance=3;
00087         } else if (strncasecmp(value,"outdoor",49)==0) {
00088           vision.white_balance=2;
00089         }
00090 #ifdef PLATFORM_APERIOS
00091         //this will actually send the new setting to the system
00092         OPrimitiveID fbkID = 0;
00093         if(OPENR::OpenPrimitive(CameraLocator, &fbkID) != oSUCCESS){
00094           std::cout << "Open FbkImageSensor failure." << std::endl;
00095         } else {
00096           OPrimitiveControl_CameraParam owb(vision.white_balance);
00097           if(OPENR::ControlPrimitive(fbkID, oprmreqCAM_SET_WHITE_BALANCE, &owb, sizeof(owb), 0, 0) != oSUCCESS){
00098             std::cout << "CAM_SET_WHITE_BALANCE : Failed!" << std::endl;
00099           }
00100           OPENR::ClosePrimitive(fbkID);
00101         }
00102 #endif
00103         return &vision.white_balance;
00104       } else if (strncasecmp(key,"gain",29)==0) {
00105         if (strncasecmp(value,"low",49)==0) {
00106           vision.gain=1;
00107         } else if (strncasecmp(value,"mid",49)==0) {
00108           vision.gain=2;
00109         } else if (strncasecmp(value,"high",49)==0) {
00110           vision.gain=3;
00111         }
00112 #ifdef PLATFORM_APERIOS
00113         //this will actually send the new setting to the system
00114         OPrimitiveID fbkID = 0;
00115         if(OPENR::OpenPrimitive(CameraLocator, &fbkID) != oSUCCESS){
00116           std::cout << "Open FbkImageSensor failure." << std::endl;
00117         } else {
00118           OPrimitiveControl_CameraParam ogain(vision.gain);
00119           if(OPENR::ControlPrimitive(fbkID, oprmreqCAM_SET_GAIN, &ogain, sizeof(ogain), 0, 0) != oSUCCESS)
00120             std::cout << "CAM_SET_GAIN : Failed!" << std::endl;
00121           OPENR::ClosePrimitive(fbkID);
00122         }
00123 #endif
00124         return &vision.gain;
00125       } else if (strncasecmp(key,"shutter_speed",29)==0) {
00126         if (strncasecmp(value,"slow",49)==0) {
00127           vision.shutter_speed=1;
00128         } else if (strncasecmp(value,"mid",49)==0) {
00129           vision.shutter_speed=2;
00130         } else if (strncasecmp(value,"fast",49)==0) {
00131           vision.shutter_speed=3;
00132         }
00133 #ifdef PLATFORM_APERIOS
00134         //this will actually send the new setting to the system
00135         OPrimitiveID fbkID = 0;
00136         if(OPENR::OpenPrimitive(CameraLocator, &fbkID) != oSUCCESS){
00137           std::cout << "Open FbkImageSensor failure." << std::endl;
00138         } else {
00139           OPrimitiveControl_CameraParam oshutter(vision.shutter_speed);
00140           if(OPENR::ControlPrimitive(fbkID,oprmreqCAM_SET_SHUTTER_SPEED, &oshutter, sizeof(oshutter), 0, 0) != oSUCCESS)
00141             std::cout << "CAM_SET_SHUTTER_SPEED : Failed!" << std::endl;
00142           OPENR::ClosePrimitive(fbkID);
00143         }
00144 #endif
00145         return &vision.shutter_speed;
00146       } else if (strncasecmp(key,"resolution",29)==0) {
00147         if (strncasecmp(value,"full",49)==0) {
00148           vision.resolution=1;
00149         } else if (strncasecmp(value,"half",49)==0) {
00150           vision.resolution=2;
00151         } else if (strncasecmp(value,"quarter",49)==0) {
00152           vision.resolution=3;
00153         }
00154         return &vision.resolution;
00155       } else if (strncasecmp(key,"thresh",29)==0) {
00156         vision.thresh.push_back(value);
00157         return &vision.thresh;
00158       } else if (strncasecmp(key,"colors",29)==0) {
00159         strncpy(vision.colors,value,49);
00160         return &vision.colors;
00161       } else if (strncasecmp(key,"rawcam_port",29)==0 || strncasecmp(key,"raw_port",29)==0) {
00162         if(strncasecmp(key,"raw_port",29)==0)
00163           std::cout << "Your tekkotsu.cfg file uses deprecated raw_port -- use rawcam_port instead" << std::endl;
00164         vision.rawcam_port=atoi(value);
00165         return &vision.rawcam_port;
00166       } else if (strncasecmp(key,"rawcam_transport",29)==0 || strncasecmp(key,"raw_transport",29)==0) {
00167         if(strncasecmp(key,"raw_transport",29)==0)
00168           std::cout << "Your tekkotsu.cfg file uses deprecated raw_transport -- use rawcam_transport instead" << std::endl;
00169         if (strncasecmp(value,"udp",49)==0)
00170           vision.rawcam_transport=0;
00171         else if (strncasecmp(value,"tcp",49)==0)
00172           vision.rawcam_transport=1;
00173         return &vision.rawcam_transport;
00174       } else if (strncasecmp(key,"rawcam_interval",29)==0) {
00175         vision.rawcam_interval=(unsigned int)atoi(value);
00176         return &vision.rawcam_interval;
00177       } else if (strncasecmp(key,"segcam_port",29)==0 || strncasecmp(key,"rle_port",29)==0) {
00178         if(strncasecmp(key,"rle_port",29)==0)
00179           std::cout << "Your tekkotsu.cfg file uses deprecated rle_port -- use segcam_port instead" << std::endl;
00180         vision.segcam_port=atoi(value);
00181         return &vision.segcam_port;
00182       } else if (strncasecmp(key,"segcam_transport",29)==0 || strncasecmp(key,"rle_transport",29)==0) {
00183         if(strncasecmp(key,"rle_transport",29)==0)
00184           std::cout << "Your tekkotsu.cfg file uses deprecated rle_transport -- use segcam_transport instead" << std::endl;
00185         if (strncasecmp(value,"udp",49)==0)
00186           vision.segcam_transport=0;
00187         else if (strncasecmp(value,"tcp",49)==0)
00188           vision.segcam_transport=1;
00189         return &vision.segcam_transport;
00190       } else if (strncasecmp(key,"segcam_interval",29)==0 || strncasecmp(key,"rle_interval",29)==0) {
00191         if(strncasecmp(key,"rle_interval",29)==0)
00192           std::cout << "Your tekkotsu.cfg file uses deprecated rle_interval -- use segcam_interval instead" << std::endl;
00193         vision.segcam_interval=(unsigned int)atoi(value);
00194         return &vision.segcam_interval;
00195       } else if (strncasecmp(key,"region_port",29)==0) {
00196         vision.region_port=atoi(value);
00197         return &vision.region_port;
00198       } else if (strncasecmp(key,"region_transport",29)==0) {
00199         if (strncasecmp(value,"udp",49)==0)
00200           vision.region_transport=0;
00201         else if (strncasecmp(value,"tcp",49)==0)
00202           vision.region_transport=1;
00203         return &vision.region_transport;
00204       } else if (strncasecmp(key,"obj_port",29)==0) {
00205         vision.obj_port=atoi(value);
00206         return &vision.obj_port;
00207       } else if (strncasecmp(key,"restore_image",29)==0) {
00208         vision.restore_image=atoi(value);
00209         return &vision.restore_image;
00210       } else if (strncasecmp(key,"region_calc_total",29)==0) {
00211         vision.region_calc_total=atoi(value);
00212         return &vision.region_calc_total;
00213       } else if (strncasecmp(key,"jpeg_dct_method",29)==0) {
00214         if (strncasecmp(value,"islow",49)==0) {
00215           vision.jpeg_dct_method=JDCT_ISLOW;
00216         } else if (strncasecmp(value,"ifast",49)==0) {
00217           vision.jpeg_dct_method=JDCT_IFAST;
00218         } else if (strncasecmp(value,"float",49)==0) {
00219           vision.jpeg_dct_method=JDCT_FLOAT;
00220         }
00221         return &vision.jpeg_dct_method;
00222       } else if (strncasecmp(key,"rawcam_encoding",29)==0) {
00223         if (strncasecmp(value,"color",49)==0) {
00224           vision.rawcam_encoding=vision_config::ENCODE_COLOR;
00225           vision.rawcam_channel=RawCameraGenerator::CHAN_Y;
00226         } else if (strncasecmp(value,"y_only",49)==0) {
00227           vision.rawcam_encoding=vision_config::ENCODE_SINGLE_CHANNEL;
00228           vision.rawcam_channel=RawCameraGenerator::CHAN_Y;
00229         } else if (strncasecmp(value,"uv_only",49)==0) {
00230           vision.rawcam_encoding=vision_config::ENCODE_COLOR;
00231           vision.rawcam_channel=-1;
00232         } else if (strncasecmp(value,"u_only",49)==0) {
00233           vision.rawcam_encoding=vision_config::ENCODE_SINGLE_CHANNEL;
00234           vision.rawcam_channel=RawCameraGenerator::CHAN_U;
00235         } else if (strncasecmp(value,"v_only",49)==0) {
00236           vision.rawcam_encoding=vision_config::ENCODE_SINGLE_CHANNEL;
00237           vision.rawcam_channel=RawCameraGenerator::CHAN_V;
00238         } else if (strncasecmp(value,"y_dx_only",49)==0) {
00239           vision.rawcam_encoding=vision_config::ENCODE_SINGLE_CHANNEL;
00240           vision.rawcam_channel=RawCameraGenerator::CHAN_Y_DX;
00241         } else if (strncasecmp(value,"y_dy_only",49)==0) {
00242           vision.rawcam_encoding=vision_config::ENCODE_SINGLE_CHANNEL;
00243           vision.rawcam_channel=RawCameraGenerator::CHAN_Y_DY;
00244         } else if (strncasecmp(value,"y_dxdy_only",49)==0) {
00245           vision.rawcam_encoding=vision_config::ENCODE_SINGLE_CHANNEL;
00246           vision.rawcam_channel=RawCameraGenerator::CHAN_Y_DXDY;
00247         }
00248         return &vision.rawcam_encoding;
00249       } else if (strncasecmp(key,"rawcam_compression",29)==0) {
00250         if (strncasecmp(value,"none",49)==0) {
00251           vision.rawcam_compression=vision_config::COMPRESS_NONE;
00252         } else if (strncasecmp(value,"jpeg",49)==0) {
00253           vision.rawcam_compression=vision_config::COMPRESS_JPEG;
00254         } else if (strncasecmp(value,"png",49)==0) {
00255           vision.rawcam_compression=vision_config::COMPRESS_PNG;
00256         }
00257         return &vision.rawcam_compression;
00258       } else if (strncasecmp(key,"rawcam_compress_quality",29)==0) {
00259         vision.rawcam_compress_quality=atoi(value);
00260         return &vision.rawcam_compress_quality;
00261       } else if (strncasecmp(key,"rawcam_y_skip",29)==0) {
00262         vision.rawcam_y_skip=atoi(value);
00263         if(ProjectInterface::defRawCameraGenerator!=NULL && vision.rawcam_y_skip>(int)ProjectInterface::defRawCameraGenerator->getNumLayers()-1)
00264           vision.rawcam_y_skip=ProjectInterface::defRawCameraGenerator->getNumLayers()-1;
00265         return &vision.rawcam_y_skip;
00266       } else if (strncasecmp(key,"rawcam_uv_skip",29)==0) {
00267         vision.rawcam_uv_skip=atoi(value);
00268         if(ProjectInterface::defRawCameraGenerator!=NULL && vision.rawcam_uv_skip>(int)ProjectInterface::defRawCameraGenerator->getNumLayers()-1)
00269           vision.rawcam_uv_skip=ProjectInterface::defRawCameraGenerator->getNumLayers()-1;
00270         return &vision.rawcam_uv_skip;
00271       } else if (strncasecmp(key,"segcam_skip",29)==0 || strncasecmp(key,"rlecam_skip",29)==0) {
00272         if(strncasecmp(key,"rlecam_skip",29)==0)
00273           std::cout << "Your tekkotsu.cfg file uses deprecated rlecam_skip -- use segcam_skip instead" << std::endl;
00274         vision.segcam_skip=atoi(value);
00275         if(ProjectInterface::defSegmentedColorGenerator!=NULL && vision.segcam_skip>(int)ProjectInterface::defSegmentedColorGenerator->getNumLayers()-1)
00276           vision.segcam_skip=ProjectInterface::defSegmentedColorGenerator->getNumLayers()-1;
00277         return &vision.segcam_skip;
00278       } else if (strncasecmp(key,"segcam_channel",29)==0 || strncasecmp(key,"rlecam_channel",29)==0) {
00279         if(strncasecmp(key,"rlecam_channel",29)==0)
00280           std::cout << "Your tekkotsu.cfg file uses deprecated rlecam_channel -- use segcam_channel instead" << std::endl;
00281         vision.segcam_channel=atoi(value);
00282         return &vision.segcam_channel;
00283       } else if (strncasecmp(key,"segcam_compression",29)==0 || strncasecmp(key,"rlecam_compression",29)==0) {
00284         if(strncasecmp(key,"rlecam_compression",29)==0)
00285           std::cout << "Your tekkotsu.cfg file uses deprecated rlecam_compression -- use segcam_compression instead" << std::endl;
00286         if (strncasecmp(value,"none",49)==0) {
00287           vision.segcam_compression=vision_config::COMPRESS_NONE;
00288         } else if (strncasecmp(value,"rle",49)==0) {
00289           vision.segcam_compression=vision_config::COMPRESS_RLE;
00290         } else if (strncasecmp(value,"png",49)==0) {
00291           vision.segcam_compression=vision_config::COMPRESS_PNG;
00292         }
00293         return &vision.segcam_compression;
00294       } else if (strncasecmp(key,"regioncam_skip",29)==0) {
00295         vision.regioncam_skip=atoi(value);
00296         return &vision.regioncam_skip;
00297       } else if (strncasecmp(key,"focal_len_x",29)==0) {
00298         vision.focal_len_x=atof(value);
00299         return &vision.focal_len_x;
00300       } else if (strncasecmp(key,"focal_len_y",29)==0) {
00301         vision.focal_len_y=atof(value);
00302         return &vision.focal_len_y;
00303       } else if (strncasecmp(key,"principle_point_x",29)==0) {
00304         vision.principle_point_x=atof(value);
00305         return &vision.principle_point_x;
00306       } else if (strncasecmp(key,"principle_point_y",29)==0) {
00307         vision.principle_point_y=atof(value);
00308         return &vision.principle_point_y;
00309       } else if (strncasecmp(key,"skew",29)==0) {
00310         vision.skew=atof(value);
00311         return &vision.skew;
00312       } else if (strncasecmp(key,"kc1_r2",29)==0) {
00313         vision.kc1_r2=atof(value);
00314         return &vision.kc1_r2;
00315       } else if (strncasecmp(key,"kc2_r4",29)==0) {
00316         vision.kc2_r4=atof(value);
00317         return &vision.kc2_r4;
00318       } else if (strncasecmp(key,"kc5_r6",29)==0) {
00319         vision.kc5_r6=atof(value);
00320         return &vision.kc5_r6;
00321       } else if (strncasecmp(key,"kc3_tan1",29)==0) {
00322         vision.kc3_tan1=atof(value);
00323         return &vision.kc3_tan1;
00324       } else if (strncasecmp(key,"kc4_tan2",29)==0) {
00325         vision.kc4_tan2=atof(value);
00326         return &vision.kc4_tan2;
00327       } else if (strncasecmp(key,"calibration_res_x",29)==0) {
00328         vision.calibration_res_x=atoi(value);
00329         return &vision.kc4_tan2;
00330       } else if (strncasecmp(key,"calibration_res_y",29)==0) {
00331         vision.calibration_res_y=atoi(value);
00332         return &vision.calibration_res_y;
00333       }
00334       break;
00335     case sec_main:
00336       if (strncasecmp(key,"seed_rng",29)==0) {
00337         main.seed_rng=atoi(value);
00338         return &main.console_port;
00339       } else if (strncasecmp(key,"console_port",29)==0) {
00340         main.console_port=atoi(value);
00341         return &main.console_port;
00342       } else if (strncasecmp(key,"consoleMode",29)==0) {
00343         if (strncasecmp(value,"controller",49)==0) {
00344           main.consoleMode=main_config::CONTROLLER;
00345         } else if (strncasecmp(value,"textmsg",49)==0) {
00346           main.consoleMode=main_config::TEXTMSG;
00347         } else if (strncasecmp(value,"auto",49)==0) {
00348           main.consoleMode=main_config::AUTO;
00349         }
00350         return &main.consoleMode;
00351       } else if (strncasecmp(key,"stderr_port",29)==0) {
00352         main.stderr_port=atoi(value);
00353         return &main.stderr_port;
00354       } else if (strncasecmp(key,"error_level",29)==0) {
00355         main.error_level=atoi(value);
00356         return &main.error_level;
00357       } else if (strncasecmp(key,"debug_level",29)==0) {
00358         main.debug_level=atoi(value);
00359         return &main.debug_level;
00360       } else if (strncasecmp(key,"verbose_level",29)==0) {
00361         main.verbose_level=atoi(value);
00362         return &main.verbose_level;
00363       } else if (strncasecmp(key,"wsjoints_port",29)==0) {
00364         main.wsjoints_port=atoi(value);
00365         return &main.wsjoints_port;
00366       } else if (strncasecmp(key,"wspids_port",29)==0) {
00367         main.wspids_port=atoi(value);
00368         return &main.wspids_port;
00369       } else if (strncasecmp(key,"headControl_port",29)==0) {
00370         main.headControl_port=atoi(value);
00371         return &main.headControl_port;
00372       } else if (strncasecmp(key,"walkControl_port",29)==0) {
00373         main.walkControl_port=atoi(value);
00374         return &main.walkControl_port;
00375       } else if (strncasecmp(key,"estopControl_port",29)==0) {
00376         main.estopControl_port=atoi(value);
00377         return &main.estopControl_port;
00378       } else if (strncasecmp(key,"aibo3d_port",29)==0) {
00379         main.aibo3d_port=atoi(value);
00380         return &main.aibo3d_port;
00381       } else if (strncasecmp(key,"wmmonitor_port",29)==0) {
00382         main.wmmonitor_port=atoi(value);
00383         return &main.wmmonitor_port;
00384       } else if (strncasecmp(key,"use_VT100",29)==0) {
00385         main.use_VT100=extractBool(value);
00386         return &main.use_VT100;
00387       }
00388       break;
00389     case sec_behaviors:
00390       if (strncasecmp(key,"flash_bytes",29)==0) {
00391         behaviors.flash_bytes = atoi(value);
00392         return &behaviors.flash_bytes;
00393       } else if (strncasecmp(key,"flash_on_start",29)==0) {
00394         behaviors.flash_on_start = atoi(value);
00395         return &behaviors.flash_on_start;
00396       }
00397       break;
00398     case sec_controller:
00399       if (strncasecmp(key,"gui_port",29)==0) {
00400         controller.gui_port = atoi(value);
00401         return &controller.gui_port ;
00402       } else if (strncasecmp(key,"select_snd",29)==0) {
00403         strncpy(controller.select_snd,value,49);
00404         return &controller.select_snd;
00405       } else if (strncasecmp(key,"next_snd",29)==0) {
00406         strncpy(controller.next_snd,value,49);
00407         return &controller.next_snd;
00408       } else if (strncasecmp(key,"prev_snd",29)==0) {
00409         strncpy(controller.prev_snd,value,49);
00410         return &controller.prev_snd;
00411       } else if (strncasecmp(key,"read_snd",29)==0) {
00412         strncpy(controller.read_snd,value,49);
00413         return &controller.read_snd;
00414       } else if (strncasecmp(key,"cancel_snd",29)==0) {
00415         strncpy(controller.cancel_snd,value,49);
00416         return &controller.cancel_snd;
00417       } else if (strncasecmp(key,"error_snd",29)==0) {
00418         strncpy(controller.error_snd,value,49);
00419         return &controller.error_snd;
00420       }
00421       break;
00422     case sec_motion:
00423       if (strncasecmp(key,"root",29)==0) {
00424         motion.root=value;
00425         return &motion.root;
00426       } else if (strncasecmp(key,"walk",29)==0) {
00427         motion.walk=value;
00428         return &motion.walk;
00429       } else if (strncasecmp(key,"kinematics",29)==0) {
00430         motion.kinematics=value;
00431         return &motion.walk;
00432       } else if (strncasecmp(key,"kinematic_chains",29)==0) {
00433         motion.kinematic_chains.push_back(value);
00434         return &motion.kinematic_chains;
00435       } else if (strncasecmp(key,"calibrate:",10)==0) {
00436         for(unsigned int i=PIDJointOffset; i<PIDJointOffset+NumPIDJoints; i++)
00437           if(strncasecmp(&key[10],outputNames[i],outputNameLen+1)==0) { //+1 to ensure full match
00438             motion.calibration[i-PIDJointOffset] = atof(value);
00439             return &motion.calibration[i];
00440           }
00441             std::cout << "WARNING: Could not match '" << (strlen(key)>10?&key[10]:key) << "' as calibration parameter" << std::endl;
00442         return NULL;
00443       } else if (strncasecmp(key,"estop_on_snd",29)==0) {
00444         strncpy(motion.estop_on_snd,value,49);
00445         return &motion.estop_on_snd;
00446       } else if (strncasecmp(key,"estop_off_snd",29)==0) {
00447         strncpy(motion.estop_off_snd,value,49);
00448         return &motion.estop_off_snd;
00449       } else if (strncasecmp(key,"max_head_tilt_speed",29)==0) {
00450         motion.max_head_tilt_speed=atof(value);
00451         return &motion.max_head_tilt_speed;
00452       } else if (strncasecmp(key,"max_head_pan_speed",29)==0) {
00453         motion.max_head_pan_speed=atof(value);
00454         return &motion.max_head_pan_speed;
00455       } else if (strncasecmp(key,"max_head_roll_speed",29)==0) {
00456         motion.max_head_roll_speed=atof(value);
00457         return &motion.max_head_roll_speed;
00458       } else if (strncasecmp(key,"inf_walk_accel",29)==0) {
00459         motion.inf_walk_accel = atoi(value);
00460         return &motion.inf_walk_accel;
00461       } else if (strncasecmp(key,"console_port",29)==0) {
00462         motion.console_port = atoi(value);
00463         return &motion.console_port;
00464       } else if (strncasecmp(key,"stderr_port",29)==0) {
00465         motion.stderr_port = atoi(value);
00466         return &motion.stderr_port ;
00467       }
00468       break;
00469     case sec_sound:
00470       if (strncasecmp(key,"root",29)==0) {
00471         sound.root=value;
00472         return &sound.root;
00473       } else if (strncasecmp(key,"volume",29)==0) {
00474         if(strncasecmp(value,"mute",49)==0)
00475           sound.volume=ospkvolinfdB;
00476         else if(strncasecmp(value,"level_1",49)==0)
00477           sound.volume=ospkvol25dB;
00478         else if(strncasecmp(value,"level_2",49)==0)
00479           sound.volume=ospkvol18dB;
00480         else if(strncasecmp(value,"level_3",49)==0)
00481           sound.volume=ospkvol10dB;
00482         else
00483           return NULL;
00484         return &sound.volume;
00485       } else if (strncasecmp(key,"sample_rate",29)==0) {
00486         sound.sample_rate = atoi(value);
00487         return &sound.sample_rate ;
00488       } else if (strncasecmp(key,"sample_bits",29)==0) {
00489         sound.sample_bits = atoi(value);
00490         return &sound.sample_bits ;
00491       } else if (strncasecmp(key,"preload",29)==0) {
00492         sound.preload.push_back(value);
00493         return &sound.preload ;
00494       } else if (strncasecmp(key,"pitchConfidenceThreshold",29)==0) {
00495         sound.pitchConfidenceThreshold=atof(value);
00496         return &sound.pitchConfidenceThreshold ;
00497       } else if (strncasecmp(key,"streaming.mic_port",29)==0) {
00498         sound.streaming.mic_port = atoi(value);
00499         return &sound.streaming.mic_port;
00500       } else if (strncasecmp(key,"streaming.mic_sample_rate",29)==0) {
00501         sound.streaming.mic_sample_rate = atoi(value);
00502         return &sound.streaming.mic_sample_rate;
00503       } else if (strncasecmp(key,"streaming.mic_bits",29)==0) {
00504         sound.streaming.mic_sample_bits = atoi(value);
00505         return &sound.streaming.mic_sample_bits;
00506       } else if (strncasecmp(key,"streaming.mic_stereo",29)==0) {
00507         sound.streaming.mic_stereo = extractBool(value);
00508         return &sound.streaming.mic_stereo;
00509       } else if (strncasecmp(key,"streaming.speaker_port",29)==0) {
00510         sound.streaming.speaker_port = atoi(value);
00511         return &sound.streaming.speaker_port;
00512       } else if (strncasecmp(key,"streaming.speaker_frame_len",29)==0) {
00513         sound.streaming.speaker_frame_length = atoi(value);
00514         return &sound.streaming.speaker_frame_length;
00515       } else if (strncasecmp(key,"streaming.speaker_max_delay",29)==0) {
00516         sound.streaming.speaker_max_delay = atoi(value);
00517         return &sound.streaming.speaker_max_delay;
00518       }
00519       break;
00520     case sec_invalid:
00521       break;
00522   }
00523   return NULL;
00524 }
00525 
00526 Config::section_t Config::parseSection(const char* key) {
00527   if (strncasecmp(key,"wireless",29)==0) {
00528     return sec_wireless;
00529   } else if (strncasecmp(key,"vision",29)==0) {
00530     return sec_vision;
00531   } else if (strncasecmp(key,"main",29)==0) {
00532     return sec_main;
00533   } else if (strncasecmp(key,"behaviors",29)==0) {
00534     return sec_behaviors;
00535   } else if (strncasecmp(key,"controller",29)==0) {
00536     return sec_controller;
00537   } else if (strncasecmp(key,"motion",29)==0) {
00538     return sec_motion;
00539   } else if (strncasecmp(key,"sound",29)==0) {
00540     return sec_sound;
00541   } else {
00542     return sec_invalid;
00543   }
00544 }
00545 
00546 void Config::readConfig(const std::string& filename) {
00547   FILE* fp = fopen(portPath(filename).c_str(), "r");
00548   if (fp==NULL)
00549     return;
00550   
00551   char buf[256], key[30], value[50];
00552   section_t section=sec_invalid;
00553   bool ignoring=false;
00554   
00555   std::vector<std::string> curmodel;
00556 #ifdef PLATFORM_APERIOS
00557   char rdStr[orobotdesignNAME_MAX + 1];
00558   memset(rdStr, 0, sizeof(rdStr));
00559   if (OPENR::GetRobotDesign(rdStr) != oSUCCESS) {
00560     printf("OPENR::GetRobotDesign() failed.\n");
00561     rdStr[0]='\0';
00562   }
00563 #else
00564 #  if TGT_ERS7
00565   char rdStr[]="ERS-7";
00566 #  elif TGT_ERS210
00567   char rdStr[]="ERS-210";
00568 #  elif TGT_ERS220
00569   char rdStr[]="ERS-220";
00570 #  elif TGT_ERS2xx
00571 #    warning "TGT_2xx is not specific for simulation purposes - defaulting to ERS210"
00572   char rdStr[]="ERS-210";
00573 #  else
00574 #    warning "TGT_<model> undefined - defaulting to ERS7"
00575   char rdStr[]="ERS-7";
00576 #  endif
00577 #endif
00578   
00579   
00580   unsigned int lineno=0;
00581   while (fgets(buf,256,fp)!=NULL) {
00582     lineno++;
00583     char * p=buf;
00584     while(*p!='\0' && isspace(*p))
00585       p++;
00586     if(*p=='\0' || *p=='#')
00587       continue; //empty line or comment
00588     
00589     if (sscanf(buf,"<%29[^>]>",key)>0) {
00590       if(key[0]=='/') {
00591         if(curmodel.size()==0) {
00592           printf("WARNING: not in a model specific section, line %d\n",lineno);
00593           continue;
00594         }
00595         bool subset=matchNoCase(&key[1],curmodel.back());
00596         bool superset=matchNoCase(curmodel.back(),&key[1]);
00597         if(subset && superset) {
00598           //printf("leaving modelsection %s\n",curmodel.back().c_str());
00599           curmodel.pop_back();
00600         } else if(superset) {
00601           while(curmodel.size()>0) {
00602             //printf("leaving modelsection %s (==%s)\n",curmodel.back().c_str(),&key[1]);
00603             curmodel.pop_back();
00604             if(!matchNoCase(curmodel.back(),&key[1]))
00605               break;
00606           }
00607         } else
00608           printf("WARNING: config model mismatch, line %d\n",lineno);
00609         
00610         ignoring=false; //scan through current model selection stack, see if we're still ignoring
00611         for(unsigned int i=0; i<curmodel.size(); i++)
00612           if(!matchNoCase(rdStr,curmodel[i])) {
00613             ignoring=true;
00614             break;
00615           }
00616             //printf("ignoring==%d\n",ignoring);
00617             
00618       } else {
00619         curmodel.push_back(key);
00620         //printf("entering section %s\n",curmodel.back().c_str());
00621         ignoring=ignoring || !matchNoCase(rdStr,key);
00622         //printf("ignoring==%d\n",ignoring);
00623       }
00624     } else if(!ignoring) {
00625       if (sscanf(buf,"[%29[^]]]",key)>0) {
00626         section=parseSection(key);
00627         if(section==sec_invalid)
00628           std::cerr << "WARNING: Reading " << filename << ", unknown section " << key << " at line number " << lineno << endl;
00629         //std::cout << "now parsing section " << section << std::endl;
00630       } else if (sscanf(buf,"%29[^ =] =%49s",key,value)>1) {
00631         //printf("setValue(%d,'%s','%s');\n",section,key,value);
00632         if(section!=sec_invalid)
00633           if(setValue(section, key, value)==NULL)
00634             std::cerr << "WARNING: Reading " << filename << ", illegal key/value " << key << '/' << value << " at line number " << lineno << endl;
00635       } else {
00636         std::cerr << "WARNING: Reading " << filename << ", parse error on key/value " << key << '/' << value << " at line number " << lineno << endl;
00637       }
00638     }
00639   }
00640     fclose(fp);
00641 }
00642 
00643 bool Config::matchNoCase(const std::string& model, const std::string& pattern) {
00644   unsigned int i=0;
00645   if(i==pattern.size() && i==model.size())
00646     return true;
00647   if(i==pattern.size() || i==model.size())
00648     return false;
00649   while(pattern[i]!='*') {
00650     if(toupper(pattern[i])!=toupper(model[i]))
00651       return false;
00652     i++;
00653     if(i==pattern.size() && i==model.size())
00654       return true;
00655     if(i==pattern.size() || i==model.size())
00656       return false;
00657   }
00658   i=pattern.size()-1;
00659   unsigned int j=model.size()-1;
00660   while(pattern[i]!='*') {
00661     if(toupper(pattern[i])!=toupper(model[j]))
00662       return false;
00663     i--; j--;
00664   }
00665   return true;
00666 }
00667 
00668 bool Config::extractBool(const char * value) {
00669   int i=0;
00670   while(isspace(value[i])) i++;
00671   if(strncasecmp(&value[i],"t",29)==0)
00672     return true;
00673   else if(strncasecmp(&value[i],"f",29)==0)
00674     return false;
00675   else if(strncasecmp(&value[i],"true",29)==0)
00676     return true;
00677   else if(strncasecmp(&value[i],"false",29)==0)
00678     return false;
00679   else if(strncasecmp(&value[i],"y",29)==0)
00680     return true;
00681   else if(strncasecmp(&value[i],"n",29)==0)
00682     return false;
00683   else if(strncasecmp(&value[i],"yes",29)==0)
00684     return true;
00685   else if(strncasecmp(&value[i],"no",29)==0)
00686     return false;
00687   else
00688     return atoi(value);
00689 }
00690 
00691 /*! @file
00692  * @brief Implements Config, which provides global access to system configuration information
00693  * @author alokl (Creator)
00694  *
00695  * $Author: ejt $
00696  * $Name: tekkotsu-3_0 $
00697  * $Revision: 1.59 $
00698  * $State: Exp $
00699  * $Date: 2006/09/27 20:36:06 $
00700  */
00701 

Tekkotsu v3.0
Generated Wed Oct 4 00:03:42 2006 by Doxygen 1.4.7