JetAxisBase.cs 56 KB

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