JetAxisBase.cs 56 KB

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