예제 #1
0
        private void StepperPositioningThreadStub()
        {
            if ((poKeysDevice == null) || (PE == null))
            {
                return;
            }

            while (StepperRunning)
            {
                // New Pulse engine state was commanded
                if (NewStateCommanded)
                {
                    PE.PulseEngineStateSetup = NewState;
                    poKeysDevice.PEv2_SetState(ref PE);
                    NewStateCommanded = false;
                }

                poKeysDevice.PEv2_GetStatus(ref PE);

                if (PE.PulseEngineState > 100) // Error or limit
                {
                    if (!ErrorFixed)
                    {
                        ErrorFixed = true;
                        Error      = Translations.Main.StepperPositioningError;
                    }

                    // Do nothing else...
                    Thread.Sleep(10);
                    continue;
                }

                ErrorFixed = true;

                // Position update was commanded
                if (SetPosition)
                {
                    for (int i = 0; i < 8; i++)
                    {
                        PE.PositionSetup[i] = SetAxisPos[i];
                    }

                    PE.param2 = 0xFF; // Set all 8 positions
                    poKeysDevice.PEv2_SetPositions(ref PE);
                    SetPosition = false;
                }

                // Homing was commanded
                if (StartHoming)
                {
                    IsHoming = true;
                    raiseHomingStatusChanged(IsHoming);
                    if (HomeStepper())
                    {
                        IsHomed = true;
                    }

                    IsHoming = false;
                    raiseHomingStatusChanged(IsHoming);

                    StartHoming = false;
                }


                if (PE.PulseEngineState != (byte)ePoKeysPEState.peRUNNING)
                {
                    NewState          = (byte)ePoKeysPEState.peRUNNING;
                    NewStateCommanded = true;
                }

                for (int i = 0; i < 8; i++)
                {
                    // Check axes states and change them to position mode...
                    if ((PE.AxesConfig[i] & (byte)ePEv2_AxisConfig.aoPOSITION_MODE) == 0)
                    {
                        PE.AxesConfig[i] |= (byte)(ePEv2_AxisConfig.aoPOSITION_MODE);
                        PE.param1         = (byte)i;
                        poKeysDevice.PEv2_SetAxisConfiguration(ref PE);
                    }
                }

                if (CheckNewState)
                {
                    switch (State)
                    {
                    case PoKeysStepperPositioningState.STOPPED:
                        // Stop the motion...
                        for (int i = 0; i < 8; i++)
                        {
                            PE.ReferencePositionSpeed[i] = PE.CurrentPosition[i];
                        }
                        break;

                    case PoKeysStepperPositioningState.MOVE_TO_POS:
                        for (int i = 0; i < 8; i++)
                        {
                            PE.ReferencePositionSpeed[i] = TargetAxisPos[i];
                        }
                        break;
                    }
                    poKeysDevice.PEv2_Move(ref PE);
                    CheckNewState = false;
                }
                else
                {
                    // Check existing state
                    switch (State)
                    {
                    case PoKeysStepperPositioningState.STOPPED:
                        break;

                    case PoKeysStepperPositioningState.MOVE_TO_POS:
                        // Check if move is complete
                        if (IsMoveComplete(5))
                        {
                            State = StateAfterMove;
                            if (State == PoKeysStepperPositioningState.STOPPED)
                            {
                                // Well... do nothing...
                            }
                            else
                            {
                                CheckNewState = true;
                            }
                        }
                        break;
                    }
                }

                if (CommandMotion)
                {
                    poKeysDevice.PEv2_Move(ref PE);
                    CommandMotion = false;
                }

                Thread.Sleep(10);
            }

            PE.PulseEngineStateSetup = (byte)ePoKeysPEState.peSTOPPED;
            poKeysDevice.PEv2_SetState(ref PE);
            poKeysDevice.DisconnectDevice();
        }