IoBufferMotor.cs 22 KB

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