DefaultRobotDebugPage.xaml.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Globalization;
  3. using System.Threading.Tasks;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using TeamAAS.Robot.Interfaces;
  7. namespace TeamAAS.Robot.UI
  8. {
  9. /// <summary>
  10. /// 默认机器人调试页:状态/坐标 + 电机/重置/刹车 + 步进/Tool + Jog 六向。
  11. /// 未在 [Robot] 特性指定 DebugPageType 的品牌机器人均用本页。
  12. /// 照 MotionDebugView 范式:code-behind 直接操作 BindRobot 得到的 IRobot 实例。
  13. /// </summary>
  14. public partial class DefaultRobotDebugPage : UserControl, IRobotDebugPage
  15. {
  16. private IRobot _robot;
  17. private readonly System.Windows.Threading.DispatcherTimer _statusTimer;
  18. public DefaultRobotDebugPage()
  19. {
  20. InitializeComponent();
  21. _statusTimer = new System.Windows.Threading.DispatcherTimer { Interval = TimeSpan.FromMilliseconds(500) };
  22. _statusTimer.Tick += (s, e) => RefreshStatus();
  23. }
  24. public void BindRobot(IRobot robot)
  25. {
  26. _robot = robot;
  27. OpsRoot.IsEnabled = robot != null;
  28. if (robot != null)
  29. {
  30. _statusTimer.Start();
  31. RefreshStatus();
  32. var _t = GetPosAsync();
  33. }
  34. else
  35. {
  36. _statusTimer.Stop();
  37. }
  38. }
  39. public void UnbindRobot()
  40. {
  41. _statusTimer.Stop();
  42. _robot = null;
  43. OpsRoot.IsEnabled = false;
  44. TxtStatus.Text = "";
  45. TxtMessage.Text = "";
  46. TxtPos.Text = "X: --- Y: --- Z: ---";
  47. }
  48. private void RefreshStatus()
  49. {
  50. if (_robot == null) { TxtStatus.Text = ""; return; }
  51. try { TxtStatus.Text = _robot.IsConnected ? "已连接" : "未连接"; }
  52. catch { }
  53. }
  54. private double Step
  55. {
  56. get { double s; return double.TryParse(TxtStep.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out s) ? s : 1.0; }
  57. }
  58. private int ToolIndex { get { return ToolCombo.SelectedIndex < 0 ? 0 : ToolCombo.SelectedIndex; } }
  59. private void Msg(string m) { TxtMessage.Text = m; }
  60. private async void BtnMotorOn_Click(object sender, RoutedEventArgs e) { await RunAsync(() => _robot.MotorAsync(true), "电机已开启", "电机操作"); }
  61. private async void BtnMotorOff_Click(object sender, RoutedEventArgs e) { await RunAsync(() => _robot.MotorAsync(false), "电机已关闭", "电机操作"); }
  62. private async void BtnReset_Click(object sender, RoutedEventArgs e) { await RunAsync(() => _robot.ResetAsync(), "已重置", "重置"); }
  63. private async void BtnSFree_Click(object sender, RoutedEventArgs e) { await RunAsync(() => _robot.SFreeAsync(), "已释放刹车", "释放刹车"); }
  64. private async void BtnSLock_Click(object sender, RoutedEventArgs e) { await RunAsync(() => _robot.SLockAsync(), "已锁定刹车", "锁定刹车"); }
  65. private async void BtnGetPos_Click(object sender, RoutedEventArgs e) { await GetPosAsync(); }
  66. private async void BtnSelectTool_Click(object sender, RoutedEventArgs e)
  67. {
  68. int t = ToolIndex;
  69. await RunAsync(() => _robot.SelectToolAsync(t), "已选择 Tool " + t, "选择Tool");
  70. }
  71. private async void BtnJog_Click(object sender, RoutedEventArgs e)
  72. {
  73. var btn = sender as Button;
  74. var param = btn == null ? null : btn.Tag as string;
  75. if (_robot == null || string.IsNullOrEmpty(param) || param.Length < 2) return;
  76. string axis = param.Substring(0, 1);
  77. string dir = param.Substring(1, 1);
  78. double dist = Step * (dir == "+" ? 1 : -1);
  79. await RunAsync(() => _robot.JogAsync(axis, dist), "Jog " + param + " " + Step + "mm", "Jog");
  80. await GetPosAsync();
  81. }
  82. private async Task RunAsync(Func<Task<bool>> op, string okMsg, string errLabel)
  83. {
  84. if (_robot == null) return;
  85. try { await op(); Msg(okMsg); }
  86. catch (Exception ex) { Msg(errLabel + "异常: " + ex.Message); }
  87. }
  88. private async Task GetPosAsync()
  89. {
  90. if (_robot == null) return;
  91. try
  92. {
  93. var pos = await _robot.GetRobotPosAsync();
  94. if (pos != null)
  95. TxtPos.Text = string.Format("X: {0:F3} Y: {1:F3} Z: {2:F3}\nU: {3:F3} V: {4:F3} W: {5:F3}",
  96. pos.X, pos.Y, pos.Z, pos.U, pos.V, pos.W);
  97. }
  98. catch (Exception ex) { Msg("读取位置异常: " + ex.Message); }
  99. }
  100. }
  101. }