public void CycleStart() { if (grblState.State == GrblStates.Hold || (grblState.State == GrblStates.Run && grblState.Substate == 1)) { Comms.com.WriteByte(GrblLegacy.ConvertRTCommand(GrblConstants.CMD_CYCLE_START)); } else if (grblState.State == GrblStates.Tool) { model.Message = ""; Comms.com.WriteByte(GrblLegacy.ConvertRTCommand(GrblConstants.CMD_CYCLE_START)); } else if (JobTimer.IsRunning) { JobTimer.Pause = false; streamingHandler.Call(StreamingState.Send, false); } else if (GCode.File.IsLoaded) { model.RunTime = ""; job.ACKPending = job.CurrLine = job.ACKPending = job.serialUsed = 0; job.Started = false; job.NextRow = GCode.File.Data.Rows[0]; Comms.com.PurgeQueue(); model.Message = ""; JobTimer.Start(); streamingHandler.Call(StreamingState.Send, false); System.Threading.Thread.Sleep(250); SendNextLine(); } }
private void RequestStatus() { parameters.WorkPositionOffset.Z = double.NaN; if (double.IsNaN(parameters.WorkPosition.X) || true) // If not NaN then MPG is polling { Comms.com.WriteByte(GrblLegacy.ConvertRTCommand(GrblConstants.CMD_STATUS_REPORT_ALL)); } }
private void DataReceived(string data) { if (awaitCoord) { Thread.Sleep(50); Comms.com.WriteByte(GrblLegacy.ConvertRTCommand(GrblConstants.CMD_STATUS_REPORT)); } else { GotPosition?.Invoke("ok"); } }
private void btnCurrPos_Click(object sender, RoutedEventArgs e) { Comms.com.CommandState = Comms.State.AwaitAck; if (double.IsNaN(parameters.WorkPosition.X)) // If not NaN then MPG is polling { Comms.com.WriteByte(GrblLegacy.ConvertRTCommand(GrblConstants.CMD_STATUS_REPORT_ALL)); } awaitCoord = true; while (awaitCoord) { Comms.com.AwaitResponse(); // TODO: add timeout? } }
public void CycleStart() { if (grblState.State == GrblStates.Hold || grblState.State == GrblStates.Tool || (grblState.State == GrblStates.Run && grblState.Substate == 1)) { Comms.com.WriteByte(GrblLegacy.ConvertRTCommand(GrblConstants.CMD_CYCLE_START)); } else if (GCode.Loaded) { lblRunTime.Content = ""; ACKPending = CurrLine = serialUsed = 0; pgmStarted = false; System.Threading.Thread.Sleep(250); Comms.com.PurgeQueue(); model.Message = ""; nextRow = GCode.Data.Rows[0]; JobTimer.Start(); SetStreamingState(StreamingState.Send); // DataReceived("!start"); } }
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); }
public bool GotoMachinePosition(Position pos, AxisFlags axisflags) { bool?res = null; bool wait = true; string command = "G53G0" + pos.ToString(axisflags); Comms.com.PurgeQueue(); new Thread(() => { res = WaitFor.AckResponse <string>( cancellationToken, null, a => Grbl.OnResponseReceived += a, a => Grbl.OnResponseReceived -= a, 100, () => Grbl.ExecuteCommand(command)); }).Start(); while (res == null) { EventUtils.DoEvents(); } if (res == true) { while (wait && !isCancelled) { res = null; new Thread(() => { res = WaitFor.SingleEvent <string>( cancellationToken, null, a => Grbl.OnRealtimeStatusProcessed += a, a => Grbl.OnRealtimeStatusProcessed -= a, 200, () => Comms.com.WriteByte(GrblLegacy.ConvertRTCommand(GrblConstants.CMD_STATUS_REPORT))); }).Start(); while (res == null) { EventUtils.DoEvents(); } wait = res != true; int i = 0, axes = (int)axisflags; while (axes != 0 && !wait) { if ((axes & 0x01) != 0) { wait = Math.Abs(pos.Values[i] - Grbl.MachinePosition.Values[i]) >= 0.003d; // use step resolution plus some? } i++; axes >>= 1; } if (wait) { Thread.Sleep(200); // needed? } } } return(isCancelled ? false : !wait); }
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; if (initOK != true) { model.Message = "Waiting for controller..."; Comms.com.PurgeQueue(); Comms.com.WriteByte(GrblLegacy.ConvertRTCommand(GrblConstants.CMD_STATUS_REPORT)); int timeout = 30; // 1.5s do { System.Threading.Thread.Sleep(50); } while (Comms.com.Reply == "" && --timeout != 0); if (Comms.com.Reply.StartsWith("<Alarm")) { GrblViewModel data = (GrblViewModel)DataContext; data.ParseStatus(Comms.com.Reply); // Alarm 1, 2 and 10 are critical events if (!(data.GrblState.Substate == 1 || data.GrblState.Substate == 2 || data.GrblState.Substate == 10)) { InitSystem(); } } else if (Comms.com.Reply != "") { InitSystem(); } } 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 } if (GCodeSender.Activate(activate)) { Task.Delay(500).ContinueWith(t => DRO.EnableFocus()); Application.Current.Dispatcher.BeginInvoke(new System.Action(() => { Focus(); }), DispatcherPriority.Render); } }
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); }
public bool GotoMachinePosition(Position pos, AxisFlags axisflags) { bool? res = null; bool wait = true, running = false; double delta, delta_max = 0d; string command = "G53" + RapidCommand + pos.ToString(axisflags); Comms.com.PurgeQueue(); Grbl.Poller.SetState(0); new Thread(() => { res = WaitFor.AckResponse <string>( cancellationToken, null, a => Grbl.OnResponseReceived += a, a => Grbl.OnResponseReceived -= a, 1000, () => Grbl.ExecuteCommand(command)); }).Start(); while (res == null) { EventUtils.DoEvents(); } if (res == true) { while (wait && !isCancelled) { res = null; new Thread(() => { res = WaitFor.SingleEvent <string>( cancellationToken, null, a => Grbl.OnRealtimeStatusProcessed += a, a => Grbl.OnRealtimeStatusProcessed -= a, 400, () => Comms.com.WriteByte(GrblLegacy.ConvertRTCommand(GrblConstants.CMD_STATUS_REPORT))); }).Start(); while (res == null) { EventUtils.DoEvents(); } wait = res != true; running |= Grbl.GrblState.State == GrblStates.Run; int i = 0, axes = (int)axisflags; while (axes != 0 && !wait) { if ((axes & 0x01) != 0) { delta = Math.Abs(pos.Values[i] - Grbl.MachinePosition.Values[i]); wait = delta > Math.Max(0.003d, GrblInfo.TravelResolution.Values[i] * 2d); delta_max = Math.Max(delta, delta_max); if (wait && Grbl.GrblState.State == GrblStates.Idle && (running || delta_max < 0.01d)) { wait = false; isCancelled = true; } } i++; axes >>= 1; } if (wait) { Thread.Sleep(AppConfig.Settings.Base.PollInterval); // needed? } } } Grbl.Poller.SetState(AppConfig.Settings.Base.PollInterval); return(isCancelled ? false : !wait); }
void btnHold_Click(object sender, RoutedEventArgs e) { Comms.com.WriteByte(GrblLegacy.ConvertRTCommand(GrblConstants.CMD_FEED_HOLD)); }
public void CycleStart() { if (grblState.State == GrblStates.Hold || (grblState.State == GrblStates.Run && grblState.Substate == 1) || (grblState.State == GrblStates.Door && grblState.Substate == 0)) { Comms.com.WriteByte(GrblLegacy.ConvertRTCommand(GrblConstants.CMD_CYCLE_START)); } else if (grblState.State == GrblStates.Idle && model.SDRewind) { streamingHandler.Call(StreamingState.Start, false); Comms.com.WriteByte(GrblLegacy.ConvertRTCommand(GrblConstants.CMD_CYCLE_START)); } else if (grblState.State == GrblStates.Tool) { model.Message = ""; Comms.com.WriteByte(GrblLegacy.ConvertRTCommand(GrblConstants.CMD_CYCLE_START)); } else if (JobTimer.IsRunning) { JobTimer.Pause = false; streamingHandler.Call(StreamingState.Send, false); } else if (GCode.File.IsLoaded) { model.Message = model.RunTime = string.Empty; if (model.IsSDCardJob) { Comms.com.WriteCommand(GrblConstants.CMD_SDCARD_RUN + model.FileName.Substring(7)); } else { job.ToolChangeLine = -1; model.BlockExecuting = 0; job.ACKPending = job.CurrLine = job.serialUsed = missed = 0; job.Started = job.Transferred = job.HasError = false; job.NextRow = GCode.File.Data.Rows[0]; Comms.com.PurgeQueue(); JobTimer.Start(); streamingHandler.Call(StreamingState.Send, false); if ((job.IsChecking = model.GrblState.State == GrblStates.Check)) { model.Message = (string)FindResource("Checking"); } bool?res = null; CancellationToken cancellationToken = new CancellationToken(); // Wait a bit for unlikely event before starting... new Thread(() => { res = WaitFor.SingleEvent <string>( cancellationToken, null, a => model.OnGrblReset += a, a => model.OnGrblReset -= a, 250); }).Start(); while (res == null) { EventUtils.DoEvents(); } SendNextLine(); } } }
public void Activate(bool activate, ViewType chgMode) { if (activate) { GCodeSender.RewindFile(); GCodeSender.SetStreamingState(GCodeSender.GCode.Loaded ? StreamingState.Idle : StreamingState.NoFile); if (!initOK) { Comms.com.WriteByte(GrblLegacy.ConvertRTCommand(GrblConstants.CMD_STATUS_REPORT)); int timeout = 30; // 1.5s do { System.Threading.Thread.Sleep(50); } while (Comms.com.Reply == "" && --timeout != 0); if (Comms.com.Reply.StartsWith("<Alarm")) { if (Comms.com.Reply.StartsWith("<Alarm:")) { int i = Comms.com.Reply.IndexOf('|'); string s = Comms.com.Reply.Substring(7, i - 7); if (!"1,2,10".Contains(s)) // Alarm 1, 2 and 10 are critical events { InitSystem(); } if (s == "11") { txtStatus.Text = "<Home> to continue."; } else { txtStatus.Text = "<Reset> then <Unlock> to continue."; } } else { txtStatus.Text = "<Reset> then <Unlock> to continue."; } } else { InitSystem(); } } #if ADD_CAMERA if (MainWindow.Camera != null) { MainWindow.Camera.CameraControl.MoveOffset += Camera_MoveOffset; MainWindow.Camera.Opened += Camera_Opened; } #endif //if (viewer == null) // viewer = new Viewer(); if (GCodeSender.GCode.Loaded) { MainWindow.ui.WindowTitle = ((GrblViewModel)DataContext).FileName; } } else if (mode != ViewType.Shutdown) { DRO.IsFocusable = false; #if ADD_CAMERA if (MainWindow.Camera != null) { MainWindow.Camera.CameraControl.MoveOffset -= Camera_MoveOffset; } #endif } if (GCodeSender.Activate(activate)) { Task.Delay(500).ContinueWith(t => DRO.EnableFocus()); Application.Current.Dispatcher.BeginInvoke(new System.Action(() => { Focus(); }), DispatcherPriority.Render); } }