// 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; /// 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, "Hauteur_Image")) height = atoi(argu); else if(!strcmp(var,"Largeur_Image")) width = atoi(argu); else 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_Iteration_Learning")) nn_nil = atol(argu); else if(!strcmp(var,"NN_Nombre_Couleurs")) nn_NbCouleurs = atoi(argu); else if(!strcmp(var,"NN_Threshold_Output")) nn_threshold_output = atoi(argu); else if(!strcmp(var,"NN_Influence_Luminosite")) nn_influ_lum = atof(argu); else if(!strcmp(var,"imgPath")) 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,"Angle_Ball")) angle_ball_weight= atoi(argu); else if(!strcmp(var,"Distance_Ball_Robot")) distance_ball_robot_weight = atoi(argu); else if(!strcmp(var,"Distance_Ball_Goal")) distance_ball_goal_weight = atoi(argu); else if(!strcmp(var,"Ball_Density")) ball_density_weight = atoi(argu); else if(!strcmp(var,"Ennemy_Presence")) ennemy_presence_weight = atoi(argu); else if(!strcmp(var,"Ball_Precision")) ball_precision_weight = atoi(argu); else if(!strcmp(var,"Skepticism")) skepticism_weight = atoi(argu); else if(!strcmp(var,"Skepticism_Max")) skepticism_max = atoi(argu); else if(!strcmp(var,"Minimum_Length_Zone")) minLengthZone = atoi(argu); else if(!strcmp(var,"Ball_Lost")) ball_lost_weight = atoi(argu); else if(!strcmp(var,"Ball_Bottom_Time_Out")) ball_bottom_time_out = atoi(argu); else if(!strcmp(var,"One_NN_Learning_Rate")) one_nn_learning_rate = atof(argu); else if(!strcmp(var,"One_NN_Learning_Iteration")) one_nn_learning_iteration = atoi(argu); } /// 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"); } 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]); //cout << point[0] << " " << point[1] << endl; } } cout << endl; fclose(file); nbDistPoint = tabPoint.size () / 4; } /// Creation d'un fichier pour la tabPointle des distances void OConfig::CreateDistFile(char *filename, int numPoint) { if (tabPoint.size () /4) { cerr << "OConfig::CreateDistFile : tabPoint vide\n"; return; } // Ecriture dans un fichier FILE *file; file = fopen(filename, "w+"); fprintf(file, "#imgX\timgY\tdistX\tdistY\n"); // for (int i=0; i