JetAxisBase.cs 56 KB

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