IoFurnaceMotor.cs 35 KB

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