IoBufferMotor.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. using Aitex.Core.RT.DataCenter;
  2. using Aitex.Core.RT.Device;
  3. using Aitex.Core.RT.Event;
  4. using Aitex.Core.RT.IOCore;
  5. using Aitex.Core.RT.Log;
  6. using Aitex.Core.RT.OperationCenter;
  7. using Aitex.Core.RT.SCCore;
  8. using Aitex.Core.Util;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Xml;
  15. namespace FurnaceRT.Devices
  16. {
  17. public class IoBufferMotor : BaseDevice, IDevice
  18. {
  19. #region fields
  20. protected enum State
  21. {
  22. Idle,
  23. Moving,
  24. ServoReset,
  25. Stopping,
  26. }
  27. protected State _state = State.Idle;
  28. protected DIAccessor _diServoOn;
  29. protected DIAccessor _diMoving;
  30. protected DIAccessor _diSensor1;
  31. protected DIAccessor _diSensor2;
  32. protected DIAccessor _diSensor3;
  33. protected DIAccessor _diSensor4;
  34. protected DIAccessor _diSensor5;
  35. protected DIAccessor _diSensor6;
  36. protected DIAccessor _diSensor7;
  37. protected DIAccessor _diSensor8;
  38. protected DIAccessor _diWarning;
  39. protected DIAccessor _diAlarm;
  40. protected DIAccessor _diNegativeLimit;
  41. protected DIAccessor _diPositiveLimit;
  42. protected DIAccessor _diPositionA1;
  43. protected DIAccessor _diPositionB1;
  44. protected DIAccessor _diPositionC1;
  45. protected DIAccessor _diPositionD1;
  46. protected DIAccessor _diPositionA2;
  47. protected DIAccessor _diPositionB2;
  48. protected DIAccessor _diPositionC2;
  49. protected DIAccessor _diPositionD2;
  50. protected DIAccessor _diPositionA3;
  51. protected DIAccessor _diPositionB3;
  52. protected DIAccessor _diPositionC3;
  53. protected DIAccessor _diPositionD3;
  54. protected DIAccessor _diPositionA4;
  55. protected DIAccessor _diPositionB4;
  56. protected DIAccessor _diPositionC4;
  57. protected DIAccessor _diPositionD4;
  58. protected DOAccessor _doServoOn;
  59. protected DOAccessor _doStop;
  60. protected DOAccessor _doMove;
  61. protected DOAccessor _doReset;
  62. protected AIAccessor _aiRealPosition;
  63. protected AIAccessor _aiRealSpeed;
  64. protected AIAccessor _aiRealTorque;
  65. protected AIAccessor _aiDriverErrorCode;
  66. protected AIAccessor _aiMotionErrorCode;
  67. protected AIAccessor _aiTargetPosFb;
  68. protected AOAccessor _aoTargetPosition;
  69. protected AOAccessor _aoSpeed;
  70. protected AOAccessor _aoAcc;
  71. protected AOAccessor _aoDec;
  72. protected SCConfigItem _scMoveTimeout;
  73. protected DeviceTimer _timer = new DeviceTimer();
  74. private const float PositionTolerance = 0.1f;
  75. private int setTime = 1500; //200ms set DO
  76. private int resetTime = 1200; //1200ms reset DO
  77. private int moveTimeout => (int)(_scMoveTimeout.IntValue * 1000);
  78. private PeriodicJob _thread;
  79. private R_TRIG _warningTrig = new R_TRIG();
  80. private R_TRIG _alarmTrig = new R_TRIG();
  81. public bool ServoOnOffSet
  82. {
  83. set
  84. {
  85. if (_doServoOn == null)
  86. return;
  87. _doServoOn.Value = value;
  88. }
  89. }
  90. public float ServoMovePositionSet
  91. {
  92. set
  93. {
  94. if (_aoTargetPosition == null)
  95. return;
  96. _aoTargetPosition.FloatValue = value;
  97. }
  98. get
  99. {
  100. return _aoTargetPosition.FloatValue;
  101. }
  102. }
  103. private bool _isFloatAioType = false;
  104. #endregion
  105. #region properties
  106. public float CurrentPosition => _aiRealPosition.FloatValue;
  107. public float CurrentSpeed => _aiRealSpeed.FloatValue;
  108. public bool IsError { get; private set; }
  109. public bool IsRunning => _diMoving == null ? false : _diMoving.Value;
  110. public bool IsReady
  111. {
  112. get
  113. {
  114. if (_diMoving != null && _diMoving.Value)
  115. return false;
  116. return _state == State.Idle;
  117. }
  118. }
  119. public bool IsInPosition
  120. {
  121. get
  122. {
  123. if (_aoTargetPosition != null)
  124. {
  125. switch (_aoTargetPosition.FloatValue)
  126. {
  127. case 1:
  128. return (_diSensor1 != null ? _diSensor1.Value : true) && (_diMoving != null ? !_diMoving.Value : true) && (_diPositionA1 != null ? _diPositionA1.Value : true);
  129. case 9:
  130. return (_diSensor1 != null ? _diSensor1.Value : true) && (_diMoving != null ? !_diMoving.Value : true) && (_diPositionA3 != null ? _diPositionA3.Value : true);
  131. case 2:
  132. return (_diSensor2 != null ? _diSensor2.Value : true) && (_diMoving != null ? !_diMoving.Value : true) && (_diPositionB1 != null ? _diPositionB1.Value : true);
  133. case 10:
  134. return (_diSensor2 != null ? _diSensor2.Value : true) && (_diMoving != null ? !_diMoving.Value : true) && (_diPositionB3 != null ? _diPositionB3.Value : true);
  135. case 3:
  136. return (_diSensor3 != null ? _diSensor3.Value : true) && (_diMoving != null ? !_diMoving.Value : true) && (_diPositionC1 != null ? _diPositionC1.Value : true);
  137. case 11:
  138. return (_diSensor3 != null ? _diSensor3.Value : true) && (_diMoving != null ? !_diMoving.Value : true) && (_diPositionC3 != null ? _diPositionC3.Value : true);
  139. case 4:
  140. return (_diSensor4 != null ? _diSensor4.Value : true) && (_diMoving != null ? !_diMoving.Value : true) && (_diPositionD1 != null ? _diPositionD1.Value : true);
  141. case 12:
  142. return (_diSensor4 != null ? _diSensor4.Value : true) && (_diMoving != null ? !_diMoving.Value : true) && (_diPositionD3 != null ? _diPositionD3.Value : true);
  143. case 5:
  144. return (_diSensor5 != null ? _diSensor5.Value : true) && (_diMoving != null ? !_diMoving.Value : true) && (_diPositionA2 != null ? _diPositionA2.Value : true);
  145. case 13:
  146. return (_diSensor5 != null ? _diSensor5.Value : true) && (_diMoving != null ? !_diMoving.Value : true) && (_diPositionA4 != null ? _diPositionA4.Value : true);
  147. case 6:
  148. return (_diSensor6 != null ? _diSensor6.Value : true) && (_diMoving != null ? !_diMoving.Value : true) && (_diPositionB2 != null ? _diPositionB2.Value : true);
  149. case 14:
  150. return (_diSensor6 != null ? _diSensor6.Value : true) && (_diMoving != null ? !_diMoving.Value : true) && (_diPositionB4 != null ? _diPositionB4.Value : true);
  151. case 7:
  152. return (_diSensor7 != null ? _diSensor7.Value : true) && (_diMoving != null ? !_diMoving.Value : true) && (_diPositionC2 != null ? _diPositionC2.Value : true);
  153. case 15:
  154. return (_diSensor7 != null ? _diSensor7.Value : true) && (_diMoving != null ? !_diMoving.Value : true) && (_diPositionC4 != null ? _diPositionC4.Value : true);
  155. case 8:
  156. return (_diSensor8 != null ? _diSensor8.Value : true) && (_diMoving != null ? !_diMoving.Value : true) && (_diPositionD2 != null ? _diPositionD2.Value : true);
  157. case 16:
  158. return (_diSensor8 != null ? _diSensor8.Value : true) && (_diMoving != null ? !_diMoving.Value : true) && (_diPositionD4 != null ? _diPositionD4.Value : true);
  159. }
  160. }
  161. return false;
  162. }
  163. }
  164. public bool IsServoOn
  165. {
  166. get
  167. {
  168. if (_diServoOn == null)
  169. return true;
  170. return _diServoOn.Value;
  171. }
  172. }
  173. public int TargetPositionFb => (int)(_aiTargetPosFb.FloatValue + 0.00001);
  174. public string ErrorCode => $"{(_aiDriverErrorCode != null ? ((int)(_aiDriverErrorCode.FloatValue + 0.00001)).ToString("X") : "")}/{(_aiMotionErrorCode != null ? ((int)(_aiMotionErrorCode.FloatValue + 0.00001)).ToString("X") : "")}";
  175. private R_TRIG _negativeLimitTrig = new R_TRIG();
  176. private R_TRIG _positiveLimitTrig = new R_TRIG();
  177. #endregion
  178. public IoBufferMotor(string module, XmlElement node, string ioModule = "")
  179. {
  180. base.Module = string.IsNullOrEmpty(node.GetAttribute("module")) ? module : node.GetAttribute("module");
  181. base.Name = node.GetAttribute("id");
  182. base.Display = node.GetAttribute("display");
  183. base.DeviceID = node.GetAttribute("schematicId");
  184. _isFloatAioType = !string.IsNullOrEmpty(node.GetAttribute("aioType")) && (node.GetAttribute("aioType") == "float");
  185. var scRootPath = string.IsNullOrEmpty(node.GetAttribute("scRootPath")) ? Module : node.GetAttribute("scRootPath");
  186. _diNegativeLimit = ParseDiNode("diNegativeLimit", node, ioModule);
  187. _diPositiveLimit = ParseDiNode("diPositiveLimit", node, ioModule);
  188. _diServoOn = ParseDiNode("diServoOn", node, ioModule);
  189. _diMoving = ParseDiNode("diMoving", node, ioModule);
  190. _diSensor1 = ParseDiNode("diSensor1", node, ioModule);
  191. _diSensor2 = ParseDiNode("diSensor2", node, ioModule);
  192. _diSensor3 = ParseDiNode("diSensor3", node, ioModule);
  193. _diSensor4 = ParseDiNode("diSensor4", node, ioModule);
  194. _diSensor5 = ParseDiNode("diSensor5", node, ioModule);
  195. _diSensor6 = ParseDiNode("diSensor6", node, ioModule);
  196. _diSensor7 = ParseDiNode("diSensor7", node, ioModule);
  197. _diSensor8 = ParseDiNode("diSensor8", node, ioModule);
  198. _diWarning = ParseDiNode("diWarning", node, ioModule);
  199. _diAlarm = ParseDiNode("diAlarm", node, ioModule);
  200. _diPositionA1 = ParseDiNode("diPositionA1", node, ioModule);
  201. _diPositionB1 = ParseDiNode("diPositionB1", node, ioModule);
  202. _diPositionC1 = ParseDiNode("diPositionC1", node, ioModule);
  203. _diPositionD1 = ParseDiNode("diPositionD1", node, ioModule);
  204. _diPositionA2 = ParseDiNode("diPositionA2", node, ioModule);
  205. _diPositionB2 = ParseDiNode("diPositionB2", node, ioModule);
  206. _diPositionC2 = ParseDiNode("diPositionC2", node, ioModule);
  207. _diPositionD2 = ParseDiNode("diPositionD2", node, ioModule);
  208. _diPositionA3 = ParseDiNode("diPositionA3", node, ioModule);
  209. _diPositionB3 = ParseDiNode("diPositionB3", node, ioModule);
  210. _diPositionC3 = ParseDiNode("diPositionC3", node, ioModule);
  211. _diPositionD3 = ParseDiNode("diPositionD3", node, ioModule);
  212. _diPositionA4 = ParseDiNode("diPositionA4", node, ioModule);
  213. _diPositionB4 = ParseDiNode("diPositionB4", node, ioModule);
  214. _diPositionC4 = ParseDiNode("diPositionC4", node, ioModule);
  215. _diPositionD4 = ParseDiNode("diPositionD4", node, ioModule);
  216. _doServoOn = ParseDoNode("doServoOn", node, ioModule);
  217. _doStop = ParseDoNode("doStop", node, ioModule);
  218. _doMove = ParseDoNode("doMove", node, ioModule);
  219. _doReset = ParseDoNode("doReset", node, ioModule);
  220. _aiRealPosition = ParseAiNode("aiRealPosition", node, ioModule);
  221. _aiRealSpeed = ParseAiNode("aiRealSpeed", node, ioModule);
  222. _aiDriverErrorCode = ParseAiNode("aiDriverErrorCode", node, ioModule);
  223. _aiMotionErrorCode = ParseAiNode("aiMotionErrorCode", node, ioModule);
  224. _aiTargetPosFb = ParseAiNode("aiTargetPosFb", node, ioModule);
  225. _aiRealTorque = ParseAiNode("aiRealTorque", node, ioModule);
  226. _aoTargetPosition = ParseAoNode("aoTargetPosition", node, ioModule);
  227. _aoSpeed = ParseAoNode("aoSpeed", node, ioModule);
  228. _aoAcc = ParseAoNode("aoAcc", node, ioModule);
  229. _aoDec = ParseAoNode("aoDec", node, ioModule);
  230. _scMoveTimeout = ParseScNode("scMoveTimeout", node, ioModule, $"{scRootPath}.MotionTimeout");
  231. if (_doServoOn != null)
  232. _doServoOn.Value = true;
  233. _thread = new PeriodicJob(50, OnTimer, Name);
  234. _thread.Start();
  235. }
  236. public virtual bool Initialize()
  237. {
  238. DATA.Subscribe($"{Module}.{Name}.CurrentPosition", () => _aiRealPosition != null ? (_isFloatAioType ? _aiRealPosition.FloatValue : _aiRealPosition.Value) : 0);
  239. DATA.Subscribe($"{Module}.{Name}.CurrentSpeed", () => _aiRealSpeed != null ? (_isFloatAioType ? _aiRealSpeed.FloatValue : _aiRealSpeed.Value) : 0);
  240. DATA.Subscribe($"{Module}.{Name}.CurrentTorque", () => _aiRealTorque != null ? (_isFloatAioType ? _aiRealTorque.FloatValue : _aiRealTorque.Value) : 0);
  241. DATA.Subscribe($"{Module}.{Name}.TargetPosition", () => (int)(_aoTargetPosition.Value + 0.00001));
  242. DATA.Subscribe($"{Module}.{Name}.IsInPosition", () => IsInPosition);
  243. DATA.Subscribe($"{Module}.{Name}.IsServoOn", () => IsServoOn);
  244. DATA.Subscribe($"{Module}.{Name}.IsReady", () => IsReady);
  245. DATA.Subscribe($"{Module}.{Name}.IsAlarm", () => _diAlarm != null ? _diAlarm.Value : false);
  246. DATA.Subscribe($"{Module}.{Name}.IsWarning", () => _diWarning != null ? _diWarning.Value : false);
  247. DATA.Subscribe($"{Module}.{Name}.Status", () => _state.ToString());
  248. DATA.Subscribe($"{Module}.{Name}.IsMotorRun", () => _diMoving != null ? _diMoving.Value : false);
  249. DATA.Subscribe($"{Module}.{Name}.AtPositionA1", () => _diPositionA1 != null ? _diPositionA1.Value : false);
  250. DATA.Subscribe($"{Module}.{Name}.AtPositionB1", () => _diPositionB1 != null ? _diPositionB1.Value : false);
  251. DATA.Subscribe($"{Module}.{Name}.AtPositionC1", () => _diPositionC1 != null ? _diPositionC1.Value : false);
  252. DATA.Subscribe($"{Module}.{Name}.AtPositionD1", () => _diPositionD1 != null ? _diPositionD1.Value : false);
  253. DATA.Subscribe($"{Module}.{Name}.AtPositionA2", () => _diPositionA2 != null ? _diPositionA2.Value : false);
  254. DATA.Subscribe($"{Module}.{Name}.AtPositionB2", () => _diPositionB2 != null ? _diPositionB2.Value : false);
  255. DATA.Subscribe($"{Module}.{Name}.AtPositionC2", () => _diPositionC2 != null ? _diPositionC2.Value : false);
  256. DATA.Subscribe($"{Module}.{Name}.AtPositionD2", () => _diPositionD2 != null ? _diPositionD2.Value : false);
  257. DATA.Subscribe($"{Module}.{Name}.AtPositionA3", () => _diPositionA3 != null ? _diPositionA3.Value : false);
  258. DATA.Subscribe($"{Module}.{Name}.AtPositionB3", () => _diPositionB3 != null ? _diPositionB3.Value : false);
  259. DATA.Subscribe($"{Module}.{Name}.AtPositionC3", () => _diPositionC3 != null ? _diPositionC3.Value : false);
  260. DATA.Subscribe($"{Module}.{Name}.AtPositionD3", () => _diPositionD3 != null ? _diPositionD3.Value : false);
  261. DATA.Subscribe($"{Module}.{Name}.AtPositionA4", () => _diPositionA4 != null ? _diPositionA4.Value : false);
  262. DATA.Subscribe($"{Module}.{Name}.AtPositionB4", () => _diPositionB4 != null ? _diPositionB4.Value : false);
  263. DATA.Subscribe($"{Module}.{Name}.AtPositionC4", () => _diPositionC4 != null ? _diPositionC4.Value : false);
  264. DATA.Subscribe($"{Module}.{Name}.AtPositionD4", () => _diPositionD4 != null ? _diPositionD4.Value : false);
  265. OP.Subscribe($"{Module}.{Name}.ServoTargetPosition", (string cmd, object[] param) =>
  266. {
  267. float.TryParse(param[0].ToString(), out float position);
  268. SetServoTargetPosition(position);
  269. return true;
  270. });
  271. OP.Subscribe($"{Module}.{Name}.ServoMoveTo", (string cmd, object[] param) =>
  272. {
  273. if (!IsServoOn)
  274. {
  275. EV.PostWarningLog($"{Module}", $"{Name} servo not on");
  276. return false;
  277. }
  278. if (_state != State.Idle)
  279. {
  280. EV.PostWarningLog($"{Module}", $"{Name} busy, wait");
  281. return false;
  282. }
  283. SetServoMoveTo();
  284. return true;
  285. });
  286. OP.Subscribe($"{Module}.{Name}.ServoOnOff", (string cmd, object[] param) =>
  287. {
  288. bool.TryParse(param[0].ToString(), out bool isOn);
  289. SetServoOnOff(isOn);
  290. return true;
  291. });
  292. OP.Subscribe($"{Module}.{Name}.ServoResetAlarm", (string cmd, object[] param) =>
  293. {
  294. if (_state != State.Idle)
  295. {
  296. EV.PostWarningLog($"{Module}", $"{Name} busy, wait");
  297. return false;
  298. }
  299. ServoReset(out _);
  300. return true;
  301. });
  302. OP.Subscribe($"{Module}.{Name}.ServoStop", (string cmd, object[] param) =>
  303. {
  304. ServoStop();
  305. return true;
  306. });
  307. return true;
  308. }
  309. public virtual void Monitor()
  310. {
  311. if (_diNegativeLimit != null)
  312. {
  313. _negativeLimitTrig.CLK = _diNegativeLimit.Value;
  314. if (_negativeLimitTrig.Q)
  315. EV.PostWarningLog(Module, $"{Module} {Name} at negative limit position");
  316. }
  317. if (_diPositiveLimit != null)
  318. {
  319. _positiveLimitTrig.CLK = _diPositiveLimit.Value;
  320. if (_positiveLimitTrig.Q)
  321. EV.PostWarningLog(Module, $"{Module} {Name} at positive limit position");
  322. }
  323. if (_diWarning != null)
  324. {
  325. _warningTrig.CLK = _diWarning.Value;
  326. if (_warningTrig.Q)
  327. EV.PostWarningLog(Module, $"{Module} {Name} warning code={ErrorCode}");
  328. }
  329. if (_diAlarm != null)
  330. {
  331. _alarmTrig.CLK = _diAlarm.Value;
  332. if (_alarmTrig.Q)
  333. EV.PostWarningLog(Module, $"{Module} {Name} alarm code={ErrorCode}");
  334. }
  335. }
  336. public virtual void Reset()
  337. {
  338. ServoReset(out _);
  339. }
  340. public virtual void Terminate()
  341. {
  342. }
  343. private bool OnTimer()
  344. {
  345. switch (_state)
  346. {
  347. case State.Idle:
  348. ResetDO();
  349. _timer.Stop();
  350. break;
  351. case State.Moving:
  352. if (_timer.GetElapseTime() >= setTime)
  353. {
  354. if (_timer.GetElapseTime() >= moveTimeout)
  355. {
  356. _doMove.SetValue(false, out _);
  357. _state = State.Idle;
  358. EV.PostAlarmLog($"{Name}", $"{Name} Servo move timeout");
  359. IsError = true;
  360. }
  361. else
  362. {
  363. if (IsInPosition)
  364. {
  365. _doMove.SetValue(false, out _);
  366. _state = State.Idle;
  367. IsError = false;
  368. }
  369. }
  370. }
  371. break;
  372. case State.Stopping:
  373. if (_timer.GetElapseTime() >= setTime)
  374. {
  375. if (_timer.GetElapseTime() >= moveTimeout)
  376. {
  377. _doStop.SetValue(false, out _);
  378. _state = State.Idle;
  379. EV.PostAlarmLog($"{Name}", $"{Name} Servo stop timeout");
  380. IsError = true;
  381. }
  382. else
  383. {
  384. if (!_diMoving.Value)
  385. {
  386. _doStop.SetValue(false, out _);
  387. _state = State.Idle;
  388. IsError = false;
  389. }
  390. }
  391. }
  392. break;
  393. }
  394. return true;
  395. }
  396. public void SetServoOnOff(bool isOn)
  397. {
  398. ServoOnOffSet = isOn;
  399. }
  400. public void SetServoTargetPosition(float position)
  401. {
  402. ServoMovePositionSet = position;
  403. }
  404. public bool SetServoMoveTo()
  405. {
  406. ResetDO();
  407. _state = State.Moving;
  408. _doMove.SetValue(true, out _);
  409. _timer.Start(0);
  410. return true;
  411. }
  412. public bool ServoReset(out string reason)
  413. {
  414. reason = string.Empty;
  415. ResetDO();
  416. _doReset.SetPulseValue(true, 1000);
  417. return true;
  418. }
  419. public void ServoStop()
  420. {
  421. ResetDO();
  422. _doStop.SetPulseValue(true, 1000);
  423. _state = State.Idle;
  424. }
  425. protected void ResetDO()
  426. {
  427. if (_doMove != null && !_doMove.Value)
  428. _doMove.SetValue(false, out _);
  429. if (_doStop != null && !_doStop.Value)
  430. _doStop.SetValue(false, out _);
  431. if (_doReset != null && !_doReset.Value)
  432. _doReset.SetValue(false, out _);
  433. }
  434. }
  435. }