private bool FnKeyHandler(Key key)
 {
     if (!model.IsJobRunning)
     {
         int id    = int.Parse(key.ToString().Substring(1));
         var macro = AppConfig.Settings.Macros.FirstOrDefault(o => o.Id == id);
         if (macro != null && MessageBox.Show(string.Format("Run {0} macro?", macro.Name), "Run macro", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
         {
             model.ExecuteCommand(macro.Code);
             return(true);
         }
     }
     return(false);
 }
Exemple #2
0
        public bool Init()
        {
            bool?res = null;

            probing.Message = String.Empty;

            Grbl.Poller.SetState(0);  // Disable status polling during probing

            // Clear error status if set
            if (Grbl.GrblError != 0)
            {
                new Thread(() =>
                {
                    res = WaitFor.AckResponse <string>(
                        cancellationToken,
                        null,
                        a => Grbl.OnResponseReceived += a,
                        a => Grbl.OnResponseReceived -= a,
                        1000, () => Grbl.ExecuteCommand(""));
                }).Start();

                while (res == null)
                {
                    EventUtils.DoEvents();
                }

                res = null;
            }

            // Get a status report in order to establish current machine position
            new Thread(() =>
            {
                res = WaitFor.SingleEvent <string>(
                    cancellationToken,
                    null,
                    a => Grbl.OnResponseReceived += a,
                    a => Grbl.OnResponseReceived -= a,
                    1000, () => Comms.com.WriteByte(GrblLegacy.ConvertRTCommand(GrblConstants.CMD_STATUS_REPORT)));
            }).Start();

            while (res == null)
            {
                EventUtils.DoEvents();
            }

            Grbl.Poller.SetState(AppConfig.Settings.Base.PollInterval);

            if (Grbl.GrblState.State == GrblStates.Alarm)
            {
                probing.Message = GrblAlarms.GetMessage(Grbl.GrblState.Substate.ToString());
                res             = false;
            }

            if (res == true && Grbl.Signals.Value.HasFlag(Signals.ProbeDisconnected))
            {
                probing.Message = "Probing failed, probe is not connected";
                res             = false;
            }

            if (res == true && Grbl.Signals.Value.HasFlag(Signals.Probe))
            {
                probing.Message = "Probing failed, probe signal is asserted";
                res             = false;
            }

            if (res == true && !(Grbl.GrblState.State == GrblStates.Idle || Grbl.GrblState.State == GrblStates.Tool))
            {
                probing.Message = "Probing failed, Grbl is not in idle or tool changing state";
                res             = false;
            }

            if (res == true && !Grbl.IsMachinePositionKnown)
            {
                probing.Message = "Probing failed, could not establish current machine position";
                res             = false;
            }

            _program.Clear();

            if (res != true) // Reenable status polling if init fails
            {
                Grbl.Poller.SetState(AppConfig.Settings.Base.PollInterval);
            }

            return(res == true);
        }
Exemple #3
0
        public bool Init(bool check_probe = true)
        {
            bool?res = null;

            IsCancelled     = false;
            probing.Message = string.Empty;

            Grbl.Poller.SetState(0);  // Disable status polling during initialization

            // Clear error status if set
            if (Grbl.GrblError != 0)
            {
                new Thread(() =>
                {
                    res = WaitFor.AckResponse <string>(
                        cancellationToken,
                        null,
                        a => Grbl.OnResponseReceived += a,
                        a => Grbl.OnResponseReceived -= a,
                        1000, () => Grbl.ExecuteCommand(""));
                }).Start();

                while (res == null)
                {
                    EventUtils.DoEvents();
                }

                res = null;
            }

            // Get a status report in order to establish current machine position
            new Thread(() =>
            {
                res = WaitFor.SingleEvent <string>(
                    cancellationToken,
                    null,
                    a => Grbl.OnResponseReceived += a,
                    a => Grbl.OnResponseReceived -= a,
                    AppConfig.Settings.Base.PollInterval * 5, () => Comms.com.WriteByte(GrblInfo.IsGrblHAL ? GrblConstants.CMD_STATUS_REPORT_ALL : GrblLegacy.ConvertRTCommand(GrblConstants.CMD_STATUS_REPORT)));
            }).Start();

            while (res == null)
            {
                EventUtils.DoEvents();
            }

            Grbl.Poller.SetState(AppConfig.Settings.Base.PollInterval);

            if (Grbl.GrblState.State == GrblStates.Alarm)
            {
                probing.Message = GrblAlarms.GetMessage(Grbl.GrblState.Substate.ToString());
                res             = false;
            }

            if (res == true && check_probe)
            {
                res = IsProbeReady(false);
            }

            if (res == true && !(Grbl.GrblState.State == GrblStates.Idle || Grbl.GrblState.State == GrblStates.Tool))
            {
                probing.Message = LibStrings.FindResource("FailedNotIdle");
                res             = false;
            }

            if (res == true && !Grbl.IsMachinePositionKnown)
            {
                probing.Message = LibStrings.FindResource("FailedNoPos");
                res             = false;
            }

            probing.StartPosition.Set(probing.Grbl.MachinePosition);

            hasPause = probeOnCycleStart = false;
            _program.Clear();

            return(res == true);
        }
Exemple #4
0
        private void JogCommand(string cmd)
        {
            GrblViewModel model = DataContext as GrblViewModel;

            if (cmd == "stop")
            {
                cmd = ((char)GrblConstants.CMD_JOG_CANCEL).ToString();
            }

            else
            {
                var distance = cmd[1] == '-' ? -JogData.Distance : JogData.Distance;

                if (softLimits)
                {
                    int axis = GrblInfo.AxisLetterToIndex(cmd[0]);

                    if (jogAxis != -1 && axis != jogAxis)
                    {
                        return;
                    }

                    if (axis != jogAxis)
                    {
                        position = distance + model.MachinePosition.Values[axis];
                    }
                    else
                    {
                        position += distance;
                    }

                    if (GrblInfo.ForceSetOrigin)
                    {
                        if (!GrblInfo.HomingDirection.HasFlag(GrblInfo.AxisIndexToFlag(axis)))
                        {
                            if (position > 0d)
                            {
                                position = 0d;
                            }
                            else if (position < (-GrblInfo.MaxTravel.Values[axis] + limitSwitchesClearance))
                            {
                                position = (-GrblInfo.MaxTravel.Values[axis] + limitSwitchesClearance);
                            }
                        }
                        else
                        {
                            if (position < 0d)
                            {
                                position = 0d;
                            }
                            else if (position > (GrblInfo.MaxTravel.Values[axis] - limitSwitchesClearance))
                            {
                                position = GrblInfo.MaxTravel.Values[axis] - limitSwitchesClearance;
                            }
                        }
                    }
                    else
                    {
                        if (position > -limitSwitchesClearance)
                        {
                            position = -limitSwitchesClearance;
                        }
                        else if (position < -(GrblInfo.MaxTravel.Values[axis] - limitSwitchesClearance))
                        {
                            position = -(GrblInfo.MaxTravel.Values[axis] - limitSwitchesClearance);
                        }
                    }

                    if (position == 0d)
                    {
                        return;
                    }

                    jogAxis = axis;

                    cmd = string.Format("$J=G53{0}{1}{2}F{3}", mode, cmd.Substring(0, 1), position.ToInvariantString(), Math.Ceiling(JogData.FeedRate).ToInvariantString());
                }
                else
                {
                    cmd = string.Format("$J=G91{0}{1}{2}F{3}", mode, cmd.Substring(0, 1), distance.ToInvariantString(), Math.Ceiling(JogData.FeedRate).ToInvariantString());
                }
            }

            model.ExecuteCommand(cmd);
        }
 private bool Home(Key key)
 {
     model.ExecuteCommand(GrblConstants.CMD_HOMING);
     return(true);
 }
Exemple #6
0
        public RestartResult Restart()
        {
            Message       = model.Message;
            model.Message = string.Format(LibStrings.FindResource("MsgWaiting"), AppConfig.Settings.Base.PortParams);

            string response = GrblInfo.Startup(model);

            if (response.StartsWith("<"))
            {
                if (model.GrblState.State != GrblStates.Unknown)
                {
                    switch (model.GrblState.State)
                    {
                    case GrblStates.Alarm:

                        model.Poller.SetState(AppConfig.Settings.Base.PollInterval);

                        switch (model.GrblState.Substate)
                        {
                        case 1:         // Hard limits
                            if (!GrblInfo.IsLoaded)
                            {
                                if (model.LimitTriggered)
                                {
                                    MessageBox.Show(string.Format(LibStrings.FindResource("MsgNoCommAlarm"), model.GrblState.Substate.ToString()), "ioSender");
                                    if (AttemptReset())
                                    {
                                        model.ExecuteCommand(GrblConstants.CMD_UNLOCK);
                                    }
                                    else
                                    {
                                        MessageBox.Show(LibStrings.FindResource("MsgResetFailed"), "ioSender");
                                        return(RestartResult.Close);
                                    }
                                }
                                else if (AttemptReset())
                                {
                                    model.ExecuteCommand(GrblConstants.CMD_UNLOCK);
                                }
                            }
                            else
                            {
                                response = string.Empty;
                            }
                            break;

                        case 2:         // Soft limits
                            if (!GrblInfo.IsLoaded)
                            {
                                MessageBox.Show(string.Format(LibStrings.FindResource("MsgNoCommAlarm"), model.GrblState.Substate.ToString()), "ioSender");
                                if (AttemptReset())
                                {
                                    model.ExecuteCommand(GrblConstants.CMD_UNLOCK);
                                }
                                else
                                {
                                    MessageBox.Show(LibStrings.FindResource("MsgResetFailed"), "ioSender");
                                    return(RestartResult.Close);
                                }
                            }
                            else
                            {
                                response = string.Empty;
                            }
                            break;

                        case 10:         // EStop
                            if (GrblInfo.IsGrblHAL && model.Signals.Value.HasFlag(Signals.EStop))
                            {
                                MessageBox.Show(LibStrings.FindResource("MsgEStop"), "ioSender", MessageBoxButton.OK, MessageBoxImage.Warning);
                                while (!AttemptReset() && model.GrblState.State == GrblStates.Alarm)
                                {
                                    if (MessageBox.Show(LibStrings.FindResource("MsgEStopExit"), "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                                    {
                                        return(RestartResult.Close);
                                    }
                                }
                                ;
                            }
                            else
                            {
                                AttemptReset();
                            }
                            if (!GrblInfo.IsLoaded)
                            {
                                model.ExecuteCommand(GrblConstants.CMD_UNLOCK);
                            }
                            break;

                        case 11:         // Homing required
                            if (GrblInfo.IsLoaded)
                            {
                                response = string.Empty;
                            }
                            else
                            {
                                Message = LibStrings.FindResource("MsgHome");
                            }
                            break;
                        }
                        break;

                    case GrblStates.Tool:
                        Comms.com.WriteByte(GrblConstants.CMD_STOP);
                        break;

                    case GrblStates.Door:
                        if (!GrblInfo.IsLoaded)
                        {
                            if (MessageBox.Show(LibStrings.FindResource("MsgDoorOpen"), "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                            {
                                return(RestartResult.Close);
                            }
                            else
                            {
                                bool exit = false;
                                do
                                {
                                    Comms.com.PurgeQueue();

                                    bool?res = null;
                                    CancellationToken cancellationToken = new CancellationToken();

                                    new Thread(() =>
                                    {
                                        res = WaitFor.SingleEvent <string>(
                                            cancellationToken,
                                            s => TrapReset(s),
                                            a => model.OnGrblReset += a,
                                            a => model.OnGrblReset -= a,
                                            200, () => Comms.com.WriteByte(GrblConstants.CMD_STATUS_REPORT));
                                    }).Start();

                                    while (res == null)
                                    {
                                        EventUtils.DoEvents();
                                    }

                                    if (!(exit = !model.Signals.Value.HasFlag(Signals.SafetyDoor)))
                                    {
                                        if (MessageBox.Show(LibStrings.FindResource("MsgDoorExit"), "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                                        {
                                            exit = true;
                                            return(RestartResult.Close);
                                        }
                                    }
                                } while (!exit);
                            }
                        }
                        else
                        {
                            MessageBox.Show(LibStrings.FindResource("MsgDoorPersist"), "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
                            response = string.Empty;
                        }
                        break;

                    case GrblStates.Hold:
                    case GrblStates.Sleep:
                        if (MessageBox.Show(string.Format(LibStrings.FindResource("MsgNoComm"), model.GrblState.State.ToString()),
                                            "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                        {
                            return(RestartResult.Close);
                        }
                        else if (!AttemptReset())
                        {
                            MessageBox.Show(LibStrings.FindResource("MsgResetExit"), "ioSender");
                            return(RestartResult.Close);
                        }
                        break;

                    case GrblStates.Idle:
                        if (response.Contains("|SD:Pending"))
                        {
                            AttemptReset();
                        }
                        break;
                    }
                }
            }
            else
            {
                MessageBox.Show(response == string.Empty
                                    ? "No respone received from controller, exiting."
                                    : string.Format("Unexpected response received from controller: \"{0}\", exiting.", response),
                                "ioSender", MessageBoxButton.OK, MessageBoxImage.Stop);
                return(RestartResult.Exit);
            }

            return(response == string.Empty ? RestartResult.NoResponse : RestartResult.Ok);
        }
        public void Activate(bool activate, ViewType chgMode)
        {
            if (activate)
            {
                GCodeSender.RewindFile();
                GCodeSender.CallHandler(GCode.File.IsLoaded ? StreamingState.Idle : (sdStream ? StreamingState.Start : StreamingState.NoFile), false);
                sdStream = false;

                model.ResponseLogFilterOk = AppConfig.Settings.Base.FilterOkResponse;

                if (initOK != true)
                {
                    focusedControl = this;

                    var message = model.Message;
                    model.Message = string.Format("Waiting for controller ({0})...", AppConfig.Settings.Base.PortParams);

                    string response = GrblInfo.Startup(model);

                    if (response.StartsWith("<"))
                    {
                        if (model.GrblState.State != GrblStates.Unknown)
                        {
                            switch (model.GrblState.State)
                            {
                            case GrblStates.Alarm:

                                model.Poller.SetState(AppConfig.Settings.Base.PollInterval);

                                switch (model.GrblState.Substate)
                                {
                                case 1:         // Hard limits
                                    if (!GrblInfo.IsLoaded)
                                    {
                                        if (model.LimitTriggered)
                                        {
                                            MessageBox.Show(string.Format("Controller is not able to communicate due to alarm {0}, attempting a soft reset.", model.GrblState.Substate.ToString()), "ioSender");
                                            if (AttemptReset())
                                            {
                                                model.ExecuteCommand(GrblConstants.CMD_UNLOCK);
                                            }
                                            else
                                            {
                                                MessageBox.Show("Controller soft reset failed, exiting.", "ioSender");
                                                MainWindow.ui.Close();
                                            }
                                        }
                                        else if (AttemptReset())
                                        {
                                            model.ExecuteCommand(GrblConstants.CMD_UNLOCK);
                                        }
                                    }
                                    else
                                    {
                                        response = string.Empty;
                                    }
                                    break;

                                case 2:         // Soft limits
                                    if (!GrblInfo.IsLoaded)
                                    {
                                        MessageBox.Show(string.Format("Controller is not able to communicate due to alarm {0}, attempting a soft reset.", model.GrblState.Substate.ToString()), "ioSender");
                                        if (AttemptReset())
                                        {
                                            model.ExecuteCommand(GrblConstants.CMD_UNLOCK);
                                        }
                                        else
                                        {
                                            MessageBox.Show("Controller soft reset failed, exiting.", "ioSender");
                                            MainWindow.ui.Close();
                                        }
                                    }
                                    else
                                    {
                                        response = string.Empty;
                                    }
                                    break;

                                case 10:         // EStop
                                    if (GrblInfo.IsGrblHAL && model.Signals.Value.HasFlag(Signals.EStop))
                                    {
                                        MessageBox.Show("E-Stop active! - clear before continuing.", "ioSender", MessageBoxButton.OK, MessageBoxImage.Warning);
                                        while (!AttemptReset() && model.GrblState.State == GrblStates.Alarm)
                                        {
                                            if (MessageBox.Show("E-Stop still active, exit?", "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                                            {
                                                MainWindow.ui.Close();
                                            }
                                        }
                                        ;
                                    }
                                    else
                                    {
                                        AttemptReset();
                                    }
                                    if (!GrblInfo.IsLoaded)
                                    {
                                        model.ExecuteCommand(GrblConstants.CMD_UNLOCK);
                                    }
                                    break;

                                case 11:         // Homing required
                                    if (GrblInfo.IsLoaded)
                                    {
                                        response = string.Empty;
                                    }
                                    else
                                    {
                                        message = "Homing cycle required, <Home> to continue";
                                    }
                                    break;
                                }
                                break;

                            case GrblStates.Tool:
                                Comms.com.WriteByte(GrblConstants.CMD_STOP);
                                break;

                            case GrblStates.Door:
                                if (!GrblInfo.IsLoaded)
                                {
                                    if (MessageBox.Show("Door is open, close door and continue?", "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                                    {
                                        MainWindow.ui.Close();
                                    }
                                    else
                                    {
                                        bool exit = false;
                                        do
                                        {
                                            Comms.com.PurgeQueue();

                                            bool?res = null;
                                            CancellationToken cancellationToken = new CancellationToken();

                                            new Thread(() =>
                                            {
                                                res = WaitFor.SingleEvent <string>(
                                                    cancellationToken,
                                                    s => TrapReset(s),
                                                    a => model.OnGrblReset += a,
                                                    a => model.OnGrblReset -= a,
                                                    200, () => Comms.com.WriteByte(GrblConstants.CMD_STATUS_REPORT));
                                            }).Start();

                                            while (res == null)
                                            {
                                                EventUtils.DoEvents();
                                            }

                                            if (!(exit = !model.Signals.Value.HasFlag(Signals.SafetyDoor)))
                                            {
                                                if (MessageBox.Show("Door is still open, exit?", "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                                                {
                                                    exit = true;
                                                    MainWindow.ui.Close();
                                                }
                                            }
                                        } while (!exit);
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("Door state cannot be cleared with <Reset>", "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
                                    response = string.Empty;
                                }
                                break;

                            case GrblStates.Hold:
                            case GrblStates.Sleep:
                                if (MessageBox.Show(string.Format("Controller is in {0} state and cannot respond, try a soft reset?", model.GrblState.State.ToString()),
                                                    "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                                {
                                    MainWindow.ui.Close();
                                }
                                else if (!AttemptReset())
                                {
                                    MessageBox.Show("Controller soft reset failed, exiting.", "ioSender");
                                    MainWindow.ui.Close();
                                }
                                break;
                            }
                        }
                    }

                    if (response != string.Empty)
                    {
                        InitSystem();
                    }

                    model.Message = message;
                }

                if (initOK == null)
                {
                    initOK = false;
                }

                #if ADD_CAMERA
                if (MainWindow.UIViewModel.Camera != null)
                {
                    MainWindow.UIViewModel.Camera.MoveOffset += Camera_MoveOffset;
                    MainWindow.UIViewModel.Camera.Opened     += Camera_Opened;
                }
                #endif
                //if (viewer == null)
                //    viewer = new Viewer();

                if (GCode.File.IsLoaded)
                {
                    MainWindow.ui.WindowTitle = ((GrblViewModel)DataContext).FileName;
                }
            }
            else if (ViewType != ViewType.Shutdown)
            {
                DRO.IsFocusable = false;
                #if ADD_CAMERA
                if (MainWindow.UIViewModel.Camera != null)
                {
                    MainWindow.UIViewModel.Camera.MoveOffset -= Camera_MoveOffset;
                }
                #endif
                focusedControl = AppConfig.Settings.Base.KeepMdiFocus && Keyboard.FocusedElement is TextBox && (string)(Keyboard.FocusedElement as TextBox).Tag == "MDI"
                                  ? Keyboard.FocusedElement
                                  : this;
            }

            if (GCodeSender.Activate(activate))
            {
                Task.Delay(500).ContinueWith(t => DRO.EnableFocus());
                Application.Current.Dispatcher.BeginInvoke(new System.Action(() =>
                {
                    focusedControl.Focus();
                }), DispatcherPriority.Render);
            }
        }