PlatingCellDepositionRoutine.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. using Aitex.Core.RT.Device;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.RT.Routine;
  4. using Aitex.Core.Util;
  5. using MECF.Framework.Common.Alarm;
  6. using MECF.Framework.Common.Beckhoff.Station;
  7. using MECF.Framework.Common.CommonData.PowerSupplier;
  8. using MECF.Framework.Common.RecipeCenter;
  9. using MECF.Framework.Common.Routine;
  10. using MECF.Framework.Common.Utilities;
  11. using PunkHPX8_Core;
  12. using PunkHPX8_RT.Devices.AXIS;
  13. using PunkHPX8_RT.Devices.PlatingCell;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Diagnostics;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. namespace PunkHPX8_RT.Modules.PlatingCell
  21. {
  22. public class PlatingCellDepositionRoutine : RoutineBase, IRoutine
  23. {
  24. private enum RunRecipeStep
  25. {
  26. RunPowerStep,
  27. RunPowerStepWait,
  28. LoopRotationStart,
  29. LoopVerticalGotoOffSet,
  30. LoopVerticalGotoOffSetCheck,
  31. LoopRecordRotationStartTime,
  32. LoopRotationStartIfCurrentIsStoped,//如果当前电机是停止状态,启动起来
  33. LoopRotationChangeSpeed,
  34. LoopCalculateRotationDelayTime,
  35. LoopRotationDelay,
  36. LoopRotationStopIfCurrentIsRuning, //如果当前电机是启动状态,则停止
  37. LoopRotationStopCheck,
  38. LoopRotationBiRotation,
  39. LoopRotationBiRotationCheck,
  40. LoopEnd,
  41. RotationStartIfCurrentIsStoped,
  42. End
  43. }
  44. #region 常量
  45. /// <summary>
  46. /// ROTATION电机转速比例
  47. /// </summary>
  48. private const int SPEED_RATIO = 1;
  49. private const int ROTATION_FAR_POSITION = 12 * 60 * 60 * 500 * 6; //以500rmp 运行12个小时的位置
  50. #endregion
  51. /// <summary>
  52. /// recipe
  53. /// </summary>
  54. private DepRecipe _recipe;
  55. /// <summary>
  56. /// Rotation axis
  57. /// </summary>
  58. private JetAxisBase _rotationAxis;
  59. /// <summary>
  60. /// Platingcell device
  61. /// </summary>
  62. private PlatingCellDevice _device;
  63. /// <summary>
  64. /// vertical axis entity
  65. /// </summary>
  66. private PlatingCellVerticalEntity _verticalEntity;
  67. /// <summary>
  68. /// 不通电
  69. /// </summary>
  70. private bool _isZeroCurrent = false;
  71. /// <summary>
  72. /// 是否启动smart spin
  73. /// </summary>
  74. private bool _isSmartSpin = false;
  75. /// <summary>
  76. /// Power step集合
  77. /// </summary>
  78. List<PowerSupplierStepPeriodData> _powerSupplierStepPeriodDatas = new List<PowerSupplierStepPeriodData>();
  79. /// <summary>
  80. /// 双向旋转routine
  81. /// </summary>
  82. private RotationBiDirectionRoutine _rotationBiDirectionRoutine;
  83. /// <summary>
  84. /// 电镀单步延迟等待时间(recipe的时间减去rotation启动的时间,单位ms)
  85. /// </summary>
  86. private int _singalStepDelayTime;
  87. /// <summary>
  88. /// 当前执行到电镀步骤的索引
  89. /// </summary>
  90. private int _depositionStepIndex = 0;
  91. /// <summary>
  92. /// 计时器
  93. /// </summary>
  94. private Stopwatch _stopwatch = new Stopwatch();
  95. /// <summary>
  96. /// 构造函数
  97. /// </summary>
  98. /// <param name="module"></param>
  99. public PlatingCellDepositionRoutine(string module) : base(module)
  100. {
  101. _rotationBiDirectionRoutine = new RotationBiDirectionRoutine(module);
  102. }
  103. /// <summary>
  104. /// 中止
  105. /// </summary>
  106. public void Abort()
  107. {
  108. Runner.Stop("Manual Abort");
  109. }
  110. /// <summary>
  111. /// 监控
  112. /// </summary>
  113. /// <returns></returns>
  114. public RState Monitor()
  115. { //没有上电保护,此刻给电
  116. Runner.RunIf(RunRecipeStep.RunPowerStep, _recipe.IsEntryTypeCold, StartPowerStep, _delay_1ms)
  117. //检验步阶电流是否完成
  118. .WaitWithStopCondition(RunRecipeStep.RunPowerStepWait, CheckRecipeStepEndStatus, CheckRecipeStepStopStatus, false, _delay_10s)
  119. .LoopStart(RunRecipeStep.LoopRotationStart, "Start rotation cycle",_recipe.DepStepCount,NullFun)
  120. //vertical 调整位置(第一步不用调,entry过程已经走到位置了)
  121. .LoopRunIf(RunRecipeStep.LoopVerticalGotoOffSet, _recipe.DepSteps[_depositionStepIndex].PlatingZoffset!=0 && _depositionStepIndex!=0, () => StartVertical("Plate", _recipe.DepSteps[_depositionStepIndex].PlatingZoffset),_delay_1ms)
  122. .LoopRunIfWithStopStatus(RunRecipeStep.LoopVerticalGotoOffSetCheck, _recipe.DepSteps[_depositionStepIndex].PlatingZoffset!=0 && _depositionStepIndex != 0, CheckVerticalEnd, CheckVerticalError)
  123. //不带双向旋转,时间到了直接变速(如果当前电机是停止状态,启动起来)
  124. .LoopRunIf(RunRecipeStep.LoopRecordRotationStartTime, !_recipe.DepSteps[_depositionStepIndex].BiDireaction,
  125. () => { return StartRecordTime(); }, _delay_1ms)
  126. .LoopRunIf(RunRecipeStep.LoopRotationStartIfCurrentIsStoped, CheckRotationIsIdle() && !_recipe.DepSteps[_depositionStepIndex].BiDireaction,
  127. () => { return StartRotation(ROTATION_FAR_POSITION, _recipe.DepSteps[_depositionStepIndex].PlatingSpeed); }, _delay_1ms)
  128. .LoopRunIf(RunRecipeStep.LoopRotationChangeSpeed, !_recipe.DepSteps[_depositionStepIndex].BiDireaction && !CheckRotationIsIdle(), () => ChangeRotationSpeed(_recipe.DepSteps[_depositionStepIndex].PlatingSpeed),_delay_1ms)
  129. .LoopRunIf(RunRecipeStep.LoopCalculateRotationDelayTime, !_recipe.DepSteps[_depositionStepIndex].BiDireaction , CalculateActualDepStepTime, _delay_1ms)
  130. .LoopDelayIf(RunRecipeStep.LoopRotationDelay, !_recipe.DepSteps[_depositionStepIndex].BiDireaction, _recipe.DepSteps[_depositionStepIndex].DurartionSeconds * 1000 - _singalStepDelayTime)
  131. //带双向旋转,启动双向旋转的routine(如果当前电机是启动状态,则停止)
  132. .LoopRunIf(RunRecipeStep.LoopRotationStopIfCurrentIsRuning, checkRotationIsRunning() && _recipe.DepSteps[_depositionStepIndex].BiDireaction,
  133. _rotationAxis.StopPositionOperation, _delay_1ms)
  134. .LoopRunIfWithStopStatus(RunRecipeStep.LoopRotationStopCheck, checkRotationIsRunning() && _recipe.DepSteps[_depositionStepIndex].BiDireaction,
  135. CheckRotationIsIdle, CheckRotationPositionRunStop)
  136. .LoopRunIf(RunRecipeStep.LoopRotationBiRotation, _recipe.DepSteps[_depositionStepIndex].BiDireaction,
  137. () => _rotationBiDirectionRoutine.Start(_recipe.DepSteps[_depositionStepIndex].DurartionSeconds, _recipe.DepSteps[_depositionStepIndex].BiDFrequency, _recipe.DepSteps[_depositionStepIndex].PlatingSpeed) == RState.Running,_delay_1ms)
  138. .LoopRunIfWithStopStatus(RunRecipeStep.LoopRotationBiRotationCheck, _recipe.DepSteps[_depositionStepIndex].BiDireaction,
  139. () => CommonFunction.CheckRoutineEndState(_rotationBiDirectionRoutine),
  140. () => CommonFunction.CheckRoutineStopState(_rotationBiDirectionRoutine))
  141. .LoopEnd(RunRecipeStep.LoopEnd, UpdateDepositionIndex, _delay_1ms)
  142. //如果电镀最后一步带双向旋转,后续电机会停止,需要再把电机启动起来(用Reclaim的速度)
  143. .RunIf(RunRecipeStep.RotationStartIfCurrentIsStoped, CheckRotationIsIdle(), () => { return StartRotation(ROTATION_FAR_POSITION,_recipe.ReclaimSpeed); }, _delay_1ms)
  144. .End(RunRecipeStep.End, NullFun);
  145. return Runner.Status;
  146. }
  147. /// <summary>
  148. /// 开始计时
  149. /// </summary>
  150. /// <returns></returns>
  151. private bool StartRecordTime()
  152. {
  153. _stopwatch.Restart();
  154. return true;
  155. }
  156. /// <summary>
  157. /// 计算当前电镀步骤的真正用时(减掉了rotation的启动时间)
  158. /// </summary>
  159. /// <returns>返回值单位ms</returns>
  160. /// <param name="recipetime"> 单位ms</param>
  161. private bool CalculateActualDepStepTime()
  162. {
  163. _stopwatch.Stop();
  164. _singalStepDelayTime = (int)_stopwatch.ElapsedMilliseconds;
  165. return true;
  166. }
  167. /// <summary>
  168. /// 判断当前电机是否在转
  169. /// </summary>
  170. /// <returns></returns>
  171. private bool checkRotationIsRunning()
  172. {
  173. return _rotationAxis.Status == RState.Running;
  174. }
  175. /// <summary>
  176. /// 判断电机当前状态是否停止
  177. /// </summary>
  178. /// <returns></returns>
  179. private bool CheckRotationIsIdle()
  180. {
  181. return _rotationAxis.Status == RState.End;
  182. }
  183. /// <summary>
  184. /// 检验Rotation是否运动失败
  185. /// </summary>
  186. /// <returns></returns>
  187. private bool CheckRotationPositionRunStop()
  188. {
  189. return _rotationAxis.Status == RState.Failed || _rotationAxis.Status == RState.Timeout;
  190. }
  191. /// <summary>
  192. /// 更新电镀步骤索引
  193. /// </summary>
  194. /// <returns></returns>
  195. private bool UpdateDepositionIndex()
  196. {
  197. if (_depositionStepIndex < _recipe.DepStepCount - 1)
  198. {
  199. _depositionStepIndex++;
  200. }
  201. return true;
  202. }
  203. /// <summary>
  204. /// 启动PowerSupplier
  205. /// </summary>
  206. /// <returns></returns>
  207. private bool StartPowerStep()
  208. {
  209. bool result = _device.PowerSupplier.StartSetStepPeriodNoWaitEnd(_powerSupplierStepPeriodDatas);
  210. if (!result)
  211. {
  212. _device.PowerSupplier.DisableOperation("", null);
  213. return false;
  214. }
  215. return true;
  216. }
  217. /// <summary>
  218. /// 检验Powerstep是否启动完成
  219. /// </summary>
  220. /// <returns></returns>
  221. private bool CheckRecipeStepEndStatus()
  222. {
  223. if (_isZeroCurrent)
  224. {
  225. return true;
  226. }
  227. return _device.PowerSupplier.Status == RState.End;
  228. }
  229. /// <summary>
  230. /// 检验Powerstep是否启动失败
  231. /// </summary>
  232. /// <returns></returns>
  233. private bool CheckRecipeStepStopStatus()
  234. {
  235. if (_isZeroCurrent)
  236. {
  237. return false;
  238. }
  239. return _device.PowerSupplier.Status == RState.Failed || _device.PowerSupplier.Status == RState.Timeout;
  240. }
  241. /// <summary>
  242. /// rotation开始旋转
  243. /// </summary>
  244. /// <param name="param"></param>
  245. /// <returns></returns>
  246. private bool StartRotation(int targetPosition,int speed)
  247. {
  248. bool result = _rotationAxis.ProfilePosition(targetPosition, speed * SPEED_RATIO * 6, 0, 0); //rpm->deg/s
  249. if (!result)
  250. {
  251. NotifyError(eEvent.ERR_PLATINGCELL, "Start Rotation is failed", 0);
  252. return false;
  253. }
  254. return true;
  255. }
  256. /// <summary>
  257. /// rotation改变速度
  258. /// </summary>
  259. /// <param name="speed"></param>
  260. /// <returns></returns>
  261. private bool ChangeRotationSpeed(int speed)
  262. {
  263. double _scale = _rotationAxis.ScaleFactor;
  264. speed = (int)Math.Round(_scale * BeckhoffVelocityUtil.ConvertVelocityToDegPerSecondByRPM(speed), 0);
  265. return _rotationAxis.ChangeSpeed(speed);
  266. }
  267. /// <summary>
  268. /// vertical 运行
  269. /// </summary>
  270. /// <param name="positionName"></param> 目标位置名称
  271. /// <param name="offset"></param> 偏移量
  272. /// <returns></returns>
  273. private bool StartVertical(string positionName, double offset)
  274. {
  275. return _verticalEntity.CheckToPostMessage<PlatingCellVerticalState, PlatingCellVerticalEntity.VerticalMsg>(eEvent.INFO_PLATINGCELL,
  276. _verticalEntity.Module.ToString(), (int)PlatingCellVerticalEntity.VerticalMsg.Position, positionName, offset);
  277. }
  278. /// <summary>
  279. /// 检验垂直电机是否运动完成
  280. /// </summary>
  281. /// <returns></returns>
  282. private bool CheckVerticalEnd()
  283. {
  284. return _verticalEntity.IsIdle;
  285. }
  286. /// <summary>
  287. /// 检验垂直是否出现错误
  288. /// </summary>
  289. /// <returns></returns>
  290. private bool CheckVerticalError()
  291. {
  292. return _verticalEntity.IsError;
  293. }
  294. /// <summary>
  295. /// 检验电流和电压合理性
  296. /// </summary>
  297. /// <param name="current"></param>
  298. /// <param name="voltage"></param>
  299. /// <param name="side"></param>
  300. /// <returns></returns>
  301. private bool CheckSidePowerInvalid(double current, double voltage)
  302. {
  303. double maxVoltage = _recipe.DepMaxVoltageFault;
  304. double warnVoltage = _recipe.DepMaxVoltageWarning;
  305. if (voltage > maxVoltage)
  306. {
  307. NotifyError(eEvent.ERR_PLATINGCELL, $" voltage {voltage} is large than recipe max voltage {maxVoltage}", 0);
  308. return true;
  309. }
  310. if (voltage > warnVoltage)
  311. {
  312. string str = $" voltage is {voltage} in warning";
  313. if (AlarmListManager.Instance.AddWarn(Module, $"voltage", str))
  314. {
  315. LOG.WriteLog(eEvent.WARN_METAL, Module, str);
  316. }
  317. }
  318. double maxErrorCurrent = _powerSupplierStepPeriodDatas[_depositionStepIndex].Current * (double)(1 + _recipe.DepCurrentFault * 0.01);
  319. double minErrorCurrent = _powerSupplierStepPeriodDatas[_depositionStepIndex].Current * (double)(1 - _recipe.DepCurrentFault * 0.01);
  320. double maxWarnCurrent = _powerSupplierStepPeriodDatas[_depositionStepIndex].Current * (double)(1 + _recipe.DepCurrentWarning * 0.01);
  321. double minWarnCurrent = _powerSupplierStepPeriodDatas[_depositionStepIndex].Current * (double)(1 - _recipe.DepCurrentWarning * 0.01);
  322. if (current > maxErrorCurrent)
  323. {
  324. NotifyError(eEvent.ERR_PLATINGCELL, $" current {current} is large than recipe max current {maxErrorCurrent}", 0);
  325. return true;
  326. }
  327. if (current < minErrorCurrent)
  328. {
  329. NotifyError(eEvent.ERR_PLATINGCELL, $" current {current} is less than recipe min current {minErrorCurrent}", 0);
  330. return true;
  331. }
  332. if ((current <= maxErrorCurrent && current >= maxWarnCurrent) || (current >= minErrorCurrent && current <= minWarnCurrent))
  333. {
  334. string str = $" current {current} is in warning";
  335. if (AlarmListManager.Instance.AddWarn(Module, $" current", str))
  336. {
  337. LOG.WriteLog(eEvent.ERR_PLATINGCELL, Module, str);
  338. }
  339. }
  340. return false;
  341. }
  342. /// <summary>
  343. /// 启动
  344. /// </summary>
  345. /// <param name="objs"></param>
  346. /// <returns></returns>
  347. public RState Start(params object[] objs)
  348. {
  349. _recipe = (DepRecipe)objs[0];
  350. _isZeroCurrent = (bool)objs[1];
  351. _powerSupplierStepPeriodDatas = (List<PowerSupplierStepPeriodData>)objs[2];
  352. _rotationAxis = DEVICE.GetDevice<JetAxisBase>($"{Module}.Rotation");
  353. _device = DEVICE.GetDevice<PlatingCellDevice>(Module);
  354. _depositionStepIndex = 0;
  355. //获取vertical entity
  356. string vertical = ModuleMatcherManager.Instance.GetPlatingVerticalByCell(Module);
  357. _verticalEntity = Singleton<RouteManager>.Instance.GetModule<PlatingCellVerticalEntity>(vertical);
  358. return Runner.Start(Module, "start Deposition routine");
  359. }
  360. }
  361. }