LinMotStartVAIPositionRoutine.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using Aitex.Core.RT.Log;
  2. using Aitex.Core.RT.Routine;
  3. using MECF.Framework.Common.Device.LinMot;
  4. using MECF.Framework.Common.Routine;
  5. using CyberX8_Core;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace CyberX8_RT.Devices.LinMot
  13. {
  14. public class LinMotStartVAIPositionRoutine : RoutineBase, IRoutine
  15. {
  16. private enum VAIPositionStep
  17. {
  18. SwitchOn,
  19. StartGoTopPosition,
  20. Delay,
  21. FirstWaitSendNextCommand,
  22. LoopGoBottomPosition,
  23. LoopWaitBottomCheckDirection,
  24. LoopGoTopPosition,
  25. LoopWaitTopCheckDirection,
  26. LoopEnd,
  27. LastGoBottomPosition,
  28. LastGoBottomPositionWait,
  29. CheckTopMotor,
  30. CheckMotor,
  31. LastDelay,
  32. SwitchOff,
  33. End
  34. }
  35. #region 内部变量
  36. /// <summary>
  37. /// 所有扫描次数
  38. /// </summary>
  39. private int _totalScan = 0;
  40. /// <summary>
  41. /// 当前次数
  42. /// </summary>
  43. private int _currentScan = 0;
  44. /// <summary>
  45. /// 参数数据
  46. /// </summary>
  47. private LinMotDeviceData _linMotDeviceData;
  48. /// <summary>
  49. /// 电机对象
  50. /// </summary>
  51. private LinMotAxis _axis;
  52. /// <summary>
  53. /// 上一次方向
  54. /// </summary>
  55. private string _lastDirection = "";
  56. /// <summary>
  57. /// 最开始的方向
  58. /// </summary>
  59. private string _startDirection = "";
  60. #endregion
  61. #region 属性
  62. /// <summary>
  63. /// 当前scan次数
  64. /// </summary>
  65. public int CurrentScan { get { return _currentScan; } }
  66. #endregion
  67. /// <summary>
  68. /// 构造函数
  69. /// </summary>
  70. /// <param name="module"></param>
  71. public LinMotStartVAIPositionRoutine(string module,LinMotAxis axis) : base(module)
  72. {
  73. _axis = axis;
  74. }
  75. /// <summary>
  76. /// 手动中止
  77. /// </summary>
  78. public void Abort()
  79. {
  80. Runner.Stop("Manual Abort");
  81. }
  82. public RState Monitor()
  83. {
  84. if(_totalScan ==1 )
  85. {
  86. Runner.Run(VAIPositionStep.SwitchOn, () => { return _axis.SendOperation(LinMotOperation.SwitchOn); }
  87. , () => { return _axis.IsSwitchOn; },_delay_5s)
  88. .Run(VAIPositionStep.StartGoTopPosition, SendVAIGoTopPosition, CheckMotorOn, _delay_3s)
  89. .Delay(VAIPositionStep.Delay, _delay_2s)
  90. .Run(VAIPositionStep.LastGoBottomPosition, ContinueVAIGoBottomPosition,CheckMotorOn, _delay_3s)
  91. .WaitWithStopCondition(VAIPositionStep.CheckTopMotor, CheckAxisArrivedTop,
  92. () => CheckAxisMotorStopStatus((int)_linMotDeviceData.TopPosition), _delay_60s)
  93. .WaitWithStopCondition(VAIPositionStep.CheckMotor, CheckAxisArrivedBottom,
  94. () => CheckAxisMotorStopStatus((int)_linMotDeviceData.BottomPosition), _delay_60s)
  95. .Wait(VAIPositionStep.CheckMotor, ()=>CheckMotorOff(false))
  96. .Delay(VAIPositionStep.LastDelay,_delay_1s)
  97. .Run(VAIPositionStep.SwitchOff, () => { return _axis.SendOperation(LinMotOperation.SwitchOff); }, CheckSwitchOff, _delay_5s)
  98. .End(VAIPositionStep.End, NullFun, _delay_1ms);
  99. }
  100. else
  101. {
  102. Runner.Run(VAIPositionStep.SwitchOn, () => { return _axis.SendOperation(LinMotOperation.SwitchOn); },
  103. () => { return _axis.IsSwitchOn; }, _delay_5s)
  104. .Run(VAIPositionStep.StartGoTopPosition, SendVAIGoTopPosition, CheckMotorOn, _delay_3s)
  105. .Delay(VAIPositionStep.Delay, _delay_2s)
  106. .LoopStart(VAIPositionStep.LoopGoBottomPosition, "Loop Scan", _totalScan - 1, ContinueVAIGoBottomPosition, _delay_1ms)
  107. .LoopRunWithStopStatus(VAIPositionStep.LoopWaitBottomCheckDirection, CheckDirectionChanged,
  108. ()=>CheckMotorOff(true),_delay_30s)
  109. .LoopRun(VAIPositionStep.LoopGoTopPosition, ContinueVAIGoTopPosition, _delay_1ms)
  110. .LoopRunWithStopStatus(VAIPositionStep.LoopWaitTopCheckDirection, CheckDirectionChanged,
  111. ()=>CheckMotorOff(true), _delay_30s)
  112. .LoopEnd(VAIPositionStep.LoopEnd, NullFun, _delay_1ms)
  113. .Run(VAIPositionStep.LastGoBottomPosition, ContinueVAIGoBottomPosition,CheckMotorOn, _delay_3s)
  114. .WaitWithStopCondition(VAIPositionStep.LastGoBottomPositionWait, CheckAxisArrivedBottom,
  115. ()=>CheckAxisMotorStopStatus((int)_linMotDeviceData.BottomPosition), _delay_60s)
  116. .Wait(VAIPositionStep.CheckMotor,()=>CheckMotorOff(false))
  117. .Delay(VAIPositionStep.LastDelay, _delay_1s)
  118. .Run(VAIPositionStep.SwitchOff, () => { return _axis.SendOperation(LinMotOperation.SwitchOff); }, CheckSwitchOff, _delay_5s)
  119. .End(VAIPositionStep.End, NullFun, _delay_1ms);
  120. }
  121. return Runner.Status;
  122. }
  123. /// <summary>
  124. /// 前置条件
  125. /// </summary>
  126. /// <returns></returns>
  127. private bool CheckPreCondition()
  128. {
  129. if(!_axis.IsHomed)
  130. {
  131. LOG.WriteLog(eEvent.ERR_LINMOT, Module, "axis is not homed");
  132. return false;
  133. }
  134. if (!_axis.IsConnectd)
  135. {
  136. LOG.WriteLog(eEvent.ERR_LINMOT, Module, "axis is not connected");
  137. return false;
  138. }
  139. if(_axis.IsError)
  140. {
  141. LOG.WriteLog(eEvent.ERR_LINMOT, Module, "axis is in error");
  142. return false;
  143. }
  144. return true;
  145. }
  146. /// <summary>
  147. /// 第一次Goto Top
  148. /// </summary>
  149. /// <returns></returns>
  150. private bool SendVAIGoTopPosition()
  151. {
  152. if(!_axis.IsConnectd)
  153. {
  154. LOG.WriteLog(eEvent.ERR_LINMOT, Module, "axis is not connected");
  155. return false;
  156. }
  157. _lastDirection= _axis.Direction;
  158. _currentScan++;
  159. return _axis.SendVAIGoToPosition((int)_linMotDeviceData.TopPosition, (int)_linMotDeviceData.UpMaxSpeed, _linMotDeviceData.UpMaxAcceleration,
  160. _linMotDeviceData.UpMaxDeceleration);
  161. }
  162. /// <summary>
  163. /// 检验Axis运动状态
  164. /// </summary>
  165. /// <returns></returns>
  166. private bool CheckMotorOn()
  167. {
  168. return _axis.IsMotorOn;
  169. }
  170. /// <summary>
  171. /// 检验Axis运动状态
  172. /// </summary>
  173. /// <returns></returns>
  174. private bool CheckMotorOff(bool showError)
  175. {
  176. bool result= !_axis.IsMotorOn;
  177. if (result)
  178. {
  179. if (showError)
  180. {
  181. LOG.WriteLog(eEvent.ERR_LINMOT, Module, "Linmot is stop");
  182. }
  183. }
  184. return result;
  185. }
  186. /// <summary>
  187. /// 检验电机停止状态
  188. /// </summary>
  189. /// <returns></returns>
  190. private bool CheckAxisMotorStopStatus(int targetPosition)
  191. {
  192. bool arrived = CheckAxisArrived(targetPosition);
  193. if (!arrived)
  194. {
  195. return CheckMotorOff(true);
  196. }
  197. else
  198. {
  199. return false;
  200. }
  201. }
  202. /// <summary>
  203. /// 检验方向是否发生变化
  204. /// </summary>
  205. /// <returns></returns>
  206. private bool CheckDirectionChanged()
  207. {
  208. bool result = _lastDirection != _axis.Direction;
  209. if(result)
  210. {
  211. _lastDirection = _axis.Direction;
  212. if(_lastDirection==_startDirection)
  213. {
  214. _currentScan++;
  215. }
  216. }
  217. return result;
  218. }
  219. /// <summary>
  220. /// 不停止Go Bottom
  221. /// </summary>
  222. /// <returns></returns>
  223. private bool ContinueVAIGoBottomPosition()
  224. {
  225. if (!_axis.IsConnectd)
  226. {
  227. LOG.WriteLog(eEvent.ERR_LINMOT, Module, "axis is not connected");
  228. return false;
  229. }
  230. bool result = _axis.IsMotorOn;
  231. if (!result)
  232. {
  233. LOG.WriteLog(eEvent.ERR_LINMOT, Module, "axis is stopped");
  234. return false;
  235. }
  236. return _axis.SendGAIGoToPositionAfterActualCommand((int)_linMotDeviceData.BottomPosition, (int)_linMotDeviceData.DownMaxSpeed,
  237. _linMotDeviceData.DownMaxAcceleration, _linMotDeviceData.DownMaxDeceleration);
  238. }
  239. /// <summary>
  240. /// 检验电机是还到达顶部
  241. /// </summary>
  242. /// <returns></returns>
  243. private bool CheckAxisArrivedTop()
  244. {
  245. return CheckAxisArrived((int)_linMotDeviceData.TopPosition);
  246. }
  247. /// <summary>
  248. /// 检验电机是还到达底部
  249. /// </summary>
  250. /// <returns></returns>
  251. private bool CheckAxisArrivedBottom()
  252. {
  253. return CheckAxisArrived((int)_linMotDeviceData.BottomPosition);
  254. }
  255. private bool CheckAxisArrived(int targetPosition)
  256. {
  257. double bias = Math.Abs(_axis.CurrentIntPosition - targetPosition) / Math.Abs(_linMotDeviceData.TopPosition - _linMotDeviceData.BottomPosition);
  258. return bias <= 0.1;
  259. }
  260. /// <summary>
  261. /// 不停止Go Top
  262. /// </summary>
  263. /// <returns></returns>
  264. private bool ContinueVAIGoTopPosition()
  265. {
  266. if (!_axis.IsConnectd)
  267. {
  268. LOG.WriteLog(eEvent.ERR_LINMOT, Module, "axis is not connected");
  269. return false;
  270. }
  271. return _axis.SendGAIGoToPositionAfterActualCommand((int)_linMotDeviceData.TopPosition, (int)_linMotDeviceData.UpMaxSpeed, _linMotDeviceData.UpMaxAcceleration,
  272. _linMotDeviceData.UpMaxDeceleration);
  273. }
  274. /// <summary>
  275. /// 检验是否SwitchOff
  276. /// </summary>
  277. /// <returns></returns>
  278. private bool CheckSwitchOff()
  279. {
  280. return !_axis.IsSwitchOn;
  281. }
  282. /// <summary>
  283. /// 启动
  284. /// </summary>
  285. /// <param name="objs"></param>
  286. /// <returns></returns>
  287. public RState Start(params object[] objs)
  288. {
  289. _totalScan=(int)objs[0];
  290. _linMotDeviceData=(LinMotDeviceData)objs[1];
  291. _lastDirection = _axis.Direction;
  292. _startDirection = _axis.Direction;
  293. _currentScan = 0;
  294. if(!CheckPreCondition())
  295. {
  296. return RState.Failed;
  297. }
  298. Runner.Start(Module, "VAI Go to Position");
  299. return RState.Running;
  300. }
  301. }
  302. }