JetAxisBase.cs 56 KB

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