#define GRAT_EXPORT #define _CRT_SECURE_NO_WARNINGS #include "gratfocus.h" #include "runtime.h" #include #include DWORD DelayTime = 100; DWORD dwDevice = 1; HINSTANCE hPmacLib; int DriverOpen = 0; char strTemp[255], sinput[50]; void RunLink(void); void SetupMotor(int axis, double ix19, int ix20, int ix21, double ix22); int GratingMovingCheck(void); void RunLink() { hPmacLib = LoadLibrary("PComm64.dll"); DeviceOpen = (OPENPMACDEVICE)GetProcAddress(hPmacLib,"OpenPmacDevice"); DeviceClose = (CLOSEPMACDEVICE)GetProcAddress(hPmacLib,"ClosePmacDevice"); DeviceGetRomVersion = (GETROMVERSIONA)GetProcAddress(hPmacLib,"PmacGetRomVersionA"); DeviceGetRomDate = (GETROMDATEA)GetProcAddress(hPmacLib,"PmacGetRomDateA"); DeviceGetResponse = (GETRESPONSEA)GetProcAddress(hPmacLib,"PmacGetResponseA"); DeviceDownload = (DOWNLOADA)GetProcAddress(hPmacLib, "PmacDownloadA"); DeviceGetBuffer = (GETBUFFERA)GetProcAddress(hPmacLib, "PmacGetBufferA"); DeviceSetQuickGather = (SETQUICKGATHER)GetProcAddress(hPmacLib,"PmacSetQuickGather"); DeviceCollectGatherData = (COLLECTGATHERDATA)GetProcAddress(hPmacLib,"PmacCollectGatherData"); DeviceGetGatherSamples = (GETGATHERSAMPLES)GetProcAddress(hPmacLib, "PmacGetGatherSamples"); DeviceGetIVariableLong = (GETIVARIABLELONG)GetProcAddress(hPmacLib, "PmacGetIVariableLong"); Sleep(1000); } GRAT_API void SlitHatchClose(void) { DeviceGetResponse(dwDevice, strTemp, 255, "m7100=0"); DeviceGetResponse(dwDevice, strTemp, 255, "m7101=1"); } GRAT_API void SlitHatchOpen(void) { DeviceGetResponse(dwDevice, strTemp, 255, "m7100=1"); DeviceGetResponse(dwDevice, strTemp, 255, "m7101=0"); } void SetupMotor(int axis, double ix19, int ix20, int ix21, double ix22) { int AddressBit; // jog & home 움직임의 최대 가속도 설정 (counts/msec^2). sprintf(sinput, "I%d19 = %lf", axis, ix19); DeviceGetResponse( dwDevice, strTemp, 255, sinput); // jogging & homing 시 가속 시간 (msec) sprintf(sinput, "I%d20=%d", axis, ix20); DeviceGetResponse( dwDevice, strTemp, 255, sinput); // jogging & homing 시 S-Curve 시간 (msec) sprintf(sinput, "I%d21=%d", axis, ix21); DeviceGetResponse( dwDevice, strTemp, 255, sinput); // jog speed sprintf(sinput, "I%d22=%lf", axis, ix22); DeviceGetResponse( dwDevice, strTemp, 255, sinput); // home speed and direction //if (axis==1) // sprintf(sinput, "I%d23=%lf", axis, ix22); // else sprintf(sinput, "I%d23=-%lf", axis, ix22); DeviceGetResponse( dwDevice, strTemp, 255, sinput); // Set the limitor sprintf(sinput, "i%d24=$500001", axis); // h/w limit enable / disable = i%d24=$520001 DeviceGetResponse( dwDevice, strTemp, 255, sinput); // set limitor status bit (mxx31: positive-end-limit, mxx32: negative-end-limit) AddressBit = 0xB0 + (axis-1) * 0x80; sprintf(sinput, "m%d31->X:$%06X,21,1", axis, AddressBit); DeviceGetResponse( dwDevice, strTemp, 255, sinput); sprintf(sinput, "m%d32->X:$%06X,22,1", axis, AddressBit); DeviceGetResponse( dwDevice, strTemp, 255, sinput); // motor running status bit setup sprintf(sinput, "m%d37->X:$%06X,17,1", axis, AddressBit); DeviceGetResponse( dwDevice, strTemp, 255, sinput); // motor inPosition bit setup sprintf(sinput, "m%d40->X:$%06X,0,1", axis, AddressBit + 0x10); } GRAT_API int OpenClipper() { int drvStatus; drvStatus = OpnDriver(); if (drvStatus == 1) { // Go Grating to home position Go2Home(1); // Slit Hatch Open (Currently not working) //SlitHatchOpen(); Sleep(DelayTime); } return drvStatus; } GRAT_API int CloseClipper() { int closeStatus; //SlitHatchClose(); (Currently not working) //MoveGrating(-50000); Go2PLimit(1); //move grating to gravitationally stable position Sleep(5000); closeStatus = ClsDriver(); return closeStatus; } GRAT_API void MoveGrating(int MotorStep) { int motorRun; MotorStep *= -1; sprintf(sinput, "#1J=%d", MotorStep); DeviceGetResponse(dwDevice, strTemp, 255, sinput); /* do { motorRun = GratingMovingCheck(); Sleep(DelayTime); } while(motorRun); */ } GRAT_API void MoveCamFocus(int cam, int MotorStep) { sprintf(sinput, "#%dJ=%d", cam+1, MotorStep); DeviceGetResponse(dwDevice, strTemp, 255, sinput); } GRAT_API void getresponse(char* string, char* strRes) { DeviceGetResponse(dwDevice, strRes, 255, string); } int GratingMovingCheck(void) { sprintf(sinput, "m137"); DeviceGetResponse( dwDevice, strTemp, 255, sinput); return atoi(strTemp); } GRAT_API int MovingCheck(int axis) { sprintf(sinput, "m%d37", axis); DeviceGetResponse( dwDevice, strTemp, 255, sinput); return atoi(strTemp); } GRAT_API int InPositionCheck(int axis) { sprintf(sinput, "m%d40", axis); DeviceGetResponse( dwDevice, strTemp, 255, sinput); return atoi(strTemp); } GRAT_API double ReadPosition(int axis) { sprintf(sinput, "#%dP", axis); DeviceGetResponse(dwDevice, strTemp, 255, sinput); return atof(strTemp); } GRAT_API void ReadLimitStatus(int axis, int* pLimit, int* nLimit) { sprintf(sinput, "m%d31", axis); DeviceGetResponse( dwDevice, strTemp, 255, sinput); *pLimit = atoi(strTemp); sprintf(sinput, "m%d32", axis); DeviceGetResponse( dwDevice, strTemp, 255, sinput); *nLimit = atoi(strTemp); } GRAT_API void Go2PLimit(int axis) { sprintf(sinput, "#%dj+", axis); // CW rotation DeviceGetResponse( dwDevice, strTemp, 255, sinput); } GRAT_API void Go2NLimit(int axis) { sprintf(sinput, "#%dj-", axis); // CCW rotation DeviceGetResponse( dwDevice, strTemp, 255, sinput); } GRAT_API void Go2Home(int axis) { sprintf(sinput, "#%dhm", axis); // CCW rotation DeviceGetResponse( dwDevice, strTemp, 255, sinput); } GRAT_API void MotorStop(int axis) { sprintf(sinput, "#%dj/", axis); DeviceGetResponse( dwDevice, strTemp, 255, sinput); } GRAT_API void MotorOnOff(int axis, int OnOffSwitch) { sprintf(sinput, "I%d00=%d", axis, OnOffSwitch); DeviceGetResponse( dwDevice, strTemp, 255, sinput); } GRAT_API void ReadSlitHatchLimit(int* plim,int* nlim) { sprintf(sinput, "m7007"); DeviceGetResponse( dwDevice, strTemp, 255, sinput); *plim = atoi(strTemp); sprintf(sinput, "m7006"); DeviceGetResponse( dwDevice, strTemp, 255, sinput); *nlim = atoi(strTemp); } GRAT_API int OpnDriver() { char vs[30], ds[30]; int i; hPmacLib = NULL; RunLink(); DriverOpen = DeviceOpen(dwDevice); if (DriverOpen) { DeviceGetRomVersion(dwDevice, vs, 30); DeviceGetRomDate(dwDevice, ds, 30); Sleep(DelayTime); for (i=1; i<=3; i++) { sprintf(sinput, "#%do0", i); // Open-loop output of zero magnitude DeviceGetResponse( dwDevice, strTemp, 255, sinput); sprintf(sinput, "#%dj/", i); // Re-establish closed-loop control, stop the motor if it hase been moving in open-loop mode. DeviceGetResponse( dwDevice, strTemp, 255, sinput); } Sleep(DelayTime); // Enable the home switch of grating sprintf(sinput, "i7012=2"); // 2,6: Capture on Flag high / 10,14: Capture on Flag low DeviceGetResponse( dwDevice, strTemp, 255, sinput); // Set N limit to the home sprintf(sinput, "i7022=6"); DeviceGetResponse( dwDevice, strTemp, 255, sinput); sprintf(sinput, "i7023=2"); DeviceGetResponse( dwDevice, strTemp, 255, sinput); sprintf(sinput, "i7032=6"); DeviceGetResponse( dwDevice, strTemp, 255, sinput); sprintf(sinput, "i7033=2"); DeviceGetResponse( dwDevice, strTemp, 255, sinput); sprintf(sinput, "M120->X:$078000,16"); DeviceGetResponse( dwDevice, strTemp, 255, sinput); SetupMotor(1, 0.01, 100, 50, 1); SetupMotor(2, 0.02, 100, 50, 2); SetupMotor(3, 0.02, 100, 50, 2); Sleep(DelayTime); return 1; } else return ERR_OpnClipper; } GRAT_API int ClsDriver() { int i; Sleep(DelayTime); if (DriverOpen) { DriverOpen = !DeviceClose(dwDevice); FreeLibrary(hPmacLib); //FREEING HANDLE } if (!DriverOpen) return 1; else return ERR_CloseClipper; }