G-codes/offsets/tools with .Net

Moderators: TomKerekes, dynomotion

Post Reply
Demondor
Posts: 16
Joined: Fri Mar 22, 2019 8:49 am

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

Post by Demondor » Thu Mar 12, 2020 2:19 pm

In application on wpf, axis and speed is selected.
xml - Jog Button

Code: Select all

<Button
                        Grid.Row="0"
                        Grid.Column="1"
                        Width="170"
                        Margin="5"
                        Content="Jog +"
                        FontSize="30"
                        Style="{DynamicResource myButton}">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="PreviewMouseDown">
                                <ei:CallMethodAction MethodName="JogPlusRunSend" TargetObject="{Binding}" />
                            </i:EventTrigger>
                            <i:EventTrigger EventName="PreviewMouseUp">
                                <ei:CallMethodAction MethodName="JogStopSend" TargetObject="{Binding}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </Button>
                    
in ViewModel code

Code: Select all

 public void JogPlusRunSend(object sender, MouseButtonEventArgs e)
        { model.kflop.JogRun(currAxis, 1, currRB); }
        public void JogMinusRunSend(object sender, MouseButtonEventArgs e)
        { model.kflop.JogRun(currAxis, -1, currRB); }
        public void JogStopSend(object sender, MouseButtonEventArgs e)
        { model.kflop.JogStop(currAxis); }
implementation in Code

Code: Select all

//model.kflop
/************************************************************************************************************************/
        // Jog Run
        /************************************************************************************************************************/
        public void JogRun(Axis axes, int curDir, int curStep)
        {
            if (!Excecute)
            {
                Excecute = true;
                // определяем направление
                string dir = string.Empty;
                if (curDir == -1)
                    dir = "-";

                // определяем шаг
                string vel = string.Empty;
                if (curStep == 1)
                    vel = Jog.JogVel[0].ToString();
                if (curStep == 2)
                    vel = Jog.JogVel[1].ToString();
                if (curStep == 3)
                    vel = Jog.JogVel[2].ToString();
                if (curStep == 4)
                    vel = Jog.JogVel[3].ToString();
                if (curStep == 5)
                    vel = Jog.JogVel[4].ToString();
                if (curStep == 6)
                    vel = Jog.JogVel[4].ToString();

                int ch = AxesToChanel(axes);
                string command = "Jog" + ch.ToString() + "=" + dir + vel;
                WriteLine(command);
            }
        }

        /************************************************************************************************************************/
        // Jog Stop
        /************************************************************************************************************************/
        public void JogStop(Axis axes)
        {
            int ch = AxesToChanel(axes);
            string command = "Jog" + ch.ToString() + "=0";
            WriteLine(command);
            Excecute = false;
        }

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

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

Post by TomKerekes » Thu Mar 12, 2020 5:21 pm

Hi Demondor,

Thanks for sharing!
Regards,

Tom Kerekes
Dynomotion, Inc.

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

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

Post by Moray » Thu Mar 12, 2020 10:29 pm

Interesting approach Demondor.
I've opted for using the .Net axis approach, but I'm guessing both approaches achieve the same outcome.

I've just finished testing all my jog buttons, and I'm happy to report after a brief bit head scratching, adding a missing "-", and realising I still had my Z counts per inch stupidly low for probe testing, that everything is working as expected.

Next step is to get Offsets and Tools functioning, then I can start on GCode formatting and then graphics :)

Demondor
Posts: 16
Joined: Fri Mar 22, 2019 8:49 am

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

Post by Demondor » Fri Mar 13, 2020 8:21 am

I can’t tell you about the tools, I’m not using it. Offset please.

In Timer

Code: Select all

 if (Connect)
 {
       if (KM.WaitToken(100) == KMOTION_TOKEN.KMOTION_LOCKED)
        {
        ..... You code ...
         offsetID = KM.CoordMotion.Interpreter.SetupParams.OriginIndex; // Current offset index
        }
 }
in model

Code: Select all

// generate an event when the current offset changes to display on the screen
private int _offsetID;
public int offsetID { get { return _offsetID; } set { if (_offsetID != value) ChangeOffsetEvent(value); _offsetID = value;} }

