IoTriStateLift2.cs 19 KB

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