IoFurnaceMotor.cs 36 KB

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