SDC C-Project CF Review 프로그램
LYW
2021-10-14 e8ba1e78e72f3f7ec2841f55fd90253b4dc414df
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
#include "StdAfx.h"
#include "CHAfmControls/AFMControl_Msg.h"
#include "AFT_LIB/atf_lib.h"
#include <tchar.h>
 
#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<CLK_TCK*10);
 
    do
    {
        ret = atf_MoveZum(homing_back_stepsize);
        if(ret) return FALSE;
        do
        {
            ret = atf_ReadHwStat(&hw_status);
            if(ret) return FALSE;
        } while (hw_status & moving);
 
    }while(hw_status&limit_sw);
 
 
    if(ret) return FALSE;
 
    return TRUE;
}
 
int CAfmControl_Msg::MoveToHomePosition(int nHomePos) // 문제 부분 
{
    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 2;
    }
 
    DWORD dwTime = GetTickCount();
    int stepspermm=5000;
 
    int nLensNum = -1;
    ret = atf_ReadObjNum(&nLensNum);
    if(ret) return 3;
 
    const int lensnumber=nLensNum;
 
    float currentspeed;
    // Read current Speed 
    ret = atf_ReadSpeed(lensnumber, &currentspeed);
    if (ret) return 4;
 
    // write low speed
    ret = atf_WriteSpeed(lensnumber, currentspeed/(float)3.0);
    u_short currentstepspermm;
 
    ret = atf_ReadStepPerMmConversion( &currentstepspermm );
    if (ret) return 5;
 
    stepspermm = (int)currentstepspermm;
 
    const int homing_distance_steps = 10000*stepspermm/1000;
    const int homing_back_stepsize = -2*stepspermm/1000;
 
    // ★ homing target distance steps is Reference Position ( Setting parameter need! ) 
    const int homing_target_distance_steps = nHomePos * stepspermm/1000.;
 
    const int homing_timeout_s=10;
 
    const int limit_sw=(1<<18);
    const int moving=(1<<12);
 
    //     const float homingspeed_mm_per_s=(float)50000.0/stepspermm;
    //     ret = atf_WriteSpeed(lensnumber, homingspeed_mm_per_s );
    //     if (ret) return FALSE;
 
    // 1. Move to Limit 
    ret = atf_MoveZ(homing_distance_steps);
    if (ret) return 6;
 
    int hw_status;
 
    clock_t t1=clock();
 
    //2. Read Limit Sensor?
    do 
    {
        ret = atf_ReadHwStat(&hw_status);
        if (ret) return 7;
    }while(~hw_status & limit_sw && clock()-t1 < CLK_TCK*homing_timeout_s);
 
    if(~hw_status & limit_sw) return 8;
 
    do
    {
        // 3. move out ( not Limit / small down)
        ret = atf_MoveZ(homing_back_stepsize);
        if(ret) return 9;
        do
        {
            // check Limit ( not Limit )
            ret = atf_ReadHwStat(&hw_status);
            if(ret) return 10;
        } while (hw_status & moving);
 
    }while(hw_status&limit_sw);
 
    // Current Position is Zero.
    ret = atf_WriteAbsZPos(0);
 
    if(ret) return 11;
 
    ret = atf_WriteSpeed(lensnumber, currentspeed);
    Sleep(50);
 
    // Target Position
    ret = atf_MoveZ(homing_target_distance_steps);
 
    dwTime = GetTickCount() - dwTime;
 
    // to read the position counter
    //atf_ReadAbsZPos
 
    if(ret) return 12;
 
    return TRUE;
}
 
 
 
BOOL CAfmControl_Msg::MoveToBasePosition(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_MoveZ(((double)-100 * m_nMicroSteps * m_nFullsteps)/1000);
 
    //if(ret) return FALSE;
 
    //Sleep(100);
 
//    ret = atf_RunHomingZ(m_pAFMHomingZParam);
    
    int nPos = int( ((double)m_pAFMHomingZParam[3] * m_nMicroSteps * m_nFullsteps)/1000.0 );
    ret = atf_MoveZ(nPos);
 
    if(ret) return FALSE;
 
    return TRUE;
}
 
