VPWStationPositionControl.xaml.cs 14 KB

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