/************************************************************************************************************************/
// Сообщение об изменении offsetID
/************************************************************************************************************************/
public event EventHandler onChangeOffset;
private void ChangeOffsetEvent(int value)
{
	INTEventArgs e = new INTEventArgs(value);
        if (onChangeOffset != null)
            onChangeOffset(this, e);
}

/******************************************************************************************************/
// Класс для передачи значения INT
/******************************************************************************************************/
public class INTEventArgs : EventArgs
{
     public int Int;

     public INTEventArgs() { }

     public INTEventArgs(int Int)
     { this.Int = Int; }
}
/------------------------------------------------------------------------------------------------------------------------------------------------------/
Class Offset
/************************************************************************************************************************/
// Перевод значения в название Offset
/************************************************************************************************************************/
public string OffsetStr(int code)
{
      string str = string.Empty;
      switch (code)
      {
          case 0: { str = "G92"; }break;
          case 1: { str = "G54"; } break;
          case 2: { str = "G55"; } break;
          case 3: { str = "G56"; } break;
          case 4: { str = "G57"; } break;
          case 5: { str = "G58"; } break;
          case 6: { str = "G59"; } break;
          case 7: { str = "G59.1"; } break;
          case 8: { str = "G59.2"; } break;
          case 9: { str = "G59.3"; } break;
      }
      return str;
}
        
/************************************************************************************************************************/
// Установка Offset
/************************************************************************************************************************/
public void SetOffset()
{
     for (int i = 0; i < 10; i++)
     {
        try
        {
           KM.CoordMotion.Interpreter.SetOrigin(i, Offset.ArrOffset[i].X, Offset.ArrOffset[i].Y, Offset.ArrOffset[i].Z, Offset.ArrOffset[i].A, Offset.ArrOffset[i].B, Offset.ArrOffset[i].C);
         }
         catch { }
      }
}
in xaml

Code: Select all

 <TextBlock
            Grid.Row="6"
            Grid.Column="1"
            Foreground="White"
            TextAlignment="Center"
            VerticalAlignment="Center"
            Text ="{Binding OffsetStr}"
            />
in ViewModel

Code: Select all

private string _OffsetStr;
public string OffsetStr { get { return _OffsetStr; } set { _OffsetStr = value; OnPropertyChanged(); } }
......
model.kflop.onChangeOffset += Kflop_onChangeOffset;
......
/************************************************************************************************************************/
// Изменился Offset - показать на экране
/************************************************************************************************************************/
private void Kflop_onChangeOffset(object sender, EventArgs e)
{
    var mes = e as INTEventArgs;
    OffsetStr = Offset.OffsetStr(mes.Int);
}
Last edited by Demondor on Fri Mar 13, 2020 8:37 am, edited 1 time in total.

Demondor
Posts: 16
Joined: Fri Mar 22, 2019 8:49 am

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

Post by Demondor » Fri Mar 13, 2020 8:31 am

I am writing an application for controlling a manipulator such as a scarа for an enterprise. I can’t lay out all the code, but I can tell you the points that I used. This is already the 3rd version of the program, for the management we “came up” with our own commands, they are parsed line by line and translated into the g code. 100 lines is equivalent to 14,000 lines in g code. Then a file is created and slipped into the interpreter.
Sorry for my English.

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

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

Post by Moray » Fri Mar 13, 2020 10:29 pm

Thanks for that Demonder.
At the moment, I'm trying to replicate the KMotionCNC functionality, so I'm using a combobox to select/show offsets.

What I'm finding the most difficult, is finding the required functions, and is something I keep meaning to mention.

Tom, one issue I have, is although I can find functions/properties within the help file, it's not always clear where you access them in dotNet. Once I've found the needed item, it can then take a good bit of guessing, or searching through example program code to find out where the item is in the hierarchy. However, I'm not sure how easy it would be to improve the documentation in this regard.

I've got offsets almost functioning (just need to add the code to copy them from my config file into the interpreter), but I'm needing some clarification for tools.
I've found ToolLengthOffsetIndex, which is fine, however what is the difference between CurrentToolSlot, and SelectedToolSlot?

I see SelectedToolSlot property, has the note "Tool slot selected but not active". I'm assuming this has something to do with M6/toolchange sequences?

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

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

Post by TomKerekes » Fri Mar 13, 2020 11:01 pm

