G-codes/offsets/tools with .Net

Moderators: TomKerekes, dynomotion

Moray
Posts: 226
Joined: Thu Apr 26, 2018 10:16 pm

Re: G-codes/offsets/tools with .Net

Post by Moray » Thu Jan 07, 2021 9:44 pm

Thanks for that Tom.

I'm now trying to understand how all the tool IDs will work, and I'm remembering some of the 'quirks' I came across when implementing my tool table.


Current/SelectedToolSlot returns the tool table index (not the tool slot value), and I probably want to use CurrentToolSlot, not SelectedToolSlot.

When using the Tool Slot in the tool table, the interpreter transfers the toolid if less than 100, or slotid if greater than 100 to the relevant vars, so I don't actually need to worry about that bit, as it's handled by the interpreter.

Now I understand that, I think I can manage the rest of the tool functions. :)

Moray
Posts: 226
Joined: Thu Apr 26, 2018 10:16 pm

Re: G-codes/offsets/tools with .Net

Post by Moray » Tue Jan 12, 2021 7:56 pm

Tom, I'm not sure whether I've just discovered a bug, or if I'm doing something wrong, but I've got a problem with tool diameters.


This is my code for loading values into the tooltable-

Code: Select all

foreach(Tool t in CF.Tools)
            {
                ComboBox_Tools.Items.Add(new ComboBoxItem { Content = t.ToolID.ToString() + "-" + t.Comment });
                KM.CoordMotion.Interpreter.SetupParams.SetTool(it, t.SlotID, t.ToolID, t.Length, t.Diameter, t.XOffset, t.YOffset);
                TextBox_Debug.AppendText("\r\nTool i:" + it + " slot:" + t.SlotID + " id:" + t.ToolID + " len:" + t.Length + " dia:" + t.Diameter + " xoff:" + t.XOffset + " yoff:" + t.YOffset);
                it++;
            }
Which results in the following in my debug output-

Code: Select all

Tool i:0 slot:1 id:1 len:2 dia:0.1 xoff:0 yoff:0
Tool i:1 slot:2 id:2 len:0 dia:2 xoff:0 yoff:0
Tool i:2 slot:3 id:3 len:2 dia:3 xoff:0 yoff:0
Tool i:3 slot:3 id:4 len:0.4 dia:4 xoff:0 yoff:0
Tool i:4 slot:3 id:5 len:0.5 dia:0 xoff:0 yoff:0
Tool i:5 slot:4 id:104 len:0 dia:0 xoff:0 yoff:0
Tool i:6 slot:4 id:105 len:0 dia:0 xoff:0 yoff:0
Now I dump the values back out using the following code -

Code: Select all

for (int i = 0; i <= 127; i++)
            {
                int slot = 0, id = 0;
                double len = 0, dia = 0, xoff = 0, yoff = 0;
                KM.CoordMotion.Interpreter.SetupParams.GetTool(i, ref slot, ref id, ref len, ref dia, ref xoff, ref yoff);
                if(slot !=0 || id !=0 || len!=0)
                    TextBox_Debug.AppendText("\r\nTool i:" + i + " slot:" + slot + " id:" + id + " len:" + len + " dia:" + dia + " xoff:" + xoff + " yoff:" + yoff);

            }
Which results in this debug output-

Code: Select all

Tool i:0 slot:1 id:1 len:2 dia:1 xoff:0 yoff:0
Tool i:1 slot:2 id:2 len:0 dia:1 xoff:0 yoff:0
Tool i:2 slot:3 id:3 len:2 dia:2 xoff:0 yoff:0
Tool i:3 slot:3 id:4 len:0.4 dia:0 xoff:0 yoff:0
Tool i:4 slot:3 id:5 len:0.5 dia:0 xoff:0 yoff:0
Tool i:5 slot:4 id:104 len:0 dia:0 xoff:0 yoff:0
Tool i:6 slot:4 id:105 len:0 dia:0 xoff:0 yoff:0
The tool diameters for tools 1,2 and 3, are not what was stored into the tool table.
Any ideas?

User avatar
TomKerekes
Posts: 1535
Joined: Mon Dec 04, 2017 1:49 am

Re: G-codes/offsets/tools with .Net

Post by TomKerekes » Tue Jan 12, 2021 8:12 pm

Oops that's a bug.

This code from KMotion_dotNet_Interop.cpp is missing setting the diameter:

Code: Select all

