| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- using System;
- using System.Globalization;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using TeamAAS.Robot.Interfaces;
- namespace TeamAAS.Robot.UI
- {
- /// <summary>
- /// 默认机器人调试页:状态/坐标 + 电机/重置/刹车 + 步进/Tool + Jog 六向。
- /// 未在 [Robot] 特性指定 DebugPageType 的品牌机器人均用本页。
- /// 照 MotionDebugView 范式:code-behind 直接操作 BindRobot 得到的 IRobot 实例。
- /// </summary>
- public partial class DefaultRobotDebugPage : UserControl, IRobotDebugPage
- {
- private IRobot _robot;
- private readonly System.Windows.Threading.DispatcherTimer _statusTimer;
- public DefaultRobotDebugPage()
- {
- InitializeComponent();
- _statusTimer = new System.Windows.Threading.DispatcherTimer { Interval = TimeSpan.FromMilliseconds(500) };
- _statusTimer.Tick += (s, e) => RefreshStatus();
- }
- public void BindRobot(IRobot robot)
- {
- _robot = robot;
- OpsRoot.IsEnabled = robot != null;
- if (robot != null)
- {
- _statusTimer.Start();
- RefreshStatus();
- var _t = GetPosAsync();
- }
- else
- {
- _statusTimer.Stop();
- }
- }
- public void UnbindRobot()
- {
- _statusTimer.Stop();
- _robot = null;
- OpsRoot.IsEnabled = false;
- TxtStatus.Text = "";
- TxtMessage.Text = "";
- TxtPos.Text = "X: --- Y: --- Z: ---";
- }
- private void RefreshStatus()
- {
- if (_robot == null) { TxtStatus.Text = ""; return; }
- try { TxtStatus.Text = _robot.IsConnected ? "已连接" : "未连接"; }
- catch { }
- }
- private double Step
- {
- get { double s; return double.TryParse(TxtStep.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out s) ? s : 1.0; }
- }
- private int ToolIndex { get { return ToolCombo.SelectedIndex < 0 ? 0 : ToolCombo.SelectedIndex; } }
- private void Msg(string m) { TxtMessage.Text = m; }
- private async void BtnMotorOn_Click(object sender, RoutedEventArgs e) { await RunAsync(() => _robot.MotorAsync(true), "电机已开启", "电机操作"); }
- private async void BtnMotorOff_Click(object sender, RoutedEventArgs e) { await RunAsync(() => _robot.MotorAsync(false), "电机已关闭", "电机操作"); }
- private async void BtnReset_Click(object sender, RoutedEventArgs e) { await RunAsync(() => _robot.ResetAsync(), "已重置", "重置"); }
- private async void BtnSFree_Click(object sender, RoutedEventArgs e) { await RunAsync(() => _robot.SFreeAsync(), "已释放刹车", "释放刹车"); }
- private async void BtnSLock_Click(object sender, RoutedEventArgs e) { await RunAsync(() => _robot.SLockAsync(), "已锁定刹车", "锁定刹车"); }
- private async void BtnGetPos_Click(object sender, RoutedEventArgs e) { await GetPosAsync(); }
- private async void BtnSelectTool_Click(object sender, RoutedEventArgs e)
- {
- int t = ToolIndex;
- await RunAsync(() => _robot.SelectToolAsync(t), "已选择 Tool " + t, "选择Tool");
- }
- private async void BtnJog_Click(object sender, RoutedEventArgs e)
- {
- var btn = sender as Button;
- var param = btn == null ? null : btn.Tag as string;
- if (_robot == null || string.IsNullOrEmpty(param) || param.Length < 2) return;
- string axis = param.Substring(0, 1);
- string dir = param.Substring(1, 1);
- double dist = Step * (dir == "+" ? 1 : -1);
- await RunAsync(() => _robot.JogAsync(axis, dist), "Jog " + param + " " + Step + "mm", "Jog");
- await GetPosAsync();
- }
- private async Task RunAsync(Func<Task<bool>> op, string okMsg, string errLabel)
- {
- if (_robot == null) return;
- try { await op(); Msg(okMsg); }
- catch (Exception ex) { Msg(errLabel + "异常: " + ex.Message); }
- }
- private async Task GetPosAsync()
- {
- if (_robot == null) return;
- try
- {
- var pos = await _robot.GetRobotPosAsync();
- if (pos != null)
- TxtPos.Text = string.Format("X: {0:F3} Y: {1:F3} Z: {2:F3}\nU: {3:F3} V: {4:F3} W: {5:F3}",
- pos.X, pos.Y, pos.Z, pos.U, pos.V, pos.W);
- }
- catch (Exception ex) { Msg("读取位置异常: " + ex.Message); }
- }
- }
- }
|