BOOL CAfmControl_Msg::MoveToTargetPosition( double dPos )
{
    u_short currentstepspermm;
 
    BOOL bRet = atf_ReadStepPerMmConversion( &currentstepspermm );
    if (bRet) return FALSE;
 
    const int nTarget_distance_steps = int(currentstepspermm/1000 * dPos);
 
    bRet = atf_MoveZ(nTarget_distance_steps);
    if(bRet) return FALSE;
 
    return TRUE;
}
 
int CAfmControl_Msg::JumpAF()
{
    int ret = atf_AfDuvJump();
    
    return ret;
}
 
void CAfmControl_Msg::WorkThreadProcess(PVOID pParameter)
{
    bool m_bIsWorking = FALSE;
    while(!m_bIsWorking){
        WaitForSingleObject(m_hMutex, INFINITE);
        atf_CloseConnection();
        int ret = atf_OpenConnection(m_strPort, m_nBaudrate_start);
        if (ret)
        {
            ReleaseMutex(m_hMutex);
            return ;
        }
 
        atf_IsInHomePosition(&m_bIsWorking);
        ReleaseMutex(m_hMutex);
        Wait(100);
    }
 
    WaitForSingleObject(m_hMutex, INFINITE);
    atf_CloseConnection();
    int ret = atf_OpenConnection(m_strPort, m_nBaudrate_start);
    if (ret)
    {
        ReleaseMutex(m_hMutex);
        return ;
    }    
    atf_AFTrack();
    ReleaseMutex(m_hMutex);
}
 
void CAfmControl_Msg::Wait(DWORD dwMillisecond)
{
    MSG msg;
    DWORD dwStart;
    dwStart = GetTickCount();
 
    while(GetTickCount() - dwStart < dwMillisecond)
    {
 
        while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
}
 
BOOL CAfmControl_Msg::GetInFocus(BOOL* pRtCheck)
{
    // bit 13 (MsInFocus)
    // 0 = sensor beyond the In-Focus Range parameter (not in focus)
    // 1 = sensor inside In-Focus Range parameter (In-focus position)
 
    if(pRtCheck == NULL) return FALSE;
 
    short shStatus;
 
    if(atf_ReadStatus(&shStatus) == 0)
    {
         if((shStatus & (1 << 13)) != 0)
         {
             *pRtCheck = TRUE;
             return TRUE;
         }
         else
         {
             *pRtCheck = TRUE;
         }
             
    }
    return FALSE;
}
 
int CAfmControl_Msg::GetAFMHomePosition(bool &bHome) const
{
    int nRet = atf_IsInHomePosition(&bHome);
    return nRet;
 
//     DWORD dwStart;
//     dwStart = GetTickCount();
//     int nRet;
//     while(GetTickCount() - dwStart < 5000)
//     {
//         nRet = atf_IsInHomePosition(&bHome);
//         if(bHome == TRUE)
//         {
//             return nRet;
//         }
//         Sleep(100);
//     }
 
//     Output Boolean *pbInHome 
//     True = in home position 
//     False = not in home 
//     position Return 
//     value int 
//     0 (ErrOK) = operation successful 
//     8 (ErrNoresources) = insufficient command buffer size 
//     10 (ErrOperFailed) = sending command to the sensor failure, sensor data receiving error or sensor responded with NACK 
//     11 (ErrTimeout) = erroneous or no response
}
 
BOOL CAfmControl_Msg::GetCurPos( double& dPos )
{
    u_short currentstepspermm;
 
    BOOL bRet = atf_ReadStepPerMmConversion( &currentstepspermm );
    if (bRet) return FALSE;
 
    int nPos = 0;
    bRet = atf_ReadAbsZPos(&nPos);
 
    if(bRet)
         return FALSE;
 
    dPos = nPos/ (currentstepspermm/1000) ;
 
    return TRUE;
}
 
BOOL CAfmControl_Msg::SetSystemTime( SYSTEMTIME stCurrentTime )
{
    return TRUE;
}