|
@@ -4,38 +4,39 @@
|
|
|
using System.Collections.Generic;
|
|
|
using System.ComponentModel;
|
|
|
using System.Windows;
|
|
|
- using System.Windows.Media;
|
|
|
using Microsoft.Kinect;
|
|
|
using uPLibrary.Networking.M2Mqtt;
|
|
|
using System.Net;
|
|
|
using uPLibrary.Networking.M2Mqtt.Messages;
|
|
|
- using System.Text;/// <summary>
|
|
|
- /// MainWindow.xaml 的交互逻辑
|
|
|
- /// </summary>
|
|
|
- public partial class MainWindow : Window, INotifyPropertyChanged
|
|
|
+ using System.Text;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// MainWindow.xaml 的交互逻辑
|
|
|
+ /// </summary>
|
|
|
+ public partial class MainWindow : INotifyPropertyChanged
|
|
|
{
|
|
|
// 活动的 Kinect 传感器
|
|
|
- private KinectSensor kinectSensor = null;
|
|
|
+ private KinectSensor _kinectSensor = null;
|
|
|
// 坐标映射器将一种类型的点映射到另一种
|
|
|
private CoordinateMapper coordinateMapper = null;
|
|
|
// 身体框架渲染器
|
|
|
private BodyFrameReader bodyFrameReader = null;
|
|
|
// 身体的数组
|
|
|
- private Body[] bodies = null;
|
|
|
+ private Body[] _bodies = null;
|
|
|
// 骨骼的定义
|
|
|
- private List<Tuple<JointType, JointType>> bones;
|
|
|
+ private List<Tuple<JointType, JointType>> _bones;
|
|
|
// 显示宽度,高度(深度空间)
|
|
|
private int displayWidth, displayHeight;
|
|
|
// 要显示的当前状态文本
|
|
|
private string statusText = null;
|
|
|
// 阈值(1/2框)(单位:米)
|
|
|
- private float THRESHOLD = 0.15f;
|
|
|
+ private const float Threshold = 0.15f;
|
|
|
// 当前状态
|
|
|
- private int gesture = -1;
|
|
|
+ private int _gesture = -1;
|
|
|
|
|
|
private static string MQTT_BROKER_ADDRESS = "123.207.151.92";
|
|
|
- //创建客户端实例
|
|
|
- MqttClient client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS), 1600, false, null, null, MqttSslProtocols.None); //主机为IP时
|
|
|
+ // 创建客户端实例
|
|
|
+ MqttClient _client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS), 1600, false, null, null, MqttSslProtocols.None); //主机为IP时
|
|
|
|
|
|
// INotifyPropertyChangedPropertyChanged 事件允许窗口控件绑定到可更改的数据
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
@@ -65,53 +66,53 @@
|
|
|
public MainWindow()
|
|
|
{
|
|
|
// 目前支持一个传感器
|
|
|
- kinectSensor = KinectSensor.GetDefault();
|
|
|
+ _kinectSensor = KinectSensor.GetDefault();
|
|
|
// 获取坐标映射器
|
|
|
- coordinateMapper = kinectSensor.CoordinateMapper;
|
|
|
+ coordinateMapper = _kinectSensor.CoordinateMapper;
|
|
|
// 获取深度(显示)范围
|
|
|
- FrameDescription frameDescription = kinectSensor.DepthFrameSource.FrameDescription;
|
|
|
+ FrameDescription frameDescription = _kinectSensor.DepthFrameSource.FrameDescription;
|
|
|
// 得到关节空间的大小
|
|
|
displayWidth = frameDescription.Width;
|
|
|
displayHeight = frameDescription.Height;
|
|
|
// 打开渲染器的身体框架
|
|
|
- bodyFrameReader = kinectSensor.BodyFrameSource.OpenReader();
|
|
|
+ bodyFrameReader = _kinectSensor.BodyFrameSource.OpenReader();
|
|
|
// 骨骼定义为两个关节之间的线
|
|
|
- bones = new List<Tuple<JointType, JointType>>();
|
|
|
+ _bones = new List<Tuple<JointType, JointType>>();
|
|
|
// 躯干
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.Head, JointType.Neck));
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.Neck, JointType.SpineShoulder));
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.SpineShoulder, JointType.SpineMid));
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.SpineMid, JointType.SpineBase));
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.SpineShoulder, JointType.ShoulderRight));
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.SpineShoulder, JointType.ShoulderLeft));
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.SpineBase, JointType.HipRight));
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.SpineBase, JointType.HipLeft));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.Head, JointType.Neck));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.Neck, JointType.SpineShoulder));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.SpineShoulder, JointType.SpineMid));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.SpineMid, JointType.SpineBase));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.SpineShoulder, JointType.ShoulderRight));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.SpineShoulder, JointType.ShoulderLeft));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.SpineBase, JointType.HipRight));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.SpineBase, JointType.HipLeft));
|
|
|
// 右臂
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.ShoulderRight, JointType.ElbowRight));
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.ElbowRight, JointType.WristRight));
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.WristRight, JointType.HandRight));
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.HandRight, JointType.HandTipRight));
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.WristRight, JointType.ThumbRight));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.ShoulderRight, JointType.ElbowRight));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.ElbowRight, JointType.WristRight));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.WristRight, JointType.HandRight));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.HandRight, JointType.HandTipRight));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.WristRight, JointType.ThumbRight));
|
|
|
// 左臂
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.ShoulderLeft, JointType.ElbowLeft));
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.ElbowLeft, JointType.WristLeft));
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.WristLeft, JointType.HandLeft));
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.HandLeft, JointType.HandTipLeft));
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.WristLeft, JointType.ThumbLeft));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.ShoulderLeft, JointType.ElbowLeft));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.ElbowLeft, JointType.WristLeft));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.WristLeft, JointType.HandLeft));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.HandLeft, JointType.HandTipLeft));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.WristLeft, JointType.ThumbLeft));
|
|
|
// 右腿
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.HipRight, JointType.KneeRight));
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.KneeRight, JointType.AnkleRight));
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.AnkleRight, JointType.FootRight));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.HipRight, JointType.KneeRight));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.KneeRight, JointType.AnkleRight));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.AnkleRight, JointType.FootRight));
|
|
|
// 左腿
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.HipLeft, JointType.KneeLeft));
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.KneeLeft, JointType.AnkleLeft));
|
|
|
- bones.Add(new Tuple<JointType, JointType>(JointType.AnkleLeft, JointType.FootLeft));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.HipLeft, JointType.KneeLeft));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.KneeLeft, JointType.AnkleLeft));
|
|
|
+ _bones.Add(new Tuple<JointType, JointType>(JointType.AnkleLeft, JointType.FootLeft));
|
|
|
// 设置 IsAvailableChanged 事件通知程序
|
|
|
- kinectSensor.IsAvailableChanged += Sensor_IsAvailableChanged;
|
|
|
+ _kinectSensor.IsAvailableChanged += Sensor_IsAvailableChanged;
|
|
|
// 打开传感器
|
|
|
- kinectSensor.Open();
|
|
|
+ _kinectSensor.Open();
|
|
|
// 设置状态文本
|
|
|
- StatusText = kinectSensor.IsAvailable ? Properties.Resources.RunningStatusText : Properties.Resources.NoSensorStatusText;
|
|
|
+ StatusText = _kinectSensor.IsAvailable ? Properties.Resources.RunningStatusText : Properties.Resources.NoSensorStatusText;
|
|
|
// 在这个简单的例子中使用window对象作为视图模型
|
|
|
DataContext = this;
|
|
|
// 初始化窗口的组件(控件)
|
|
@@ -125,14 +126,14 @@
|
|
|
bodyFrameReader.FrameArrived += Reader_FrameArrived;
|
|
|
}
|
|
|
// 注册消息接收处理事件,还可以注册消息订阅成功、取消订阅成功、与服务器断开等事件处理函数
|
|
|
- client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
|
|
|
+ _client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
|
|
|
//生成客户端 ID 并连接服务器
|
|
|
string clientId = Guid.NewGuid().ToString();
|
|
|
- client.Connect(clientId, "yumin", "minbb.cn");
|
|
|
- // 订阅主题"wifi/car" 消息质量为 2
|
|
|
- client.Subscribe(new string[] { "wifi/car" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
|
|
|
+ _client.Connect(clientId, "yumin", "minbb.cn");
|
|
|
+ // 订阅主题"" 消息质量为 2
|
|
|
+ _client.Subscribe(new string[] { "iot.time" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
|
|
|
// 发布消息到主题 "wifi/car" 消息质量为 2,不保留
|
|
|
- client.Publish("wifi/car", Encoding.UTF8.GetBytes("I'm C#"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
|
|
|
+ _client.Publish("iot.all", Encoding.UTF8.GetBytes("I'm C#"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
|
|
|
}
|
|
|
|
|
|
private void Window_Closing(object sender, CancelEventArgs e)
|
|
@@ -143,15 +144,13 @@
|
|
|
bodyFrameReader = null;
|
|
|
}
|
|
|
|
|
|
- if (kinectSensor != null)
|
|
|
- {
|
|
|
- kinectSensor.Close();
|
|
|
- kinectSensor = null;
|
|
|
- }
|
|
|
- if (client != null)
|
|
|
+ if (_kinectSensor != null)
|
|
|
{
|
|
|
- client.Disconnect();
|
|
|
+ _kinectSensor.Close();
|
|
|
+ _kinectSensor = null;
|
|
|
}
|
|
|
+
|
|
|
+ _client?.Disconnect();
|
|
|
}
|
|
|
|
|
|
void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
|
|
@@ -168,23 +167,23 @@
|
|
|
{
|
|
|
if (bodyFrame != null)
|
|
|
{
|
|
|
- if (bodies == null)
|
|
|
+ if (_bodies == null)
|
|
|
{
|
|
|
- bodies = new Body[bodyFrame.BodyCount];
|
|
|
+ _bodies = new Body[bodyFrame.BodyCount];
|
|
|
}
|
|
|
// 第一次调用GetAndRefreshBodyData时,Kinect将在数组中分配每个Body。只要这些主体对象没有被释放并且在数组中没有设置为null,那么这些主体对象将被重用。
|
|
|
- bodyFrame.GetAndRefreshBodyData(bodies);
|
|
|
+ bodyFrame.GetAndRefreshBodyData(_bodies);
|
|
|
dataReceived = true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (dataReceived)
|
|
|
{
|
|
|
- foreach (Body body in bodies)
|
|
|
+ foreach (Body body in _bodies)
|
|
|
{
|
|
|
if (body.IsTracked)
|
|
|
{
|
|
|
- status.Content = "追踪到Body.";
|
|
|
+ Status.Content = "追踪到Body.";
|
|
|
IReadOnlyDictionary<JointType, Joint> joints = body.Joints;
|
|
|
|
|
|
Joint jointHandRight = joints[JointType.HandRight], jointShoulderRight = joints[JointType.ShoulderRight];
|
|
@@ -195,85 +194,85 @@
|
|
|
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)
|
|
|
+ if (xHandRight >= 2 * xShoulderRight - Threshold && xHandRight <= 2 * xShoulderRight + Threshold &&
|
|
|
+ yHandRight >= 2 * yShoulderRight - Threshold && yHandRight <= 2 * yShoulderRight + Threshold)
|
|
|
{
|
|
|
// 中 - 5
|
|
|
- if(gesture != 5)
|
|
|
+ if(_gesture != 5)
|
|
|
{
|
|
|
- gesture = 5;
|
|
|
+ _gesture = 5;
|
|
|
Console.WriteLine("中");
|
|
|
}
|
|
|
}
|
|
|
- else if(xHandRight > 2 * xShoulderRight - THRESHOLD && xHandRight < 2 * xShoulderRight + THRESHOLD && yHandRight > 2 * yShoulderRight + THRESHOLD)
|
|
|
+ else if(xHandRight > 2 * xShoulderRight - Threshold && xHandRight < 2 * xShoulderRight + Threshold && yHandRight > 2 * yShoulderRight + Threshold)
|
|
|
{
|
|
|
// 上 - 2
|
|
|
- if (gesture != 2)
|
|
|
+ if (_gesture != 2)
|
|
|
{
|
|
|
- gesture = 2;
|
|
|
+ _gesture = 2;
|
|
|
Console.WriteLine("上");
|
|
|
}
|
|
|
}
|
|
|
- else if (xHandRight > 2 * xShoulderRight - THRESHOLD && xHandRight < 2 * xShoulderRight + THRESHOLD && yHandRight < 2 * yShoulderRight + THRESHOLD)
|
|
|
+ else if (xHandRight > 2 * xShoulderRight - Threshold && xHandRight < 2 * xShoulderRight + Threshold && yHandRight < 2 * yShoulderRight + Threshold)
|
|
|
{
|
|
|
// 下 - 8
|
|
|
- if (gesture != 8)
|
|
|
+ if (_gesture != 8)
|
|
|
{
|
|
|
- gesture = 8;
|
|
|
+ _gesture = 8;
|
|
|
Console.WriteLine("下");
|
|
|
}
|
|
|
}
|
|
|
- else if (xHandRight < 2 * xShoulderRight - THRESHOLD && yHandRight > 2 * yShoulderRight - THRESHOLD && yHandRight < 2 * yShoulderRight + THRESHOLD)
|
|
|
+ else if (xHandRight < 2 * xShoulderRight - Threshold && yHandRight > 2 * yShoulderRight - Threshold && yHandRight < 2 * yShoulderRight + Threshold)
|
|
|
{
|
|
|
// 左 - 4
|
|
|
- if (gesture != 4)
|
|
|
+ if (_gesture != 4)
|
|
|
{
|
|
|
- gesture = 4;
|
|
|
+ _gesture = 4;
|
|
|
Console.WriteLine("左");
|
|
|
}
|
|
|
}
|
|
|
- else if (xHandRight > 2 * xShoulderRight + THRESHOLD && yHandRight > 2 * yShoulderRight - THRESHOLD && yHandRight < 2 * yShoulderRight + THRESHOLD)
|
|
|
+ else if (xHandRight > 2 * xShoulderRight + Threshold && yHandRight > 2 * yShoulderRight - Threshold && yHandRight < 2 * yShoulderRight + Threshold)
|
|
|
{
|
|
|
// 右 - 6
|
|
|
- if (gesture != 6)
|
|
|
+ if (_gesture != 6)
|
|
|
{
|
|
|
- gesture = 6;
|
|
|
+ _gesture = 6;
|
|
|
Console.WriteLine("右");
|
|
|
}
|
|
|
}
|
|
|
- else if (xHandRight > 2 * xShoulderRight + THRESHOLD && yHandRight > 2 * yShoulderRight + THRESHOLD)
|
|
|
+ else if (xHandRight > 2 * xShoulderRight + Threshold && yHandRight > 2 * yShoulderRight + Threshold)
|
|
|
{
|
|
|
// 右上 - 3
|
|
|
- if (gesture != 3)
|
|
|
+ if (_gesture != 3)
|
|
|
{
|
|
|
- gesture = 3;
|
|
|
+ _gesture = 3;
|
|
|
Console.WriteLine("右上");
|
|
|
}
|
|
|
}
|
|
|
- else if (xHandRight > 2 * xShoulderRight + THRESHOLD && yHandRight < 2 * yShoulderRight - THRESHOLD)
|
|
|
+ else if (xHandRight > 2 * xShoulderRight + Threshold && yHandRight < 2 * yShoulderRight - Threshold)
|
|
|
{
|
|
|
// 右下 - 9
|
|
|
- if (gesture != 9)
|
|
|
+ if (_gesture != 9)
|
|
|
{
|
|
|
- gesture = 9;
|
|
|
+ _gesture = 9;
|
|
|
Console.WriteLine("右下");
|
|
|
}
|
|
|
}
|
|
|
- else if (xHandRight < 2 * xShoulderRight - THRESHOLD && yHandRight > 2 * yShoulderRight + THRESHOLD)
|
|
|
+ else if (xHandRight < 2 * xShoulderRight - Threshold && yHandRight > 2 * yShoulderRight + Threshold)
|
|
|
{
|
|
|
// 左上 - 1
|
|
|
- if (gesture != 1)
|
|
|
+ if (_gesture != 1)
|
|
|
{
|
|
|
- gesture = 1;
|
|
|
+ _gesture = 1;
|
|
|
Console.WriteLine("左上");
|
|
|
}
|
|
|
}
|
|
|
- else if (xHandRight < 2 * xShoulderRight - THRESHOLD && yHandRight < 2 * yShoulderRight - THRESHOLD)
|
|
|
+ else if (xHandRight < 2 * xShoulderRight - Threshold && yHandRight < 2 * yShoulderRight - Threshold)
|
|
|
{
|
|
|
// 左下 - 7
|
|
|
- if (gesture != 7)
|
|
|
+ if (_gesture != 7)
|
|
|
{
|
|
|
- gesture = 7;
|
|
|
+ _gesture = 7;
|
|
|
Console.WriteLine("左下");
|
|
|
}
|
|
|
}
|
|
@@ -293,14 +292,14 @@
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- status.Content = "无";
|
|
|
+ Status.Content = "无";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void Sensor_IsAvailableChanged(object sender, IsAvailableChangedEventArgs e)
|
|
|
{
|
|
|
// 如果失败,设置状态文本
|
|
|
- StatusText = kinectSensor.IsAvailable ? Properties.Resources.RunningStatusText : Properties.Resources.SensorNotAvailableStatusText;
|
|
|
+ StatusText = _kinectSensor.IsAvailable ? Properties.Resources.RunningStatusText : Properties.Resources.SensorNotAvailableStatusText;
|
|
|
}
|
|
|
}
|
|
|
}
|