徐孝锋 8 месяцев назад
Родитель
Сommit
6ade23d2c6

+ 31 - 0
TeamAAS-VM/Core/Common.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Drawing;
 using System.Linq;
 using System.Net;
 using System.Text;
@@ -75,5 +76,35 @@ namespace TeamAAS_VP.Core
             }
             System.IO.File.WriteAllText(filePath, sb.ToString());
         }
+
+        /// <summary>
+        /// 计算两点之间的角度(弧度)
+        /// </summary>
+        /// <param name="p1"></param>
+        /// <param name="p2"></param>
+        /// <returns></returns>
+        public static double CalculateAngleBetweenPoints(PointF p1, PointF p2)
+        {
+            double deltaY = p2.Y - p1.Y;
+            double deltaX = p2.X - p1.X;
+
+            // 使用 Atan2 计算角度(弧度)
+            // Atan2 自动处理四个象限,返回值在 -π 到 π 之间
+            double angleRad = Math.Atan2(deltaY, deltaX);
+
+            return angleRad;
+        }
+
+        /// <summary>
+        /// 计算两点之间的角度(度)
+        /// </summary>
+        /// <param name="p1"></param>
+        /// <param name="p2"></param>
+        /// <returns></returns>
+        public static double CalculateAngleBetweenPointsDegrees(PointF p1, PointF p2)
+        {
+            double angleRad = CalculateAngleBetweenPoints(p1, p2);
+            return angleRad * (180.0 / Math.PI);
+        }
     }
 }

+ 20 - 0
TeamAAS-VM/Models/Product/ProductModel.cs

@@ -166,6 +166,16 @@ namespace TeamAAS_VP.Models
             set { SetProperty(ref _FixedUpCamera2ProcedureId, value); }
         }
 
+        /// <summary>
+        /// 固定向上的相机模板角度
+        /// </summary>
+        private double _FixedUpCameraTempletAngle;
+        public double FixedUpCameraTempletAngle
+        {
+            get { return _FixedUpCameraTempletAngle; }
+            set { SetProperty(ref _FixedUpCameraTempletAngle, value); }
+        }
+
         private Guid _FixedDownCamera1ProcedureId;
         /// <summary>
         /// 固定向下相机取料流程ID
@@ -226,6 +236,16 @@ namespace TeamAAS_VP.Models
             set { SetProperty(ref _MoveDownCamer2PickPhotoProcedureId, value); }
         }
 
+        /// <summary>
+        /// 移动向上的相机模板角度
+        /// </summary>
+        private double _MoveDownCamerTempletAngle;
+        public double MoveDownCamerTempletAngle
+        {
+            get { return _MoveDownCamerTempletAngle; }
+            set { SetProperty(ref _MoveDownCamerTempletAngle, value); }
+        }
+
         private ObservableCollection<ProductGlobalParam> _GlobalParamListCollection;
         /// <summary>
         /// 产品全局参数集合

+ 112 - 33
TeamAAS-VM/ViewModels/Product/PlcPointParamsViewModel.cs

@@ -47,7 +47,6 @@ namespace TeamAAS_VP.ViewModels.Product
         ICalibrationService _calibrationService;
         ICameraService _cameraService;
         IProductService _productService;
-        CoordinateTransformer local=null;
 
         #region 属性
 
