JetAxisBase.cs 59 KB

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