InputProviderIntel.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using Emgu.CV;
  3. using Emgu.CV.Structure;
  4. using System.Timers;
  5. using System.Threading;
  6. using System.Diagnostics;
  7. namespace BBIWARG.Input.InputProviding
  8. {
  9. class InputProviderIntel : MarshalByRefObject, IInputProvider
  10. {
  11. IntelCameraWrapper wrapper;
  12. public int CurrentFrameID
  13. {
  14. get;
  15. set;
  16. }
  17. public float FieldOfViewHorizontal
  18. {
  19. get;
  20. set;
  21. }
  22. public float FieldOfViewVertical
  23. {
  24. get;
  25. set;
  26. }
  27. public int ImageWidth
  28. {
  29. get { return 640; }
  30. }
  31. public int ImageHeight
  32. {
  33. get { return 480; }
  34. }
  35. public bool IsActive
  36. {
  37. get;
  38. set;
  39. }
  40. public event DeviceStartedEventHandler DeviceStartedEvent;
  41. public event NewFrameEventHandler NewFrameEvent;
  42. public UInt16 lowConfidenceValue;
  43. private AppDomain domain;
  44. private System.Threading.Timer watchdogTimer;
  45. public void initialize()
  46. {
  47. CurrentFrameID = 0;
  48. watchdogTimer = new System.Threading.Timer(Timer_Elapsed, null, 2000, 1000);
  49. }
  50. private bool errorstate = false;
  51. private void Timer_Elapsed(object state)
  52. {
  53. checkExitErrorstate();
  54. errorstate = true;
  55. }
  56. int startId = 0;
  57. Stopwatch startTimeWatch;
  58. public void start()
  59. {
  60. Console.WriteLine("starting new AppDomain (" + startId +")...");
  61. domain = System.AppDomain.CreateDomain("AppDomain-" + startId);
  62. startId++;
  63. wrapper = (IntelCameraWrapper) domain.CreateInstanceAndUnwrap(typeof(IntelCameraWrapper).Assembly.GetName().ToString(), typeof(IntelCameraWrapper).FullName);
  64. wrapper.init(this);
  65. IsActive = true;
  66. if (DeviceStartedEvent != null) {
  67. DeviceStartedEvent(this, new EventArgs());
  68. DeviceStartedEvent = null; //only notify once...
  69. }
  70. scheduledForRestart = false;
  71. startTimeWatch = Stopwatch.StartNew();
  72. wrapper.run();
  73. checkExitErrorstate();
  74. }
  75. private bool scheduledForRestart = false;
  76. private void checkExitErrorstate()
  77. {
  78. if (errorstate && !scheduledForRestart)
  79. {
  80. if (startTimeWatch != null && startTimeWatch.ElapsedMilliseconds < 5000) {
  81. Console.WriteLine("...");
  82. return;
  83. }
  84. startTimeWatch.Stop();
  85. //we have crashed. Kill the app domain and try again.
  86. Console.WriteLine("Killing AppDomain (" + domain.FriendlyName + ")...");
  87. scheduledForRestart = true;
  88. //wrapper.terminated = true;
  89. try
  90. {
  91. System.AppDomain.Unload(domain);
  92. }
  93. catch (Exception)
  94. {
  95. Console.WriteLine("Could not unload app domain. We will just wait a moment and try to start a new one.");
  96. }
  97. domain = null;
  98. wrapper = null;
  99. Thread.Sleep(5000);
  100. start();
  101. }
  102. }
  103. public void stop()
  104. {
  105. IsActive = false;
  106. }
  107. internal bool hasNewFrameEvent()
  108. {
  109. return NewFrameEvent != null;
  110. }
  111. internal void newFrame(int currentFrameID, Image<Gray, ushort> dImg)
  112. {
  113. errorstate = false;
  114. NewFrameEvent(this, new NewFrameEventArgs(currentFrameID, dImg));
  115. }
  116. }
  117. }