SRDMotionViewModel.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Threading;
  7. using MECF.Framework.Common.CommonData.PUF;
  8. using MECF.Framework.Common.DataCenter;
  9. using MECF.Framework.Common.Equipment;
  10. using MECF.Framework.Common.Utilities;
  11. using Prism.Mvvm;
  12. namespace CyberX8_MainPages.ViewModels
  13. {
  14. public class SRDMotionViewModel:BindableBase
  15. {
  16. #region 常量
  17. private const string MOTION_DATA = "MotionData";
  18. private const int ACCEL_FACTOR = 10;
  19. #endregion
  20. #region 内部变量
  21. #region SRD1Arm
  22. /// <summary>
  23. /// SRD1Arm模块名称
  24. /// </summary>
  25. private string _sRD1ArmModuleName;
  26. /// <summary>
  27. /// 数据
  28. /// </summary>
  29. private CommandMotionData _sRD1ArmMotionData;
  30. #endregion
  31. #region SRD1Rotation
  32. /// <summary>
  33. /// SRD1Rotation模块名称
  34. /// </summary>
  35. private string _sRD1RotationModuleName;
  36. /// <summary>
  37. /// 数据
  38. /// </summary>
  39. private CommandMotionData _sRD1RotationMotionData;
  40. #endregion
  41. #region SRD2Arm
  42. /// <summary>
  43. /// SRD2Arm模块名称
  44. /// </summary>
  45. private string _sRD2ArmModuleName;
  46. /// <summary>
  47. /// 数据
  48. /// </summary>
  49. private CommandMotionData _sRD2ArmMotionData;
  50. #endregion
  51. #region SRD2Rotation
  52. /// <summary>
  53. /// SRD2Rotation模块名称
  54. /// </summary>
  55. private string _sRD2RotationModuleName;
  56. /// <summary>
  57. /// 数据
  58. /// </summary>
  59. private CommandMotionData _sRD2RotationMotionData;
  60. #endregion
  61. /// <summary>
  62. /// 增加
  63. /// </summary>
  64. private double _incrementValue = 0.01;
  65. /// <summary>
  66. /// 定时器
  67. /// </summary>
  68. DispatcherTimer _timer;
  69. /// <summary>
  70. /// RT查询key集合
  71. /// </summary>
  72. private List<string> _rtDataKeys = new List<string>();
  73. /// <summary>
  74. /// rt查询key数值字典
  75. /// </summary>
  76. private Dictionary<string, object> _rtDataValueDic = new Dictionary<string, object>();
  77. #endregion
  78. #region 属性
  79. #region SRD1Arm
  80. /// <summary>
  81. /// SRD1Arm名称
  82. /// </summary>
  83. public string SRD1ArmModuleName
  84. {
  85. get { return _sRD1ArmModuleName; }
  86. set { SetProperty(ref _sRD1ArmModuleName, value); }
  87. }
  88. /// <summary>
  89. ///
  90. /// </summary>
  91. public CommandMotionData SRD1ArmMotionData
  92. {
  93. get { return _sRD1ArmMotionData; }
  94. set { SetProperty(ref _sRD1ArmMotionData, value); }
  95. }
  96. #endregion
  97. #region SRD1Rotation
  98. /// <summary>
  99. /// SRD1Rotation名称
  100. /// </summary>
  101. public string SRD1RotaionModuleName
  102. {
  103. get { return _sRD1RotationModuleName; }
  104. set { SetProperty(ref _sRD1RotationModuleName, value); }
  105. }
  106. /// <summary>
  107. ///
  108. /// </summary>
  109. public CommandMotionData SRD1RotationMotionData
  110. {
  111. get { return _sRD1RotationMotionData; }
  112. set { SetProperty(ref _sRD1RotationMotionData, value); }
  113. }
  114. #endregion
  115. #region SRD2Arm
  116. /// <summary>
  117. /// SRD2Arm名称
  118. /// </summary>
  119. public string SRD2ArmModuleName
  120. {
  121. get { return _sRD2ArmModuleName; }
  122. set { SetProperty(ref _sRD2ArmModuleName, value); }
  123. }
  124. /// <summary>
  125. ///
  126. /// </summary>
  127. public CommandMotionData SRD2ArmMotionData
  128. {
  129. get { return _sRD2ArmMotionData; }
  130. set { SetProperty(ref _sRD2ArmMotionData, value); }
  131. }
  132. #endregion
  133. #region SRD2Rotation
  134. /// <summary>
  135. /// SRD2Rotation名称
  136. /// </summary>
  137. public string SRD2RotaionModuleName
  138. {
  139. get { return _sRD2RotationModuleName; }
  140. set { SetProperty(ref _sRD2RotationModuleName, value); }
  141. }
  142. /// <summary>
  143. ///
  144. /// </summary>
  145. public CommandMotionData SRD2RotationMotionData
  146. {
  147. get { return _sRD2RotationMotionData; }
  148. set { SetProperty(ref _sRD2RotationMotionData, value); }
  149. }
  150. #endregion
  151. /// <summary>
  152. /// 步进
  153. /// </summary>
  154. public double IncrementValue
  155. {
  156. get { return _incrementValue; }
  157. set { SetProperty(ref _incrementValue, value); }
  158. }
  159. #endregion
  160. /// <summary>
  161. /// 加载数据
  162. /// </summary>
  163. public void LoadData(string systemName)
  164. {
  165. SRD1ArmModuleName = $"{ModuleName.SRD1}.Arm";
  166. SRD1RotaionModuleName = $"{ModuleName.SRD1}.Rotation";
  167. SRD2ArmModuleName = $"{ModuleName.SRD2}.Arm";
  168. SRD2RotaionModuleName = $"{ModuleName.SRD2}.Rotation";
  169. _rtDataKeys.Clear();
  170. _rtDataKeys.Add($"{SRD1ArmModuleName}.IsHomed");
  171. _rtDataKeys.Add($"{SRD1ArmModuleName}.IsSwitchOn");
  172. _rtDataKeys.Add($"{SRD1ArmModuleName}.{MOTION_DATA}");
  173. _rtDataKeys.Add($"{SRD1RotaionModuleName}.IsHomed");
  174. _rtDataKeys.Add($"{SRD1RotaionModuleName}.IsSwitchOn");
  175. _rtDataKeys.Add($"{SRD1RotaionModuleName}.{MOTION_DATA}");
  176. _rtDataKeys.Add($"{SRD2ArmModuleName}.IsHomed");
  177. _rtDataKeys.Add($"{SRD2ArmModuleName}.IsSwitchOn");
  178. _rtDataKeys.Add($"{SRD2ArmModuleName}.{MOTION_DATA}");
  179. _rtDataKeys.Add($"{SRD2RotaionModuleName}.IsHomed");
  180. _rtDataKeys.Add($"{SRD2RotaionModuleName}.IsSwitchOn");
  181. _rtDataKeys.Add($"{SRD2RotaionModuleName}.{MOTION_DATA}");
  182. if (_timer == null)
  183. {
  184. _timer = new DispatcherTimer();
  185. _timer.Interval = TimeSpan.FromMilliseconds(500);
  186. _timer.Tick += Timer_Tick;
  187. }
  188. _timer.Start();
  189. }
  190. /// <summary>
  191. /// 定时器执行
  192. /// </summary>
  193. /// <param name="sender"></param>
  194. /// <param name="e"></param>
  195. private void Timer_Tick(object sender, EventArgs e)
  196. {
  197. if (_rtDataKeys.Count != 0)
  198. {
  199. _rtDataValueDic = QueryDataClient.Instance.Service.PollData(_rtDataKeys);
  200. if (_rtDataValueDic != null)
  201. {
  202. SRD1ArmMotionData = CommonFunction.GetValue<CommandMotionData>(_rtDataValueDic, $"{SRD1ArmModuleName}.{MOTION_DATA}");
  203. CommandMotionData tmp1 = CommonFunction.GetValue<CommandMotionData>(_rtDataValueDic, $"{SRD1RotaionModuleName}.{MOTION_DATA}");
  204. if(tmp1 != null)
  205. {
  206. tmp1.ActualVelocity = Math.Round(tmp1.ActualVelocity / 6, 2);
  207. tmp1.ProfileVelocity = Math.Round(tmp1.ProfileVelocity / 6, 2);
  208. tmp1.HomingVelocity = Math.Round(tmp1.HomingVelocity / 6, 2);
  209. tmp1.HomingVelocitySlow = Math.Round(tmp1.HomingVelocitySlow / 6, 2);
  210. tmp1.ProfileDecel = tmp1.ProfileDecel * ACCEL_FACTOR;
  211. tmp1.ProfileAccel = tmp1.ProfileAccel * ACCEL_FACTOR;
  212. }
  213. SRD1RotationMotionData = tmp1;
  214. SRD2ArmMotionData = CommonFunction.GetValue<CommandMotionData>(_rtDataValueDic, $"{SRD2ArmModuleName}.{MOTION_DATA}");
  215. CommandMotionData tmp2 = CommonFunction.GetValue<CommandMotionData>(_rtDataValueDic, $"{SRD2RotaionModuleName}.{MOTION_DATA}");
  216. if (tmp2 != null)
  217. {
  218. tmp2.ActualVelocity = Math.Round(tmp2.ActualVelocity / 6, 2);
  219. tmp2.ProfileVelocity = Math.Round(tmp2.ProfileVelocity / 6, 2);
  220. tmp2.HomingVelocity = Math.Round(tmp2.HomingVelocity / 6, 2);
  221. tmp2.HomingVelocitySlow = Math.Round(tmp2.HomingVelocitySlow / 6, 2);
  222. tmp2.ProfileDecel = tmp2.ProfileDecel * ACCEL_FACTOR;
  223. tmp2.ProfileAccel = tmp2.ProfileAccel * ACCEL_FACTOR;
  224. }
  225. SRD2RotationMotionData = tmp2;
  226. }
  227. }
  228. }
  229. /// <summary>
  230. /// 隐藏
  231. /// </summary>
  232. public void Hide()
  233. {
  234. _timer.Stop();
  235. }
  236. }
  237. }