JetAxisBase.cs 60 KB

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