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.

729 lines
23 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using GummingCommon;
using GummingEntity;
using GummingSupport;
using GummingControl;
using System.Text;
using GummingBusiness;
using Newtonsoft.Json;
using GummingLine;
using System.Threading.Tasks;
namespace Gumming
{
public class ControlCOTArmViewModule : ViewModelBase
{
#region base parameters
private int rows = 20;
private int pagenumber = 1;
public ControlCOTArmView View { get; set; }
private bool IsConfirm;
private bool isLoading;
private System.Timers.Timer timer;
private HardFormulaFlowEntity HomeFlow;
private bool Homing = false;
private int MovingPostion = 0;
#endregion
#region constructor
public ControlCOTArmViewModule()
{
View = new ControlCOTArmView();
View.DataContext = this;
InitializeCommands();
InitializeParameters();
}
private void InitializeCommands()
{
CancelCommand = new DelegateCommand(OnCancelCommand);
SVONCommand = new DelegateCommand(OnSVONCommand);
UpDownCommand = new DelegateCommand(OnUpDownCommand);
//JogForwardCommand = new DelegateCommand(OnJogForwardCommand);
//JogBackCommand = new DelegateCommand(OnJogBackCommand);
HomeCommand = new DelegateCommand(OnHomeCommand);
PosMoveCommand = new DelegateCommand(OnPosMoveCommand);
}
public override void InitializeParameters(object content = null)
{
IsConfirm = false;
WindowTitle = "";
//*
if (timer == null)
{
timer = new System.Timers.Timer();
timer.Interval = 300;
timer.Elapsed += timer_Elapsed;
timer.Start();
}//*/
}
public void ShowDialog()
{
IsConfirm = false;
View.ShowDialog();
}
~ControlCOTArmViewModule()
{
}
public void LoadDefaultValue()
{
SafeStart = Common.ToDecimal(HardParameters.Read(StationCode + (IsPrArm ? Constant.PrSafeStart : Constant.EbrSafeStart)));
SafeEnd = Common.ToDecimal(HardParameters.Read(StationCode + (IsPrArm ? Constant.PrSafeEnd : Constant.EbrSafeEnd)));
AxisSpeed = Common.ToDecimal(HardParameters.Read(StationCode + (IsPrArm ? Constant.PrAxisSpeed : Constant.EbrAxisSpeed)));
string postionkey = (IsPrArm ? string.Format("{0}_Arm1PRHomingOffset", StationCode) : string.Format("{0}_Arm2EBRHomingOffset", StationCode));
var postionSet = Global.Settings.FirstOrDefault(q => q.RecId == postionkey);
HomePosition = postionSet.SettingValue;
HomeSpeed = Common.ToDecimal(HardParameters.Read(StationCode + (IsPrArm ? Constant.PrHomeSpeed : Constant.EbrHomeSpeed)));
MovePosition = Common.ToDecimal(HardParameters.Read(StationCode + (IsPrArm ? Constant.PrMovePosition : Constant.EbrMovePosition)));
MoveSpeed = Common.ToDecimal(HardParameters.Read(StationCode + (IsPrArm ? Constant.PrMoveSpeed : Constant.EbrMoveSpeed)));
//Ĭ<><C4AC>Arm<72><6D><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ArmFlow = (IsPrArm ? HardParameters.GetCotPrAxis(StationCode) : HardParameters.GetCotEbrAxis(StationCode));
HomeFlow = (!IsPrArm ? HardParameters.GetCotPrAxis(StationCode) : HardParameters.GetCotEbrAxis(StationCode));
//<2F><><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>״̬
var axis = Global.Axises.FirstOrDefault(q => q.NodeId == ArmFlow.NodeId && q.SlotId == ArmFlow.SlotId);
if (axis != null)
{
SVONStatus = axis.Energy;
}
}
#endregion
#region Binding Properties
public string WindowTitle { get; private set; }
public bool IsPrArm { get; set; }
private ushort _MainAxisTopStatus;
public ushort MainAxisTopStatus
{
get
{
return _MainAxisTopStatus;
}
set
{
_MainAxisTopStatus = value;
OnPropertyChanged("MainAxisTopStatus");
}
}
private string _HeaderTitle;
public string HeaderTitle
{
get
{
return _HeaderTitle;
}
set
{
_HeaderTitle = value;
OnPropertyChanged("HeaderTitle");
}
}
private string _StationCode;
public string StationCode
{
get
{
return _StationCode;
}
set
{
_StationCode = value;
OnPropertyChanged("StationCode");
}
}
private ushort _SVONStatus;
public ushort SVONStatus
{
get
{
return _SVONStatus;
}
set
{
_SVONStatus = value;
OnPropertyChanged("SVONStatus");
}
}
private ushort _ForwardStatus;
public ushort ForwardStatus
{
get
{
return _ForwardStatus;
}
set
{
_ForwardStatus = value;
OnPropertyChanged("ForwardStatus");
}
}
private ushort _BackStatus;
public ushort BackStatus
{
get
{
return _BackStatus;
}
set
{
_BackStatus = value;
OnPropertyChanged("BackStatus");
}
}
private ushort _UpDownStatus;
public ushort UpDownStatus
{
get
{
return _UpDownStatus;
}
set
{
_UpDownStatus = value;
OnPropertyChanged("UpDownStatus");
}
}
private decimal _CurPosition;
public decimal CurPosition
{
get
{
return _CurPosition;
}
set
{
_CurPosition = value;
OnPropertyChanged("CurPosition");
}
}
private decimal _AxisSpeed;
public decimal AxisSpeed
{
get
{
return _AxisSpeed;
}
set
{
_AxisSpeed = value;
OnPropertyChanged("AxisSpeed");
}
}
private decimal _HomePosition;
public decimal HomePosition
{
get
{
return _HomePosition;
}
set
{
_HomePosition = value;
OnPropertyChanged("HomePosition");
}
}
private decimal _HomeSpeed;
public decimal HomeSpeed
{
get
{
return _HomeSpeed;
}
set
{
_HomeSpeed = value;
OnPropertyChanged("HomeSpeed");
}
}
private decimal _MovePosition;
public decimal MovePosition
{
get
{
return _MovePosition;
}
set
{
_MovePosition = value;
OnPropertyChanged("MovePosition");
}
}
private decimal _MoveSpeed;
public decimal MoveSpeed
{
get
{
return _MoveSpeed;
}
set
{
_MoveSpeed = value;
OnPropertyChanged("MoveSpeed");
}
}
private decimal _SafeStart;
public decimal SafeStart
{
get
{
return _SafeStart;
}
set
{
_SafeStart = value;
OnPropertyChanged("SafeStart");
}
}
private decimal _SafeEnd;
public decimal SafeEnd
{
get
{
return _SafeEnd;
}
set
{
_SafeEnd = value;
OnPropertyChanged("SafeEnd");
}
}
private string _ControlName;
public string ControlName
{
get
{
return _ControlName;
}
set
{
_ControlName = value;
OnPropertyChanged("ControlName");
}
}
private HardFormulaFlowEntity _ArmFlow;
public HardFormulaFlowEntity ArmFlow
{
get
{
return _ArmFlow;
}
set
{
_ArmFlow = value;
OnPropertyChanged("ArmFlow");
}
}
private bool _IsSub1Open;
public bool IsSub1Open
{
get { return _IsSub1Open; }
set
{
_IsSub1Open = value;
OnPropertyChanged("IsSub1Open");
}
}
private bool _IsSub2Open;
public bool IsSub2Open
{
get { return _IsSub2Open; }
set
{
_IsSub2Open = value;
OnPropertyChanged("IsSub2Open");
}
}
private bool _IsSub3Open;
public bool IsSub3Open
{
get { return _IsSub3Open; }
set
{
_IsSub3Open = value;
OnPropertyChanged("IsSub3Open");
}
}
private ushort _HOMEStatus;
public ushort HOMEStatus
{
get
{
return _HOMEStatus;
}
set
{
_HOMEStatus = value;
OnPropertyChanged("HOMEStatus");
}
}
#endregion
#region Binding Commands
private ICommand _CancelCommand;
public ICommand CancelCommand
{
get
{
return _CancelCommand;
}
set
{
_CancelCommand = value;
OnPropertyChanged("CancelCommand");
}
}
private ICommand _PosMoveCommand;
public ICommand PosMoveCommand
{
get
{
return _PosMoveCommand;
}
set
{
_PosMoveCommand = value;
OnPropertyChanged("PosMoveCommand");
}
}
private ICommand _HomeCommand;
public ICommand HomeCommand
{
get
{
return _HomeCommand;
}
set
{
_HomeCommand = value;
OnPropertyChanged("HomeCommand");
}
}
private ICommand _JogBackCommand;
public ICommand JogBackCommand
{
get
{
return _JogBackCommand;
}
set
{
_JogBackCommand = value;
OnPropertyChanged("JogBackCommand");
}
}
private ICommand _JogForwardCommand;
public ICommand JogForwardCommand
{
get
{
return _JogForwardCommand;
}
set
{
_JogForwardCommand = value;
OnPropertyChanged("JogForwardCommand");
}
}
private ICommand _UpDownCommand;
public ICommand UpDownCommand
{
get
{
return _UpDownCommand;
}
set
{
_UpDownCommand = value;
OnPropertyChanged("UpDownCommand");
}
}
private ICommand _SVONCommand;
public ICommand SVONCommand
{
get
{
return _SVONCommand;
}
set
{
_SVONCommand = value;
OnPropertyChanged("SVONCommand");
}
}
#endregion
#region Private Methods
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
int position = 0;
if (ArmFlow != null && !Helpering.IsTerminate && this.IsActiveChildPage)
{
DeviceControl.GetPosition(ArmFlow.NodeId, ArmFlow.SlotId, ref position);
if (Homing && ControlCheck.IsHomeDone(ArmFlow.NodeId, ArmFlow.SlotId))//<2F>ָ<EFBFBD>Home״̬
{
HOMEStatus = 0;
Homing = false;
}
if (MovingPostion == position)
{
MainAxisTopStatus = 0;
}
ushort Datasize = 4;
byte[] Data = new byte[4];
DeviceControl.ReadPDOParameter(ArmFlow.NodeId, 0, Global.PositionSign, 0, Datasize, ref Data[0]);
int c = TypeHelper.ByteArrayToInt(Data);
string values = Convert.ToString(c, 2);
IsSub1Open = false;
IsSub2Open = false;
IsSub3Open = false;
//ԭ<><D4AD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD>
if (values.Length > 0)
{
IsSub3Open = values.Substring(values.Length - 1) == "1";
}
//<2F>м为<D0BC><E4B8BA>
if (values.Length > 1)
{
IsSub2Open = values.Substring(values.Length - 2, 1) == "1";
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (values.Length > 2)
{
IsSub1Open = values.Substring(values.Length - 3, 1) == "1";
}
}
View.Dispatcher.BeginInvoke(new Action(() =>
{
CurPosition = BusinessHelper.ConvertFromPrDegree(StationCode, (double)position, IsPrArm);
}), null);
}
private void OnCancelCommand(object sender)
{
View.DialogResult = true;
}
private void OnSVONCommand(Object sender)
{
if (ArmFlow != null)
{
SVONStatus = (ushort)(SVONStatus != 1 ? 1 : 0);
DeviceControl.SetSvon(Common.ToUInt16(ArmFlow.NodeId), Common.ToUInt16(ArmFlow.SlotId), SVONStatus);
}
}
private void OnUpDownCommand(Object sender)
{
var flow = (IsPrArm ? HardParameters.GetCotPrArmIO(StationCode) : HardParameters.GetCotEbrArmIO(StationCode));
if (flow != null)
{
UpDownStatus = (ushort)(UpDownStatus != 1 ? 1 : 0);
ControlCheck.PMCInvoke(false, "SetPortStatus", null, () => DeviceControl.SetPortStatus(flow.NodeId, flow.SlotId, flow.BitNum, UpDownStatus));
}
}
public void OnJogForwardStart()
{
if (!ControlCheck.IsHomeDone(HomeFlow.NodeId, HomeFlow.SlotId))
{
ShowErrorBox(string.Format("<22><>ȷ<EFBFBD><C8B7>{0}<7D><>ԭ<EFBFBD><D4AD>λ<EFBFBD><CEBB>", IsPrArm ? <>߱ۿ<DFB1><DBBF><EFBFBD>" : "<22><><EFBFBD>ۿ<EFBFBD><DBBF><EFBFBD>"));
return;
}
if (ArmFlow != null)
{
int axisSpeed = BusinessHelper.ConvertPrDegree(StationCode, (double)AxisSpeed, IsPrArm);
double axisAcc = Global.ArmAcc;
SVONStatus = 1;
if (ControlCheck.PMCInvoke(false, "SetSvon", null, () => DeviceControl.SetSvon(ArmFlow.NodeId, ArmFlow.SlotId, SVONStatus)))
{
ForwardStatus = 1;
//<2F><><EFBFBD><EFBFBD><EFBFBD>˶<EFBFBD>
ControlCheck.PMCInvoke(false, "StartMove", null, () => DeviceControl.StartMove(ArmFlow.NodeId, ArmFlow.SlotId, 0, axisSpeed, axisSpeed, axisAcc));
}
}
}
public void OnJogForwardStop()
{
if (ArmFlow != null)
{
int axisSpeed = BusinessHelper.ConvertPrDegree(StationCode, (double)AxisSpeed, IsPrArm);
double axisAcc = Global.ArmAcc;
SVONStatus = 1;
if (ControlCheck.PMCInvoke(false, "SetSvon", null, () => DeviceControl.SetSvon(ArmFlow.NodeId, ArmFlow.SlotId, SVONStatus)))
{
ForwardStatus = 0;
//<2F><><EFBFBD><EFBFBD><EFBFBD>˶<EFBFBD>
ControlCheck.PMCInvoke(false, "StopMove", null, () => DeviceControl.StopMove(ArmFlow.NodeId, ArmFlow.SlotId, axisAcc));
}
}
}
public void OnJogBackStart()
{
if (!ControlCheck.IsHomeDone(HomeFlow.NodeId, HomeFlow.SlotId))
{
ShowErrorBox(string.Format("<22><>ȷ<EFBFBD><C8B7>{0}<7D><>ԭ<EFBFBD><D4AD>λ<EFBFBD><CEBB>", IsPrArm ? <>߱ۿ<DFB1><DBBF><EFBFBD>" : "<22><><EFBFBD>ۿ<EFBFBD><DBBF><EFBFBD>"));
return;
}
if (ArmFlow != null)
{
int axisSpeed = BusinessHelper.ConvertPrDegree(StationCode, (double)AxisSpeed, IsPrArm);
double axisAcc = Global.ArmAcc;
SVONStatus = 1;
if (ControlCheck.PMCInvoke(false, "SetSvon", null, () => DeviceControl.SetSvon(ArmFlow.NodeId, ArmFlow.SlotId, SVONStatus)))
{
BackStatus = 1;
//<2F><><EFBFBD><EFBFBD><EFBFBD>˶<EFBFBD>
ControlCheck.PMCInvoke(false, "StartMove", null, () => DeviceControl.StartMove(ArmFlow.NodeId, ArmFlow.SlotId, 1, axisSpeed, axisSpeed, axisAcc));
}
}
}
public void OnJogBackStop()
{
if (ArmFlow != null)
{
int axisSpeed = BusinessHelper.ConvertPrDegree(StationCode, (double)AxisSpeed, IsPrArm);
double axisAcc = Global.ArmAcc;
SVONStatus = 1;
if (ControlCheck.PMCInvoke(false, "SetSvon", null, () => DeviceControl.SetSvon(ArmFlow.NodeId, ArmFlow.SlotId, SVONStatus)))
{
BackStatus = 0;
//<2F><><EFBFBD><EFBFBD><EFBFBD>˶<EFBFBD>
ControlCheck.PMCInvoke(false, "StopMove", null, () => DeviceControl.StopMove(ArmFlow.NodeId, ArmFlow.SlotId, axisAcc));
}
}
}
private void OnPosMoveCommand(Object sender)
{
if (!ControlCheck.IsHomeDone(HomeFlow.NodeId, HomeFlow.SlotId))
{
ShowErrorBox(string.Format("<22><>ȷ<EFBFBD><C8B7>{0}<7D><>ԭ<EFBFBD><D4AD>λ<EFBFBD><CEBB>", IsPrArm ? <>߱ۿ<DFB1><DBBF><EFBFBD>" : "<22><><EFBFBD>ۿ<EFBFBD><DBBF><EFBFBD>"));
return;
}
if (ArmFlow != null)
{
int axisSpeed = BusinessHelper.ConvertPrDegree(StationCode, (double)AxisSpeed, IsPrArm);
int axisPostion = BusinessHelper.ConvertPrDegree(StationCode, (double)MovePosition, IsPrArm);
double axisAcc = Global.ArmAcc;
SVONStatus = 1;
if (ControlCheck.PMCInvoke(false, "SetSvon", null, () => DeviceControl.SetSvon(ArmFlow.NodeId, ArmFlow.SlotId, SVONStatus)))
{
if (MainAxisTopStatus != 1)
{
MovingPostion = axisPostion;
MainAxisTopStatus = 1;
//<2F><><EFBFBD><EFBFBD><EFBFBD>˶<EFBFBD>
ControlCheck.PMCInvoke(false, "StartMove", null, () => DeviceControl.StartMove(ArmFlow.NodeId, ArmFlow.SlotId, axisPostion, axisSpeed, axisSpeed, 0, (uint)axisAcc, (uint)axisAcc, 1));
}
else
{
MainAxisTopStatus = 0;
//ֹͣ<CDA3>˶<EFBFBD>
ControlCheck.PMCInvoke(false, "StopMove", null, () => DeviceControl.StopMove(ArmFlow.NodeId, ArmFlow.SlotId, Global.ArmAcc));
}
}
}
}
private void OnHomeCommand(Object sender)
{
if (ArmFlow != null)
{
SVONStatus = 1;
if (ControlCheck.PMCInvoke(false, "SetSvon", null, () => DeviceControl.SetSvon(ArmFlow.NodeId, ArmFlow.SlotId, SVONStatus)))
{
if (HOMEStatus != 1)
{
Homing = true;
HOMEStatus = 1;
uint homeSpeed = (uint)BusinessHelper.ConvertPrDegree(StationCode, (double)HomeSpeed, IsPrArm);
int homePostion = BusinessHelper.ConvertPrDegree(StationCode, (double)HomePosition, IsPrArm);
ControlCheck.PMCInvoke(false, "StartMove", null, () => DeviceControl.GoArmHome(Global.ArmHomeWay, ArmFlow.NodeId, ArmFlow.SlotId, homePostion, homeSpeed));
}
else
{
HOMEStatus = 0;
//<2F><><EFBFBD><EFBFBD><EFBFBD>˶<EFBFBD>
ControlCheck.PMCInvoke(false, "StopMove", null, () => DeviceControl.StopMove(ArmFlow.NodeId, ArmFlow.SlotId, Global.StopAcc));
}
}
}
}
public void OnSafeStartTextChangedCommand(string value)
{
HardParameterDA.Save(StationCode + (IsPrArm ? Constant.PrSafeStart : Constant.EbrSafeStart), Convert.ToString(value));
}
public void OnSafeEndTextChangedCommand(string value)
{
HardParameterDA.Save(StationCode + (IsPrArm ? Constant.PrSafeEnd : Constant.EbrSafeEnd), Convert.ToString(value));
}
public void OnAxisSpeedTextChangedCommand(string value)
{
HardParameterDA.Save(StationCode + (IsPrArm ? Constant.PrAxisSpeed : Constant.EbrAxisSpeed), Convert.ToString(value));
}
public void OnHomePositionTextChangedCommand(string value)
{
//HardParameterDA.Save(StationCode + (IsPrArm ? Constant.PrHomePosition : Constant.EbrHomePosition), Convert.ToString(value));
string postionkey = (IsPrArm ? string.Format("{0}_Arm1PRHomingOffset", StationCode) : string.Format("{0}_Arm2EBRHomingOffset", StationCode));
var postionSet = Global.Settings.FirstOrDefault(q => q.RecId == postionkey);
postionSet.SettingValue = Common.ToDecimal(value);
SysSettingDA.Update(postionSet);
}
public void OnHomeSpeedTextChangedCommand(string value)
{
HardParameterDA.Save(StationCode + (IsPrArm ? Constant.PrHomeSpeed : Constant.EbrHomeSpeed), Convert.ToString(value));
}
public void OnMovePositionTextChangedCommand(string value)
{
HardParameterDA.Save(StationCode + (IsPrArm ? Constant.PrMovePosition : Constant.EbrMovePosition), Convert.ToString(value));
}
public void OnMoveSpeedTextChangedCommand(string value)
{
HardParameterDA.Save(StationCode + (IsPrArm ? Constant.PrMoveSpeed : Constant.EbrMoveSpeed), Convert.ToString(value));
}
#endregion
}
}