258 lines
8.5 KiB
C#
258 lines
8.5 KiB
C#
using Aurora;
|
|
using Aurora.Api;
|
|
using Aurora.Api.Devices;
|
|
using Aurora.Api.Manager;
|
|
using Aurora.View;
|
|
using System;
|
|
using System.Windows.Input;
|
|
using System.Windows.Threading;
|
|
|
|
namespace AuroraACS.Views
|
|
{
|
|
class UCACSMotorViewModel : NotifyPropertyChangedBase
|
|
{
|
|
readonly Logger LOG = new Logger("ACSMotor");
|
|
|
|
static readonly TimeSpan TIMESPAN_NORMAL = TimeSpan.FromMilliseconds(300);
|
|
static readonly TimeSpan TIMESPAN_FREQUENT = TimeSpan.FromMilliseconds(100);
|
|
|
|
public ICommand CMD_VisibleChanged { get; }
|
|
public ICommand CMD_Init { get; }
|
|
public ICommand CMD_ClearAlarm { get; }
|
|
public ICommand CMD_Home { get; }
|
|
public ICommand CMD_RunBuffer { get; }
|
|
public ICommand CMD_MoveABS { get; }
|
|
public ICommand CMD_MoveINC { get; }
|
|
public ICommand CMD_StopSlow { get; }
|
|
public ICommand CMD_StopEmergency { get; }
|
|
|
|
private bool bSensor_P;
|
|
public bool IsPSensor { get => bSensor_P; set => SetProperty(ref bSensor_P, value); }
|
|
|
|
private bool bSensor_M;
|
|
public bool IsMSensor { get => bSensor_M; set => SetProperty(ref bSensor_M, value); }
|
|
|
|
private bool bSensor_H;
|
|
public bool IsHomeSignal { get => bSensor_H; set => SetProperty(ref bSensor_H, value); }
|
|
|
|
private bool isSPSensor;
|
|
public bool IsSPSensor { get => isSPSensor; set => SetProperty(ref isSPSensor, value); }
|
|
|
|
private bool isSNSensor;
|
|
public bool IsSNSensor { get => isSNSensor; set => SetProperty(ref isSNSensor, value); }
|
|
|
|
private bool bInPosition;
|
|
public bool IsInPos { get => bInPosition; set => SetProperty(ref bInPosition, value); }
|
|
|
|
private double dCurrentPos = 12345.6;
|
|
public double CurrentPos { get => dCurrentPos; set => SetProperty(ref dCurrentPos, value); }
|
|
|
|
public double dCmdPos = 45678.9;
|
|
public double CmdPos { get => dCmdPos; set => SetProperty(ref dCmdPos, value); }
|
|
|
|
public double dVelocity = 0;
|
|
public double Velocity { get => dVelocity; set => SetProperty(ref dVelocity, value); }
|
|
|
|
private bool? isInit;
|
|
public bool? IsInit { get => isInit; set => SetProperty(ref isInit, value); }
|
|
|
|
private bool bServoAlarm;
|
|
public bool IsServoAlarm { get => bServoAlarm; set => SetProperty(ref bServoAlarm, value); }
|
|
|
|
private double homeRate = 0;
|
|
public double HomeRate { get => homeRate; set => SetProperty(ref homeRate, value); }
|
|
|
|
private bool isMoving;
|
|
public bool IsMoving { get => isMoving; set => SetProperty(ref isMoving, value); }
|
|
|
|
private bool isBufferRunnning;
|
|
public bool IsBufferRunning { get => isBufferRunnning; set => SetProperty(ref isBufferRunnning, value); }
|
|
|
|
private double aCSAcc;
|
|
public double ACSAcc { get => aCSAcc; set => SetProperty(ref aCSAcc, value); }
|
|
|
|
private double aCSDec;
|
|
public double ACSDec { get => aCSDec; set => SetProperty(ref aCSDec, value); }
|
|
|
|
private double aCSJerk;
|
|
public double ACSJerk { get => aCSJerk; set => SetProperty(ref aCSJerk, value); }
|
|
|
|
public double TargetPos { get; set; }
|
|
public double Speed { get; set; }
|
|
public string Buffer { get; set; }
|
|
|
|
private double _JogSpeed;
|
|
public double JogSpeed { get => _JogSpeed; set => SetProperty(ref _JogSpeed, value); }
|
|
|
|
private readonly IDeviceManager DeviceManager;
|
|
readonly DispatcherTimer MonitorTimer;
|
|
readonly DispatcherTimer CurrentPosUpdateTimer;
|
|
readonly ACSMotor ActivatedMotor;
|
|
|
|
public string DeviceId { get; }
|
|
|
|
public UCACSMotorViewModel(IDeviceManager devMngr, string deviceId)
|
|
{
|
|
DeviceManager = devMngr;
|
|
DeviceId = deviceId;
|
|
|
|
ActivatedMotor = DeviceManager[deviceId] as ACSMotor;
|
|
|
|
CMD_VisibleChanged = new DelegateCommand(VisibleChanged);
|
|
CMD_Init = new DelegateCommand(Init);
|
|
CMD_ClearAlarm = new DelegateCommand(ClearAlarm);
|
|
CMD_Home = new DelegateCommand(Home);
|
|
CMD_RunBuffer = new DelegateCommand(RunBuffer);
|
|
CMD_MoveABS = new DelegateCommand(MoveABS);
|
|
CMD_MoveINC = new DelegateCommand(MoveINC);
|
|
CMD_StopSlow = new DelegateCommand(StopSlow);
|
|
CMD_StopEmergency = new DelegateCommand(StopEmergency);
|
|
|
|
MonitorTimer = CreateTimer(
|
|
TIMESPAN_NORMAL,
|
|
OnMonitorTime
|
|
);
|
|
CurrentPosUpdateTimer = CreateTimer(
|
|
TIMESPAN_FREQUENT,
|
|
OnCurrentPosUpdateTime
|
|
);
|
|
|
|
CurrentPosUpdateTimer.Interval = TIMESPAN_NORMAL;
|
|
}
|
|
|
|
DispatcherTimer CreateTimer(TimeSpan interval, Action onTime)
|
|
{
|
|
var timer = new DispatcherTimer();
|
|
timer.Tick += (s, e) => onTime();
|
|
timer.Interval = interval;
|
|
return timer;
|
|
}
|
|
|
|
private void VisibleChanged(object obj)
|
|
{
|
|
bool visible = (bool)obj;
|
|
if (visible)
|
|
{
|
|
var config = DeviceManager.GetDeviceConfig(ActivatedMotor.DeviceId) as ACSMotorConfig;
|
|
//MonitorTimer.Start();
|
|
//CurrentPosUpdateTimer.Start();
|
|
ACSAcc = ActivatedMotor.GetAcceleration();
|
|
ACSDec = ActivatedMotor.GetDeceleration();
|
|
aCSJerk = ActivatedMotor.GetJerk();
|
|
JogSpeed = config.MidJogSpeed;
|
|
|
|
MonitorTimer.Start();
|
|
CurrentPosUpdateTimer.Start();
|
|
}
|
|
else
|
|
{
|
|
MonitorTimer.Stop();
|
|
CurrentPosUpdateTimer.Stop();
|
|
}
|
|
}
|
|
|
|
|
|
void OnMonitorTime()
|
|
{
|
|
IsInit = ActivatedMotor.IsInit;
|
|
IsServoAlarm = ActivatedMotor.IsAlarm();
|
|
IsMoving = ActivatedMotor.IsMoving();
|
|
IsInPos = ActivatedMotor.ReadInposition();
|
|
IsPSensor = ActivatedMotor.IsHPLimit();
|
|
IsMSensor = ActivatedMotor.IsHNLimit();
|
|
IsSPSensor = ActivatedMotor.IsSPLimit();
|
|
IsSNSensor = ActivatedMotor.IsSNLimit();
|
|
}
|
|
|
|
void OnCurrentPosUpdateTime()
|
|
{
|
|
CurrentPos = ActivatedMotor.GetActualPos();
|
|
CmdPos = ActivatedMotor.GetReferencePosition();
|
|
Velocity = ActivatedMotor.GetFeedbackVelocity();
|
|
}
|
|
|
|
private void Init(object obj)
|
|
{
|
|
ActivatedMotor.Init();
|
|
}
|
|
|
|
internal void StopEmergency(object obj)
|
|
{
|
|
ActivatedMotor.EmergencyStop();
|
|
}
|
|
|
|
private void StopSlow(object obj)
|
|
{
|
|
ActivatedMotor.Stop();
|
|
}
|
|
|
|
internal void JogUp()
|
|
{
|
|
ActivatedMotor.Jog(-1, JogSpeed);
|
|
}
|
|
|
|
internal void JogDown()
|
|
{
|
|
ActivatedMotor.Jog(1, JogSpeed);
|
|
}
|
|
|
|
internal void Home(object obj)
|
|
{
|
|
ActivatedMotor.Home();
|
|
}
|
|
|
|
internal void ClearAlarm(object obj)
|
|
{
|
|
ActivatedMotor.ClearAlarm();
|
|
}
|
|
|
|
internal void ServoOn(bool isOn)
|
|
{
|
|
//ActivatedMotor.ServoOn(isOn);
|
|
}
|
|
|
|
private void MoveABS(object obj)
|
|
{
|
|
ActivatedMotor.SetTargetPos(TargetPos, TargetPosType.ABS);
|
|
ActivatedMotor.SetVelocityImm(Speed);
|
|
ActivatedMotor.SetAccelerationImm(aCSAcc);
|
|
ActivatedMotor.SetDecelerationImm(aCSDec);
|
|
ActivatedMotor.SetJerkImm(aCSJerk);
|
|
ActivatedMotor.Move();
|
|
|
|
LOG.D(string.Format("Motion MoveABS : {0}, Speed : {1}", TargetPos, Speed));
|
|
}
|
|
|
|
private void MoveINC(object obj)
|
|
{
|
|
ActivatedMotor.SetTargetPos(TargetPos, TargetPosType.REL);
|
|
ActivatedMotor.SetVelocityImm(Speed);
|
|
ActivatedMotor.SetAccelerationImm(aCSAcc);
|
|
ActivatedMotor.SetDecelerationImm(aCSDec);
|
|
ActivatedMotor.SetJerkImm(aCSJerk);
|
|
ActivatedMotor.Move();
|
|
|
|
LOG.D(string.Format("Motion MoveInc : {0}, Speed : {1}", TargetPos, Speed));
|
|
}
|
|
|
|
internal void RunBuffer(object obj)
|
|
{
|
|
if (string.IsNullOrEmpty(Buffer)) {
|
|
MessageUtil.CreateAndShowMessageDialog("Fail", "label is empty");
|
|
return;
|
|
}
|
|
|
|
IsBufferRunning = true;
|
|
ActivatedMotor.RunBuffer(Buffer, ProgramEnd);
|
|
|
|
}
|
|
|
|
internal void ProgramEnd(int bufferNo)
|
|
{
|
|
if (ActivatedMotor.BufferNo == bufferNo) {
|
|
IsBufferRunning = false;
|
|
}
|
|
}
|
|
}
|
|
}
|