middleware/src/Tracking/KinectMain.cs
changeset 5 d40f84d77db4
parent 3 92f19af39024
child 6 93dfb08dcc97
equal deleted inserted replaced
4:f4e52a4c34b3 5:d40f84d77db4
    94         public static event SwipeHandler SwipeEvent;
    94         public static event SwipeHandler SwipeEvent;
    95         //L'événement push.
    95         //L'événement push.
    96         public static event PushHandler PushEvent;
    96         public static event PushHandler PushEvent;
    97         //L'événement jump.
    97         //L'événement jump.
    98         public static event JumpHandler JumpEvent;
    98         public static event JumpHandler JumpEvent;
    99             
    99 
   100         //Voici les ID des noeuds d'un squelette.
       
   101         public int hipCenterID = 0, spineID = 1, shoulderCenterID = 2, headID = 3;
       
   102         public int shoulderLeftID = 4, elbowLeftID = 5, wristLeftID = 6, handLeftID = 7;
       
   103         public int shoulderRightID = 8, elbowRightID = 9, wristRightID = 10, handRightID = 11;
       
   104         public int hipLeftID = 12, kneeLeftID = 13, ankleLeftID = 14, footLeftID = 15;
       
   105         public int hipRightID = 16, kneeRightID = 17, ankleRightID = 18, footRightID = 19;
       
   106         
       
   107         private string connexionHost;
   100         private string connexionHost;
   108         private int connexionPort;
   101         private int connexionPort;
   109 
   102 
   110         /*
   103         /*
   111         *  Initialisation de la classe principale.
   104         *  Initialisation de la classe principale.
   112         *  Affiche l'écran de debug dans lequel on voit la distance à la Kinect,
   105         *  Affiche l'écran de debug dans lequel on voit la distance à la Kinect,
   113         *  les mains détectées et le squelette de l'utilisateur.
   106         *  les mains détectées et le squelette de l'utilisateur.
   114         */
   107         */
   115         public KinectMain()
   108         public KinectMain()
   116         {
   109         {
   117             //Si on n'a pas fait appel au gestionnaire de ressources avant, on le fait là.
   110             //On fait appel au gestionnaire de ressources.
   118             if(rm == null)
   111             rm = new ResourceManager("Trakers.Properties.resources", Assembly.GetExecutingAssembly());
   119                 rm = new ResourceManager("Trakers.Properties.resources", Assembly.GetExecutingAssembly());
       
   120             //On crée la fenêtre de debug.
   112             //On crée la fenêtre de debug.
   121             debug = new Debug.DebugWindow(this);
   113             debug = new Debug.DebugWindow(this);
   122             
   114             
   123             //On crée les détecteurs de gestes.
   115             //On crée les détecteurs de gestes.
   124             swipeDetector = new SwipeDetector(debug);
   116             swipeDetector = new SwipeDetector(debug);
   218             //Fonction appelée lorsque l'utilisateur effectue un Jump.
   210             //Fonction appelée lorsque l'utilisateur effectue un Jump.
   219             JumpListener jumpListener = new JumpListener();
   211             JumpListener jumpListener = new JumpListener();
   220             JumpEvent += new JumpHandler(jumpListener.ShowOnScreen);
   212             JumpEvent += new JumpHandler(jumpListener.ShowOnScreen);
   221 
   213 
   222             //On connecte le serveur à l'adresse locale sur le port 80.
   214             //On connecte le serveur à l'adresse locale sur le port 80.
   223             server = new Server(connexionHost, connexionPort, timerElapsing);
   215             server = new Server(connexionHost, connexionPort, timerElapsing, debug);
   224         }
   216         }
   225 
   217 
   226         /*
   218         /*
   227         *  Fermeture du sensor de la Kinect.
   219         *  Fermeture du sensor de la Kinect.
   228         */
   220         */
   328             
   320             
   329             //Si ce squelette est tracké (donc suivi et reconnu par la camera)
   321             //Si ce squelette est tracké (donc suivi et reconnu par la camera)
   330             if (first.TrackingState == SkeletonTrackingState.Tracked)
   322             if (first.TrackingState == SkeletonTrackingState.Tracked)
   331             {
   323             {
   332                 //Ensemble des noeuds du squelette.
   324                 //Ensemble des noeuds du squelette.
   333                 Joint hipCenter = getJoint(first, hipCenterID), spine = getJoint(first, spineID), shoulderCenter = getJoint(first, shoulderCenterID), head = getJoint(first, headID);
   325                 Joint hipCenter = getJoint(first, JointType.HipCenter), spine = getJoint(first, JointType.Spine), shoulderCenter = getJoint(first, JointType.ShoulderCenter), head = getJoint(first, JointType.Head);
   334                 Joint shoulderLeft = getJoint(first, shoulderLeftID), elbowLeft = getJoint(first, elbowLeftID), wristLeft = getJoint(first, wristLeftID), handLeft = getJoint(first, handLeftID);
   326                 Joint shoulderLeft = getJoint(first, JointType.ShoulderLeft), elbowLeft = getJoint(first, JointType.ElbowLeft), wristLeft = getJoint(first, JointType.WristLeft), handLeft = getJoint(first, JointType.HandLeft);
   335                 Joint shoulderRight = getJoint(first, shoulderRightID), elbowRight = getJoint(first, elbowRightID), wristRight = getJoint(first, wristRightID), handRight = getJoint(first, handRightID);
   327                 Joint shoulderRight = getJoint(first, JointType.ShoulderRight), elbowRight = getJoint(first, JointType.ElbowRight), wristRight = getJoint(first, JointType.WristRight), handRight = getJoint(first, JointType.HandRight);
   336                 Joint hipLeft = getJoint(first, hipLeftID), kneeLeft = getJoint(first, kneeLeftID), ankleLeft = getJoint(first, ankleLeftID), footLeft = getJoint(first, footLeftID);
   328                 Joint hipLeft = getJoint(first, JointType.HipLeft), kneeLeft = getJoint(first, JointType.KneeLeft), ankleLeft = getJoint(first, JointType.AnkleLeft), footLeft = getJoint(first, JointType.FootLeft);
   337                 Joint hipRight = getJoint(first, hipRightID), kneeRight = getJoint(first, kneeRightID), ankleRight = getJoint(first, ankleRightID), footRight = getJoint(first, footRightID);
   329                 Joint hipRight = getJoint(first, JointType.HipRight), kneeRight = getJoint(first, JointType.KneeRight), ankleRight = getJoint(first, JointType.AnkleRight), footRight = getJoint(first, JointType.FootRight);
   338 
   330 
   339                 //On construit l'historique des postures.
   331                 //On construit l'historique des postures.
   340                 List<Joint> joints = new List<Joint>();
   332                 List<Joint> joints = new List<Joint>();
   341                 joints.Clear();
   333                 joints.Clear();
   342                 joints.Insert(hipCenterID, hipCenter);
   334                 joints.Insert((int)JointType.HipCenter, hipCenter);
   343                 joints.Insert(spineID, spine);
   335                 joints.Insert((int)JointType.Spine, spine);
   344                 joints.Insert(shoulderCenterID, shoulderCenter);
   336                 joints.Insert((int)JointType.ShoulderCenter, shoulderCenter);
   345                 joints.Insert(headID, head);
   337                 joints.Insert((int)JointType.Head, head);
   346                 joints.Insert(shoulderLeftID, shoulderLeft);
   338                 joints.Insert((int)JointType.ShoulderLeft, shoulderLeft);
   347                 joints.Insert(elbowLeftID, elbowLeft);
   339                 joints.Insert((int)JointType.ElbowLeft, elbowLeft);
   348                 joints.Insert(wristLeftID, wristLeft);
   340                 joints.Insert((int)JointType.WristLeft, wristLeft);
   349                 joints.Insert(handLeftID, handLeft);
   341                 joints.Insert((int)JointType.HandLeft, handLeft);
   350                 joints.Insert(shoulderRightID, shoulderRight);
   342                 joints.Insert((int)JointType.ShoulderRight, shoulderRight);
   351                 joints.Insert(elbowRightID, elbowRight);
   343                 joints.Insert((int)JointType.ElbowRight, elbowRight);
   352                 joints.Insert(wristRightID, wristRight);
   344                 joints.Insert((int)JointType.WristRight, wristRight);
   353                 joints.Insert(handRightID, handRight);
   345                 joints.Insert((int)JointType.HandRight, handRight);
   354                 joints.Insert(hipLeftID, hipLeft);
   346                 joints.Insert((int)JointType.HipLeft, hipLeft);
   355                 joints.Insert(kneeLeftID, kneeLeft);
   347                 joints.Insert((int)JointType.KneeLeft, kneeLeft);
   356                 joints.Insert(ankleLeftID, ankleLeft);
   348                 joints.Insert((int)JointType.AnkleLeft, ankleLeft);
   357                 joints.Insert(footLeftID, footLeft);
   349                 joints.Insert((int)JointType.FootLeft, footLeft);
   358                 joints.Insert(hipRightID, hipRight);
   350                 joints.Insert((int)JointType.HipRight, hipRight);
   359                 joints.Insert(kneeRightID, kneeRight);
   351                 joints.Insert((int)JointType.KneeRight, kneeRight);
   360                 joints.Insert(ankleRightID, ankleRight);
   352                 joints.Insert((int)JointType.AnkleRight, ankleRight);
   361                 joints.Insert(footRightID, footRight);
   353                 joints.Insert((int)JointType.FootRight, footRight);
   362                 GestureDetector.UpdateSkeletonHistory(joints);
   354                 GestureDetector.UpdateSkeletonHistory(joints);
   363                 
   355                 
   364                 //On obtient sa distance à la Kinect.
   356                 //On obtient sa distance à la Kinect.
   365                 float distance = first.Position.Z;
   357                 float distance = first.Position.Z;
   366 
   358 
   435         }
   427         }
   436 
   428 
   437         /*
   429         /*
   438         *  Change l'échelle des coordonnées d'un noeud pour qu'en X et Y il corresponde à la résolution et en Z à la distance à la Kinect.
   430         *  Change l'échelle des coordonnées d'un noeud pour qu'en X et Y il corresponde à la résolution et en Z à la distance à la Kinect.
   439         */
   431         */
   440         public Joint getJoint(Skeleton ske, int jointID)
   432         public Joint getJoint(Skeleton ske, JointType jointID)
   441         {
   433         {
   442             return Coding4Fun.Kinect.Wpf.SkeletalExtensions.ScaleTo(ske.Joints.ElementAt(jointID), 600, 400, 0.75f, 0.75f);
   434             return Coding4Fun.Kinect.Wpf.SkeletalExtensions.ScaleTo(ske.Joints[jointID], 600, 400, 0.75f, 0.75f);
   443         }
   435         }
   444 
   436 
   445         /*
   437         /*
   446         *  Initialise l'événement et fait appel aux fonctions du listener quand la main gauche entre dans le champ.
   438         *  Initialise l'événement et fait appel aux fonctions du listener quand la main gauche entre dans le champ.
   447         */
   439         */
   514             {
   506             {
   515                 minDistHands = (float)double.Parse(ConfigurationManager.AppSettings["searchMinDistance"]);
   507                 minDistHands = (float)double.Parse(ConfigurationManager.AppSettings["searchMinDistance"]);
   516                 maxDistHands = (float)double.Parse(ConfigurationManager.AppSettings["searchMaxDistance"]);
   508                 maxDistHands = (float)double.Parse(ConfigurationManager.AppSettings["searchMaxDistance"]);
   517                 connexionHost = ConfigurationManager.AppSettings["connexionHost"];
   509                 connexionHost = ConfigurationManager.AppSettings["connexionHost"];
   518                 connexionPort = int.Parse(ConfigurationManager.AppSettings["connexionPort"]);
   510                 connexionPort = int.Parse(ConfigurationManager.AppSettings["connexionPort"]);
   519                 hipCenterID = int.Parse(ConfigurationManager.AppSettings["hipCenterID"]);
       
   520                 spineID = int.Parse(ConfigurationManager.AppSettings["spineID"]);
       
   521                 shoulderCenterID = int.Parse(ConfigurationManager.AppSettings["shoulderCenterID"]);
       
   522                 headID = int.Parse(ConfigurationManager.AppSettings["headID"]);
       
   523                 shoulderLeftID = int.Parse(ConfigurationManager.AppSettings["shoulderLeftID"]);
       
   524                 elbowLeftID = int.Parse(ConfigurationManager.AppSettings["elbowLeftID"]);
       
   525                 wristLeftID = int.Parse(ConfigurationManager.AppSettings["wristLeftID"]);
       
   526                 handLeftID = int.Parse(ConfigurationManager.AppSettings["handLeftID"]);
       
   527                 shoulderRightID = int.Parse(ConfigurationManager.AppSettings["shoulderRightID"]);
       
   528                 elbowRightID = int.Parse(ConfigurationManager.AppSettings["elbowRightID"]);
       
   529                 wristRightID = int.Parse(ConfigurationManager.AppSettings["wristRightID"]);
       
   530                 handRightID = int.Parse(ConfigurationManager.AppSettings["handRightID"]);
       
   531                 hipLeftID = int.Parse(ConfigurationManager.AppSettings["hipLeftID"]);
       
   532                 kneeLeftID = int.Parse(ConfigurationManager.AppSettings["kneeLeftID"]);
       
   533                 ankleLeftID = int.Parse(ConfigurationManager.AppSettings["ankleLeftID"]);
       
   534                 footLeftID = int.Parse(ConfigurationManager.AppSettings["footLeftID"]);
       
   535                 hipRightID = int.Parse(ConfigurationManager.AppSettings["hipRightID"]);
       
   536                 kneeRightID = int.Parse(ConfigurationManager.AppSettings["kneeRightID"]);
       
   537                 ankleRightID = int.Parse(ConfigurationManager.AppSettings["ankleRightID"]);
       
   538                 footRightID = int.Parse(ConfigurationManager.AppSettings["footRightID"]);
       
   539                 timerElapsing = int.Parse(ConfigurationManager.AppSettings["timerElapsing"]);
   511                 timerElapsing = int.Parse(ConfigurationManager.AppSettings["timerElapsing"]);
   540             }
   512             }
   541             catch (Exception)
   513             catch (Exception)
   542             {
   514             {
   543                 return false;
   515                 return false;
   548                 debug.ExceptionLbl.Content = rm.GetString("loadParametersIncorrect");
   520                 debug.ExceptionLbl.Content = rm.GetString("loadParametersIncorrect");
   549                 return false;
   521                 return false;
   550             }
   522             }
   551             return true;
   523             return true;
   552         }
   524         }
       
   525 
       
   526         /*
       
   527          * Met à jour les nouveaux paramètres dans la configuration.
       
   528          */
       
   529         public void updateParameters()
       
   530         {
       
   531             //On récupère la config.
       
   532             Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
       
   533             //On met à jour.
       
   534             config.AppSettings.Settings.Remove("searchMinDistance");
       
   535             config.AppSettings.Settings.Add("searchMinDistance", minDistHands.ToString());
       
   536             config.AppSettings.Settings.Remove("searchMaxDistance");
       
   537             config.AppSettings.Settings.Add("searchMaxDistance", maxDistHands.ToString());
       
   538             config.AppSettings.Settings.Remove("connexionHost");
       
   539             config.AppSettings.Settings.Add("connexionHost", connexionHost);
       
   540             config.AppSettings.Settings.Remove("connexionPort");
       
   541             config.AppSettings.Settings.Add("connexionPort", connexionPort.ToString());
       
   542             config.AppSettings.Settings.Remove("timerElapsing");
       
   543             config.AppSettings.Settings.Add("timerElapsing", timerElapsing.ToString());
       
   544 
       
   545             //Sauvegarde la configuration.
       
   546             config.Save(ConfigurationSaveMode.Modified);
       
   547             ConfigurationManager.RefreshSection("appSettings");
       
   548         }
       
   549 
       
   550         /*
       
   551          * Getters et setters des paramètres du Middleware.
       
   552          */
       
   553         public void setMinDistHands(float min)
       
   554         {
       
   555             minDistHands = min;
       
   556         }
       
   557         public void setMaxDistHands(float max)
       
   558         {
       
   559             maxDistHands = max;
       
   560         }
       
   561         public void setConnexionHost(String host)
       
   562         {
       
   563             connexionHost = host;
       
   564         }
       
   565         public void setConnexionPort(int port)
       
   566         {
       
   567             connexionPort = port;
       
   568         }
       
   569         public void setTimerElapsing(int time)
       
   570         {
       
   571             timerElapsing = time;
       
   572         }
       
   573 
       
   574         public float getMinDistHands()
       
   575         {
       
   576             return minDistHands;
       
   577         }
       
   578         public float getMaxDistHands()
       
   579         {
       
   580             return maxDistHands;
       
   581         }
       
   582         public String getConnexionHost()
       
   583         {
       
   584             return connexionHost;
       
   585         }
       
   586         public int getConnexionPort()
       
   587         {
       
   588             return connexionPort;
       
   589         }
       
   590         public int getTimerElapsing()
       
   591         {
       
   592             return timerElapsing;
       
   593         }
   553     }
   594     }
   554 }
   595 }