Subversion Repositories f9daq

Compare Revisions

Ignore whitespace Rev 355 → Rev 356

/praktikum/fmf-fft-p5/motor.c
13,11 → 13,13
//stepping motor busheng --> declaration 1step=1.8degrees
//experimantally: full rotation = 3200 steps (on setting 1step/mm ($100))
 
 
// PORTS
int pCOM1 = 6;
int pCOM2 = 7;
char GRBL[10] = "COM8"; //BE CAREFUL, IT'S IMPORTANT TO KNOW THE NAME OF THE PORTS
char ADC[10] = "COM7";
 
int run, graph, f_t, ji, vi, run1;
int run, graph, chart, ji, vi, run1;
int left_1, right_1;
 
static int panel, tfID, tfID1;
38,7 → 40,7
void initialize()
{
// Open grbl serial port //("COM6",115200)
OpenComConfig (pCOM1, "COM6", 115200, 0, 8, 1, 512, 512);
OpenComConfig (pCOM1, GRBL, 115200, 0, 8, 1, 512, 512);
 
/* Turn off Hardware handshaking (loopback test will not function with it on) */
SetCTSMode (pCOM1, LWRS_HWHANDSHAKE_OFF);
121,8 → 123,8
 
void initialize_voltage()
{
// Open serial port for photodetector - reading is on chanel A1
OpenComConfig (pCOM2, "COM7", 115200, 0, 8, 1, 512, 512);
// Open serial port for ADC & photodetector - reading is on chanel A1
OpenComConfig (pCOM2, ADC, 115200, 0, 8, 1, 512, 512);
SetCTSMode (pCOM2, LWRS_HWHANDSHAKE_OFF);
 
Delay(0.5);
295,7 → 297,6
graph = PlotY (panel, p1_VOLTAGE_GRAPH, &volt_g[0], num_points, VAL_DOUBLE,
VAL_CONNECTED_POINTS, VAL_SIMPLE_DOT, VAL_SOLID, 1, VAL_WHITE);
ji = num_points;
f_t = 1;
}
 
FlushInQ (pCOM1);
310,46 → 311,24
 
static int CVICALLBACK voltage_run(void *functionData)
{
int fix_range;
double ymin,ymax,xmin,xmax;
double voltA,voltB=0;
int time = 0;
//double volt1[TEXT_LENGTH];
double voltA;
 
initialize_voltage();
 
int chart = NewCtrl (panel, CTRL_GRAPH_LS, "RUN VOLTAGE", 100, 15);
chart = NewCtrl (panel, CTRL_STRIP_CHART_LS, "RUN VOLTAGE", 100, 15);
SetCtrlAttribute(panel, chart, ATTR_HEIGHT, 600);
SetCtrlAttribute(panel, chart, ATTR_WIDTH , 1300);
SetAxisRange(panel, chart, VAL_NO_CHANGE, 0, 0, VAL_AUTOSCALE, 0, 0);
 
while(1)
{
// Range
GetCtrlVal(panel, p1_RANGE, &fix_range);
if(fix_range)
{
GetAxisRange (panel, chart, VAL_MANUAL, &xmin, &xmax, VAL_MANUAL, &ymin, &ymax);
if (ymax<=2) SetAxisRange (panel, chart, VAL_NO_CHANGE, 0, 0, VAL_MANUAL, 0, 2);
else SetAxisRange (panel, chart, VAL_NO_CHANGE, 0, 0, VAL_MANUAL, 0, 5);
}
else SetAxisRange (panel, chart, VAL_NO_CHANGE, 0, 0, VAL_AUTOSCALE, 0, 0);
 
//volt1[time] = read_voltage();
voltA = read_voltage();
PlotStripChartPoint (panel, chart, voltA);
 
PlotPoint(panel, chart, time, voltA, VAL_SOLID_SQUARE, VAL_WHITE);
if (time>0) PlotLine(panel, chart, time, voltA, time-1, voltB, VAL_WHITE);
if (time>50) SetAxisRange (panel, chart, VAL_MANUAL, time-50, time, VAL_NO_CHANGE, 0, 0);
 
SetCtrlVal(panel, p1_VOLTAGE, voltA);
 
Delay(0.05);
time++;
 
voltB = voltA;
 
//volt1[time] = voltA;
 
FlushInQ (pCOM2);
 
if (vi == 0) break;
356,13 → 335,8
}
 
DiscardCtrl (panel, chart);
chart = 0;
 
// After some time (~4000 points) it becomes slower in writing out data...
 
//if (graph!=0) DeleteGraphPlot (panel, p1_VOLTAGE_GRAPH, graph, VAL_DELAYED_DRAW);
//graph = PlotY (panel, p1_VOLTAGE_GRAPH, &volt1[0], time+1, VAL_DOUBLE,
// VAL_CONNECTED_POINTS, VAL_SIMPLE_DOT, VAL_SOLID, 1, VAL_WHITE);
 
return 0;
}
 
394,7 → 368,7
bytes_read0 = ComRd (pCOM1, read_data0, strLen);
//printf(" : %s", read_data0);
 
printf("Stop run1\n");
//printf("Stop run1\n");
 
break;
}
492,6 → 466,7
const int ii = ji;
double fourier_t[ii], fourier_ti[ii];
char window_fun;
double voltmax=0, voltmin=5, voltavg;
 
