2026-01-26 18:56:53 +09:00

213 lines
5.4 KiB
C#

using Aurora;
using Aurora.Api;
using Aurora.Api.Devices;
using Hermes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace AuroraMotor
{
public enum eMoverErrCode
{
OK = 0,
// Info
Moving = 30100,
NotArrived = 30101,
MoverSeq_Stopped = 30102,
// Warning
TargetSPDIsZero = 30300,
// Error
NotActivated = 31000,
CantChangeMoverGroupByMoving = 31100,
IncorrectMoveCallByMoverGroup = 31150,
IncorrectMotorIndex = 31160,
IncorrectMoverGroup = 31161,
TargetSPDIsNegative = 31200,
NoMotors = 31201,
CantMoveByMoving = 31202,
//CmdMoveFail = 31200,
MoverSeq_InternalErr = 31300,
TimeOut = 31310,
}
public class SeqMover : Device
{
private static readonly Logger Log = new Logger("SeqMover");
//private object _lock = new object();
public enum eMoverState
{
Idle,
Moving, // Jogging 포함..
Stopped,
StoppedByError,
CmdMoveFail,
}
public eMoverState moverState = eMoverState.Idle;
// 모션축 리스트 관리 변수
public List<AuroraMotorBase> Motors { get; set; } = new List<AuroraMotorBase>();
public int CurMoverGroup { get; set; }
public bool CheckArrival { get; set; }
public TargetPosType Movetype = TargetPosType.ABS;
#region 새로운 버전
public SeqMover()
{
Motors.Clear();
}
public SeqMover(List<AuroraMotorBase> motors)
{
Motors.Clear();
foreach (var motor in motors) {
Motors.Add(motor);
}
}
public int SetMoveParam(double position, double tarSPD)
{
if (tarSPD > 0) {
int res2 = 0;
int res = 0;
Motors.ForEach(x => {
res2 = x.SetTargetSPD(tarSPD);
if (res2 != 0) { res = res2; };
res2 = x.SetTargetPos(position, Movetype);
if (res2 != 0) { res = res2; };
});
if (res != 0) return res;
} else {
return (int)eMoverErrCode.TargetSPDIsZero; // tarVel
}
return 0;
}
public int SetMoveParam(int motorIdx, double position)
{
var res = Motors[motorIdx].SetTargetPos(position, Movetype);
if (res != 0) return res;
return 0;
}
public int SetMoveParam(int motorIdx, double position, double tarSPD)
{
if (tarSPD > 0) {
Motors[motorIdx].SetTargetSPD(tarSPD);
var res = Motors[motorIdx].SetTargetPos(position, Movetype);
if (res != 0) return res;
} else {
return (int)eMoverErrCode.TargetSPDIsZero; // tarVel
}
return 0;
}
#region Move 부분
public double GetTargetSPD(int motorIdx)
{
return Motors[motorIdx].GetTargetSPD();
}
#endregion
#region 조깅 부분
// 항상 단축을 가정하면, 여기도 인덱스 인자는 지워야겠는데...추후 검토..
public int SetJogSpeed(int motorIdx, eJogSpeed jogSPD)
{
return Motors[motorIdx].SetJogSPD(jogSPD);
}
public int SetJogSpeed(eJogSpeed jogSPD)
{
foreach (var motor in Motors) {
motor.SetJogSPD(jogSPD);
}
return 0;
}
public int Jog(int jogDir)
{
// 단축 조깅을 가정!!!
moverState = eMoverState.Moving;
Log.FuncI($"{Motors.FirstOrDefault().DeviceId}");
return Motors.FirstOrDefault().Jog(jogDir);
}
//public int Jog(int motorIdx, int jogDir)
//{
// moverState = eMoverState.Moving;
// var motorIndex = MotorIds[motorIdx];
// return Motors[motorIndex].Jog(jogDir);
//}
// 조깅은 무조건 단축을 가정하므로, 모터 인덱스 불필요
public void StopJog()
{
Log.FuncI();
moverState = eMoverState.Stopped;
Log.FuncI($"{Motors.FirstOrDefault().DeviceId}");
Motors.FirstOrDefault().StopJog();
}
//public void StopJog(int motorIdx)
//{
// moverState = eMoverState.Stopped;
// var motorIndex = MotorIds[motorIdx];
// Motors[motorIndex].StopJog();
//}
#endregion
#endregion
//=============================================================
#region Aurora Device 관련 메서드
protected override int DoActivate(IDeviceConfig config)
{
moverState = eMoverState.Idle;
return 0;
}
protected override int DoDeactivate()
{
return 0;
}
public class BaseConfig : IDeviceConfig
{
[Browsable(false)]
public IConnConfig ConnConfig { get; set; } = null;
public Position TeachOffset { get; set; } = new Position();
}
#endregion
}
}