IoTriStateLift.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. using Aitex.Core.RT.Device;
  2. using Aitex.Core.RT.Event;
  3. using Aitex.Core.RT.IOCore;
  4. using System.Xml;
  5. using System.Diagnostics;
  6. using Aitex.Core.Common.DeviceData;
  7. using Aitex.Core.RT.DataCenter;
  8. using Aitex.Core.RT.SCCore;
  9. using Aitex.Core.RT.OperationCenter;
  10. using VirgoCommon;
  11. namespace VirgoRT.Devices
  12. {
  13. public enum Position
  14. {
  15. position1,
  16. position2,
  17. position3,
  18. origin,
  19. Invalid,
  20. }
  21. public class IoTriStateLift : BaseDevice, IDevice
  22. {
  23. private readonly DIAccessor _diOrigin;//是否到达原点
  24. private readonly DIAccessor _diP1;//是否到达位置1
  25. private readonly DIAccessor _diP2;//是否到达位置2
  26. private readonly DIAccessor _diP3;//是否到达位置3
  27. private readonly DIAccessor _diABSAlarm;//去位置1,2,3超时
  28. private readonly DIAccessor _diORGAlarm;//去原点超时
  29. private readonly DIAccessor _diServoAlarm; // servo alarm
  30. private readonly DIAccessor _diCCWLimitSensorAlarm; //
  31. private readonly DIAccessor _diOverSoftwareLimitAlarm; //
  32. private readonly DOAccessor _doReset;
  33. private readonly DOAccessor _doOrigin;//去原点
  34. private readonly DOAccessor _doP1;//去位置1
  35. private readonly DOAccessor _doP2;//去位置2
  36. private readonly DOAccessor _doP3;//去位置3
  37. private readonly DOAccessor _doStop;
  38. private readonly DOAccessor _doUp;
  39. private readonly DOAccessor _doDown;
  40. private readonly AIAccessor _currentValue;
  41. private readonly AOAccessor _aoSetP1;
  42. private readonly AOAccessor _aoSetP2;
  43. private readonly AOAccessor _aoSetP3;
  44. private readonly AOAccessor _aoServoEnable;
  45. private readonly AOAccessor _aoAutoSpeed;
  46. private readonly AOAccessor _aoManualSpeed;
  47. private readonly AOAccessor _aoAccTime;
  48. private readonly AOAccessor _aoDecTime;
  49. private readonly AOAccessor _aoCCWLimitPosition;
  50. private readonly AOAccessor _aoCWLimitPosition;
  51. private readonly AOAccessor _aoCorrectionValue;
  52. private Stopwatch sw = new Stopwatch();
  53. private Stopwatch _manualStopTimer = new Stopwatch();
  54. private readonly int _stopButtonAutoResetTime = 1000;
  55. private Position _currentTarget = Position.Invalid;
  56. long _timeout = 10000;
  57. private bool _bAlarmReported = false;
  58. private AITTriStateLiftPinData DeviceData
  59. {
  60. get
  61. {
  62. AITTriStateLiftPinData deviceData = new AITTriStateLiftPinData
  63. {
  64. Module = Module,
  65. DeviceName = Name,
  66. DeviceSchematicId = DeviceID,
  67. DisplayName = Display,
  68. };
  69. return deviceData;
  70. }
  71. }
  72. public MovementPosition PinPosition
  73. {
  74. get
  75. {
  76. if (_diP1.Value && _diP2.Value == false && _diP3.Value == false)
  77. return MovementPosition.Up;
  78. else if (_diP1.Value == false && _diP2.Value && _diP3.Value == false)
  79. return MovementPosition.Middle;
  80. else if (_diP1.Value == false && _diP2.Value == false && _diP3.Value)
  81. return MovementPosition.Down;
  82. else if (_diOrigin.Value)
  83. return MovementPosition.Origin;
  84. return MovementPosition.Unknown;
  85. }
  86. }
  87. public int PinPositionint
  88. {
  89. get
  90. {
  91. if (PinPosition == MovementPosition.Up)
  92. return 3;
  93. else if (PinPosition == MovementPosition.Middle)
  94. return 2;
  95. else if (PinPosition == MovementPosition.Down)
  96. return 1;
  97. else if (PinPosition == MovementPosition.Origin)
  98. return 0;
  99. return -1;
  100. }
  101. }
  102. public IoTriStateLift(string module, XmlElement node, string ioModule = "")
  103. {
  104. base.Module = module;
  105. base.Name = node.GetAttribute("id");
  106. base.Display = node.GetAttribute("display");
  107. base.DeviceID = node.GetAttribute("schematicId");
  108. _diOrigin = ParseDiNode("diOrigin", node, ioModule);
  109. _diP1 = ParseDiNode("diP1", node, ioModule);
  110. _diP2 = ParseDiNode("diP2", node, ioModule);
  111. _diP3 = ParseDiNode("diP3", node, ioModule);
  112. _diABSAlarm= ParseDiNode("diABSAlarm", node, ioModule);
  113. _diORGAlarm= ParseDiNode("diORGAlarm", node, ioModule);
  114. _diServoAlarm = ParseDiNode("diServoAlarm", node, ioModule);
  115. _diCCWLimitSensorAlarm = ParseDiNode("diCCWLimitAlarm", node, ioModule);
  116. _diOverSoftwareLimitAlarm = ParseDiNode("diOverSoftwareLimitAlarm", node, ioModule);
  117. _doReset= ParseDoNode("doReset", node, ioModule);
  118. _doOrigin = ParseDoNode("doOrigin", node, ioModule);
  119. _doP1 = ParseDoNode("doP1", node, ioModule);
  120. _doP2 = ParseDoNode("doP2", node, ioModule);
  121. _doP3 = ParseDoNode("doP3", node, ioModule);
  122. _doStop = ParseDoNode("doStop", node, ioModule);
  123. _doUp = ParseDoNode("doUp", node, ioModule);
  124. _doDown = ParseDoNode("doDown", node, ioModule);
  125. _currentValue = ParseAiNode("aiCurrentValue", node, ioModule);
  126. _aoSetP1 = ParseAoNode("aoSetP1", node, ioModule);
  127. _aoSetP2 = ParseAoNode("aoSetP2", node, ioModule);
  128. _aoSetP3 = ParseAoNode("aoSetP3", node, ioModule);
  129. _aoServoEnable = ParseAoNode("aoServoEnable", node, ioModule);
  130. _aoAutoSpeed = ParseAoNode("aoAutoSpeed", node, ioModule);
  131. _aoManualSpeed = ParseAoNode("aoManualSpeed", node, ioModule);
  132. _aoAccTime = ParseAoNode("aoAccTime", node, ioModule);
  133. _aoDecTime = ParseAoNode("aoDecTime", node, ioModule);
  134. _aoCCWLimitPosition = ParseAoNode("aoCCWLimitPosition", node, ioModule);
  135. _aoCWLimitPosition = ParseAoNode("aoCWLimitPosition", node, ioModule);
  136. _aoCorrectionValue = ParseAoNode("aoCorrectionValue", node, ioModule);
  137. }
  138. private void updatePinCfg()
  139. {
  140. // AO-27, Lift Servo Enable: 0=Lift Pin ,1=Lift Servo
  141. _SetRealFloat(_aoServoEnable, 1);
  142. void _updateItem(string data, AOAccessor ao)
  143. {
  144. var value = (float)SC.GetValue<double>($"{Module}.{Name}.{data}");
  145. _SetRealFloat(ao, value);
  146. }
  147. _updateItem("AutoSpeed", _aoAutoSpeed);
  148. _updateItem("ManualSpeed", _aoManualSpeed);
  149. _updateItem("AccTime", _aoAccTime);
  150. _updateItem("DecTime", _aoDecTime);
  151. _updateItem("CCWLimitPosition", _aoCCWLimitPosition);
  152. _updateItem("CWLimitPosition", _aoCWLimitPosition);
  153. _updateItem("CorrectionValue", _aoCorrectionValue);
  154. _updateItem("Position1", _aoSetP1);
  155. _updateItem("Position2", _aoSetP2);
  156. _updateItem("Position3", _aoSetP3);
  157. }
  158. public bool GoPosition(Position position)
  159. {
  160. if (_diABSAlarm.Value)
  161. return false;
  162. _currentTarget = position;
  163. sw.Restart();
  164. switch (position)
  165. {
  166. case Position.position1:
  167. _doP1.Value = true;
  168. _doP2.Value = false;
  169. _doP3.Value = false;
  170. break;
  171. case Position.position2:
  172. _doP1.Value = false;
  173. _doP2.Value = true;
  174. _doP3.Value = false;
  175. break;
  176. case Position.position3:
  177. _doP1.Value = false;
  178. _doP2.Value = false;
  179. _doP3.Value = true;
  180. break;
  181. case Position.origin:
  182. {
  183. if (_diOrigin.Value)
  184. {
  185. EV.PostInfoLog(Module, $"Lift Pin already on original position.");
  186. sw.Stop();
  187. return true;
  188. }
  189. _doOrigin.Value = true;
  190. }
  191. break;
  192. }
  193. EV.PostInfoLog(Module, $"Lift Pin goto {_currentTarget}");
  194. return true;
  195. }
  196. public bool ManulStop()
  197. {
  198. if (_diABSAlarm.Value)
  199. return false;
  200. _doUp.Value = false;
  201. _doDown.Value = false;
  202. _doStop.Value = true;
  203. _manualStopTimer.Restart();
  204. return true;
  205. }
  206. public bool ManulUp()
  207. {
  208. if (_diABSAlarm.Value)
  209. return false;
  210. _doDown.Value = false;
  211. _doStop.Value = false;
  212. _doUp.Value = !_doUp.Value;
  213. return true;
  214. }
  215. public bool ManulDown()
  216. {
  217. if (_diABSAlarm.Value)
  218. return false;
  219. _doUp.Value = false;
  220. _doStop.Value = false;
  221. _doDown.Value = !_doDown.Value;
  222. return true;
  223. }
  224. public float CurrentValue
  225. {
  226. get
  227. {
  228. if (_currentValue == null)
  229. {
  230. return 0;
  231. }
  232. return _GetRealFloat(_currentValue); ;
  233. }
  234. }
  235. public bool Initialize()
  236. {
  237. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  238. DATA.Subscribe($"{Module}.{Name}.PinPosition", () => PinPositionint);
  239. DATA.Subscribe($"{Module}.{Name}.CurrentValue", () => CurrentValue);
  240. DATA.Subscribe($"{Module}.{Name}.ManualStopState", () => _doStop.Value);
  241. DATA.Subscribe($"{Module}.{Name}.ManualUpState", () => _doUp.Value);
  242. DATA.Subscribe($"{Module}.{Name}.ManualDownState", () => _doDown.Value) ;
  243. OP.Subscribe($"{Module}.{Name}.SetState", (out string reason, int time, object[] param) => {
  244. reason = string.Empty;
  245. Position pos = Position.Invalid;
  246. if ((string)param[0] == "Up")
  247. pos = Position.position1;
  248. else if ((string)param[0] == "Down")
  249. pos = Position.position3;
  250. else if ((string)param[0] == "Middle")
  251. pos = Position.position2;
  252. else
  253. {
  254. reason = "Invalid moving position";
  255. return false;
  256. }
  257. GoPosition(pos);
  258. return true;
  259. });
  260. OP.Subscribe($"{Module}.{Name}.Stop", (out string reason, int time, object[] param) => {
  261. reason = string.Empty;
  262. ManulStop();
  263. return true;
  264. });
  265. OP.Subscribe($"{Module}.{Name}.Up", (out string reason, int time, object[] param) => {
  266. reason = string.Empty;
  267. ManulUp();
  268. return true;
  269. });
  270. OP.Subscribe($"{Module}.{Name}.Down", (out string reason, int time, object[] param) => {
  271. reason = string.Empty;
  272. ManulDown();
  273. return true;
  274. });
  275. OP.Subscribe($"{Module}.{Name}.Home", (out string reason, int time, object[] param) => {
  276. reason = string.Empty;
  277. GoPosition(Position.origin);
  278. return true;
  279. });
  280. OP.Subscribe($"{Module}.{Name}.UpdateConfig", (out string reason, int time, object[] param) => {
  281. reason = string.Empty;
  282. return true;
  283. });
  284. updatePinCfg();
  285. return true;
  286. }
  287. public void Terminate()
  288. {
  289. }
  290. public void Monitor()
  291. {
  292. if (_manualStopTimer.ElapsedMilliseconds > _stopButtonAutoResetTime)
  293. {
  294. _doStop.Value = false;
  295. _manualStopTimer.Stop();
  296. }
  297. if (_diORGAlarm.Value)
  298. {
  299. NoDuplicatedAlarm($"Lift Pin DI-{_diORGAlarm.Index} 回原点超时");
  300. }
  301. if (_diServoAlarm.Value)
  302. {
  303. NoDuplicatedAlarm($"Lift Pin DI-{_diServoAlarm.Index} 伺服驱动器报警");
  304. }
  305. if (_diCCWLimitSensorAlarm.Value)
  306. {
  307. NoDuplicatedAlarm($"Lift Pin DI-{_diCCWLimitSensorAlarm.Index} CCW Limit sensor 报警");
  308. }
  309. if (_diOverSoftwareLimitAlarm.Value)
  310. {
  311. NoDuplicatedAlarm($"Lift Pin DI-{_diOverSoftwareLimitAlarm.Index} Over Software Limit 报警");
  312. }
  313. if (_diABSAlarm.Value)
  314. {
  315. NoDuplicatedAlarm($"Lift Pin DI-{_diABSAlarm.Index} ABS 报警");
  316. }
  317. if (_currentTarget == Position.Invalid)
  318. return;
  319. if ((_currentTarget == Position.position1 && _diP1.Value) ||
  320. (_currentTarget == Position.position2 && _diP2.Value) ||
  321. (_currentTarget == Position.position3 && _diP3.Value) ||
  322. (_currentTarget == Position.origin && _diOrigin.Value))
  323. {
  324. EV.PostInfoLog(Module, $"Lift Pin 到达 {_currentTarget}");
  325. Reset();
  326. return;
  327. }
  328. if (sw.ElapsedMilliseconds > _timeout)
  329. {
  330. NoDuplicatedAlarm($"Lift Pin 超时, 未到达 {_currentTarget}");
  331. }
  332. }
  333. public void Reset()
  334. {
  335. _currentTarget = Position.Invalid;
  336. sw.Reset();
  337. _doP1.Value = false;
  338. _doP2.Value = false;
  339. _doP3.Value = false;
  340. _doOrigin.Value = false;
  341. _doUp.Value = false;
  342. _doDown.Value = false;
  343. _doStop.Value = false;
  344. _bAlarmReported = false;
  345. EV.PostInfoLog(Module, $"Lift Pin reset all do to off.");
  346. }
  347. private void NoDuplicatedAlarm(string log)
  348. {
  349. if (_bAlarmReported == false)
  350. {
  351. EV.PostAlarmLog(Module, log);
  352. _bAlarmReported = true;
  353. }
  354. }
  355. }
  356. }