You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
TwoLaserHead-PushJig/LaiPuLaser/OptoWaveLaserMgr.cpp

488 lines
15 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "StdAfx.h"
#include "OptoWaveLaserMgr.h"
#include "Propertie.h"
#include "PropertieMgr.h"
#include "AuthorityMgr.h"
#include "CStringFuc.h"
#include "ExceptionMsg.h"
#include "LogMgr.h"
#include "MsgBox.h"
#include "GlobalFunction.h"
#include "FileMgr.h"
//激光电流和功率的校准文件
#define LASER_POWER_FILE _T("\\Parameter\\OptoWaveLaserMgr\\OptoWavePower.txt")
COptoWaveLaserMgr *gOptoWaveLaserMgr = new COptoWaveLaserMgr;
COptoWaveLaserMgr::COptoWaveLaserMgr(void)
{
m_Com = 2;//通信用com 口编号
m_PowerCurr = 20;//电流
m_Fre = 30;//激光频率khz
m_SendDelay = 200;//指令发送延时
m_CurrUpTime = 5;//电流上升等待时间(秒)
}
COptoWaveLaserMgr::~COptoWaveLaserMgr(void)
{
}
#if 1//属性设置
CMFCPropertyGridProperty *COptoWaveLaserMgr::CreatGridProperty()
{
CString PropertyName;//属性名称
CString Description;//描述
CString Path = _T("OptoWaveLaserMgr");//存储路径
CString Name;
//-------------------------------------------------------------------------------//
PropertyName = _T("OptoWave 激光器");
CMFCPropertyGridProperty* pGroup = new CMFCPropertyGridProperty(PropertyName);
//-------------------------------------------------------------------------------//
if(gAuthorityMgr->CheckAuthority(_FACTORY))
{
{
//添加属性变量映射
Name = _T("m_Com");//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_Com);
pPropertie->SetType(_PROP_TYPE_INT);
pPropertie->SetpModule(this);
pPropertie->SetPath(Path);
pPropertie->SetName(Name);
pPropertie->WriteRead(true);//读取保存的属性
//添加属性显示
PropertyName = _T("通信串口");
Description = _T("激光器和PC 通信使用的串口编号");
CMFCPropertyGridProperty* p = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_Com, Description);
pGroup->AddSubItem(p);
gDevicePropertieMgr.Insert(p, pPropertie);
}
{
//添加属性变量映射
Name = _T("m_PowerCurr");//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_PowerCurr);
pPropertie->SetType(_PROP_TYPE_DOUBLE);
pPropertie->SetpModule(this);
pPropertie->SetPath(Path);
pPropertie->SetName(Name);
pPropertie->WriteRead(true);//读取保存的属性
//添加属性显示
PropertyName = _T("电流");
Description = _T("激光电流值(单位:A)");
CMFCPropertyGridProperty* p = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_PowerCurr, Description);
pGroup->AddSubItem(p);
gDevicePropertieMgr.Insert(p, pPropertie);
}
{
//添加属性变量映射
Name = _T("m_Fre");//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_Fre);
pPropertie->SetType(_PROP_TYPE_INT);
pPropertie->SetpModule(this);
pPropertie->SetPath(Path);
pPropertie->SetName(Name);
pPropertie->WriteRead(true);//读取保存的属性
//添加属性显示
PropertyName = _T("频率");
Description = _T("激光频率(单位:kHz)");
CMFCPropertyGridProperty* p = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_Fre, Description);
pGroup->AddSubItem(p);
gDevicePropertieMgr.Insert(p, pPropertie);
}
{
//添加属性变量映射
Name = _T("m_SendDelay");//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_SendDelay);
pPropertie->SetType(_PROP_TYPE_INT);
pPropertie->SetpModule(this);
pPropertie->SetPath(Path);
pPropertie->SetName(Name);
pPropertie->WriteRead(true);//读取保存的属性
//添加属性显示
PropertyName = _T("指令延时");
Description = _T("发送给激光器的指令延时(单位:ms)");
CMFCPropertyGridProperty* p = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_SendDelay, Description);
pGroup->AddSubItem(p);
gDevicePropertieMgr.Insert(p, pPropertie);
}
{
//添加属性变量映射
Name = _T("m_CurrUpTime");//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_CurrUpTime);
pPropertie->SetType(_PROP_TYPE_INT);
pPropertie->SetpModule(this);
pPropertie->SetPath(Path);
pPropertie->SetName(Name);
pPropertie->WriteRead(true);//读取保存的属性
//添加属性显示
PropertyName = _T("电流上升时间");
Description = _T("激光器初始化时电流上升等待时间(秒)");
CMFCPropertyGridProperty* p = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_CurrUpTime, Description);
pGroup->AddSubItem(p);
gDevicePropertieMgr.Insert(p, pPropertie);
}
}
//-------------------------------------------------------------------------------//
return pGroup;
}
void COptoWaveLaserMgr::ExportPar(ofstream *pFile)
{
(*pFile)<<"[模块] [CLaserDeviceMgr]------------------------------------------------"<<endl;
(*pFile)<<"[串口号]"<<m_Com<<endl;
}
#endif
void COptoWaveLaserMgr::Ini()
{
//只在开始的时候打开串口
m_ComMgr.Open(m_Com);
}
#if 1
void COptoWaveLaserMgr::WriteWorkFileExt(vector<CLab> &LabVec)
{
//读取也要按照这个顺序
LabVec.push_back(CLab(LAB_NULL,m_PowerCurr));//电流
LabVec.push_back(CLab(LAB_NULL,m_Fre));//频率
}
void COptoWaveLaserMgr::ReadWorkFile(CLabVecRang &LabVecRang)
{
int idx = LabVecRang.GetStart()+1;
m_PowerCurr = LabVecRang.GetDouble(idx++);
m_Fre = LabVecRang.GetInt(idx++);
}
#endif
#if 1
//读取激光器状态
CString COptoWaveLaserMgr::GetDeviceStateStr(OPTO_LASER_STATE state)
{
CString str;
switch(state)
{
case _STATE_COM_ERR:
str = _T("串口未连接");
break;
case _STATE_NOT_CONNECT:
str = _T("未连接");
break;
case _STATE_NOT_INI:
str = _T("未初始化");
break;
case _STATE_WAITING:
str = _T("待机中");
break;
case _STATE_STARTING:
str = _T("启动中");
break;
case _STATE_WORK:
str = _T("运行中");
break;
default:
break;
}
return str;
}
COLORREF COptoWaveLaserMgr::GetColor(OPTO_LASER_STATE state)
{
COLORREF c;
switch(state)
{
case _STATE_COM_ERR:
c = RGB_RED;
break;
case _STATE_NOT_CONNECT:
c = RGB_RED;
break;
case _STATE_NOT_INI:
c = RGB_YELLOW;
break;
case _STATE_WAITING:
c = RGB_YELLOW;
break;
case _STATE_STARTING:
c = RGB_YELLOW;
break;
case _STATE_WORK:
c = RGB_GREEN;
break;
default:
break;
}
return c;
}
//读取激光器状态0:未初始化1:待机2:工作中
OPTO_LASER_STATE COptoWaveLaserMgr::ReadDeviceState()
{
//打开串口-----------------------------
if(!m_ComMgr.IsOpen())
{
return _STATE_COM_ERR;
}
//实际电流值
float IDI = 0;
{
//发送查询
CString str1 = "?IDI";
CString strSend = str1+char(13)+char(10);
m_ComMgr.Write(strSend);
Sleep(m_SendDelay);
//读取反馈
CString str2 = m_ComMgr.ReadStr();
if(str2 == "")
{
return _STATE_NOT_CONNECT;
}
if(str2[0]=='L' && str2[1]=='O'&&str2[2]=='C')
{
return _STATE_NOT_INI;
}
IDI = atof(str2)/10;
if(IsDbEqualZero(IDI))
{
return _STATE_WAITING;
}
}
//设置电流值
float IDS = 0;
{
//发送查询
CString str1 = "?IDS";
CString strSend = str1+char(13)+char(10);
m_ComMgr.Write(strSend);
Sleep(m_SendDelay);
//读取反馈
CString str2 = m_ComMgr.ReadStr();
IDS = atof(str2)/10;
}
if(abs(IDS-IDI)<2)
{
return _STATE_WORK;
}
return _STATE_STARTING;
}
//初始化激光器
bool COptoWaveLaserMgr::IniLaserDevice()
{
gLogMgr->WriteDebugLog("532 激光器初始化Start");
//控制状态修改
CtrlIni();
OPTO_LASER_STATE state = ReadDeviceState();
if(state==_STATE_COM_ERR)
{
CMsgBox MsgBox;
MsgBox.Show(_T("激光器串口打开失败!"));
return false;
}
else if(state==_STATE_NOT_CONNECT)
{
CMsgBox MsgBox;
MsgBox.Show(_T("激光器连接失败!"));
return false;
}
//未初始化或者待机状态
else if(state==_STATE_NOT_INI || state==_STATE_WAITING)
{
//设置功率和频率
SetFrequencyCurrent();
//打开二极管
SwitchDiode(true);
//等待电流上升
Sleep(m_CurrUpTime*1000);
}
else//_STATE_STARTING 和_STATE_WORK 两个状态设置电流频率即可
{
//设置功率和频率
SetFrequencyCurrent();
}
return true;
}
//关闭激光器
void COptoWaveLaserMgr::CloseLaserDevice()
{
gLogMgr->WriteDebugLog("532 激光器关闭");
if(!m_ComMgr.IsOpen())
{
return;
}
//将控制状态改为外控--------------
{
CString str1 = "MCH";
CString str = str1+char(13)+char(10);
m_ComMgr.Write(str);
Sleep(m_SendDelay);
}
//打开QSW
{
CString str1 = "QSW=0";
CString str = str1+char(13)+char(10);
m_ComMgr.Write(str);
Sleep(m_SendDelay);
}
//打开SHT
{
CString str1 = "SHT=0";
CString str = str1+char(13)+char(10);
m_ComMgr.Write(str);
Sleep(m_SendDelay);
}
{
CString str1 = "DIO=0";
CString str = str1+char(13)+char(10);
m_ComMgr.Write(str);
Sleep(m_SendDelay);
}
}
//控制初始化
void COptoWaveLaserMgr::CtrlIni()
{
if(!m_ComMgr.IsOpen())
{
return;
}
//将控制状态改为外控--------------
{
CString str1 = "MCH";
CString str = str1+char(13)+char(10);
m_ComMgr.Write(str);
Sleep(m_SendDelay);
}
//打开QSW
{
CString str1 = "QSW=1";
CString str = str1+char(13)+char(10);
m_ComMgr.Write(str);
Sleep(m_SendDelay);
}
//打开SHT
{
CString str1 = "SHT=1";
CString str = str1+char(13)+char(10);
m_ComMgr.Write(str);
Sleep(m_SendDelay);
}
//开关光栅,避免漏光
{
CString str1 = "EXT=1";
CString str = str1+char(13)+char(10);
m_ComMgr.Write(str);
Sleep(m_SendDelay);
}
{
CString str1 = "EXT=0";
CString str = str1+char(13)+char(10);
m_ComMgr.Write(str);
Sleep(m_SendDelay);
}
}
//设置频率和电流f 单位khz
void COptoWaveLaserMgr::SetFrequencyCurrent()
{
int f = m_Fre;
double c = m_PowerCurr;
//打开串口-----------------------------
if(!m_ComMgr.IsOpen())
{
return;
}
{
CString str1 = "PRF=";//设置频率kHZ
CString str2;
str2.Format("%ld",(f*1000));
CString str = str1+str2+char(13)+char(10);
m_ComMgr.Write(str);
Sleep(m_SendDelay);
}
{
CString str1 = "IDI=";//设置电流,A
long Current = c*10;
CString str2;
str2.Format("%ld",Current);
CString str = str1+str2+char(13)+char(10);
m_ComMgr.Write(str);
Sleep(m_SendDelay);
}
}
//打开关闭二极管
void COptoWaveLaserMgr::SwitchDiode(bool b)
{
//打开串口-----------------------------
if(!m_ComMgr.IsOpen())
{
return;
}
if(b)
{
CString str1 = "DIO=1";
CString str = str1+char(13)+char(10);
m_ComMgr.Write(str);
Sleep(m_SendDelay);
}
else
{
CString str1 = "DIO=0";
CString str = str1+char(13)+char(10);
m_ComMgr.Write(str);
Sleep(m_SendDelay);
}
}
//获取当前的激光能量(焦耳)
double COptoWaveLaserMgr::GetLaserPower()
{
//根据电流和功率的对照表查出对应的功率值
CFileMgr FileMgr;
CString FullPath;//完整路径
FileMgr.GetFullFilePath(FullPath,LASER_POWER_FILE);
//提取校准数据
vector<vector<double>> Vec;//电流,频率,功率
FileMgr.ReadFileToDoubleVec(FullPath,Vec);
double p = 1;
//查询电流最相近的功率
double MinDiff = 0;//最小差
double FindVal = m_PowerCurr;//查询值
vector<vector<double>>::iterator iter = Vec.begin();
vector<vector<double>>::iterator iter_end = Vec.end();
for(;iter!=iter_end;iter++)
{
vector<double> &Arr = (*iter);
if(Arr.size()==3)
{
double CurVal = Arr[0];//当前值
double Diff = FindVal-CurVal;
if(iter == Vec.begin())
{
MinDiff = Diff;
p = Arr[2];
}
else if(abs(Diff)<abs(MinDiff))
{
MinDiff = Diff;
p = Arr[2];
}
}
}
return p;
}
#endif