/// <summary>
        /// Closes the connection to this Kinect device
        /// </summary>
        public void Close()
        {
            // Stop Cameras
            if (this.VideoCamera.IsRunning)
            {
                this.VideoCamera.Stop();
            }
            if (this.DepthCamera.IsRunning)
            {
                this.DepthCamera.Stop();
            }

            // Close device
            int result = KinectNative.freenect_close_device(this.devicePointer);

            if (result != 0)
            {
                throw new Exception("Could not close connection to Kinect Device (ID=" + this.DeviceID + "). Error Code = " + result);
            }

            // Dispose of child instances
            this.LED           = null;
            this.Motor         = null;
            this.Accelerometer = null;
            this.VideoCamera   = null;
            this.DepthCamera   = null;

            // Unegister the device
            KinectNative.UnregisterDevice(this.devicePointer);

            // Not open anymore
            this.IsOpen = false;
        }
        /// <summary>
        /// Sets the color for the LED on the Kinect.
        /// </summary>
        /// <param name="color">
        /// Color value
        /// </param>
        private void SetLEDColor(LEDColor color)
        {
            int result = KinectNative.freenect_set_led(this.parentDevice.devicePointer, color);

            if (result != 0)
            {
                throw new Exception("Could not set color to " + color + ". Error Code:" + result);
            }
            this.color = color;
        }
Exemple #3
0
        /// <summary>
        /// Stops streaming video data from this camera
        /// </summary>
        public void Stop()
        {
            int result = KinectNative.freenect_stop_video(this.parentDevice.devicePointer);

            if (result != 0)
            {
                throw new Exception("Could not stop video stream. Error Code: " + result);
            }
            this.IsRunning = false;
        }
        /// <summary>
        /// Gets updated device status from the Kinect. This updates any properties in the
        /// child devices (Motor, LED, etc.)
        /// </summary>
        public void UpdateStatus()
        {
            // Ask for new device status
            KinectNative.freenect_update_tilt_state(this.devicePointer);

            // Get updated device status
            IntPtr ptr = KinectNative.freenect_get_tilt_state(this.devicePointer);

            this.cachedDeviceState = (FreenectTiltState)Marshal.PtrToStructure(ptr, typeof(FreenectTiltState));
        }
        /// <summary>
        /// Sets the direct access buffer for the DepthCamera.
        /// </summary>
        /// <param name="ptr">
        /// Pointer to the direct access data buffer for the DepthCamera.
        /// </param>
        protected override void SetDataBuffer(IntPtr ptr)
        {
            // Save data buffer
            this.dataBuffer = ptr;

            // Tell the kinect library about it
            KinectNative.freenect_set_depth_buffer(this.parentDevice.devicePointer, ptr);

            // update depth map
            this.UpdateNextFrameDepthMap();
        }
Exemple #6
0
        /// <summary>
        /// Handles image data from teh video camera
        /// </summary>
        /// <param name="device">
        /// A <see cref="IntPtr"/>
        /// </param>
        /// <param name="imageData">
        /// A <see cref="IntPtr"/>
        /// </param>
        /// <param name="timestamp">
        /// A <see cref="UInt32"/>
        /// </param>
        private static void HandleDataReceived(IntPtr device, IntPtr imageData, UInt32 timestamp)
        {
            // Figure out which device actually got this frame
            Kinect realDevice = KinectNative.GetDevice(device);

            // Calculate datetime from timestamp
            DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(timestamp);

            // Send out event
            realDevice.VideoCamera.DataReceived(realDevice, new DataReceivedEventArgs(dateTime, realDevice.VideoCamera.nextFrameImage));
        }
