KMotion Libraries - GCode Interpreter - Trajectory Planner - Coordinated Motion - .NET

From Dynomotion

Jump to: navigation, search

Building the KMotion Libraries

To build the KMotion Libraries:

  1. Install Microsoft Visual Studio Community 2015 (include MFC option) (Note Microsoft removed VS2015 from their servers so on-line installations will fail, the installation package must be downloaded to your files system and installed from there)
  2. Open BuildAllLibs.sln Solution from root directory of KMotion Installation
  3. Select Release or Debug Configuration as desired
  4. Select Build | Build Solution
  5. new Library .DLLs and .EXEs will reside in the root\KMotion\Release or Debug folder

Upgrading Projects to VS2022

Opening the VS2015 Solution and Projects in VS2022 may require some modifications. In most cases right-click properties on each project and change the Windows SDK Version to 10.0 (latest installed Version) and the Platform Toolset to VS 2022(V143).

TCC67 may have a define conflict error with a definition of int8_t in elf.h. Change:

typedef char int8_t;

to:

typedef signed char int8_t;

There may be a number of depreciated warnings but the code should work ok.

KMotionDLL.dll

C++ Library that facilitates and coordinates all the communication to KFLOP.  It contains the CKMotionDLL class which provides the multi-process, multi-thread, multi-board access to KFLOP using a Client/Server approach.  It also creates and instance of the Coordinated Motion Class and GCode Interpreter Classes.

32/64-bit Libraries and Examples

The Library and Example Solutions/Projects support 4 Configuration|Platforms to build Debug or Release binaries for 32/64-bit code Platforms.


The 4 Configuration|Platforms are:

Debug        |    x86

Release      |    x86

Debug64    |    x64

Release64 |    x64


ConfigurationPlatform.png


Note due to Microsoft legacy issues the Win32 platform and x86 platform are basically equivalent.  x86 should be used where available.  To build a particular Configuration | Platform use the drop downs to select it then select build.  Make sure both are selected properly before building.  Some combinations such as Debug64 | x86 are inappropriate and should not be selected.


Binary Output Files will be will be placed into separate folders with the name of the Configuration under the <>\KMotion\ folder:

ConfigurationFolders.png

Visual Studio Build | Batch Build can be used to build all 4 configurations with one click.  Configurations | Platforms that are appropriate to be built have been selected.  Note Microsoft Visual Studio Batch Build does not support building Visual Basic projects.  If a Visual Basic Project is included in a solution Batch Build will be disabled.  None of the Libraries are Visual Basic based so BuildAllLibs.sln allows Batch Build to be used.  BuildExamples contains several Visual Basic examples so Batch Build will be disabled.  BuildExampleNoVB.sln has Visual Basic Projects removed so Batch Build can be used for the Non-Visual basic examples.


BatchBuild.png



Note various versions of Visual Studio have an issue where Batch Build settings were not being saved so all build settings would need to be re-selected each time after launching Visual Studio.   VS2022 Version 17.7 has this corrected.


Note in some Versions of Visual Studio Batch Build is available but is not displayed on the tool bar or menus.  It can be added using Visual Studio | Tools | Customize feature.  Shown below is the process to add it to the Build Menu.  It can be added to the Build Toolbar in a similar manner.


CustomizeBatchBuild.png


Most of the differences between 32 vs 64-bit applications or libraries involve compiler and linker settings.  The appropriate libraries must be linked together.  32 and 64-bit libraries can not be mixed.  Certain .NET assemblies can be created to support both types (AnyCPU).  However KMotion's Projects are selected to specifically be either x86 or x64.

Regarding Coding for 32 vs 64-bit environments is mostly transparent.  The main difference being that addresses in 64-bit code are 64 bits and in 32-bit code they are 32 bits.  So in C++ pointers use 8 bytes vs 4 bytes of memory.  This is normally automatically handled by compilers so is transparent to the coder.

The C++ int data type is 32-bits in both environments.  A 64-bit integer can be defined as type __int64.

In cases where an address is being saved or passed an an int, in will not fit in a 64-bit environment.  KMotion's C++ libraries include a macro define as HANDLE64 to be of the appropriate type for the environment.  HANDLE64 is defined as:

#if _WIN64
#define HANDLE64 __int64
#else
#define HANDLE64 int
#endif

32-bit code has a program/data range of 2 GBytes. 64-bit systems can theoretically allow up to 16 exabytes (16 billion GB) of RAM. The SimpleFormsCS C# example has an #define option to allocate more than 2GB of data to show that this is possible with a 64-bit program. To enable this change line 1 of Form1.cs to define TEST_BIG_ARRAY

#define TEST_BIG_ARRAY

For the test two Double Arrays of 1/2 billion elements each are allocated from Heap Memory.  For a total of 1 billion elements.  Since Doubles are 8-bytes each this allocates 8GBytes of memory.  Note that Arrays in C# are limited to a 32-bit value number of elements (~2 billion) even in 64-bit environments. 

