Changes

KFLOP C Programs

3,581 bytes added, 20:06, 30 October 2015
Added Persist and Gather sections
===<span id="Simple_DiskReadWrite.c_example" class="mw-headline">Simple DiskReadWrite.c example</span>===
include "KMotionDef.h"<br /><br />main()<br />{<br />    FILE *f;<br />    char s[256];<br />    double a=123.456,b=999.999,c=0.001;<br />    double x=0,y=0,z=0;<br />    int result;<br />    <br />    // write 3 comma separated values to a disk file<br />    f=fopen("c:\\Temp\\KFlopData.txt","wt");<br />    fprintf(s,"%f,%f,%f\n",a,b,c);<br />    fclose(f);<br />    <br />    // read them back in<br />    f=fopen("c:\\Temp\\KFlopData.txt","rt");<br />    if (!f)<br />    {<br />        printf("Unable to open file\n");<br />        return;<br />    }<br />    <br />    // read a line and convert 3 doubles<br />    result=fscanf(f,"%lf,%lf,%lf",&x,&y,&z);<br />    fclose(f);<br />    <br />    printf("# values converted = %d, x=%f, y=%f, z=%f\n",result,x,y,z);<br />}
 
 
 
==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)<br />{<br />    double d;<br />    ((int*)(&d))[0] = persist.UserData[i*2];<br />    ((int*)(&d))[1] = persist.UserData[i*2+1];<br />    return d;<br />}<br /><br />void SetUserDataDouble(int i, double v)<br />{<br />    double d=v;<br />    persist.UserData[i*2]   = ((int*)(&d))[0];<br />    persist.UserData[i*2+1] = ((int*)(&d))[1] ;<br />}
 
 
 
The PC can also access these variables with Console Script Commands:  [http://www.dynomotion.com/Help/Cmd.htm#SetPersistDec SetPersistDec],  [http://www.dynomotion.com/Help/Cmd.htm#GetPersistDec GetPersistDec],  [http://www.dynomotion.com/Help/Cmd.htm#SetPersistHex SetPersistHex],  [http://www.dynomotion.com/Help/Cmd.htm#GetPersistHex 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<br />#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
 
 
 
==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).                   <br />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: 
 
[http://www.dynomotion.com/Help/Cmd.htm#GetGatherDec GetGatherDec]  [http://www.dynomotion.com/Help/Cmd.htm#SetGatherDec SetGatherDec]  [http://www.dynomotion.com/Help/Cmd.htm#GetGatherHex GetGatherHex]  [http://www.dynomotion.com/Help/Cmd.htm#SetGatherHex SetGatherHex]
Bureaucrat, administrator
469
edits