IoFurnaceMotor.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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 DocumentFormat.OpenXml.Spreadsheet;
  10. using FurnaceRT.Equipments.Boats;
  11. using FurnaceRT.Equipments.Systems;
  12. using MECF.Framework.Common.Equipment;
  13. using System;
  14. using System.Collections;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using System.Xml;
  20. namespace FurnaceRT.Devices
  21. {
  22. public class IoFurnaceMotor : BaseDevice, IDevice
  23. {
  24. #region fields
  25. protected enum State
  26. {
  27. Idle,
  28. MoveToPosition,
  29. Rotating,
  30. Homing,
  31. Jogging,
  32. Pitching,
  33. Stopping,
  34. ServoOff,
  35. ServoOn,
  36. ServoReset,
  37. }
  38. public enum Direction
  39. {
  40. Unknown,
  41. CW,
  42. CCW,
  43. }
  44. protected State _state = State.Idle;
  45. protected DIAccessor _diHoming;
  46. protected DIAccessor _diInitializing;
  47. protected DIAccessor _diHomeDone;
  48. protected DIAccessor _diInitDone;
  49. protected DIAccessor _diMoving;
  50. protected DIAccessor _diMotorWarning;
  51. protected DIAccessor _diMotorAlarm;
  52. protected DIAccessor _diAlarm;
  53. protected DIAccessor _diServoOn;
  54. protected DIAccessor _diDirection;
  55. protected DIAccessor _diM0;
  56. protected DIAccessor _diM1;
  57. protected DIAccessor _diM2;
  58. protected DIAccessor _diM3;
  59. protected DIAccessor _diM4;
  60. protected DIAccessor _diM5;
  61. protected DIAccessor _diHomePosition;
  62. protected DIAccessor _diNegativeLimit;
  63. protected DIAccessor _diPositiveLimit;
  64. protected DIAccessor _diPosition1;
  65. protected DIAccessor _diPosition2;
  66. protected DIAccessor _diPosition3;
  67. protected DOAccessor _doStop;
  68. protected DOAccessor _doHome;
  69. protected DOAccessor _doInit;
  70. protected DOAccessor _doMove;
  71. protected DOAccessor _doReset;
  72. protected DOAccessor _doServoOn;
  73. protected DOAccessor _doCW;
  74. protected DOAccessor _doCCW;
  75. protected DOAccessor _doM0;
  76. protected DOAccessor _doM1;
  77. protected DOAccessor _doM2;
  78. protected DOAccessor _doM3;
  79. protected DOAccessor _doM4;
  80. protected DOAccessor _doM5;
  81. protected AIAccessor _aiRealPosition;
  82. protected AIAccessor _aiRealSpeed;
  83. protected AIAccessor _aiBoatElevatorErrorCode;
  84. protected AIAccessor _aiTargetSpeed;
  85. protected AIAccessor _aiDriverErrorCode;
  86. protected AIAccessor _aiMotionErrorCode;
  87. protected AIAccessor _aiTargetPosFb;
  88. protected AOAccessor _aoTargetPosition;
  89. protected AOAccessor _aoTargetSpeed;
  90. protected AOAccessor _aoAcc;
  91. protected AOAccessor _aoDec;
  92. protected SCConfigItem _scServoMoveSpeed;
  93. protected SCConfigItem _scServoAcc;
  94. protected SCConfigItem _scServoDec;
  95. protected SCConfigItem _scServoPosition1;
  96. protected SCConfigItem _scServoPosition2;
  97. protected SCConfigItem _scServoPosition3;
  98. protected SCConfigItem _scServoPosition4;
  99. protected SCConfigItem _scServoPosition5;
  100. protected SCConfigItem _scServoPosition6;
  101. protected SCConfigItem _scServoPosition7;
  102. protected SCConfigItem _scServoPosition8;
  103. protected SCConfigItem _scServoPosition9;
  104. protected SCConfigItem _scServoPosition10;
  105. protected SCConfigItem _scMoveTimeout;
  106. protected DeviceTimer _timer = new DeviceTimer();
  107. private const float PositionTolerance = 0.1f;
  108. private int setTime = 100; //200ms set DO
  109. private int resetTime = 2000; //2000 reset DO
  110. private int moveTimeout => (int)(_scMoveTimeout.IntValue * 1000);
  111. private PeriodicJob _thread;
  112. private R_TRIG _stateTrig = new R_TRIG();
  113. private R_TRIG _negativeLimitTrig = new R_TRIG();
  114. private R_TRIG _positiveLimitTrig = new R_TRIG();
  115. private R_TRIG _warningTrig = new R_TRIG();
  116. private R_TRIG _alarmTrig = new R_TRIG();
  117. private string _lastTarget = "";
  118. private bool IsRAxis = false;
  119. public bool ServoOnOffSet
  120. {
  121. set
  122. {
  123. if (_doServoOn == null)
  124. return;
  125. _doServoOn.Value = value;
  126. }
  127. }
  128. public float ServoMoveSpeedSet
  129. {
  130. set
  131. {
  132. if (_aoTargetSpeed == null)
  133. return;
  134. _aoTargetSpeed.FloatValue = value;
  135. }
  136. }
  137. public float ServoAccSet
  138. {
  139. set
  140. {
  141. if (_aoAcc == null)
  142. return;
  143. _aoAcc.FloatValue = value;
  144. }
  145. }
  146. public float ServoDecSet
  147. {
  148. set
  149. {
  150. if (_aoDec == null)
  151. return;
  152. _aoDec.FloatValue = value;
  153. }
  154. }
  155. public float ServoMovePositionSet
  156. {
  157. set
  158. {
  159. if (_aoTargetPosition == null)
  160. return;
  161. _aoTargetPosition.FloatValue = value;
  162. }
  163. get
  164. {
  165. return _aoTargetPosition.FloatValue;
  166. }
  167. }
  168. public Direction MotorDirection
  169. {
  170. get
  171. {
  172. if (_diDirection != null)
  173. return _diDirection.Value ? Direction.CCW : Direction.CW;
  174. if (_doCW != null && _doCCW != null)
  175. {
  176. if (_doCW.Value)
  177. return Direction.CW;
  178. if (_doCCW.Value)
  179. return Direction.CCW;
  180. }
  181. return Direction.Unknown;
  182. }
  183. }
  184. public int TargetPositionFb => (int)(_aiTargetPosFb.FloatValue + 0.00001);
  185. public string ErrorCode => $"{(_aiDriverErrorCode != null ? ((int)(_aiDriverErrorCode.FloatValue + 0.00001)).ToString("X") : "")}/{(_aiMotionErrorCode != null ? ((int)(_aiMotionErrorCode.FloatValue + 0.00001)).ToString("X") : "")}";
  186. private bool _isFloatAioType = false;
  187. public bool IsHomingState = false;
  188. #endregion
  189. #region properties
  190. public float CurrentPosition => _aiRealPosition.FloatValue;
  191. public float CurrentSpeed => _aiRealSpeed.FloatValue;
  192. public Dictionary<string, string> IoMappingDic { set; get; }
  193. public bool IsError
  194. {
  195. get
  196. {
  197. if (_diMotorAlarm != null && _diMotorAlarm.Value)
  198. return true;
  199. if (_diAlarm != null && _diAlarm.Value)
  200. return true;
  201. return false;
  202. }
  203. }
  204. public bool IsHomeing => _diHoming == null ? false : _diHoming.Value;
  205. public bool IsHomeDone => _diHomeDone == null ? false : _diHomeDone.Value;
  206. public bool IsInitDone => _diInitDone == null ? false : _diInitDone.Value;
  207. public bool IsMoving => _diMoving == null ? false : _diMoving.Value;
  208. public bool IsPause { get; set; }
  209. public bool IsReady
  210. {
  211. get
  212. {
  213. if (_diMotorWarning != null && _diMotorWarning.Value)
  214. return false;
  215. if (_diMotorAlarm != null && _diMotorAlarm.Value)
  216. return false;
  217. if (_diAlarm != null && _diAlarm.Value)
  218. return false;
  219. if (_diServoOn != null && !_diServoOn.Value)
  220. return false;
  221. if (_diInitializing != null && _diInitializing.Value)
  222. return false;
  223. if (_diHoming != null && _diHoming.Value)
  224. return false;
  225. if (_diMoving != null && _diMoving.Value)
  226. return false;
  227. return _state == State.Idle;
  228. }
  229. }
  230. public bool IsServoOn
  231. {
  232. get
  233. {
  234. if (_diServoOn == null)
  235. return true;
  236. return _diServoOn.Value;
  237. }
  238. }
  239. private R_TRIG _homingTrig = new R_TRIG();
  240. #endregion
  241. public IoFurnaceMotor(string module, XmlElement node, string ioModule = "")
  242. {
  243. base.Module = string.IsNullOrEmpty(node.GetAttribute("module")) ? module : node.GetAttribute("module");
  244. base.Name = node.GetAttribute("id");
  245. base.Display = node.GetAttribute("display");
  246. base.DeviceID = node.GetAttribute("schematicId");
  247. _isFloatAioType = !string.IsNullOrEmpty(node.GetAttribute("aioType")) && (node.GetAttribute("aioType") == "float");
  248. var scRootPath = string.IsNullOrEmpty(node.GetAttribute("scRootPath")) ? Module : node.GetAttribute("scRootPath");
  249. _diHoming = ParseDiNode("diHoming", node, ioModule);
  250. _diInitializing = ParseDiNode("diInitializing", node, ioModule);
  251. _diHomeDone = ParseDiNode("diHomeDone", node, ioModule);
  252. _diInitDone = ParseDiNode("diInitDone", node, ioModule);
  253. _diMoving = ParseDiNode("diMoving", node, ioModule);
  254. _diMotorWarning = ParseDiNode("diMotorWarning", node, ioModule);
  255. _diMotorAlarm = ParseDiNode("diMotorAlarm", node, ioModule);
  256. _diAlarm = ParseDiNode("diAlarm", node, ioModule);
  257. _diServoOn = ParseDiNode("diServoOn", node, ioModule);
  258. _diDirection = ParseDiNode("diDirection", node, ioModule);
  259. _diM0 = ParseDiNode("diM0", node, ioModule);
  260. _diM1 = ParseDiNode("diM1", node, ioModule);
  261. _diM2 = ParseDiNode("diM2", node, ioModule);
  262. _diM3 = ParseDiNode("diM3", node, ioModule);
  263. _diM4 = ParseDiNode("diM4", node, ioModule);
  264. _diM5 = ParseDiNode("diM5", node, ioModule);
  265. _diHomePosition = ParseDiNode("diHomePosition", node, ioModule);
  266. _diNegativeLimit = ParseDiNode("diNegativeLimit", node, ioModule);
  267. _diPositiveLimit = ParseDiNode("diPositiveLimit", node, ioModule);
  268. _diPosition1 = ParseDiNode("diPosition1", node, ioModule);
  269. _diPosition2 = ParseDiNode("diPosition2", node, ioModule);
  270. _diPosition3 = ParseDiNode("diPosition3", node, ioModule);
  271. _doStop = ParseDoNode("doStop", node, ioModule);
  272. _doHome = ParseDoNode("doHome", node, ioModule);
  273. _doInit = ParseDoNode("doInit", node, ioModule);
  274. _doMove = ParseDoNode("doMove", node, ioModule);
  275. _doReset = ParseDoNode("doReset", node, ioModule);
  276. _doServoOn = ParseDoNode("doServoOn", node, ioModule);
  277. _doCW = ParseDoNode("doCW", node, ioModule);
  278. _doCCW = ParseDoNode("doCCW", node, ioModule);
  279. _doM0 = ParseDoNode("doM0", node, ioModule);
  280. _doM1 = ParseDoNode("doM1", node, ioModule);
  281. _doM2 = ParseDoNode("doM2", node, ioModule);
  282. _doM3 = ParseDoNode("doM3", node, ioModule);
  283. _doM4 = ParseDoNode("doM4", node, ioModule);
  284. _doM5 = ParseDoNode("doM5", node, ioModule);
  285. _aiRealPosition = ParseAiNode("aiRealPosition", node, ioModule);
  286. _aiRealSpeed = ParseAiNode("aiRealSpeed", node, ioModule);
  287. _aiBoatElevatorErrorCode = ParseAiNode("aiBoatElevatorErrorCode", node, ioModule);
  288. _aiTargetSpeed = ParseAiNode("aiTargetSpeed", node, ioModule);
  289. _aiDriverErrorCode = ParseAiNode("aiDriverErrorCode", node, ioModule);
  290. _aiMotionErrorCode = ParseAiNode("aiMotionErrorCode", node, ioModule);
  291. _aiTargetPosFb = ParseAiNode("aiTargetPosFb", node, ioModule);
  292. _aoTargetPosition = ParseAoNode("aoTargetPosition", node, ioModule);
  293. _aoTargetSpeed = ParseAoNode("aoTargetSpeed", node, ioModule);
  294. _aoAcc = ParseAoNode("aoAcc", node, ioModule);
  295. _aoDec = ParseAoNode("aoDec", node, ioModule);
  296. _scServoMoveSpeed = ParseScNode("scSpeed1", node, ioModule, $"{scRootPath}.MoveSpeed");
  297. _scServoAcc = ParseScNode("scSpeed1", node, ioModule, $"{scRootPath}.Acc");
  298. _scServoDec = ParseScNode("scSpeed1", node, ioModule, $"{scRootPath}.Dec");
  299. _scServoPosition1 = ParseScNode("scServoPosition1", node, ioModule, $"{scRootPath}.Position1");
  300. _scServoPosition2 = ParseScNode("scServoPosition2", node, ioModule, $"{scRootPath}.Position2");
  301. _scServoPosition3 = ParseScNode("scServoPosition3", node, ioModule, $"{scRootPath}.Position3");
  302. _scServoPosition4 = ParseScNode("scServoPosition4", node, ioModule, $"{scRootPath}.Position4");
  303. _scServoPosition5 = ParseScNode("scServoPosition5", node, ioModule, $"{scRootPath}.Position5");
  304. _scServoPosition6 = ParseScNode("scServoPosition6", node, ioModule, $"{scRootPath}.Position6");
  305. _scServoPosition7 = ParseScNode("scServoPosition7", node, ioModule, $"{scRootPath}.Position7");
  306. _scServoPosition8 = ParseScNode("scServoPosition8", node, ioModule, $"{scRootPath}.Position8");
  307. _scServoPosition9 = ParseScNode("scServoPosition9", node, ioModule, $"{scRootPath}.Position9");
  308. _scServoPosition10 = ParseScNode("scServoPosition10", node, ioModule, $"{scRootPath}.Position10");
  309. _scMoveTimeout = ParseScNode("scMoveTimeout", node, ioModule, $"{scRootPath}.MotionTimeout");
  310. IsRAxis = Name == "BoatRotationServo";
  311. _thread = new PeriodicJob(50, OnTimer, Name);
  312. _thread.Start();
  313. }
  314. public virtual bool Initialize()
  315. {
  316. DATA.Subscribe($"{Module}.{Name}.CurrentPosition", () => _aiRealPosition != null ? (_isFloatAioType ? _aiRealPosition.FloatValue : _aiRealPosition.Value) : 0);
  317. DATA.Subscribe($"{Module}.{Name}.CurrentSpeed", () => GetAxisSpeed());
  318. if (_aiTargetPosFb != null)
  319. DATA.Subscribe($"{Module}.{Name}.TargetPositionFb", () => _aiTargetPosFb != null ? (_isFloatAioType ? _aiTargetPosFb.FloatValue : _aiTargetPosFb.Value) : 0);
  320. DATA.Subscribe($"{Module}.{Name}.TargetPosition", () => _aoTargetPosition != null ? (_isFloatAioType ? _aoTargetPosition.FloatValue : _aoTargetPosition.Value) : 0);
  321. DATA.Subscribe($"{Module}.{Name}.IsReady", () => IsReady);
  322. DATA.Subscribe($"{Module}.{Name}.IsWarning", () => _diMotorWarning != null ? _diMotorWarning.Value : false);
  323. DATA.Subscribe($"{Module}.{Name}.IsAlarm", () => IsError);
  324. DATA.Subscribe($"{Module}.{Name}.IsServoOn", () => _diServoOn != null ? _diServoOn.Value : false);
  325. DATA.Subscribe($"{Module}.{Name}.ErrorCode", () => ErrorCode);
  326. DATA.Subscribe($"{Module}.{Name}.Status", () => _state.ToString());
  327. DATA.Subscribe($"{Module}.{Name}.Direction", () => MotorDirection.ToString());
  328. DATA.Subscribe($"{Module}.{Name}.IsMoving", () => _diMoving != null ? _diMoving.Value : false);
  329. DATA.Subscribe($"{Module}.{Name}.IsInitDone", () => _diInitDone != null ? _diInitDone.Value : false);
  330. DATA.Subscribe($"{Module}.{Name}.IsHomeDone", () => _diHomeDone != null ? _diHomeDone.Value : false);
  331. DATA.Subscribe($"{Module}.{Name}.IsHoming", () => _diHoming != null ? _diHoming.Value : false);
  332. DATA.Subscribe($"{Module}.{Name}.IsInitializing", () => _diInitializing != null ? _diInitializing.Value : false);
  333. DATA.Subscribe($"{Module}.{Name}.AtPosition1", () => _diPosition1 != null ? _diPosition1.Value : false);
  334. DATA.Subscribe($"{Module}.{Name}.AtPosition2", () => _diPosition2 != null ? _diPosition2.Value : false);
  335. DATA.Subscribe($"{Module}.{Name}.AtPosition3", () => _diPosition3 != null ? _diPosition3.Value : false);
  336. DATA.Subscribe($"{Module}.{Name}.AtHomePosition", () => _diHomePosition != null ? _diHomePosition.Value : false);
  337. OP.Subscribe($"{Module}.{Name}.SetServoPara", (string cmd, object[] param) =>
  338. {
  339. float.TryParse(param[1].ToString(), out float value);
  340. SetServoPara(param[0].ToString(), value);
  341. return true;
  342. });
  343. OP.Subscribe($"{Module}.{Name}.ServoMoveTo", (string cmd, object[] param) =>
  344. {
  345. //if (!IsServoOn)
  346. //{
  347. // EV.PostWarningLog($"{Module}", $"{Name} servo not on");
  348. // return false;
  349. //}
  350. if (_state != State.Idle)
  351. {
  352. EV.PostWarningLog($"{Module}", $"{Name} busy, wait");
  353. return false;
  354. }
  355. float setSpeed = 0.0f;
  356. if (param.Length >= 2)
  357. {
  358. float.TryParse(param[1].ToString(), out setSpeed);
  359. }
  360. SetServoMoveTo(param[0].ToString(), out _, setSpeed);
  361. return true;
  362. });
  363. OP.Subscribe($"{Module}.{Name}.ServoHome", (string cmd, object[] param) =>
  364. {
  365. //if (!IsServoOn)
  366. //{
  367. // EV.PostWarningLog($"{Module}", $"{Name} servo not on");
  368. // return false;
  369. //}
  370. if (_state != State.Idle)
  371. {
  372. EV.PostWarningLog($"{Module}", $"{Name} busy, wait");
  373. return false;
  374. }
  375. SetServoHome();
  376. return true;
  377. });
  378. OP.Subscribe($"{Module}.{Name}.ServoOnOff", (string cmd, object[] param) =>
  379. {
  380. bool.TryParse(param[0].ToString(), out bool isOn);
  381. SetServoOnOff(isOn);
  382. return true;
  383. });
  384. OP.Subscribe($"{Module}.{Name}.ServoResetAlarm", (string cmd, object[] param) =>
  385. {
  386. ServoReset(out _);
  387. return true;
  388. });
  389. OP.Subscribe($"{Module}.{Name}.ServoStop", (string cmd, object[] param) =>
  390. {
  391. ServoStop();
  392. return true;
  393. });
  394. OP.Subscribe($"{Module}.{Name}.Continue", (string cmd, object[] param) =>
  395. {
  396. Continue();
  397. return true;
  398. });
  399. if (_scServoMoveSpeed != null)
  400. SetServoMoveSpeed((float)_scServoMoveSpeed.DoubleValue);
  401. if (_scServoAcc != null)
  402. ServoAccSet = (float)_scServoAcc.DoubleValue;
  403. if (_scServoDec != null)
  404. ServoDecSet = (float)_scServoDec.DoubleValue;
  405. if (_doServoOn != null)
  406. _doServoOn.SetValue(true, out _);
  407. return true;
  408. }
  409. public float GetSpeedFromOutputs()
  410. {
  411. if (_diMoving.Value || _diHoming.Value)
  412. {
  413. return GetRAxisSpeed();
  414. }
  415. return 0f;
  416. }
  417. private float GetRAxisSpeed()
  418. {
  419. List<bool> chunks = new List<bool> { _diM0.Value, _diM1.Value, _diM2.Value, _diM3.Value, _diM4.Value, _diM5.Value };
  420. chunks.Reverse();
  421. string binaryString = "";
  422. foreach (bool chunk in chunks)
  423. {
  424. binaryString += chunk ? "1" : "0";
  425. }
  426. int intValue = Convert.ToInt32(binaryString, 2);
  427. if (intValue >= 8 && intValue <= 47)
  428. {
  429. decimal standardSpeed = 0.1M;
  430. for (int i = 8; i < intValue; i++)
  431. {
  432. standardSpeed += 0.1M;
  433. }
  434. return (float)standardSpeed;
  435. }
  436. else
  437. {
  438. return 0f;
  439. }
  440. }
  441. private float GetAxisSpeed()
  442. {
  443. if (IsRAxis)
  444. {
  445. return _aiRealSpeed != null ? (_isFloatAioType ? _aiRealSpeed.FloatValue : _aiRealSpeed.Value) : GetSpeedFromOutputs();
  446. }
  447. else
  448. {
  449. return _aiRealSpeed != null ? (_isFloatAioType ? _aiRealSpeed.FloatValue : _aiRealSpeed.Value) : 0;
  450. }
  451. }
  452. public virtual void Monitor()
  453. {
  454. if (_diNegativeLimit != null)
  455. {
  456. _negativeLimitTrig.CLK = _diNegativeLimit.Value;
  457. if (_negativeLimitTrig.Q)
  458. EV.PostWarningLog(Module, $"{Module} {Name} at negative limit position");
  459. }
  460. if (_diPositiveLimit != null)
  461. {
  462. _positiveLimitTrig.CLK = _diPositiveLimit.Value;
  463. if (_positiveLimitTrig.Q)
  464. EV.PostWarningLog(Module, $"{Module} {Name} at positive limit position");
  465. }
  466. if (_diMotorWarning != null)
  467. {
  468. _warningTrig.CLK = _diMotorWarning.Value;
  469. if (_warningTrig.Q)
  470. EV.PostWarningLog(Module, $"{Module} {Name} warning code={ErrorCode}");
  471. }
  472. if (IsError)
  473. {
  474. _alarmTrig.CLK = IsError;
  475. if (_alarmTrig.Q)
  476. EV.PostWarningLog(Module, $"{Module} {Name} alarm code={ErrorCode}");
  477. }
  478. }
  479. public virtual void Reset()
  480. {
  481. ServoReset(out _);
  482. }
  483. public virtual void Terminate()
  484. {
  485. }
  486. private bool OnTimer()
  487. {
  488. switch (_state)
  489. {
  490. case State.Idle:
  491. if (!_timer.IsIdle())
  492. {
  493. _timer.Stop();
  494. }
  495. _stateTrig.CLK = true;
  496. if (_stateTrig.Q)
  497. ResetDO();
  498. break;
  499. case State.Homing:
  500. if (_timer.GetElapseTime() >= setTime)
  501. {
  502. if (_timer.GetElapseTime() >= moveTimeout)
  503. {
  504. _doHome.SetValue(false, out _);
  505. _state = State.Idle;
  506. EV.PostAlarmLog($"{Module}", $"{Name} Motor home timeout");
  507. }
  508. else
  509. {
  510. _homingTrig.CLK = !_diHoming.Value;
  511. if (_homingTrig.Q)
  512. {
  513. if (_diHomeDone != null && _diHomeDone.Value)
  514. {
  515. _doHome.SetValue(false, out _);
  516. _state = State.Idle;
  517. IsHomingState = false;
  518. EV.PostInfoLog($"{Module}", $"{Name} Motor home finish");
  519. }
  520. }
  521. }
  522. }
  523. if (_timer.GetElapseTime() >= resetTime)
  524. {
  525. _doHome.SetValue(false, out _);
  526. }
  527. break;
  528. case State.ServoOff:
  529. if (_timer.GetElapseTime() >= setTime)
  530. {
  531. if (_timer.GetElapseTime() >= moveTimeout)
  532. {
  533. _state = State.Idle;
  534. EV.PostAlarmLog($"{Name}", $"{Name} Motor servo off timeout");
  535. }
  536. else
  537. {
  538. if (_diServoOn != null && !_diServoOn.Value)
  539. {
  540. _state = State.Idle;
  541. }
  542. }
  543. }
  544. break;
  545. case State.ServoOn:
  546. if (_timer.GetElapseTime() >= setTime)
  547. {
  548. if (_timer.GetElapseTime() >= moveTimeout)
  549. {
  550. _state = State.Idle;
  551. EV.PostAlarmLog($"{Module}", $"{Name} Motor on timeout");
  552. }
  553. else
  554. {
  555. if (_diServoOn != null && _diServoOn.Value)
  556. {
  557. _state = State.Idle;
  558. }
  559. }
  560. }
  561. break;
  562. case State.Stopping:
  563. if (_timer.GetElapseTime() >= setTime)
  564. {
  565. if (_timer.GetElapseTime() >= moveTimeout)
  566. {
  567. _doStop.SetValue(false, out _);
  568. _state = State.Idle;
  569. EV.PostAlarmLog($"{Module}", $"{Name} Motor stop timeout");
  570. }
  571. else
  572. {
  573. //if (_diMotorRun != null && !_diMotorRun.Value)
  574. {
  575. _doStop.SetValue(false, out _);
  576. _state = State.Idle;
  577. }
  578. }
  579. }
  580. if (_timer.GetElapseTime() >= resetTime)
  581. {
  582. _doStop.SetValue(false, out _);
  583. }
  584. break;
  585. case State.ServoReset:
  586. if (_timer.GetElapseTime() >= setTime)
  587. {
  588. if (_timer.GetElapseTime() >= moveTimeout)
  589. {
  590. _doReset.SetValue(false, out _);
  591. _state = State.Idle;
  592. EV.PostAlarmLog($"{Module}", $"{Name} Servo reset timeout");
  593. }
  594. if (_timer.GetElapseTime() >= resetTime)
  595. {
  596. _doReset.SetValue(false, out _);
  597. _state = State.Idle;
  598. }
  599. }
  600. break;
  601. case State.MoveToPosition:
  602. var boatModule = Singleton<EquipmentManager>.Instance.Modules[ModuleName.Boat] as BoatModule;
  603. if (!boatModule.CheckPrepareMove(out string reason))
  604. {
  605. boatModule.BoatZAxisMoveFailedForInterlock.Set(reason);
  606. _doMove?.SetValue(false, out _);
  607. _state = State.Idle;
  608. break;
  609. }
  610. if (_timer.GetElapseTime() >= setTime && TargetPositionFb == (int)(ServoMovePositionSet + 0.00001))
  611. {
  612. if (!_doMove.Value && _timer.GetElapseTime() < resetTime)
  613. _doMove.SetValue(true, out _);
  614. if (_timer.GetElapseTime() >= moveTimeout)
  615. {
  616. _doMove.SetValue(false, out _);
  617. _state = State.Idle;
  618. EV.PostAlarmLog($"{Module}", $"{Name} Motor move to position timeout");
  619. }
  620. else if (CheckServoAtPosition())
  621. {
  622. _doMove.SetValue(false, out _);
  623. _state = State.Idle;
  624. EV.PostInfoLog($"{Module}", $"{Name} Motor move to position finish");
  625. }
  626. }
  627. if (_timer.GetElapseTime() >= resetTime)
  628. {
  629. _doMove.SetValue(false, out _);
  630. }
  631. break;
  632. case State.Rotating:
  633. //if (_timer.GetElapseTime() >= 100)
  634. //{
  635. // var currSpeed = GetRAxisSpeed();
  636. // if (!_doMove.Value && _setSpeed == (float)(currSpeed))
  637. // {
  638. // _doCCW.SetValue(!_isCW, out _);
  639. // _doCW.SetValue(_isCW, out _);
  640. // _doMove.SetValue(true, out _);
  641. // SC.SetItemValue(_scServoMoveSpeed.PathName, _setSpeed);
  642. // }
  643. // else
  644. // {
  645. // if (_timer.GetElapseTime() >= resetTime && !_doMove.Value && _setSpeed != (float)(currSpeed))
  646. // {
  647. // EV.PostAlarmLog($"{Module}", $"{Name} Motor Rotating timeout");
  648. // _doMove.SetValue(false, out _);
  649. // _state = State.Idle;
  650. // }
  651. // }
  652. //}
  653. break;
  654. default:
  655. break;
  656. }
  657. return true;
  658. }
  659. public bool SetServoHome()
  660. {
  661. ResetDO();
  662. if (_doHome != null)
  663. {
  664. _doHome?.SetValue(true, out _);
  665. _state = State.Homing;
  666. IsHomingState = true;
  667. }
  668. else
  669. {
  670. ServoMoveSpeedSet = (float)_scServoMoveSpeed.DoubleValue;
  671. ServoMovePositionSet = 1;
  672. //_doMove.SetValue(true, out string reason);
  673. _timer.Start(0);
  674. _state = State.MoveToPosition;
  675. }
  676. _timer.Start(0);
  677. LOG.Write($"{Name} set motor home");
  678. return true;
  679. }
  680. private void Continue()
  681. {
  682. _state = State.Idle;
  683. SetServoMoveTo(_lastTarget, out string reason);
  684. }
  685. public bool SetServoMoveTo(string position, out string reason, float speed = 0)
  686. {
  687. reason = string.Empty;
  688. if (_state != State.Idle && _state != State.Rotating)
  689. {
  690. reason = "busy";
  691. return false;
  692. }
  693. ResetDO();
  694. ServoOnOffSet = true;
  695. if (_scServoAcc != null)
  696. ServoAccSet = (float)_scServoAcc.DoubleValue;
  697. if (_scServoDec != null)
  698. ServoDecSet = (float)_scServoDec.DoubleValue;
  699. var target = position;
  700. if (IoMappingDic != null && IoMappingDic.ContainsKey(position))
  701. {
  702. target = IoMappingDic[position];
  703. }
  704. _lastTarget = target;
  705. switch (target)
  706. {
  707. case "Position1":
  708. speed = speed > 0 ? speed : (float)_scServoMoveSpeed.DoubleValue;
  709. ServoMoveSpeedSet = speed;
  710. ServoMovePositionSet = 1;
  711. //_doMove.SetValue(true, out reason);
  712. _timer.Start(0);
  713. _state = State.MoveToPosition;
  714. SC.SetItemValue(_scServoMoveSpeed.PathName, speed);
  715. break;
  716. case "CapPosition":
  717. case "Position2":
  718. speed = speed > 0 ? speed : (float)_scServoMoveSpeed.DoubleValue;
  719. ServoMoveSpeedSet = speed;
  720. ServoMovePositionSet = 2;
  721. //_doMove.SetValue(true, out reason);
  722. _timer.Start(0);
  723. _state = State.MoveToPosition;
  724. SC.SetItemValue(_scServoMoveSpeed.PathName, speed);
  725. break;
  726. case "HomePosition":
  727. case "Position3":
  728. speed = speed > 0 ? speed : (float)_scServoMoveSpeed.DoubleValue;
  729. ServoMoveSpeedSet = speed;
  730. ServoMovePositionSet = 3;
  731. //_doMove.SetValue(true, out reason);
  732. _timer.Start(0);
  733. _state = State.MoveToPosition;
  734. SC.SetItemValue(_scServoMoveSpeed.PathName, speed);
  735. break;
  736. case "CW":
  737. _state = State.Rotating;
  738. SetRotateSpeed(speed);
  739. _doCCW.SetValue(false, out _);
  740. _doCW.SetValue(true, out _);
  741. SC.SetItemValue(_scServoMoveSpeed.PathName, speed);
  742. _doMove.SetPulseValue(true, resetTime);
  743. break;
  744. case "CCW":
  745. _state = State.Rotating;
  746. SetRotateSpeed(speed);
  747. _doCCW.SetValue(true, out _);
  748. _doCW.SetValue(false, out _);
  749. SC.SetItemValue(_scServoMoveSpeed.PathName, speed);
  750. _doMove.SetPulseValue(true, resetTime);
  751. break;
  752. }
  753. return true;
  754. }
  755. private void SetRotateSpeed(float speed)
  756. {
  757. var value = Math.Round(speed, 3, MidpointRounding.AwayFromZero);
  758. decimal standardSpeed = 0.1M;
  759. bool enterSplit = false;
  760. for (int i = 8; i <= 47; i++)
  761. {
  762. decimal compareSpeed = Convert.ToDecimal(value);
  763. if (Decimal.Compare(compareSpeed, standardSpeed) > 0)
  764. {
  765. standardSpeed = standardSpeed + 0.1M;
  766. continue;
  767. }
  768. enterSplit = true;
  769. var binaryString = Convert.ToString(i, 2);
  770. int chunkSize = 1; // 每个分片的大小
  771. List<bool> chunks = new List<bool>();
  772. for (int j = 0; j < binaryString.Length - chunkSize + 1; j += chunkSize)
  773. {
  774. string chunk = binaryString.Substring(j, chunkSize);
  775. chunks.Add(chunk.Equals("1"));
  776. }
  777. chunks.Reverse();
  778. _doM0.SetValue(chunks[0], out _);
  779. _doM1.SetValue(chunks[1], out _);
  780. _doM2.SetValue(chunks[2], out _);
  781. _doM3.SetValue(chunks[3], out _);
  782. switch (chunks.Count)
  783. {
  784. case 5:
  785. _doM4.SetValue(chunks[4], out _);
  786. _doM5.SetValue(false, out _);
  787. break;
  788. case 6:
  789. _doM4.SetValue(chunks[4], out _);
  790. _doM5.SetValue(chunks[5], out _);
  791. break;
  792. default:
  793. _doM4.SetValue(false, out _);
  794. _doM5.SetValue(false, out _);
  795. break;
  796. }
  797. return;
  798. }
  799. if (!enterSplit)
  800. {
  801. _doM0.SetValue(true, out _);
  802. _doM1.SetValue(true, out _);
  803. _doM2.SetValue(false, out _);
  804. _doM3.SetValue(false, out _);
  805. _doM4.SetValue(true, out _);
  806. _doM5.SetValue(false, out _);
  807. }
  808. }
  809. public bool CheckServoAtPosition()
  810. {
  811. int target = (int)(_aoTargetPosition.FloatValue + 0.0001);
  812. switch (target)
  813. {
  814. case 1:
  815. return _diPosition1.Value;
  816. case 2:
  817. return _diPosition2.Value;
  818. case 3:
  819. return _diPosition3.Value;
  820. }
  821. return false;
  822. }
  823. public bool CheckServoAtPosition(string position)
  824. {
  825. switch (position)
  826. {
  827. case "Position1":
  828. return _diPosition1.Value;
  829. case "Position2":
  830. case "CapPosition":
  831. return _diPosition2.Value;
  832. case "HomePosition":
  833. case "Position3":
  834. return _diPosition3.Value;
  835. }
  836. return false;
  837. }
  838. public bool CheckServoHomeDone()
  839. {
  840. if (IsHomeDone && IsReady)
  841. {
  842. return true;
  843. }
  844. return false;
  845. }
  846. public bool ServoReset(out string reason)
  847. {
  848. reason = string.Empty;
  849. _doReset.SetPulseValue(true, resetTime);
  850. return true;
  851. }
  852. public void SetServoOnOff(bool isOn)
  853. {
  854. ServoOnOffSet = isOn;
  855. }
  856. public void ServoStop()
  857. {
  858. ResetDO();
  859. _state = State.Idle;
  860. _doStop.SetPulseValue(true, resetTime);
  861. }
  862. public void SetSpeed(float value)
  863. {
  864. SetServoPara("MoveSpeed", value);
  865. }
  866. public void SetPauseResume(bool isPause)
  867. {
  868. IsPause = IsPause;
  869. }
  870. private void SetServoPara(string module, float value)
  871. {
  872. var target = module;
  873. if (IoMappingDic != null && IoMappingDic.ContainsKey(module))
  874. {
  875. target = IoMappingDic[module];
  876. }
  877. switch (target)
  878. {
  879. case "MoveSpeed":
  880. SetServoMoveSpeed(value);
  881. break;
  882. case "Position1":
  883. SC.SetItemValue(_scServoPosition1.PathName, value);
  884. break;
  885. case "Position2":
  886. SC.SetItemValue(_scServoPosition2.PathName, value);
  887. break;
  888. case "Position3":
  889. SC.SetItemValue(_scServoPosition3.PathName, value);
  890. break;
  891. case "Position4":
  892. SC.SetItemValue(_scServoPosition4.PathName, value);
  893. break;
  894. case "Position5":
  895. SC.SetItemValue(_scServoPosition5.PathName, value);
  896. break;
  897. case "Position6":
  898. SC.SetItemValue(_scServoPosition6.PathName, value);
  899. break;
  900. case "Position7":
  901. SC.SetItemValue(_scServoPosition7.PathName, value);
  902. break;
  903. case "Position8":
  904. SC.SetItemValue(_scServoPosition8.PathName, value);
  905. break;
  906. case "Position9":
  907. SC.SetItemValue(_scServoPosition9.PathName, value);
  908. break;
  909. case "Position10":
  910. SC.SetItemValue(_scServoPosition10.PathName, value);
  911. break;
  912. }
  913. }
  914. private void SetServoMoveSpeed(float speed)
  915. {
  916. ServoMoveSpeedSet = speed;
  917. SC.SetItemValue(_scServoMoveSpeed.PathName, speed);
  918. }
  919. private void SetServoMovePosition(float position)
  920. {
  921. ServoMovePositionSet = position;
  922. }
  923. protected void ResetDO()
  924. {
  925. if (_doHome != null && _doHome.Value)
  926. _doHome.SetValue(false, out _);
  927. if (_doInit != null && _doInit.Value)
  928. _doInit.SetValue(false, out _);
  929. if (_doMove != null && _doMove.Value)
  930. _doMove.SetValue(false, out _);
  931. if (_doStop != null && _doStop.Value)
  932. _doStop.SetValue(false, out _);
  933. if (_doCW != null && _doCW.Value)
  934. _doCW.SetValue(false, out _);
  935. if (_doCCW != null && _doCCW.Value)
  936. _doCCW.SetValue(false, out _);
  937. if (_doReset != null && _doReset.Value)
  938. _doReset.SetValue(false, out _);
  939. }
  940. }
  941. }