Browse Source

add gesture

iec-zsl 6 năm trước cách đây
mục cha
commit
3c90d4fe57

+ 1 - 0
.gitignore

@@ -4,3 +4,4 @@
 
 /KinectCar/bin/Debug
 /KinectCar/obj/Debug
+/.vs/KinectCar/v15/Server/sqlite3

BIN
.vs/KinectCar/v15/.suo


+ 126 - 93
KinectCar/MainWindow.xaml.cs

@@ -131,9 +131,9 @@
             string clientId = Guid.NewGuid().ToString();
             _client.Connect(clientId, "yumin", "minbb.cn");
             // 订阅主题"" 消息质量为 2
-            _client.Subscribe(new string[] { "iot.time" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
+            _client.Subscribe(new string[] { "wifi/car" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
             // 发布消息到主题 "wifi/car" 消息质量为 2,不保留 
-            _client.Publish("iot.all", Encoding.UTF8.GetBytes("I'm C#"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
+            _client.Publish("wifi/car", Encoding.UTF8.GetBytes("I'm C#"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
         }
 
         private void Window_Closing(object sender, CancelEventArgs e)
@@ -176,117 +176,150 @@
                     dataReceived = true;
                 }
             }
-
+            //所有工作
             if (dataReceived)
             {
+
+                //Console.WriteLine("进入程序");
+
                 foreach (Body body in _bodies)
                 {
                     if (body.IsTracked)
                     {
                         Status.Content = "追踪到Body.";
+                        //Console.WriteLine(body.HandRightConfidence);
                         IReadOnlyDictionary<JointType, Joint> joints = body.Joints;
 
-                        Joint jointHandRight = joints[JointType.HandRight], jointShoulderRight = joints[JointType.ShoulderRight];
-                        CameraSpacePoint positionHandRight = jointHandRight.Position, positionShoulderRight = jointShoulderRight.Position;
-                        if (jointHandRight.TrackingState == TrackingState.Tracked &&
-                            jointShoulderRight.TrackingState == TrackingState.Tracked)
+                        //Console.WriteLine("追踪到body");
+                        //右手
+                        Joint jointHandRight = joints[JointType.HandRight];
+                        //右肩膀
+                        Joint jointShoulderRight = joints[JointType.ShoulderRight];
+                        //左手
+                        Joint jointHandLeft = joints[JointType.HandLeft];
+                        //左肩
+                        Joint jointShoulderLeft = joints[JointType.ShoulderLeft];
+                        // 脊柱中
+                        Joint jointSpineMid = joints[JointType.SpineBase];
+
+
+                        //右手空间坐标
+                        CameraSpacePoint positionHandRight = jointHandRight.Position;
+                        //左手空间坐标 
+                        CameraSpacePoint positionHandLeft = jointHandLeft.Position;
+                        //脊椎中空间坐标
+                        CameraSpacePoint positionSpineMid = jointSpineMid.Position;
+
+                        //右手坐标集
+                        float xHandRight = positionHandRight.X;
+                        float yHandRight = positionHandRight.Y;
+                        float zHandRight = positionHandRight.Z;
+
+                        //左手坐标集
+                        float xHandLeft = positionHandLeft.X;
+                        float yHandLeft = positionHandLeft.Y;
+                        float ZHandLeft = positionHandLeft.Z;
+
+                        //脊椎中坐标集
+                        float xSpineMid = positionSpineMid.X;
+                        float ySpineMid = positionSpineMid.Y;
+                        float ZSpineMid = positionSpineMid.Z;
+                        //追踪到所需目标。
+
+
+                        Console.WriteLine(body.HandRightState);
+                        Console.WriteLine(body.HandLeftState);
+                        //双手进入控制区域
+                        if (body.HandRightState == HandState.Closed && body.HandLeftState == HandState.Closed && yHandRight > ySpineMid && yHandLeft > xSpineMid)
                         {
-                            float xHandRight = positionHandRight.X, yHandRight = positionHandRight.Y;
-                            float xShoulderRight = positionShoulderRight.X, yShoulderRight = positionShoulderRight.Y;
-                            // 右手位置判定(基于右肩为原点且无深度检测)
-                            if (xHandRight >= 2 * xShoulderRight - Threshold && xHandRight <= 2 * xShoulderRight + Threshold && 
-                                yHandRight >= 2 * yShoulderRight - Threshold && yHandRight <= 2 * yShoulderRight + Threshold)
-                            {
-                                // 中 - 5
-                                if(_gesture != 5)
-                                {
-                                    _gesture = 5;
-                                    Console.WriteLine("中");
-                                }
-                            }
-                            else if(xHandRight > 2 * xShoulderRight - Threshold && xHandRight < 2 * xShoulderRight + Threshold && yHandRight > 2 * yShoulderRight + Threshold)
-                            {
-                                // 上 - 2
-                                if (_gesture != 2)
-                                {
-                                    _gesture = 2;
-                                    Console.WriteLine("上");
-                                }
-                            }
-                            else if (xHandRight > 2 * xShoulderRight - Threshold && xHandRight < 2 * xShoulderRight + Threshold && yHandRight < 2 * yShoulderRight + Threshold)
-                            {
-                                // 下 - 8
-                                if (_gesture != 8)
-                                {
-                                    _gesture = 8;
-                                    Console.WriteLine("下");
-                                }
-                            }
-                            else if (xHandRight < 2 * xShoulderRight - Threshold && yHandRight > 2 * yShoulderRight - Threshold && yHandRight < 2 * yShoulderRight + Threshold)
-                            {
-                                // 左 - 4
-                                if (_gesture != 4)
-                                {
-                                    _gesture = 4;
-                                    Console.WriteLine("左");
-                                }
-                            }
-                            else if (xHandRight > 2 * xShoulderRight + Threshold && yHandRight > 2 * yShoulderRight - Threshold && yHandRight < 2 * yShoulderRight + Threshold)
-                            {
-                                // 右 - 6
-                                if (_gesture != 6)
-                                {
-                                    _gesture = 6;
-                                    Console.WriteLine("右");
-                                }
-                            }
-                            else if (xHandRight > 2 * xShoulderRight + Threshold && yHandRight > 2 * yShoulderRight + Threshold)
+
+
+
+                            Console.WriteLine("前进");
+                            _client.Publish("wifi/car", Encoding.UTF8.GetBytes("go"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
+
+                            //角度计算
+
+                            double value = Math.Atan2((yHandRight - yHandLeft), (xHandRight - xHandLeft)) * 180 / Math.PI;
+
+                            Console.WriteLine("执行到这里了 {0}", value);
+                            // 20 < value < 80 
+                            if (20 < value && value < 90)
                             {
-                                // 右上 - 3
-                                if (_gesture != 3)
-                                {
-                                    _gesture = 3;
-                                    Console.WriteLine("右上");
-                                }
+                                Console.WriteLine("左前方行驶");
+                                _client.Publish("wifi/car", Encoding.UTF8.GetBytes("lef go"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                             }
-                            else if (xHandRight > 2 * xShoulderRight + Threshold && yHandRight < 2 * yShoulderRight - Threshold)
+
+                            //  < value < 170
+                            if (-90 < value && value < -20)
                             {
-                                // 右下 - 9
-                                if (_gesture != 9)
-                                {
-                                    _gesture = 9;
-                                    Console.WriteLine("右下");
-                                }
+                                Console.WriteLine("右前方行驶");
+                                _client.Publish("wifi/car", Encoding.UTF8.GetBytes("right go"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                             }
-                            else if (xHandRight < 2 * xShoulderRight - Threshold && yHandRight > 2 * yShoulderRight + Threshold)
+
+                        }
+
+
+
+
+                        if (body.HandRightState == HandState.Lasso && body.HandLeftState == HandState.Lasso && yHandRight > ySpineMid && yHandLeft > xSpineMid)
+                        {
+
+                            //角度计算
+                            _client.Publish("wifi/car", Encoding.UTF8.GetBytes("Reversing"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
+                            double value = Math.Atan2((yHandRight - yHandLeft), (xHandRight - xHandLeft)) * 180 / Math.PI;
+
+                            Console.WriteLine("执行到这里了 {0}", value);
+                            // 20 < value < 80 
+                            if (20 < value && value < 90)
                             {
-                                // 左上 - 1
-                                if (_gesture != 1)
-                                {
-                                    _gesture = 1;
-                                    Console.WriteLine("左上");
-                                }
+                                Console.WriteLine("右后方行驶");
+                                _client.Publish("wifi/car", Encoding.UTF8.GetBytes("Reversing right"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
+
                             }
-                            else if (xHandRight < 2 * xShoulderRight - Threshold && yHandRight < 2 * yShoulderRight - Threshold)
+
+                            //  < value < 170
+                            if (-90 < value && value < -20)
                             {
-                                // 左下 - 7
-                                if (_gesture != 7)
-                                {
-                                    _gesture = 7;
-                                    Console.WriteLine("左下");
-                                }
+                                Console.WriteLine("左后方行驶");
+                                _client.Publish("wifi/car", Encoding.UTF8.GetBytes("Reversing left"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                             }
-                            //Console.WriteLine("X = " + positionHead.X + ", Y = " + positionHead.Y + ", Z = " + positionHead.Z);
-                            //Console.WriteLine("X = " + positionHandRight.X + ", Y = " + positionHandRight.Y + ", Z = " + positionHandRight.Z);
+
                         }
 
-                        // 将关节点转换为深度(显示)空间
-                        //Dictionary<JointType, Point> jointPoints = new Dictionary<JointType, Point>();
-                        //foreach (JointType jointType in joints.Keys)
-                        //{
-                        //    DepthSpacePoint depthSpacePoint = coordinateMapper.MapCameraPointToDepthSpace(positionHead);
-                        //    jointPoints[jointType] = new Point(depthSpacePoint.X, depthSpacePoint.Y);
-                        //}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
                     }
                 }
             }

+ 70 - 0
KinectCar/obj/Release/App.g.cs

@@ -0,0 +1,70 @@
+#pragma checksum "..\..\App.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "F8DB598471CC3A35BC3752144FFE70D63377C4AA"
+//------------------------------------------------------------------------------
+// <auto-generated>
+//     此代码由工具生成。
+//     运行时版本:4.0.30319.42000
+//
+//     对此文件的更改可能会导致不正确的行为,并且如果
+//     重新生成代码,这些更改将会丢失。
+// </auto-generated>
+//------------------------------------------------------------------------------
+
+using KinectCar;
+using System;
+using System.Diagnostics;
+using System.Windows;
+using System.Windows.Automation;
+using System.Windows.Controls;
+using System.Windows.Controls.Primitives;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Ink;
+using System.Windows.Input;
+using System.Windows.Markup;
+using System.Windows.Media;
+using System.Windows.Media.Animation;
+using System.Windows.Media.Effects;
+using System.Windows.Media.Imaging;
+using System.Windows.Media.Media3D;
+using System.Windows.Media.TextFormatting;
+using System.Windows.Navigation;
+using System.Windows.Shapes;
+using System.Windows.Shell;
+
+
+namespace KinectCar {
+    
+    
+    /// <summary>
+    /// App
+    /// </summary>
+    public partial class App : System.Windows.Application {
+        
+        /// <summary>
+        /// InitializeComponent
+        /// </summary>
+        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
+        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
+        public void InitializeComponent() {
+            
+            #line 5 "..\..\App.xaml"
+            this.StartupUri = new System.Uri("MainWindow.xaml", System.UriKind.Relative);
+            
+            #line default
+            #line hidden
+        }
+        
+        /// <summary>
+        /// Application Entry Point.
+        /// </summary>
+        [System.STAThreadAttribute()]
+        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
+        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
+        public static void Main() {
+            KinectCar.App app = new KinectCar.App();
+            app.InitializeComponent();
+            app.Run();
+        }
+    }
+}
+

+ 1 - 0
KinectCar/obj/Release/KinectCar.csproj.CoreCompileInputs.cache

@@ -0,0 +1 @@
+ecd7965459d0a815d17117ca245789317c74aee0

+ 20 - 0
KinectCar/obj/Release/KinectCar_MarkupCompile.cache

@@ -0,0 +1,20 @@
+KinectCar
+
+
+winexe
+C#
+.cs
+F:\C#\KinectCar\KinectCar\obj\Release\
+KinectCar
+none
+false
+TRACE
+F:\C#\KinectCar\KinectCar\App.xaml
+11151548125
+
+5-2017746502
+15-1598475018
+MainWindow.xaml;
+
+False
+

BIN
KinectCar/obj/Release/MainWindow.baml


+ 102 - 0
KinectCar/obj/Release/MainWindow.g.cs

@@ -0,0 +1,102 @@
+#pragma checksum "..\..\MainWindow.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "757A187AD4062FA2001DBC890A3D17AED7580557"
+//------------------------------------------------------------------------------
+// <auto-generated>
+//     此代码由工具生成。
+//     运行时版本:4.0.30319.42000
+//
+//     对此文件的更改可能会导致不正确的行为,并且如果
+//     重新生成代码,这些更改将会丢失。
+// </auto-generated>
+//------------------------------------------------------------------------------
+
+using System;
+using System.Diagnostics;
+using System.Windows;
+using System.Windows.Automation;
+using System.Windows.Controls;
+using System.Windows.Controls.Primitives;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Ink;
+using System.Windows.Input;
+using System.Windows.Markup;
+using System.Windows.Media;
+using System.Windows.Media.Animation;
+using System.Windows.Media.Effects;
+using System.Windows.Media.Imaging;
+using System.Windows.Media.Media3D;
+using System.Windows.Media.TextFormatting;
+using System.Windows.Navigation;
+using System.Windows.Shapes;
+using System.Windows.Shell;
+
+
+namespace KinectCar {
+    
+    
+    /// <summary>
+    /// MainWindow
+    /// </summary>
+    public partial class MainWindow : System.Windows.Window, System.Windows.Markup.IComponentConnector {
+        
+        
+        #line 9 "..\..\MainWindow.xaml"
+        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
+        internal System.Windows.Controls.Label Status;
+        
+        #line default
+        #line hidden
+        
+        private bool _contentLoaded;
+        
+        /// <summary>
+        /// InitializeComponent
+        /// </summary>
+        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
+        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
+        public void InitializeComponent() {
+            if (_contentLoaded) {
+                return;
+            }
+            _contentLoaded = true;
+            System.Uri resourceLocater = new System.Uri("/KinectCar;component/mainwindow.xaml", System.UriKind.Relative);
+            
+            #line 1 "..\..\MainWindow.xaml"
+            System.Windows.Application.LoadComponent(this, resourceLocater);
+            
+            #line default
+            #line hidden
+        }
+        
+        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
+        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
+        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
+        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")]
+        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
+        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
+        void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) {
+            switch (connectionId)
+            {
+            case 1:
+            
+            #line 7 "..\..\MainWindow.xaml"
+            ((KinectCar.MainWindow)(target)).Loaded += new System.Windows.RoutedEventHandler(this.Window_Loaded);
+            
+            #line default
+            #line hidden
+            
+            #line 7 "..\..\MainWindow.xaml"
+            ((KinectCar.MainWindow)(target)).Closing += new System.ComponentModel.CancelEventHandler(this.Window_Closing);
+            
+            #line default
+            #line hidden
+            return;
+            case 2:
+            this.Status = ((System.Windows.Controls.Label)(target));
+            return;
+            }
+            this._contentLoaded = true;
+        }
+    }
+}
+