SDC C-Project CF Review 프로그램
LYW
2021-06-07 b785acb6a38b295544c03f83caccf99368d1b598
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
#include "StdAfx.h"
#include "PathScheduler_Dual_Flying.h"
#include "MotionInfo_Axis.h"
#include <algorithm>
 
CPathScheduler_Dual_Flying::CPathScheduler_Dual_Flying(void)
{
}
 
 
CPathScheduler_Dual_Flying::~CPathScheduler_Dual_Flying(void)
{
}
 
inline bool ComparePosAscend(SPathData& a, SPathData& b)
{
    return (a.nPosX < b.nPosX);
}
 
inline bool ComparePosDescend(SPathData& a, SPathData& b)
{
    return (a.nPosX > b.nPosX);
}
 
int CPathScheduler_Dual_Flying::CalculatePath(int nModuleIdx, int nScanIdx, const SPathData& startPath, const VectorPathData& vecTotalPathData, const SRangeData& rtRange, CPathSchedulerResult& scheduleResult )
{
    scheduleResult.Reset();
 
    CRect rtTemp(rtRange.nLeft, rtRange.nTop, rtRange.nRight, rtRange.nBottom);
 
    // get path data
    VectorPathData vecPathData;
    for (constVectorPathDataIt it=vecTotalPathData.begin(); it!=vecTotalPathData.end(); it++)
    {
        if ( rtTemp.PtInRect( CPoint(it->nPosX,it->nPosY )) )
        {
            vecPathData.push_back(*it);
        }
    }
 
    // sort for scan direction
    if (rtRange.nScanDir==0)
    {
        std::sort(vecPathData.begin(), vecPathData.end(), ComparePosAscend);
    }
    else
    {
        std::sort(vecPathData.begin(), vecPathData.end(), ComparePosDescend);
    }
    
    const SMotionInfo* pAxisX = m_PathSchedulerParam.GetScanMotionInfo();
    const SMotionInfo* pAxisY = m_PathSchedulerParam.GetModuleMotionInfo();
    if (pAxisX==NULL || pAxisY==NULL)
    {
        return 0;
    }
    
    // ½ºÄÉÁ층
    CMotionInfo_Axis axisX(0);
    CMotionInfo_Axis axisY(1);
 
    axisX.SetVelocity(pAxisX->dMoveSpeed);
    axisX.SetAccelTime(pAxisX->dAccelTime);
    axisX.SetDecelTime(pAxisX->dDecelTime);
 
    axisY.SetVelocity(pAxisY->dMoveSpeed);
    axisY.SetAccelTime(pAxisY->dAccelTime);
    axisY.SetDecelTime(pAxisY->dDecelTime);
 
    // set result count
    scheduleResult.SetScheduleResultCount((int)vecPathData.size()+1);
 
    FILE *fp = NULL;
    CString strFilename = _T("");
    strFilename.Format(_T("c:\\temp\\%d_%d.csv"), nModuleIdx, nScanIdx);
    _tfopen_s(&fp, strFilename, _T("w"));
    if (fp)
    {
        _ftprintf_s(fp, _T("[Left], [Right], [Top], [Bottom], [ScanStart], [ScanEnd], [ScanDir]\n"));
        _ftprintf_s(fp, _T("%d, %d, %d, %d, %d, %d, %d\n"), 
            rtRange.nLeft, 
            rtRange.nRight, 
            rtRange.nTop, 
            rtRange.nBottom, 
            rtRange.nScanStartX, 
            rtRange.nScanEndX, 
            rtRange.nScanDir);
        _ftprintf_s(fp, _T("[Index], [PrevX], [PrevY], [NextX], [NextY]\n"));
    }
 
 
    // add start pos
    int nSetCount = 0;
 
    // calculate path
    SPathData prevPos = startPath;
    for (int nIdx=0; nIdx<(int)vecPathData.size(); nIdx++)
    {
        axisX.SetStartPos(prevPos.nPosX/1000.0);
        axisX.SetEndPos(vecPathData[nIdx].nPosX/1000.0);
 
        axisY.SetStartPos(prevPos.nPosY/1000.0);
        axisY.SetEndPos(vecPathData[nIdx].nPosY/1000.0);
 
        double x_time = axisX.GetCurTotalTime();
        double y_time = axisY.GetCurTotalTime();
        if (x_time > (y_time+pAxisY->dInpositionTime)) // check motion time
        {
            if (fp)
                _ftprintf_s(fp, _T("%d, %d, %d, %d, %d\n"), nSetCount, prevPos.nPosX, prevPos.nPosY, vecPathData[nIdx].nPosX, vecPathData[nIdx].nPosY);
 
            prevPos = vecPathData[nIdx];
            SSchedulerResult sResult;
            sResult.nTotalIndex            = prevPos.nIndex;
            sResult.dPositionX            = double(prevPos.nPosX) / 1000.0;
            sResult.dPositionY            = double(prevPos.nPosY) / 1000.0;
 
            memcpy(sResult.pDataType, prevPos.pDataType, sizeof(int)*PATH_DATA_TYPE_COUNT);
 
            scheduleResult.SetScheduleResult(nSetCount, sResult);
            nSetCount++;
        }
    }
 
    if (fp)
        fclose(fp);
 
    // resize result count;
    scheduleResult.ResizeScheduleResult(nSetCount);
 
    return scheduleResult.GetPathSchedulerResultCount();
}
 
