JetAxisBase.cs 58 KB

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