summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/ovision/ui/comm.cc
diff options
context:
space:
mode:
Diffstat (limited to '2005/i/robert/src/ovision/ui/comm.cc')
-rw-r--r--2005/i/robert/src/ovision/ui/comm.cc339
1 files changed, 339 insertions, 0 deletions
diff --git a/2005/i/robert/src/ovision/ui/comm.cc b/2005/i/robert/src/ovision/ui/comm.cc
new file mode 100644
index 0000000..906ad51
--- /dev/null
+++ b/2005/i/robert/src/ovision/ui/comm.cc
@@ -0,0 +1,339 @@
+// comm.cc - Classe Comm
+// robert - Programme du robot APBteam
+// Copyright (C) 2005 Olivier Gaillard
+
+
+/// @file comm.cc Interprete les commandes envoyes par l'interface UI et les executent
+
+#include <iostream>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <string>
+#include <sstream>
+
+#include "comm.hh"
+
+/// Constructeur
+/// @param *filename nom de l'image a utiliser
+Comm::Comm (const char *filename)
+ : yuvSwitch (0)
+{
+ // Copie du nom de l'image courante
+ strcpy(fname, filename);
+}
+
+void
+Comm::init ()
+{
+ // Ecriture du PID dans un fichier
+ long pid = getpid();
+ FILE *file = fopen("adjust.PID", "w+");
+ if (!file)
+ {
+ std::cerr << "Comm::Comm Error during file opening" << std::endl;
+ return;
+ }
+ fprintf(file, "%li\n", pid);
+ fclose(file);
+
+ live_ = new Live (NBIMG);
+ live_->init (fname);
+}
+
+/// Destructeur
+Comm::~Comm()
+{
+ close(fifo);
+}
+
+/// Synchronisation des poids locaux et de ceux du programme ui
+void
+Comm::sendNodes ()
+{
+ char buf[10];
+ for (int i=0; i < live_->oconfig->nnNbColor*3; i++)
+ {
+ sprintf(buf, "%u\n", live_->segm->node_[i]);
+ write(fifo, buf, 10);
+ }
+}
+
+/// Execute une commande venant de l'interface ui
+/// @param *buffer ligne de commande a analyser
+void
+Comm::executeUiCmds (const char *buffer)
+{
+ std::cout << "Commande reçue : " << buffer;
+ std::stringstream ss;
+ ss << buffer;
+ char cmd;
+ ss >> cmd;
+ // Detection de la commande recue
+ switch (cmd)
+ {
+ case 'i': // ouverture du fifo
+ std::cout << "Initialisation du fifo\n";
+ // Ouverture du fifo
+ fifo = open("uicmds", O_RDWR);
+ if (!fifo) {
+ std::cerr << "InitComm : Error during opening FIFO" << std::endl;
+ exit(1);
+ }
+ // On choppe le pid du prog ui situé dans le fichier uiPid.PID
+ FILE *file;
+ file = fopen("ui.PID", "r");
+ if (!file)
+ {
+ std::cerr << "Comm::ExecuteUiCmds : PID file not found" << std::endl;
+ exit(1);
+ }
+ char buf[10];
+ fgets(buf, 10, file);
+ uiPid = atol(buf);
+ fclose(file);
+ break;
+
+ case 'c': // changement de couleur
+ int numColor;
+ ss >> numColor;
+
+ // Changement des valeurs sur les poids du NN
+ int colorToChange;
+ for (int i=0; i<3; i++)
+ {
+ ss >> colorToChange;
+ live_->segm->node_[numColor*3+i] = colorToChange;
+ }
+ // On live_->segmente l'image puis on la stocke
+ live_->segmAndGroup ();
+ std::cout << "Couleur " << numColor << " changé aux valeurs " << (int)live_->segm->node_[numColor*3] << " " << (int)live_->segm->node_[numColor*3+1] << " " << (int)live_->segm->node_[numColor*3+2];
+ break;
+
+ case 'm': // mélanger couleurs
+ int nbColorToMerge, numIndexColor;
+
+ // Si on recoit une commande de remise a zero de l'index_
+ ss >> nbColorToMerge;
+ if (nbColorToMerge == -1)
+ {
+ for (int i=0; i<live_->oconfig->nnNbColor; i++)
+ live_->segm->index_[i] = i;
+ }
+ // Sinon on mix les couleurs
+ else
+ {
+ numIndexColor = live_->segm->index_[nbColorToMerge];
+
+ // On inscrit les changements dans l'index_
+ int numColorToMerge;
+ for (int i=1; i<nbColorToMerge; i++)
+ {
+ ss >> numColorToMerge;
+ live_->segm->index_[numColorToMerge] = numIndexColor;
+ }
+ }
+ // On live_->segmente l'image puis on la stocke
+ live_->segmAndGroup();
+ std::cout << nbColorToMerge << " colors merged to " << numIndexColor << std::endl;
+ break;
+
+ case 's': // isole une couleur
+ int numColorToShow;
+ ss >> numColorToShow;
+ // Cas ou toutes les couleurs doivent etre affiche
+ if (numColorToShow == -1)
+ live_->segmAndGroup ();
+ // Afficher seulement une couleur
+ else
+ live_->segmAndGroup (numColorToShow);
+ break;
+
+ case 'd': // supprimer couleur
+ int numColorToDel;
+ ss >> numColorToDel;
+ // Decalage de toutes les couleurs pour supprimer une couleur
+ unsigned char *pCur;
+ pCur = &live_->oconfig->node[numColorToDel*3];
+ for(int i=numColorToDel*3; i<live_->oconfig->nnNbColor*3; i++)
+ {
+ *(pCur) = *(pCur+3);
+ pCur++;
+ }
+ live_->oconfig->nnNbColor--;
+ // On refait le NN vu qu'il y a une couleur de moins
+ live_->segm->buildNN(live_->oconfig->nnNbColor, Segm::loadFromFile);
+ live_->segm->showNodes();
+ // On live_->segmente l'image puis on la stocke
+ live_->segmAndGroup();
+ break;
+
+ case 'r': // Reload l'image
+ int nbNNOutput;
+ ss >> nbNNOutput;
+ // Reattribution du nombre de sorties d'origine
+ if (nbNNOutput != -1) live_->oconfig->nnNbColor = nbNNOutput;
+ // Recharge du fichier des poids
+ live_->oconfig->loadNNFile("rc/poids");
+ // On refait le NN vu qu'il y a une couleur de moins
+ live_->segm->buildNN(live_->oconfig->nnNbColor, Segm::loadFromFile);
+ // On live_->segmente l'image puis on la stocke
+ live_->segmAndGroup();
+ // Si l'image a ete sauve on change l'image pivot
+ if (nbNNOutput == -1)
+ live_->rootPics ();
+ break;
+
+ case 'a': // Selectionne la couleur des objets à afficher
+ {
+ int obj;
+ ss >> obj;
+ std::cout << "Changement de l'objet affiché " << obj << std::endl;
+ live_->oconfig->uiGroupToDisplay = obj;
+ live_->segmAndGroup ();
+ live_->rootPics ();
+ }
+ break;
+
+ case 'b': // Selectionne la couleur des objets
+ {
+ int col;
+ std::string obj;
+ ss >> obj >> col;
+ for (std::vector<ObjectColor>::iterator iter=live_->oconfig->groupColor.begin ();
+ iter != live_->oconfig->groupColor.end (); ++iter)
+ {
+ if (obj == iter->label)
+ {
+ iter->color = col;
+ break;
+ }
+ }
+ live_->segmAndGroup ();
+ live_->rootPics ();
+ }
+ break;
+ /*
+ case 'g': // Selection du live_->groupe
+ ss >> live_->oconfig->live_->groupColor;
+ */
+ // On live_->segmente l'image puis on la stocke
+ live_->segmAndGroup();
+ break;
+
+
+ case 'n': // Image suivante
+ std::cout << ss.str () << std::endl;
+ // Copie du nom de l'image courante
+ ss >> fname;
+ // Ouverture de l'image pilote et stockage
+ live_->img->loadRaw(fname, Image::rgb, 360, 296);
+ memcpy(live_->data[0], live_->img->tabData_, sizeof(char)*live_->img->nbPixels_*3);
+ live_->tex[0] = loadImage(live_->img->width_, live_->img->height_, live_->data[0], live_->tex[0]);
+ // Conversion en YUV et stockage
+ ConvertImg convert;
+ convert.RGBtoYUV (*live_->img);
+ // NN live_->oconfigure en RGB ou YUV ?
+ if (!live_->oconfig->inputColor == Image::rgb) live_->img->loadRaw(fname, Image::rgb, 360, 296);
+ live_->segmAndGroup();
+ // Affichage de l'image pilote
+ live_->rootPics ();
+ break;
+
+
+ case 't': // Entraine le reseau
+ live_->segm->trainNN(live_->img);
+ // On live_->segmente l'image puis on la stocke
+ live_->segmAndGroup();
+ // Synchronisation des poids avec ui
+ sendNodes();
+ break;
+
+ case 'p': // Regenere les poids du reseau
+ int nbColor;
+ ss >> nbColor;
+ // Assignation du nombre de couleurs a isoler par le reseau
+ live_->oconfig->nnNbColor = nbColor;
+ // Genere aleatoire les poids
+ live_->segm->buildNN(live_->oconfig->nnNbColor, Segm::generate);
+ // Apprentissage
+ live_->segm->trainNN(live_->img);
+ // On live_->segmente l'image puis on la stocke
+ live_->segmAndGroup();
+ // Synchronisation des poids avec ui
+ sendNodes();
+ break;
+ /*
+ case 'v': // Envoie les points pour le calcul de la distance
+ int *pPoint;
+ pPoint = getPpoint();
+
+ for (int i=0; i<NB_POINTS_UI; i++)
+ if (pPoint[i*2] == -1)
+ {
+ cerr << "Comm::ExecuteUiCmds : Pas assez de points selectionnes\n";
+ return;
+ }
+
+ // for (int i = 0; i<NB_POINTS_UI; i++)
+ // {
+ // live_->oconfig->tabPoint[i*4+0] = pPoint[i*2];
+ // live_->oconfig->tabPoint[i*4+1] = pPoint[i*2+1];
+ // live_->oconfig->tabPoint[i*4+2] = 0;
+ // live_->oconfig->tabPoint[i*4+3] = 0;
+ // }
+
+ live_->oconfig->createDistFile("rc/dist", NB_POINTS_UI);
+ break;
+ *//*
+ case 'y':
+ yuvSwitch++;
+
+ // Conversion en YUV et stockage
+ live_->img->loadRaw(filename, Image::yuv, 360, 296);
+ if (yuvSwitch%3 == 1)
+ {
+ ConvertImg convert;
+ convert.YUVtoRGB (live_->img->;
+ }
+ else if (yuvSwitch%3 == 2)
+ {
+ ConvertImg convert;
+ convert.RGBtoYUV (live_->img->;
+ }
+ delete[] live_->img->eft[1];
+ live_->img->eft[1] = new unsigned char[live_->img->nbPixels_*3];
+ memcpy(live_->img->eft[1], live_->img->tabData_, sizeof(char)*live_->img->nbPixels_*3);
+ live_->tex[3] = loadImage(live_->img->width_, live_->img->height_, live_->img->eft[1], live_->tex[3]);
+ live_->img->loadRaw(fname, Image::yuv, 360, 296);
+ break;
+ */
+ case 'z':
+ live_->group->showZones();
+ break;
+ /*
+ case 'f':
+ {
+ ///TODO
+ char buf[20];
+ Zone *pCur = live_->group->zoneListBall;
+ write (fifo, "Groupes balles:\n", 20);
+ while(pCur) {
+ sprintf(buf, "%u %i %i %i %i\n", pCur->id, pCur->xmin, pCur->xmax, pCur->ymin, pCur->ymax);
+ write (fifo, buf, 20);
+ }
+
+ pCur = live_->group->zoneListGoal;
+ write (fifo, "Groupes poteaux:\n", 20);
+ while(pCur) {
+ sprintf(buf, "%u %i %i %i %i\n", pCur->id, pCur->xmin, pCur->xmax, pCur->ymin, pCur->ymax);
+ write (fifo, buf, 20);
+ }
+ write (fifo, "end\n", 20);
+ }
+ break;
+ */
+ }
+ drawGLScene();
+}
+