GalilLipselAxis.cs 14 KB

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