IoFurnaceMotor.cs 36 KB

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