版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UIAxisDev.cs 12 KiB

2 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using Advantech.Motion;
  2. using Newtonsoft.Json;
  3. using AssistClient.Device;
  4. using AssistClient.Utils;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. using System.Xml.Linq;
  17. using static AssistClient.Device.AxisDev;
  18. using static AssistClient.UI.UIAxisDev;
  19. namespace AssistClient.UI
  20. {
  21. public partial class UIAxisDev : UserControl
  22. {
  23. SynchronizationContext SyncContext = null;
  24. public Action<string> GetParamsEvent;
  25. //
  26. private AxisDevProp prop = new AxisDevProp();
  27. private AxisDev dev;
  28. public UIAxisDev()
  29. {
  30. InitializeComponent();
  31. propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged;
  32. refreshUIState();
  33. //获取UI线程同步上下文
  34. SyncContext = SynchronizationContext.Current;
  35. //init();
  36. }
  37. private void refreshUIState()
  38. {
  39. foreach (ToolStripItem item in this.toolStrip1.Items)
  40. {
  41. if (item.Text == "打开设备")
  42. item.Visible = (dev == null || !dev.IsInit);
  43. else
  44. this.propertyGrid1.Enabled=item.Enabled = !(dev == null || !dev.IsInit);
  45. }
  46. }
  47. public string getParamsData()
  48. {
  49. return prop.serialize();
  50. }
  51. public bool stopNow()
  52. {
  53. if (dev != null && dev.IsInit)
  54. {
  55. dev.stopNow();
  56. return true;
  57. }
  58. else
  59. return false;
  60. }
  61. public void setParamsData(string json)
  62. {
  63. if (json == "") return;
  64. prop.deserialize(json);
  65. this.propertyGrid1.Refresh();
  66. }
  67. public void init()
  68. {
  69. this.tbtnSave.Visible = tssSave.Visible = (GetParamsEvent != null);
  70. this.propertyGrid1.SelectedObject = prop;
  71. dev = new AxisDev();
  72. dev.WarningEvent = (level, info) =>
  73. {
  74. txtLog.Text = $"({level}){info}";
  75. };
  76. //STATE
  77. dev.axisStateEvent += new System.Action<int, AxisStateType, uint>((axisIndex, type, stateValue) =>
  78. {
  79. if (axisIndex != (int)prop.AxisIndex)
  80. return;
  81. switch (type)
  82. {
  83. case AxisStateType.AxisState:
  84. if (prop.AxState != (AxisState)stateValue)
  85. {
  86. prop.AxState = (AxisState)stateValue;
  87. this.refreshUI();
  88. }
  89. break;
  90. case AxisStateType.AxisIOState:
  91. if (prop.AxIOStatus != stateValue)
  92. {
  93. prop.AxIOStatus = stateValue;
  94. prop.ALM = (stateValue & (uint)Ax_Motion_IO.AX_MOTION_IO_ALM) > 0;//需IO close
  95. prop.EMG = (stateValue & (uint)Ax_Motion_IO.AX_MOTION_IO_EMG) > 0;//需重置 state
  96. prop.LMTP = (stateValue & (uint)Ax_Motion_IO.AX_MOTION_IO_LMTP) > 0;//右极限 true->false
  97. prop.LMTN = (stateValue & (uint)Ax_Motion_IO.AX_MOTION_IO_LMTN) > 0;//左极限
  98. prop.ORG = (stateValue & (uint)Ax_Motion_IO.AX_MOTION_IO_ORG) > 0;
  99. prop.EZ = (stateValue & (uint)Ax_Motion_IO.AX_MOTION_IO_EZ) > 0;
  100. prop.SVON = (stateValue & (uint)Ax_Motion_IO.AX_MOTION_IO_SVON) > 0;
  101. this.refreshUI();
  102. }
  103. break;
  104. case AxisStateType.AxisMotionState:
  105. if (prop.AxMotionState != stateValue)
  106. {
  107. prop.AxMotionState = stateValue;
  108. this.refreshUI();
  109. }
  110. break;
  111. }
  112. });
  113. //位置POS
  114. dev.axisPosEvent += new System.Action<int, AxisPosType, double>((axisIndex, type, pos) =>
  115. {
  116. if (axisIndex != (int)prop.AxisIndex)
  117. return;
  118. switch (type)
  119. {
  120. case AxisPosType.CmdPos:
  121. if (prop.CmdPos != pos)
  122. {
  123. prop.CmdPos = pos;
  124. this.refreshUI();
  125. }
  126. break;
  127. case AxisPosType.ActualPos:
  128. if (prop.ActualPos != pos)
  129. {
  130. prop.ActualPos = pos;
  131. this.refreshUI();
  132. }
  133. break;
  134. }
  135. });
  136. if (!dev.start(Config.Axis_PulseOutMode,true))
  137. {
  138. this.closeDev();
  139. return;
  140. }
  141. prop.DeviceNum = dev.DeviceNum.ToString();
  142. refreshAxisVelParam();
  143. refreshUIState();
  144. }
  145. private void refreshUI()
  146. {
  147. SyncContext.Post(m =>
  148. {
  149. var result = m as string;
  150. propertyGrid1.Refresh();
  151. tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  152. tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
  153. }, "异步操作完成结果");
  154. }
  155. private void refreshAxisVelParam()
  156. {
  157. int axisIndex = (int)prop.AxisIndex;
  158. //prop.HomeMode = (AxitHomeMode)Config.Axis_HomeMode[axisIndex];
  159. var values = dev.getAxisVelParam(axisIndex);
  160. prop.VelLow = dev.tomm(values[0], Config.Axis_MM2PulseNum[axisIndex]);
  161. prop.VelHigh = dev.tomm(values[1], Config.Axis_MM2PulseNum[axisIndex]);
  162. prop.Acc = dev.tomm(values[2], Config.Axis_MM2PulseNum[axisIndex]);
  163. prop.Dec = dev.tomm(values[3], Config.Axis_MM2PulseNum[axisIndex]);
  164. }
  165. private void closeDev()
  166. {
  167. try
  168. {
  169. if (dev == null)
  170. return;
  171. dev.stop();
  172. }
  173. catch (Exception ex)
  174. {
  175. this.refreshUIState();
  176. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  177. }
  178. }
  179. protected override void OnHandleDestroyed(EventArgs e)
  180. {
  181. base.OnHandleDestroyed(e);
  182. // 在此添加需要手动释放资源的代码
  183. this.closeDev();
  184. }
  185. private void tbtnRun_Click(object sender, EventArgs e)
  186. {
  187. try
  188. {
  189. dev.setAxisVelParam(prop.VelLow, prop.VelHigh, prop.Acc, prop.Dec, (int)prop.AxisIndex);
  190. dev.move_ptp((int)prop.AxisIndex, prop.Value, prop.MoveMode);
  191. //running...
  192. }
  193. catch(Exception ex)
  194. {
  195. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  196. }
  197. }
  198. private void tbtnExport_Click(object sender, EventArgs e)
  199. {
  200. string filePath = FileUtil.saveAsFile($"Axis{(int)prop.AxisIndex}配置.json", "JSON|*.json");
  201. if (filePath != "")
  202. {
  203. string jsonText = prop.serialize();
  204. File.WriteAllText(filePath, jsonText);
  205. MessageBox.Show("保存成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  206. }
  207. }
  208. private void tbtnImport_Click(object sender, EventArgs e)
  209. {
  210. string filePath = FileUtil.openFile("JSON|*.json");
  211. if (filePath != "")
  212. {
  213. string jsonText = File.ReadAllText(filePath);
  214. prop.deserialize(jsonText);
  215. this.propertyGrid1.Refresh();
  216. //this.propertyGrid1.SelectedObject= axisProp;
  217. //MessageBox.Show("加载成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  218. }
  219. }
  220. private void tbtnHome_Click(object sender, EventArgs e)
  221. {
  222. int axitIndex = (int)prop.AxisIndex;
  223. dev.setAxisVelParam(prop.VelLow, prop.VelHigh, prop.Acc, prop.Dec, (int)prop.AxisIndex);
  224. //dev.setAxisVelParam(prop.VelLow, prop.VelHigh, prop.Acc, prop.Dec, (int)prop.AxisIndex,true);
  225. dev.home(axitIndex, (uint)Config.Axis_HomeMode[axitIndex], (uint)Config.Axis_HomeDir[axitIndex]);
  226. //if (prop.DeviceType == AxisType.默认)
  227. //{
  228. // dev.home((int)prop.AxisIndex);
  229. //}
  230. //else //直线电机
  231. //{
  232. // //dev.move_ptp((int)prop.AxisIndex, - prop.ActualPos, false);
  233. // dev.move_ptp((int)prop.AxisIndex, 0, true);
  234. //}
  235. }
  236. private void tbtnLeft_MouseDown(object sender, MouseEventArgs e)
  237. {
  238. dev.jog((int)prop.AxisIndex, 1);
  239. }
  240. private void tbtnLeft_MouseUp(object sender, MouseEventArgs e)
  241. {
  242. dev.stopDec((int)prop.AxisIndex);
  243. }
  244. private void tbtnRight_MouseDown(object sender, MouseEventArgs e)
  245. {
  246. dev.jog((int)prop.AxisIndex, 0);
  247. }
  248. private void tbtnRight_MouseUp(object sender, MouseEventArgs e)
  249. {
  250. dev.stopDec((int)prop.AxisIndex);
  251. }
  252. private void tbtnJogOnOff_Click(object sender, EventArgs e)
  253. {
  254. if(prop.AxState==AxisState.STA_AX_EXT_JOG)
  255. {
  256. dev.closeJogMode((int)prop.AxisIndex);
  257. //tbtnJogOnOff.Text = "开启Jog";
  258. }
  259. else
  260. {
  261. dev.setAxisVelParam(prop.VelLow, prop.VelHigh, prop.Acc, prop.Dec, (int)prop.AxisIndex);
  262. dev.openJogMode((int)prop.AxisIndex);
  263. //tbtnJogOnOff.Text = "关闭Jog";
  264. }
  265. }
  266. private void tbtnStop_Click(object sender, EventArgs e)
  267. {
  268. dev.stopDec((int)prop.AxisIndex);
  269. }
  270. private void tbtnRestState_Click(object sender, EventArgs e)
  271. {
  272. dev.resetAxisState((int)prop.AxisIndex);
  273. }
  274. private void tbtnResetPos_Click(object sender, EventArgs e)
  275. {
  276. dev.resetCmdPosition((int)prop.AxisIndex);
  277. dev.resetActualPosition((int)prop.AxisIndex);
  278. }
  279. private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  280. {
  281. //其中包含了两个重要的属性:OldValue和ChangeItem。
  282. //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。
  283. //而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。
  284. string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
  285. string propertyName = e.ChangedItem.PropertyDescriptor.Name;
  286. var oldValue = e.OldValue;
  287. var newValue = e.ChangedItem.Value;
  288. switch (propertyName)
  289. {
  290. case "AxisIndex":
  291. refreshAxisVelParam();
  292. break;
  293. }
  294. }
  295. private void tbtnSave_Click(object sender, EventArgs e)
  296. {
  297. GetParamsEvent?.Invoke(prop.serialize());
  298. }
  299. private void tsbtnOpenDev_Click(object sender, EventArgs e)
  300. {
  301. this.init();
  302. }
  303. }
  304. }