JetAxisBase.cs 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  1. using Aitex.Core.RT.DataCenter;
  2. using Aitex.Core.RT.Device;
  3. using Aitex.Core.RT.Event;
  4. using Aitex.Core.RT.Log;
  5. using Aitex.Core.RT.OperationCenter;
  6. using Aitex.Core.RT.Routine;
  7. using Aitex.Core.RT.SCCore;
  8. using Aitex.Core.Util;
  9. using MECF.Framework.Common.Beckhoff.AxisProvider;
  10. using MECF.Framework.Common.Beckhoff.IOAxis;
  11. using MECF.Framework.Common.Beckhoff.Station;
  12. using MECF.Framework.Common.CommonData.PUF;
  13. using MECF.Framework.Common.TwinCat;
  14. using MECF.Framework.Common.Utilities;
  15. using CyberX8_Core;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Reflection;
  20. using System.Text;
  21. using System.Threading;
  22. using System.Threading.Tasks;
  23. using System.Timers;
  24. using CommunityToolkit.HighPerformance.Buffers;
  25. using System.Windows.Documents;
  26. using MECF.Framework.Common.Device.Galil;
  27. namespace CyberX8_RT.Devices.AXIS
  28. {
  29. public abstract class JetAxisBase : BaseDevice, IDevice
  30. {
  31. #region 常量
  32. private const string STATUS_WORD = "StatusWord";
  33. private const string DIGITAL_INPUTS="DigitalInputs";
  34. private const string CONTROL_WORD = "ControlWord";
  35. private const string MODE_OF_OPERATION = "ModeOfOperation";
  36. protected const string PROFILE_VELOCITY = "ProfileVelocity";
  37. protected const string PROFILE_ACCEL = "ProfileAccel";
  38. protected const string PROFILE_DECEL = "ProfileDecel";
  39. private const string TARGET_POSITION = "TargetPosition";
  40. private const string MOTOR_POSITION = "MotorPosition";
  41. private const string ACTUAL_VELOCITY = "Velocity";
  42. private const string ACTUAL_TORQUE = "ActualTorque";
  43. private const string POSITION_ERROR = "PositionError";
  44. private const string MOTION_DATA = "MotionData";
  45. private const string CURRENT_STATION = "CurrentStation";
  46. protected const string CURRENT_STATION_LIST = "CurrentStationList";
  47. private const string IS_SWITCH_ON = "IsSwitchOn";
  48. private const string HOME_OFFSET = "HomeOffset";
  49. private const string HOMING_METHOD = "HomingMethod";
  50. protected const string HOMING_VELOCITY = "HomingVelocity";
  51. protected const string HOMING_VELOCITY_SLOW = "HomingVelocitySlow";
  52. protected const string HOMING_ACCEL = "HomingAccel";
  53. private const string NEGATIVE_TORQUE_LIMIT="NegativeTorqueLimit";
  54. private const string POSITIVE_TORQUE_LIMIT = "PositiveTorqueLimit";
  55. private const string MANUF_STATUS="ManufStatus";
  56. private const string SOFTWARE_LIMIT_MINUS= "SoftwareLimitMinus";
  57. private const string SOFTWARE_LIMIT_PLUS = "SoftwareLimitPlus";
  58. private const string STOP_CODE = "StopCode";
  59. private const string FORWARD_LIMIT = "ForwardLimit";
  60. #endregion
  61. #region 内部变量
  62. /// <summary>
  63. /// 比例因子
  64. /// </summary>
  65. private double _scaleFactor = 0;
  66. /// <summary>
  67. /// jog限制
  68. /// </summary>
  69. private double _jogLimit = 0;
  70. /// <summary>
  71. /// 当前位置数值(用于判定是否正在运动)
  72. /// </summary>
  73. private int _currentLocation = 0;
  74. /// <summary>
  75. /// 是否运动
  76. /// </summary>
  77. private bool _isRun = false;
  78. /// <summary>
  79. /// 当前位置
  80. /// </summary>
  81. private string _currentStation = "";
  82. /// <summary>
  83. /// 当前位置集合(多个位置共用一个数值)
  84. /// </summary>
  85. private List<string> _currentStationList = new List<string>();
  86. /// <summary>
  87. /// 当前位置锁
  88. /// </summary>
  89. private object _locationLocker = new object();
  90. /// <summary>
  91. /// 工位位置对象
  92. /// </summary>
  93. private BeckhoffStationAxis _stationAxis;
  94. /// <summary>
  95. /// 运动时间
  96. /// </summary>
  97. private DateTime _runTime = DateTime.Now;
  98. /// <summary>
  99. /// inter lock
  100. /// </summary>
  101. private IAxisInterLock _interLock;
  102. /// <summary>
  103. /// 是否存在Rev Sensor Limit
  104. /// </summary>
  105. private bool _isRevSensorLimit = false;
  106. /// <summary>
  107. /// 是否存在Forward Limit
  108. /// </summary>
  109. private bool _isForwardSensorLimit = false;
  110. /// <summary>
  111. /// Home Switched是否触发
  112. /// </summary>
  113. private bool _isHomeSwitchedTrigger = false;
  114. /// <summary>
  115. /// 变量是否初始化字典
  116. /// </summary>
  117. private Dictionary<string, bool> _variableInitializeDic = new Dictionary<string, bool>();
  118. #endregion
  119. #region protected 字段
  120. /// <summary>
  121. /// 轴参数对象
  122. /// </summary>
  123. protected GalilAxisConfig _galilAxisConfig = null;
  124. /// <summary>
  125. /// 状态
  126. /// </summary>
  127. protected RState _status;
  128. /// <summary>
  129. /// 当前操作
  130. /// </summary>
  131. protected MotionOperation _currentOperation = MotionOperation.None;
  132. /// <summary>
  133. /// 模式
  134. /// </summary>
  135. protected byte _modeOfOperation;
  136. /// <summary>
  137. /// 状态字
  138. /// </summary>
  139. protected ushort _statusWord;
  140. /// <summary>
  141. /// 控制字
  142. /// </summary>
  143. protected ushort _controlWord;
  144. /// <summary>
  145. /// 运动数据对象
  146. /// </summary>
  147. protected CommandMotionData _commandMotionData = new CommandMotionData();
  148. /// <summary>
  149. /// Home状态
  150. /// </summary>
  151. protected bool _isHomed;
  152. /// <summary>
  153. /// SwitchOn状态
  154. /// </summary>
  155. protected bool _isSwitchOn;
  156. /// <summary>
  157. /// 是否错误
  158. /// </summary>
  159. protected bool _isError;
  160. /// <summary>
  161. /// 是否到达目标位置
  162. /// </summary>
  163. protected bool _inTargetPosition = false;
  164. /// <summary>
  165. /// 初始化的速度
  166. /// </summary>
  167. protected int _initialVelocity = 0;
  168. /// <summary>
  169. /// 初始化的加速度
  170. /// </summary>
  171. protected int _initialAcceleration = 0;
  172. /// <summary>
  173. /// 初始化的减速度
  174. /// </summary>
  175. protected int _initialDeceleration = 0;
  176. /// <summary>
  177. /// 运动速度
  178. /// </summary>
  179. protected int _profileVelocity = 0;
  180. /// <summary>
  181. /// 运动加速度
  182. /// </summary>
  183. protected int _profileAcceleration = 0;
  184. /// <summary>
  185. /// 运动减速度
  186. /// </summary>
  187. protected int _profileDeceleration = 0;
  188. /// <summary>
  189. /// 负向Torque限制
  190. /// </summary>
  191. protected int _profileNegativeTorqueLimit = 0;
  192. /// <summary>
  193. /// 正向Torque限制
  194. /// </summary>
  195. protected int _profilePositiveTorqueLimit = 0;
  196. /// <summary>
  197. /// Homing 速度
  198. /// </summary>
  199. protected int _profileHomingVelocity = 0;
  200. /// <summary>
  201. /// Homing 速度Slow
  202. /// </summary>
  203. protected int _profileHomingVelocitySlow = 0;
  204. /// <summary>
  205. /// Homing加速度
  206. /// </summary>
  207. protected int _profileHomingAccel = 0;
  208. /// <summary>
  209. /// <summary>
  210. /// 目标位置
  211. /// </summary>
  212. protected double _targetPosition = 0.0;
  213. /// <summary>
  214. /// home超时时长
  215. /// </summary>
  216. protected int _homeTimeout = 5000;
  217. /// <summary>
  218. /// coe输出变量集合
  219. /// </summary>
  220. protected List<string> _coeOutputs = new List<string>();
  221. /// <summary>
  222. /// 速度比例
  223. /// </summary>
  224. protected int _speedRatio = 1;
  225. /// <summary>
  226. /// 加速度的比例
  227. /// </summary>
  228. protected int _accelerationRatio = 1;
  229. /// <summary>
  230. /// torque比例
  231. /// </summary>
  232. protected int _torqueRatio = 1000;
  233. /// <summary>
  234. /// Motion Position比例
  235. /// </summary>
  236. protected double _motionPositionRation = 1;
  237. #endregion
  238. #region 属性
  239. /// <summary>
  240. /// 状态
  241. /// </summary>
  242. public RState Status { get { return _status; } }
  243. /// <summary>
  244. /// Home状态
  245. /// </summary>
  246. public bool IsHomed { get { return _isHomed; } set { _isHomed = value; } }
  247. /// <summary>
  248. /// SwitchOn状态
  249. /// </summary>
  250. public bool IsSwitchOn { get { return _isSwitchOn; } }
  251. /// <summary>
  252. /// 模式
  253. /// </summary>
  254. public byte ModeOfOperation { get { return _modeOfOperation; } }
  255. /// <summary>
  256. /// 控制字
  257. /// </summary>
  258. public ushort ControlWord { get { return _controlWord; } }
  259. /// <summary>
  260. /// 是否运动中
  261. /// </summary>
  262. public bool IsRun { get { return _isRun; } }
  263. /// <summary>
  264. /// 当前位置
  265. /// </summary>
  266. public string CurrentStation { get { return _currentStation; } }
  267. /// <summary>
  268. /// 是否到达目标位置
  269. /// </summary>
  270. public bool InTargetPosition { get { return _inTargetPosition; } }
  271. /// <summary>
  272. /// 运动数据对象
  273. /// </summary>
  274. public CommandMotionData MotionData { get { return _commandMotionData; } }
  275. /// <summary>
  276. /// 负向Torque限制数值
  277. /// </summary>
  278. public int NegativeTorqueLimit { get { return _profileNegativeTorqueLimit; } }
  279. /// <summary>
  280. /// 正向Torque限制数值
  281. /// </summary>
  282. public int PositiveTorqueLimit { get { return _profilePositiveTorqueLimit; } }
  283. /// <summary>
  284. /// 是否错误
  285. /// </summary>
  286. public bool IsError { get { return _isError; } }
  287. /// <summary>
  288. /// 目标位置
  289. /// </summary>
  290. public double TargetPosition { get { return _targetPosition; } }
  291. /// <summary>
  292. /// 加速度
  293. /// </summary>
  294. public double ProfileAcceleration { get { return _profileAcceleration; } }
  295. /// <summary>
  296. /// 减速度
  297. /// </summary>
  298. public double ProfileDeceleration { get { return _profileDeceleration; } }
  299. /// <summary>
  300. /// inter lock接口对象
  301. /// </summary>
  302. public IAxisInterLock InterLock { set { _interLock= value; } }
  303. /// <summary>
  304. /// 是否存在Sensor Limit
  305. /// </summary>
  306. public bool IsRevSensorLimit { set { _isRevSensorLimit = value; } }
  307. /// <summary>
  308. /// 是否存在正向Sensor Limit
  309. /// </summary>
  310. public bool IsForwardSensorLimit { set { _isForwardSensorLimit = value; } }
  311. /// <summary>
  312. /// Home Switch是否触发
  313. /// </summary>
  314. public bool IsHomeSwitchedTriggered { get { return _isHomeSwitchedTrigger; } set { _isHomeSwitchedTrigger = value; } }
  315. /// <summary>
  316. /// 所有io变量是否初始化
  317. /// </summary>
  318. public bool IOInitialized { get { return AllIoVariableInitialized(); } }
  319. #endregion
  320. /// <summary>
  321. /// 构造函数
  322. /// </summary>
  323. /// <param name="moduleName"></param>
  324. /// <param name="name"></param>
  325. public JetAxisBase(string moduleName,string name) : base(moduleName, name,name,name)
  326. {
  327. InitializeParameter();
  328. LoadStation();
  329. InitializeRoutine();
  330. SubscribeData();
  331. InitializeOperation();
  332. }
  333. #region private方法
  334. /// <summary>
  335. /// 加载Station位置
  336. /// </summary>
  337. private void LoadStation()
  338. {
  339. _stationAxis = BeckhoffStationLocationManager.Instance.GetStationAxis(Module, Name);
  340. }
  341. /// <summary>
  342. /// 订阅数据
  343. /// </summary>
  344. private void SubscribeData()
  345. {
  346. BeckhoffProviderAxis beckhoffProviderAxis = BeckhoffAxisProviderManager.Instance.GetAxisProvider($"{Module}.{Name}");
  347. if (beckhoffProviderAxis != null)
  348. {
  349. _scaleFactor = beckhoffProviderAxis.ScaleFactor;
  350. _jogLimit = beckhoffProviderAxis.JogLimit;
  351. _motionPositionRation = beckhoffProviderAxis.MotorPositionRate;
  352. }
  353. _galilAxisConfig = GalilControllerCfgManager.Instance.GetGalilAxisConfig(Module.ToString(),Name);
  354. if (_galilAxisConfig != null)
  355. {
  356. _profileVelocity =CalculateMultiplySpeedRatio(_galilAxisConfig.Speed);
  357. _initialVelocity = _profileVelocity;
  358. _profileAcceleration = CalculateDivideAccelerationRatio(_galilAxisConfig.Acceleration);
  359. _initialAcceleration = _profileAcceleration;
  360. _profileDeceleration = CalculateDivideAccelerationRatio(_galilAxisConfig.Deceleration);
  361. _initialDeceleration = _profileDeceleration;
  362. _profileNegativeTorqueLimit = _galilAxisConfig.NegativeTorqueLimit;
  363. _profilePositiveTorqueLimit = _galilAxisConfig.PositiveTorqueLimit;
  364. _commandMotionData.FileAcceleration = CalculateValueAfterScale(_galilAxisConfig.Acceleration);
  365. _commandMotionData.FileDeceleration = CalculateValueAfterScale(_galilAxisConfig.Deceleration);
  366. _commandMotionData.HomeOffset = CalculateValueAfterScale(_galilAxisConfig.HomingOffset);
  367. _commandMotionData.FileHomingAccel = CalculateValueAfterScale(_galilAxisConfig.HomingAcceleration);
  368. _commandMotionData.FileHomingVelocitySlow = CalculateValueAfterScale(_galilAxisConfig.HomingSpeed);
  369. _commandMotionData.FileHomingVelocity = CalculateValueAfterScale(_galilAxisConfig.HomingSpeed);
  370. _commandMotionData.FileProfileVelocity = CalculateValueAfterScale(_galilAxisConfig.Speed);
  371. _commandMotionData.FwdSoftLimit = CalculateValueAfterScale(_galilAxisConfig.ForwardSoftwareLimit)-_commandMotionData.HomeOffset;
  372. _commandMotionData.RevSoftLimit = CalculateValueAfterScale(_galilAxisConfig.ReverseSoftwareLimit)-_commandMotionData.HomeOffset;
  373. _commandMotionData.NegativeTorqueLimit = _galilAxisConfig.NegativeTorqueLimit;
  374. _commandMotionData.PositiveTorqueLimit = _galilAxisConfig.PositiveTorqueLimit;
  375. if (_galilAxisConfig.NegativeTorqueLimit != 0 || _galilAxisConfig.PositiveTorqueLimit != 0)
  376. {
  377. _commandMotionData.TorqueLimit = $"-{_galilAxisConfig.NegativeTorqueLimit}/+{_galilAxisConfig.PositiveTorqueLimit}";
  378. }
  379. if(_galilAxisConfig.HomingTimeOut!=0)
  380. {
  381. _homeTimeout = _galilAxisConfig.HomingTimeOut;
  382. }
  383. }
  384. DATA.Subscribe($"{Module}.{Name}.{MOTION_DATA}", () => _commandMotionData,SubscriptionAttribute.FLAG.IgnoreSaveDB);
  385. DATA.Subscribe($"{Module}.{Name}.{MOTOR_POSITION}", () => _commandMotionData.MotorPosition,SubscriptionAttribute.FLAG.IgnoreSaveDB);
  386. DATA.Subscribe($"{Module}.{Name}.ProfileVelocity", () => _commandMotionData.ProfileVelocity, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  387. DATA.Subscribe($"{Module}.{Name}.Acceleration",()=>_commandMotionData.FileAcceleration,SubscriptionAttribute.FLAG.IgnoreSaveDB);
  388. DATA.Subscribe($"{Module}.{Name}.Deceleration",()=>_commandMotionData.FileDeceleration,SubscriptionAttribute.FLAG.IgnoreSaveDB);
  389. DATA.Subscribe($"{Module}.{Name}.ActualTorque",()=>_commandMotionData.ActualTorque,SubscriptionAttribute.FLAG.IgnoreSaveDB);
  390. DATA.Subscribe($"{Module}.{Name}.{CURRENT_STATION}", () => _currentStation,SubscriptionAttribute.FLAG.IgnoreSaveDB);
  391. DATA.Subscribe($"{Module}.{Name}.{CURRENT_STATION_LIST}", () => _currentStationList, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  392. DATA.Subscribe($"{Module}.{Name}.{IS_SWITCH_ON}", () => IsSwitchOn, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  393. DATA.Subscribe($"{Module}.{Name}.IsHomed", () => IsHomed, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  394. DATA.Subscribe($"{Module}.{Name}.IsError",()=>IsError, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  395. DATA.Subscribe($"{Module}.{Name}.IsMoving", () => _isRun, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  396. DATA.Subscribe($"{Module}.{Name}.IsRun", () => _isRun, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  397. DATA.Subscribe($"{Module}.{Name}.{FORWARD_LIMIT}", () => _commandMotionData.FwdLimited, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  398. }
  399. /// <summary>
  400. /// 初始化操作
  401. /// </summary>
  402. private void InitializeOperation()
  403. {
  404. OP.Subscribe($"{Module}.{Name}.{MotionOperation.SwitchOn}", (cmd, args) => { SwitchOn(); return true; });
  405. OP.Subscribe($"{Module}.{Name}.{MotionOperation.SwitchOff}", (cmd, args) => { SwitchOff(); return true; });
  406. OP.Subscribe($"{Module}.{Name}.{MotionOperation.Home}", (cmd, args) => { Home(); return true; });
  407. OP.Subscribe($"{Module}.{Name}.JogUp", JogUpPosition);
  408. OP.Subscribe($"{Module}.{Name}.JogDown", JogDownPosition);
  409. OP.Subscribe($"{Module}.{Name}.{MotionOperation.KeyDown}", KeyDownOperation);
  410. OP.Subscribe($"{Module}.{Name}.{MotionOperation.Stop}",(cmd,args)=> { return StopPositionOperation(); });
  411. OP.Subscribe($"{Module}.{Name}.{MotionOperation.Save}", SaveOperation);
  412. OP.Subscribe($"{Module}.{Name}.GotoSavedPosition", (cmd, args) => {
  413. return PositionStation(args[1].ToString()); });
  414. }
  415. /// <summary>
  416. /// 保存操作
  417. /// </summary>
  418. /// <param name="cmd"></param>
  419. /// <param name="args"></param>
  420. /// <returns></returns>
  421. public bool SaveOperation(string cmd, object[] args)
  422. {
  423. if (args.Length >= 2)
  424. {
  425. string key = args[0].ToString();
  426. double paramValue = double.Parse(args[1].ToString());
  427. BeckhoffStationLocationManager.Instance.SaveMotionPosition(key, paramValue);
  428. lock (_locationLocker)
  429. {
  430. List<string> lst = new List<string>();
  431. foreach (Station item in _stationAxis.Stations)
  432. {
  433. if (item.Name == key)
  434. {
  435. item.Position = paramValue.ToString();
  436. _currentStation = item.Name;
  437. if (!lst.Contains(item.Name))
  438. {
  439. lst.Add(item.Name);
  440. }
  441. }
  442. }
  443. _currentStationList.Clear();
  444. if (lst.Count != 0)
  445. {
  446. _currentStationList.AddRange(lst);
  447. }
  448. }
  449. LOG.WriteLog(eEvent.INFO_AXIS, $"{Module}.{Name}", "save success");
  450. }
  451. return true;
  452. }
  453. /// <summary>
  454. /// 计算所处当前工位
  455. /// </summary>
  456. private void CalculateCurrentStation(double motor)
  457. {
  458. lock (_locationLocker)
  459. {
  460. List<string> tmp = _currentStationList.ToList();
  461. List<string> lst = new List<string>();
  462. foreach (Station station in _stationAxis.Stations)
  463. {
  464. if (double.TryParse(station.Position, out double value))
  465. {
  466. if (Math.Round(Math.Abs(motor - value),2) <= _stationAxis.ToleranceDefault)
  467. {
  468. _currentStation = station.Name;
  469. if (!lst.Contains(station.Name))
  470. {
  471. lst.Add(station.Name);
  472. }
  473. }
  474. }
  475. }
  476. _currentStationList.Clear();
  477. if (lst.Count != 0)
  478. {
  479. string str = string.Join(",", tmp);
  480. _currentStationList.AddRange(lst);
  481. string strLst = string.Join(",", _currentStationList);
  482. if (str != strLst)
  483. {
  484. LOG.WriteLog(eEvent.INFO_AXIS, $"{Module}.{Name}", $"position {motor} current {strLst}");
  485. }
  486. }
  487. else
  488. {
  489. _currentStation = "";
  490. }
  491. if (tmp.Count != 0&&_currentStationList.Count==0)
  492. {
  493. LOG.WriteLog(eEvent.INFO_AXIS, $"{Module}.{Name}", $"position {motor} current is empty");
  494. }
  495. }
  496. }
  497. /// <summary>
  498. /// 文本框回车操作
  499. /// </summary>
  500. /// <param name="cmd"></param>
  501. /// <param name="args"></param>
  502. /// <returns></returns>
  503. private bool KeyDownOperation(string cmd, object[] args)
  504. {
  505. if (args.Length < 2)
  506. {
  507. return false;
  508. }
  509. if (double.TryParse(args[1].ToString(), out double value))
  510. {
  511. AxisKeyDown(args[0].ToString(), value);
  512. }
  513. else
  514. {
  515. EV.PostWarningLog($"{Module}.{Name}", eEvent.ERR_AXIS, $"{args[0]} value {args[1]}is not int value");
  516. }
  517. return true;
  518. }
  519. /// <summary>
  520. /// 是否所有IO变量初始化完成
  521. /// </summary>
  522. /// <returns></returns>
  523. private bool AllIoVariableInitialized()
  524. {
  525. foreach (string item in _variableInitializeDic.Keys)
  526. {
  527. if (!_variableInitializeDic[item])
  528. {
  529. LOG.WriteLog(eEvent.ERR_DRYER, Module, $"{item} is not initialized");
  530. return false;
  531. }
  532. }
  533. return true;
  534. }
  535. /// <summary>
  536. /// 更新运动数据
  537. /// </summary>
  538. /// <param name="variable"></param>
  539. /// <param name="value"></param>
  540. private void UpdateMotionData(string variable, object value)
  541. {
  542. if(!MotionData.IsDataInitialized)
  543. {
  544. MotionData.IsDataInitialized = true;
  545. }
  546. PropertyInfo property = MotionData.GetType().GetProperty(variable);
  547. if (property != null)
  548. {
  549. if (JudgeIsScale(variable))
  550. {
  551. if (int.TryParse(value.ToString(), out int intValue))
  552. {
  553. if (JudgeSpeedRatio(variable))
  554. {
  555. property.SetValue(MotionData, CalculateValueAfterScale(CalculateDivideSpeedRatio(intValue)));
  556. }
  557. else if(JudgeAccelerationRation(variable))
  558. {
  559. property.SetValue(MotionData, CalculateValueAfterScale(CalculateMultiplyAccelerationRatio(intValue)));
  560. }
  561. else
  562. {
  563. if (variable == MOTOR_POSITION)
  564. {
  565. intValue = (int)Math.Round(intValue * _motionPositionRation,0);
  566. }
  567. property.SetValue(MotionData, CalculateValueAfterScale(intValue));
  568. }
  569. }
  570. else
  571. {
  572. property.SetValue(MotionData, value);
  573. }
  574. }
  575. else if(variable==ACTUAL_TORQUE)
  576. {
  577. if (short.TryParse(value.ToString(), out short shortValue))
  578. {
  579. property.SetValue(MotionData, CalculateDivideTorqueRatio(shortValue));
  580. }
  581. else
  582. {
  583. property.SetValue(MotionData, value);
  584. }
  585. }
  586. else
  587. {
  588. property.SetValue(MotionData, value);
  589. }
  590. }
  591. if (variable == IS_SWITCH_ON)
  592. {
  593. _isSwitchOn = MotionData.IsSwitchOn;
  594. }
  595. }
  596. /// <summary>
  597. /// 判定是否需要比例计算
  598. /// </summary>
  599. /// <param name="variable"></param>
  600. /// <returns></returns>
  601. private bool JudgeIsScale(string variable)
  602. {
  603. switch (variable)
  604. {
  605. case PROFILE_VELOCITY:
  606. case PROFILE_ACCEL:
  607. case PROFILE_DECEL:
  608. case MOTOR_POSITION:
  609. case POSITION_ERROR:
  610. case ACTUAL_VELOCITY:
  611. case TARGET_POSITION:
  612. case HOMING_ACCEL:
  613. case HOMING_VELOCITY:
  614. case HOMING_VELOCITY_SLOW:
  615. case HOME_OFFSET:
  616. return true;
  617. default:
  618. return false;
  619. }
  620. }
  621. /// <summary>
  622. /// 判定是否需要速度调整比例
  623. /// </summary>
  624. /// <param name="variable"></param>
  625. /// <returns></returns>
  626. private bool JudgeSpeedRatio(string variable)
  627. {
  628. switch (variable)
  629. {
  630. case PROFILE_VELOCITY:
  631. case ACTUAL_VELOCITY:
  632. case HOMING_VELOCITY:
  633. case HOMING_VELOCITY_SLOW:
  634. case HOME_OFFSET:
  635. return true;
  636. default:
  637. return false;
  638. }
  639. }
  640. /// <summary>
  641. /// 是否是加速度调整比例
  642. /// </summary>
  643. /// <param name="variable"></param>
  644. /// <returns></returns>
  645. protected bool JudgeAccelerationRation(string variable)
  646. {
  647. switch(variable)
  648. {
  649. case PROFILE_ACCEL:
  650. case PROFILE_DECEL:
  651. case HOMING_ACCEL:
  652. return true;
  653. default:
  654. return false;
  655. }
  656. }
  657. /// <summary>
  658. /// motor position发生变化
  659. /// </summary>
  660. /// <param name="location"></param>
  661. private void MotionPositionChanged(int location)
  662. {
  663. if(Math.Round(Math.Abs(location - _currentLocation)/_scaleFactor, 2)>0)
  664. //if (Math.Abs(location - _currentLocation)/_scaleFactor >= _stationAxis.ToleranceDefault)
  665. {
  666. _currentLocation = location;
  667. _runTime = DateTime.Now;
  668. _isRun = true;
  669. }
  670. }
  671. /// <summary>
  672. /// 更新Digital Inputs
  673. /// </summary>
  674. /// <param name="digitalInputs"></param>
  675. private void UpdateDigitalInputs(uint digitalInputs)
  676. {
  677. if (_isRevSensorLimit)
  678. {
  679. MotionData.RevLimited = !(((digitalInputs>>18) & 0x01) == 0x01);
  680. }
  681. if(_isForwardSensorLimit)
  682. {
  683. MotionData.FwdLimited = !(((digitalInputs >> 17) & 0x01) == 0x01);
  684. }
  685. MotionData.HomedSwitched =((digitalInputs>>22)&0x01) == 0x01;
  686. if(!_isHomeSwitchedTrigger&&MotionData.HomedSwitched)
  687. {
  688. _isHomeSwitchedTrigger = true;
  689. }
  690. }
  691. /// <summary>
  692. /// 更新ManufactureStatus
  693. /// </summary>
  694. /// <param name="manufactureStatus"></param>
  695. private void UpdateManufactureStatus(uint manufactureStatus)
  696. {
  697. MotionData.HomedSwitched = ((manufactureStatus >> 26)&0x01) == 0x01;
  698. if (!_isHomeSwitchedTrigger && MotionData.HomedSwitched)
  699. {
  700. _isHomeSwitchedTrigger = true;
  701. }
  702. }
  703. /// <summary>
  704. /// 更新Torque Limited状态
  705. /// </summary>
  706. private void UpdateTorqueLimited()
  707. {
  708. if(MotionData.NegativeTorqueLimit!=0||MotionData.PositiveTorqueLimit!=0)
  709. {
  710. MotionData.TorqueLimited = (MotionData.ActualTorque >= -MotionData.NegativeTorqueLimit) && (MotionData.ActualTorque <= MotionData.PositiveTorqueLimit) ;
  711. }
  712. else
  713. {
  714. MotionData.TorqueLimited = true;
  715. }
  716. }
  717. /// <summary>
  718. /// 根据位置获取相应的Position数值
  719. /// </summary>
  720. /// <param name="station"></param>
  721. /// <returns></returns>
  722. public (bool success,double position) GetPositionByStation(string station)
  723. {
  724. foreach(Station item in _stationAxis.Stations)
  725. {
  726. if(item.Name.ToLower().EndsWith(station.ToLower()))
  727. {
  728. if(double.TryParse(item.Position,out double position))
  729. {
  730. return (true, position);
  731. }
  732. else
  733. {
  734. return (false, 0);
  735. }
  736. }
  737. }
  738. return (false, 0);
  739. }
  740. #endregion
  741. #region protected 子类使用共用类,子类不再扩展
  742. /// <summary>
  743. /// 计算比例后的数值
  744. /// </summary>
  745. /// <param name="value"></param>
  746. /// <returns></returns>
  747. protected double CalculateValueAfterScale(int value)
  748. {
  749. if (_scaleFactor != 0)
  750. {
  751. return Math.Round((double)value / _scaleFactor, 2);
  752. }
  753. else
  754. {
  755. return (double)value;
  756. }
  757. }
  758. /// <summary>
  759. /// 确认操作状态
  760. /// </summary>
  761. /// <param name="operation"></param>
  762. protected void ConfirmOperationState(MotionOperation operation)
  763. {
  764. if (_currentOperation == operation)
  765. {
  766. _status = RState.End;
  767. EndOperation();
  768. }
  769. }
  770. /// <summary>
  771. /// 结束操作
  772. /// </summary>
  773. protected void EndOperation()
  774. {
  775. LOG.WriteLog(eEvent.INFO_AXIS,$"{Module}.{Name}", $"{Module}.{Name} execute {_currentOperation} complete");
  776. _currentOperation = MotionOperation.None;
  777. _targetPosition = 0;
  778. }
  779. /// <summary>
  780. /// 订阅变量数值发生变化
  781. /// </summary>
  782. protected void SubscribeValueAction()
  783. {
  784. GalilAxisSubscribeUpdateVariable(IS_SWITCH_ON);
  785. GalilAxisSubscribeUpdateVariable(STOP_CODE);
  786. GalilAxisSubscribeUpdateVariable(MOTOR_POSITION);
  787. GalilAxisSubscribeUpdateVariable(POSITION_ERROR);
  788. GalilAxisSubscribeUpdateVariable(ACTUAL_TORQUE);
  789. GalilAxisSubscribeUpdateVariable(ACTUAL_VELOCITY);
  790. GalilAxisSubscribeUpdateVariable(FORWARD_LIMIT);
  791. }
  792. /// <summary>
  793. /// 订阅IO变量
  794. /// </summary>
  795. /// <param name="variable"></param>
  796. private void GalilAxisSubscribeUpdateVariable(string variable)
  797. {
  798. _variableInitializeDic[variable] = false;
  799. GalilAxisManager.Instance.SubscribeModuleVariable($"{Module}.{Name}", variable, UpdateVariableValue);
  800. }
  801. /// <summary>
  802. /// 更新变量数值
  803. /// </summary>
  804. /// <param name="variable"></param>
  805. /// <param name="value"></param>
  806. protected void UpdateVariableValue(string variable, object value)
  807. {
  808. if (value == null)
  809. {
  810. return;
  811. }
  812. if (_variableInitializeDic.ContainsKey(variable) && !_variableInitializeDic[variable])
  813. {
  814. _variableInitializeDic[variable] = true;
  815. }
  816. if (variable == MOTOR_POSITION)
  817. {
  818. if (int.TryParse(value.ToString(), out int location))
  819. {
  820. MotionPositionChanged(location);
  821. if (_galilAxisConfig.ForwardSoftwareLimit != 0)
  822. {
  823. MotionData.ForwardSoftwareLimited = location > _galilAxisConfig.ForwardSoftwareLimit-_galilAxisConfig.HomingOffset;
  824. if (!_isForwardSensorLimit && !_isRevSensorLimit)
  825. {
  826. MotionData.FwdLimited = MotionData.ForwardSoftwareLimited;
  827. }
  828. }
  829. if (_galilAxisConfig.ReverseSoftwareLimit != 0)
  830. {
  831. MotionData.ReverseSoftwareLimited = location < _galilAxisConfig.ReverseSoftwareLimit-_galilAxisConfig.HomingOffset;
  832. if (!_isRevSensorLimit && !_isForwardSensorLimit)
  833. {
  834. MotionData.RevLimited = MotionData.ReverseSoftwareLimited;
  835. }
  836. }
  837. }
  838. }
  839. UpdateMotionData(variable, value);
  840. if (variable == MOTOR_POSITION)
  841. {
  842. CalculateCurrentStation(MotionData.MotorPosition);
  843. }
  844. if (variable == ACTUAL_TORQUE)
  845. {
  846. UpdateTorqueLimited();
  847. }
  848. else if (variable == POSITIVE_TORQUE_LIMIT || variable == NEGATIVE_TORQUE_LIMIT)
  849. {
  850. MotionData.TorqueLimit = $"-{MotionData.NegativeTorqueLimit}/+{MotionData.PositiveTorqueLimit}";
  851. }
  852. if(variable == FORWARD_LIMIT)
  853. {
  854. MotionData.FwdLimited = (bool)value;
  855. }
  856. if(variable == ACTUAL_VELOCITY)
  857. {
  858. MotionData.ActualVelocity = (int)value;
  859. }
  860. }
  861. /// <summary>
  862. /// 监控(用于判定是否停止运动)
  863. /// </summary>
  864. protected void JudgeRunMonitor()
  865. {
  866. if (_isRun && DateTime.Now.Subtract(_runTime).TotalMilliseconds >= 500)
  867. {
  868. _isRun = false;
  869. }
  870. }
  871. #endregion
  872. #region public 公开方法
  873. /// <summary>
  874. /// 初始化
  875. /// </summary>
  876. /// <returns></returns>
  877. public bool Initialize()
  878. {
  879. SubscribeValueAction();
  880. return true;
  881. }
  882. /// <summary>
  883. /// 当前位置是否离目标位置不远
  884. /// </summary>
  885. /// <param name="targetPosition"></param>
  886. /// <returns></returns>
  887. public bool JudgeCurrentPositionIsInTargetPosition(int targetPosition)
  888. {
  889. double scaledTargetPosition = targetPosition /_scaleFactor;
  890. double currentMotionPosition = MotionData.MotorPosition;
  891. double delta = Math.Round(Math.Abs(currentMotionPosition - scaledTargetPosition), 2);
  892. bool result = delta <= _stationAxis.ToleranceDefault;
  893. return result;
  894. }
  895. /// <summary>
  896. /// 计算乘以比例后的数值
  897. /// </summary>
  898. /// <param name="value"></param>
  899. /// <returns></returns>
  900. public int CalculateValueMultiplyScale(double value)
  901. {
  902. if (_scaleFactor != 0)
  903. {
  904. return (int)Math.Round(value * _scaleFactor, 0);
  905. }
  906. else
  907. {
  908. return (int)Math.Round(value, 0);
  909. }
  910. }
  911. /// <summary>
  912. /// 计算乘上速度比例后的速度
  913. /// </summary>
  914. /// <param name="speed"></param>
  915. /// <returns></returns>
  916. public int CalculateMultiplySpeedRatio(int speed)
  917. {
  918. return speed * _speedRatio;
  919. }
  920. /// <summary>
  921. /// 计算乘上加速度比例后的加速度
  922. /// </summary>
  923. /// <param name="acceleration"></param>
  924. /// <returns></returns>
  925. public int CalculateMultiplyAccelerationRatio(int acceleration)
  926. {
  927. return acceleration * _accelerationRatio;
  928. }
  929. /// <summary>
  930. /// 计算除以速度比例后的速度
  931. /// </summary>
  932. /// <param name="speed"></param>
  933. /// <returns></returns>
  934. protected int CalculateDivideSpeedRatio(int speed)
  935. {
  936. return speed / _speedRatio;
  937. }
  938. /// <summary>
  939. /// 计算除以加速度比例后的加速度
  940. /// </summary>
  941. /// <param name="acceleration"></param>
  942. /// <returns></returns>
  943. protected int CalculateDivideAccelerationRatio(int acceleration)
  944. {
  945. return acceleration / _accelerationRatio;
  946. }
  947. /// <summary>
  948. /// 计算除以Torque比例后的Torque
  949. /// </summary>
  950. /// <param name="speed"></param>
  951. /// <returns></returns>
  952. protected double CalculateDivideTorqueRatio(int torque)
  953. {
  954. return Math.Round((double)torque / _torqueRatio, 2);
  955. }
  956. /// <summary>
  957. /// Jog Up
  958. /// </summary>
  959. /// <param name="cmd"></param>
  960. /// <param name="args"></param>
  961. /// <returns></returns>
  962. public bool JogUpPosition(string cmd, object[] args)
  963. {
  964. double jog=(double)args[1];
  965. double currentPosition=MotionData.MotorPosition;
  966. if(_jogLimit!=0)
  967. {
  968. if(jog>_jogLimit)
  969. {
  970. LOG.WriteLog(eEvent.ERR_AXIS, Module, $"Jog Value {jog} is over {_jogLimit}");
  971. return false;
  972. }
  973. }
  974. if (!_isSwitchOn)
  975. {
  976. LOG.WriteLog(eEvent.ERR_AXIS, Module, $"Axis is switch off,cannot jog");
  977. return false;
  978. }
  979. return ProfilePositionOperation(Math.Round(currentPosition + jog,2));
  980. }
  981. /// <summary>
  982. /// Jog Down
  983. /// </summary>
  984. /// <param name="cmd"></param>
  985. /// <param name="args"></param>
  986. /// <returns></returns>
  987. public bool JogDownPosition(string cmd, object[] args)
  988. {
  989. double jog = (double)args[1];
  990. double currentPosition = MotionData.MotorPosition;
  991. if (_jogLimit != 0)
  992. {
  993. if (jog > _jogLimit)
  994. {
  995. LOG.WriteLog(eEvent.ERR_AXIS, Module, $"Jog Value {jog} is over {_jogLimit}");
  996. return false;
  997. }
  998. }
  999. if(!_isSwitchOn)
  1000. {
  1001. LOG.WriteLog(eEvent.ERR_AXIS, Module, $"Axis is switch off,cannot jog");
  1002. return false;
  1003. }
  1004. return ProfilePositionOperation(Math.Round(currentPosition - jog,2));
  1005. }
  1006. /// <summary>
  1007. /// Profile position操作
  1008. /// </summary>
  1009. /// <param name="cmd"></param>
  1010. /// <param name="args"></param>
  1011. /// <returns></returns>
  1012. public bool ProfilePositionOperation(double position)
  1013. {
  1014. _targetPosition = position;
  1015. int targetPosition = CalculateValueMultiplyScale(_targetPosition);
  1016. if (_galilAxisConfig.ForwardSoftwareLimit != 0 && targetPosition > _galilAxisConfig.ForwardSoftwareLimit - _galilAxisConfig.HomingOffset)
  1017. {
  1018. LOG.WriteLog(eEvent.ERR_AXIS, Module, $"target position {_targetPosition} is over forward limit");
  1019. return false;
  1020. }
  1021. if (_galilAxisConfig.ReverseSoftwareLimit != 0 && targetPosition < _galilAxisConfig.ReverseSoftwareLimit - _galilAxisConfig.HomingOffset)
  1022. {
  1023. LOG.WriteLog(eEvent.ERR_AXIS, Module, $"target position {_targetPosition} is less reverse limit");
  1024. return false;
  1025. }
  1026. bool result= ProfilePosition(targetPosition, _profileVelocity, _profileAcceleration, _profileDeceleration);
  1027. if (result)
  1028. {
  1029. MotionData.TargetPosition = _targetPosition;
  1030. }
  1031. return result;
  1032. }
  1033. /// <summary>
  1034. /// 移动至指定位置
  1035. /// </summary>
  1036. /// <param name="targetStation"></param>
  1037. /// <returns></returns>
  1038. public bool PositionStation(string targetStation,bool isHome=false,int velocity=0,int acceleration=0,int deceleration=0, bool judgeTorqueLimit = true)
  1039. {
  1040. if (!CheckPositionIsInStation(MotionData.MotorPosition,targetStation))
  1041. {
  1042. if(_interLock!=null&&!isHome&&!_interLock.CheckGotoPosition(targetStation))
  1043. {
  1044. return false;
  1045. }
  1046. var result=GetPositionByStation(targetStation);
  1047. if(result.success)
  1048. {
  1049. _targetPosition = result.position;
  1050. int targetPosition = (int)Math.Round(result.position*_scaleFactor, 0);
  1051. bool positionResult = false;
  1052. if (velocity != 0)
  1053. {
  1054. positionResult= ProfilePosition(targetPosition, velocity, acceleration, deceleration, judgeTorqueLimit);
  1055. }
  1056. else
  1057. {
  1058. positionResult= ProfilePosition(targetPosition, _profileVelocity, _profileAcceleration, _profileDeceleration, judgeTorqueLimit);
  1059. }
  1060. if (positionResult)
  1061. {
  1062. MotionData.TargetPosition = _targetPosition;
  1063. }
  1064. return positionResult;
  1065. }
  1066. LOG.WriteLog(eEvent.ERR_AXIS, Module, $"{targetStation} not in list,cannot goto fixed position");
  1067. return false;
  1068. }
  1069. else
  1070. {
  1071. _status = RState.End;
  1072. }
  1073. return true;
  1074. }
  1075. /// <summary>
  1076. /// 上电
  1077. /// </summary>
  1078. /// <returns></returns>
  1079. public bool WriteSwitchOn()
  1080. {
  1081. return GalilControllerCfgManager.Instance.SetAxisCommand(Module, Name, "SH", null);
  1082. }
  1083. /// <summary>
  1084. /// 下电
  1085. /// </summary>
  1086. /// <returns></returns>
  1087. public bool WriteSwitchOff()
  1088. {
  1089. return GalilControllerCfgManager.Instance.SetAxisCommand(Module, Name, "MO", null);
  1090. }
  1091. /// <summary>
  1092. /// 写入相对位置
  1093. /// </summary>
  1094. /// <param name="referencePosition"></param>
  1095. /// <returns></returns>
  1096. public bool WriteReferencePosition(int referencePosition)
  1097. {
  1098. return GalilControllerCfgManager.Instance.SetAxisCommand(Module, Name, "PR", referencePosition);
  1099. }
  1100. /// <summary>
  1101. /// 写入绝对位置
  1102. /// </summary>
  1103. /// <param name="referencePosition"></param>
  1104. /// <returns></returns>
  1105. public bool WriteAbsolutePosition(int absolutePosition)
  1106. {
  1107. return GalilControllerCfgManager.Instance.SetAxisCommand(Module, Name, "PA", absolutePosition);
  1108. }
  1109. /// <summary>
  1110. /// 写入速度
  1111. /// </summary>
  1112. /// <param name="referencePosition"></param>
  1113. /// <returns></returns>
  1114. public bool WriteSpeed(int speed)
  1115. {
  1116. return GalilControllerCfgManager.Instance.SetAxisCommand(Module, Name, "SP", speed);
  1117. }
  1118. /// <summary>
  1119. /// 写入加速度
  1120. /// </summary>
  1121. /// <param name="referencePosition"></param>
  1122. /// <returns></returns>
  1123. public bool WriteAcceleration(int acceleration)
  1124. {
  1125. return GalilControllerCfgManager.Instance.SetAxisCommand(Module, Name, "AC", acceleration);
  1126. }
  1127. /// <summary>
  1128. /// 写入减速度
  1129. /// </summary>
  1130. /// <param name="referencePosition"></param>
  1131. /// <returns></returns>
  1132. public bool WriteDeceleration(int deceleration)
  1133. {
  1134. return GalilControllerCfgManager.Instance.SetAxisCommand(Module, Name, "DC", deceleration);
  1135. }
  1136. /// <summary>
  1137. /// 停止
  1138. /// </summary>
  1139. /// <returns></returns>
  1140. public bool WriteStop()
  1141. {
  1142. return GalilControllerCfgManager.Instance.SetAxisCommand(Module, Name, "ST", null);
  1143. }
  1144. /// <summary>
  1145. /// 开始运动
  1146. /// </summary>
  1147. /// <returns></returns>
  1148. public bool WriteStartMotion()
  1149. {
  1150. return GalilControllerCfgManager.Instance.SetAxisCommand(Module, Name, "BG", null);
  1151. }
  1152. /// <summary>
  1153. /// Home 电机
  1154. /// </summary>
  1155. /// <returns></returns>
  1156. public bool WriteHomeAxisCommand()
  1157. {
  1158. return GalilControllerCfgManager.Instance.SetAxisCommand(Module, Name, "HM", null);
  1159. }
  1160. /// <summary>
  1161. /// Home 电机
  1162. /// </summary>
  1163. /// <returns></returns>
  1164. public bool WriteFIAxisCommand()
  1165. {
  1166. return GalilControllerCfgManager.Instance.SetAxisCommand(Module, Name, "FI", null);
  1167. }
  1168. /// <summary>
  1169. /// Home 电机
  1170. /// </summary>
  1171. /// <returns></returns>
  1172. public bool WriteCNCommand(string cn)
  1173. {
  1174. return GalilControllerCfgManager.Instance.SetSystemCommand(Module, Name, "CN", cn);
  1175. }
  1176. /// <summary>
  1177. /// 手动置零
  1178. /// </summary>
  1179. /// <returns></returns>
  1180. public bool WriteDP(int dp)
  1181. {
  1182. bool result= GalilControllerCfgManager.Instance.SetAxisCommand(Module, Name, "DP", dp);
  1183. if (result)
  1184. {
  1185. return GalilControllerCfgManager.Instance.SetAxisCommand(Module, Name, "DE", dp);
  1186. }
  1187. return false;
  1188. }
  1189. /// <summary>
  1190. /// 写入控制字
  1191. /// </summary>
  1192. /// <param name="controlWord"></param>
  1193. public bool WriteControlWord(ushort controlWord)
  1194. {
  1195. return BeckhoffAxisManager.Instance.WriteVariableValue($"{Module}.{Name}.{CONTROL_WORD}", controlWord);
  1196. }
  1197. /// <summary>
  1198. /// 写入ModeOfOperation
  1199. /// </summary>
  1200. /// <param name="modeOfOperation"></param>
  1201. /// <returns></returns>
  1202. public bool WriteModeOfMode(AxisModeOfOperation modeOfOperation)
  1203. {
  1204. return BeckhoffAxisManager.Instance.WriteVariableValue($"{Module}.{Name}.{MODE_OF_OPERATION}", (byte)modeOfOperation);
  1205. }
  1206. /// <summary>
  1207. /// 写变量数值
  1208. /// </summary>
  1209. /// <param name="variableName"></param>
  1210. /// <param name="value"></param>
  1211. /// <returns></returns>
  1212. public bool WriteVariable(string variableName,object value)
  1213. {
  1214. return BeckhoffAxisManager.Instance.WriteVariableValue($"{Module}.{Name}.{variableName}", value);
  1215. }
  1216. /// <summary>
  1217. /// 检验GotoPosition条件
  1218. /// </summary>
  1219. /// <param name="station"></param>
  1220. public bool CheckGotoPosition(string station)
  1221. {
  1222. return _interLock.CheckGotoPosition(station);
  1223. }
  1224. /// <summary>
  1225. /// 检验当位Position是否在位置上
  1226. /// </summary>
  1227. /// <param name="position"></param>
  1228. /// <param name="stationName"></param>
  1229. /// <returns></returns>
  1230. public bool CheckPositionIsInStation(double position,string stationName)
  1231. {
  1232. foreach (Station station in _stationAxis.Stations)
  1233. {
  1234. string[] strAry = station.Name.Split('.');
  1235. if(strAry.Length == 0)
  1236. {
  1237. continue;
  1238. }
  1239. if (strAry[strAry.Length-1].ToLower()==(stationName.ToLower()))
  1240. {
  1241. if (double.TryParse(station.Position, out double value))
  1242. {
  1243. if (Math.Round(Math.Abs(position - value), 2) <= _stationAxis.ToleranceDefault)
  1244. {
  1245. return true;
  1246. }
  1247. }
  1248. }
  1249. }
  1250. return false;
  1251. }
  1252. /// <summary>
  1253. /// 检验位置(忽略WaferSize)
  1254. /// </summary>
  1255. /// <param name="position"></param>
  1256. /// <param name="stationName"></param>
  1257. /// <returns></returns>
  1258. public bool CheckPositionInStationIgnoreWaferSize(double position,string stationName)
  1259. {
  1260. foreach (Station station in _stationAxis.Stations)
  1261. {
  1262. if (station.Name.ToLower().Contains(stationName.ToLower()))
  1263. {
  1264. if (double.TryParse(station.Position, out double value))
  1265. {
  1266. if (Math.Round(Math.Abs(position - value), 2) <= _stationAxis.ToleranceDefault)
  1267. {
  1268. return true;
  1269. }
  1270. }
  1271. }
  1272. }
  1273. return false;
  1274. }
  1275. /// <summary>
  1276. /// 检验位置是否为空
  1277. /// </summary>
  1278. /// <param name="position"></param>
  1279. /// <returns></returns>
  1280. public bool CheckPositionIsEmpty(double position)
  1281. {
  1282. foreach (Station station in _stationAxis.Stations)
  1283. {
  1284. if (double.TryParse(station.Position, out double value))
  1285. {
  1286. if (Math.Round(Math.Abs(position - value), 2) <= _stationAxis.ToleranceDefault)
  1287. {
  1288. return false;
  1289. }
  1290. }
  1291. }
  1292. return true;
  1293. }
  1294. /// <summary>
  1295. /// 判定与目标位置相对关系
  1296. /// </summary>
  1297. /// <param name="station"></param>
  1298. /// <param name="compareType">Left--位于目标位置左侧,Right--位于目标位置右侧</param>
  1299. /// <returns></returns>
  1300. public bool JudgeCompareTargetStation(string station,string compareType)
  1301. {
  1302. double position = 0;
  1303. var result = GetPositionByStation(station);
  1304. if(result.success)
  1305. {
  1306. position = result.position;
  1307. }
  1308. else
  1309. {
  1310. return false;
  1311. }
  1312. var buffer16Position=GetPositionByStation(station);
  1313. switch(compareType)
  1314. {
  1315. case "Left":
  1316. return IsInStationLeftDirection(position);
  1317. case "Right":
  1318. if (buffer16Position.success)
  1319. {
  1320. position=buffer16Position.position;
  1321. }
  1322. return IsInStationRightPosition(position);
  1323. default:
  1324. return false;
  1325. }
  1326. }
  1327. /// <summary>
  1328. /// 是否位于位置左侧
  1329. /// </summary>
  1330. /// <param name="stationPosition"></param>
  1331. /// <returns></returns>
  1332. private bool IsInStationLeftDirection(double stationPosition)
  1333. {
  1334. double currentPosition = MotionData.MotorPosition;
  1335. double targetPosition = _targetPosition;
  1336. if(_currentOperation==MotionOperation.Position)
  1337. {
  1338. return currentPosition <= stationPosition && targetPosition <= stationPosition;
  1339. }
  1340. else
  1341. {
  1342. return currentPosition <= stationPosition;
  1343. }
  1344. }
  1345. /// <summary>
  1346. /// 是否位于位置右侧
  1347. /// </summary>
  1348. /// <param name="stationPosition"></param>
  1349. /// <returns></returns>
  1350. private bool IsInStationRightPosition(double stationPosition)
  1351. {
  1352. double currentPosition = MotionData.MotorPosition;
  1353. double targetPosition = _targetPosition;
  1354. if (_currentOperation == MotionOperation.Position)
  1355. {
  1356. return currentPosition > stationPosition && targetPosition > stationPosition;
  1357. }
  1358. else
  1359. {
  1360. return currentPosition > stationPosition ;
  1361. }
  1362. }
  1363. #endregion
  1364. #region virtual共用方法,子类可实现更多方法
  1365. /// <summary>
  1366. /// Home共用方法
  1367. /// </summary>
  1368. /// <returns></returns>
  1369. public virtual bool Home()
  1370. {
  1371. if (!_isSwitchOn)
  1372. {
  1373. EV.PostAlarmLog($"{Module}.{Name}", eEvent.ERR_PUF, $"{Module}.{Name} switch off,cannot home");
  1374. return false;
  1375. }
  1376. if (_status == RState.Running)
  1377. {
  1378. EV.PostAlarmLog($"{Module}.{Name}", eEvent.ERR_PUF, $"{Module}.{Name} current execute {_currentOperation},cannot home");
  1379. return false;
  1380. }
  1381. _currentOperation = MotionOperation.Home;
  1382. _status = RState.Running;
  1383. return true;
  1384. }
  1385. /// <summary>
  1386. /// Home(isLogError)共用方法
  1387. /// </summary>
  1388. /// <returns></returns>
  1389. public virtual bool Home(bool isLogError)
  1390. {
  1391. if (!_isSwitchOn)
  1392. {
  1393. EV.PostAlarmLog($"{Module}.{Name}", eEvent.ERR_PUF, $"{Module}.{Name} switch off,cannot home");
  1394. return false;
  1395. }
  1396. if (_status == RState.Running)
  1397. {
  1398. EV.PostAlarmLog($"{Module}.{Name}", eEvent.ERR_PUF, $"{Module}.{Name} current execute {_currentOperation},cannot home");
  1399. return false;
  1400. }
  1401. _currentOperation = MotionOperation.Home;
  1402. _status = RState.Running;
  1403. return true;
  1404. }
  1405. /// <summary>
  1406. /// 更改速度百分比
  1407. /// </summary>
  1408. /// <param name="percent"></param>
  1409. /// <returns></returns>
  1410. public bool ChangePercentSpeedAceleration(int percent)
  1411. {
  1412. double percentSpeed = _initialVelocity * ((double)percent / 100);
  1413. double percentAceleration = _initialAcceleration * ((double)percent / 100);
  1414. double percentDeceleration = _initialDeceleration * ((double)percent / 100);
  1415. int changedSpeed = (int)(Math.Round(percentSpeed, 0));
  1416. int changedAcceleration = (int)(Math.Round(percentAceleration, 0));
  1417. int changedDeceleration = (int)(Math.Round(percentDeceleration, 0));
  1418. bool result= ChangeSpeedAcceleration(changedSpeed,changedAcceleration,changedDeceleration);
  1419. if(result)
  1420. {
  1421. _profileVelocity = changedSpeed;
  1422. _profileAcceleration = changedAcceleration;
  1423. _profileDeceleration = changedDeceleration;
  1424. }
  1425. return result;
  1426. }
  1427. #endregion
  1428. #region public abstract 子类实现方法
  1429. /// <summary>
  1430. /// 停止操作
  1431. /// </summary>
  1432. /// <param name="cmd"></param>
  1433. /// <param name="args"></param>
  1434. /// <returns></returns>
  1435. public abstract bool StopPositionOperation();
  1436. /// <summary>
  1437. /// change speed
  1438. /// </summary>
  1439. /// <param name="speed"></param>
  1440. /// <returns></returns>
  1441. public abstract bool ChangeSpeed(int speed);
  1442. /// <summary>
  1443. /// 改变速度加速度
  1444. /// </summary>
  1445. /// <param name="speed"></param>
  1446. /// <returns></returns>
  1447. public abstract bool ChangeSpeedAcceleration(int speed, int acceleration, int deceleration);
  1448. /// <summary>
  1449. /// Switch On
  1450. /// </summary>
  1451. public abstract bool SwitchOn();
  1452. /// <summary>
  1453. /// Switch Off
  1454. /// </summary>
  1455. public abstract bool SwitchOff();
  1456. /// <summary>
  1457. /// 停止
  1458. /// </summary>
  1459. public abstract void Stop();
  1460. /// <summary>
  1461. /// Enable Operation
  1462. /// </summary>
  1463. /// <returns></returns>
  1464. public abstract bool EnableOperation();
  1465. /// <summary>
  1466. /// 定时器执行
  1467. /// </summary>
  1468. /// <returns></returns>
  1469. public abstract bool OnTimer();
  1470. /// <summary>
  1471. /// Profile operation
  1472. /// </summary>
  1473. /// <param name="targetPoint"></param>
  1474. /// <param name="profileVelocity"></param>
  1475. /// <param name="profileAcceleration"></param>
  1476. /// <param name="profileDeceleration"></param>
  1477. public abstract bool ProfilePosition(int targetPoint, int profileVelocity, int profileAcceleration, int profileDeceleration, bool judgeTorqueLimit = true);
  1478. #endregion
  1479. #region protected abstract 子类实现方法
  1480. /// <summary>
  1481. /// 初始化Routine
  1482. /// </summary>
  1483. protected abstract void InitializeRoutine();
  1484. /// <summary>
  1485. /// 初始化参数
  1486. /// </summary>
  1487. protected abstract void InitializeParameter();
  1488. /// <summary>
  1489. /// 更新状态字
  1490. /// </summary>
  1491. /// <param name="statusWord"></param>
  1492. public abstract void UpdateStatusWord(ushort statusWord);
  1493. /// <summary>
  1494. /// 回车输入
  1495. /// </summary>
  1496. /// <param name="arg"></param>
  1497. /// <param name="value"></param>
  1498. protected abstract void AxisKeyDown(string arg, double value);
  1499. #endregion
  1500. /// <summary>
  1501. /// 监控
  1502. /// </summary>
  1503. public void Monitor()
  1504. {
  1505. }
  1506. public void Reset()
  1507. {
  1508. }
  1509. /// 停止
  1510. /// </summary>
  1511. public void Terminate()
  1512. {
  1513. }
  1514. }
  1515. }