GalilAxis.cs 14 KB

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