JetAxisBase.cs 56 KB

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