IoTriStateLift2.cs 13 KB

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