/// <summary> /// Shallow clone /// </summary> /// <returns>A new shallow copied instance of RawKinectFrames</returns> public object Clone() { var copy = new RawKinectFrames(); this.CopyTo(copy); return(copy); }
/// <summary> /// Read kinect frames when requested via GetFrame /// </summary> /// <param name="rawFrames">Raw Kinect Frames</param> /// <param name="kinectSensorParam">Kinect Sensor</param> /// <param name="includeDepth">Inclue depth?</param> /// <param name="includeVideo">Inclue video?</param> /// <param name="includeSkeletons">Inclue skeletons?</param> private void ReadKinectFramesOnDemand( RawKinectFrames rawFrames, KinectSensor kinectSensorParam, bool includeDepth, bool includeVideo, bool includeSkeletons) { if (includeVideo) { this.ReadColorFrameOnDemandBehavior( rawFrames, kinectSensorParam.ColorStream); } if (includeDepth) { this.ReadDepthFrameOnDemandBehavior( rawFrames, kinectSensorParam.DepthStream); } if (includeSkeletons) { this.ReadSkeletalFrameBehavior(rawFrames, kinectSensorParam.SkeletonStream); } this.lastFrameReadTime = Common.Utilities.ElapsedSecondsSinceStart; }
/// <summary> /// Read skeletal frame if needed /// </summary> /// <param name="rawFrames">Raw frames</param> /// <param name="skeletonStream">Skeleton stream</param> private void DoReadSkeletalFrame(RawKinectFrames rawFrames, SkeletonStream skeletonStream) { SkeletonFrame skeletonFrame = skeletonStream.OpenNextFrame(AVeryLargeNumberOfMilliseconds); if (skeletonFrame != null) { rawFrames.RawSkeletonFrameData = new SkeletonDataFrame(skeletonFrame); } }
/// <summary> /// Invoked right after a raw frame was obtained from Kinect service /// </summary> /// <param name="frames">Raw frame as recieved from Kinect sensor</param> public void SetRawFrame(kinect.RawKinectFrames frames) { this.RawFrames = frames; if (null == this.RawFrames.RawDepthFrameData) { // could be that depth frame was not requested return; } }
/// <summary> /// DoReadDepthFrame Frame Read Behavior /// </summary> /// <param name="rawFrames">Raw frames</param> /// <param name="kinectStream">Kinect stream</param> private void DoReadDepthFrame(RawKinectFrames rawFrames, DepthImageStream kinectStream) { DepthImageFrame kinectFrame = kinectStream.OpenNextFrame(AVeryLargeNumberOfMilliseconds); if (kinectFrame != null) { rawFrames.RawDepthFrameInfo = new KinectFrameInfo(kinectFrame); rawFrames.RawDepthFrameData = new short[kinectFrame.PixelDataLength]; kinectFrame.CopyPixelDataTo(rawFrames.RawDepthFrameData); } }
/// <summary> /// Read frames for alternates /// </summary> /// <param name="rawFrames">Raw Kinect Frames</param> /// <param name="kinectSensorParam">Kinect Sensor Parameters</param> private void ReadKinectFramesForAlternates(RawKinectFrames rawFrames, KinectSensor kinectSensorParam) { this.ReadColorFrameForAlternateBehavior( rawFrames, kinectSensorParam.ColorStream); this.ReadDepthFrameForAlternateBehavior( rawFrames, kinectSensorParam.DepthStream); this.lastFrameReadTime = Common.Utilities.ElapsedSecondsSinceStart; }
/// <summary> /// Main read loop /// Read raw frame from Kinect service, then process it asynchronously, then request UI update /// </summary> /// <returns>A standard CCR iterator.</returns> private IEnumerator <ITask> ReadKinectLoop() { while (true) { kinectProxy.QueryRawFrameRequest frameRequest = new kinectProxy.QueryRawFrameRequest(); frameRequest.IncludeDepth = this.IncludeDepth; frameRequest.IncludeVideo = this.IncludeVideo; frameRequest.IncludeSkeletons = this.IncludeSkeletons; if (!this.IncludeDepth && !this.IncludeVideo && !this.IncludeSkeletons) { // poll 5 times a sec if user for some reason deselected all image options (this would turn // into a busy loop then) yield return(TimeoutPort(200).Receive()); } kinect.RawKinectFrames rawFrames = null; // poll depth camera yield return(this.kinectPort.QueryRawFrame(frameRequest).Choice( rawFrameResponse => { rawFrames = rawFrameResponse.RawFrames; }, failure => { if (!this.frameQueryFailed) { this.frameQueryFailed = true; LogError(failure); } })); this.frameProcessor.SetRawFrame(rawFrames); if (null != rawFrames.RawSkeletonFrameData) { yield return(new IterativeTask(this.frameProcessor.ProcessSkeletons)); } this.UpdateUI(this.frameProcessor); // poll state at low frequency to see if tilt has shifted (may happen on an actual robot due to shaking) if (common.Utilities.ElapsedSecondsSinceStart - this.lastStateReadTime > 1) { yield return(this.kinectPort.Get().Choice( kinectState => { this.UpdateState(kinectState); }, failure => { if (!this.tiltPollFailed) { this.tiltPollFailed = true; LogError(failure); } })); this.lastStateReadTime = common.Utilities.ElapsedSecondsSinceStart; } } }
/// <summary> /// Main read loop /// Read raw frame from Kinect service, then process it asynchronously, then request UI update /// </summary> /// <returns>A standard CCR iterator.</returns> private IEnumerator <ITask> ReadKinectLoop() { // note: see frame rate at C:\Microsoft Robotics Dev Studio 4\projects\TrackRoamer\TrackRoamerServices\Config\TrackRoamer.TrackRoamerBot.Kinect.Config.xml while (true) { try { kinectProxy.QueryRawFrameRequest frameRequest = new kinectProxy.QueryRawFrameRequest(); frameRequest.IncludeDepth = this.IncludeDepth; frameRequest.IncludeVideo = this.IncludeVideo; frameRequest.IncludeSkeletons = this.IncludeSkeletons; if (!this.IncludeDepth && !this.IncludeVideo && !this.IncludeSkeletons) { // poll 2 times a sec if user for some reason deselected all image options (this would turn into a busy loop then) yield return(TimeoutPort(KinectLoopWaitIntervalMs).Receive()); } kinect.RawKinectFrames rawFrames = null; // poll depth camera yield return(this.kinectPort.QueryRawFrame(frameRequest).Choice( rawFrameResponse => { rawFrames = rawFrameResponse.RawFrames; }, failure => { if (!this.atLeastOneFrameQueryFailed) { this.atLeastOneFrameQueryFailed = true; LogError(failure); } })); this.frameProcessor.currentPanKinect = _state.currentPanKinect; this.frameProcessor.currentTiltKinect = _state.currentTiltKinect; this.frameProcessor.SetRawFrame(rawFrames); if (null != rawFrames.RawSkeletonFrameData) { yield return(new IterativeTask(this.frameProcessor.ProcessSkeletons)); var tmpAllSkeletons = frameProcessor.AllSkeletons; // get a snapshot of the pointer to allocated array, and then take sweet time processing it knowing it will not change if (tmpAllSkeletons != null) { // tmpAllSkeletons is a list of seven skeletons, those good enough for processing have IsSkeletonActive true var skels = from s in tmpAllSkeletons where s.IsSkeletonActive select s; foreach (VisualizableSkeletonInformation skel in skels) { // Kinect Z goes straight forward, X - to the left side, Y - up double kZ = skel.JointPoints[nui.JointType.Spine].Z; // meters, relative to Kinect camera double kX = skel.JointPoints[nui.JointType.Spine].X; GeoPosition pos1 = (GeoPosition)_mapperVicinity.robotPosition.Clone(); double relBearing = Direction.to180fromRad(Math.Atan2(-kX, kZ)); double rangeMeters = Math.Sqrt(kZ * kZ + kX * kX); pos1.translate(new Direction() { heading = _mapperVicinity.robotDirection.heading, bearingRelative = relBearing }, new Distance(rangeMeters)); DetectedHuman dhum1 = new DetectedHuman() { geoPosition = pos1, firstSeen = DateTime.Now.Ticks, lastSeen = DateTime.Now.Ticks, detectorType = DetectorType.KINECT_SKELETON, }; lock (_mapperVicinity) { _mapperVicinity.Add(dhum1); } } } } if (null != rawFrames.RawColorFrameData) { yield return(new IterativeTask(this.frameProcessor.ProcessImageFrame)); // RGB Video } if (null != rawFrames.RawDepthFrameData) { yield return(new IterativeTask(this.frameProcessor.ProcessDepthFrame)); // Depth information frame } this.UpdateUI(this.frameProcessor); Decide(SensorEventSource.Kinect); // poll state at low frequency to see if tilt has shifted (may happen on an actual robot due to shaking) if (common.Utilities.ElapsedSecondsSinceStart - this.lastStateReadTime > 1) { yield return(this.kinectPort.Get().Choice( kinectState => { this.UpdateState(kinectState); // update value displayed in WPF window _state.currentTiltKinect = kinectState.TiltDegrees; }, failure => { if (!this.atLeastOneTiltPollFailed) { this.atLeastOneTiltPollFailed = true; LogError(failure); } })); this.lastStateReadTime = common.Utilities.ElapsedSecondsSinceStart; } } finally { } } }