extern "C" __declspec(dllexport) 	void __stdcall KM_dotnet_Interop_GCodeInterpreter_SetTool(int *handle, int index, int slot, int id, 
																							  double length, double diameter, double xoffset, double yoffset)
{
	 CGCodeInterpreter *GC_dll=(CGCodeInterpreter *)handle;
 
	 GC_dll->p_setup->tool_table[index].slot = slot;
	 GC_dll->p_setup->tool_table[index].id = id;
	 GC_dll->p_setup->tool_table[index].length = length;
	 GC_dll->p_setup->tool_table[index].xoffset = xoffset;
	 GC_dll->p_setup->tool_table[index].yoffset = yoffset;
} 

extern "C" __declspec(dllexport) 	void __stdcall KM_dotnet_Interop_GCodeInterpreter_GetTool(int *handle, int index, int *slot, int *id, 
																							  double *length, double *diameter, double *xoffset, double *yoffset)
{
	 CGCodeInterpreter *GC_dll=(CGCodeInterpreter *)handle;
	 *slot = GC_dll->GetRealTimeState()->tool_table[index].slot;
	 *id = GC_dll->GetRealTimeState()->tool_table[index].id;
	 *length = GC_dll->GetRealTimeState()->tool_table[index].length;
	 *diameter = GC_dll->GetRealTimeState()->tool_table[index].diameter;
	 *xoffset = GC_dll->GetRealTimeState()->tool_table[index].xoffset;
	 *yoffset = GC_dll->GetRealTimeState()->tool_table[index].yoffset;
} 
Regards,

Tom Kerekes
Dynomotion, Inc.

Moray
Posts: 226
Joined: Thu Apr 26, 2018 10:16 pm

Re: G-codes/offsets/tools with .Net

Post by Moray » Tue Jan 12, 2021 9:42 pm

That would do it 8-)

And I've even managed to add the required code, and build a new DLL :D

Code: Select all

Tool i:0 slot:1 id:1 len:2 dia:0.1 xoff:0 yoff:0
Tool i:1 slot:2 id:2 len:0 dia:2 xoff:0 yoff:0
Tool i:2 slot:3 id:3 len:2 dia:3 xoff:0 yoff:0
Tool i:3 slot:3 id:4 len:0.4 dia:4 xoff:0 yoff:0
Tool i:4 slot:3 id:5 len:0.5 dia:0 xoff:0 yoff:0
Tool i:5 slot:4 id:104 len:0 dia:0 xoff:0 yoff:0
Tool i:6 slot:4 id:105 len:0 dia:0 xoff:0 yoff:0
Tool i:0 slot:1 id:1 len:2 dia:0.1 xoff:0 yoff:0


Tool i:1 slot:2 id:2 len:0 dia:2 xoff:0 yoff:0
Tool i:2 slot:3 id:3 len:2 dia:3 xoff:0 yoff:0
Tool i:3 slot:3 id:4 len:0.4 dia:4 xoff:0 yoff:0
Tool i:4 slot:3 id:5 len:0.5 dia:0 xoff:0 yoff:0
Tool i:5 slot:4 id:104 len:0 dia:0 xoff:0 yoff:0
Tool i:6 slot:4 id:105 len:0 dia:0 xoff:0 yoff:0
And the new code-

Code: Select all

extern "C" __declspec(dllexport) 	void __stdcall KM_dotnet_Interop_GCodeInterpreter_SetTool(int *handle, int index, int slot, int id, 
																							  double length, double diameter, double xoffset, double yoffset)
{
	 CGCodeInterpreter *GC_dll=(CGCodeInterpreter *)handle;
 
	 GC_dll->p_setup->tool_table[index].slot = slot;
	 GC_dll->p_setup->tool_table[index].id = id;
	 GC_dll->p_setup->tool_table[index].length = length;
	 GC_dll->p_setup->tool_table[index].diameter = diameter;
	 GC_dll->p_setup->tool_table[index].xoffset = xoffset;
	 GC_dll->p_setup->tool_table[index].yoffset = yoffset;
} 

Moray
Posts: 226
Joined: Thu Apr 26, 2018 10:16 pm

Re: G-codes/offsets/tools with .Net

Post by Moray » Sat Jan 23, 2021 8:58 pm

Having got a bit bored with re-writing KMotionCNC C++ to C#, I've been working on a little idea I've wanted to implement for a while.

Automatic C code generation.

To configure channel settings, go to the Config tab, then the Channels tab, then click Config, which will open a new window.