@@ -301,10 +300,12 @@ namespace TeamAAS_VP.ViewModels.Product
         public CameraInfo SelectedCamera
         {
             get { return _SelectedCamera; }
-            set { SetProperty(ref _SelectedCamera, value);
-                if (value!=null)
+            set
+            {
+                SetProperty(ref _SelectedCamera, value);
+                if (value != null)
                 {
-                    if(Camera!=null)
+                    if (Camera != null)
                     {
                         if (Camera.IsGrabbing)
                         {
@@ -344,7 +345,9 @@ namespace TeamAAS_VP.ViewModels.Product
         public bool IsLeftDrawerOpen
         {
             get { return _IsLeftDrawerOpen; }
-            set { SetProperty(ref _IsLeftDrawerOpen, value);
+            set
+            {
+                SetProperty(ref _IsLeftDrawerOpen, value);
                 if (!value)
                 {
                     if (IsGrap)
@@ -355,7 +358,7 @@ namespace TeamAAS_VP.ViewModels.Product
             }
         }
 
-        private int _Inhal=1;
+        private int _Inhal = 1;
         public int Inhal
         {
             get { return _Inhal; }
@@ -429,7 +432,7 @@ namespace TeamAAS_VP.ViewModels.Product
 
         private DelegateCommand _StartGrabbingCommand;
         public DelegateCommand StartGrabbingCommand =>
-            _StartGrabbingCommand ?? (_StartGrabbingCommand = new DelegateCommand(ExecuteStartGrabbingCommand, CanExecuteStartGrabbingCommand).ObservesProperty(() => IsGrap).ObservesProperty(()=> SelectedCamera));
+            _StartGrabbingCommand ?? (_StartGrabbingCommand = new DelegateCommand(ExecuteStartGrabbingCommand, CanExecuteStartGrabbingCommand).ObservesProperty(() => IsGrap).ObservesProperty(() => SelectedCamera));
 
         private DelegateCommand _StopGrabbingCommand;
         public DelegateCommand StopGrabbingCommand =>
@@ -455,7 +458,7 @@ namespace TeamAAS_VP.ViewModels.Product
 
         private DelegateCommand _GoPickCommand;
         public DelegateCommand GoPickCommand =>
-            _GoPickCommand ?? (_GoPickCommand = new DelegateCommand(ExecuteGoPickCommand, CanExecuteCommandGoPickCommandName).ObservesProperty(()=>SelectedPoint));
+            _GoPickCommand ?? (_GoPickCommand = new DelegateCommand(ExecuteGoPickCommand, CanExecuteCommandGoPickCommandName).ObservesProperty(() => SelectedPoint));
 
         private DelegateCommand _GoRepositionCommand;
         public DelegateCommand GoRepositionCommand =>
@@ -465,7 +468,11 @@ namespace TeamAAS_VP.ViewModels.Product
         public DelegateCommand TakeProductPhotoCommand =>
             _TakeProductPhotoCommand ?? (_TakeProductPhotoCommand = new DelegateCommand(ExecuteTakeProductPhotoCommand, CanExecuteCommandGoPickCommandName).ObservesProperty(() => SelectedPoint));
 
-        
+        private DelegateCommand _TakeProductCommand;
+        public DelegateCommand TakeProductCommand =>
+            _TakeProductCommand ?? (_TakeProductCommand = new DelegateCommand(ExecuteTakeProductCommand, CanExecuteCommandGoPickCommandName).ObservesProperty(() => SelectedPoint));
+
+
         #endregion
 
         #region 事件
@@ -660,9 +667,9 @@ namespace TeamAAS_VP.ViewModels.Product
                 if (SelectedIndex == 4)
                 {
                     //如果local为空,则必须先通过相机定位产品,否则无法示教锁付点位
-                    if (local == null)
+                    if (toolCoord == null)
                     {
-                        _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "当前模式不允许直接示教锁付点位,请先通过相机定位产品!", Duration = 1 });
+                        _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "当前模式不允许直接示教贴敷点位,请先通过相机定位产品和定位辅料!", Duration = 1 });
                         return;
                     }
 
@@ -674,12 +681,13 @@ namespace TeamAAS_VP.ViewModels.Product
 
                     var curpos1 = await Robot.GetRobotPosAsync();
                     //将当前点位转换为产品Local坐标系下的点位
-                    var localPoint = local.ToNewCoord(curpos1.X, curpos1.Y);
-                    SelectedPoint.X_Position = (float)localPoint[0];
-                    SelectedPoint.Y_Position = (float)localPoint[1];
+                    SelectedPoint.X_Position = (float)curpos1.X;
+                    SelectedPoint.Y_Position = (float)curpos1.Y;
                     SelectedPoint.Z_Position_Start = curpos1.Z;
                     SelectedPoint.U_Position = curpos1.U;
                     SelectedPoint.R_Position = curpos1.V;
+                    SelectProduct.FixedUpCameraTempletAngle = _FixedUpCameraTempletAngle;
+                    SelectProduct.MoveDownCamerTempletAngle = _MoveCameraTempletAngle;
                     _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
                     return;
                 }
@@ -1041,7 +1049,7 @@ namespace TeamAAS_VP.ViewModels.Product
         /// </summary>
         void ExecuteStopGrabbingCommand()
         {
-            
+
             if (Camera != null)
                 Camera.StopGrabbing();
             IsGrap = false;
@@ -1109,7 +1117,7 @@ namespace TeamAAS_VP.ViewModels.Product
             catch (Exception ex)
             {
                 LogHelper.WriteLogError("附加轴点动+时出错!", ex);
-                _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg =ex.Message, Duration = 0.4 });
+                _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 0.4 });
             }
         }
 
@@ -1223,11 +1231,11 @@ namespace TeamAAS_VP.ViewModels.Product
                 };
                 await Robot.CalibMotionAsync(rpoint, 0);
 
