added check for connected kinect

This commit is contained in:
SND\floAr_cp 2011-12-29 21:12:43 +00:00
parent a32da95cde
commit 115de0957f

View File

@ -60,7 +60,7 @@ using ANX.Framework.Input.MotionSensing;
namespace ANX.InputDevices.Windows.Kinect namespace ANX.InputDevices.Windows.Kinect
{ {
public class Kinect : IMotionSensingDevice public class Kinect : IMotionSensingDevice
{ {
#region Private Members #region Private Members
@ -74,17 +74,23 @@ namespace ANX.InputDevices.Windows.Kinect
public Kinect() public Kinect()
{ {
pNui = Runtime.Kinects[0]; if (Runtime.Kinects.Count > 0)
pNui.Initialize(RuntimeOptions.UseDepthAndPlayerIndex | RuntimeOptions.UseSkeletalTracking | RuntimeOptions.UseColor); {
pNui.SkeletonEngine.TransformSmooth = true; pNui = Runtime.Kinects[0];
pNui.Initialize(RuntimeOptions.UseDepthAndPlayerIndex | RuntimeOptions.UseSkeletalTracking | RuntimeOptions.UseColor);
pNui.SkeletonEngine.TransformSmooth = true;
}
else
{
throw new Exception("No Kinect was detected, please connect it to your Computer before running this program and make sure you install the Kinect SDK from Microsoft.");
}
this.cache = new Vector3[21]; this.cache = new Vector3[21];
//init for the first time //init for the first time
for (int i = 0; i < 21; ++i) for (int i = 0; i < 21; ++i)
{ {
this.cache[i]=Vector3.Zero; this.cache[i] = Vector3.Zero;
} }
//Added parameters which where used in our Kinect project //Added parameters which where used in our Kinect project
var parameters = new TransformSmoothParameters var parameters = new TransformSmoothParameters
@ -98,15 +104,15 @@ namespace ANX.InputDevices.Windows.Kinect
pNui.SkeletonEngine.SmoothParameters = parameters; pNui.SkeletonEngine.SmoothParameters = parameters;
try try
{ {
pNui.VideoStream.Open(ImageStreamType.Video, 2, ImageResolution.Resolution640x480, ImageType.Color); pNui.VideoStream.Open(ImageStreamType.Video, 2, ImageResolution.Resolution640x480, ImageType.Color);
pNui.DepthStream.Open(ImageStreamType.Depth, 2, ImageResolution.Resolution320x240, ImageType.DepthAndPlayerIndex); pNui.DepthStream.Open(ImageStreamType.Depth, 2, ImageResolution.Resolution320x240, ImageType.DepthAndPlayerIndex);
} }
catch (InvalidOperationException) catch (InvalidOperationException)
{ {
// Display error message; omitted for space return; // Display error message; omitted for space return;
} }
//lastTime = DateTime.Now; //lastTime = DateTime.Now;
pNui.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(pNui_SkeletonFrameReady); pNui.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(pNui_SkeletonFrameReady);
@ -165,7 +171,7 @@ namespace ANX.InputDevices.Windows.Kinect
foreach (Joint joint in data.Joints) foreach (Joint joint in data.Joints)
{ {
if (joint.Position.W < 0.6f) return;// Quality check if (joint.Position.W < 0.6f) return;// Quality check
cache[(int)joint.ID] = toVector3(joint.Position); cache[(int)joint.ID] = toVector3(joint.Position);
} }
} }
} }
@ -175,10 +181,10 @@ namespace ANX.InputDevices.Windows.Kinect
//evtl -z //evtl -z
return new Vector3(vector.X, vector.Y, vector.Z); return new Vector3(vector.X, vector.Y, vector.Z);
} }
public MotionSensingDeviceState GetState() public MotionSensingDeviceState GetState()
{ {
return new MotionSensingDeviceState(rgb, depth, cache[0], cache[1], cache[2], cache[3], cache[4], cache[5], cache[6], cache[7], cache[8], cache[9], cache[10],cache[11], cache[12], cache[13], cache[14], cache[15], cache[16], cache[17], cache[18], cache[19], cache[20]); return new MotionSensingDeviceState(rgb, depth, cache[0], cache[1], cache[2], cache[3], cache[4], cache[5], cache[6], cache[7], cache[8], cache[9], cache[10], cache[11], cache[12], cache[13], cache[14], cache[15], cache[16], cache[17], cache[18], cache[19], cache[20]);
} }