IoServoMotor.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Runtime.Serialization;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Xml;
  9. using Aitex.Core.Common.DeviceData;
  10. using Aitex.Core.RT.DataCenter;
  11. using Aitex.Core.RT.Event;
  12. using Aitex.Core.RT.Fsm;
  13. using Aitex.Core.RT.IOCore;
  14. using Aitex.Core.RT.Log;
  15. using Aitex.Core.RT.SCCore;
  16. using Aitex.Core.Util;
  17. namespace Aitex.Core.RT.Device.Unit
  18. {
  19. public class IoServoMotor : BaseDevice, IDevice
  20. {
  21. #region FSM Entity
  22. protected void Transition<T, V>(T state, V msg, FsmFunc func, T next)
  23. {
  24. Debug.Assert(typeof(T).IsEnum && typeof(V).IsEnum);
  25. int _state = Convert.ToInt32(state);
  26. int _next = Convert.ToInt32(next);
  27. int _msg = Convert.ToInt32(msg);
  28. Transition(_state, _msg, func, _next);
  29. }
  30. protected void Transition(int state, int msg, FsmFunc func, int next)
  31. {
  32. _fsm.Transition(state, msg, func, next);
  33. }
  34. protected void AnyStateTransition(int msg, FsmFunc func, int next)
  35. {
  36. _fsm.AnyStateTransition(msg, func, next);
  37. }
  38. protected void AnyStateTransition<T, V>(V msg, FsmFunc func, T next)
  39. {
  40. Debug.Assert(typeof(T).IsEnum && typeof(V).IsEnum);
  41. int _next = Convert.ToInt32(next);
  42. int _msg = Convert.ToInt32(msg);
  43. AnyStateTransition(_msg, func, _next);
  44. }
  45. protected void EnterExitTransition<T, V>(T state, FsmFunc enter, Nullable<V> msg, FsmFunc exit) where V : struct
  46. {
  47. Debug.Assert(typeof(T).IsEnum && ((msg == null) || typeof(V).IsEnum));
  48. int _state = Convert.ToInt32(state);
  49. int _msg = msg == null ? (int)FSM_MSG.NONE : Convert.ToInt32(msg);
  50. EnterExitTransition(_state, enter, _msg, exit);
  51. }
  52. protected void EnterExitTransition(int state, FsmFunc enter, int msg, FsmFunc exit)
  53. {
  54. _fsm.EnterExitTransition(state, enter, msg, exit);
  55. }
  56. public void PostMsg<T>(T msg, params object[] args) where T : struct
  57. {
  58. Debug.Assert(typeof(T).IsEnum);
  59. int id = Convert.ToInt32(msg);
  60. PostMsg(id, args);
  61. }
  62. public void PostMsg(int msg, params object[] args)
  63. {
  64. _fsm.PostMsg(msg, args);
  65. }
  66. #endregion
  67. public int State
  68. {
  69. get { return _fsm.State; }
  70. }
  71. public bool IsServoOn
  72. {
  73. get { return _diServoOnFeedback.Value; }
  74. }
  75. public bool IsAlarm
  76. {
  77. get { return _diAlarm.Value; }
  78. }
  79. public bool IsPulseOn
  80. {
  81. get { return _diPulseOutputOn.Value; }
  82. }
  83. public bool IsWithoutOrigin
  84. {
  85. get { return _diWithoutOrigin.Value; }
  86. }
  87. public float Acceleration
  88. {
  89. get { return _aoAcceleration.Value / 100; }
  90. set { _aoAcceleration.Value = value * 100; }
  91. }
  92. public float Deceleration
  93. {
  94. get { return _aoDeceleration.Value / 100; }
  95. set { _aoDeceleration.Value = value * 100; }
  96. }
  97. public float StartFrequency
  98. {
  99. get { return _aoStartFrequency.Value / 100; }
  100. set { _aoStartFrequency.Value = value * 100; }
  101. }
  102. public float ServoSpeedSetPoint
  103. {
  104. get { return _aoServoSpeedSetPoint.Value / 100; }
  105. set { _aoServoSpeedSetPoint.Value = value * 100; }
  106. }
  107. public float ManualSpeedSetPoint
  108. {
  109. get { return _aoManualSpeedSetPoint.Value / 100; }
  110. set { _aoManualSpeedSetPoint.Value = value * 100; }
  111. }
  112. public float PositionSetPoint
  113. {
  114. get { return _aoPositionSetPoint.Value / 100; }
  115. set { _aoPositionSetPoint.Value = value * 100; }
  116. }
  117. public float PulseFeedback
  118. {
  119. get { return _aiPulseFeedback.Value / 100; }
  120. }
  121. public float ServoSpeedFeedback
  122. {
  123. get { return _aiServoSpeedFeedback.Value / 100; }
  124. }
  125. public float ManualSpeedFeedback
  126. {
  127. get { return _aiManualSpeedFeedback.Value / 100; }
  128. }
  129. public float PositionFeedback
  130. {
  131. get { return _aiPositionFeedback.Value / 100; }
  132. }
  133. private DIAccessor _diOriginSignal;
  134. private DIAccessor _diCWLimitSignal;
  135. private DIAccessor _diCCWLimitSignal;
  136. private DIAccessor _diLocationComplete;
  137. private DIAccessor _diAlarm;
  138. private DIAccessor _diPulseOutputOn;
  139. private DIAccessor _diPulseOutputComplete;
  140. private DIAccessor _diWithoutOrigin;
  141. private DIAccessor _diServoOnFeedback;
  142. private DOAccessor _doBreakRelay;
  143. private DOAccessor _doServoOn;
  144. private DOAccessor _doMoveUp;
  145. private DOAccessor _doMoveDown;
  146. private DOAccessor _doMoveToPosition;
  147. private DOAccessor _doDeviationCounterReset;
  148. private DOAccessor _doAlarmReset;
  149. private DOAccessor _doStopMoveUp;
  150. private DOAccessor _doStopMoveDown;
  151. private DOAccessor _doLookingForOrigin;
  152. private AOAccessor _aoServoSpeedSetPoint;
  153. private AOAccessor _aoManualSpeedSetPoint;
  154. private AOAccessor _aoAcceleration;
  155. private AOAccessor _aoDeceleration;
  156. private AOAccessor _aoStartFrequency;
  157. private AOAccessor _aoPositionSetPoint;
  158. private AIAccessor _aiPulseFeedback;
  159. private AIAccessor _aiServoSpeedFeedback;
  160. private AIAccessor _aiManualSpeedFeedback;
  161. private AIAccessor _aiPositionFeedback;
  162. private AIAccessor _aiAccelerationFeedback;
  163. private AIAccessor _aiDecelerationFeedback;
  164. private AIAccessor _aiStartFrequencyFeedback;
  165. private DeviceTimer _timerServoOn = new DeviceTimer();
  166. private DeviceTimer _timerServoOff = new DeviceTimer();
  167. private DeviceTimer _timerHome = new DeviceTimer();
  168. private DeviceTimer _timerResetAlarm = new DeviceTimer();
  169. private DeviceTimer _timerMove = new DeviceTimer();
  170. private DeviceTimer _timerStop = new DeviceTimer();
  171. private SCItem<double> _scServoOnTimeout = null;
  172. private SCItem<double> _scResetAlarmTimeout = null;
  173. private SCItem<double> _scMoveTimeout = null;
  174. private SCItem<double> _scStopTimeout = null;
  175. private SCItem<double> _scHomeTimeout = null;
  176. private SCItem<double> _scAcceleration = null;
  177. private SCItem<double> _scDeceleration = null;
  178. private SCItem<double> _scStartFrequency = null;
  179. private SCItem<double> _scDefaultServoSpeed = null;
  180. private SCItem<double> _scDefaultManualSpeed = null;
  181. private R_TRIG _trigCWLimit = new R_TRIG();
  182. private R_TRIG _trigCCWLimit = new R_TRIG();
  183. private IStateMachine _fsm;
  184. protected Thread _thread = null;
  185. RD_TRIG _trigPulseOutput = new RD_TRIG();
  186. public IoServoMotor(string module, XmlElement node)
  187. {
  188. _fsm = new StateMachine<IoServoMotor>("ServoMotorFSM", (int)ServoState.ServoOff, 500);
  189. Transition(ServoState.ServoOff, FSM_MSG.TIMER, MonitorServoOff, ServoState.ServoOff);
  190. Transition(ServoState.ServoOff, ServoMsg.ServoOn, DoServoOn, ServoState.ServoOff);
  191. Transition(ServoState.ServoOff, ServoMsg.TrigServoOn, null, ServoState.NotHomed);
  192. Transition(ServoState.NotHomed, FSM_MSG.TIMER, MonitorNotHomed, ServoState.NotHomed);
  193. Transition(ServoState.NotHomed, ServoMsg.Home, DoHome, ServoState.Homing);
  194. Transition(ServoState.NotHomed, ServoMsg.ServoOff, DoServoOff, ServoState.NotHomed);
  195. Transition(ServoState.NotHomed, ServoMsg.TrigServoOff, null, ServoState.ServoOff);
  196. Transition(ServoState.NotHomed, ServoMsg.TrigHomed, null, ServoState.Homed);
  197. Transition(ServoState.NotHomed, ServoMsg.TrigError, null, ServoState.Error);
  198. Transition(ServoState.Homing, FSM_MSG.TIMER, MonitorHoming, ServoState.Homing);
  199. Transition(ServoState.Homing, ServoMsg.ServoOff, DoServoOff, ServoState.Homing);
  200. Transition(ServoState.Homing, ServoMsg.Error, null, ServoState.Error);
  201. Transition(ServoState.Homing, ServoMsg.TrigServoOff, null, ServoState.ServoOff);
  202. Transition(ServoState.Homing, ServoMsg.TrigHomed, null, ServoState.Homed);
  203. Transition(ServoState.Homing, ServoMsg.TrigError, null, ServoState.Error);
  204. Transition(ServoState.Homed, FSM_MSG.TIMER, MonitorHomed, ServoState.Homed);
  205. Transition(ServoState.Homed, ServoMsg.ServoOff, DoServoOff, ServoState.Homed);
  206. Transition(ServoState.Homed, ServoMsg.MoveTo, DoMoveTo, ServoState.Moving);
  207. Transition(ServoState.Homed, ServoMsg.MoveOffset, DoMoveOffset, ServoState.Moving);
  208. Transition(ServoState.Homed, ServoMsg.TrigServoOff, null, ServoState.ServoOff);
  209. Transition(ServoState.Homed, ServoMsg.TrigNotHomed, null, ServoState.NotHomed);
  210. Transition(ServoState.Homed, ServoMsg.TrigError, null, ServoState.Error);
  211. Transition(ServoState.Homed, ServoMsg.Home, DoHome, ServoState.Homing);
  212. Transition(ServoState.Moving, ServoMsg.Home, DoHome, ServoState.Homing);
  213. Transition(ServoState.Moving, FSM_MSG.TIMER, MonitorMoving, ServoState.Moving);
  214. Transition(ServoState.Moving, ServoMsg.Stop, DoStop, ServoState.Homed);
  215. Transition(ServoState.Moving, ServoMsg.ServoOff, DoServoOff, ServoState.Moving);
  216. Transition(ServoState.Moving, ServoMsg.Error, null, ServoState.Error);
  217. Transition(ServoState.Moving, ServoMsg.TrigServoOff, null, ServoState.ServoOff);
  218. Transition(ServoState.Moving, ServoMsg.TrigNotHomed, null, ServoState.Error);
  219. Transition(ServoState.Moving, ServoMsg.TrigError, null, ServoState.Error);
  220. Transition(ServoState.Moving, ServoMsg.TrigAtPosition, null, ServoState.Homed);
  221. Transition(ServoState.Reseting, FSM_MSG.TIMER, MonitorResetting, ServoState.Reseting);
  222. Transition(ServoState.Reseting, ServoMsg.ServoOff, DoServoOff, ServoState.Reseting);
  223. Transition(ServoState.Reseting, ServoMsg.Error, null, ServoState.Error);
  224. Transition(ServoState.Reseting, ServoMsg.TrigServoOff, null, ServoState.ServoOff);
  225. Transition(ServoState.Reseting, ServoMsg.TrigRecovered, null, ServoState.NotHomed);
  226. Transition(ServoState.Reseting, ServoMsg.TrigError, null, ServoState.Error);
  227. Transition(ServoState.Error, FSM_MSG.TIMER, MonitorError, ServoState.Error);
  228. Transition(ServoState.Error, ServoMsg.Reset, DoReset, ServoState.Reseting);
  229. Transition(ServoState.Error, ServoMsg.ServoOff, DoServoOff, ServoState.Error);
  230. Transition(ServoState.Error, ServoMsg.TrigServoOff, null, ServoState.ServoOff);
  231. base.Module = module;
  232. base.Name = node.GetAttribute("id");
  233. base.Display = node.GetAttribute("display");
  234. base.DeviceID = node.GetAttribute("schematicId");
  235. _diOriginSignal = ParseDiNode("diOriginSignal", node);
  236. _diCWLimitSignal = ParseDiNode("diCWLimitSignal", node);
  237. _diCCWLimitSignal = ParseDiNode("diCCWLimitSignal", node);
  238. _diLocationComplete = ParseDiNode("diLocationComplete", node);
  239. _diAlarm = ParseDiNode("diAlarm", node);
  240. _diPulseOutputOn = ParseDiNode("diPulseOutputOn", node);
  241. _diPulseOutputComplete = ParseDiNode("diPulseOutputComplete", node);
  242. _diWithoutOrigin = ParseDiNode("diWithoutOrigin", node);
  243. _diServoOnFeedback = ParseDiNode("diServoOnFeedback", node);
  244. _doBreakRelay = ParseDoNode("doBreakRelay", node);
  245. _doServoOn = ParseDoNode("doServoOn", node);
  246. _doMoveUp = ParseDoNode("doMoveUp", node);
  247. _doMoveDown = ParseDoNode("doMoveDown", node);
  248. _doMoveToPosition = ParseDoNode("doMoveToPosition", node);
  249. _doDeviationCounterReset = ParseDoNode("doDeviationCounterReset", node);
  250. _doAlarmReset = ParseDoNode("doAlarmReset", node);
  251. _doStopMoveUp = ParseDoNode("doStopMoveUp", node);
  252. _doStopMoveDown = ParseDoNode("doStopMoveDown", node);
  253. _doLookingForOrigin = ParseDoNode("doLookingForOrigin", node);
  254. _aoServoSpeedSetPoint = ParseAoNode("aoServoSpeedSetPoint", node);
  255. _aoManualSpeedSetPoint = ParseAoNode("aoManualSpeedSetPoint", node);
  256. _aoAcceleration = ParseAoNode("aoAcceleration", node);
  257. _aoDeceleration = ParseAoNode("aoDeceleration", node);
  258. _aoStartFrequency = ParseAoNode("aoStartFrequency", node);
  259. _aoPositionSetPoint = ParseAoNode("aoPositionSetPoint", node);
  260. _aiPulseFeedback = ParseAiNode("aiPulseFeedback", node);
  261. _aiServoSpeedFeedback = ParseAiNode("aiServoSpeedFeedback", node);
  262. _aiManualSpeedFeedback = ParseAiNode("aiManualSpeedFeedback", node);
  263. _aiPositionFeedback = ParseAiNode("aiPositionFeedback", node);
  264. _aiAccelerationFeedback = ParseAiNode("aiAccelerationFeedback", node);
  265. _aiDecelerationFeedback = ParseAiNode("aiDecelerationFeedback", node);
  266. _aiStartFrequencyFeedback = ParseAiNode("aiStartFrequencyFeedback", node);
  267. _scAcceleration = ParseScNodeDouble("scAcceleration", node);
  268. _scDeceleration = ParseScNodeDouble("scDeceleration", node);
  269. _scStartFrequency = ParseScNodeDouble("scStartFrequency", node);
  270. _scDefaultServoSpeed = ParseScNodeDouble("scDefaultServoSpeed", node);
  271. _scDefaultManualSpeed = ParseScNodeDouble("scDefaultManualSpeed", node);
  272. _scServoOnTimeout = ParseScNodeDouble("scServoOnTimeout", node);
  273. _scMoveTimeout = ParseScNodeDouble("scMoveTimeout", node);
  274. _scStopTimeout = ParseScNodeDouble("scStopTimeout", node);
  275. _scResetAlarmTimeout = ParseScNodeDouble("scResetAlarmTimeout", node);
  276. _scHomeTimeout = ParseScNodeDouble("scHomeTimeout", node);
  277. }
  278. public bool ServoOn( )
  279. {
  280. PostMsg(ServoMsg.ServoOn);
  281. return true;
  282. }
  283. public bool IsServoOnState( )
  284. {
  285. return _fsm.State != (int)ServoState.ServoOff;
  286. }
  287. public bool IsHomedState( )
  288. {
  289. return _fsm.State == (int)ServoState.Homed;
  290. }
  291. public bool InvokeStop()
  292. {
  293. PostMsg(ServoMsg.Stop);
  294. return true;
  295. }
  296. public bool Home(out string reason )
  297. {
  298. if( (_fsm.State == (int)ServoState.ServoOff) || (_fsm.State == (int)ServoState.Error) )
  299. {
  300. reason = string.Format("Can not home {0} while in {1} status.", Display, ((ServoState)_fsm.State));
  301. return false;
  302. }
  303. reason = string.Format("{0} Home", Display);
  304. PostMsg(ServoMsg.Home);
  305. return true;
  306. }
  307. public bool MoveTo(double position, out string reason)
  308. {
  309. if (_fsm.State != (int)ServoState.Homed)
  310. {
  311. reason = string.Format("Can not move {0} while in {1} status.", Display, ((ServoState)_fsm.State));
  312. return false;
  313. }
  314. double speed = _scDefaultServoSpeed.Value;
  315. reason = string.Format("{0} Move to {1} at speed {2}", Display, position, speed);
  316. PostMsg(ServoMsg.MoveTo, speed, position);
  317. return true;
  318. }
  319. private bool DoServoOn(object[] objs)
  320. {
  321. string reason = string.Empty;
  322. if (!_doServoOn.SetValue(true, out reason))
  323. {
  324. return false;
  325. }
  326. _timerServoOn.Start(_scServoOnTimeout.Value * 1000);
  327. return true;
  328. }
  329. private bool MonitorServoOff(object[] objs)
  330. {
  331. if (_doServoOn.Value && _timerServoOn.IsTimeout())
  332. {
  333. _doServoOn.Value = false;
  334. EV.PostMessage("System", EventEnum.DefaultWarning, string.Format("Can not turn {0} servo on in {1} seconds", Display, _scServoOnTimeout.Value));
  335. }
  336. if (IsServoOn)
  337. {
  338. PostMsg(ServoMsg.TrigServoOn);
  339. return true;
  340. }
  341. return true;
  342. }
  343. private bool DoHome(object[] objs)
  344. {
  345. string reason = string.Empty;
  346. if (!_doLookingForOrigin.SetValue(true, out reason))
  347. {
  348. return false;
  349. }
  350. _timerHome.Start(_scHomeTimeout.Value * 1000);
  351. return true;
  352. }
  353. private bool DoServoOff(object[] objs)
  354. {
  355. string reason = string.Empty;
  356. if (!_doServoOn.SetValue(false, out reason))
  357. {
  358. LOG.Write(reason);
  359. return false;
  360. }
  361. if (!_doMoveToPosition.SetValue(false, out reason))
  362. {
  363. return false;
  364. }
  365. _timerServoOff.Start(_scServoOnTimeout.Value * 1000);
  366. return true;
  367. }
  368. private bool MonitorNotHomed(object[] objs)
  369. {
  370. if (!_doServoOn.Value && _timerServoOff.IsTimeout())
  371. {
  372. EV.PostMessage("System", EventEnum.DefaultWarning, string.Format("Can not turn {0} servo off in {1} seconds", Display, _scServoOnTimeout.Value));
  373. _doServoOn.Value = true;
  374. }
  375. if (!IsServoOn)
  376. {
  377. PostMsg(ServoMsg.TrigServoOff);
  378. return true;
  379. }
  380. if (!IsWithoutOrigin)
  381. {
  382. PostMsg(ServoMsg.TrigHomed);
  383. return true;
  384. }
  385. if (IsAlarm)
  386. {
  387. PostMsg(ServoMsg.TrigError);
  388. return true;
  389. }
  390. return true;
  391. }
  392. private bool MonitorHoming(object[] objs)
  393. {
  394. if (!_doServoOn.Value && _timerServoOff.IsTimeout())
  395. {
  396. EV.PostMessage("System", EventEnum.DefaultWarning, string.Format("Can not turn {0} servo off in {1} seconds", Display, _scServoOnTimeout.Value));
  397. _doServoOn.Value = true;
  398. }
  399. if (_doLookingForOrigin.Value && _timerHome.IsTimeout())
  400. {
  401. _doLookingForOrigin.Value = false;
  402. EV.PostMessage("System", EventEnum.DefaultWarning, string.Format("Can not home {0} in {1} seconds", Display, _scHomeTimeout.Value));
  403. }
  404. if (!IsServoOn)
  405. {
  406. PostMsg(ServoMsg.TrigServoOff);
  407. return true;
  408. }
  409. if (!IsWithoutOrigin)
  410. {
  411. if (_timerHome.GetElapseTime() < 1000)
  412. {
  413. return true; // Delay 1秒钟。
  414. }
  415. PostMsg(ServoMsg.TrigHomed);
  416. _doLookingForOrigin.Value = false;
  417. return true;
  418. }
  419. if (IsAlarm)
  420. {
  421. PostMsg(ServoMsg.TrigError);
  422. return true;
  423. }
  424. return true;
  425. }
  426. private bool MonitorHomed(object[] objs)
  427. {
  428. if (!_doServoOn.Value && _timerServoOff.IsTimeout())
  429. {
  430. EV.PostMessage("System", EventEnum.DefaultWarning, string.Format("Can not turn {0} servo off in {1} seconds", Display, _scServoOnTimeout.Value));
  431. _doServoOn.Value = true;
  432. }
  433. if (!IsServoOn)
  434. {
  435. PostMsg(ServoMsg.TrigServoOff);
  436. return true;
  437. }
  438. if (IsWithoutOrigin)
  439. {
  440. PostMsg(ServoMsg.TrigNotHomed);
  441. return true;
  442. }
  443. if (IsAlarm)
  444. {
  445. PostMsg(ServoMsg.TrigError);
  446. return true;
  447. }
  448. return true;
  449. }
  450. private bool MonitorMoving(object[] objs)
  451. {
  452. bool isWroteToPlc = ((Math.Abs(ServoSpeedSetPoint - ServoSpeedFeedback) < 0.0001)
  453. && (Math.Abs(PositionSetPoint - PositionFeedback) < 0.0001));
  454. bool isAtPosition = Math.Abs(PositionSetPoint - PulseFeedback) < 0.0001;
  455. string reason = string.Empty;
  456. if (isAtPosition && !_diPulseOutputOn.Value)
  457. {
  458. _doMoveToPosition.SetValue(false, out reason);
  459. PostMsg(ServoMsg.TrigAtPosition);
  460. LOG.Write(string.Format("{0} Moved to position {1}", Display, PositionSetPoint ));
  461. return true;
  462. }
  463. if (isWroteToPlc && !_doMoveToPosition.Value)
  464. {
  465. _doMoveToPosition.SetValue(true, out reason);
  466. _timerMove.Start(_scMoveTimeout.Value * 1000);
  467. LOG.Write(string.Format("{0} Send moving command ", Display ));
  468. }
  469. if (_doMoveToPosition.Value && _timerMove.IsTimeout())
  470. {
  471. _doMoveToPosition.SetValue(false, out reason);
  472. EV.PostMessage("System", EventEnum.DefaultWarning, string.Format("Can not move {0} to position in {1} seconds", Display, _scServoOnTimeout.Value));
  473. PostMsg(ServoMsg.Error);
  474. return true;
  475. }
  476. if (!IsServoOn)
  477. {
  478. PostMsg(ServoMsg.TrigServoOff);
  479. return true;
  480. }
  481. if (IsWithoutOrigin)
  482. {
  483. PostMsg(ServoMsg.TrigNotHomed);
  484. return true;
  485. }
  486. if (IsAlarm)
  487. {
  488. PostMsg(ServoMsg.TrigError);
  489. return true;
  490. }
  491. return true;
  492. }
  493. private bool DoMoveTo(object[] objs)
  494. {
  495. ServoSpeedSetPoint = (float)(double)objs[0];
  496. PositionSetPoint = (float)(double)objs[1];
  497. //string reason;
  498. //if (!_doMoveToPosition.SetValue(true, out reason))
  499. //{
  500. // LOG.Write(reason);
  501. // return false;
  502. //}
  503. LOG.Write(string.Format("{0} Moving to {1}", Display, PositionSetPoint));
  504. return true;
  505. }
  506. private bool DoMoveOffset(object[] objs)
  507. {
  508. double speed = (double)objs[0];
  509. double offset = (double)objs[1];
  510. double position = PulseFeedback + offset;
  511. PositionSetPoint = (float)position;
  512. ServoSpeedSetPoint = (float)speed;
  513. return true;
  514. }
  515. private bool DoStop(object[] objs)
  516. {
  517. string reason = string.Empty;
  518. _doMoveToPosition.SetValue(false, out reason);
  519. return true;
  520. }
  521. private bool MonitorError(object[] objs)
  522. {
  523. if (!IsServoOn)
  524. {
  525. PostMsg(ServoMsg.TrigServoOff);
  526. return true;
  527. }
  528. return true;
  529. }
  530. private bool MonitorResetting(object[] objs)
  531. {
  532. string reason;
  533. if (_doAlarmReset.Value && _timerResetAlarm.IsTimeout())
  534. {
  535. _doAlarmReset.SetValue(false, out reason);
  536. EV.PostMessage("System", EventEnum.DefaultWarning, string.Format("Can not recover {0} in {1} seconds", Display, _scResetAlarmTimeout.Value));
  537. PostMsg(ServoMsg.Error);
  538. return true;
  539. }
  540. if (!IsServoOn)
  541. {
  542. PostMsg(ServoMsg.TrigServoOff);
  543. return true;
  544. }
  545. if (IsAlarm)
  546. {
  547. PostMsg(ServoMsg.TrigError);
  548. return true;
  549. }
  550. else
  551. {
  552. PostMsg(ServoMsg.TrigRecovered);
  553. }
  554. return true;
  555. }
  556. private bool DoReset(object[] objs)
  557. {
  558. string reason;
  559. _doAlarmReset.SetValue(true, out reason);
  560. _timerResetAlarm.Start(_scResetAlarmTimeout.Value * 1000);
  561. return true;
  562. }
  563. public bool Initialize()
  564. {
  565. Acceleration = (float)_scAcceleration.Value;
  566. Deceleration = (float)_scDeceleration.Value;
  567. StartFrequency = (float)_scStartFrequency.Value;
  568. ManualSpeedSetPoint = (float)_scDefaultManualSpeed.Value;
  569. ServoSpeedSetPoint = (float)_scDefaultServoSpeed.Value;
  570. DATA.Subscribe(string.Format("Device.{0}.{1}", Module, Name), () =>
  571. {
  572. AITServoMotorData data = new AITServoMotorData()
  573. {
  574. DeviceName = Name,
  575. DeviceSchematicId = DeviceID,
  576. DisplayName = Display,
  577. IsServoOn = IsServoOn,
  578. IsAlarm = IsAlarm,
  579. IsWithoutOrigin = IsWithoutOrigin,
  580. IsPulseOn = IsPulseOn,
  581. PositionFeedback = PositionFeedback,
  582. PositionSetPoint = PositionSetPoint,
  583. PulseFeedback = PulseFeedback,
  584. ManualSpeedSetPoint = ManualSpeedSetPoint,
  585. ManualSpeedFeedback = ManualSpeedFeedback,
  586. State = State,
  587. ServoSpeedFeedback = ServoSpeedFeedback,
  588. ServoSpeedSetPoint = ServoSpeedSetPoint,
  589. };
  590. return data;
  591. }, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  592. DEVICE.Register(string.Format("{0}.{1}", Name, AITServoMotorOperation.SetServoOn),
  593. (out string reason, int time, object[] param) =>
  594. {
  595. reason = string.Format("{0} Servo on", Display);
  596. PostMsg(ServoMsg.ServoOn);
  597. return true;
  598. });
  599. DEVICE.Register(string.Format("{0}.{1}", Name, AITServoMotorOperation.Home),
  600. (out string reason, int time, object[] param) =>
  601. {
  602. if( (_fsm.State == (int)ServoState.ServoOff) || (_fsm.State == (int)ServoState.Error) )
  603. {
  604. reason = string.Format("Can not home {0} while in {1} status.", Display, ((ServoState)_fsm.State));
  605. return false;
  606. }
  607. reason = string.Format("{0} Home", Display);
  608. PostMsg(ServoMsg.Home);
  609. return true;
  610. });
  611. DEVICE.Register(string.Format("{0}.{1}", Name, AITServoMotorOperation.SetServoOff),
  612. (out string reason, int time, object[] param) =>
  613. {
  614. reason = string.Format("{0} Servo off", Display);
  615. PostMsg(ServoMsg.ServoOff);
  616. return true;
  617. });
  618. DEVICE.Register(string.Format("{0}.{1}", Name, AITServoMotorOperation.MoveTo),
  619. (out string reason, int time, object[] param) =>
  620. {
  621. if (_fsm.State != (int)ServoState.Homed)
  622. {
  623. reason = string.Format("Can not move {0} while in {1} status.", Display, ((ServoState)_fsm.State));
  624. return false;
  625. }
  626. double speed = Convert.ToDouble(param[0]);
  627. double position = Convert.ToDouble(param[1]);
  628. reason = string.Format("{0} Move to {1} at speed {2}", Display, position, speed);
  629. PostMsg(ServoMsg.MoveTo, speed, position);
  630. return true;
  631. });
  632. DEVICE.Register(string.Format("{0}.{1}", Name, AITServoMotorOperation.MoveOffset),
  633. (out string reason, int time, object[] param) =>
  634. {
  635. if (_fsm.State != (int)ServoState.Homed)
  636. {
  637. reason = string.Format("Can not move {0} while in {1} status.", Display, ((ServoState)_fsm.State));
  638. return false;
  639. }
  640. double speed = Convert.ToDouble(param[0]);
  641. double offset = Convert.ToDouble(param[1]);
  642. reason = string.Format("{0} Move offset {1} at speed {2}", Display, offset, speed);
  643. PostMsg(ServoMsg.MoveOffset, speed, offset);
  644. return true;
  645. });
  646. DEVICE.Register(string.Format("{0}.{1}", Name, AITServoMotorOperation.Reset),
  647. (out string reason, int time, object[] param) =>
  648. {
  649. if (_fsm.State != (int)ServoState.Error)
  650. {
  651. reason = string.Format("Can not reset {0} while in {1} status.", Display, ((ServoState)_fsm.State));
  652. return false;
  653. }
  654. reason = string.Format("{0} Reset", Display);
  655. PostMsg(ServoMsg.Reset);
  656. return true;
  657. });
  658. DEVICE.Register(string.Format("{0}.{1}", Name, AITServoMotorOperation.Stop),
  659. (out string reason, int time, object[] param) =>
  660. {
  661. if (_fsm.State != (int)ServoState.Moving)
  662. {
  663. reason = string.Format("Can not stop {0} while in {1} status.", Display, ((ServoState)_fsm.State));
  664. return false;
  665. }
  666. reason = string.Format("{0} Stop move", Display);
  667. PostMsg(ServoMsg.Stop);
  668. return true;
  669. });
  670. _fsm.Start();
  671. _thread = new Thread(new ThreadStart(_fsm.Loop));
  672. _thread.Name = _fsm.Name;
  673. _thread.Start();
  674. while (!_thread.IsAlive)
  675. Thread.Sleep(1);
  676. return true;
  677. }
  678. public void Terminate()
  679. {
  680. _fsm.Stop();
  681. if (_thread.IsAlive)
  682. {
  683. Thread.Sleep(100);
  684. if (_thread.IsAlive)
  685. {
  686. try
  687. {
  688. _thread.Abort();
  689. }
  690. catch (Exception ex)
  691. {
  692. LOG.Error(String.Format("IoServoMotor terminate has exception."), ex);
  693. }
  694. }
  695. }
  696. }
  697. public void Monitor()
  698. {
  699. _trigCCWLimit.CLK = _diCCWLimitSignal.Value;
  700. if (_trigCCWLimit.Q)
  701. {
  702. EV.PostMessage(Module, EventEnum.DefaultWarning, string.Format("{0} in CCW Limit", Display));
  703. }
  704. _trigCWLimit.CLK = _diCWLimitSignal.Value;
  705. if (_trigCWLimit.Q)
  706. {
  707. EV.PostMessage(Module, EventEnum.DefaultWarning, string.Format("{0} in CW Limit", Display));
  708. }
  709. Acceleration = (float)_scAcceleration.Value;
  710. Deceleration = (float)_scDeceleration.Value;
  711. StartFrequency = (float)_scStartFrequency.Value;
  712. ManualSpeedSetPoint = (float)_scDefaultManualSpeed.Value;
  713. ServoSpeedSetPoint = (float)_scDefaultServoSpeed.Value;
  714. _trigPulseOutput.CLK = _diPulseOutputOn.Value;
  715. if (_trigPulseOutput.R)
  716. {
  717. LOG.Write(string.Format("{0} Moving", Display ));
  718. }
  719. if (_trigPulseOutput.T)
  720. {
  721. LOG.Write(string.Format("{0} Stop Moving", Display ));
  722. }
  723. }
  724. public void Reset()
  725. {
  726. PostMsg(ServoMsg.Reset);
  727. }
  728. public bool IsStoppedAtPosition(double position)
  729. {
  730. return (Math.Abs(position - PulseFeedback) < 0.01)
  731. && (Math.Abs(position - PositionSetPoint) < 0.01)
  732. && !_diPulseOutputOn.Value && IsHomedState( );
  733. }
  734. }
  735. }