-                await Robot.CalibParameAsync(new PickInfo() { Inhal= Inhal,Blow=1},20,20,true,200,200);
+                await Robot.CalibParameAsync(new PickInfo() { Inhal = Inhal, Blow = 1 }, 20, 20, true, 200, 200);
                 //打开吸真空
                 await Robot.CalibOutIOAsync(true);
 
-               await Task.Delay(500);
+                await Task.Delay(500);
 
                 rpoint.Z = 0;
                 await Robot.CalibMotionAsync(rpoint, 0);
@@ -1254,6 +1262,9 @@ namespace TeamAAS_VP.ViewModels.Product
 
 
         ToolCoord toolCoord = null;
+        double _FixedUpCameraTempletAngle = 0;
+        double _MoveCameraTempletAngle = 0;
+        PointF _PutPoint = new PointF();
         /// <summary>
         /// 去二次定位
         /// </summary>
@@ -1322,12 +1333,9 @@ namespace TeamAAS_VP.ViewModels.Product
                 var visionResults = await Task.WhenAll(task1, task2);
                 if (visionResults[0].IsSucceed && visionResults[1].IsSucceed)
                 {
-                    float tool1X = (float)(visionResults[0].X - visionResults[0].X);
-                    float tool1Y = (float)(visionResults[0].Y - visionResults[0].Y);
-                    float tool2X = (float)(visionResults[1].Y - visionResults[1].Y);
-                    float tool2Y = (float)(visionResults[1].Y - visionResults[1].Y);
-                    float toolX = (tool1X + tool2X) / 2;
-                    float toolY = (tool1Y + tool2Y) / 2;
+                    float toolX = (float)(visionResults[0].X + visionResults[1].X) / 2;
+                    float toolY = (float)(visionResults[0].Y + visionResults[1].Y) / 2;
+                    _FixedUpCameraTempletAngle = Common.CalculateAngleBetweenPointsDegrees(new PointF((float)visionResults[0].X, (float)visionResults[0].Y), new PointF((float)visionResults[1].X, (float)visionResults[1].Y));
                     toolCoord = new ToolCoord();
                     toolCoord.SetTool(toolX, toolY);
                     _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "视觉定位成功!", Duration = 0.4 });
@@ -1435,12 +1443,10 @@ namespace TeamAAS_VP.ViewModels.Product
                 var visionResults = await Task.WhenAll(task1, task2);
                 if (visionResults[0].IsSucceed && visionResults[1].IsSucceed)
                 {
-                    float tool1X = (float)(visionResults[0].X - visionResults[0].X);
-                    float tool1Y = (float)(visionResults[0].Y - visionResults[0].Y);
-                    float tool2X = (float)(visionResults[1].Y - visionResults[1].Y);
-                    float tool2Y = (float)(visionResults[1].Y - visionResults[1].Y);
-                    float toolX = (tool1X + tool2X) / 2;
-                    float toolY = (tool1Y + tool2Y) / 2;
+                    float centerX = (float)(visionResults[0].X + visionResults[1].X) / 2;
+                    float centerY = (float)(visionResults[0].Y + visionResults[1].Y) / 2;
+                    _MoveCameraTempletAngle = Common.CalculateAngleBetweenPointsDegrees(new PointF((float)visionResults[0].X, (float)visionResults[0].Y), new PointF((float)visionResults[1].X, (float)visionResults[1].Y));
+                    _PutPoint = new PointF(centerX, centerY);
 
                     if (DialogHost.IsDialogOpen("RootDialog"))
                     {
@@ -1471,6 +1477,79 @@ namespace TeamAAS_VP.ViewModels.Product
                 }
             }
         }
+
+        /// <summary>
+        /// 执行产品贴敷
+        /// </summary>
+        async void ExecuteTakeProductCommand()
+        {
+            try
+            {
+                if (SelectedPoint == null)
+                {
+                    return;
+                }
+                if (Robot == null || !Robot.IsConnected)
+                {
+                    _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "机器人未连接,无法执行产品贴敷!", Duration = 1 });
+                    return;
+                }
+                if (MessageBox.Show("确认要执行产品贴敷吗?", "去产品贴敷", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
+                {
+                    return;
+                }
+
+                //如果二次定位的工具坐标为空,则提示用户先进行二次定位
+                if (toolCoord == null)
+                {
+                    _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "二次定位的工具坐标为空,无法执行产品拍照!", Duration = 1 });
+                    return;
+                }
+
+                if (_PutPoint.X == 0 && _PutPoint.Y == 0)
+                {
+                    _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "检测到未定位产品", Duration = 1 });
+                    return;
+                }
+
+                var waiting = new WaitingControl();
+                //show the dialog
+                var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
+
+                var pn = Vector<double>.Build.Dense(new double[] { _PutPoint.X, _PutPoint.Y });
+                //目标角度:
+                double angle = SelectedPoint.U_Position + (_FixedUpCameraTempletAngle - SelectProduct.FixedUpCameraTempletAngle) + (_MoveCameraTempletAngle - SelectProduct.MoveDownCamerTempletAngle);
+                //将_PutPoint转换为Tool0下要走的点
+                var p0 = toolCoord.GetTool0Coord(pn, angle);
+
+                // 1. 控制机器人移动至拍照点位
+                var rpoint = new RPoint()
+                {
+                    X = (float)p0[0],
+                    Y = (float)p0[1],
+                    Z = SelectedPoint.Z_Position_Start,
+                    U = (float)angle,
+                    V = SelectedPoint.R_Position,
+                };
+                await Robot.CalibMotionAsync(rpoint, 0);
+                if (DialogHost.IsDialogOpen("RootDialog"))
+                {
+                    DialogHost.Close("RootDialog");
+                }
+                await task;
+                return;
+
+            }
+            catch (Exception ex)
+            {
+                LogHelper.WriteLogError("执行产品贴敷时出错!", ex);
+                _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
+                if (DialogHost.IsDialogOpen("RootDialog"))
+                {
+                    DialogHost.Close("RootDialog");
+                }
+            }
+        }
         #endregion
 
         #region 继承
@@ -1494,7 +1573,7 @@ namespace TeamAAS_VP.ViewModels.Product
             try
             {
                 toolCoord = null;
-                local = null;
+                _PutPoint = new PointF();
                 SelectProduct = navigationContext.Parameters.GetValue<ProductModel>("SelectProduct");
 
                 SelectedIndex = 0;
@@ -1534,7 +1613,7 @@ namespace TeamAAS_VP.ViewModels.Product
                 LockScrewProgramNumber = SelectProduct.ScrewPoints.FirstOrDefault()?.ScrewProNum ?? 1;
 
                 CameraList = new ObservableCollection<CameraInfo>(_configService.GetAllCameras());
-               
+
             }
             catch (Exception ex)
             {
@@ -1572,7 +1651,7 @@ namespace TeamAAS_VP.ViewModels.Product
                         Camera.StopGrabbing();
                     }
                     Camera.ImageCallbackEvent -= Camera_ImageCallbackEvent;
