// config.cc - Classe OConfig // nono - Programme du robot Efrei Robotique I1-I2 2004 // Copyright (C) 2004 Olivier Gaillard /// @file config.cc Charge le fichier config et distribue les variables #include "oconfig.h" #include #include #include #include using namespace std; OConfig *OConfig::instance = 0; /// Constructor /// @param *filename nom du fichier de config OConfig::OConfig(char *filename) { instance = this; Load (filename); colorMode = 0; color = NULL; node = NULL; index = NULL; LoadNNFile("rc/poids"); LoadDistFile("rc/dist"); } /// Destructor OConfig::~OConfig() { } /// Parse une ligne du fichier de config /// @param *var nom de la variable a fixer /// @param *arg valeur de la variable void OConfig::Parse(char *var, char *arg) { char argu[20]; if (!arg) cerr << "OConfig::Parse : Error during config file parsing" << endl; strcpy(argu, arg); // Verifie si l'argument est un nombre ou un nom de fichier if (((argu[0]>'9') || (argu[0] < '0')) && strcmp(var, "Source") && strcmp(var, "imgPath")) { FILE *file; file=fopen(arg, "r"); if (!file) cerr << "OConfig::Parse : Bad pathfile for " << arg << endl; fgets(argu, 20,file); fclose(file); } // Affecte la valeur de argu a la variable var if (!strcmp (var,"NN_step_learning")) nn_sl = atof(argu); else if (!strcmp (var,"NN_neighborhood_learning")) nn_nl = atof(argu); else if (!strcmp (var,"NN_number_of_iteration_learning")) nn_nil = atol(argu); else if (!strcmp (var,"NN_number_of_color_to_segment")) nn_NbCouleurs = atoi(argu); else if (!strcmp (var,"NN_threshold_output")) nn_threshold_output = atoi(argu); else if (!strcmp (var,"NN_luminosity_influence")) nn_influ_lum = atof(argu); else if (!strcmp (var,"UI_img_path ")) strcpy(imgPath, argu); else if (!strcmp (var,"NN_lazy_threshold")) nn_lazy_threshold = atoi(argu); else if (!strcmp (var,"Map_error")) map_error = atoi(argu); else if (!strcmp (var,"Map_error_part")) map_error_part = atoi(argu); else if (!strcmp (var,"Map_angle_ball_weight")) angle_ball_weight= atoi(argu); else if (!strcmp (var,"Map_distance_ball_robot_weight")) distance_ball_robot_weight = atoi(argu); else if (!strcmp (var,"Map_distance_ball_goal_weight")) distance_ball_goal_weight = atoi(argu); else if (!strcmp (var,"Map_ball_density_weight")) ball_density_weight = atoi(argu); else if (!strcmp (var,"Map_ennemy_presence_weight")) ennemy_presence_weight = atoi(argu); else if (!strcmp (var,"Map_ball_precision_weight")) ball_precision_weight = atoi(argu); else if (!strcmp (var,"Map_skepticism_weight")) skepticism_weight = atoi(argu); else if (!strcmp (var,"Map_skepticism_max")) skepticism_max = atoi(argu); else if (!strcmp (var,"Map_ball_lost_weight")) ball_lost_weight = atoi(argu); else if (!strcmp (var,"Map_ball_bottom_time_out")) ball_bottom_time_out = atoi(argu); else if (!strcmp (var,"Group_minimum_length_zone")) minLengthZone = atoi(argu); else if (!strcmp (var,"Group_jump_point_distance")) jumpPointDist = atoi(argu); } void OConfig::Load (char *filename) { const int NBARG = 3; char *cut[NBARG] = {NULL}; FILE *file; char ligne[50]; int i; // Ouverture du fichier de conf file = fopen(filename, "r"); if (!file) cerr << "OConfig::OConfig : Error during config file opening" << endl; else cout << "Lecture du ficher de configuration" << 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] != NULL) && (i<(NBARG-1))) { i++; cut[i] = strtok( NULL, " \t\n"); } Parse(cut[0], cut[2]); } } /// Chargement des poids d'un reseau de neurones /// @param *filePath nom du fichier de poids a charger void OConfig::LoadNNFile (char *filePath) { const int NBARG = 4; char *cut[NBARG] = {NULL}; FILE *file; char ligne[50]; int i; unsigned char *pNode; // Ouverture du fichier de conf file = fopen(filePath, "r"); if (!file) { cerr << "OConfig::LoadNNFile : Error during poids file opening" << endl; index = new int[nn_NbCouleurs]; 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]); tabPoint.push_back(point[0]); tabPoint.push_back(point[1]); tabPoint.push_back(point[2]); tabPoint.push_back(point[3]); } } fclose(file); // Nombre de points venant d'être chargé nbDistPoint = tabPoint.size () / 4; } /// Creation d'un fichier pour la liste des points pour les distances void OConfig::CreateDistFile (char *filename, int numPoint) { if (tabPoint.size () == 0) { cerr << "OConfig::CreateDistFile : tabPoint vide" << endl; return; } // Ecriture dans un fichier FILE *file; file = fopen(filename, "w+"); // Remplissage du fichier fprintf(file, "#imgX\timgY\tdistX\tdistY\n"); // for (int i=0; i