IoBufferMotor.cs 20 KB

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