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

245 lines
8.9 KiB
C#

/*
* Copyright (c) 2022 LG Electronics Inc.
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
*/
using Aurora;
using Aurora.Api;
using Aurora.Api.Devices;
using Aurora.View;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace AuroraUIComponent.UserControls
{
/// <summary>
/// MotorStatusTableView.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MotorStatusTableView : UserControl
{
public MotorStatusTableView()
{
InitializeComponent();
}
}
public class MotorStatusItem : NotifyPropertyChangedBase
{
private bool use;
public bool Use { get => use; set => SetProperty(ref use, value); }
private string deviceId;
public string DeviceId { get => deviceId; set => SetProperty(ref deviceId, value); }
//private int axisInfo;
//public int AxisInfo { get => axisInfo; set => SetProperty(ref axisInfo, value); }
private double currPos;
public double CurrPos { get => currPos; set { SetProperty(ref currPos, value); NotifyPropertyChanged(nameof(Final)); } }
private double teaching;
public double Teaching { get => teaching; set { SetProperty(ref teaching, value); NotifyPropertyChanged(nameof(Final)); } }
private double offset;
public double Offset { get => offset; set { SetProperty(ref offset, value); NotifyPropertyChanged(nameof(Final)); } }
private double correction;
public double Correction { get => correction; set => SetProperty(ref correction, value); }
public double Final { get => Teaching + Offset + Correction; }
private bool inPos;
public bool InPos { get => inPos; set => SetProperty(ref inPos, value); }
private bool homeState;
public bool HomeState { get => homeState; set => SetProperty(ref homeState, value); }
private bool servoOn;
public bool ServoOn { get => servoOn; set => SetProperty(ref servoOn, value); }
private double target;
public double Target { get => target; set { SetProperty(ref target, value); } }
private bool error;
public bool Error { get => error; set => SetProperty(ref error, value); }
private bool lLimit;
public bool LLimit { get => lLimit; set => SetProperty(ref lLimit, value); }
private bool rLimit;
public bool RLimit { get => rLimit; set => SetProperty(ref rLimit, value); }
private Motor _motor;
public ICommand CMD_Move { get; }
public ICommand CMD_Stop { get; }
public ICommand CMD_Home { get; }
private void PerformCmdMove(object commandParameter)
{
PerformCmdRelMove(Target, TargetPosType.ABS);
}
private void PerformCmdRelMove(double target, TargetPosType type)
{
if (_motor == null) {
Logger.E(string.Format($"motor is null"));
return;
}
try {
var config = (Motor.BaseConfig)Application.Current.AuroraEnv().DeviceManager.GetDeviceConfig(_motor.DeviceId);
double vel = config.DefaultVelocity;
if (vel <= 0) {
Logger.W($"Invalid Velocity({vel}). Check default velocity of the motor device config.");
}
_motor.SetTargetPosition(target, type);
_motor.SetVelocity((uint)vel);
// SetOthers()라도 만들어야 하나?
//Motor?.SetAccelerationImm(config.AxisVelInfo.Acc);
//Motor?.SetDecelerationImm(config.AxisVelInfo.Dec);
//Motor?.SetJerkImm(config.AxisVelInfo.Jerk);
_motor.Move();
Logger.D(string.Format($"{_motor.DeviceId} Move"));
} catch (Exception exc) {
Logger.E(exc.Message);
}
}
private void PerformCmdStop(object commandParameter)
{
if (_motor == null) {
Logger.E(string.Format($"motor is null"));
return;
}
_motor.Stop();
}
private void PerformCmdHome(object commandParameter)
{
if (_motor == null) {
Logger.E(string.Format($"motor is null"));
return;
}
// Interlock??
_motor.Home();
}
public MotorStatusItem()
{
CMD_Move = new DelegateCommand(PerformCmdMove);
CMD_Stop = new DelegateCommand(PerformCmdStop);
CMD_Home = new DelegateCommand(PerformCmdHome);
}
public MotorStatusItem(Motor motor): this()
{
_motor = motor;
}
private Visibility moveVisible = Visibility.Visible;
public Visibility MoveVisible
{
get => moveVisible;
set => SetProperty(ref moveVisible, value);
}
private Visibility homeVisible =Visibility.Visible;
public Visibility HomeVisible
{
get => homeVisible;
set => SetProperty(ref homeVisible, value);
}
}
public class MotorStatus
{
public List<MotorStatusItem> MotorStatusList { get; set; } = new List<MotorStatusItem>();
private Dictionary<string, int> _idx = new Dictionary<string, int>();
//private Dictionary<int, string> _idxAxis = new Dictionary<int, int>();
//public void AddMotor(string deviceId, int axisNo)
//{
// _idx.Add(deviceId, MotorStatusList.Count);
// _idxAxis.Add(axisNo, MotorStatusList.Count);
// MotorStatusList.Add(new MotorStatusItem() { DeviceId = deviceId, AxisInfo = axisNo, Use = false });
//}
public void AddMotor(Motor motor, bool useMove = true, bool useHome = true)
{
_idx.Add(motor.DeviceId, MotorStatusList.Count);
//_idxAxis.Add((int)motor.AxisNo, MotorStatusList.Count);
MotorStatusList.Add(new MotorStatusItem(motor)
{
DeviceId = motor.DeviceId,
//AxisInfo = (int)motor.AxisNo,
Use = false,
MoveVisible = useMove ? Visibility.Visible : Visibility.Collapsed,
HomeVisible = useHome ? Visibility.Visible : Visibility.Collapsed
});
}
public void UpdateStatus(MotorStatusUpdateItem statusItem)
{
int idx = _idx[statusItem.devId];
MotorStatusList[idx].InPos = statusItem.IsInPos;
MotorStatusList[idx].ServoOn = statusItem.IsServoOn;
MotorStatusList[idx].HomeState = statusItem.IsHomeState;
MotorStatusList[idx].LLimit = statusItem.IsLLimit;
MotorStatusList[idx].RLimit = statusItem.IsRLimit;
}
public void ClearUse()
{
foreach (var item in MotorStatusList)
{
item.Use = false;
}
}
public void ClearAlign()
{
foreach (var item in MotorStatusList)
{
item.Correction = 0;
}
}
public bool SetTarget(string devId, double teaching, double offset)
{
if (_idx.ContainsKey(devId))
{
MotorStatusList[_idx[devId]].Use = true;
MotorStatusList[_idx[devId]].Teaching = teaching;
MotorStatusList[_idx[devId]].Offset = offset;
return true;
}
return false;
}
public bool SetTarget(string devId, double teaching, double offset, double TeachingCarrierPos)
{
return SetTarget(devId, teaching + TeachingCarrierPos, offset);
}
public bool SetTarget(string devId, double teaching, double offset, double TeachingCarrierPos, double TeachingDisToCam)
{
return SetTarget(devId, teaching + TeachingCarrierPos + TeachingDisToCam, offset);
}
public bool SetAlignValue(string devId, double alignValue)
{
if (_idx.ContainsKey(devId))
{
MotorStatusList[_idx[devId]].Correction = alignValue;
return true;
}
return false;
}
public bool SetCurrPos(string devId, double currPos)
{
if (_idx.ContainsKey(devId))
{
//MotorStatusList[_idx[devId]].CurrPos = Math.Round(currPos, 4);
MotorStatusList[_idx[devId]].CurrPos = currPos;
return true;
}
return false;
}
}
public class MotorStatusUpdateItem
{
public string devId;
public bool IsChanged;
public bool IsServoOn;
public bool IsInPos;
public bool IsHomeState; // Home과는 다르다. Home을 갔다왔는지 여부, 알람 발생 시 이게 풀어진다.
public bool IsLLimit; // Limit State
public bool IsRLimit; // Limit State
}
}