2026-06-05 13:44:39 +09:00

324 lines
11 KiB
C#

using Aurora.Api;
using Aurora.Api.Control;
using Aurora.Api.Devices;
using Aurora.Api.Manager;
using Aurora.View;
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
namespace AuroraMotionBoard.Ajin.Views
{
public class UCAJINMotorViewModel : 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; }
public ICommand CMD_ServoOn { get; }
public ICommand CMD_MotorAxisChanged { get; }
public ICommand CMD_ReScan { 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 bool _IsServoOn;
public bool IsServoOn { get => _IsServoOn; set => SetProperty(ref _IsServoOn, value); }
private double _Acc;
public double Acc { get => _Acc; set => SetProperty(ref _Acc, value); }
private double _Dec;
public double Dec { get => _Dec; set => SetProperty(ref _Dec, value); }
private double _Jerk;
public double Jerk { get => _Jerk; set => SetProperty(ref _Jerk, value); }
private double _TargetPos;
public double TargetPos { get => _TargetPos; set => SetProperty(ref _TargetPos, value); }
private double _Speed;
public double Speed { get => _Speed; set => SetProperty(ref _Speed, value); }
private string _Buffer;
public string Buffer { get => _Buffer; set => SetProperty(ref _Buffer, value); }
private double _JogSpeed;
private string _motorAxis = "ZAxis";
public double JogSpeed { get => _JogSpeed; set => SetProperty(ref _JogSpeed, value); }
private readonly IDeviceManager DeviceManager;
readonly DispatcherTimer MonitorTimer;
readonly DispatcherTimer CurrentPosUpdateTimer;
readonly AJINMotor ActivatedMotor;
public UCAJINMotorViewModel(IDeviceManager devMngr, string deviceId)
{
// BaseMotor 클래스 사용하니깐 헷갈림.. 사용안하는걸로하자
DeviceManager = devMngr;
ActivatedMotor = DeviceManager[deviceId] as AJINMotor;
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);
CMD_ServoOn = new DelegateCommand(ServoOn);
CMD_MotorAxisChanged = new DelegateCommand(OnMotorAxisChanged);
CMD_ReScan = new DelegateCommand(ReScan);
MonitorTimer = CreateTimer(
TIMESPAN_NORMAL,
OnMonitorTime
);
CurrentPosUpdateTimer = CreateTimer(
TIMESPAN_FREQUENT,
OnCurrentPosUpdateTime
);
CurrentPosUpdateTimer.Interval = TIMESPAN_NORMAL;
}
private void OnMotorAxisChanged(object obj)
{
_motorAxis = (string)obj;
}
private void ReScan(object obj)
{
uint res = 0;
res = ActivatedMotor.MotorReScan();
}
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 AJINMotorConfig;
//MonitorTimer.Start();
//CurrentPosUpdateTimer.Start();
Acc = config.Acceleration;
Dec = config.Deceleration;
//Jerk = ActivatedMotor.GetJerk();
JogSpeed = config.MidJogSpeed;
Speed = config.ManualSpeed;
MonitorTimer.Start();
CurrentPosUpdateTimer.Start();
} else {
MonitorTimer.Stop();
CurrentPosUpdateTimer.Stop();
}
}
void OnMonitorTime()
{
IsInit = ActivatedMotor.IsInit;
IsServoOn = ActivatedMotor.IsServoOn();
IsHomeSignal = ActivatedMotor.IsHomeCompleted();
IsServoAlarm = ActivatedMotor.IsAlarm();
IsMoving = ActivatedMotor.IsMoving();
IsInPos = ActivatedMotor.ReadInposition();
IsPSensor = ActivatedMotor.IsHWPosLimit();
IsMSensor = ActivatedMotor.IsHWNegLimit();
IsSPSensor = ActivatedMotor.IsSWPosLimit();
IsSNSensor = ActivatedMotor.IsSWNegLimit();
}
void OnCurrentPosUpdateTime()
{
CurrentPos = ActivatedMotor.GetCurrentPosition();
CmdPos = ActivatedMotor.GetTargetPosition();
Velocity = ActivatedMotor.GetFeedbackVelocity();
}
private void Init(object obj)
{
//ActivatedMotor.Init();
}
private void ServoOn(object obj)
{
int res = 0;
if (!ActivatedMotor.IsServoOn())
res = ActivatedMotor.ServoOn(true);
else
res = ActivatedMotor.ServoOn(false);
if (res != 0) {
var result = MessageDialog.Show(Application.Current.MainWindow, "Motion", "Please, Check Motor Status.",
MessageDialog.Type.Affirmative);
}
}
internal void StopEmergency(object obj)
{
ActivatedMotor.EmergencyStop();
}
private void StopSlow(object obj)
{
int res = ActivatedMotor.DoStop();
if (res != 0) {
var result = MessageDialog.Show(Application.Current.MainWindow, "Motion", "Please, Check Motor Status.",
MessageDialog.Type.Affirmative);
}
}
internal void JogUp()
{
//JogSpeed가 버튼 한번 클릭 후 바뀌는 이유가 뭐지..
int res = ActivatedMotor.DoJog(1, JogSpeed);
if (res != 0) {
var result = MessageDialog.Show(Application.Current.MainWindow, "Motion", "Please, Check Motor Status.",
MessageDialog.Type.Affirmative);
}
}
internal void JogDown()
{
int res = ActivatedMotor.DoJog(-1, JogSpeed);
if (res != 0) {
var result = MessageDialog.Show(Application.Current.MainWindow, "Motion", "Please, Check Motor Status.",
MessageDialog.Type.Affirmative);
}
}
internal void Home(object obj)
{
int res = ActivatedMotor.Home();
if (res != 0) {
var result = MessageDialog.Show(Application.Current.MainWindow, "Motion", "Please, Check Motor Status.",
MessageDialog.Type.Affirmative);
}
}
internal void ClearAlarm(object obj)
{
int res = ActivatedMotor.ClearAlarm();
if (res != 0) {
var result = MessageDialog.Show(Application.Current.MainWindow, "Motion", "Please, Check Motor Status.",
MessageDialog.Type.Affirmative);
}
}
internal void ServoOn(bool isOn)
{
//ActivatedMotor.ServoOn(isOn);
}
private void MoveABS(object obj)
{
ActivatedMotor.SetTargetPosition(TargetPos, TargetPosType.ABS);
ActivatedMotor.SetVelocity(Speed, Acc, Dec);
int res = ActivatedMotor.DoMove();
if (res != 0) {
var result = MessageDialog.Show(Application.Current.MainWindow, "Motion", "Please, Check Motor Status.",
MessageDialog.Type.Affirmative);
}
LOG.D(string.Format("Motion MoveACS : {0}, Speed : {1}", TargetPos, Speed));
}
private void MoveINC(object obj)
{
ActivatedMotor.SetTargetPosition(TargetPos, TargetPosType.REL);
ActivatedMotor.SetVelocity(Speed, Acc, Dec);
int res = ActivatedMotor.DoMove();
if (res != 0) {
var result = MessageDialog.Show(Application.Current.MainWindow, "Motion", "Please, Check Motor Status.",
MessageDialog.Type.Affirmative);
}
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;
//}
}
}
}