diff --git a/LaiPuLaser/GlobalDefine.h b/LaiPuLaser/GlobalDefine.h index 8d3b3a8..9099a7e 100644 --- a/LaiPuLaser/GlobalDefine.h +++ b/LaiPuLaser/GlobalDefine.h @@ -133,6 +133,9 @@ inline bool IsDbEqual(double a,double b) class Dbxy { friend bool operator==(const Dbxy &lhs, const Dbxy &rhs); + friend bool operator!=(const Dbxy &lhs, const Dbxy &rhs); + friend Dbxy operator+(const Dbxy &lhs, const Dbxy &rhs); + friend Dbxy operator-(const Dbxy &lhs, const Dbxy &rhs); public: Dbxy(double _x,double _y):x(_x),y(_y){}; Dbxy() @@ -154,7 +157,18 @@ inline bool operator==(const Dbxy &lhs, const Dbxy &rhs) { return (IsDbEqual(lhs.x,rhs.x) && IsDbEqual(lhs.y,rhs.y)); } - +inline bool operator!=(const Dbxy &lhs, const Dbxy &rhs) +{ + return ((!IsDbEqual(lhs.x, rhs.x)) || (!IsDbEqual(lhs.y, rhs.y))); +} +inline Dbxy operator+(const Dbxy &lhs, const Dbxy &rhs) +{ + return Dbxy((lhs.x+ rhs.x),(lhs.y+ rhs.y)); +} +inline Dbxy operator-(const Dbxy &lhs, const Dbxy &rhs) +{ + return Dbxy((lhs.x - rhs.x), (lhs.y - rhs.y)); +} //抓取点的类型 class CCatchPoint :public Dbxy { diff --git a/LaiPuLaser/MarkArea.cpp b/LaiPuLaser/MarkArea.cpp index 984b957..d87c92a 100644 --- a/LaiPuLaser/MarkArea.cpp +++ b/LaiPuLaser/MarkArea.cpp @@ -240,8 +240,8 @@ void CMarkArea::CollectWorkData(bool bNeedSel,CProduct &Product) { Dbxy AllOffset2; AllOffset2 = gCommonFlowMgr->GetAdjustOffsetAll(); - Offset.x = AllOffset2.x - m_RealBasePt.x; - Offset.y = AllOffset2.y - m_RealBasePt.y; + Offset.x = AllOffset2.x;//-m_RealBasePt.x; + Offset.y = AllOffset2.y;// -m_RealBasePt.y; } if(bNeedSel) @@ -286,6 +286,11 @@ void CMarkArea::CollectWorkData(bool bNeedSel,CProduct &Product) SpecialWorkDataVec.clear(); return; } + + Product.TheoryDataToRealData(WorkDataVec, m_BasePt,Offset); + Product.TheoryDataToRealData(SpecialWorkDataVec, m_BasePt,Offset); + return; + //根据抓取两个mark 来计算拉伸数据 if(gCommonFlowMgr->IsbStretchDataToRealSize()) { diff --git a/LaiPuLaser/Product.cpp b/LaiPuLaser/Product.cpp index e290661..b458b47 100644 --- a/LaiPuLaser/Product.cpp +++ b/LaiPuLaser/Product.cpp @@ -83,8 +83,8 @@ void CProduct::ResetRealMarkPt() m_RealMarkPt1.y = 0; m_RealMarkPt2.x = 0; m_RealMarkPt2.y = 0; - m_RealMarkPt3.x = 0; - m_RealMarkPt3.y = 0; + //m_RealMarkPt3.x = 0; + //m_RealMarkPt3.y = 0; //偏移和旋转都要重设 m_Offset.x = 0;//理论数据映射为平台坐标的偏移X m_Offset.y = 0;//理论数据映射为平台坐标的偏移Y @@ -140,6 +140,9 @@ void CProduct::SetRealMarkPt(Dbxy pt) if(!IsSetRealMarkPt1() || !IsSetRealMarkPt2()) return; + CalAffinePars(); + return; + //计算偏移旋转 CalTheoryToRealPar(); @@ -157,20 +160,98 @@ void CProduct::SetRealMarkPt(Dbxy pt) CalTheoryToRealPar(); } } + +#include +using namespace cv; + +void CProduct::CalAffinePars() +{ + CString logstr; + + o_TheoryMarkPt1 = m_TheoryMarkPt1 - m_BasePt; + logstr.Format("TheoryMarkPt1 Coord(%f,%f)", o_TheoryMarkPt1.x, o_TheoryMarkPt1.y); + gLogMgr->WriteDebugLog(logstr); + + o_TheoryMarkPt2 = m_TheoryMarkPt2 - m_BasePt; + logstr.Format("TheoryMarkPt2 Coord(%f,%f)", o_TheoryMarkPt2.x, o_TheoryMarkPt2.y); + gLogMgr->WriteDebugLog(logstr); + + o_TheoryMarkPt3 = m_TheoryMarkPt3 - m_BasePt; + logstr.Format("TheoryMarkPt3 Coord(%f,%f)", o_TheoryMarkPt3.x, o_TheoryMarkPt3.y); + gLogMgr->WriteDebugLog(logstr); + + o_RealMarkPt1 = m_RealMarkPt1 - m_BasePt; + logstr.Format("RealMarkPt1 Coord(%f,%f)", o_RealMarkPt1.x, o_RealMarkPt1.y); + gLogMgr->WriteDebugLog(logstr); + + o_RealMarkPt2 = m_RealMarkPt2 - m_BasePt; + logstr.Format("RealMarkPt2 Coord(%f,%f)", o_RealMarkPt2.x, o_RealMarkPt2.y); + gLogMgr->WriteDebugLog(logstr); + + o_RealMarkPt3 = m_RealMarkPt3 - m_BasePt; + logstr.Format("RealMarkPt3 Coord(%f,%f)", o_RealMarkPt3.x, o_RealMarkPt3.y); + gLogMgr->WriteDebugLog(logstr); + + double TheoryDis = CalDistance(o_TheoryMarkPt1, o_TheoryMarkPt2); + logstr.Format("[Mark1&2理论间距]: [%f]", TheoryDis); + gLogMgr->WriteDebugLog(logstr); + + double RealDis = CalDistance(o_RealMarkPt1, o_RealMarkPt2); + logstr.Format("[Mark1&2真实间距]: [%f]", RealDis); + gLogMgr->WriteDebugLog(logstr); + + double DisDiff = abs(TheoryDis - RealDis); + logstr.Format(_T("[Mark 间距误差] = [%f]"), DisDiff); + gLogMgr->WriteDebugLog(logstr); + + if (DisDiff > abs(gProductMgr->GetMaxMarkDisDiff())) + { + gTrackWorkFlow1.RadAlamOnOff(true);//报警提示 + ResetRealMarkPt(); + CString LogStr("Mark 间距误差超出范围,定位可能误判!"); + AfxMessageBox(logstr); + gTrackWorkFlow1.RadAlamOnOff(false);//报警提示 + CExceptionMsg Msg; + Msg.SetMsg(CString("")); + throw Msg;//抛出异常 + /* CExceptionMsg Msg; + Msg.SetMsg(LogStr); + throw Msg;*/ + } + + Point2f ThroryCoords[3]; + Point2f RealCoords[3]; + + Mat warp_mat(2, 3, CV_32FC1); + + ThroryCoords[1] = Point2f(o_TheoryMarkPt1.x, o_TheoryMarkPt1.y); + ThroryCoords[2] = Point2f(o_TheoryMarkPt2.x, o_TheoryMarkPt2.y); + ThroryCoords[0] = Point2f(o_TheoryMarkPt3.x, o_TheoryMarkPt3.y); + + RealCoords[1] = Point2f(o_RealMarkPt1.x, o_RealMarkPt1.y); + RealCoords[2] = Point2f(o_RealMarkPt2.x, o_RealMarkPt2.y); + RealCoords[0] = Point2f(o_RealMarkPt3.x, o_RealMarkPt3.y); + + //得放射变换参数矩阵 + warp_mat = getAffineTransform(ThroryCoords, RealCoords); + warp_mat.convertTo(warp_mat, CV_32FC1);//不转化时,默认CV_64FC1 后续计算会错误 + + //取出6个变换参数 + m_p00 = warp_mat.at(0, 0); + m_p01 = warp_mat.at(0, 1); + m_p02 = warp_mat.at(0, 2); + + m_p10 = warp_mat.at(1, 0); + m_p11 = warp_mat.at(1, 1); + m_p12 = warp_mat.at(1, 2); + + +} //计算真实数据的偏移和旋转值 void CProduct::CalTheoryToRealPar() { gLogMgr->WriteDebugLog("func : CalTheoryToRealPar"); - - if (gLogMgr->IsDebuging())//调试时,理论点用作实际点. - { - m_RealMarkPt1 = m_TheoryMarkPt1; - m_RealMarkPt2 = m_TheoryMarkPt2; - m_Offset.x = 0; - m_Offset.y = 0; - m_RotateAng = 0; - return; - } + //以第一个点的来计算相对的偏移 m_Offset.x = m_RealMarkPt1.x - m_TheoryMarkPt1.x; m_Offset.y = m_RealMarkPt1.y - m_TheoryMarkPt1.y; @@ -263,27 +344,6 @@ void CProduct::CalRealStretchPar() //理论数据转换为实际数据 void CProduct::TheoryDataToRealData(vector &vec,Dbxy &Offset) { -#ifdef Create_MarkData_file - CStdioFile sf; - if (!sf.Open("MarkData.txt", CFile::modeCreate | CFile::modeReadWrite | CFile::modeNoTruncate)) - sf.m_hFile = NULL; - CString str; - vector::iterator iter = vec.begin(); - vector::iterator iter_end = vec.end(); - for (; iter != iter_end; iter++) - { - (*iter) = TheoryPtToRealPt((*iter)); - (*iter).x += Offset.x; - (*iter).y += Offset.y; - if (sf.m_hFile != NULL) - { - str.Format("MarkAreaCenter: X=%f;Y=%f \n", (*iter).x, (*iter).y); - sf.SeekToEnd(); - sf.WriteString(str); - } - } - sf.Close(); -#else vector::iterator iter = vec.begin(); vector::iterator iter_end = vec.end(); for(;iter!=iter_end;iter++) @@ -292,7 +352,41 @@ void CProduct::TheoryDataToRealData(vector &vec,Dbxy &Offset) (*iter).x += Offset.x; (*iter).y += Offset.y; } -#endif +} + +//理论数据转换为实际数据 +void CProduct::TheoryDataToRealData(vector &vec, Dbxy &BaseOffset, Dbxy CutAdjust) +{ + vector::iterator iter = vec.begin(); + vector::iterator iter_end = vec.end(); + for (;iter != iter_end;iter++) + { + //(*iter) = TheoryPtToRealPt((*iter)); + //(*iter) = (*iter) - m_BasePt; + auto x = (*iter).x; + auto y = (*iter).y; + double retx = m_p00*x + m_p01*y + m_p02+ CutAdjust.x; + double rety = m_p10*x + m_p11*y + m_p12+ CutAdjust.y; + (*iter).x = retx; + (*iter).y = rety; + + auto temp = BaseOffset - m_BasePt; + + (*iter) = (*iter) - temp; + + /*(*iter).x += CutAdjust.x; + (*iter).y += CutAdjust.y;*/ + } +} +void CProduct::ResetAffinePars() +{ + m_p00 = 1; + m_p01 = 0; + m_p02 = 0; + + m_p10 = 0; + m_p11 = 1; + m_p12 = 0; } //获取实际产品的尺寸比例 Dbxy CProduct::CalRealProductScale() @@ -434,4 +528,16 @@ void CProduct::TheoryDataToRealData(vector> &vec,Dbxy &Offset) } } + +//理论数据转换为实际数据 +void CProduct::TheoryDataToRealData(vector> &vec, Dbxy &BaseOffset, Dbxy CutAdjust) +{ + gLogMgr->WriteDebugLog("CProduct::TheoryDataToRealData"); + vector>::iterator iter = vec.begin(); + vector>::iterator iter_end = vec.end(); + for (;iter != iter_end;iter++) + { + TheoryDataToRealData(*iter, BaseOffset,CutAdjust); + } +} #endif diff --git a/LaiPuLaser/Product.h b/LaiPuLaser/Product.h index 818461c..5c803a5 100644 --- a/LaiPuLaser/Product.h +++ b/LaiPuLaser/Product.h @@ -27,13 +27,18 @@ public: void SetbHasMarkPt3(bool b){m_bHasMarkPt3 = b;}; void SetTheoryMark3Pt(Dbxy pt){m_TheoryMarkPt3 = pt;}; void SetRealMark3Pt(Dbxy pt){m_RealMarkPt3 = pt;}; - Dbxy TheoryPtToRealPt(Dbxy TheoryMarkPt); - void TheoryDataToRealData(vector> &vec,Dbxy &Offset); + Dbxy TheoryPtToRealPt(Dbxy TheoryMarkPt); void StretchDataToRealSize(vector> &vec); - void TheoryDataToRealData(vector &vec,Dbxy &Offset); - void TheoryDataToRealData(vector> &SrcVec,vector> &DecVec,Dbxy &Offset); - void TheoryDataToRealData(vector &SrcVec,vector &DecVec,Dbxy &Offset); - bool IsSetRealMarkPt1(); + + void TheoryDataToRealData(vector> &vec, Dbxy &Offset); + void TheoryDataToRealData(vector &vec, Dbxy &Offset); + void TheoryDataToRealData(vector& vec, Dbxy & BaseOffset, Dbxy CutAdjust); + void ResetAffinePars();//重置仿射变换参数 + void TheoryDataToRealData(vector>& vec, Dbxy & BaseOffset, Dbxy CutAdjust); + + void TheoryDataToRealData(vector> &SrcVec,vector> &DecVec,Dbxy &Offset); + void TheoryDataToRealData(vector &SrcVec,vector &DecVec,Dbxy &Offset); + bool IsSetRealMarkPt1(); bool IsSetRealMarkPt2(); void UseDefualtOffset(); bool IsMarkReady(); @@ -42,12 +47,13 @@ public: void SetbNewAddProduct(bool bNew) { m_bNewAddProduct = bNew; };//设置是否是改造新增的产品 bool IsbNewAddProduct() { return m_bNewAddProduct; };//查询是否是改造新增的产品 private: + void CalAffinePars(); void CalTheoryToRealPar(); Dbxy CalRealProductScale(); void StretchPt(Dbxy &Pt,SObjOperatePar &Par); void CalRealStretchPar(); double CalRealProductScaleExt(Dbxy TheoryMarkPt1,Dbxy TheoryMarkPt2,Dbxy RealMarkPt1,Dbxy RealMarkPt2); -public: +private: Dbxy m_BasePt;//工件的基准坐标点 bool m_bUsed;//是否使用 bool m_bLastOne;//是否为最后一个 @@ -62,6 +68,23 @@ public: Dbxy m_RealMarkPt1;//真实坐标(相对于激光中心点) mm Dbxy m_RealMarkPt2; Dbxy m_RealMarkPt3; +public: + Dbxy o_TheoryMarkPt1;//理论坐标(不移动时) mm + Dbxy o_TheoryMarkPt2; + Dbxy o_TheoryMarkPt3; + //ccd 抓取的实际值------------------------------------ + Dbxy o_RealMarkPt1;//真实坐标(不移动时) mm + Dbxy o_RealMarkPt2; + Dbxy o_RealMarkPt3; + + //仿射参数------------------------------------ + float m_p00=1; + float m_p01=0; + float m_p02=0; + + float m_p10=0; + float m_p11=1; + float m_p12=0; public: //实际偏移旋转结果--------------------------------- Dbxy m_Offset;//理论数据映射为平台坐标的偏移 diff --git a/LaiPuLaser/Program_SZ_XL_TrackWorkFlow.cpp b/LaiPuLaser/Program_SZ_XL_TrackWorkFlow.cpp index 418c9e5..9883848 100644 --- a/LaiPuLaser/Program_SZ_XL_TrackWorkFlow.cpp +++ b/LaiPuLaser/Program_SZ_XL_TrackWorkFlow.cpp @@ -817,8 +817,8 @@ void CTrackWorkFlow::MarkProcess() //设置当前的工作步骤 SetCurTrackWorkStep(_ETrack_Step_Marking); //数据移动回默认的位置 - gProgramCutMgr->MoveObjData(Dbxy(0, 0)); - gMarkAreaMgr->MoveAllAreaToTargetPt(Dbxy(0, 0)); + //gProgramCutMgr->MoveObjData(Dbxy(0, 0)); + //gMarkAreaMgr->MoveAllAreaToTargetPt(Dbxy(0, 0)); //gCurLockTrackType = _ETrackType_NULL;//解锁 @@ -839,6 +839,8 @@ bool CTrackWorkFlow::MarkProcessExt() //gProgram_SZ_XL->SetCurMarkingTrack(m_TrackType); CProduct &Product = gServer->m_RcvProduct; +/* + Dbxy BasePt = Product.GetProductBasePt(); //=======移动obj============== gProgramCutMgr->MoveObjData(BasePt); @@ -847,6 +849,7 @@ bool CTrackWorkFlow::MarkProcessExt() gObjComponentMgr->CalAllObjCenterPt(); Dbxy AllObjCenterPt = gObjComponentMgr->GetAllObjCenterPt(); gMarkAreaMgr->MoveAllAreaToTargetPt(AllObjCenterPt); +*/ bool Ret; //启动切割 diff --git a/LaiPuLaser/WorkCmdMarkArea.cpp b/LaiPuLaser/WorkCmdMarkArea.cpp index 24d7a89..7cf6b68 100644 --- a/LaiPuLaser/WorkCmdMarkArea.cpp +++ b/LaiPuLaser/WorkCmdMarkArea.cpp @@ -57,7 +57,8 @@ bool CWorkCmdMarkArea::Excute() m_MarkArea.SetSelState(true);//选择当前加工的area //设置markarea 内的obj 为已加工状态 - gObjComponentMgr->SetMarkedStateRect(m_MarkArea.GetRect(),m_bSelMark); + // gObjComponentMgr->SetMarkedStateRect(m_MarkArea.GetRect(), m_bSelMark); + gObjComponentMgr->SetMarkedStateRect(m_MarkArea.GetRect(),true); //可能会引起报错? //m_pView->MoveViewCenter(); //m_pView->RefreshView(); diff --git a/LaiPuLaser/hlp/LaiPuLaser.chm b/LaiPuLaser/hlp/LaiPuLaser.chm index b1a86fd..d28b7f6 100644 Binary files a/LaiPuLaser/hlp/LaiPuLaser.chm and b/LaiPuLaser/hlp/LaiPuLaser.chm differ