SrdStationPositionControl.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 CyberX8_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 CyberX8_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 List<string> ModuleItemSource
  84. {
  85. get { return _moduleStations; }
  86. set
  87. {
  88. _moduleStations = value;
  89. InvokePropertyChanged(nameof(ModuleItemSource));
  90. }
  91. }
  92. public static readonly DependencyProperty IsAtStationProperty = DependencyProperty.Register(
  93. "IsAtStation", typeof(bool), typeof(SrdStationPositionControl));
  94. public bool IsAtStation
  95. {
  96. get
  97. {
  98. return (bool)this.GetValue(IsAtStationProperty);
  99. }
  100. set
  101. {
  102. SetValue(IsAtStationProperty, value);
  103. InvokePropertyChanged(nameof(IsAtStation));
  104. }
  105. }
  106. /// <summary>
  107. /// 选项索引
  108. /// </summary>
  109. public string ModuleSelectedItem
  110. {
  111. get
  112. {
  113. return _moduleSelectedItem;
  114. }
  115. set
  116. {
  117. SavedPosition = GetStationPosition(_axis, value);
  118. _moduleSelectedItem = value;
  119. InvokePropertyChanged(nameof(ModuleSelectedItem));
  120. }
  121. }
  122. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
  123. "OperationCommand", typeof(ICommand), typeof(SrdStationPositionControl),
  124. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  125. public ICommand OperationCommand
  126. {
  127. get
  128. {
  129. return (ICommand)this.GetValue(CommandProperty);
  130. }
  131. set
  132. {
  133. this.SetValue(CommandProperty, value);
  134. }
  135. }
  136. public static readonly DependencyProperty ModuleTitleProperty = DependencyProperty.Register(
  137. "ModuleTitle", typeof(string), typeof(SrdStationPositionControl),
  138. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  139. /// <summary>
  140. /// 标题
  141. /// </summary>
  142. public string ModuleTitle
  143. {
  144. get
  145. {
  146. return (string)this.GetValue(ModuleTitleProperty);
  147. }
  148. set
  149. {
  150. this.SetValue(ModuleTitleProperty, value);
  151. }
  152. }
  153. public static readonly DependencyProperty SavedPositionProperty = DependencyProperty.Register(
  154. "SavedPosition", typeof(double), typeof(SrdStationPositionControl), new FrameworkPropertyMetadata(0.00, new PropertyChangedCallback(OnCurrentPositionChanged)));
  155. /// <summary>
  156. /// 保存位置
  157. /// </summary>
  158. public double SavedPosition
  159. {
  160. get
  161. {
  162. return _savePosition;
  163. }
  164. set
  165. {
  166. _savePosition = value;
  167. SetValue(SavedPositionProperty, value);
  168. IsAtStation = JudgeAtStation(value,CurrentPosition,ToleranceDefault);
  169. InvokePropertyChanged(nameof(SavedPosition));
  170. }
  171. }
  172. public static readonly DependencyProperty ToleranceDefaultProperty = DependencyProperty.Register(
  173. "ToleranceDefault", typeof(double), typeof(SrdStationPositionControl), new FrameworkPropertyMetadata(0.00, new PropertyChangedCallback(OnCurrentPositionChanged)));
  174. /// <summary>
  175. /// 保存位置
  176. /// </summary>
  177. public double ToleranceDefault
  178. {
  179. get
  180. {
  181. if(_axis!=null)
  182. {
  183. return _axis.ToleranceDefault;
  184. }
  185. else
  186. {
  187. return 0;
  188. }
  189. }
  190. set
  191. {
  192. SetValue(ToleranceDefaultProperty, _axis != null ? _axis.ToleranceDefault : 0);
  193. }
  194. }
  195. public static readonly DependencyProperty CurrentPositionProperty = DependencyProperty.Register(
  196. "CurrentPosition", typeof(double), typeof(SrdStationPositionControl), new FrameworkPropertyMetadata(0.00, new PropertyChangedCallback(OnCurrentPositionChanged)));
  197. /// <summary>
  198. /// 当前位置
  199. /// </summary>
  200. public double CurrentPosition
  201. {
  202. get
  203. {
  204. return (double)this.GetValue(CurrentPositionProperty);
  205. }
  206. set
  207. {
  208. SetValue(CurrentPositionProperty, value);
  209. }
  210. }
  211. private static void OnCurrentPositionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  212. {
  213. if (e.NewValue != null)
  214. {
  215. double tmpSavedPosition = (double)d.GetValue(SavedPositionProperty);
  216. double toleranceDefault = (double)d.GetValue(ToleranceDefaultProperty);
  217. bool result = JudgeAtStation(tmpSavedPosition, (double)e.NewValue, toleranceDefault);
  218. d.SetValue(IsAtStationProperty, result);
  219. }
  220. }
  221. public static readonly DependencyProperty DegValueProperty = DependencyProperty.Register(
  222. "DegValue", typeof(double), typeof(SrdStationPositionControl), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
  223. /// <summary>
  224. /// jog value
  225. /// </summary>
  226. public double DegValue
  227. {
  228. get
  229. {
  230. return (double)this.GetValue(DegValueProperty);
  231. }
  232. set
  233. {
  234. this.SetValue(DegValueProperty, value);
  235. }
  236. }
  237. public static readonly DependencyProperty TorqueProperty = DependencyProperty.Register(
  238. "Torque", typeof(double), typeof(SrdStationPositionControl), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));
  239. /// <summary>
  240. /// Torque
  241. /// </summary>
  242. public double Torque
  243. {
  244. get
  245. {
  246. return (double)this.GetValue(TorqueProperty);
  247. }
  248. set
  249. {
  250. this.SetValue(TorqueProperty, value);
  251. }
  252. }
  253. public static readonly DependencyProperty CurrentStationProperty = DependencyProperty.Register("CurrentStation", typeof(string), typeof(SrdStationPositionControl));
  254. /// <summary>
  255. /// 当前位置
  256. /// </summary>
  257. public string CurrentStation
  258. {
  259. get
  260. {
  261. return (string)this.GetValue(CurrentStationProperty);
  262. }
  263. set
  264. {
  265. this.SetValue(CurrentStationProperty, value);
  266. }
  267. }
  268. /// <summary>
  269. /// 当前位置
  270. /// </summary>
  271. public double IncrementValue
  272. {
  273. get { return _incrementValue; }
  274. set
  275. {
  276. _incrementValue = value;
  277. InvokePropertyChanged(nameof(IncrementValue));
  278. }
  279. }
  280. public event PropertyChangedEventHandler PropertyChanged;
  281. #endregion
  282. /// <summary>
  283. /// 构造函数
  284. /// </summary>
  285. public SrdStationPositionControl()
  286. {
  287. InitializeComponent();
  288. }
  289. private void SavedPos_Click(object sender, RoutedEventArgs e)
  290. {
  291. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.GotoSavedPosition", "TargetPosition", ModuleSelectedItem);
  292. }
  293. private void MotorPos_Click(object sender, RoutedEventArgs e)
  294. {
  295. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.Save", $"{ModuleSelectedItem}", Math.Round(CurrentPosition, 2));
  296. SavedPosition = CurrentPosition;
  297. foreach (Station item in _axis.Stations)
  298. {
  299. if (item.Name == ModuleSelectedItem)
  300. {
  301. item.Position = CurrentPosition.ToString();
  302. break;
  303. }
  304. }
  305. }
  306. private void LeftCommand_Click(object sender, RoutedEventArgs e)
  307. {
  308. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.JogDown", "TargetPosition", DegValue,CurrentPosition);
  309. }
  310. private void RightCommand_Click(object sender, RoutedEventArgs e)
  311. {
  312. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.JogUp", "TargetPosition", DegValue,CurrentPosition);
  313. }
  314. /// <summary>
  315. /// 加载
  316. /// </summary>
  317. /// <param name="sender"></param>
  318. /// <param name="e"></param>
  319. private void Self_Loaded(object sender, RoutedEventArgs e)
  320. {
  321. if (!string.IsNullOrEmpty(ModuleName))
  322. {
  323. _rtDataKeys.Clear();
  324. IncrementValue = (double)QueryDataClient.Instance.Service.GetConfig("System.Increment");
  325. _rtDataKeys.Add($"Station.{ModuleName}");
  326. _rtDataKeys.Add($"{ModuleName}.AxisProvider");
  327. _rtDataValues = QueryDataClient.Instance.Service.PollData(_rtDataKeys);
  328. _axis = CommonFunction.GetValue<BeckhoffStationAxis>(_rtDataValues, $"Station.{ModuleName}");
  329. BeckhoffProviderAxis beckhoffProviderAxis = CommonFunction.GetValue<BeckhoffProviderAxis>(_rtDataValues, $"{ModuleName}.AxisProvider");
  330. if (_axis != null)
  331. {
  332. ToleranceDefault = _axis.ToleranceDefault;
  333. ModuleItemSource = GetStationList(_axis);
  334. if (ModuleItemSource != null && ModuleItemSource.Count != 0)
  335. {
  336. ModuleSelectedItem = ModuleItemSource[0];
  337. }
  338. }
  339. if(beckhoffProviderAxis != null )
  340. {
  341. _scaleFactor = beckhoffProviderAxis.ScaleFactor;
  342. }
  343. }
  344. }
  345. /// <summary>
  346. /// 获取工位集合
  347. /// </summary>
  348. /// <param name="stationAxis"></param>
  349. /// <returns></returns>
  350. private List<string> GetStationList(BeckhoffStationAxis stationAxis)
  351. {
  352. List<string> lst = new List<string>();
  353. if (stationAxis == null)
  354. {
  355. return lst;
  356. }
  357. foreach (var item in stationAxis.Stations)
  358. {
  359. lst.Add(item.Name);
  360. }
  361. return lst;
  362. }
  363. /// <summary>
  364. /// 获取工位位置
  365. /// </summary>
  366. /// <param name="stationAxis"></param>
  367. /// <param name="station"></param>
  368. /// <returns></returns>
  369. private double GetStationPosition(BeckhoffStationAxis stationAxis, string station)
  370. {
  371. if (stationAxis == null)
  372. {
  373. return 0;
  374. }
  375. foreach (var item in stationAxis.Stations)
  376. {
  377. if (item.Name == station)
  378. {
  379. double.TryParse(item.Position, out var position);
  380. return position;
  381. }
  382. }
  383. return 0;
  384. }
  385. /// <summary>
  386. /// 转换数值
  387. /// </summary>
  388. /// <param name="value"></param>
  389. /// <returns></returns>
  390. private int ConvertValue(double value)
  391. {
  392. if(_scaleFactor!=0)
  393. {
  394. return (int)Math.Round(value * _scaleFactor,0);
  395. }
  396. else
  397. {
  398. return (int)Math.Round(value,0);
  399. }
  400. }
  401. private void InvokePropertyChanged(string propertyName)
  402. {
  403. if (PropertyChanged != null)
  404. {
  405. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  406. }
  407. }
  408. /// <summary>
  409. /// 判定AtStation
  410. /// </summary>
  411. private static bool JudgeAtStation(double saved,double current,double tolerance)
  412. {
  413. return Math.Abs(saved - current) <= tolerance;
  414. }
  415. }
  416. }