Config tab you can set the overall axis channels (defincoordsystem code), the directory where C programs will be generated to (this uses a bit of a bodge just now due to lack of WPF functionality to select folders. Just navigate to the folder you want to use, then click open. If you happen to click on a file within the folder you'd like to use, it doesn't matter. Still just click open), and any prefix you'd like to add to the files.
KStep option works (but only adds code for enabling it, not any of the autodisable code yet). Kanalog does nothing.

Channels tab should be pretty obvious, although the gain/offset boxes aren't labelled. Limitswitch options don't do anything yet, as I need to go and read up on how to implement them.

C Output tab. Generate Init, will generate the init file from the settings in the other tabs, and display in the text box.
Generate Chan, was simply for testing, and only generates the channel code for whatever channel is selected on the previous tab, and displays it in the box.

Once you've created all the settings above, click save, then close the window.
Then if you go to the usual User Button config on the main widow, the combobox will have an option for AutoC. Select that, enter a thread number as normal, select Init from the second combobox that appear, then click Save Settings. Remember to add a button name, or it won't appear.


KMoCNC needs copied to your KMotion/Release folder.
I've attached my KMC_3040mm.xml as it has lots of configuration options already configured, and it can be saved anywhere. Select it on the Tool/Setup Files tab.
To load KMoCNC, you'll also need to create a copy of the emc.var file (in KMotion/Data ), called emc_test.var (I would add a copy, but the forum software doesn't let me upload .var files, and it's probably as quick copy, pasting, and renaming your existing file, than trying to change a file suffix!)

I'm planning on expanding this functionality as I need it, but if somebody does want to use it and is keen for something specific to be implemented, let me know and I'll consider it.
Next on my hit list is keyboard jogging, and finishing the KFlopToPC functionality.

<edited to remove old .exe>
Attachments
KMC_3040mm.xml
(34.23 KiB) Downloaded 9 times
Last edited by Moray on Sat Jan 23, 2021 10:14 pm, edited 1 time in total.

User avatar
TomKerekes
Posts: 1535
Joined: Mon Dec 04, 2017 1:49 am

Re: G-codes/offsets/tools with .Net

Post by TomKerekes » Sat Jan 23, 2021 9:28 pm

Hi Moray,

I can't get it to run. I had to add your 3DTools.dll to get it to do anything. But says Unable to open file C:\KMotion435f\KMotion\Data\emc_test.var even though I believe it is there. It displays that message several times the main screen pops up then disappears and exits.
Regards,

Tom Kerekes
Dynomotion, Inc.

Moray
Posts: 226
Joined: Thu Apr 26, 2018 10:16 pm

Re: G-codes/offsets/tools with .Net

Post by Moray » Sat Jan 23, 2021 9:51 pm

Tom, I've just realised emc_test.var is still hardcoded to C:\KMotion435f\KMotion\Data\emc_test.var
And I'd forgotten about the need for 3DTools.

I've actually just tried removing the reference to emc_test.var, and everything seems to load. Is not having a var file specified likely to cause any problems?
New exe attached.
<edited to remove old .exe>
Last edited by Moray on Sat Jan 23, 2021 10:14 pm, edited 1 time in total.

Moray
Posts: 226
Joined: Thu Apr 26, 2018 10:16 pm

Re: G-codes/offsets/tools with .Net

Post by Moray » Sat Jan 23, 2021 9:55 pm

And I've just remembered, make sure you load a GCode file, otherwise certain MDI functions may cause a crash.

I re-worked my internal handling of MDIs, which introduced a problem whereby the GCode viewer can crash if there is no text lines. It's on my list of bugs to fix, along with validation of values entered into text boxes.

Moray
Posts: 226
Joined: Thu Apr 26, 2018 10:16 pm

Re: G-codes/offsets/tools with .Net

Post by Moray » Sat Jan 23, 2021 10:13 pm

Here's a file with the GCode viewer bug hopefully squashed.
Attachments
KMoCNC.exe
(149 KiB) Downloaded 15 times

User avatar
TomKerekes
Posts: 1535
Joined: Mon Dec 04, 2017 1:49 am

Re: G-codes/offsets/tools with .Net

Post by TomKerekes » Mon Jan 25, 2021 8:25 pm

Hi Moray,

Cool it runs (after I figured out I needed to create a \C Programs\KMoCNC folder)

AutoC.png
I've actually just tried removing the reference to emc_test.var, and everything seems to load. Is not having a var file specified likely to cause any problems?
If no filename is specified emc.var is used.
Regards,

Tom Kerekes
Dynomotion, Inc.

Post Reply