Hi Moray,
I've found ToolLengthOffsetIndex, which is fine, however what is the difference between CurrentToolSlot, and SelectedToolSlot?
SelectedToolSlot is the table index of the last T word specified. For example code "T9" would update the SelectedToolSlot but some other tool might be still loaded.

After an M6 is executed to actually load the tool then CurrentToolSlot will be updated to reflect the last tool loaded.

HTH
Regards,

Tom Kerekes
Dynomotion, Inc.

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

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

Post by Moray » Sun Mar 15, 2020 9:06 pm

Tom,

I seem to have hit a problem with random offsets and my program jumping to G21 mode, and I'm struggling to understand what is causing it. Even removing all code that sets offsets, and the problem still exists.

Starting with the KFlop rebooted, I run my program, and I've added code to print out all the current offsets, which are 0 -
Offset Set indexi:0X:0Y:0 Z:0
Offset Set indexi:1X:0Y:0 Z:0
Offset Set indexi:2X:0Y:0 Z:0
Offset Set indexi:3X:0Y:0 Z:0
Offset Set indexi:4X:0Y:0 Z:0
Offset Set indexi:5X:0Y:0 Z:0
Offset Set indexi:6X:0Y:0 Z:0
Offset Set indexi:7X:0Y:0 Z:0
Offset Set indexi:8X:0Y:0 Z:0
Offset Set indexi:9X:0Y:0 Z:0
SelectedToolSlot:0
CurrentToolSlot:0
Then when I run any kind of MDI, the interpreter jumps to G21/mm mode, and the offsets change to -

Offset Set indexi:0X:5.119598Y:0 Z:-0.565039
Offset Set indexi:1X:-5.9207Y:0 Z:0
Offset Set indexi:2X:-1.75Y:-1.75 Z:-1.5
Offset Set indexi:3X:0Y:0 Z:0
Offset Set indexi:4X:0Y:0 Z:0
Offset Set indexi:5X:0Y:0 Z:0
Offset Set indexi:6X:0Y:0 Z:0
Offset Set indexi:7X:0Y:0 Z:0
Offset Set indexi:8X:0Y:0 Z:0
Offset Set indexi:9X:0Y:0 Z:0
SelectedToolSlot:0
CurrentToolSlot
Even if I add my code back in, my offsets will show up as expected, but as soon as I try changing anything, my program will jump to G21/mm mode, and set the offsets to the above.

If I then reload my offsets, everything works as expected (with the exception my DROs don't change, as during this I've realised N_AxisPosition doesn't compensate for offsets...)

My MDI replicates how KMotionCNC does things, by writing the MDI to a file, then loading it to the interpreter. I've even tried commenting out the code my program uses for issuing the G21 command, but the mode still changes, and the random offsets appear.


And having just typed all that, I've realised the offsets being loaded, are the ones I've got setup in KMotionCNC, although I've currently got KMotionCNC all set to G20/inch…
How/why is that happening, and how do I stop it?
I'm assuming there is some kind of setting file the interpreter is loading..

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

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

Post by TomKerekes » Sun Mar 15, 2020 10:49 pm

Hi Moray,

The GCodeInterpreter has a VarsFile and SetupFile property where you can configure what files are used to save/restore the GCode Variables and what file is used to Setup the Interpreter.

Regarding switching to Metric mode: If there is a SetupFile specified then the first time the Interpreter is used it will read the SetupFile and make the specified settings possibly switching to Metric mode.

Regarding Offsets: The Offsets are saved in the VarsFile. If no VarsFile is specified then "\\KMotion\\Data\\emc.var" which might be the same file as KMotionCNC is set to use either by having nothing specified or explicitly.

HTH
Regards,

Tom Kerekes
Dynomotion, Inc.

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

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

Post by Moray » Sun Mar 15, 2020 11:19 pm

That makes sense. I've spent two nights chasing this problem, and it was only tonight I thought about checking KMotionCNC, and you are correct, I don't have a vars file specified.

I'm not specifying any files in the interpreter, so I'm assuming it's using default.set, as it does have units set to default to MM.

Now I've learnt this, how do I get around the problem of external offsets being used?
Do I need to initialise things in a different order, or just send some kind of command to the interpreter prior to loading my settings?


Also, during all this, I've noticed that any changes to offsets using SetOrigin, don't take effect until the related offset is requested again. Do I need to explicitly call the offset again, or is there some method I'm missing?

Post Reply