IoTriStateLift2.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 class IoTriStateLift2 : BaseDevice, IDevice
  14. {
  15. private readonly DIAccessor _diOrigin;//是否到达原点
  16. private readonly DIAccessor _diOrigin2;
  17. private readonly DIAccessor _diOrigin3;
  18. private readonly DIAccessor _diP1;//是否到达位置1
  19. private readonly DIAccessor _diP2;//是否到达位置2
  20. private readonly DIAccessor _diP3;//是否到达位置3
  21. private readonly DIAccessor _diCOMMAlarm;//去位置1,2,3超时
  22. private readonly DIAccessor _diBatteryLowAlarm;//去原点超时
  23. private readonly DIAccessor _di1_L_Servo_Driver_Alarm;
  24. private readonly DIAccessor _di1_L_Servo_Driver_Warning;
  25. private readonly DIAccessor _di1_Search_ORG_Fail_Alarm;
  26. private readonly DIAccessor _di1_ABS_Fail_Alarm;
  27. private readonly DIAccessor _di1_Up_Limit_Alarm;
  28. private readonly DIAccessor _di1_Down_Limit_Alarm;
  29. private readonly DIAccessor _di1_Over_Software_Alarm;
  30. private readonly DIAccessor _di2_L_Servo_Driver_Alarm;
  31. private readonly DIAccessor _di2_L_Servo_Driver_Warning;
  32. private readonly DIAccessor _di2_Search_ORG_Fail_Alarm;
  33. private readonly DIAccessor _di2_ABS_Fail_Alarm;
  34. private readonly DIAccessor _di2_Up_Limit_Alarm;
  35. private readonly DIAccessor _di2_Down_Limit_Alarm;
  36. private readonly DIAccessor _di2_Over_Software_Alarm;
  37. // private readonly DIAccessor _diServoAlarm; // servo alarm
  38. // private readonly DIAccessor _diCCWLimitSensorAlarm; //
  39. //private readonly DIAccessor _diOverSoftwareLimitAlarm; //
  40. private readonly DOAccessor _doReset;
  41. private readonly DOAccessor _doOrigin;//去原点
  42. private readonly DOAccessor _doP1;//去位置1
  43. private readonly DOAccessor _doP2;//去位置2
  44. private readonly DOAccessor _doP3;//去位置3
  45. private readonly DOAccessor _doStop;
  46. private readonly DOAccessor _doUp;
  47. private readonly DOAccessor _doDown;
  48. private readonly AIAccessor _currentValue;
  49. private readonly AOAccessor _aoSetP1;
  50. private readonly AOAccessor _aoSetP2;
  51. private readonly AOAccessor _aoSetP3;
  52. private readonly AOAccessor _aoServoEnable;
  53. private readonly AOAccessor _aoServoWorkMode;
  54. private readonly AOAccessor _aoOriginSpeed;
  55. private readonly AOAccessor _aoAutoSpeed;
  56. private readonly AOAccessor _aoManualSpeed;
  57. private readonly AOAccessor _aoAccDecSpeedTime;
  58. private readonly AOAccessor _aoPosDiff;
  59. private readonly AOAccessor _aoDecTime;
  60. private readonly AOAccessor _aoSoftUpLimit;
  61. private readonly AOAccessor _aoSoftDownLimit;
  62. private readonly AOAccessor _aoCorrectionValue;
  63. private Stopwatch swOrigin = new Stopwatch();
  64. private Stopwatch sw = new Stopwatch();
  65. private Stopwatch _manualStopTimer = new Stopwatch();
  66. private readonly int _stopButtonAutoResetTime = 1000;
  67. private Position _currentTarget = Position.Invalid;
  68. private Position _preTarget = Position.Invalid;
  69. long _timeout = 60000;
  70. private bool _bAlarmReported = false;
  71. private AITTriStateLiftPinData DeviceData
  72. {
  73. get
  74. {
  75. AITTriStateLiftPinData deviceData = new AITTriStateLiftPinData
  76. {
  77. Module = Module,
  78. DeviceName = Name,
  79. DeviceSchematicId = DeviceID,
  80. DisplayName = Display,
  81. };
  82. return deviceData;
  83. }
  84. }
  85. public MovementPosition PinPosition
  86. {
  87. get
  88. {
  89. if ((_preTarget == Position.position1 && _diP1.Value) && _diP2.Value == false && _diP3.Value == false)
  90. return MovementPosition.Up;
  91. else if (_diP1.Value == false && (_diP2.Value && _preTarget == Position.position2) && _diP3.Value == false)
  92. return MovementPosition.Middle;
  93. else if (_diP1.Value == false && _diP2.Value == false && (_diP3.Value && _preTarget == Position.position3))
  94. return MovementPosition.Down;
  95. else if (_diOrigin.Value && _diOrigin2.Value && _diOrigin3.Value && _preTarget == Position.origin)
  96. return MovementPosition.Origin;
  97. return MovementPosition.Unknown;
  98. }
  99. }
  100. public int PinPositionint
  101. {
  102. get
  103. {
  104. if (PinPosition == MovementPosition.Up)
  105. return 3;
  106. else if (PinPosition == MovementPosition.Middle)
  107. return 2;
  108. else if (PinPosition == MovementPosition.Down)
  109. return 1;
  110. else if (PinPosition == MovementPosition.Origin)
  111. return 0;
  112. return -1;
  113. }
  114. }
  115. public IoTriStateLift2(string module, XmlElement node, string ioModule = "")
  116. {
  117. base.Module = module;
  118. base.Name = node.GetAttribute("id");
  119. base.Display = node.GetAttribute("display");
  120. base.DeviceID = node.GetAttribute("schematicId");
  121. _diOrigin = ParseDiNode("diOrigin", node, ioModule);
  122. _diOrigin2 = ParseDiNode("diOrigin2", node, ioModule);
  123. _diOrigin3 = ParseDiNode("diOrigin3", node, ioModule);
  124. _diP1 = ParseDiNode("diP1", node, ioModule);
  125. _diP2 = ParseDiNode("diP2", node, ioModule);
  126. _diP3 = ParseDiNode("diP3", node, ioModule);
  127. _diCOMMAlarm = ParseDiNode("diCOMMAlarm", node, ioModule);
  128. _diBatteryLowAlarm = ParseDiNode("diBatteryLowAlarm", node, ioModule);
  129. _di1_L_Servo_Driver_Alarm = ParseDiNode("di1_L_Servo_Driver_Alarm", node, ioModule);
  130. _di1_L_Servo_Driver_Warning = ParseDiNode("di1_L_Servo_Driver_Warning", node, ioModule);
  131. _di1_Search_ORG_Fail_Alarm = ParseDiNode("di1_Search_ORG_Fail_Alarm", node, ioModule);
  132. _di1_ABS_Fail_Alarm = ParseDiNode("di1_ABS_Fail_Alarm", node, ioModule);
  133. _di1_Up_Limit_Alarm = ParseDiNode("di1_Up_Limit_Alarm", node, ioModule);
  134. _di1_Down_Limit_Alarm = ParseDiNode("di1_Down_Limit_Alarm", node, ioModule);
  135. _di1_Over_Software_Alarm = ParseDiNode("di1_Over_Software_Alarm", node, ioModule);
  136. _di2_L_Servo_Driver_Alarm = ParseDiNode("di2_L_Servo_Driver_Alarm", node, ioModule);
  137. _di2_L_Servo_Driver_Warning = ParseDiNode("di2_L_Servo_Driver_Warning", node, ioModule);
  138. _di2_Search_ORG_Fail_Alarm = ParseDiNode("di2_Search_ORG_Fail_Alarm", node, ioModule);
  139. _di2_ABS_Fail_Alarm = ParseDiNode("di2_ABS_Fail_Alarm", node, ioModule);
  140. _di2_Up_Limit_Alarm = ParseDiNode("di2_Up_Limit_Alarm", node, ioModule);
  141. _di2_Down_Limit_Alarm = ParseDiNode("di2_Down_Limit_Alarm", node, ioModule);
  142. _di2_Over_Software_Alarm = ParseDiNode("di2_Over_Software_Alarm", node, ioModule);
  143. _doReset = ParseDoNode("doReset", node, ioModule);
  144. _doOrigin = ParseDoNode("doOrigin", node, ioModule);
  145. _doP1 = ParseDoNode("doP1", node, ioModule);
  146. _doP2 = ParseDoNode("doP2", node, ioModule);
  147. _doP3 = ParseDoNode("doP3", node, ioModule);
  148. _doStop = ParseDoNode("doStop", node, ioModule);
  149. _doUp = ParseDoNode("doUp", node, ioModule);
  150. _doDown = ParseDoNode("doDown", node, ioModule);
  151. _currentValue = ParseAiNode("aiCurrentValue", node, ioModule);
  152. _aoSetP1 = ParseAoNode("aoSetP1", node, ioModule);
  153. _aoSetP2 = ParseAoNode("aoSetP2", node, ioModule);
  154. _aoSetP3 = ParseAoNode("aoSetP3", node, ioModule);
  155. _aoServoEnable = ParseAoNode("aoServoEnable", node, ioModule);
  156. _aoServoWorkMode = ParseAoNode("aoServoWorkMode", node, ioModule);
  157. _aoOriginSpeed = ParseAoNode("aoOriginSpeed", node, ioModule);
  158. _aoAutoSpeed = ParseAoNode("aoAutoSpeed", node, ioModule);
  159. _aoManualSpeed = ParseAoNode("aoManualSpeed", node, ioModule);
  160. _aoSoftUpLimit = ParseAoNode("aoSoftUpLimit", node, ioModule);
  161. _aoSoftDownLimit = ParseAoNode("aoSoftDownLimit", node, ioModule);
  162. _aoAccDecSpeedTime = ParseAoNode("aoAccDecSpeedTime", node, ioModule);
  163. _aoPosDiff = ParseAoNode("aoPosDiff", node, ioModule);
  164. }
  165. private void updatePinCfg()
  166. {
  167. // AO-27, Lift Servo Enable: 0=Lift Pin ,1=Lift Servo
  168. _SetRealFloat(_aoServoEnable, 1);
  169. void _updateItem(string data, AOAccessor ao)
  170. {
  171. var value = (float)SC.GetValue<double>($"{Module}.{Name}.{data}");
  172. _SetRealFloat(ao, value);
  173. }
  174. _updateItem("ServoWorkMode", _aoOriginSpeed);
  175. _updateItem("OriginSpeed", _aoOriginSpeed);
  176. _updateItem("AutoSpeed", _aoAutoSpeed);
  177. _updateItem("ManualSpeed", _aoManualSpeed);
  178. _updateItem("SoftUpLimit", _aoSoftUpLimit);
  179. _updateItem("SoftDownLimit", _aoSoftDownLimit);
  180. _updateItem("AccDecSpeedTime", _aoAccDecSpeedTime);
  181. _updateItem("Position1", _aoSetP1);
  182. _updateItem("Position2", _aoSetP2);
  183. _updateItem("Position3", _aoSetP3);
  184. _updateItem("PosDiff", _aoPosDiff);
  185. }
  186. public bool GoPosition(Position position)
  187. {
  188. if (_diCOMMAlarm.Value)
  189. return false;
  190. _currentTarget = position;
  191. _preTarget = position;
  192. sw.Restart();
  193. swOrigin.Restart();
  194. switch (position)
  195. {
  196. case Position.position1:
  197. _doP1.Value = true;
  198. _doP2.Value = false;
  199. _doP3.Value = false;
  200. break;
  201. case Position.position2:
  202. _doP1.Value = false;
  203. _doP2.Value = true;
  204. _doP3.Value = false;
  205. break;
  206. case Position.position3:
  207. _doP1.Value = false;
  208. _doP2.Value = false;
  209. _doP3.Value = true;
  210. break;
  211. case Position.origin:
  212. {
  213. //if (_diOrigin.Value && _diOrigin2.Value && _diOrigin3.Value)
  214. //{
  215. // EV.PostInfoLog(Module, $"Lift Pin already on original position.");
  216. // sw.Stop();
  217. // return true;
  218. //}
  219. _doOrigin.Value = true;
  220. }
  221. break;
  222. }
  223. EV.PostInfoLog(Module, $"Lift Pin goto {_currentTarget}");
  224. return true;
  225. }
  226. public bool ManulStop()
  227. {
  228. if (_diCOMMAlarm.Value)
  229. return false;
  230. _doUp.Value = false;
  231. _doDown.Value = false;
  232. _doStop.Value = true;
  233. _manualStopTimer.Restart();
  234. return true;
  235. }
  236. public bool ManulUp()
  237. {
  238. if (_diCOMMAlarm.Value)
  239. return false;
  240. _doDown.Value = false;
  241. _doStop.Value = false;
  242. _doUp.Value = !_doUp.Value;
  243. return true;
  244. }
  245. public bool ManulDown()
  246. {
  247. if (_diCOMMAlarm.Value)
  248. return false;
  249. _doUp.Value = false;
  250. _doStop.Value = false;
  251. _doDown.Value = !_doDown.Value;
  252. return true;
  253. }
  254. public float CurrentValue
  255. {
  256. get
  257. {
  258. if (_currentValue == null)
  259. {
  260. return 0;
  261. }
  262. return _GetRealFloat(_currentValue); ;
  263. }
  264. }
  265. public bool Initialize()
  266. {
  267. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  268. DATA.Subscribe($"{Module}.{Name}.PinPosition", () => PinPositionint);
  269. DATA.Subscribe($"{Module}.{Name}.CurrentValue", () => CurrentValue);
  270. DATA.Subscribe($"{Module}.{Name}.ManualStopState", () => _doStop.Value);
  271. DATA.Subscribe($"{Module}.{Name}.ManualUpState", () => _doUp.Value);
  272. DATA.Subscribe($"{Module}.{Name}.ManualDownState", () => _doDown.Value);
  273. OP.Subscribe($"{Module}.{Name}.SetState", (out string reason, int time, object[] param) => {
  274. reason = string.Empty;
  275. Position pos = Position.Invalid;
  276. if ((string)param[0] == "Up")
  277. pos = Position.position1;
  278. else if ((string)param[0] == "Down")
  279. pos = Position.position3;
  280. else if ((string)param[0] == "Middle")
  281. pos = Position.position2;
  282. else
  283. {
  284. reason = "Invalid moving position";
  285. return false;
  286. }
  287. GoPosition(pos);
  288. return true;
  289. });
  290. OP.Subscribe($"{Module}.{Name}.Stop", (out string reason, int time, object[] param) => {
  291. reason = string.Empty;
  292. ManulStop();
  293. return true;
  294. });
  295. OP.Subscribe($"{Module}.{Name}.Up", (out string reason, int time, object[] param) => {
  296. reason = string.Empty;
  297. ManulUp();
  298. return true;
  299. });
  300. OP.Subscribe($"{Module}.{Name}.Down", (out string reason, int time, object[] param) => {
  301. reason = string.Empty;
  302. ManulDown();
  303. return true;
  304. });
  305. OP.Subscribe($"{Module}.{Name}.Home", (out string reason, int time, object[] param) => {
  306. reason = string.Empty;
  307. GoPosition(Position.origin);
  308. return true;
  309. });
  310. OP.Subscribe($"{Module}.{Name}.UpdateConfig", (out string reason, int time, object[] param) => {
  311. reason = string.Empty;
  312. return true;
  313. });
  314. updatePinCfg();
  315. return true;
  316. }
  317. public void Terminate()
  318. {
  319. }
  320. public void Monitor()
  321. {
  322. if (_manualStopTimer.ElapsedMilliseconds > _stopButtonAutoResetTime)
  323. {
  324. _doStop.Value = false;
  325. _manualStopTimer.Stop();
  326. }
  327. if (_diBatteryLowAlarm.Value)
  328. {
  329. NoDuplicatedAlarm($"Lift Pin DI-{_diBatteryLowAlarm.Index} alarm");
  330. }
  331. if (_diCOMMAlarm.Value)
  332. {
  333. NoDuplicatedAlarm($"Lift Pin DI-{_diCOMMAlarm.Index} alarm");
  334. }
  335. if(_di1_L_Servo_Driver_Alarm.Value)
  336. {
  337. NoDuplicatedAlarm($"Lift Pin DI-{_di1_L_Servo_Driver_Alarm.Index} alarm");
  338. }
  339. if (_di1_L_Servo_Driver_Warning.Value)
  340. {
  341. NoDuplicatedAlarm($"Lift Pin DI-{_di1_L_Servo_Driver_Warning.Index} alarm");
  342. }
  343. if (_di1_Search_ORG_Fail_Alarm.Value)
  344. {
  345. NoDuplicatedAlarm($"Lift Pin DI-{_di1_Search_ORG_Fail_Alarm.Index} alarm");
  346. }
  347. if (_di1_ABS_Fail_Alarm.Value)
  348. {
  349. NoDuplicatedAlarm($"Lift Pin DI-{_di1_ABS_Fail_Alarm.Index} alarm");
  350. }
  351. if (_di1_Up_Limit_Alarm.Value)
  352. {
  353. NoDuplicatedAlarm($"Lift Pin DI-{_di1_Up_Limit_Alarm.Index} alarm");
  354. }
  355. if (_di1_Down_Limit_Alarm.Value)
  356. {
  357. NoDuplicatedAlarm($"Lift Pin DI-{_di1_Down_Limit_Alarm.Index} alarm");
  358. }
  359. if (_di1_Over_Software_Alarm.Value)
  360. {
  361. NoDuplicatedAlarm($"Lift Pin DI-{_di1_Over_Software_Alarm.Index} alarm");
  362. }
  363. if (_di2_L_Servo_Driver_Alarm.Value)
  364. {
  365. NoDuplicatedAlarm($"Lift Pin DI-{_di2_L_Servo_Driver_Alarm.Index} alarm");
  366. }
  367. if (_di2_L_Servo_Driver_Warning.Value)
  368. {
  369. NoDuplicatedAlarm($"Lift Pin DI-{_di2_L_Servo_Driver_Warning.Index} alarm");
  370. }
  371. if (_di2_Search_ORG_Fail_Alarm.Value)
  372. {
  373. NoDuplicatedAlarm($"Lift Pin DI-{_di2_Search_ORG_Fail_Alarm.Index} alarm");
  374. }
  375. if (_di2_ABS_Fail_Alarm.Value)
  376. {
  377. NoDuplicatedAlarm($"Lift Pin DI-{_di2_ABS_Fail_Alarm.Index} alarm");
  378. }
  379. if (_di2_Up_Limit_Alarm.Value)
  380. {
  381. NoDuplicatedAlarm($"Lift Pin DI-{_di2_Up_Limit_Alarm.Index} alarm");
  382. }
  383. if (_di2_Down_Limit_Alarm.Value)
  384. {
  385. NoDuplicatedAlarm($"Lift Pin DI-{_di2_Down_Limit_Alarm.Index} alarm");
  386. }
  387. if (_di2_Over_Software_Alarm.Value)
  388. {
  389. NoDuplicatedAlarm($"Lift Pin DI-{_di2_Over_Software_Alarm.Index} alarm");
  390. }
  391. if (_currentTarget == Position.Invalid)
  392. return;
  393. if ((_currentTarget == Position.position1 && _diP1.Value) ||
  394. (_currentTarget == Position.position2 && _diP2.Value) ||
  395. (_currentTarget == Position.position3 && _diP3.Value) ||
  396. (_currentTarget == Position.origin && swOrigin.ElapsedMilliseconds > 1000 && (_diOrigin.Value && _diOrigin2.Value && _diOrigin3.Value)))
  397. {
  398. EV.PostInfoLog(Module, $"Lift Pin arrive {_currentTarget}, ({_diOrigin.Index})_DI_Lift_Servo_1_Servo_Origin_Complete={_diOrigin.Value} ({_diOrigin2.Index})_DI_Lift_Servo_2_Servo_Origin_Complete={_diOrigin2.Value} ({_diOrigin3.Index})_DI_Lift_Servo_Group_Go_Pos_Origin_Complete={_diOrigin3.Value} ");
  399. Reset();
  400. return;
  401. }
  402. if (sw.ElapsedMilliseconds > _timeout)
  403. {
  404. NoDuplicatedAlarm($"Lift Pin timeout, go {_currentTarget} failed");
  405. }
  406. }
  407. public void Reset()
  408. {
  409. _currentTarget = Position.Invalid;
  410. sw.Reset();
  411. swOrigin.Reset();
  412. _doP1.Value = false;
  413. _doP2.Value = false;
  414. _doP3.Value = false;
  415. _doOrigin.Value = false;
  416. _doUp.Value = false;
  417. _doDown.Value = false;
  418. _doStop.Value = false;
  419. _bAlarmReported = false;
  420. EV.PostInfoLog(Module, $"Lift Pin reset all do to off.");
  421. }
  422. private void NoDuplicatedAlarm(string log)
  423. {
  424. if (_bAlarmReported == false)
  425. {
  426. EV.PostAlarmLog(Module, log);
  427. _bAlarmReported = true;
  428. }
  429. }
  430. }
  431. }