using Aurora.Api; using PropertyTools.DataAnnotations; using System; using System.Data; using WeldingCommon.Utils; namespace WeldingCommon.Settings { public class LaserPowerCalParam : ISettingItem { DataTable m_centerCalibrationTable; DataTable m_ringCalibrationTable; public enum beamMode { Center = 1, Ring = 2, Error = 3 } [Browsable(false)] public DataTable CenterCalibrationTable { get { return m_centerCalibrationTable; } set { m_centerCalibrationTable = value; } } [Browsable(false)] public DataTable RingCalibrationTable { get { return m_ringCalibrationTable; } set { m_ringCalibrationTable = value; } } [Browsable(true)] [ReadOnly(false)] [Category("Calibration")] [InputFilePath] [DisplayName("PowerCalibrationCenterBeamCSVFile")] public string PowerCalibrationCenterBeamCSVFile { get; set; } [Browsable(true)] [ReadOnly(false)] [Category("Calibration")] [InputFilePath] [DisplayName("PowerCalibrationRingBeamCSVFile")] public string PowerCalibrationRingBeamCSVFile { get; set; } public LaserPowerCalParam() { // 초기 설정할 때, CSV 파일을 토대로 Center, Ring Beam을 // 확인하는 부분이 필수적인 듯. } public void Initialize(string centerBeamPath, string ringBeamPath) { LoadTable(centerBeamPath, ringBeamPath); } public void InitializeTable(double maxPower, double interval, out DataTable dataTable, uint beam = 0) { DataTable m_calibrationTable = new DataTable(); if (beam == (int)beamMode.Error) { dataTable = m_calibrationTable; // MessageBox return; } m_calibrationTable.Columns.Add(new DataColumn("Index", typeof(double))); //m_calibrationTable.Columns.Add(new DataColumn("Voltage", typeof(double))); m_calibrationTable.Columns.Add(new DataColumn("DesiredPower", typeof(double))); m_calibrationTable.Columns.Add(new DataColumn("CalibratedPower", typeof(double))); double index = 0; DataRow row; int iRowCount; object[] rowArray; while (index < 100) { row = m_calibrationTable.NewRow(); iRowCount = m_calibrationTable.Rows.Count; rowArray = new object[3]; //Index Percentage(0 ~ 100 %) rowArray[0] = index; //Voltage(0 ~ 10 V) //rowArray[1] = index / 10; //Desired Power(0 ~ Max Watt) rowArray[1] = maxPower * index / 100; //초기에는 Calibrated Power = Desired Power rowArray[2] = rowArray[1]; row.ItemArray = rowArray; m_calibrationTable.Rows.Add(row); index = index + interval; } //마지막 100% 인 경우를 추가 row = m_calibrationTable.NewRow(); iRowCount = m_calibrationTable.Rows.Count; rowArray = new object[3]; //Index Percentage(0 ~ 100 %) rowArray[0] = 100; //Voltage(0 ~ 10 V) //rowArray[1] = 10; //Desired Power(0 ~ Max Watt) rowArray[1] = maxPower; //Calibrated Power(0 ~ Max Watt) rowArray[2] = maxPower; row.ItemArray = rowArray; m_calibrationTable.Rows.Add(row); index = index + interval; dataTable = m_calibrationTable; } public void LoadTable(string centerBeamPath, string ringBeamPath) { if (true == System.IO.Path.GetExtension(centerBeamPath).ToUpper().Contains("CSV") && true == System.IO.Path.GetExtension(ringBeamPath).ToUpper().Contains("CSV")) { CenterCalibrationTable = ExcelManagementUnit.ImportDataFromCsv(centerBeamPath, true, ','); RingCalibrationTable = ExcelManagementUnit.ImportDataFromCsv(ringBeamPath, true, ','); //m_centerBeamPath = centerBeamPath; //m_ringBeamPath = ringBeamPath; } else { // Message 박스 열기. } } public void SaveTable(DataTable dataTable, uint beam = (uint)beamMode.Center) { string fileName; if (beam == (int)beamMode.Center) { fileName = PowerCalibrationCenterBeamCSVFile; CenterCalibrationTable = dataTable; ExcelManagementUnit.ExportToCSV(fileName, CenterCalibrationTable, ",", false); } if (beam == (int)beamMode.Ring) { fileName = PowerCalibrationRingBeamCSVFile; RingCalibrationTable = dataTable; ExcelManagementUnit.ExportToCSV(fileName, RingCalibrationTable, ",", false); } // if (beam == (int)beamMode.Error) MessageBox } public double LinearInterpolation(double a, double b, double fa, double fb, double fc) { // x : a < c < b, y: f(a) < f(c) < f(b) double incline = (fb - fa) / (b - a); double sol = (fc - fa + incline * a) / incline; return sol; } public double CalibrateWattToVolt(double power, uint beam = (uint)beamMode.Center) { DataTable m_dataTable; double wattToPercent = CalibrateWattToPercent(power, beam); if (0 <= wattToPercent && wattToPercent <= 100) return wattToPercent / 10; return 0; } public double CalibrateWattToPercent(double power, uint beam = (uint)beamMode.Center) { DataTable m_dataTable; if (beam == (int)beamMode.Center) m_dataTable = CenterCalibrationTable; else if (beam == (int)beamMode.Ring) m_dataTable = RingCalibrationTable; else return 0; int percentIndex = 0, calibratedIndex = 2; double x1 = 0, x2 = 0, y1 = 0, y2 = 0; for (int i = 1; i < m_dataTable.Rows.Count; i++) { double.TryParse(Convert.ToString(m_dataTable.Rows[i][calibratedIndex]), out y2); if (y1 < power && power <= y2) { double.TryParse(Convert.ToString(m_dataTable.Rows[i - 1][percentIndex]), out x1); double.TryParse(Convert.ToString(m_dataTable.Rows[i][percentIndex]), out x2); return LinearInterpolation(x1, x2, y1, y2, power); } else y1 = y2; } return 0; } public double CalibratePercentToWatt(double power, uint beam = (uint)beamMode.Center) { DataTable m_dataTable; if (beam == (int)beamMode.Center) m_dataTable = CenterCalibrationTable; else if (beam == (int)beamMode.Ring) m_dataTable = RingCalibrationTable; else return 0; int percentIndex = 0, calibratedIndex = 2; double x1 = 0, x2 = 0, y1 = 0, y2 = 0; for (int i = 1; i < m_dataTable.Rows.Count; i++) { double.TryParse(Convert.ToString(m_dataTable.Rows[i][percentIndex]), out y2); if (y1 < power && power <= y2) { double.TryParse(Convert.ToString(m_dataTable.Rows[i - 1][calibratedIndex]), out x1); double.TryParse(Convert.ToString(m_dataTable.Rows[i][calibratedIndex]), out x2); return LinearInterpolation(x1, x2, y1, y2, power); } else y1 = y2; } return 0; } } }