Writing a plugin, some questions

This is where you talk about Plugins. How they are made and how they work, show examples.

Writing a plugin, some questions

Postby sebmay » Sun May 14, 2017 6:29 pm

Hello,
I wrote a plugin for UCCNC. Rotate, translate jobs, optimize drilling path with Djiskra, probe the surface to correct the Z, like Caties Plugin, but more the way i want it, and probe the tools to a fixed point. I use a stepcraft 3D probe.
VS 2015 C# Win 7 64 Intel I5, 8g RAM + UC300ETH 5LPT

I have got a spurious effect, and i think i do not proceed correctly.

The plugin shows a windows form. Somewhere, when a button in the form is pressed, i want to EXEC some Gcode :
G54 G00 G17 G90 G49 G80 F100 S3000
G92 X0 Y0 Z0
G31 Z-15 F100
G92 Z0
G00 Z3 F100
G31 Z-15 F30
G92 Z0
(END)

Etc...

No UC.<function> can be called from the GUI (Is this correct ? anyway i suppose this assertion, after lots of tests). None ? some of them ? then which ones ?

I use a Queue to push the gcode lines from the UI thread (OnClick func..) to the plugin loop, one by one. I lock the queue.SyncRoot on both sides, to make sure there are no concurrent access.

Into the UCplugin loop, i dequeue the line, and:

(dequeue code in String s)
UC.Codesync(s); // ************ Should i suppose that in any case the CNC stand still when the Codesync exits ? Now i do not suppose this
UC.Wait(200); // Is this the best way to wait ? Or do i just delay the execution ?
while (UC.IsMoving()) Application.DoEvents(); // After tests, i found this way of using IsMoving. Is this the best way ?

On the other side (in the form UI) :
This is my enqueue function Exec("Some GCODE");
private void Exec(String s) {
if (StopAll)
return;
// AddLogProbe(s);
Boolean success = false;
while (!success) {
if (Monitor.TryEnter(Pluginmain.Q.SyncRoot)) {
try {
// use object
Pluginmain.Q.Enqueue(s);
success = true;
} finally {
Monitor.Exit(Pluginmain.Q.SyncRoot);
}
}
if (!success)
Wait5ticks();
}
// AddLogProbe("Queued !");
}


Question : What is the correct way to send gcode to UCCNC, and to wait for completion.....
In my form UI, i would like to know the "good" Exec (String Gcode), probably linked to some function in UC.plugin.
I want each line sent be executed, and wait in the UI form until everything is finished...line by line, or i can enqueue many lines, and wait for complete end of execution : i mean the CNC machine has finishes ALL movments.

Could you provide a simplistic plugin source code, with one form, one button, and when the user press that button, some GCODE is sent and executed. Like the gcode snippet i gave.

Some inert code like G90, which does not move anything
some G1 or G2 or G3 or G83 : moves
some G31 : move with stop when probe touch the stock

Supposing that the UI form Exec func returns when there are no more movement. A kind of codesync.

My program works most of the time.
I got those trouble, erratically :
-When i press the button, nothing happens
- When i press the button, the G31 is like "skipped"

Another question with Macro : I wrote some macro, with same problem :

exec.Code("gcode...");
exec.Wait(200);
while(exec.IsMoving()){}
Sometimes, some code is skipped. I suppose i do not operate correctly.

Thanks,

Sebastien






*********

My loop function : Notice that all function like myform.SayMoving calls a BeginInvoke, in order to have the action executed in the UI thread, using BeginInvoke in order to avoid to "load" the loop function, and give the control back ASAP to UCCNC core.

public void Loop_event() {
if (loopstop)
return;
if (UC==null || myform == null || myform.IsDisposed || loopworking)
return;
myform.IncrementTick(); // Begin invoke to UI, in order to be able to wait, in the UI, n occurrence of the loop.
loopworking = true;
Boolean prov;
prov = UC.IsLoading();
if (prov != Oldloading) {
Oldloading = prov;
myform.SayLoading(prov); // Set a Boolean in the UI : IsLoading cannot be called from the UI. I am sure !
}
prov = UC.IsMoving();
if (prov != Oldmoving) {
Oldmoving = prov;
myform.SayMoving(prov);
}
prov = UC.GetLED(56) && UC.GetLED(57) && UC.GetLED(58);
if(prov != OldHomed) {
OldHomed = prov;
myform.ShowHomed(prov);
}
if (Monitor.TryEnter (Q.SyncRoot)) {
try {
if ( Q.Count > 0) {
String s = "";

s = (String)Q.Dequeue();
while (UC.IsMoving()) Application.DoEvents(); // do not start anything if the cnc is moving. Is this correct way to do it ?

if (s == "***COORD***") { // The only way i found to access XYZ positions in the UI
X = Convert.ToSingle(UC.GetXpos());
Y = Convert.ToSingle(UC.GetYpos());
Z = Convert.ToSingle(UC.GetZpos());
MX = Convert.ToSingle(UC.GetXmachpos());
MY = Convert.ToSingle(UC.GetYmachpos());
MZ = Convert.ToSingle(UC.GetZmachpos());

} else if (s.StartsWith("***TOOLSZ***")) { // Get the tool height. Not used at the moment

Int32 t = Int32.Parse(s.Substring(12));
Int32 register = t < 21 ? t + 195 : t + 900;
Double len = UC.Getfielddouble(true, register);

} else if (s.StartsWith("***Reset***")) {
StopAll = false;
// myform.SetStopAll(StopAll);
} else {
if (!StopAll) {
myform.DoLog(s);

UC.Codesync(s);
UC.Wait(200);
while (UC.IsMoving()) Application.DoEvents();
}
}
// SystemSounds.Asterisk.Play();
}
} finally {
Monitor.Exit(Q.SyncRoot);
}
goto exit;
}
//if (firstrun){
// firstrun = false;
// //Write code here which has to be run on first cycle only...
//}
//try {
//} catch (Exception) { }
exit:
loopworking = false;
}
sebmay
 
Posts: 37
Joined: Sun Mar 05, 2017 12:16 pm

Return to Plugins

Who is online

Users browsing this forum: No registered users and 8 guests