/// <summary> /// Main working method - runs in loop on Start /// </summary> public override void Job() { Initialize(); //Detach from streamer _publisher.Detach(this); //get current ball coordinates BallCoordinates coordinates = SampleCoordinates(); //show current ball coordinates on screen and GUI System.Drawing.PointF p = TransformAgent.Data.InvertTransform(new System.Drawing.PointF(_x, _y)); Marks.DrawBall(new Point(p.X, p.Y), _ballRadius); Statistics.TryUpdateBasicImageProcessingInfo(String.Format("Generated coordinates: {0}x{1}", _x, _y)); //set current coordinates to update ImagingData.BallCoords = coordinates; //set current coordinates and publish new ball coordinates BallLocationUpdater.UpdateAndNotify(); //attach back to streamer _publisher.Attach(this); }
/// <summary> /// Update Last Know Location and Calculate new ball coordinates /// </summary> /// <param name="xLocation">Calculated X location [Foosbot world coordinates]</param> /// <param name="yLocation">Calculated Y location [Foosbot world coordinates]</param> /// <param name="timeStamp">Current image time stamp</param> /// <param name="detectionColor">Color to draw ball on screen</param> private void UpdateCoordinates(double xLocation, double yLocation, DateTime timeStamp, SolidColorBrush detectionColor) { ImagingData.LastKnownBallLocation = new BallLocation(xLocation, yLocation, timeStamp); int x = ImagingData.LastKnownBallLocation.X + OffsetX; int y = ImagingData.LastKnownBallLocation.Y + OffsetY; Marks.DrawBall(new System.Windows.Point(x, y), Convert.ToInt32(20), detectionColor); System.Drawing.PointF coordinates = TransformAgent.Data.Transform(new System.Drawing.PointF(x, y)); ImagingData.BallCoords = new BallCoordinates(Convert.ToInt32(coordinates.X), Convert.ToInt32(coordinates.Y), timeStamp); Statistics.TryUpdateBallCoordinates( String.Format("[IP Unit] Ball coordinates: {0}x{1}", ImagingData.BallCoords.X, ImagingData.BallCoords.Y)); }
/// <summary> /// Main Method of Vector Calculation Unit /// </summary> public override void Job() { try { _publisher.Detach(this); if (_stabilizer == null) { int ballRadius = (_imagingData.BallRadius < 0) ? 5 : _imagingData.BallRadius; _stabilizer = new CoordinatesStabilizer(ballRadius); } //Get data and remove noise BallCoordinates newCoordinates = _publisher.Data; BallCoordinates ballCoordinates = _stabilizer.Stabilize(newCoordinates, _storedBallCoordinates); //Draw coordinates from stabilizer if (ballCoordinates.IsDefined) { double x, y; TransformAgent.Data.InvertTransform(ballCoordinates.X, ballCoordinates.Y, out x, out y); } else { //Delete old locations Marks.DrawBall(new System.Windows.Point(0, 0), 0); } ballCoordinates.Vector = VectorCalculationAlgorithm(ballCoordinates); if (ballCoordinates.IsDefined && ballCoordinates.Vector.IsDefined) { try { (_coordinatesUpdater as BallCoordinatesUpdater).LastBallCoordinates = ballCoordinates; LastBallLocationPublisher.UpdateAndNotify(); Marks.DrawBallVector(new Point(ballCoordinates.X, ballCoordinates.Y), new Point(Convert.ToInt32(ballCoordinates.Vector.X), Convert.ToInt32(ballCoordinates.Vector.Y))); } catch (Exception e) { Log.Print(String.Format("{0} [{1}]", e.Message, ballCoordinates.ToString()), eCategory.Error, LogTag.VECTOR); } } else { Marks.DrawBallVector(new Point(0, 0), new Point(0, 0), false); } } catch (ThreadInterruptedException) { /* new data received */ } catch (Exception e) { Log.Print(String.Format("Error in vector calculation. Reason: {0}", e.Message), eCategory.Error, LogTag.VECTOR); } finally { _publisher.Attach(this); } }