Difference between revisions of "KFLOP C Programs"
From Dynomotion
(→C Programming References) |
(C Program Size and Stack Limitations) |
||
Line 26: | Line 26: | ||
<span class="mw-headline">For general C programming references/tutorials, s</span>ee the links listed near the bottom of [http://dynomotion.com/Support.html this page]. | <span class="mw-headline">For general C programming references/tutorials, s</span>ee the links listed near the bottom of [http://dynomotion.com/Support.html this page]. | ||
− | For KFlop specific references, all available variables and functions are listed in the KMotionDef.h file, located within the DSP_FLOP directory at | + | For KFlop specific references, all available variables and functions are listed in the KMotionDef.h file, located within the DSP_FLOP directory at \DSP_KFLOP. An index of the available variables and functions can be found in the [[KMotionDef]] page. |
===<span id="Simplest_C_Program" class="mw-headline">Simplest C Program</span>=== | ===<span id="Simplest_C_Program" class="mw-headline">Simplest C Program</span>=== | ||
Line 92: | Line 92: | ||
int PC_comm[N_PC_COMM_PERSIST];// 8 persist Variables constantly uploaded to send misc commands/data to PC | int PC_comm[N_PC_COMM_PERSIST];// 8 persist Variables constantly uploaded to send misc commands/data to PC | ||
+ | |||
+ | |||
+ | |||
+ | ==C Program Size and Stack Limitations== | ||
+ | Each of the first 6 User Thread Memory Spaces are limited to 64KBytes (65536 Bytes). Thread 7 is larger and limited to 5x64KBytes (327680Bytes). | ||
+ | |||
+ | If subsequent Threads are unused it is possible to overflow into their Thread Spaces. | ||
+ | |||
+ | |||
+ | When compiling a message is displayed showing the Memory Usage. The total value indicates the total amount of memory used and must fit into the allowed memory space. | ||
+ | |||
+ | No Errors, No Warnings, text=100, bss=0, data=14, total=160 | ||
+ | |||
+ | |||
+ | Each User Thread Stack is located in precious single cycle (5ns) internal memory and is 2 KBytes in length. Care should be used to limit stack usage. Overflowing the stack will likely cause KFLOP to crash. Local variables (defined within functions) and passed function parameters reside on the stack. Large variables should be defined as Global Variables or in the 8 MByte [http://www.dynomotion.com/wiki/index.php?title=KFLOP_C_Programs&action=submit#Gather_Buffer Gather Buffer] | ||
Revision as of 15:07, 5 March 2016
C Programs provide a powerful and flexible capability to perform almost any sequence of operations within KFLOP. In most cases after you have tested and tuned all your hardware using the KMotion.exe setup program all the settings and initialization steps required for your system can be placed into an Initialization C Program so that your system can be fully initialized simply by executing the Initialization program. Using a C Program offers full flexibility to initialize your system however and in whatever order you wish. In most common cases an existing example can be used with simple modification of values specific to your system. The KMotion.exe Setup program has some automatic capability to translate the Axes Screen Values that were determined by you during configuration and testing into C Code.
Contents
Initialization C Program
The Initialization C Program will normally perform operations of the following type:
- Enable/define any Option boards present (ie. KStepPresent=TRUE; // enable KSTEP input multiplexing)
- The setting of Axes parameters (ie. ch0->Accel=200000; )
- Enable Axes (ie.EnableAxisDest(0,0); )
- Define the Coordinated Motion Axes (ie. DefineCoordSystem(0,1,2,-1); // define axes for XYZ
- Forever Loop to service any continuous requirements such as MPG, External Buttons, EStop, etc
Adding the configuration for a new Axis to your Initialization C Program:
- Configure the Channel in KMotion.exe and verify that after Pushing "Enable" (which downloads and enables) it all works properly
- On the Config/Flash Screen Push the "C Code To Clip Board" Button
- Open your Initialization Program in the C Programs Screen
- Position the cursor after the previous Axis settings
- Right Mouse Click "Paste"
- If desired also enable the axis by inserting EnableAxisDest(xx,0); where xx is the Axis number
- Save the file
- Test after a power cycle if the C Program initializes all Axes Properly
Programming References
For general C programming references/tutorials, see the links listed near the bottom of this page.
For KFlop specific references, all available variables and functions are listed in the KMotionDef.h file, located within the DSP_FLOP directory at \DSP_KFLOP. An index of the available variables and functions can be found in the KMotionDef page.
Simplest C Program
It is important to understand that any KFLOP C Program consists of a minimum number of parts as shown below.
The first part is an #include statement which includes a
definition file that defines all the functionality available in KFLOP. The KMotionDef.h file is included with every installation to define all the functions, variables, structures, and defines available in that Version. It is located in the DSP_KFLOP subdirectory of the installation. Open it with the KMotion.exe C Programs Screen to see what's available.
The next part is the "main" function. This is where execution of the program will actually begin.
The last part is the code that is to be executed and belongs to the main function. Curly brackets { } define the beginning and end of the function. This example contains only one print statement to be executed. Note how the code within the curly brackets is indented (using a tab or spaces) to show that it belongs to the main function block. This indentation is not required but helps readers see the program structure. Instructions must end with a semicolon ';'. Double forward slashes allow comments to be added at the end of an instruction.
include "KMotionDef.h"
main()
{
printf("Hello World!\n"); // send message to console
}
Basic Disk Read/Write
KFLOP has no file system on its own, but when connected to a PC with a PC App running it can do basic Disk Read or Write Operation. Read capability was recently added in Version 4.33q. There isn't any PC Keyboard access (getchar()). See the KmotionDef.h file for supported functions:
// Note: standard C language printf
int printf(const char *format, ...); // Print formatted string to console
int sprintf(char *s, const char *format, ...); // Print formatted string to string
typedef int FILE;
FILE *fopen(const char*, const char*); // Open a text file for writing on the PC (2nd param = "rt" or "wt")
int fprintf(FILE *f, const char * format, ...); // Print formatted string to the PC's Disk File
int fclose(FILE *f); // Close the disk file on the PC
int Print(char *s); // Print a string to the console window
int PrintFloat(char *Format, double v); // Print a double using printf format, ex "%8.3f\n"
int PrintInt(char *Format, int v); // Print an integer using printf format, ex "result=%4d\n"
int sscanf(const char *_str, const char *_fmt, ...); //scan string and convert to values
#define MAX_READ_DISK_LENGTH 1024 // max allowed length of disk file line length
extern volatile int read_disk_buffer_status; //status of read disk buffer 1=line available, 2=error, 3=eof
extern char read_disk_buffer[MAX_READ_DISK_LENGTH+1];
char *fgets(char *str, int n, FILE *file); //read string from PC disk file, str=buffer, n=buffer length, f=FILE pointer, returns NULL on error
int fscanf(FILE *f, const char *format, ...); //read sting from PC Disk file, convert values, returns number of items converted
int feof(FILE *f); // End of file status for disk reading
Simple DiskReadWrite.c example
include "KMotionDef.h"
main()
{
FILE *f;
char s[256];
double a=123.456,b=999.999,c=0.001;
double x=0,y=0,z=0;
int result;
// write 3 comma separated values to a disk file
f=fopen("c:\\Temp\\KFlopData.txt","wt");
fprintf(s,"%f,%f,%f\n",a,b,c);
fclose(f);
// read them back in
f=fopen("c:\\Temp\\KFlopData.txt","rt");
if (!f)
{
printf("Unable to open file\n");
return;
}
// read a line and convert 3 doubles
result=fscanf(f,"%lf,%lf,%lf",&x,&y,&z);
fclose(f);
printf("# values converted = %d, x=%f, y=%f, z=%f\n",result,x,y,z);
}
Global Persist Variables
KFLOP contains a global array of 200 32-bit integer variables that can be used to save values from one program execution to the next or to share values between Threads or Programs. The values default to zero on KFLOP Power up but if the variables are changed and Flash User Data is performed to KFLOP the values will persist after the next power cycle. From C the values can be accessed as (where xxx is a number 0-199):
persist.UserData[xxx]
32-bit Floating point variables can be read or written to the variables using casting. The technique to avoid a compiler conversion to integer is to take the address of the variable, cast it to a pointer to an integer, then de-reference the pointer. The C syntax is:
persist.UserData[xxx] = *(int *) & my_float;
my_float = *(float *) &persist.UserData[xxx];
64-bit Floating point variables can be read or written to a pair of UserData Variables. Using these functions (KflopToKMotionCNCFunctions.c)
double GetUserDataDouble(int i)
{
double d;
((int*)(&d))[0] = persist.UserData[i*2];
((int*)(&d))[1] = persist.UserData[i*2+1];
return d;
}
void SetUserDataDouble(int i, double v)
{
double d=v;
persist.UserData[i*2] = ((int*)(&d))[0];
persist.UserData[i*2+1] = ((int*)(&d))[1] ;
}
The PC can also access these variables with Console Script Commands: SetPersistDec, GetPersistDec, SetPersistHex, GetPersistHex.
For easy and fast access several persist variables are uploaded in the KFLOP Main Status Record as defined below in PC-DSP.h. Certain PC Applications like KMotionCNC make use of these to receive commands from KFLOP to perform various actions. The supported command codes are defined in PC-DSP.h
define PC_COMM_PERSIST 100 // First Persist Variable that is uploaded in status
#define N_PC_COMM_PERSIST 8 // Number of Persist Variables that are uploaded in status
The member variable of Main Status:
int PC_comm[N_PC_COMM_PERSIST];// 8 persist Variables constantly uploaded to send misc commands/data to PC
C Program Size and Stack Limitations
Each of the first 6 User Thread Memory Spaces are limited to 64KBytes (65536 Bytes). Thread 7 is larger and limited to 5x64KBytes (327680Bytes).
If subsequent Threads are unused it is possible to overflow into their Thread Spaces.
When compiling a message is displayed showing the Memory Usage. The total value indicates the total amount of memory used and must fit into the allowed memory space.
No Errors, No Warnings, text=100, bss=0, data=14, total=160
Each User Thread Stack is located in precious single cycle (5ns) internal memory and is 2 KBytes in length. Care should be used to limit stack usage. Overflowing the stack will likely cause KFLOP to crash. Local variables (defined within functions) and passed function parameters reside on the stack. Large variables should be defined as Global Variables or in the 8 MByte Gather Buffer
Gather Buffer
KFLOP contains a relatively large global array (8MBytes) of 1 million double precision floating point values that can be used by C Programs. It is often used for capturing/gathering data so it is named the gather_buffer. KMotionCNC Step Response Screen uses this memory. But the memory can be used for any purpose.
The Gather Buffer is defined in KMotionDef.h as:
define MAX_GATHER_DATA 1000000 // Size of gather buffer (number of doubles, 8 bytes each).
extern double *gather_buffer; // Large buffer for data gathering, Bode plots, or User use
C access syntax is gather_buffer[xxx] where xxx is in the range 0 ... 999999.
The gather_buffer pointer can be cast as other types to make use of the memory. ie:
int *gather_buffer_int = (int *)gather_buffer;
The PC can also access the gather buffer with Console Script Commands: