using Newtonsoft.Json; using PLCSharp.Core.Prism; using Prism.Commands; using Prism.Mvvm; using System.Collections.ObjectModel; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Windows; namespace PLCSharp.VVMs.Robots { /// /// 机器人 /// public class Robot : BindableBase { [NotMapped] public RobotModel Model { get; set; } /// /// 唯一标识 /// [Key] public Guid ID { get; set; } = Guid.NewGuid(); private string _Name; /// /// 机器人名称 /// public string Name { get { return _Name; } set { if (_Name != value) { Prompt = "已修改,请保存"; } SetProperty(ref _Name, value); } } private RobotType _Type; /// /// 机器人品牌类型 /// public RobotType Type { get { return _Type; } set { if (_Type == RobotType.Undefined) { SetProperty(ref _Type, value); } else { MessageBox.Show("选定型号后不可改变!"); } } } private string _IP; /// /// IP地址 /// public string IP { get { return _IP; } set { SetProperty(ref _IP, value); } } private int _Port; /// /// 控制器端口 /// public int Port { get { return _Port; } set { SetProperty(ref _Port, value); } } private int _CommanPort; /// /// 指令交互端口 /// public int CommanPort { get { return _CommanPort; } set { SetProperty(ref _CommanPort, value); } } private int _Speed = 100; /// /// 总速度百分比 1-100 /// public int Speed { get { return _Speed; } set { if (value > 100) value = 100; if (value < 1) value = 1; SetProperty(ref _Speed, value); } } private string _Comment; /// /// 备注 /// public string Comment { get { return _Comment; } set { if (_Comment != value) { Prompt = "已修改,请保存"; } SetProperty(ref _Comment, value); } } /// /// 序列化Params /// [Column("Params")] public string SerializedParams { get => JsonConvert.SerializeObject(Params); set => Params = JsonConvert.DeserializeObject(value); } private string _Prompt; /// /// 提示 /// [NotMapped] /// /// 提示 /// public string Prompt { get { return _Prompt; } set { SetProperty(ref _Prompt, value); } } private ObservableCollection _Status = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]; /// /// 状态 /// [NotMapped] public ObservableCollection Status { get { return _Status; } set { SetProperty(ref _Status, value); } } private ObservableCollection _StatusInfo = ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]; /// /// 状态信息 /// [NotMapped] /// /// 状态信息 /// public ObservableCollection StatusInfo { get { return _StatusInfo; } set { SetProperty(ref _StatusInfo, value); } } private RobotParams _Params; /// /// 参数集合 /// [NotMapped] public RobotParams Params { get { _Params ??= new RobotParams(); return _Params; } set { SetProperty(ref _Params, value); } } /// /// 机器人Params /// public class RobotParams : BindableBase { private RobotPoint _ToolP1; /// /// 工具P1 /// public RobotPoint ToolP1 { get { return _ToolP1; } set { SetProperty(ref _ToolP1, value); } } private RobotPoint _ToolP2; /// /// 工具P2 /// public RobotPoint ToolP2 { get { return _ToolP2; } set { SetProperty(ref _ToolP2, value); } } } private ObservableCollection _Points = []; /// /// 坐标点集合 /// [NotMapped] public ObservableCollection Points { get { return _Points; } set { SetProperty(ref _Points, value); } } private ObservableDictionary _Tools = []; /// /// Tools /// [NotMapped] public ObservableDictionary Tools { get { return _Tools; } set { SetProperty(ref _Tools, value); } } /// /// 序列化Tools /// [Column("Tools")] public string SerializedTools { get { var str = JsonConvert.SerializeObject(Tools); return str; } set => Tools = value != null ? JsonConvert.DeserializeObject>(value) : []; } private int _ToolTarget; /// /// 工具Target /// [NotMapped] public int ToolTarget { get { return _ToolTarget; } set { SetProperty(ref _ToolTarget, value); } } private RobotPoint _SelectedPoint; /// /// 选中点 /// [NotMapped] public RobotPoint SelectedPoint { get { return _SelectedPoint; } set { SetProperty(ref _SelectedPoint, value); } } private RobotPoint _CurrPoint = new(); /// /// Curr点位 /// [NotMapped] public RobotPoint CurrPoint { get { return _CurrPoint; } set { SetProperty(ref _CurrPoint, value); } } private RobotPoint _CurrToolPoint = new(); /// /// Curr工具点位 /// [NotMapped] public RobotPoint CurrToolPoint { get { return _CurrToolPoint; } set { SetProperty(ref _CurrToolPoint, value); } } private int _CurrTool; /// /// Curr工具 /// [NotMapped] public int CurrTool { get { return _CurrTool; } set { SetProperty(ref _CurrTool, value); } } private string _RcvShowInfo; /// /// Rcv显示信息 /// [NotMapped] public string RcvShowInfo { get { return _RcvShowInfo; } set { SetProperty(ref _RcvShowInfo, value); } } private string _ConnStatus; /// /// Conn状态 /// [NotMapped] public string ConnStatus { get { return _ConnStatus; } set { SetProperty(ref _ConnStatus, value); } } private string _ErrorInfo; /// /// 错误信息 /// [NotMapped] public string ErrorInfo { get { return _ErrorInfo; } set { SetProperty(ref _ErrorInfo, value); } } private DelegateCommand _StartCmd; /// /// 启动Cmd /// public DelegateCommand StartCmd => _StartCmd ??= new DelegateCommand(ExecuteStartCmd); void ExecuteStartCmd() { Start(); } /// /// 启动 /// public virtual void Start() { System.Diagnostics.Debug.WriteLine($"[{Name}] Start"); } private DelegateCommand _ResetCmd; /// /// 重置Cmd /// public DelegateCommand ResetCmd => _ResetCmd ??= new DelegateCommand(ExecuteResetCmd); void ExecuteResetCmd() { } /// /// 重置 /// public virtual void Reset() { } private DelegateCommand _StopCmd; /// /// 停止Cmd /// public DelegateCommand StopCmd => _StopCmd ??= new DelegateCommand(ExecuteStopCmd); void ExecuteStopCmd() { Stop(); } /// /// 停止 /// public virtual void Stop() { } /// /// 点位Command /// [NotMapped] public RobotPoint PointCommand { get; set; } /// /// 运行点位 /// /// 机器人点位 public virtual void RunPoint(string pointName) { } /// /// Jog /// /// 点位 /// 命令参数 /// dist /// rate /// 返回布尔值 public virtual void Jog(string pointName, string cmd, double dist, double rate = 0) { } /// /// 点位运行完成 /// [NotMapped] public bool PointDone { get; set; } private DelegateCommand _HomeCmd; /// /// 回零Cmd /// public DelegateCommand HomeCmd => _HomeCmd ??= new DelegateCommand(ExecuteHomeCmd); void ExecuteHomeCmd() { Home(); } /// /// 回零 /// public virtual void Home() { } private DelegateCommand _PowerCmd; /// /// PowerCmd /// public DelegateCommand PowerCmd => _PowerCmd ??= new DelegateCommand(ExecutePowerCmd); void ExecutePowerCmd(object cmd) { var cmdStr = cmd as string; Power(cmdStr); } /// /// Power /// /// 命令参数 public virtual void Power(string cmd) { } #region RobotMatrix private ObservableCollection _RobotMatrices = []; /// /// 机器人矩阵列表 /// [NotMapped] public ObservableCollection RobotMatrices { get { return _RobotMatrices; } set { SetProperty(ref _RobotMatrices, value); } } /// /// 根据矩阵配置生成点位网格(平行四边形插值) /// /// 矩阵配置 internal void Create(RobotMatrix matrix) { matrix.Points.Clear(); int xCount = matrix.XCount > 0 ? matrix.XCount : 1; int yCount = matrix.YCount > 0 ? matrix.YCount : 1; int total = xCount * yCount; // 从机器人的点位列表中查找三个角点 var startPoint = Points.FirstOrDefault(p => p.Name == matrix.StartName); var xEndPoint = Points.FirstOrDefault(p => p.Name == matrix.XEndName); var yEndPoint = Points.FirstOrDefault(p => p.Name == matrix.YEndName); if (startPoint == null || xEndPoint == null || yEndPoint == null) { Model.SendInfoDialog("基准点位名称错误,创建失败!"); return; } // 计算 X/Y/Z 方向基向量(平行四边形插值) double xBasisX = xCount > 1 ? (xEndPoint.X - startPoint.X) / (xCount - 1) : 0; double xBasisY = xCount > 1 ? (xEndPoint.Y - startPoint.Y) / (xCount - 1) : 0; double xBasisZ = xCount > 1 ? (xEndPoint.Z - startPoint.Z) / (xCount - 1) : 0; double yBasisX = yCount > 1 ? (yEndPoint.X - startPoint.X) / (yCount - 1) : 0; double yBasisY = yCount > 1 ? (yEndPoint.Y - startPoint.Y) / (yCount - 1) : 0; double yBasisZ = yCount > 1 ? (yEndPoint.Z - startPoint.Z) / (yCount - 1) : 0; double originX = startPoint.X; double originY = startPoint.Y; double originZ = startPoint.Z; // 按 MatrixType 确定遍历顺序 for (int i = 0; i < total; i++) { int x, y; if (matrix.MatrixType == 0) { x = i % xCount; y = i / xCount; } else { y = i % yCount; x = i / yCount; } // 平行四边形插值坐标(X/Y/Z 线性插值,U/V/W 取自起始点) double px = originX + x * xBasisX + y * yBasisX; double py = originY + x * xBasisY + y * yBasisY; double pz = originZ + x * xBasisZ + y * yBasisZ; var point = new RobotPoint { RobotID = matrix.RobotID, XIndex = x, YIndex = y, X = px, Y = py, Z = pz, U = startPoint.U, V = startPoint.V, W = startPoint.W, Rate = startPoint.Rate, Hand = startPoint.Hand, ToolNum = startPoint.ToolNum, }; matrix.Points.Add(point); } } public virtual void RunMatrixPoint(RobotMatrix matrix, int xTarget, int yTarget) { } #endregion } }