switch (event)
{
501,55 → 476,70
{
fourier_t[i] = volt_g[i];
fourier_ti[i] = 0;
if (fourier_t[i]>voltmax) voltmax = fourier_t[i];
if (fourier_t[i]<voltmin) voltmin = fourier_t[i];
}
 
voltavg = (voltmin+voltmax)/2.0;
GetCtrlVal(panel, p1_WIN_FUN, &window_fun);
//printf("window_fun = %d\n", window_fun);
//printf("max %f, min %f, avg %f\n", voltmax, voltmin, voltavg);
if (window_fun!=0)
{
//do something with window functions
if(window_fun==1) for(int i=0; i<ii; i++) fourier_t[i] = (2*fourier_t[i]);
if(window_fun==2) for(int i=0; i<ii; i++) fourier_t[i] = sin(fourier_t[i]);
YGraphPopup ("HOW volt_g LOOKS NOW", fourier_t, ii, VAL_DOUBLE);
//do something with window functions (N=ii)
if(window_fun==1) for(int i=0; i<ii; i++) fourier_t[i] = (fourier_t[i] - voltavg)*(1 - fabs((i-(ii/2.0))/(ii/2))) + voltavg; //Bartlett
if(window_fun==2) for(int i=0; i<ii; i++) fourier_t[i] = (fourier_t[i] - voltavg)*(1 - ((i-(ii/2.0))/(ii/2))*((i-(ii/2.0))/(ii/2))) + voltavg; //Welch
if(window_fun==3) for(int i=0; i<ii; i++) fourier_t[i] = (fourier_t[i] - voltavg)*(0.5*(1 - cos(2*3.14159265359*i/ii))) + voltavg; //Hann
YGraphPopup ("HOW voltage LOOKS NOW", fourier_t, ii, VAL_DOUBLE);
}
 
FFT (fourier_t, fourier_ti, ii);
for (int i=0; i<ii; i++) fourier_t[i] = fabs(fourier_t[i]);
fourier_t[0]=0;
 
YGraphPopup ("FOURIER", fourier_t, ii/2+1, VAL_DOUBLE);
 
if (f_t == 1)
{
FILE *fp,*fp1;
FILE *fp,*fp1;
 
//file name has to have file extension
char fname[50];
GetCtrlVal(panel, p1_F_NAME, fname);
fp = fopen(fname, "w");
//file name has to have file extension
char fname[50];
GetCtrlVal(panel, p1_F_NAME, fname);
char fname0[100] = "C:/Users/";
char *name;
name = getenv("USERNAME");
strcat(fname0,name);
strcat(fname0,"/Desktop/");
//char fname0[100] = "C:/Users/Student/Desktop/"; //Alternative way
strcat(fname0,fname);
fp = fopen(fname0, "w");
 
char fname1[50];
GetCtrlVal(panel, p1_F_NAME1, fname1);
fp1 = fopen(fname1, "w");
char fname1[50];
GetCtrlVal(panel, p1_F_NAME1, fname1);
char fname10[100] = "C:/Users/";
strcat(fname10,name);
strcat(fname10,"/Desktop/");
strcat(fname10,fname1);
fp1 = fopen(fname10, "w");
 
fprintf(fp, "step\tvolt_g\n");
for(int i=0; i<ii; i++)
{
fprintf(fp, "%d\t%lf\n", i+1, volt_g[i]);
}
fprintf(fp, "step\tvoltage\n");
for(int i=0; i<ii; i++)
{
fprintf(fp, "%d\t%lf\n", i+1, volt_g[i]);
}
 
fprintf(fp1, "step\tfourier_t\n");
for(int i=0; i<ii/2+1; i++)
{
fprintf(fp1, "%d\t%lf\n", i, fourier_t[i]); //0th component is from DC - background?
}
 
fclose(fp);
fclose(fp1);
printf("Files %s and %s created.\n", fname, fname1);
fprintf(fp1, "step\tfourier\n");
for(int i=0; i<ii/2+1; i++)
{
fprintf(fp1, "%d\t%lf\n", i, fourier_t[i]); //0th component is from DC - background? YES
}
 
f_t = 0; //so that we make only one file
fclose(fp);
fclose(fp1);
printf("Files %s and %s created.\n", fname, fname1);
 
break;
 
}
565,6 → 555,7
double ret[ki], ret1[ki];
double val = 3.14159265/180;
int x1, x2, a1, a2;
char window_fun;
 
switch (event)
{
575,11 → 566,21
GetCtrlVal(panel, p1_AMPL1, &a1);
GetCtrlVal(panel, p1_AMPL2, &a2);
 
GetCtrlVal(panel, p1_WIN_FUN, &window_fun);
//printf("window_fun = %d\n", window_fun);
for (int i=0; i<ki; i++)
{
ret[i] = a1*sin(x1*i*val)+a2*sin(x2*i*val);
ret1[i] = 0;
}
if (window_fun!=0)
{
//do something with window functions (N=ki)
if(window_fun==1) for(int i=0; i<ki; i++) ret[i] = ret[i]*(1 - fabs((i-(ki/2.0))/(ki/2))); //Bartlett
if(window_fun==2) for(int i=0; i<ki; i++) ret[i] = ret[i]*(1 - ((i-(ki/2.0))/(ki/2))*((i-(ki/2.0))/(ki/2))); //Welch
if(window_fun==3) for(int i=0; i<ki; i++) ret[i] = ret[i]*(0.5*(1 - cos(2*3.14159265359*i/ki))); //Hann
}
YGraphPopup ("example graph", ret, ki, VAL_DOUBLE);
FFT (ret, ret1, ki);
for (int i=0; i<ki; i++) ret[i] = fabs(ret[i]);
634,6 → 635,18
case EVENT_COMMIT:
 
GetCtrlVal(panel, p1_RANGE, &fix_range);
if(chart)
{
if(fix_range)
{
GetAxisRange (panel, chart, VAL_MANUAL, &xmin, &xmax, VAL_MANUAL, &ymin, &ymax);
if (ymax<=2) SetAxisRange (panel, chart, VAL_NO_CHANGE, 0, 0, VAL_MANUAL, 0, 2);
else SetAxisRange (panel, chart, VAL_NO_CHANGE, 0, 0, VAL_MANUAL, 0, 5);
}
else SetAxisRange(panel, chart, VAL_NO_CHANGE, 0, 0, VAL_AUTOSCALE, 0, 0);
}
if(fix_range)
{
GetAxisRange (panel, p1_VOLTAGE_GRAPH, VAL_MANUAL, &xmin, &xmax, VAL_MANUAL, &ymin, &ymax);
704,7 → 717,24
if (run == 1) break;
if (run1 == 0) break;
run1 = 0;
if (run1 == 0) //Additionally here just to be sure
{
ComWrt (pCOM1, "!\n", 2); //stops grbl
 
Delay(0.01);
 
ComWrt (pCOM1, "~\n", 2); //releases grbl
 
Delay(0.01);
 
int strLen = GetInQLen (pCOM1);
bytes_read0 = ComRd (pCOM1, read_data0, strLen);
//printf(" : %s", read_data0);
 
printf("Stop run1\n");
}
 
CmtWaitForThreadPoolFunctionCompletion (poolHandle, tfID1,
OPT_TP_PROCESS_EVENTS_WHILE_WAITING);
CmtReleaseThreadPoolFunctionID (poolHandle, tfID1);
/praktikum/fmf-fft-p5/motor.cws
13,11 → 13,11
Active Project = 1
Project 0001 = "motor.prj"
Drag Bar Left = 320
Window Top = 75
Window Left = 601
Window Bottom = 881
Window Right = 1849
Maximized = True
Window Top = 122
Window Left = 642
Window Bottom = 928
Window Right = 1890
Maximized = False
Maximized Children = True
Max32 Number Of Errors = 20
Track Include File Dependencies = True
40,7 → 40,7
Version = 1302
Don't Update DistKit = False
Platform Code = 4
Build Configuration = "Debug"
Build Configuration = "Release"
Warn User If Debugging Release = 1
Batch Build Release = False
Batch Build Debug = False
48,16 → 48,16
[File 0001]
Path = "/c/home/Fourier/grbl-master/praktikum_fft/motor.c"
File Type = "CSource"
Disk Date = 3677835890
Disk Date = 3680605378
In Projects = "1,"
Window Top = 31
Window Z-Order = 1
Source Window State = "1,575,575,575,33,37,37,0,0,173,0,583,0,0,0,47,675,0,579,45,400,600,1,0,"
Source Window State = "1,126,126,126,26,32,32,0,0,98,0,498,0,0,0,33,3,0,13,83,400,600,1,18,"
 
[File 0002]
Path = "/c/home/Fourier/grbl-master/praktikum_fft/motor.uir"
File Type = "User Interface Resource"
Disk Date = 3677835891
Disk Date = 3680602734
In Projects = "1,"
Window Top = 126
Window Left = 58
68,12 → 68,12
[File 0003]
Path = "/c/home/Fourier/grbl-master/praktikum_fft/motor.h"
File Type = "Include"
Disk Date = 3677835891
Disk Date = 3680602699
In Projects = ""
Window Top = 33
Window Left = 10
Window Z-Order = 3
Source Window State = "1,33,33,33,9,22,22,0,3,80,0,0,0,0,0,21,23,0,33,9,363,768,1,0,"
Source Window State = "1,33,33,33,9,22,22,0,3,80,0,23,0,0,0,21,23,0,33,9,363,768,1,11,"
 
[Tab Order]
Tab 0001 = "/c/home/Fourier/grbl-master/praktikum_fft/motor.c"
168,3 → 168,8
Working Directory = ""
Environment Options = ""
 
[Distributions]
Active Index = 1
AbsCDSPath 0001 = "/c/home/Fourier/grbl-master/praktikum_fft/Motor_1.cds"
RelCDSPath 0001 = "Motor_1.cds"
 
/praktikum/fmf-fft-p5/motor.h
34,8 → 34,8
#define p1_AMPL1 19 /* control type: numeric, callback function: (none) */
#define p1_FREQ1 20 /* control type: numeric, callback function: (none) */
#define p1_VOLT_GRAPH 21 /* control type: textButton, callback function: Only_voltageCB */
#define p1_RANGE 22 /* control type: radioButton, callback function: RangeCB */
#define p1_POS0 23 /* control type: command, callback function: Pos0CB */
#define p1_POS0 22 /* control type: command, callback function: Pos0CB */
#define p1_RANGE 23 /* control type: radioButton, callback function: RangeCB */
 
 
/* Control Arrays: */
/praktikum/fmf-fft-p5/motor.uir
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream