Exemplo n.º 1
0
        /// <summary>
        /// Robot sequence task
        /// </summary>
        /// <param name="token"></param>
        private void ExecuteRobotSequence(CancellationToken token)
        {
            int R1Step = 0, R2Step = 0, R3Step = 0;
            int newStep = 0;

            Task.Factory.StartNew(() => {
                Restart();

                Tuple <bool, int[]> step;

                while (true)
                {
                    try
                    {
                        if (token.IsCancellationRequested)
                        {
                            break;
                        }

                        if (robotManager.IsReadyCommand())
                        {
                            step = robotManager.GetCurrentStep();
                            if (!step.Item1)
                            {
                                R1Step = step.Item2[0];
                                R2Step = step.Item2[1];
                                R3Step = step.Item2[2];

                                step = null;
                            }
                            else
                            {
                                logger.Error("Read fail current step");

                                Thread.Sleep(500);
                                continue;
                            }


                            logger.DebugFormat("Cycle count = {0}", CycleCount);
                            logger.DebugFormat("R1Step={0}, R2Step={1}, R3Step={2}  => lastStep={3}", R1Step, R2Step, R3Step, lastStep);

                            jigIndex = robotManager.GetCurrentJig(Robots.R1);

                            if (R1Step == 0 && R2Step == 0 && R3Step == 0 && (lastStep == 0 || lastStep == 7 || lastStep == 11))
                            {
                                if (lastStep == 11 && jigIndex == 1 && !doneCycle)
                                {
                                    CycleCount++;

                                    if (LoopCount != 0 && LoopCount <= CycleCount)
                                    {
                                        doneCycle = true;
                                        logger.DebugFormat("모든 사이클을 완료 하였습니다. Cycle Count={0}", CycleCount);

                                        Pause();
                                        Thread.Sleep(1000);

                                        continue;
                                    }
                                }
                                else if (lastStep == 11 && jigIndex == 1 && doneCycle)
                                {
                                    if (SetStep(1))
                                    {
                                        doneCycle  = false;
                                        CycleCount = 0;
                                        lastStep   = 1;
                                    }
                                }

                                logger.DebugFormat("현재 지그 번호 : {0}", jigIndex);

                                switch (lastStep)
                                {
                                case 0:
                                case 7:
                                    if (jigIndex == 5)
                                    {
                                        newStep = 8;
                                    }
                                    else
                                    {
                                        newStep = 1;

                                        if (lastStep == 0 && jigIndex == 0)
                                        {
                                            if (SetJig(1))
                                            {
                                                jigIndex++;
                                            }
                                        }
                                        else
                                        {
                                            if (SetJig(jigIndex + 1))
                                            {
                                                jigIndex++;
                                            }
                                        }

                                        Thread.Sleep(500);
                                    }

                                    if (SetStep(newStep))
                                    {
                                        lastStep = newStep;
                                    }
                                    Thread.Sleep(500);

                                    break;

                                case 11:
                                    if (jigIndex == 0)
                                    {
                                        jigIndex = 1;
                                    }

                                    if (jigIndex == 1)
                                    {
                                        newStep = 1;
                                    }
                                    else
                                    {
                                        newStep = 8;

                                        if (SetJig(jigIndex - 1))
                                        {
                                            jigIndex--;
                                        }
                                        Thread.Sleep(500);
                                    }

                                    if (SetStep(newStep))
                                    {
                                        lastStep = newStep;
                                    }
                                    Thread.Sleep(500);

                                    break;
                                }
                            }
                            else if (R1Step == R2Step && R1Step == R3Step && R1Step == lastStep)
                            {
                                // 최종 스탭을 기준으로 다음 스탭을 설정한다.
                                newStep = lastStep + 1;

                                if (SetStep(newStep))
                                {
                                    lastStep = newStep;
                                }

                                Thread.Sleep(500);
                            }
                            else if (R1Step == R2Step && R1Step == R3Step && lastStep == 0 && R1Step != 0)
                            {
                                newStep = R1Step + 1;

                                if (SetStep(newStep))
                                {
                                    lastStep = newStep;
                                }

                                Thread.Sleep(500);
                            }
                            else if (R1Step == R2Step && R1Step == R3Step && lastStep != 0)
                            {
                                newStep = R1Step + 1;

                                if (SetStep(newStep))
                                {
                                    lastStep = newStep;
                                }

                                Thread.Sleep(500);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex.Message);
                    }

                    Thread.Sleep(1000);
                }
            });
        }