IoFurnaceMotor.cs 34 KB

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