SrdStationPositionControl.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. using MECF.Framework.Common.Beckhoff.AxisProvider;
  2. using MECF.Framework.Common.Beckhoff.Station;
  3. using MECF.Framework.Common.DataCenter;
  4. using MECF.Framework.Common.OperationCenter;
  5. using MECF.Framework.Common.Utilities;
  6. using PunkHPX8_Core;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Linq;
  11. using System.Runtime.CompilerServices;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Data;
  18. using System.Windows.Documents;
  19. using System.Windows.Input;
  20. using System.Windows.Media;
  21. using System.Windows.Media.Imaging;
  22. using System.Windows.Navigation;
  23. using System.Windows.Shapes;
  24. namespace PunkHPX8_Themes.UserControls
  25. {
  26. /// <summary>
  27. /// SrdStationPositionControl.xaml 的交互逻辑
  28. /// </summary>
  29. public partial class SrdStationPositionControl : UserControl, INotifyPropertyChanged
  30. {
  31. #region 内部变量
  32. /// <summary>
  33. /// 步进
  34. /// </summary>
  35. private double _incrementValue;
  36. /// <summary>
  37. /// 查询后台数据集合
  38. /// </summary>
  39. private List<string> _rtDataKeys = new List<string>();
  40. /// <summary>
  41. /// 查询后台的数据
  42. /// </summary>
  43. private Dictionary<string, object> _rtDataValues;
  44. /// <summary>
  45. /// axis
  46. /// </summary>
  47. BeckhoffStationAxis _axis;
  48. /// <summary>
  49. /// Stations
  50. /// </summary>
  51. private List<string> _moduleStations;
  52. /// <summary>
  53. /// 选择项
  54. /// </summary>
  55. private string _moduleSelectedItem;
  56. /// <summary>
  57. /// 已经保存的位置
  58. /// </summary>
  59. private double _savePosition;
  60. /// <summary>
  61. /// scaleFactor
  62. /// </summary>
  63. private double _scaleFactor;
  64. #endregion
  65. #region 属性
  66. public static readonly DependencyProperty ModuleNameProperty = DependencyProperty.Register(
  67. "ModuleName", typeof(string), typeof(SrdStationPositionControl),
  68. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  69. /// <summary>
  70. /// 模块名称
  71. /// </summary>
  72. public string ModuleName
  73. {
  74. get
  75. {
  76. return (string)this.GetValue(ModuleNameProperty);
  77. }
  78. set
  79. {
  80. this.SetValue(ModuleNameProperty, value);
  81. }
  82. }
  83. public static readonly DependencyProperty UnitProperty = DependencyProperty.Register(
  84. "Unit", typeof(string), typeof(SrdStationPositionControl),
  85. new FrameworkPropertyMetadata("deg", FrameworkPropertyMetadataOptions.AffectsRender));
  86. /// <summary>
  87. /// 模块名称
  88. /// </summary>
  89. public string Unit
  90. {
  91. get
  92. {
  93. return (string)this.GetValue(UnitProperty);
  94. }
  95. set
  96. {
  97. this.SetValue(UnitProperty, value);
  98. }
  99. }
  100. public List<string> ModuleItemSource
  101. {
  102. get { return _moduleStations; }
  103. set
  104. {
  105. _moduleStations = value;
  106. InvokePropertyChanged(nameof(ModuleItemSource));
  107. }
  108. }
  109. public static readonly DependencyProperty IsAtStationProperty = DependencyProperty.Register(
  110. "IsAtStation", typeof(bool), typeof(SrdStationPositionControl));
  111. public bool IsAtStation
  112. {
  113. get
  114. {
  115. return (bool)this.GetValue(IsAtStationProperty);
  116. }
  117. set
  118. {
  119. SetValue(IsAtStationProperty, value);
  120. InvokePropertyChanged(nameof(IsAtStation));
  121. }
  122. }
  123. /// <summary>
  124. /// 选项索引
  125. /// </summary>
  126. public string ModuleSelectedItem
  127. {
  128. get
  129. {
  130. return _moduleSelectedItem;
  131. }
  132. set
  133. {
  134. SavedPosition = GetStationPosition(_axis, value);
  135. _moduleSelectedItem = value;
  136. InvokePropertyChanged(nameof(ModuleSelectedItem));
  137. }
  138. }
  139. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
  140. "OperationCommand", typeof(ICommand), typeof(SrdStationPositionControl),
  141. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  142. public ICommand OperationCommand
  143. {
  144. get
  145. {
  146. return (ICommand)this.GetValue(CommandProperty);
  147. }
  148. set
  149. {
  150. this.SetValue(CommandProperty, value);
  151. }
  152. }
  153. public static readonly DependencyProperty ModuleTitleProperty = DependencyProperty.Register(
  154. "ModuleTitle", typeof(string), typeof(SrdStationPositionControl),
  155. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  156. /// <summary>
  157. /// 标题
  158. /// </summary>
  159. public string ModuleTitle
  160. {
  161. get
  162. {
  163. return (string)this.GetValue(ModuleTitleProperty);
  164. }
  165. set
  166. {
  167. this.SetValue(ModuleTitleProperty, value);
  168. }
  169. }
  170. public static readonly DependencyProperty SavedPositionProperty = DependencyProperty.Register(
  171. "SavedPosition", typeof(double), typeof(SrdStationPositionControl), new FrameworkPropertyMetadata(0.00, new PropertyChangedCallback(OnCurrentPositionChanged)));
  172. /// <summary>
  173. /// 保存位置
  174. /// </summary>
  175. public double SavedPosition
  176. {
  177. get
  178. {
  179. return _savePosition;
  180. }
  181. set
  182. {
  183. _savePosition = value;
  184. SetValue(SavedPositionProperty, value);
  185. IsAtStation = JudgeAtStation(value,CurrentPosition,ToleranceDefault);
  186. InvokePropertyChanged(nameof(SavedPosition));
  187. }
  188. }
  189. public static readonly DependencyProperty ToleranceDefaultProperty = DependencyProperty.Register(
  190. "ToleranceDefault", typeof(double), typeof(SrdStationPositionControl), new FrameworkPropertyMetadata(0.00, new PropertyChangedCallback(OnCurrentPositionChanged)));
  191. /// <summary>
  192. /// 保存位置
  193. /// </summary>
  194. public double ToleranceDefault
  195. {
  196. get
  197. {
  198. if(_axis!=null)
  199. {
  200. return _axis.ToleranceDefault;
  201. }
  202. else
  203. {
  204. return 0;
  205. }
  206. }
  207. set
  208. {
  209. SetValue(ToleranceDefaultProperty, _axis != null ? _axis.ToleranceDefault : 0);
  210. }
  211. }
  212. public static readonly DependencyProperty CurrentPositionProperty = DependencyProperty.Register(
  213. "CurrentPosition", typeof(double), typeof(SrdStationPositionControl), new FrameworkPropertyMetadata(0.00, new PropertyChangedCallback(OnCurrentPositionChanged)));
  214. /// <summary>
  215. /// 当前位置
  216. /// </summary>
  217. public double CurrentPosition
  218. {
  219. get
  220. {
  221. return (double)this.GetValue(CurrentPositionProperty);
  222. }
  223. set
  224. {
  225. SetValue(CurrentPositionProperty, value);
  226. }
  227. }
  228. private static void OnCurrentPositionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  229. {
  230. if (e.NewValue != null)
  231. {
  232. double tmpSavedPosition = (double)d.GetValue(SavedPositionProperty);
  233. double toleranceDefault = (double)d.GetValue(ToleranceDefaultProperty);
  234. bool result = JudgeAtStation(tmpSavedPosition, (double)e.NewValue, toleranceDefault);
  235. d.SetValue(IsAtStationProperty, result);
  236. }
  237. }
  238. public static readonly DependencyProperty DegValueProperty = DependencyProperty.Register(
  239. "DegValue", typeof(double), typeof(SrdStationPositionControl), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
  240. /// <summary>
  241. /// jog value
  242. /// </summary>
  243. public double DegValue
  244. {
  245. get
  246. {
  247. return (double)this.GetValue(DegValueProperty);
  248. }
  249. set
  250. {
  251. this.SetValue(DegValueProperty, value);
  252. }
  253. }
  254. public static readonly DependencyProperty TorqueProperty = DependencyProperty.Register(
  255. "Torque", typeof(double), typeof(SrdStationPositionControl), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
  256. /// <summary>
  257. /// Torque
  258. /// </summary>
  259. public double Torque
  260. {
  261. get
  262. {
  263. return (double)this.GetValue(TorqueProperty);
  264. }
  265. set
  266. {
  267. this.SetValue(TorqueProperty, value);
  268. }
  269. }
  270. public static readonly DependencyProperty CurrentStationProperty = DependencyProperty.Register("CurrentStation", typeof(string), typeof(SrdStationPositionControl));
  271. /// <summary>
  272. /// 当前位置
  273. /// </summary>
  274. public string CurrentStation
  275. {
  276. get
  277. {
  278. return (string)this.GetValue(CurrentStationProperty);
  279. }
  280. set
  281. {
  282. this.SetValue(CurrentStationProperty, value);
  283. }
  284. }
  285. /// <summary>
  286. /// 当前位置
  287. /// </summary>
  288. public double IncrementValue
  289. {
  290. get { return _incrementValue; }
  291. set
  292. {
  293. _incrementValue = value;
  294. InvokePropertyChanged(nameof(IncrementValue));
  295. }
  296. }
  297. public event PropertyChangedEventHandler PropertyChanged;
  298. #endregion
  299. /// <summary>
  300. /// 构造函数
  301. /// </summary>
  302. public SrdStationPositionControl()
  303. {
  304. InitializeComponent();
  305. }
  306. private void SavedPos_Click(object sender, RoutedEventArgs e)
  307. {
  308. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.GotoSavedPosition", "TargetPosition", ModuleSelectedItem);
  309. }
  310. private void MotorPos_Click(object sender, RoutedEventArgs e)
  311. {
  312. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.Save", $"{ModuleSelectedItem}", Math.Round(CurrentPosition, 2));
  313. SavedPosition = CurrentPosition;
  314. foreach (Station item in _axis.Stations)
  315. {
  316. if (item.Name == ModuleSelectedItem)
  317. {
  318. item.Position = CurrentPosition.ToString();
  319. break;
  320. }
  321. }
  322. }
  323. private void LeftCommand_Click(object sender, RoutedEventArgs e)
  324. {
  325. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.JogDown", "TargetPosition", DegValue,CurrentPosition);
  326. }
  327. private void RightCommand_Click(object sender, RoutedEventArgs e)
  328. {
  329. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.JogUp", "TargetPosition", DegValue,CurrentPosition);
  330. }
  331. /// <summary>
  332. /// 加载
  333. /// </summary>
  334. /// <param name="sender"></param>
  335. /// <param name="e"></param>
  336. private void Self_Loaded(object sender, RoutedEventArgs e)
  337. {
  338. if (!string.IsNullOrEmpty(ModuleName))
  339. {
  340. _rtDataKeys.Clear();
  341. IncrementValue = (double)QueryDataClient.Instance.Service.GetConfig("System.Increment");
  342. _rtDataKeys.Add($"Station.{ModuleName}");
  343. _rtDataKeys.Add($"{ModuleName}.AxisProvider");
  344. _rtDataValues = QueryDataClient.Instance.Service.PollData(_rtDataKeys);
  345. _axis = CommonFunction.GetValue<BeckhoffStationAxis>(_rtDataValues, $"Station.{ModuleName}");
  346. BeckhoffProviderAxis beckhoffProviderAxis = CommonFunction.GetValue<BeckhoffProviderAxis>(_rtDataValues, $"{ModuleName}.AxisProvider");
  347. if (_axis != null)
  348. {
  349. ToleranceDefault = _axis.ToleranceDefault;
  350. ModuleItemSource = GetStationList(_axis);
  351. if (ModuleItemSource != null && ModuleItemSource.Count != 0)
  352. {
  353. ModuleSelectedItem = ModuleItemSource[0];
  354. }
  355. }
  356. if(beckhoffProviderAxis != null )
  357. {
  358. _scaleFactor = beckhoffProviderAxis.ScaleFactor;
  359. }
  360. }
  361. }
  362. /// <summary>
  363. /// 获取工位集合
  364. /// </summary>
  365. /// <param name="stationAxis"></param>
  366. /// <returns></returns>
  367. private List<string> GetStationList(BeckhoffStationAxis stationAxis)
  368. {
  369. List<string> lst = new List<string>();
  370. if (stationAxis == null)
  371. {
  372. return lst;
  373. }
  374. foreach (var item in stationAxis.Stations)
  375. {
  376. lst.Add(item.Name);
  377. }
  378. return lst;
  379. }
  380. /// <summary>
  381. /// 获取工位位置
  382. /// </summary>
  383. /// <param name="stationAxis"></param>
  384. /// <param name="station"></param>
  385. /// <returns></returns>
  386. private double GetStationPosition(BeckhoffStationAxis stationAxis, string station)
  387. {
  388. if (stationAxis == null)
  389. {
  390. return 0;
  391. }
  392. foreach (var item in stationAxis.Stations)
  393. {
  394. if (item.Name == station)
  395. {
  396. double.TryParse(item.Position, out var position);
  397. return position;
  398. }
  399. }
  400. return 0;
  401. }
  402. /// <summary>
  403. /// 转换数值
  404. /// </summary>
  405. /// <param name="value"></param>
  406. /// <returns></returns>
  407. private int ConvertValue(double value)
  408. {
  409. if(_scaleFactor!=0)
  410. {
  411. return (int)Math.Round(value * _scaleFactor,0);
  412. }
  413. else
  414. {
  415. return (int)Math.Round(value,0);
  416. }
  417. }
  418. private void InvokePropertyChanged(string propertyName)
  419. {
  420. if (PropertyChanged != null)
  421. {
  422. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  423. }
  424. }
  425. /// <summary>
  426. /// 判定AtStation
  427. /// </summary>
  428. private static bool JudgeAtStation(double saved,double current,double tolerance)
  429. {
  430. return Math.Abs(saved - current) <= tolerance;
  431. }
  432. }
  433. }