int CPathScheduler_Dual_Flying::GetDirectionX(int nDir1, int nDir2)
{
    //SchedulerDir_LeftTop=0, SchedulerDir_RightTop, SchedulerDir_LeftBottom, SchedulerDir_RightBottom
    
    // Dir1 == Left
    if (nDir1==SchedulerDir_LeftTop || nDir1==SchedulerDir_LeftBottom)
    {
        if (nDir2==SchedulerDir_LeftTop || nDir2==SchedulerDir_LeftBottom)
        {
            return 1;
        }
        else 
        {
            return -1;
        }
    }
 
    // Dir1 == Right
    if (nDir2==SchedulerDir_RightTop || nDir2==SchedulerDir_RightBottom)
    {
        return 1;
    }
 
    return -1;
}
 
int CPathScheduler_Dual_Flying::GetDirectionY(int nDir1, int nDir2)
{
    //SchedulerDir_LeftTop=0, SchedulerDir_RightTop, SchedulerDir_LeftBottom, SchedulerDir_RightBottom
 
    // Dir1 == Top
    if (nDir1==SchedulerDir_LeftTop || nDir1==SchedulerDir_RightTop)
    {
        if (nDir2==SchedulerDir_LeftTop || nDir2==SchedulerDir_RightTop)
        {
            return 1;
        }
        else 
        {
            return -1;
        }
    }
 
    // Dir1 == Bottom
    if (nDir2==SchedulerDir_LeftBottom || nDir2==SchedulerDir_RightBottom)
    {
        return 1;
    }
 
    return -1;
}
 
 
int CPathScheduler_Dual_Flying::IsChangeModuleIndex(int nDir1, int nDir2)
{
    return (nDir1!=nDir2);
}
 
int CPathScheduler_Dual_Flying::IsChangeScanIndex(int nDir1, int nDir2)
{
    if (GetDirectionY(nDir1, nDir2)==-1) return 1;
    return 0;
}
 
int CPathScheduler_Dual_Flying::IsChangeScanDir(int nDir1, int nDir2)
{
    if (GetDirectionX(nDir1, nDir2)==-1) return 1;
    return 0;
}
 
int CPathScheduler_Dual_Flying::GetChangeModuleIndex(int nModuleIndex, int nOriginDir, int nBaseDir, int nGantryModuleCount, int nTotalModuleCount)
{
    int nTmpValue = nModuleIndex;
 
    if (GetDirectionX(nOriginDir, nBaseDir)==-1) // x ¿ª¹æÇâ
    {
        if (nTmpValue<nGantryModuleCount)
        {
            nTmpValue = nTmpValue + nGantryModuleCount;
        }
        else
        {
            nTmpValue = nTmpValue - nGantryModuleCount;
        }        
    }
 
    if (GetDirectionY(nOriginDir, nBaseDir)==-1) // y ¿ª¹æÇâ
    {
        if (nTmpValue<nGantryModuleCount)
        {
            nTmpValue = (nGantryModuleCount - 1) - nTmpValue;
        }
        else
        {
            nTmpValue = nTotalModuleCount + (nGantryModuleCount - 1) - nTmpValue;
        }    
    }
    
    return nTmpValue;
}
 
int CPathScheduler_Dual_Flying::GetChangeScanIndex(int nScanIndex, int nTotalScanCount)
{
    return (nTotalScanCount - 1) - nScanIndex;
}
 
int CPathScheduler_Dual_Flying::GetChangeScanDir(int nScanDir)
{
    return !nScanDir;
}
 
