JetAxisBase.cs 56 KB

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