MechnicalMonitor.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #include "stdafx.h"
  2. #include "MechnicalMonitor.h"
  3. #include "IMotionEventReceiver.h"
  4. #include "IPositionManager.h"
  5. #include "LogicDriverThreadLocker.h"
  6. using namespace DIOS::Dev::Detail::MachineryECOM;
  7. MechnicalMonitor *MechnicalMonitor::m_instance = nullptr;
  8. MechnicalMonitor::MechnicalMonitor()
  9. :m_positionManager(nullptr),
  10. m_eventReceiver(nullptr),
  11. m_monitorThread(nullptr),
  12. m_monitorworkThread(nullptr),
  13. m_notifyEventHandleCount(0),
  14. m_waitEventHandleCount(0),
  15. m_isMonitorStoped(TRUE),
  16. m_angleStopedNotifyTime(0),
  17. m_heightStopedNotifyTime(0),
  18. m_horizontalStopedNotifyTime(0),
  19. m_dofTubeAngleAxis(TOMO_TUBE_ANGLE),
  20. m_dofTubeHeightAxis(TOMO_TUBE_HEIGHT),
  21. m_dofTubeHorizontalAxis(TOMO_TUBE_HORIZONTAL)
  22. {
  23. for (int i = 0; i < MONITOR_NOTIFY_EVENT_MAX; ++i)
  24. {
  25. m_waitEventHandles[i] = nullptr;
  26. }
  27. for (int i = 0; i < MONITOR_NOTIFY_EVENT_MAX; ++i)
  28. {
  29. m_notifyEventHandles[i] = nullptr;
  30. }
  31. }
  32. MechnicalMonitor::~MechnicalMonitor()
  33. {
  34. if (m_monitorThread)
  35. {
  36. m_monitorThread->StopThread();
  37. }
  38. if (m_monitorworkThread)
  39. {
  40. m_monitorworkThread->StopThread();
  41. }
  42. }
  43. MechnicalMonitor *MechnicalMonitor::Instance()
  44. {
  45. if (m_instance == nullptr)
  46. {
  47. m_instance = new MechnicalMonitor();
  48. }
  49. return m_instance;
  50. }
  51. void MechnicalMonitor::Initialize(IPositionManager *coordinates, int dofTubeAngleAxis, int dofTubeHeightAxis)
  52. {
  53. m_positionManager = coordinates;
  54. m_dofTubeAngleAxis = dofTubeAngleAxis;
  55. m_dofTubeHeightAxis = dofTubeHeightAxis;
  56. HANDLE monitorThreadBeginEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  57. HANDLE monitorThreadEndEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  58. AddThreadWaitEventHandle(MONITOR_EVENT_WAIT_BEGIN_MONITOR,monitorThreadBeginEvent);
  59. AddThreadWaitEventHandle(MONITOR_EVENT_WAIT_END_MONITOR,monitorThreadEndEvent);
  60. HANDLE tubeAngleMotionStoped = CreateEvent(NULL, FALSE, FALSE, NULL);
  61. HANDLE tubeHeightMotionStoped = CreateEvent(NULL, FALSE, FALSE, NULL);
  62. HANDLE tubeHorizontalMotionStoped = CreateEvent(NULL, FALSE, FALSE, NULL);
  63. AddThreadNotifyEventHandle(MONITOR_EVENT_TUBEANGLE_MOVE_STOP, tubeAngleMotionStoped);
  64. AddThreadNotifyEventHandle(MONITOR_EVENT_TUBEHEIGHT_MOVE_STOP, tubeHeightMotionStoped);
  65. AddThreadNotifyEventHandle(MONITOR_EVENT_TUBEHORIZONTAL_MOVE_STOP, tubeHorizontalMotionStoped);
  66. m_monitorThread = new MotionMonitorThread();
  67. m_monitorThread->Initialize(this);
  68. m_monitorThread->StartThread();
  69. m_monitorworkThread = new MotionMonitorWorkThread();
  70. m_monitorworkThread->Initialize(this);
  71. m_monitorworkThread->StartThread();
  72. }
  73. void MechnicalMonitor::AddThreadNotifyEventHandle(const std::string &name, HANDLE eventHandle)
  74. {
  75. m_notifyEventsIndexNameMap[m_notifyEventHandleCount] = name;
  76. m_notifyEventHandles[m_notifyEventHandleCount++] = eventHandle;
  77. }
  78. void MechnicalMonitor::AddThreadWaitEventHandle(const std::string &name, HANDLE eventHandle)
  79. {
  80. m_waitEventsIndexNameMap[m_waitEventHandleCount] = name;
  81. m_waitEventHandles[m_waitEventHandleCount++] = eventHandle;
  82. }
  83. void MechnicalMonitor::BeginMonitor(IMotionEventReceiver *receiver, int offset)
  84. {
  85. m_eventReceiver = receiver;
  86. if (offset == MO_TUBE_ANGLE)
  87. {
  88. InterlockedIncrement((LONG*)&m_angleStopedNotifyTime);
  89. }
  90. else if (offset == MO_TUBE_HEIGHT)
  91. {
  92. InterlockedIncrement((LONG*)&m_heightStopedNotifyTime);
  93. }
  94. else if (offset == MO_TUBE_HORIZONTAL)
  95. {
  96. InterlockedIncrement((LONG*)&m_horizontalStopedNotifyTime);
  97. }
  98. else
  99. {
  100. InterlockedIncrement((LONG*)&m_angleStopedNotifyTime);
  101. InterlockedIncrement((LONG*)&m_heightStopedNotifyTime);
  102. InterlockedIncrement((LONG*)&m_horizontalStopedNotifyTime);
  103. }
  104. if(gbusinessLog) gbusinessLog->Info("[MechnicalMonitor][BeginMonitor]->[{$:d} {$:d} {$:d}]", m_angleStopedNotifyTime, m_heightStopedNotifyTime, m_horizontalStopedNotifyTime);
  105. SetEvent(m_waitEventHandles[0]);
  106. }
  107. void MechnicalMonitor::EndMonitor()
  108. {
  109. SetEvent(m_waitEventHandles[1]);
  110. }
  111. void MechnicalMonitor::ReceiveEvent(const std::string &name)
  112. {
  113. //LogicDriverThreadLocker::Instance()->Lock();
  114. if (m_eventReceiver)
  115. {
  116. if(gbusinessLog) gbusinessLog->Info("[MechnicalMonitor][ReceiveEvent]->[{$}]", name.c_str());
  117. m_eventReceiver->OnMotionEvent(name);
  118. }
  119. //LogicDriverThreadLocker::Instance()->UnLock();
  120. }
  121. void MechnicalMonitor::OnStartMonitorInThread()
  122. {
  123. if(gbusinessLog) gbusinessLog->Info("[MechnicalMonitor][OnStartMonitorInThread]");
  124. //轮询球管高度/角度编码器值的变化,直至为 0,并通知work线程
  125. long presionSensor = 2L;
  126. InterlockedExchange((LONG *)&m_isMonitorStoped,FALSE);
  127. auto lastanglesensor = m_positionManager->GetCurrentSensorValue((DOF_MECH)m_dofTubeAngleAxis);
  128. auto lastheightsensor = m_positionManager->GetCurrentSensorValue((DOF_MECH)m_dofTubeHeightAxis);
  129. auto lasthorizontalsensor = m_positionManager->GetCurrentSensorValue((DOF_MECH)TOMO_TUBE_HORIZONTAL);
  130. while (!m_isMonitorStoped && (m_angleStopedNotifyTime > 0 || m_heightStopedNotifyTime > 0 || m_horizontalStopedNotifyTime > 0))
  131. {
  132. Sleep(300);
  133. if (!m_notifyEventHandles[0])
  134. {
  135. InterlockedDecrement((LONG*)&m_angleStopedNotifyTime);
  136. }
  137. if (!m_notifyEventHandles[1])
  138. {
  139. InterlockedDecrement((LONG*)&m_heightStopedNotifyTime);
  140. }
  141. if (!m_notifyEventHandles[2])
  142. {
  143. InterlockedDecrement((LONG*)&m_horizontalStopedNotifyTime);
  144. }
  145. auto currentanglesensor = m_positionManager->GetCurrentSensorValue((DOF_MECH)m_dofTubeAngleAxis);
  146. if (m_angleStopedNotifyTime > 0 && (abs((long)currentanglesensor - (long)lastanglesensor) < presionSensor))
  147. {
  148. InterlockedDecrement((LONG*)&m_angleStopedNotifyTime);
  149. if(gbusinessLog) gbusinessLog->Info("[MechnicalMonitor][OnStartMonitorInThread]->[Notify TubeAngle Stop.]");
  150. SetEvent(m_notifyEventHandles[0]);
  151. }
  152. lastanglesensor = currentanglesensor;
  153. auto currentheightsensor = m_positionManager->GetCurrentSensorValue((DOF_MECH)m_dofTubeHeightAxis);
  154. if (m_heightStopedNotifyTime > 0 && (abs((long)currentheightsensor - (long)lastheightsensor) < presionSensor))
  155. {
  156. InterlockedDecrement((LONG*)&m_heightStopedNotifyTime);
  157. if(gbusinessLog) gbusinessLog->Info("[MechnicalMonitor][OnStartMonitorInThread]->[Notify TubeHeight Stop.]");
  158. SetEvent(m_notifyEventHandles[1]);
  159. }
  160. lastheightsensor = currentheightsensor;
  161. long adTolerance = 1L;
  162. auto currenthorizontalsensor = m_positionManager->GetCurrentSensorValue((DOF_MECH)m_dofTubeHorizontalAxis);
  163. if (m_horizontalStopedNotifyTime > 0 && (abs((long)currenthorizontalsensor - (long)lasthorizontalsensor) < adTolerance))
  164. {
  165. InterlockedDecrement((LONG*)&m_horizontalStopedNotifyTime);
  166. if (gbusinessLog) gbusinessLog->Info("[MechnicalMonitor][OnStartMonitorInThread]->[Notify TubeHorizontal Stop.]");
  167. SetEvent(m_notifyEventHandles[2]);
  168. }
  169. lasthorizontalsensor = currenthorizontalsensor;
  170. if(gbusinessLog) gbusinessLog->Info("[MechnicalMonitor][CurrentMonitorValues]->[Angle:{$:d}, Height:{$:d}, Horizontal:{$:d}]", currentanglesensor, currentheightsensor, currenthorizontalsensor);
  171. }
  172. InterlockedExchange((LONG *)&m_isMonitorStoped, TRUE);
  173. }
  174. void MechnicalMonitor::OnStopMonitorInThread()
  175. {
  176. //停止轮询球管高度/角度编码器值
  177. InterlockedExchange((LONG *)&m_isMonitorStoped, TRUE);
  178. }
  179. bool MechnicalMonitor::OnMotionMonitorThreadExecute()
  180. {
  181. if (m_waitEventHandleCount <= 0 || m_waitEventHandleCount > MONITOR_NOTIFY_EVENT_MAX)
  182. {
  183. return false;
  184. }
  185. while (true)
  186. {
  187. auto waitResult = ::WaitForMultipleObjects(m_waitEventHandleCount, m_waitEventHandles, FALSE, INFINITE);
  188. auto eventIndex = waitResult - WAIT_OBJECT_0;
  189. if (m_waitEventsIndexNameMap.find(eventIndex) != m_waitEventsIndexNameMap.end())
  190. {
  191. auto eventName = m_waitEventsIndexNameMap[eventIndex];
  192. if (eventName == MONITOR_EVENT_WAIT_BEGIN_MONITOR)
  193. {
  194. if (!m_isMonitorStoped)
  195. {
  196. continue;
  197. }
  198. OnStartMonitorInThread();
  199. }
  200. else if (eventName == MONITOR_EVENT_WAIT_END_MONITOR)
  201. {
  202. OnStopMonitorInThread();
  203. }
  204. else
  205. {
  206. }
  207. }
  208. }
  209. return true;
  210. }
  211. bool MechnicalMonitor::OnMotionMonitorWorkThreadExecute()
  212. {
  213. if (m_notifyEventHandleCount <= 0 || m_notifyEventHandleCount > MONITOR_NOTIFY_EVENT_MAX)
  214. {
  215. return false;
  216. }
  217. while (true)
  218. {
  219. auto waitResult = ::WaitForMultipleObjects(m_notifyEventHandleCount, m_notifyEventHandles, FALSE, INFINITE);
  220. auto eventIndex = waitResult - WAIT_OBJECT_0;
  221. if (m_notifyEventsIndexNameMap.find(eventIndex) != m_notifyEventsIndexNameMap.end())
  222. {
  223. auto eventName = m_notifyEventsIndexNameMap[eventIndex];
  224. ReceiveEvent(eventName);
  225. }
  226. }
  227. }
  228. void MotionMonitorThread::Initialize(MechnicalMonitor *monitor)
  229. {
  230. m_monitor = monitor;
  231. }
  232. bool MotionMonitorThread::Exec()
  233. {
  234. if (!m_monitor)
  235. {
  236. return false;
  237. }
  238. std::thread::id this_id = std::this_thread::get_id();
  239. unsigned int t = *(unsigned int*)&this_id;// threadid 转成 unsigned int
  240. printf("MotionMonitorThread ID: %d\n", t);
  241. return m_monitor->OnMotionMonitorThreadExecute();
  242. }
  243. void MotionMonitorWorkThread::Initialize(MechnicalMonitor *monitor)
  244. {
  245. m_monitor = monitor;
  246. }
  247. bool MotionMonitorWorkThread::Exec()
  248. {
  249. if (!m_monitor)
  250. {
  251. return false;
  252. }
  253. std::thread::id this_id = std::this_thread::get_id();
  254. unsigned int t = *(unsigned int*)&this_id;// threadid 转成 unsigned int
  255. printf("MotionMonitorWorkThread ID: %d\n", t);
  256. return m_monitor->OnMotionMonitorWorkThreadExecute();
  257. }