#include "StdAfx.h" #include "CHAfmControls/AFMControl_Msg.h" #include "AFT_LIB/atf_lib.h" #include #pragma comment(lib, "legacy_stdio_definitions.lib") CAfmControl_Msg::CAfmControl_Msg(int nIndex) : CAfmControl(nIndex) { m_dSpeed = 1.0; m_hMutex = CreateMutex(NULL, FALSE, _T("AFMMutex")); } CAfmControl_Msg::~CAfmControl_Msg(void) { CloseHandle(m_hMutex); } int CAfmControl_Msg::Connect(const CAfmControlInfo& controlInfo) { WaitForSingleObject(m_hMutex, INFINITE); m_bConnected = FALSE; m_ControlInfo = controlInfo; CString strPort = _T("COM") + m_ControlInfo.GetConnectionPort(); //strPort.Format(_T("COM%d"), connParam.nPortNum); #ifdef UNICODE int Ret = 0; Ret = WideCharToMultiByte(CP_ACP, 0, strPort, (int)_tcslen(strPort), m_strPort, 15, NULL, NULL); m_strPort[Ret] = 0; #endif //CWinThread* thread = AfxBeginThread(MyThreadProc, m_strPort); m_nBaudrate_start = 9600; // upon powercycle sensor always starts with 9600 int baudrate_run = 57600; baudrate_run = 115200; DWORD dTest = 115200; int point_count = -1; // open port, return if failed int ret = atf_OpenConnection(m_strPort, m_nBaudrate_start); if (ret) { atf_CloseConnection(); ReleaseMutex(m_hMutex); return 0; } else { atf_WriteBaud(115200); if(atf_ChangeCommBaudrate(115200)) { atf_CloseConnection(); ReleaseMutex(m_hMutex); return 0; } //atf_ChangeCommBaudrate(115200); /*int nBaudrate = 0; ret=atf_ReadBaud(nBaudrate); if (atf_ChangeCommBaudrate(dTest)) { atf_CloseConnection(); ReleaseMutex(m_hMutex); return 0; }*/ } Sleep(100); if(m_bConnected = atf_isSerialConnection()) { atf_SetComIdx(m_nIndex); Sleep(100); atf_ReadMicrostep(&m_nMicroSteps); Sleep(100); atf_ReadStepPerMmConversion(&m_nFullsteps); Sleep(100); atf_ReadHomingZ_Parameters(m_pAFMHomingZParam); } if (m_bConnected==0) { ReleaseMutex(m_hMutex); return 0; } m_nRecipeIndex = 0; m_strRecipeName = _T("Default"); int nZoomIndex = m_nZoomIndex = -1; ret = atf_ReadObjNum(&nZoomIndex); if (ret==0) { m_nZoomIndex = nZoomIndex; } RecipeZoom(m_nZoomIndex); ReleaseMutex(m_hMutex); return 1; } void CAfmControl_Msg::Disconnect() { int ret; if(atf_GetComIdx() != m_nIndex) { ret = atf_SetComIdx(m_nIndex); } atf_CloseConnection(); ReleaseMutex(m_hMutex); } int CAfmControl_Msg::RecipeJogSpeed(double dSpeed) { m_dSpeed = dSpeed; return 1; } int CAfmControl_Msg::RecipeJogCommand(int nCmd) { int ret; // if(!atf_isSerialConnection()) return 0; if(!m_bConnected) return 0; if(atf_GetComIdx() != m_nIndex) { ret = atf_SetComIdx(m_nIndex); if(ret) return 0; } int nPos = 0; switch(nCmd) { case 0: ret = 0; break; case AfmJogCmd_Down: nPos = int( ((double)m_dSpeed * -m_nMicroSteps * m_nFullsteps)/1000.0 ); ret = atf_MoveZ(nPos); break; case AfmJogCmd_Up: nPos = int( ((double)-m_dSpeed * -m_nMicroSteps * m_nFullsteps)/1000.0 ); ret = atf_MoveZ(nPos); break; } if(ret) return 0; return 1; } int CAfmControl_Msg::RecipeTracking(int nTracking) { return 1; } int CAfmControl_Msg::RecipeZoom(int nZoomIndex) { int ret; // if(!atf_isSerialConnection()) return 0; if(!m_bConnected) return 0; if(atf_GetComIdx() != m_nIndex) { ret = atf_SetComIdx(m_nIndex); if(ret) return 0; } ret = atf_WriteObjNum(nZoomIndex); if(ret) return 0; return 1; } int CAfmControl_Msg::RecipeChange(int nRecipeIndex, int nZoomIndex) { return RecipeZoom(nZoomIndex); } int CAfmControl_Msg::RecipeChange(const CString& strRecipeName, int nZoomIndex) { return RecipeZoom(nZoomIndex); } BOOL CAfmControl_Msg::SetTracking(int nTracking) { int ret; // if(!atf_isSerialConnection()) return 0; if(!m_bConnected) return 0; if(atf_GetComIdx() != m_nIndex) { ret = atf_SetComIdx(m_nIndex); if(ret) return 0; } if(nTracking) { ret = atf_AFTrack(); } else { ret = atf_AfStop(); } if(ret) return FALSE; return TRUE; } BOOL CAfmControl_Msg::SetZoomIndex(int nZoomIndex) { int ret; // if(!atf_isSerialConnection()) return 0; if(!m_bConnected) return 0; if(atf_GetComIdx() != m_nIndex) { ret = atf_SetComIdx(m_nIndex); if(ret) return 0; } ret = atf_WriteObjNum(nZoomIndex); if(ret) return FALSE; m_nZoomIndex = nZoomIndex; return TRUE; } BOOL CAfmControl_Msg::SetRecipeIndex(int nRecipeIdnex, int nZoomIndex) { int ret; if (nZoomIndex!=-1) { ret = SetZoomIndex(nZoomIndex); } return ret; } BOOL CAfmControl_Msg::GetConnected() const { int ret; // if(!atf_isSerialConnection()) return 0; if(!m_bConnected) return 0; if(atf_GetComIdx() != m_nIndex) { ret = atf_SetComIdx(m_nIndex); if(ret) return 0; } return atf_isSerialConnection(); } BOOL CAfmControl_Msg::SetRecipeName(const CString& strRecipeName, int nZoomIndex) { int ret; if (nZoomIndex!=-1) { ret = SetZoomIndex(nZoomIndex); } // ReleaseMutex(m_hMutex); return ret; } BOOL CAfmControl_Msg::GetTracking(int& nTracking) const { nTracking = m_nTracking; return TRUE; } BOOL CAfmControl_Msg::GetZoomIndex(int& nZoomIndex) const { nZoomIndex = m_nZoomIndex; return TRUE; } BOOL CAfmControl_Msg::GetRecipeIndex(int& nRecipeIdnex, int& nZoomIndex) const { nRecipeIdnex = m_nRecipeIndex; nZoomIndex = m_nZoomIndex; return TRUE; } BOOL CAfmControl_Msg::GetRecipeName(CString& strRecipeName, int& nZoomIndex) const { strRecipeName = strRecipeName; nZoomIndex = m_nZoomIndex; return TRUE; } BOOL CAfmControl_Msg::MoveToLimitPlus() { return FALSE; int ret; // if(!atf_isSerialConnection()) return 0; if(!m_bConnected) return 0; if(atf_GetComIdx() != m_nIndex) { ret = atf_SetComIdx(m_nIndex); if(ret) return 0; } //ret = atf_MoveZ(((double)300000 * m_nMicroSteps * m_nFullsteps)/1000); int arrHomeParam[8]; memcpy(arrHomeParam, m_pAFMHomingZParam, sizeof(int) * 8); arrHomeParam[3] = arrHomeParam[7] = 0; ret = atf_RunHomingZ(arrHomeParam); if(ret) return FALSE; return TRUE; } BOOL CAfmControl_Msg::MoveToLimitMinus() { return FALSE; // int ret; // if(!atf_isSerialConnection()) return 0; if(!m_bConnected) return 0; if(atf_GetComIdx() != m_nIndex) { ret = atf_SetComIdx(m_nIndex); if(ret) return 0; } //ret = atf_MoveZ(((double)-300000 * m_nMicroSteps * m_nFullsteps)/1000); int arrHomeParam[8]; memcpy(arrHomeParam, m_pAFMHomingZParam, sizeof(int) * 8); // arrHomeParam[3] = arrHomeParam[7] = 0; // // arrHomeParam[0] *= -1; // // arrHomeParam[1] *= -1; // // arrHomeParam[2] *= -1; //ret = atf_RunHomingZ(arrHomeParam); const int homing_distance = -10000; const int homing_back_stepsize = 1; const int limit_sw=(1<<16); const int moving=(1<<12); ret = atf_MoveZum(homing_distance); if (ret) return FALSE; int hw_status; clock_t ts=clock(); do { ret = atf_ReadHwStat(&hw_status); if (ret) return FALSE; }while(~hw_status & limit_sw && hw_status & moving || clock()-ts