IoTriStateLift2.cs 14 KB

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