-                    
+
                     Camera = null;
                 }
                 SelectedCamera = null;

+ 18 - 8
TeamAAS-VM/ViewModels/Product/ProductParamsViewModel.cs

@@ -254,18 +254,28 @@ namespace TeamAAS_VP.ViewModels.Product
                 var waiting = new WaitingControl();
                 //show the dialog
                 var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
-                await _productService.UpdateProductAsync(SelectProduct);
-                var current = _productService.GetCurrentProduct();
-                if (current != null && current.IsLoad)
+                try
                 {
-                    await management.WritePositionToPlcAsync();
+                    await _productService.UpdateProductAsync(SelectProduct);
+                    var current = _productService.GetCurrentProduct();
+                    if (current != null && current.IsLoad)
+                    {
+                        await management.WritePositionToPlcAsync();
+                    }
+                    if (DialogHost.IsDialogOpen("RootDialog"))
+                    {
+                        DialogHost.Close("RootDialog");
+                    }
+                    await task;
                 }
-                if (DialogHost.IsDialogOpen("RootDialog"))
+                catch (Exception)
                 {
-                    DialogHost.Close("RootDialog");
+                    if (DialogHost.IsDialogOpen("RootDialog"))
+                    {
+                        DialogHost.Close("RootDialog");
+                    }
+                    await task;
                 }
-                await task;
-
             }
             NavigationParameters param = new NavigationParameters();
             param.Add("SelectProduct", SelectProduct);

+ 30 - 3
TeamAAS-VM/Views/Product/PlcPointParams.xaml

@@ -390,7 +390,7 @@
                                             <Setter Property="Visibility"
                                                     Value="Collapsed" />
                                             <Style.Triggers>
-                                                <!--当 SelectedIndex == 1 时显示此面板-->
+                                                <!--当 SelectedIndex == 2 时显示此面板-->
                                                 <DataTrigger Binding="{Binding SelectedIndex}"
                                                              Value="2">
                                                     <Setter Property="Visibility"
@@ -420,7 +420,7 @@
                                             <Setter Property="Visibility"
                                                     Value="Collapsed" />
                                             <Style.Triggers>
-                                                <!--当 SelectedIndex == 1 时显示此面板-->
+                                                <!--当 SelectedIndex == 3 时显示此面板-->
                                                 <DataTrigger Binding="{Binding SelectedIndex}"
                                                              Value="3">
                                                     <Setter Property="Visibility"
@@ -440,7 +440,34 @@
                                         </StackPanel>
                                     </ScrollViewer>
                                 </StackPanel>
-
+                                <!--产品贴敷-->
+                                <StackPanel Orientation="Vertical"
+                                            d:Visibility="Visible">
+                                    <StackPanel.Style>
+                                        <Style TargetType="StackPanel">
+                                            <Setter Property="Visibility"
+                                                    Value="Collapsed" />
+                                            <Style.Triggers>
+                                                <!--当 SelectedIndex == 4 时显示此面板-->
+                                                <DataTrigger Binding="{Binding SelectedIndex}"
+                                                             Value="4">
+                                                    <Setter Property="Visibility"
+                                                            Value="Visible" />
+                                                </DataTrigger>
+                                            </Style.Triggers>
+                                        </Style>
+                                    </StackPanel.Style>
+                                    <ScrollViewer HorizontalScrollBarVisibility="Auto"
+                                                  VerticalScrollBarVisibility="Disabled">
+                                        <StackPanel Orientation="Horizontal"
+                                                    HorizontalAlignment="Left">
+                                            <Button Content="执行产品贴敷"
+                                                    Margin="10,0,0,0"
+                                                    materialDesign:ButtonAssist.CornerRadius="5"
+                                                    Command="{Binding TakeProductCommand}" />
+                                        </StackPanel>
+                                    </ScrollViewer>
+                                </StackPanel>
 
 
                             </StackPanel>