#if TEST_BIG_ARRAY
        static int ASize = 500000000;
        Double[] Big = new Double[ASize];
        Double[] Big2 = new Double[ASize];
        Double sum = 0;
#endif

This code fills the arrays with their indexes squared.  Then sums all the elements

#if TEST_BIG_ARRAY
            for (uint i = 0; i < ASize; i++) Big[i] = Big2[i] = (double)i * (double)i;
            for (uint i = 0; i < ASize; i++) sum += Big[i] + Big2[i];
#endif

On an older Intel i7 Windows 10 PC with 16GB of RAM this code initially required 40 seconds to execute but reduced to nearly 5 seconds on subsequent runs possibly  because the first run required more memory to be swapped out to Disk to free RAM.


Note for the run-time library to support objects larger than 2 GBytes an app.config file must have the gcAllowVeryLargeObjects enabled property set true.  As shown below.

AllowBigObjects.png


Otherwise a System.OutOfMemoryException may occur when attempting to allocate the object.  Such as the one shown below:

BigArrayExceptionSmaller.png


GCodeInterpreter.dll

C++ Library that facilitates Trajectory Planning, Coordinated Motion, Motion Buffering, and GCode.  It contains the CCoordMotion and CGCodeInterpreter classes.


GCode Actions

The GCode Interpreter has a method of invoking generalized "Actions".  Actions can be invoked from MCodes, S Words, User Defined Buttons, or Special Actions.  The Actions performed can be various operations such as changing the states of one or two I/O Bits, writing a Speed value to a DAC, Invoking a User C Program in KFLOP, Invoking a PC Program, Application Callback, or Executing GCode.


The  MCODE_TYPE defines what type of Action to perform.  It is not related to an MCode Number.   An Action consists of an MCODE_TYPE and several parameters (numeric and a string).  The number and purpose of the parameters vary depending on the MCODE_TYPE.


The Interpreter allows you to configure about 30 MCodes to perform Actions.  Several MCodes such as M0 (stop) and M30 (stop/rewind) have Interpreter functionality, but can also perform an additional Action.  20 MCodes (100-119) are for User use.

The Interpreter's 'S' word can also invoke an Action.

The KMotionCNC Application also uses Actions assigned to custom User Buttons and to "Special Actions" that are executed on operations such as Application Startup.  All Actions are stored in an array and referenced by the index into the array (not necessarily the MCode number).  See the chart below.


KMotionCNC allows the MCode Actions to be configured in its Tool Setup.  See here and here.

From .NET you can configure the Action Array using the KM_Interpreter.SetMcodeAction Method.  Note the Array Index does not necessarily equal the MCode Number.

After the Action Array in the Interpreter is configured any executed MCode (or S) will perform the configured Action.

Note that MCodes can also set GCode Parameters (PQR words) into KFLOP persist variables.  This is described here.

This Forum Thread may also help.

GCode Action Table

The GCode Interpreter contains a Table of Actions:

MCODE_ACTION McodeActions[MAX_MCODE_ACTIONS];

An Action is defined by an action code and several parameters (several numeric and a string parameter).


The Actions are packed into a contiguous array of actions.  When configuring the Actions  from an Application the Array Index is used.  When Invoking the Action the Action # is used.  See the table below for the relationship.

Index Description Action#
0 M0 0
1 M1 1
2 M2 2
3 M3 3
4 M4 4
5 M5 5
6 M6 6
7 M7 7
8 M8 8
9 M9 9
10 S 10
11 User But0 11
12 User But1 12
13 User But2 13
14 User But3 14
15 User But4 15
16 User But5 16
17 User But6 17
18 User But7 18
19 User But8 19
20 User But9 20
21 M100 100
22 M101 101
23 M102 102
24 M103 103
25 M104 104
26 M105 105
27 M106 106
28 M107 107
29 M108 108
30 M109 109
31 M110 110
32 M111 111
33 M112 112
34 M113 113
35 M114 114
36 M115 115
37 M116 116
38 M117 117
39 M118 118
40 M119 119
41 M30 24
42 Cycle Start 25
43 Halt 26
44 Stop 27
45 FeedHold 28
46 Resume 29
47 Prog Start 30
48 ProgExit 31

KMotion_dotNet.dll

The KMotion Libraries are accessible from Programming environments that support .NET (ie C#, VB, Labview, etc.).  The .NET interface allows a single reference to KMotion dotNet.dll to pull in all the available functionality of the library and permit support for things like Auto Complete and type/parameter checking.


On-line html based .NET Library help files are available here.


A Help File Based description of the KMotion dotNet functionality is available here

(note when downloading help files some Operating Systems may require the file to be unblocked in its properties to work fully).  See Below:

Unblock Help File.png