GalilRotationAxis.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. using Aitex.Core.RT.DataCenter;
  2. using Aitex.Core.RT.Event;
  3. using Aitex.Core.RT.Fsm;
  4. using Aitex.Core.RT.Log;
  5. using Aitex.Core.RT.Routine;
  6. using Aitex.Core.RT.SCCore;
  7. using Aitex.Core.Util;
  8. using MECF.Framework.Common.CommonData.PUF;
  9. using MECF.Framework.Common.TwinCat;
  10. using CyberX8_Core;
  11. using CyberX8_RT.Modules;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Data;
  15. using System.Linq;
  16. using System.Reflection;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using CyberX8_RT.Devices.AXIS.Galil;
  20. using MECF.Framework.Common.Device.Galil;
  21. using MECF.Framework.Common.Utilities;
  22. namespace CyberX8_RT.Devices.AXIS.GalilLipsel
  23. {
  24. public class GalilRotationAxis : JetAxisBase
  25. {
  26. #region 内部变量
  27. /// <summary>
  28. /// Home routine
  29. /// </summary>
  30. private GalilRotationHomeRoutine _homeRoutine;
  31. /// <summary>
  32. /// 运动Routine
  33. /// </summary>
  34. private GalilRotationProfilePositionRoutine _profilePositionRoutine;
  35. /// <summary>
  36. /// Switch On Routine
  37. /// </summary>
  38. private GalilRotationSwitchOnRoutine _switchOnRoutine;
  39. /// <summary>
  40. /// Switch Off Routine
  41. /// </summary>
  42. private GalilRotationSwitchOffRoutine _switchOffRoutine;
  43. /// <summary>
  44. /// Stop Position
  45. /// </summary>
  46. private GalilRotationStopPositionRoutine _stopPositionRoutine;
  47. #endregion
  48. /// <summary>
  49. /// 构造函数
  50. /// </summary>
  51. /// <param name="Module"></param>
  52. public GalilRotationAxis(string Module, string name) : base(Module, name)
  53. {
  54. //deg->RPM
  55. _commandMotionData.FileProfileVelocity /= 6;
  56. _commandMotionData.FileHomingVelocity /= 6;
  57. }
  58. /// <summary>
  59. /// 初始化参数
  60. /// </summary>
  61. protected override void InitializeParameter()
  62. {
  63. _accelerationRatio = 1;
  64. _speedRatio = 6;
  65. }
  66. /// <summary>
  67. /// 初始化Routine
  68. /// </summary>
  69. protected override void InitializeRoutine()
  70. {
  71. _homeRoutine = new GalilRotationHomeRoutine($"{Module}.{Name}", this);
  72. _profilePositionRoutine = new GalilRotationProfilePositionRoutine($"{Module}.{Name}", this);
  73. _switchOnRoutine = new GalilRotationSwitchOnRoutine($"{Module}.{Name}", this);
  74. _switchOffRoutine = new GalilRotationSwitchOffRoutine($"{Module}.{Name}", this);
  75. _stopPositionRoutine = new GalilRotationStopPositionRoutine($"{Module}.{Name}", this);
  76. }
  77. /// <summary>
  78. /// 中止操作
  79. /// </summary>
  80. /// <param name="cmd"></param>
  81. /// <param name="args"></param>
  82. /// <returns></returns>
  83. public override bool StopPositionOperation()
  84. {
  85. if (!IsRun)
  86. {
  87. return true;
  88. }
  89. if (_profilePositionRoutine.Monitor() == RState.Running)
  90. {
  91. _profilePositionRoutine.Abort();
  92. }
  93. _status = _stopPositionRoutine.Start();
  94. _currentOperation = MotionOperation.StopPosition;
  95. return true;
  96. }
  97. /// <summary>
  98. /// 更新StatusWord
  99. /// </summary>
  100. /// <param name="status"></param>
  101. public override void UpdateStatusWord(ushort status)
  102. {
  103. }
  104. /// <summary>
  105. /// EnableOperation
  106. /// </summary>
  107. public override bool EnableOperation()
  108. {
  109. return WriteControlWord(0x0F);
  110. }
  111. /// <summary>
  112. /// Home
  113. /// </summary>
  114. public override bool Home()
  115. {
  116. bool result = base.Home();
  117. if (!result)
  118. {
  119. return false;
  120. }
  121. MotionData.IsHomed = false;
  122. _homeRoutine.Start(_homeTimeout, _galilAxisConfig.HomingAcceleration,_galilAxisConfig.HomingDeceleration,_galilAxisConfig.HomingSpeed,_galilAxisConfig.HomingOffset);
  123. IsHomeSwitchedTriggered = false;
  124. return true;
  125. }
  126. /// <summary>
  127. /// 停止
  128. /// </summary>
  129. public override void Stop()
  130. {
  131. }
  132. /// <summary>
  133. /// SwitchOff
  134. /// </summary>
  135. public override bool SwitchOff()
  136. {
  137. if (_status == RState.Running)
  138. {
  139. EV.PostAlarmLog($"{Module}.{Name}", eEvent.ERR_AXIS, $"{Module}.{Name} current execute {_currentOperation},cannot switchoff");
  140. return false;
  141. }
  142. _currentOperation = MotionOperation.SwitchOff;
  143. _switchOffRoutine.Start();
  144. _status = RState.Running;
  145. return true;
  146. }
  147. /// <summary>
  148. /// SwitchOn
  149. /// </summary>
  150. public override bool SwitchOn()
  151. {
  152. if (_status == RState.Running)
  153. {
  154. EV.PostAlarmLog($"{Module}.{Name}", eEvent.ERR_AXIS, $"{Module}.{Name} current execute {_currentOperation},cannot SwitchOn");
  155. return false;
  156. }
  157. _currentOperation = MotionOperation.SwitchOn;
  158. _switchOnRoutine.Start();
  159. _status = RState.Running;
  160. return true;
  161. }
  162. /// <summary>
  163. /// 定时器
  164. /// </summary>
  165. /// <returns></returns>
  166. public override bool OnTimer()
  167. {
  168. if (_status == RState.Running)
  169. {
  170. if (_currentOperation == MotionOperation.Position)
  171. {
  172. RState state = _profilePositionRoutine.Monitor();
  173. if (state == RState.End)
  174. {
  175. _inTargetPosition = false;
  176. EndOperation();
  177. _status = RState.End;
  178. LOG.WriteLog(eEvent.INFO_AXIS, $"{Module}.{Name}", $"Position Complete,Current Position {MotionData.MotorPosition}");
  179. }
  180. else if (state == RState.Failed || state == RState.Timeout)
  181. {
  182. _inTargetPosition = false;
  183. EndOperation();
  184. _status = RState.Failed;
  185. LOG.WriteLog(eEvent.ERR_AXIS, $"{Module}.{Name}", $"Profile Position error {_profilePositionRoutine.ErrorMsg}");
  186. }
  187. }
  188. else if (_currentOperation == MotionOperation.Home)
  189. {
  190. RState state = _homeRoutine.Monitor();
  191. if (state == RState.End)
  192. {
  193. MotionData.IsHomed = true;
  194. IsHomed = true;
  195. EndOperation();
  196. _status = RState.End;
  197. }
  198. else if (state == RState.Failed || state == RState.Timeout)
  199. {
  200. EndOperation();
  201. _status = RState.Failed;
  202. LOG.WriteLog(eEvent.ERR_AXIS, $"{Module}.{Name}", "Home error");
  203. }
  204. }
  205. else if (_currentOperation == MotionOperation.SwitchOn)
  206. {
  207. RState state = _switchOnRoutine.Monitor();
  208. if (state == RState.End)
  209. {
  210. EndOperation();
  211. _status = RState.End;
  212. }
  213. else if (state == RState.Failed || state == RState.Timeout)
  214. {
  215. EndOperation();
  216. _status = RState.Failed;
  217. LOG.WriteLog(eEvent.ERR_AXIS, $"{Module}.{Name}", "Switch On error");
  218. }
  219. }
  220. else if (_currentOperation == MotionOperation.SwitchOff)
  221. {
  222. RState state = _switchOffRoutine.Monitor();
  223. if (state == RState.End)
  224. {
  225. EndOperation();
  226. _status = RState.End;
  227. }
  228. else if (state == RState.Failed || state == RState.Timeout)
  229. {
  230. EndOperation();
  231. _status = RState.Failed;
  232. LOG.WriteLog(eEvent.ERR_AXIS, $"{Module}.{Name}", "Switch Off error");
  233. }
  234. }
  235. else if (_currentOperation == MotionOperation.StopPosition)
  236. {
  237. RState state = _stopPositionRoutine.Monitor();
  238. if (state == RState.End)
  239. {
  240. EndOperation();
  241. _status = RState.End;
  242. }
  243. else if (state == RState.Failed || state == RState.Timeout)
  244. {
  245. EndOperation();
  246. _status = RState.Failed;
  247. LOG.WriteLog(eEvent.ERR_AXIS, $"{Module}.{Name}", "StopPosition error");
  248. }
  249. }
  250. }
  251. JudgeRunMonitor();
  252. return true;
  253. }
  254. /// <summary>
  255. /// 位置
  256. /// </summary>
  257. /// <param name="targetPoint"></param>
  258. public override bool ProfilePosition(int targetPoint, int profileVelocity, int profileAcceleration, int profileDeceleration, bool judgeTorqueLimit = true)
  259. {
  260. if (_status == RState.Running)
  261. {
  262. LOG.WriteLog(eEvent.WARN_AXIS, $"{Module}.{Name}", $"current execute {_currentOperation},cannot profile position");
  263. return false;
  264. }
  265. if (profileAcceleration == 0)
  266. {
  267. profileAcceleration = _profileAcceleration;
  268. }
  269. if (profileDeceleration == 0)
  270. {
  271. profileDeceleration = _profileDeceleration;
  272. }
  273. _status = _profilePositionRoutine.Start(targetPoint, profileVelocity, profileAcceleration, profileDeceleration, judgeTorqueLimit);
  274. _currentOperation = MotionOperation.Position;
  275. _inTargetPosition = false;
  276. return true;
  277. }
  278. /// <summary>
  279. /// 改变速度
  280. /// </summary>
  281. /// <param name="speed"></param>
  282. /// <returns></returns>
  283. public override bool ChangeSpeed(int speed)
  284. {
  285. bool result = WriteControlWord(0x2F);
  286. if (!result)
  287. {
  288. return false;
  289. }
  290. result = BeckhoffAxisManager.Instance.WriteVariableValue($"{Module}.{Name}.ProfileVelocity", speed);
  291. if (!result)
  292. {
  293. return false;
  294. }
  295. result = WriteControlWord(0x3F);
  296. return result;
  297. }
  298. /// <summary>
  299. /// 改变速度加速度
  300. /// </summary>
  301. /// <param name="speed"></param>
  302. /// <returns></returns>
  303. public override bool ChangeSpeedAcceleration(int speed, int acceleration, int deceleration)
  304. {
  305. bool result = WriteControlWord(0x2F);
  306. if (!result)
  307. {
  308. return false;
  309. }
  310. result = BeckhoffAxisManager.Instance.WriteVariableValue($"{Module}.{Name}.ProfileVelocity", speed);
  311. BeckhoffAxisManager.Instance.WriteVariableValue($"{Module}.{Name}.{PROFILE_ACCEL}", acceleration);
  312. BeckhoffAxisManager.Instance.WriteVariableValue($"{Module}.{Name}.{PROFILE_DECEL}", deceleration);
  313. if (!result)
  314. {
  315. return false;
  316. }
  317. result = WriteControlWord(0x3F);
  318. return result;
  319. }
  320. /// <summary>
  321. /// KeyDown事件
  322. /// </summary>
  323. /// <param name="arg"></param>
  324. /// <param name="value"></param>
  325. protected override void AxisKeyDown(string arg, double value)
  326. {
  327. switch (arg)
  328. {
  329. case PROFILE_VELOCITY:
  330. _profileVelocity = CalculateMultiplySpeedRatio(CalculateValueMultiplyScale(value));
  331. _commandMotionData.FileProfileVelocity = value;
  332. GalilControllerCfgManager.Instance.SetAxisCommand(Module, Name, "SP", _profileVelocity);
  333. break;
  334. case PROFILE_ACCEL:
  335. _profileAcceleration = CalculateDivideAccelerationRatio(CalculateValueMultiplyScale(value));
  336. _commandMotionData.FileAcceleration = value;
  337. GalilControllerCfgManager.Instance.SetAxisCommand(Module, Name, "AC", _profileAcceleration);
  338. break;
  339. case PROFILE_DECEL:
  340. _profileDeceleration = CalculateDivideAccelerationRatio(CalculateValueMultiplyScale(value));
  341. _commandMotionData.FileDeceleration = value;
  342. GalilControllerCfgManager.Instance.SetAxisCommand(Module, Name, "DC", _profileDeceleration);
  343. break;
  344. case HOMING_VELOCITY:
  345. _profileHomingVelocity = CalculateMultiplySpeedRatio(CalculateValueMultiplyScale(value));
  346. _commandMotionData.FileHomingVelocity = value;
  347. GalilControllerCfgManager.Instance.SetAxisCommand(Module, Name, "SP", _profileHomingVelocity);
  348. break;
  349. case HOMING_VELOCITY_SLOW:
  350. _profileHomingVelocitySlow = CalculateMultiplySpeedRatio(CalculateValueMultiplyScale(value));
  351. _commandMotionData.FileHomingVelocitySlow = value;
  352. TwincatCoeManager.Instance.WriteVariableValue($"{Module}.{Name}", arg, _profileHomingVelocitySlow);
  353. break;
  354. case HOMING_ACCEL:
  355. _profileHomingAccel = CalculateDivideAccelerationRatio(CalculateValueMultiplyScale(value));
  356. _commandMotionData.FileHomingAccel = value;
  357. GalilControllerCfgManager.Instance.SetAxisCommand(Module, Name, "AC", _profileHomingAccel);
  358. break;
  359. }
  360. }
  361. }
  362. }