Exemple #7
0
        /// <summary>
        /// Sets the direct access buffer for the VideoCamera.
        /// </summary>
        /// <param name="ptr">
        /// Pointer to the direct access data buffer for the VideoCamera.
        /// </param>
        protected void SetDataBuffer(IntPtr ptr)
        {
            // Save data buffer
            this.dataBuffer = ptr;

            // Tell the kinect library about it
            KinectNative.freenect_set_video_buffer(this.parentDevice.devicePointer, ptr);

            // update image map
            this.UpdateNextFrameImageMap();
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="parent">
        /// Parent <see cref="Kinect"/> device that this video camera is part of
        /// </param>
        internal VideoCamera(Kinect parent) : base(parent)
        {
            // Update modes available for this video camera
            this.UpdateVideoModes();

            // Use the first mode by default
            this.Mode = this.Modes[0];

            // Setup callbacks
            KinectNative.freenect_set_video_callback(parent.devicePointer, this.DataCallback);
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="parent">
        /// Parent <see cref="Kinect"/> device that this depth camera is part of
        /// </param>
        internal DepthCamera(Kinect parent) : base(parent)
        {
            // Update lsit of available modes for this camera
            this.UpdateDepthModes();

            // Set the mode to the first available mode
            this.Mode = this.Modes[0];

            // Setup callbacks
            KinectNative.freenect_set_depth_callback(parent.devicePointer, this.DataCallback);
        }
        /// <summary>
        /// Initializes the freenect context
        /// </summary>
        private static void InitializeContext()
        {
            int result = KinectNative.freenect_init(ref KinectNative.freenectContext, IntPtr.Zero);

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

            // Set callbacks for logging
            KinectNative.freenect_set_log_callback(KinectNative.freenectContext, LogCallback);
        }
Exemple #11
0
        /// <summary>
        /// Sets the DepthCameras's data format. Support function for DepthCamera.DataFormat
        /// </summary>
        /// <param name="format">
        /// A <see cref="DepthCamera.DataFormatOptions"/>
        /// </param>
        private void SetDataFormat(DepthCamera.DataFormatOption format)
        {
            // change depth map that's waiting cause format has changed
            this.UpdateNextFrameDepthMap();

            int result = KinectNative.freenect_set_depth_format(this.parentDevice.devicePointer, format);

            if (result != 0)
            {
                throw new Exception("Could not switch to depth format " + format + ". Error Code: " + result);
            }
            this.dataFormat = format;
        }
Exemple #12
0
 /// <summary>
 /// Updates the next frame imagemap that's waiting for data with any state changes
 /// </summary>
 protected void UpdateNextFrameImageMap()
 {
     if (this.DataBuffer == IntPtr.Zero)
     {
         // have to set our own buffer as the video buffer
         this.nextFrameImage = new ImageMap(this.DataFormat);
         KinectNative.freenect_set_video_buffer(this.parentDevice.devicePointer, this.nextFrameImage.DataPointer);
     }
     else
     {
         // already have a buffer from user
         this.nextFrameImage = new ImageMap(this.DataFormat, this.DataBuffer);
     }
 }
Exemple #13
0
        /// <summary>
        /// Sets the VideoCamera's data format. Support function for VideoCamera.DataFormat property.
        /// </summary>
        /// <param name="format">
        /// Format to change the video camera to
        /// </param>
        protected void SetDataFormat(VideoCamera.DataFormatOption format)
        {
            // change imagemap that's waiting cause format has changed
            this.UpdateNextFrameImageMap();

            // change format
            int result = KinectNative.freenect_set_video_format(this.parentDevice.devicePointer, format);

            if (result != 0)
            {
                throw new Exception("Could not switch to video format " + format + ". Error Code: " + result);
            }
            this.dataFormat = format;
        }
Exemple #14
0
        /// <summary>
        /// Starts streaming RGB data from this camera
        /// </summary>
        public void Start()
        {
            // Update image map before starting
            this.UpdateNextFrameImageMap();

            // Start
            int result = KinectNative.freenect_start_video(this.parentDevice.devicePointer);

            if (result != 0)
            {
                throw new Exception("Could not start video stream. Error Code: " + result);
            }
            this.IsRunning = true;
        }
Exemple #15
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="parent">
        /// Parent <see cref="Kinect"/> device that this video camera is part of
        /// </param>
        internal VideoCamera(Kinect parent)
        {
            // Save parent device
            this.parentDevice = parent;

            // Not running by default
            this.IsRunning = false;

            // Set format to RGB by default
            this.DataFormat = DataFormatOption.RGB;

            // Setup callbacks
            KinectNative.freenect_set_video_callback(parent.devicePointer, VideoCallback);
        }
Exemple #16
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="parent">
        /// Parent <see cref="Kinect"/> device that this depth camera is part of
        /// </param>
        internal DepthCamera(Kinect parent)
        {
            // Save parent device
            this.parentDevice = parent;

            // Not running by default
            this.IsRunning = false;

            // Set format to 11 bit by default
            this.DataFormat = DataFormatOption.Format11Bit;

            // Setup callbacks
            KinectNative.freenect_set_depth_callback(parent.devicePointer, DepthCallback);
        }
        /// <summary>
        /// Updates the next frame depth map that's waiting for data with any state changes
        /// </summary>
        protected void UpdateNextFrameDepthMap()
        {
            if (this.DataBuffer == IntPtr.Zero)
            {
                // have to set our own buffer as the depth buffer
                this.nextFrameData = new DepthMap(this.Mode);
            }
            else
            {
                // already have a buffer from user
                this.nextFrameData = new DepthMap(this.Mode, this.DataBuffer);
            }

            // Set new buffer at library level;
            KinectNative.freenect_set_depth_buffer(this.parentDevice.devicePointer, this.nextFrameData.DataPointer);
        }
        /// <summary>
        /// Starts streaming depth data from this camera
        /// </summary>
        public void Start()
        {
            // Update depth map before starting
            this.UpdateNextFrameDepthMap();

            // Start
            int result = KinectNative.freenect_start_depth(this.parentDevice.devicePointer);

            if (result != 0)
            {
                throw new Exception("Could not start depth stream. Error Code: " + result);
            }

            // All done
            this.IsRunning = true;
        }
        /// <summary>
        /// Stops streaming depth data from this camera
        /// </summary>
        public void Stop()
        {
            if (this.IsRunning == false)
            {
                // Not running, nothing to do
                return;
            }

            // Stop camera
            int result = KinectNative.freenect_stop_depth(this.parentDevice.devicePointer);

            if (result != 0)
            {
                throw new Exception("Could not stop depth stream. Error Code: " + result);
            }
            this.IsRunning = false;
        }
        /// <summary>
        /// Sets the current depth camera mode
        /// </summary>
        /// <param name="mode">
        /// Depth camera mode to switch to.
        /// </param>
        protected void SetDepthMode(DepthFrameMode mode)
        {
            // Is this a different mode?
            if (this.Mode == mode)
            {
                return;
            }

            // Stop camera first if running
            bool running = this.IsRunning;

            if (running)
            {
                this.Stop();
            }

            // Check to make sure mode is valid by finding it again
            DepthFrameMode foundMode = DepthFrameMode.Find(mode.Format, mode.Resolution);

            if (foundMode == null)
            {
                throw new Exception("Invalid Depth Camera Mode: [" + mode.Format + ", " + mode.Resolution + "]");
            }

            // Save mode
            this.captureMode = mode;

            // All good, switch to new mode
            int result = KinectNative.freenect_set_depth_mode(this.parentDevice.devicePointer, foundMode.nativeMode);

            if (result != 0)
            {
                throw new Exception("Mode switch failed. Error Code: " + result);
            }

            // Update depth map
            this.UpdateNextFrameDepthMap();

            // If we were running before, start up again
            if (running)
            {
                this.Start();
            }
        }
        /// <summary>
        /// Shuts down the context and closes any open devices.
        /// </summary>
        public static void ShutdownContext()
        {
            // Close all devices
            foreach (Kinect device in KinectNative.deviceMap.Values)
            {
                device.Close();
            }

            // Shutdown context
            int result = KinectNative.freenect_shutdown(KinectNative.freenectContext);

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

            // Dispose pointer
            KinectNative.freenectContext = IntPtr.Zero;
        }
        /// <summary>
        /// Updates list of depth modes that this camera has.
        /// </summary>
        private void UpdateDepthModes()
        {
            List <DepthFrameMode> modes = new List <DepthFrameMode>();

            // Get number of modes
            int numModes = KinectNative.freenect_get_depth_mode_count(this.parentDevice.devicePointer);

            // Go through modes
            for (int i = 0; i < numModes; i++)
            {
                DepthFrameMode mode = (DepthFrameMode)FrameMode.FromInterop(KinectNative.freenect_get_depth_mode(i), FrameMode.FrameModeType.DepthFormat);
                if (mode != null)
                {
                    modes.Add(mode);
                }
            }

            // All done
            this.Modes = modes.ToArray();
        }
        /// <summary>
        /// Sets the motor's tilt angle.
        /// </summary>
        /// <param name="angle">
        /// Value between [-1.0, 1.0]
        /// </param>
        private void SetMotorTilt(double angle)
        {
            // Check if value is in valid ranges
            if (angle < -1.0 || angle > 1.0)
            {
                throw new ArgumentOutOfRangeException("Motor tilt has to be in the range [-1.0, 1.0]");
            }

            // Figure out raw angle between -31 and 31
            double rawAngle = Math.Round(angle * 31);

            // Call native func.
            int result = KinectNative.freenect_set_tilt_degs(this.parentDevice.devicePointer, rawAngle);

            if (result != 0)
            {
                throw new Exception("Coult not set raw motor tilt angle to " + angle + ". Error Code: " + result);
            }

            // Save commanded tilt
            this.commandedTilt = angle;
        }
        /// <summary>
        /// Opens up the connection to this Kinect device
        /// </summary>
        public void Open()
        {
            int result = KinectNative.freenect_open_device(KinectNative.Context, ref this.devicePointer, this.DeviceID);

            if (result != 0)
            {
                throw new Exception("Could not open connection to Kinect Device (ID=" + this.DeviceID + "). Error Code = " + result);
            }

            // Create child instances
            this.LED           = new LED(this);
            this.Motor         = new Motor(this);
            this.Accelerometer = new Accelerometer(this);
            this.VideoCamera   = new VideoCamera(this);
            this.DepthCamera   = new DepthCamera(this);

            //Register the device
            KinectNative.RegisterDevice(this.devicePointer, this);

            // Open now
            this.IsOpen = true;
        }
 /// <summary>
 /// Finds a mode, given a format and resolution.
 /// </summary>
 /// <param name="format">
 /// Depth format for the mode
 /// </param>
 /// <param name="resolution">
 /// Resolution for the mode
 /// </param>
 /// <returns>
 /// Mode with the format/resolution combo. Null if the combination is invalid.
 /// </returns>
 public static DepthFrameMode Find(DepthFormat format, Resolution resolution)
 {
     return((DepthFrameMode)FrameMode.FromInterop(KinectNative.freenect_find_depth_mode(resolution, format), FrameMode.FrameModeType.DepthFormat));
 }
        /// <summary>
        /// Logging callback.
        /// </summary>
        /// <param name="device">
        /// A <see cref="IntPtr"/>
        /// </param>
        /// <param name="logLevel">
        /// A <see cref="Kinect.LogLevelOptions"/>
        /// </param>
        /// <param name="message">
        /// A <see cref="System.String"/>
        /// </param>
        internal static void LogCallback(IntPtr device, LoggingLevel logLevel, string message)
        {
            Kinect realDevice = KinectNative.GetDevice(device);

            Kinect.Log(null, new LogEventArgs(realDevice, logLevel, message));
        }
 /// <summary>
 /// Sets the logging level for the Kinect session. Support function for Kinect.LogLevel property.
 /// </summary>
 /// <param name="level">
 /// A <see cref="LogLevel"/>
 /// </param>
 private static void SetLogLevel(LoggingLevel level)
 {
     KinectNative.freenect_set_log_level(KinectNative.Context, level);
     Kinect.logLevel = level;
 }
 /// <summary>
 /// Gets the number of Kinect devices connected
 /// </summary>
 /// <remarks>
 /// This is just a support function for the Kinect.DeviceCount property
 /// </remarks>
 /// <returns>
 /// Number of Kinect devices connected.
 /// </returns>
 private static int GetDeviceCount()
 {
     // Now we can just return w/e native method puts out
     return(KinectNative.freenect_num_devices(KinectNative.Context));
 }
 /// <summary>
 /// Shuts down the Kinect.NET library and closes any open devices.
 /// </summary>
 public static void Shutdown()
 {
     KinectNative.ShutdownContext();
 }
 /// <summary>
 /// Makes the base library handle any pending USB events. Either this, or UpdateStatus
 /// should be called repeatedly.
 /// </summary>
 public static void ProcessEvents()
 {
     KinectNative.freenect_process_events(KinectNative.Context);
 }