JetAxisBase.cs 56 KB

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