int CPathScheduler_Dual_Flying::CalculateRangeRect(int nGantryCount, int nModuleCount, int nScanCount, int nGlassSizeX, int nGlassSizeY, int nScanMargin, int nOriginDir, int nBaseDir, VectorRangeData& vecRangeData)
{
    int nGlassDivX            = nGlassSizeX / nGantryCount;
    int nGantryModuleCount    = nModuleCount / nGantryCount;
    int nGlassDivModuleY    = nGlassSizeY / nGantryModuleCount;
    int nGlassDivScanY        = nGlassDivModuleY / nScanCount;
 
    SRangeData tmpRange;
 
    // x axis
    int nTotalModuleIdx = 0;
    for (int nGantryIdx=0; nGantryIdx<nGantryCount; nGantryIdx++)
    {
        // scan x range
        tmpRange.nLeft    = nGantryIdx * nGlassDivX;
        tmpRange.nRight    = tmpRange.nLeft + nGlassDivX;
 
        for (int nModuleIdx=0; nModuleIdx<nGantryModuleCount; nModuleIdx++)
        {
            // module y range
            int nModuleStart     = nModuleIdx * nGlassDivModuleY;
 
            for (int nScanIdx = 0; nScanIdx<nScanCount; nScanIdx++)
            {
                // scan y  range
                tmpRange.nTop        = nModuleStart + (nScanIdx*nGlassDivScanY);
                tmpRange.nBottom    = tmpRange.nTop + nGlassDivScanY;
 
                // ½ºÄµ y ½ÃÀÛÀ§Ä¡ ±âº»°ªÀº ¿µ¿ªÀÇ Áß¾ÓÀÓ.
            //    tmpRange.nScanStartY    = tmpRange.nTop + ((tmpRange.nBottom - tmpRange.nTop) / 2);
                tmpRange.nModuleIdx        = nTotalModuleIdx;
                tmpRange.nScanIdx        = nScanIdx;
                tmpRange.nScanDir        = (nScanIdx % 2);
 
                if (tmpRange.nScanDir==0) // Á¤¹æÇâ ½ºÄµ
                {
                    tmpRange.nScanStartX    = tmpRange.nLeft - nScanMargin;    //  ½ºÄµ ½ÃÀÛÀ§Ä¡                
                    tmpRange.nScanEndX        = tmpRange.nRight + nScanMargin; // ½ºÄµ Á¾·áÀ§Ä¡
                }
                else // ¿ª¹æÇâ ½ºÄµ
                {
                    tmpRange.nScanStartX    = tmpRange.nRight + nScanMargin; // ½ºÄµ ½ÃÀÛÀ§Ä¡
                    tmpRange.nScanEndX        = tmpRange.nLeft - nScanMargin;    //  ½ºÄµ Á¾·áÀ§Ä¡                
                }
 
                vecRangeData.push_back(tmpRange);
            }
 
            nTotalModuleIdx++;
        }
    }
 
    // change range data (origin_dir / base_dir)
 
//     int nModuleIdx;        // module index
//     int nScanIdx;        // scan index
//     int nScanDir;        // scan dir 0/1
// 
//     int nScanStartX;    // scan start x
//     int nScanStartY;    // scan start y
//     int nScanEndX;        // scan end x
// 
//     int nLeft;            // range left
//     int nTop;            // range top
//     int nRight;            // range right
//     int nBottom;        // range bottom
 
    VectorRangeData vecResult;
 
    for (int nModuleIdx=0; nModuleIdx<nModuleCount; nModuleIdx++)
    {
        for (int nScanIdx=0; nScanIdx<nScanCount; nScanIdx++)
        {
            SRangeData *pData = GetScanRangeData(nModuleIdx, nScanIdx);
            if (pData==NULL) continue;
 
            SRangeData newData = *pData;
 
            if (IsChangeModuleIndex(nOriginDir, nBaseDir))
            {
                int nTmpValue= GetChangeModuleIndex(nModuleIdx, nOriginDir, nBaseDir, nGantryModuleCount, nModuleCount);
                newData.nModuleIdx = nTmpValue;
            }
 
            int bScanIdx = IsChangeScanIndex(nOriginDir, nBaseDir);
            int bScanDir = IsChangeScanDir(nOriginDir, nBaseDir);
 
            if (bScanDir==1 && bScanIdx==1) // 11
            {
                // change scan index
                int nTmpValue = GetChangeScanIndex(nScanIdx, nScanCount);
                newData.nScanIdx = nTmpValue;
            }
            else if (bScanDir==1 && bScanIdx==0) // 10
            {
                // change scan dir
                int nTmpValue = GetChangeScanDir(newData.nScanDir);
                newData.nScanDir = nTmpValue;
 
                // change scan pos
                swap_value(newData.nScanStartX,    newData.nScanEndX);
            }
            else if (bScanDir==0 && bScanIdx==1) // 01
            {
                // change scan index
                int nTmpValue = GetChangeScanIndex(nScanIdx, nScanCount);
                newData.nScanIdx = nTmpValue;
 
                // change scan dir
                nTmpValue = GetChangeScanDir(newData.nScanDir);
                newData.nScanDir = nTmpValue;
 
                // change scan pos
                swap_value(newData.nScanStartX,    newData.nScanEndX);
            }
            // 00
 
            vecResult.push_back(newData);
        }
    }
 
    // set member data
    vecRangeData = vecResult;
 
    return (int)vecRangeData.size();
}
 
 
int CPathScheduler_Dual_Flying::PathScheduling( const VectorPathData& vecPathData, const VectorPathData& vecStartPath )
{
    m_vecRangeRect.clear();
    m_vecScanRangeData.clear();
    m_vecPathSchedulerResult.clear();
 
    int nGantryCount = 2;
 
    int nRangeCount = CalculateRangeRect(
        nGantryCount, 
        m_PathSchedulerParam.GetModuleCount(), 
        m_PathSchedulerParam.GetScanCount(), 
        m_PathSchedulerParam.GetGlassSizeX(),
        m_PathSchedulerParam.GetGlassSizeY(),
        m_PathSchedulerParam.GetScanMargin(),
        m_PathSchedulerParam.GetOriginDir(),
        m_PathSchedulerParam.GetBaseModuleDir(),
        m_vecScanRangeData);
 
    if (nRangeCount<1) return 0;
 
    int nTotalCount = 0;
    SPathData sStartPath; // ¸ðµâº° ½ºÄµ ½ÃÀÛÀ§Ä¡ ÀúÀå.
    for (int nModuleIdx=0; nModuleIdx<m_PathSchedulerParam.GetModuleCount(); nModuleIdx++)
    {
        if (int(vecStartPath.size()) <= nModuleIdx) continue;
 
        for (int nScanIdx = 0; nScanIdx<m_PathSchedulerParam.GetScanCount(); nScanIdx++)
        {
            SRangeData* pRangeData = GetScanRangeData(nModuleIdx, nScanIdx);
 
            if (pRangeData==NULL) continue;
 
            // add range rect
            m_vecRangeRect.push_back(CRect(pRangeData->nLeft, pRangeData->nTop, pRangeData->nRight, pRangeData->nBottom));
 
            if (nScanIdx==0) // first scan ÀÇ ½ÃÀÛÀ§Ä¡´Â ÇöÀç ¸ðµâÀÇ À§Ä¡ÀÌ´Ù.
            {
                sStartPath = vecStartPath[nModuleIdx];
            }
 
            // ¿µ¿ªº° ½ºÄÉÁ층 ¼öÇà.
            CPathSchedulerResult schedulerResult(nModuleIdx, nScanIdx);
            int nCount = CalculatePath(nModuleIdx, nScanIdx, sStartPath, vecPathData, *pRangeData, schedulerResult);
            nTotalCount += nCount;
            if (nCount>1)
            {
                // ´ÙÀ½ ½ºÄµÀÇ ½ÃÀÛÀ§Ä¡´Â ÀÌÀü ½ºÄµÀÇ Á¾·á À§Ä¡ÀÌ´Ù.
                const SSchedulerResult* pNode = schedulerResult.GetPathSchedulerResult(schedulerResult.GetPathSchedulerResultCount() - 1);
                if (pNode)
                {
                    sStartPath.nPosX = (pRangeData->nScanEndX);
                    sStartPath.nPosY = int(pNode->dPositionY * 1000);
                }
                m_vecPathSchedulerResult.push_back(schedulerResult);
            }
            else // ´ë»ó °áÇÔÀÌ ¾øÀ»¶§ xÃà À§Ä¡¸¸ º¯°æÇÑ´Ù.
            {
                sStartPath.nPosX = (pRangeData->nScanEndX); // 
            }
        }
    }
    
    return int(m_vecPathSchedulerResult.size());
}
 
SRangeData* CPathScheduler_Dual_Flying::GetScanRangeData( int nModuleIdx, int nScanIdx )
{
    for (VectorRangeDataIt it=m_vecScanRangeData.begin(); it!=m_vecScanRangeData.end(); it++)
    {
        if (it->nModuleIdx==nModuleIdx && it->nScanIdx==nScanIdx)
        {
            return &(*it);
        }
    }
    return NULL;
}
 
const SRangeData* CPathScheduler_Dual_Flying::GetScanRangeData( int nModuleIdx, int nScanIdx ) const
{
    for (constVectorRangeDataIt it=m_vecScanRangeData.begin(); it!=m_vecScanRangeData.end(); it++)
    {
        if (it->nModuleIdx==nModuleIdx && it->nScanIdx==nScanIdx)
        {
            return &(*it);
        }
    }
    return NULL;
}