// config.cc - Classe OConfig // robert - Programme du robot APBteam // Copyright (C) 2005 Olivier Gaillard /// @file oconfig.cc Charge le fichier config et distribue les variables #include #include #include #include #include #include "oconfig.hh" #include "group.hh" OConfig *OConfig::instance = 0; /// Constructor /// @param *filename nom du fichier de config OConfig::OConfig (const char *filename) : node (0), color (0), index (0) { instance = this; load (filename); loadNNFile ("../runtime/rc/poids"); loadDistFile ("../runtime/rc/dist"); } /// Destructor OConfig::~OConfig () { delete [] node; delete [] color; delete [] index; } /// Parse une ligne du fichier de config /// @param *var nom de la variable a fixer /// @param *arg valeur de la variable void OConfig::parse (const char *var, const char *arg) { if (!arg) return; std::string varName (var); if (varName == "Cam_color") { std::string argName (arg); if (argName == "RGB") inputColor = Image::rgb; else if (argName == "BGR") inputColor = Image::bgr; else if (argName == "YUV") inputColor = Image::yuv; else if (argName == "HSI") inputColor = Image::hsi; return; } // Affecte la valeur de arg a la variable varName if (varName == "NN_step_learning") nnSl = atof (arg); else if (varName == "NN_neighborhood_learning") nnNl = atof (arg); else if (varName == "NN_number_of_iteration_learning") nnNil = atol(arg); else if (varName == "NN_threshold_output") nnThresholdOutput = atoi (arg); else if (varName == "NN_luminosity_influence") nnInfluLum = atof (arg); else if (varName == "UI_img_path ") strcpy(imgPath, arg); else if (varName == "NN_lazy_threshold") nnLazyThreshold = atoi (arg); // else if (varName == "Map_error") mapError = atoi (arg); // else if (varName == "Map_error_part") mapErrorPart = atoi (arg); // else if (varName == "Map_angle_ball_weight") angleBallWeight= atoi (arg); // else if (varName == "Map_distance_ball_robot_weight") distanceBallRobotWeight = atoi (arg); // else if (varName == "Map_distance_ball_goal_weight") distanceBallGoalWeight = atoi (arg); // else if (varName == "Map_ball_density_weight") ballDensityWeight = atoi (arg); // else if (varName == "Map_ennemy_presence_weight") ennemyPresenceWeight = atoi (arg); // else if (varName == "Map_ball_precision_weight") ballPrecisionWeight = atoi (arg); // else if (varName == "Map_skepticism_weight") skepticismWeight = atoi (arg); // else if (varName == "Map_skepticism_max") skepticismMax = atoi (arg); // else if (varName == "Map_ball_lost_weight") ballLostWeight = atoi (arg); // else if (varName == "Map_ball_bottom_time_out") ballBottomTimeOut = atoi (arg); else if (varName == "Group_minimum_length_zone") minLengthZone = atoi (arg); else if (varName == "Group_jump_point_distance") jumpPointDist = atoi (arg); else if (varName == "UI_group_to_display") uiGroupToDisplay = atoi (arg); else if (varName == "Skittle_border") skittleBorder = atoi (arg); else if (varName == "Skittle_grow") skittleGrow = atoi (arg); else if (varName == "Skittle_div_jump") skittleDivJump = atoi (arg); else if (varName == "Skittle_vertical_ratio") skittleVerticalRatio = atof (arg); else if (varName == "Skittle_score_bend_ratio") skittleScoreBendRatio = atof (arg); else if (varName == "Mag_uniqueness") magUniqueness = atoi (arg); else if (varName == "Cam_brightness") brightness = atoi (arg); else if (varName == "Cam_width") width = atoi (arg); else if (varName == "Cam_height") height = atoi (arg); } /// Charge les variables du fichier de configuration (par défaut rc/vision.conf) void OConfig::load (const char *filename) { const int NBARG = 3; char *cut[NBARG] = {0}; FILE *file; char ligne[50]; int i; // Ouverture du fichier de conf file = fopen(filename, "r"); if (!file) throw "OConfig::OConfig : Error during config file opening"; else std::cout << "Lecture du ficher de configuration" << std::endl; // Parcours des lignes et analyse while(fgets(ligne, 50, file)) { if (ligne[0] == '#') continue; // Division du string cut[0] = strtok(ligne, " \n"); if (!cut[0]) continue; i=0; while ((cut[i] != 0) && (i<(NBARG-1))) { i++; cut[i] = strtok( 0, " \t\n"); } parse(cut[0], cut[2]); } fclose (file); } /// Chargement des poids d'un reseau de neurones /// @param *filePath nom du fichier de poids a charger void OConfig::loadNNFile (const char *filePath) { const int maxChar = 50; // Ouverture du fichier de conf std::ifstream file (filePath); if (!file) { throw "OConfig::LoadNNFile : Error during poids file opening"; return; } // Chargement des couleurs char buf [maxChar]; groupColor.clear (); while (!file.eof ()) { file.getline (buf, maxChar); std::string line (buf); if (line.find("#index") != std::string::npos) break; if (buf[0] == '\0' || buf[0] == ' ' || buf[0] == '\n' || buf[0] == '\t' || buf[0] == '#') continue; std::istringstream iss (buf); ObjectColor tmp; iss >> tmp.label >> tmp.color; if (tmp.label == "redSktittle") tmp.type = Group::redSkittle; else if (tmp.label == "greenSkittle") tmp.type = Group::greenSkittle; else if (tmp.label == "border") tmp.type = Group::border; else if (tmp.label == "base") tmp.type = Group::base; else if (tmp.label == "gap") tmp.type = Group::gap; else if (tmp.label == "reflectBand") tmp.type = Group::reflectBand; else tmp.type = Group::undefined; groupColor.push_back (tmp); } // Chargement des poids std::vector tmpNode, tmpIndex; int tmp; while (!file.eof ()) { file.getline (buf, maxChar); std::istringstream iss (buf); if (buf[0] == '\0' || buf[0] == ' ' || buf[0] == '\n' || buf[0] == '\t' || buf[0] == '#') continue; iss >> tmp; tmpIndex.push_back (tmp); for (int i=0; i<3; i++) { iss >> tmp; tmpNode.push_back (tmp); } } file.close (); // Recopie dans des tableaux delete [] node; node = new unsigned char[tmpNode.size ()]; for (unsigned int i=0; i::const_iterator i = groupColor.begin (); i < groupColor.end (); ++i) { file << i->label << " " << i->color << "\n"; } // Poids file << "#index\t#x1\tx2\tx3\n"; for (int i=0; i= '0') && (buf[0] <= '9')) { sscanf(buf, "%i\t%i\t%i\t%i\n", &point[0], &point[1], &point[2], &point[3]); spaceTabPoint.push_back(point[0]); spaceTabPoint.push_back(point[1]); spaceTabPoint.push_back(point[2]); spaceTabPoint.push_back(point[3]); } file.close (); // Nombre de points venant d'être chargés spaceNbDistPoint = spaceTabPoint.size () / 4; } /// Creation d'un fichier pour la liste des points pour les distances void OConfig::createDistFile (const char *filename, const int numPoint) const { if (spaceTabPoint.size () == 0) { throw "OConfig::CreateDistFile : spaceTabPoint vide"; return; } // Ecriture dans un fichier std::ofstream file (filename); // Remplissage du fichier file << "#imgX\timgY\tdistX\tdistY\n"; for (int i=0; i