===<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 />}
==Data/Motion Capture with a C Program==
A C Program can be used to capture real-time data to KFLOP's gather buffer, then upload the data to a PC Disk File, and then be analyzed using a PC App such as Microsoft Excel. This can be a very powerful method used to troubleshoot problems such as those involving complex GCode trajectories. See the many Capture examples (Whose names begin with "Capture").
Here is a [https://www.dynomotion.com/wiki/index.php?action=ajax&title=-&rs=SecureFileStore::getFile&f=/4/44/CaptureXYZMotionToFileWithHeader.c CaptureXYZMotionToFileWithHeader.c] example which captures the xyz trajectory:
<pre class="brush:c">#include "KMotionDef.h"
#define N 5000
extern double CS0_TimeExecuted;
main()
{
int i,k;
double X0,Y0,Z0,T0,*p=gather_buffer;
CS0_TimeExecuted=0.0;
while (CS0_TimeExecuted==0.0) ; // wait till we Start
*p++ = 0.0;
X0 = *p++ = ch0->Dest;
Y0 = *p++ = ch1->Dest;
Z0 = *p++ = ch2->Dest;
T0 = Time_sec();
// Capture Data
for (i=0; i<N-1; i++)
{
for (k=0; k<2; k++) WaitNextTimeSlice(); *p++ = Time_sec() - T0; *p++ = ch0->Dest;
*p++ = ch1->Dest;
*p++ = ch2->Dest;
}
p=gather_buffer;
FILE *f=fopen("C:\\temp\\kflopdata.txt","wt");
fprintf(f,"T,X,Y,Z\n"); // Header
for (i=0; i<N; i++)
{
// round times to nearest servo tick
p[0] = ((int)(p[0]/TIMEBASE + 0.5))*TIMEBASE;
fprintf(f,"%16.9f,%16.6f,%16.6f,%16.6f\n",p[0],p[1],p[2],p[3]);
p += 4;
}
fclose(f);
}
</pre>
Note the data is written to the file C:\temp\kflopdata.txt. So the folder C:\temp must exist.
Note double backslashes are used in C Programs to insert a single slash as a slash operates as a means of inserting special characters such as the newline character \n.
N determines how many samples are saved.
The gather_buffer is 8MBytes so is limited to 1 million doubles. This program captures 4 doubles per sample x 5000 samples = 20000 doubles.
The 2 in this line sets the number of Time Slices occur between samples. The number must be 1 or more.
<pre class="brush:c"> for (k=0; k<2; k++) WaitNextTimeSlice(); </pre>
The C Program can be assigned to an MCode so it can be executed from GCode
[[File:M100_Capture.png|none|link=]]
This GCode Executes the C Program then moves in a square
'''M100 (Trigger Capture)'''<br />'''G4 P0.1 (Wait till loaded)'''<br />'''G0 G20 X0 Y0 Z0'''<br />'''F100'''<br />'''G1 X0 Y1'''<br />'''G1 X1 Y1'''<br />'''G1 X1 Y0'''<br />'''G1 X0 Y0'''<br />'''M2'''
This is an example Microsoft Excel spreadsheet to Import (refresh) the data from a file, compute velocities and acceleration from the time/posistions, and chart the data.
[https://www.dynomotion.com/wiki/index.php?action=ajax&title=-&rs=SecureFileStore::getFile&f=/b/b6/PlotCaptureXYZwithHeader.xlsx PlotCaptureXYZwithHeader.xlsx]
Here is an example of charted data
[[File:XY_Vel_plot_example_from_Excel.png|none|link=]]
int PC_comm[N_PC_COMM_PERSIST];// 8 persist Variables constantly uploaded to send misc commands/data to PC
==Threads and KMotion.exec C Programs Screen==