Esempio n. 1
0
        /// <summary>
        /// Set color of Kinect's LED.
        /// </summary>
        ///
        /// <param name="ledColor">LED color to set.</param>
        ///
        /// <exception cref="DeviceErrorException">Some error occurred with the device. Check error message.</exception>
        ///
        public void SetLedColor(LedColorOption ledColor)
        {
            lock (sync)
            {
                CheckDevice();

                int result = KinectNative.freenect_set_led(rawDevice, ledColor);

                if (result != 0)
                {
                    throw new DeviceErrorException("Failed setting LED color to " + ledColor + ". Error code: " + result);
                }
            }
        }
        // Shuts down the context and closes any open devices
        public static void ShutdownContext()
        {
            if (freenectContext != IntPtr.Zero)
            {
                // shutdown context
                int result = KinectNative.freenect_shutdown(freenectContext);
                if (result != 0)
                {
                    throw new ApplicationException("Could not shutdown freenect context. Error Code:" + result);
                }

                // Dispose pointer
                KinectNative.freenectContext = IntPtr.Zero;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Set motor's tilt value.
        /// </summary>
        ///
        /// <param name="angle">Tilt value to set, [-31, 30] degrees.</param>
        ///
        /// <exception cref="ArgumentOutOfRangeException">Motor tilt has to be in the [-31, 31] range.</exception>
        /// <exception cref="DeviceErrorException">Some error occurred with the device. Check error message.</exception>
        ///
        public void SetMotorTilt(int angle)
        {
            lock (sync)
            {
                CheckDevice();

                // check if value is in valid range
                if ((angle < -31) || (angle > 31))
                {
                    throw new ArgumentOutOfRangeException("angle", "Motor tilt has to be in the [-31, 31] range.");
                }

                int result = KinectNative.freenect_set_tilt_degs(rawDevice, angle);
                if (result != 0)
                {
                    throw new DeviceErrorException("Failed setting motor tilt. Error code: " + result);
                }
            }
        }
        // Initializes the freenect context
        private static void InitializeContext()
        {
            try
            {
                int result = freenect_init(ref KinectNative.freenectContext, IntPtr.Zero);

                if (result != 0)
                {
                    throw new ApplicationException("Could not initialize freenect context. Error Code:" + result);
                }

                // set callback for logging
                KinectNative.freenect_set_log_level(freenectContext, LogLevelOptions.Error);
                KinectNative.freenect_set_log_callback(freenectContext, logCallback);
            }
            catch (DllNotFoundException ex)
            {
                throw new DllNotFoundException("Freenect.dll library could not be found. Please "
                                               + "make sure that you also have installed LibUSB for your Kinect device.", ex);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Get initialized instance of the Kinect device.
        /// </summary>
        ///
        /// <param name="deviceID">ID of the Kinect device to get instance of, [0, <see cref="DeviceCount"/>),</param>
        ///
        /// <returns>Returns initialized Kinect device. Use <see cref="Dispose()"/> method
        /// when the device is no longer required.</returns>
        ///
        /// <exception cref="ArgumentException">There is no Kinect device with specified ID connected to the system.</exception>
        /// <exception cref="ConnectionFailedException">Failed connecting to the Kinect device specified ID.</exception>
        ///
        public static Kinect GetDevice(int deviceID)
        {
            if ((deviceID < 0) || (deviceID >= DeviceCount))
            {
                throw new ArgumentException("There is no Kinect device with specified ID connected to the system.");
            }

            bool   needToStartStatusThread = false;
            Kinect kinect = null;

            lock (openDevices)
            {
                needToStartStatusThread = (openDevices.Count == 0);

                // check if the device is open already
                if (!openDevices.ContainsKey(deviceID))
                {
                    IntPtr devicePointer = IntPtr.Zero;

                    // connect to Kinect device witht the specified ID
                    if (KinectNative.freenect_open_device(KinectNative.Context, ref devicePointer, deviceID) != 0)
                    {
                        throw new ConnectionFailedException("Failed connecting to the Kinect device with ID: " + deviceID);
                    }

                    openDevices.Add(deviceID, new DeviceContext(devicePointer));
                }

                openDevices[deviceID].ReferenceCounter++;
                kinect = new Kinect(openDevices[deviceID].Device, deviceID);
            }

            if (needToStartStatusThread)
            {
                StartStatusThread();
            }

            return(kinect);
        }
Esempio n. 6
0
        /// <summary>
        /// Stop video source.
        /// </summary>
        ///
        /// <remarks><para>The method stops the video source, so it no longer provides new video frames
        /// and does not consume any resources.</para>
        /// </remarks>
        ///
        public void Stop()
        {
            lock (sync)
            {
                lock (runningCameras)
                {
                    if (device != null)
                    {
                        bool deviceFailed = device.IsDeviceFailed(deviceID);

                        if (!deviceFailed)
                        {
                            KinectNative.freenect_stop_video(device.RawDevice);
                        }

                        device.Dispose();
                        device = null;
                        runningCameras.Remove(deviceID);

                        if (PlayingFinished != null)
                        {
                            PlayingFinished(this, (!deviceFailed) ?
                                            ReasonToFinishPlaying.StoppedByUser : ReasonToFinishPlaying.DeviceLost);
                        }
                    }

                    if (imageBuffer != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(imageBuffer);
                        imageBuffer = IntPtr.Zero;
                    }

                    videoCallback = null;
                }
            }
        }
Esempio n. 7
0
        // Kinect's status thread to process freenect's events
        private static void KinectStatusThread()
        {
            while (!stopEvent.WaitOne(100, false))
            {
                lock (openDevices)
                {
                    if (openDevices.Count != 0)
                    {
                        // update the status for each open device
                        foreach (DeviceContext deviceContext in openDevices.Values)
                        {
                            if (deviceContext.DeviceFailed)
                            {
                                continue;
                            }

                            if (KinectNative.freenect_update_tilt_state(deviceContext.Device) < 0)
                            {
                                deviceContext.DeviceFailed = true;
                                deviceContext.FireFailureHandlers();
                            }
                            else
                            {
                                // get updated device status
                                IntPtr ptr = KinectNative.freenect_get_tilt_state(deviceContext.Device);
                                deviceContext.TiltState = (KinectNative.TiltState)
                                                          System.Runtime.InteropServices.Marshal.PtrToStructure(ptr, typeof(KinectNative.TiltState));
                            }
                        }
                    }
                }

                // let the kinect library handle any pending stuff on the usb stream
                KinectNative.freenect_process_events_timeout0(KinectNative.Context);
            }
        }
Esempio n. 8
0
        private void Dispose(bool disposing)
        {
            lock (sync)
            {
                if (rawDevice == IntPtr.Zero)
                {
                    return;
                }

                bool needToStopStatusThread = false;

                lock (openDevices)
                {
                    // decrease reference counter and check if we need to close the device
                    if (--openDevices[deviceID].ReferenceCounter == 0)
                    {
                        if (!openDevices[deviceID].DeviceFailed)
                        {
                            KinectNative.freenect_close_device(rawDevice);
                        }
                        openDevices.Remove(deviceID);
                    }

                    needToStopStatusThread = (openDevices.Count == 0);
                }

                rawDevice = IntPtr.Zero;

                if (needToStopStatusThread)
                {
                    StopStatusThread();
                }
            }

            KinectNative.OnError -= new KinectNative.ErrorHandler(Kinect_OnError);
        }
Esempio n. 9
0
        /// <summary>
        /// Start video source.
        /// </summary>
        ///
        /// <remarks>Starts video source and returns execution to caller. Video camera will be started
        /// and will provide new video frames through the <see cref="NewFrame"/> event.</remarks>
        ///
        /// <exception cref="ArgumentException">The specified resolution is not supported for the selected
        /// mode of the Kinect video camera.</exception>
        /// <exception cref="ConnectionFailedException">Could not connect to Kinect's video camera.</exception>
        /// <exception cref="DeviceBusyException">Another connection to the specified video camera is already running.</exception>
        ///
        public void Start()
        {
            lock (sync)
            {
                lock (runningCameras)
                {
                    if (device == null)
                    {
                        bool success = false;

                        try
                        {
                            if (runningCameras.Contains(deviceID))
                            {
                                throw new DeviceBusyException("Another connection to the specified video camera is already running.");
                            }

                            // get Kinect device
                            device = Kinect.GetDevice(deviceID);

                            KinectNative.VideoCameraFormat dataFormat = KinectNative.VideoCameraFormat.RGB;

                            if (cameraMode == VideoCameraMode.Bayer)
                            {
                                dataFormat = KinectNative.VideoCameraFormat.Bayer;
                            }
                            else if (cameraMode == VideoCameraMode.InfraRed)
                            {
                                dataFormat = KinectNative.VideoCameraFormat.IR8Bit;
                            }

                            // find video format parameters
                            videoModeInfo = KinectNative.freenect_find_video_mode(resolution, dataFormat);

                            if (videoModeInfo.IsValid == 0)
                            {
                                throw new ArgumentException("The specified resolution is not supported for the selected mode of the Kinect video camera.");
                            }

                            // set video format
                            if (KinectNative.freenect_set_video_mode(device.RawDevice, videoModeInfo) != 0)
                            {
                                throw new VideoException("Could not switch to the specified video format.");
                            }

                            // allocate video buffer and provide it freenect
                            imageBuffer = Marshal.AllocHGlobal((int)videoModeInfo.Bytes);
                            KinectNative.freenect_set_video_buffer(device.RawDevice, imageBuffer);

                            // set video callback
                            videoCallback = new KinectNative.FreenectVideoDataCallback(HandleDataReceived);
                            KinectNative.freenect_set_video_callback(device.RawDevice, videoCallback);

                            // start the camera
                            if (KinectNative.freenect_start_video(device.RawDevice) != 0)
                            {
                                throw new ConnectionFailedException("Could not start video stream.");
                            }

                            success = true;
                            runningCameras.Add(deviceID);

                            device.AddFailureHandler(deviceID, Stop);
                        }
                        finally
                        {
                            if (!success)
                            {
                                if (device != null)
                                {
                                    device.Dispose();
                                    device = null;
                                }

                                if (imageBuffer != IntPtr.Zero)
                                {
                                    Marshal.FreeHGlobal(imageBuffer);
                                    imageBuffer = IntPtr.Zero;
                                }
                            }
                        }
                    }
                }
            }
        }