VPWStationPositionControl.xaml.cs 14 KB

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