Guest User

Dataglove

a guest
Jun 5th, 2021
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.22 KB | None | 0 0
  1. // Code Processing Dataglove Pierre,Feuille, Ciseaux :
  2.  
  3.  
  4. /* Librairie pour la réalisation 3D et la communication série */
  5. import peasy.*;    // Librairie pour la fenêtre 3D
  6. import processing.serial.*;    // Librairie pour la communication série avec l'ESP32
  7. /* VARIABLES */
  8. float d1p1;
  9. float d1p2;
  10. float d1p3;
  11. float d2p1;
  12. float d2p2;
  13. float d2p3;
  14. float d3p1;
  15. float d3p2;
  16. float d3p3;
  17. float d4p1;
  18. float d4p2;
  19. float d4p3;
  20. float d5p1;
  21. float d5p2;
  22. float d5p3;
  23.  
  24. Serial myPort;      // déclaration de la variable 'myPort' pour la communication série
  25. String data_transmise;
  26.  
  27. int valeurs[];  //pour stocker les valeurs reçues
  28.  
  29. float angle_1;
  30. float angle_2;
  31. float angle_3;
  32. float angle_4;
  33. float angle_5;
  34.  
  35. PGraphics dessin;
  36. PeasyCam cam;
  37.  
  38. int score = 0;    //initilaisation du score
  39.  
  40. void setup() {
  41.   size(1000, 1000, P3D);  // Définir la taille de la fenêtre, P3D (traitement 3D): rendu graphique 3D  
  42.  
  43.   myPort  =  new Serial (this, "/dev/cu.Dataglove-ESP32SPP", 115200); // Config du port COM de l'ESP32
  44.   myPort.bufferUntil ( '\n' );   // Reception des données via ce port COM
  45.  
  46.   dessin = createGraphics(1000, 700, P3D); // création d'un objet PGraphics
  47.   cam = new PeasyCam(this, dessin, 1500); // permet de créer un objet caméra pour pouvoir faire tourner l'objet PGraphics
  48.  
  49.   cam.reset(); // permet de remettre la camera aux parametres de départ
  50.  
  51.  
  52. }
  53.  
  54. void serialEvent  (Serial myPort) {
  55.  
  56.   data_transmise  =   myPort.readStringUntil ( '\n' )  ;  // récupération de la chaine du port COM
  57.  
  58.   if (data_transmise != null) {    //vérification de la trame
  59.     data_transmise = trim(data_transmise);
  60.     valeurs = int(split(data_transmise, ','));
  61.  
  62.     for (int i = 0; i < 5; i++) {
  63.       print(valeurs[i] + ",");  //affichage dans la console des valeurs reçues
  64.     }
  65.  
  66.     angle_1= valeurs[0];
  67.     angle_2= valeurs[1];
  68.     angle_3= valeurs[2];
  69.     angle_4= valeurs[3];
  70.     angle_5= valeurs[4];
  71.     println();        // retour à la ligne dans la console :
  72.   }
  73. }
  74.  
  75. /* Fonction permettant d'afficher du contenu dans la fenêtre */
  76. void draw() {
  77.  
  78.   background(200); // couleur de fond
  79.  
  80.  
  81.   float fingerThumbMin = 2800;    // extremums des valeurs des flexomètres
  82.   float fingerIndexMin = 1200;
  83.   float fingerMiddleMin = 1550;
  84.   float fingerRingMin = 1400;
  85.   float fingerLittleMin = 2700;
  86.  
  87.   float fingerThumbMax = 3200;
  88.   float fingerIndexMax = 2900;
  89.   float fingerMiddleMax = 2800;
  90.   float fingerRingMax = 2600;
  91.   float fingerLittleMax = 3400;
  92.  
  93.  
  94.   float angleThumb = map(angle_1, fingerThumbMin, fingerThumbMax, PI/4, 0);    //map pour transformer des valeurs numériques en angles (rad)
  95.   float angleIndex = map(angle_2, fingerIndexMin, fingerIndexMax, PI/4, 0);
  96.   float angleMiddle = map(angle_3, fingerMiddleMin, fingerMiddleMax, PI/4, 0);
  97.   float angleRing = map(angle_4, fingerRingMin, fingerRingMax, PI/4, 0);
  98.   float angleLittle = map(angle_5, fingerLittleMin, fingerLittleMax, PI/4, 0);
  99.  
  100.  
  101.  
  102.   d1p1=4*(angleMiddle)-2.7468; // Majeur
  103.   d1p2=3*(angleMiddle)-1.6613;
  104.   d1p3=2*(angleMiddle)-1.0813;
  105.  
  106.   d2p1=4*(angleRing)-2.7468;    //Annulaire
  107.   d2p2=3*(angleRing)-1.6613;
  108.   d2p3=2*(angleRing)-1.0813;
  109.  
  110.   d3p1=4*(angleIndex)-2.7468;    //Index
  111.   d3p2=3*(angleIndex)-1.6613;
  112.   d3p3=2*(angleIndex)-1.0813;
  113.  
  114.   d4p1=4*(angleLittle)-2.7468;    //Auriculaire
  115.   d4p2=3*(angleLittle)-1.6613;
  116.   d4p3=2*(angleLittle)-1.0813;
  117.  
  118.   d5p1=4*(angleThumb)-2.7468;  //Pouce
  119.   d5p2=2*(angleThumb)-1.6613;
  120.  
  121.  
  122.   /* appel de la fonction pour créer et dessiner la main */
  123.   graphMain( d1p1, d1p2, d1p3, d2p1, d2p2, d2p3, d3p1, d3p2, d3p3, d4p1, d4p2, d4p3, d5p1, d5p2);    
  124.  
  125.   image(dessin, 100, 100); // affichage de la main en 3D
  126.  
  127.   /*
  128.        affichage de la valeur des 5 capteurs en dessous de la main
  129.   */
  130.  
  131.   textSize(16); // taille du texte
  132.   text("Pouce: ", 20, 650);
  133.   text(angleThumb, 120, 650);
  134.  
  135.   text("Index : ", 300, 650);
  136.   text(angleIndex, 400, 650);
  137.  
  138.   text("Majeur : ", 600, 650);
  139.   text(angleMiddle, 700, 650);
  140.  
  141.   text("Annulaire : ", 20, 700);
  142.   text(angleRing, 120, 700);
  143.  
  144.   text("Auriculaire : ", 300, 700);
  145.   text(angleLittle, 400, 700);
  146.  
  147.   fill(0, 102, 153);
  148.  
  149.   //-------------------Rock,Paper,Scisors---------------------------
  150.  
  151.  
  152.   int coup = -1;
  153.  
  154.   if (angleThumb<PI/16 && angleIndex<PI/16 && angleMiddle<PI/16 && angleRing<PI/16 && angleLittle<PI/16 )    //pierre
  155.   {
  156.     textSize(35); // taille du texte
  157.     text("Pierre! ", 20, 780);
  158.     coup=0;
  159.   }
  160.   if (angleThumb>PI/8 && angleIndex>PI/8 && angleMiddle>PI/8 && angleRing>PI/8 && angleLittle>PI/8 )  //feuille
  161.   {
  162.     textSize(35); // taille du texte
  163.     text("Feuille! ", 20, 780);
  164.     coup=1;
  165.   }
  166.  
  167.   if (angleThumb<PI/8 && angleIndex>PI/8 && angleMiddle>PI/8 && angleRing<PI/8 && angleLittle<PI/8 )    //ciseaux
  168.   {
  169.     textSize(35); // taille du texte
  170.     text("Ciseaux! ", 20, 780);
  171.     coup=2;
  172.   }
  173.     if (angleThumb<PI/16 && angleIndex<PI/16 && angleMiddle>PI/8 && angleRing<PI/16 && angleLittle<PI/16 )    //...(._.)
  174.   {
  175.     textSize(35); // taille du texte
  176.     text(" (ʘ‿ʘ) ╭∩╮ ", 20, 780);
  177.   }
  178.  
  179.    
  180.   if (mousePressed)     // Si on clique sur la fenêtre :
  181.   {
  182.    
  183.     int random=int(random(0, 2.99));    //l'ordinateur choisit
  184.     textSize(35); // taille du texte
  185.     text("Ordinateur :  ", 200, 780);
  186.     if(random == coup)    //si égalité
  187.     {
  188.  
  189.       textSize(35);
  190.       text("Pareil : Égalité",460,780);
  191.   }
  192.     if(random == 0 && coup == 1)    // comparaison entre l'ordinateur et notre mouvement
  193.         {
  194.       textSize(35);
  195.       text("Pierre : Gagné",460,780);
  196.       score++;
  197.     }
  198.         if(random == 0 && coup == 2)
  199.         {
  200.       textSize(35);
  201.       text("Pierre : Perdu",460,780);
  202.       score--;
  203.     }
  204.         if(random == 1 && coup == 0)
  205.         {
  206.       textSize(35);
  207.       text("Feuille : Perdu",460,780);
  208.       score--;
  209.     }
  210.         if(random == 1 && coup == 2)
  211.         {
  212.       textSize(35);
  213.       text("Feuille : Gagné",460,780);
  214.       score++;
  215.     }
  216.         if(random == 2 && coup == 0)
  217.         {
  218.       textSize(35);
  219.       text("Ciseaux : Gagné",460,780);
  220.       score++;
  221.     }
  222.         if(random == 2 && coup == 1)
  223.         {
  224.       textSize(35);
  225.       text("Ciseaux : Perdu",460,780);
  226.       score--;
  227.     }
  228.  
  229.   }
  230.  
  231.   textSize(16);
  232.   text("Score : ",900,780);
  233.   text(score,970,780);
  234.  
  235. }
  236.  
  237.  
  238. /* Fonction graphMain permettant de dessiner la main en fonction des valeurs d'angles */
  239. void graphMain(float d1p1, float d1p2, float d1p3, float d2p1, float d2p2, float d2p3, float d3p1, float d3p2, float d3p3, float d4p1, float d4p2, float d4p3, float d5p1, float d5p2 )
  240. {
  241.  
  242.   dessin.beginDraw();
  243.   dessin.background(200); // couleur de fond  
  244.   dessin.stroke(224, 144, 90); // couleur des arêtes l'objet
  245.   //dessin.fill(234, 154, 100); // couleur de l'objet
  246.  
  247.   // Majeur
  248.  
  249.   dessin.pushMatrix(); // cette fonction permet d'enregistrer les coordonnées actuel
  250.  
  251.   dessin.rotateX(d1p1); // rotation de la 1er phalange
  252.   /* creation de la premiere partie du doigt */
  253.   for (int i=0; i<25; i++) {
  254.     dessin.sphere(45);
  255.     dessin.translate(0, 10);
  256.   }
  257.   dessin.rotateX(d1p2); // rotation de la 2eme phalange
  258.   /* creation de la deuxieme partie du doigt */
  259.   for (int i=0; i<15; i++) {
  260.     dessin.sphere(45);
  261.     dessin.translate(0, 10);
  262.   }
  263.   dessin.rotateX(d1p3);// rotation de la 3eme phalange
  264.   /* creation de la 3eme partie du doigt */
  265.   for (int i=0; i<7; i++) {
  266.     dessin.sphere(45);
  267.     dessin.translate(0, 10);
  268.   }
  269.  
  270.   //Annulaire
  271.  
  272.   dessin.popMatrix(); // permet de restaurer les coordonnées antérieures
  273.   dessin.pushMatrix();
  274.  
  275.   dessin.rotateX(d2p1);
  276.   dessin.translate(100, 0 );
  277.   for (int i=0; i<20; i++) {
  278.     dessin.sphere(45);
  279.     dessin.translate(0, 10);
  280.   }
  281.   dessin.rotateX(d2p2);
  282.   for (int i=0; i<10; i++) {
  283.     dessin.sphere(45);
  284.     dessin.translate(0, 10);
  285.   }
  286.   dessin.rotateX(d2p3);
  287.   for (int i=0; i<5; i++) {
  288.     dessin.sphere(45);
  289.     dessin.translate(0, 10);
  290.   }
  291.  
  292.   //Index
  293.  
  294.   dessin.popMatrix();
  295.   dessin.pushMatrix();
  296.  
  297.   dessin.rotateX(d3p1);
  298.   dessin.translate(-100, 0);
  299.   for (int i=0; i<20; i++) {
  300.     dessin.sphere(45);
  301.     dessin.translate(0, 10);
  302.   }
  303.   dessin.rotateX(d3p2);
  304.   for (int i=0; i<10; i++) {
  305.     dessin.sphere(45);
  306.     dessin.translate(0, 10);
  307.   }
  308.   dessin.rotateX(d3p3);
  309.   for (int i=0; i<5; i++) {
  310.     dessin.sphere(45);
  311.     dessin.translate(0, 10);
  312.   }
  313.  
  314.   //Auriculaire
  315.  
  316.   dessin.popMatrix();
  317.   dessin.pushMatrix();
  318.  
  319.   dessin.rotateX(d4p1);
  320.   dessin.translate(200, 0);
  321.   for (int i=0; i<15; i++) {
  322.     dessin.sphere(45);
  323.     dessin.translate(0, 10);
  324.   }
  325.   dessin.rotateX(d4p2);
  326.   for (int i=0; i<10; i++) {
  327.     dessin.sphere(45);
  328.     dessin.translate(0, 10);
  329.   }
  330.   dessin.rotateX(d4p3);
  331.   for (int i=0; i<3; i++) {
  332.     dessin.sphere(45);
  333.     dessin.translate(0, 10);
  334.   }
  335.  
  336.   // Pouce
  337.  
  338.   dessin.popMatrix();    
  339.   dessin.translate(-200, 0, 200);
  340.   dessin.pushMatrix();
  341.  
  342.   dessin.rotateZ((PI/3)-d5p1);
  343.   dessin.rotateX(-d5p1/6);
  344.  
  345.   for (int i=0; i<15; i++) {
  346.     dessin.sphere(45);
  347.     dessin.translate(0, 10);
  348.   }
  349.  
  350.   dessin.rotateZ(-d5p2);
  351.   for (int i=0; i<5; i++) {
  352.     dessin.sphere(45);
  353.     dessin.translate(0, 10);
  354.   }
  355.  
  356.  
  357.   dessin.popMatrix();
  358.  
  359.   dessin.endDraw();
  360. }    // fin de la fonction graphMain
Advertisement
Add Comment
Please, Sign In to add comment