Rev 354 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
354 | f9daq | 1 | #include <cvirte.h> |
2 | #include <userint.h> |
||
3 | #include <utility.h> |
||
4 | #include <ansi_c.h> |
||
5 | #include <rs232.h> |
||
6 | #include <string.h> |
||
7 | #include <stdio.h> |
||
8 | #include <stdlib.h> |
||
9 | #include "motor.h" |
||
10 | #include <analysis.h> |
||
11 | #include <math.h> |
||
12 | |||
13 | //stepping motor busheng --> declaration 1step=1.8degrees |
||
14 | //experimantally: full rotation = 3200 steps (on setting 1step/mm ($100)) |
||
15 | |||
356 | f9daq | 16 | // PORTS |
354 | f9daq | 17 | int pCOM1 = 6; |
18 | int pCOM2 = 7; |
||
356 | f9daq | 19 | char GRBL[10] = "COM8"; //BE CAREFUL, IT'S IMPORTANT TO KNOW THE NAME OF THE PORTS |
20 | char ADC[10] = "COM7"; |
||
354 | f9daq | 21 | |
356 | f9daq | 22 | int run, graph, chart, ji, vi, run1; |
354 | f9daq | 23 | int left_1, right_1; |
24 | |||
25 | static int panel, tfID, tfID1; |
||
26 | static int poolHandle = 0; |
||
27 | |||
28 | #define TEXT_LENGTH 2000 |
||
29 | #define MAX_THREADS 10 |
||
30 | |||
31 | double volt_g[TEXT_LENGTH], volt_g1[TEXT_LENGTH*200]; |
||
32 | |||
33 | int bytes_read0; |
||
34 | int bytes_read1; |
||
35 | |||
36 | char read_data0[TEXT_LENGTH]; |
||
37 | char read_data1[TEXT_LENGTH]; |
||
38 | |||
39 | |||
40 | void initialize() |
||
41 | { |
||
42 | // Open grbl serial port //("COM6",115200) |
||
356 | f9daq | 43 | OpenComConfig (pCOM1, GRBL, 115200, 0, 8, 1, 512, 512); |
354 | f9daq | 44 | |
45 | /* Turn off Hardware handshaking (loopback test will not function with it on) */ |
||
46 | SetCTSMode (pCOM1, LWRS_HWHANDSHAKE_OFF); |
||
47 | |||
48 | ComWrt (pCOM1, "\r\n\r\n", 5); //Writing on Port |
||
49 | Delay(2); //Wait for grbl to initialize |
||
50 | |||
51 | int strLen0 = GetInQLen (pCOM1); //Reading from Port |
||
52 | bytes_read0 = ComRd (pCOM1, read_data0, strLen0); |
||
53 | printf("%s\n", read_data0); |
||
54 | |||
55 | //Left in this form - can have more initializing commands if needeed |
||
56 | char init[1][10] = {"$RST=$\n"};//, "$100=20\n","$$\n","$100=100\n" |
||
57 | char *cmd; |
||
58 | |||
59 | for(int i=0; i<1; i++) |
||
60 | { |
||
61 | cmd = init + i; //Takes init[i] |
||
62 | int len = strlen(cmd); |
||
63 | ComWrt (pCOM1, cmd, len); |
||
64 | printf("%s", cmd); |
||
65 | |||
66 | Delay(0.01); //Give it a little time to respond... |
||
67 | |||
68 | int strLen1 = GetInQLen (pCOM1); |
||
69 | bytes_read1 = ComRd (pCOM1, read_data1, strLen1); |
||
70 | printf(" : %s\n", read_data1); |
||
71 | //printf("bytes_read = %d\n\n", bytes_read1); |
||
72 | } |
||
73 | |||
74 | FlushOutQ (pCOM1); |
||
75 | |||
76 | } |
||
77 | |||
78 | |||
79 | void asking_position() |
||
80 | { |
||
81 | while (1) |
||
82 | { |
||
83 | FlushInQ (pCOM1); |
||
84 | FlushOutQ (pCOM1); |
||
85 | |||
86 | ComWrt (pCOM1, "?\n", 2); //asks grbl his position |
||
87 | Delay(0.01); |
||
88 | |||
89 | int strLen0 = GetInQLen (pCOM1); |
||
90 | bytes_read0 = ComRd (pCOM1, read_data0, strLen0); |
||
91 | //printf(" : %s", read_data0); |
||
92 | //printf("bytes_read = %d\n\n", bytes_read0); |
||
93 | |||
94 | //taking subtring of a string to first appernace of character |
||
95 | char *read_data_0 = strtok(read_data0, "\n"); |
||
96 | //printf(" : %s\n", read_data_0); |
||
97 | |||
98 | //finding substring in string |
||
99 | char *substr = strstr(read_data_0,"Idle"); |
||
100 | //printf("\n%s\n", substr); |
||
101 | |||
102 | if(substr>0) break; |
||
103 | |||
104 | if (!run) |
||
105 | { |
||
106 | ComWrt (pCOM1, "!\n", 2); //stops grbl |
||
107 | |||
108 | Delay(0.01); |
||
109 | |||
110 | ComWrt (pCOM1, "~\n", 2); //releases grbl |
||
111 | |||
112 | Delay(0.01); |
||
113 | |||
114 | int strLen = GetInQLen (pCOM1); |
||
115 | bytes_read0 = ComRd (pCOM1, read_data0, strLen); |
||
116 | //printf(" : %s", read_data0); |
||
117 | |||
118 | break; |
||
119 | } |
||
120 | } |
||
121 | } |
||
122 | |||
123 | |||
124 | void initialize_voltage() |
||
125 | { |
||
356 | f9daq | 126 | // Open serial port for ADC & photodetector - reading is on chanel A1 |
127 | OpenComConfig (pCOM2, ADC, 115200, 0, 8, 1, 512, 512); |
||
354 | f9daq | 128 | SetCTSMode (pCOM2, LWRS_HWHANDSHAKE_OFF); |
129 | |||
130 | Delay(0.5); |
||
131 | FlushInQ (pCOM2); |
||
132 | FlushOutQ (pCOM2); |
||
133 | |||
134 | } |
||
135 | |||
136 | |||
137 | int bytes_read2; |
||
138 | char read_data2[TEXT_LENGTH]; |
||
139 | |||
140 | double read_voltage() |
||
141 | { |
||
142 | |||
143 | //int strLen2 = GetInQLen (pCOM2); |
||
144 | bytes_read2 = ComRdTerm (pCOM2, read_data2, 11, 10); |
||
145 | //IMPORTANT: 11 bytes is the size of one line |
||
146 | printf("Voltage: \n%s\n", read_data2); |
||
147 | //printf("bytes_read = %d\n\n", bytes_read2); |
||
148 | //Delay(0.1); |
||
149 | |||
150 | double Volt; |
||
151 | sscanf(read_data2, "%lf", &Volt); |
||
152 | //printf("Volt = %lf\n", Volt); |
||
153 | return Volt; |
||
154 | } |
||
155 | |||
156 | |||
157 | int bytes_read3; |
||
158 | char read_data3[TEXT_LENGTH]; |
||
159 | |||
160 | void movement(int steps) |
||
161 | { |
||
162 | |||
163 | //Move on the steps |
||
164 | char str[10], move[20] = "$J=X"; |
||
165 | sprintf(str,"%d",steps); |
||
166 | strcat(move,str); |
||
167 | strcat(move,"F600\n"); |
||
168 | //Command must look like "X200\n" or "$J=X200F600" |
||
169 | int len0 = strlen(move); |
||
170 | //printf("len0 is %d ", len0); |
||
171 | printf("Moving on point %d\n", steps); |
||
172 | |||
173 | ComWrt (pCOM1, move, len0); |
||
174 | Delay(0.01); |
||
175 | |||
176 | int strLen3 = GetInQLen (pCOM1); |
||
177 | bytes_read3 = ComRd (pCOM1, read_data3, strLen3); |
||
178 | //printf(" :\n%s", read_data3); |
||
179 | //printf("bytes_read = %d\n\n", bytes_read3); |
||
180 | FlushInQ (pCOM1); |
||
181 | |||
182 | } |
||
183 | |||
184 | |||
185 | static int CVICALLBACK daq_run(void *functionData) |
||
186 | { |
||
187 | |||
188 | int num_points; |
||
189 | int waittime; |
||
190 | double volt; |
||
191 | int j = 0; //for graph |
||
192 | |||
193 | GetCtrlVal(panel, p1_NUM_POINTS, &num_points); |
||
194 | GetCtrlVal(panel, p1_WAIT_TIME, &waittime); |
||
195 | //double wait = waittime/1000.0; //Wait time between each step |
||
196 | //printf("wait: %lf", wait); |
||
197 | |||
198 | movement(0); |
||
199 | Delay(0.1); |
||
200 | |||
201 | asking_position(); |
||
202 | |||
203 | initialize_voltage(); |
||
204 | |||
205 | if (!run) return 0; |
||
206 | |||
207 | FlushInQ (pCOM1); |
||
208 | FlushOutQ (pCOM1); |
||
209 | |||
210 | Delay(0.1); |
||
211 | |||
212 | movement(num_points); |
||
213 | |||
214 | while (1) // Continious reading |
||
215 | { |
||
216 | volt = read_voltage(); |
||
217 | FlushInQ (pCOM2); |
||
218 | SetCtrlVal(panel, p1_VOLTAGE, volt); |
||
219 | |||
220 | //writing in tabel for graph |
||
221 | volt_g1[j] = volt; |
||
222 | if (volt>6) volt_g1[j]=volt_g1[j-1]; |
||
223 | if (volt<-0.1) volt_g1[j]=volt_g1[j-1]; |
||
224 | j++; |
||
225 | |||
226 | if (!run) // Stopping grbl if pressed STOP |
||
227 | { |
||
228 | ComWrt (pCOM1, "!\n", 2); //stops grbl |
||
229 | Delay(0.01); |
||
230 | |||
231 | ComWrt (pCOM1, "~\n", 2); //releases grbl |
||
232 | Delay(0.01); |
||
233 | |||
234 | int strLen = GetInQLen (pCOM1); |
||
235 | bytes_read0 = ComRd (pCOM1, read_data0, strLen); |
||
236 | //printf(" : %s", read_data0); |
||
237 | break; |
||
238 | } |
||
239 | |||
240 | FlushInQ (pCOM1); |
||
241 | FlushOutQ (pCOM1); |
||
242 | |||
243 | ComWrt (pCOM1, "?\n", 2); //asks grbl his position |
||
244 | Delay(0.01); |
||
245 | |||
246 | int strLen5 = GetInQLen (pCOM1); |
||
247 | bytes_read0 = ComRd (pCOM1, read_data0, strLen5); |
||
248 | |||
249 | char *read_data_0 = strtok(read_data0, "\n"); |
||
250 | |||
251 | char *substr2 = strstr(read_data_0,"Idle"); |
||
252 | //printf("\n%d\n", substr); |
||
253 | |||
254 | if(substr2>0) break; |
||
255 | } |
||
256 | |||
257 | if (run) |
||
258 | { |
||
259 | // Choosing 2^n points |
||
260 | int j_j = j - 500; //cut off first 300 and last - 200 - points |
||
261 | int j_jj = j_j/num_points; |
||
262 | //printf("j_j = %d, j_jj = %d\n",j_j,j_jj); |
||
263 | |||
264 | //IMPORTANT! On graph must be only num_points of points |
||
265 | for (int i=0; i<num_points; i++) |
||
266 | { |
||
267 | volt_g[i] = volt_g1[300+i*j_jj]; |
||
268 | } |
||
269 | } |
||
270 | |||
271 | // Moving by steps |
||
272 | |||
273 | /*for(int i=(-num_points/2); i<(num_points/2); i++) |
||
274 | { |
||
275 | //printf("i is: %d\n", i); |
||
276 | movement(i); |
||
277 | asking_position(); |
||
278 | Delay(0.01); // Little time has to be in between so that grbl can respond |
||
279 | volt = read_voltage(); |
||
280 | Delay(wait); |
||
281 | FlushInQ (pCOM1); |
||
282 | FlushInQ (pCOM2); |
||
283 | SetCtrlVal(panel, p1_VOLTAGE, volt); |
||
284 | |||
285 | //writing in tabel for graph |
||
286 | volt_g[j] = volt; |
||
287 | j++; |
||
288 | |||
289 | if (!run) break; |
||
290 | |||
291 | }*/ |
||
292 | |||
293 | if (run) //IMPORTANT!! SAME NUM. of POINTS FOR FOURIER |
||
294 | { |
||
295 | //for(int i=0;i<j;i++) printf("volt_g[%d] = %lf\n",i,volt_g[i]); |
||
296 | if (graph>0) DeleteGraphPlot (panel, p1_VOLTAGE_GRAPH, graph, VAL_DELAYED_DRAW); |
||
297 | graph = PlotY (panel, p1_VOLTAGE_GRAPH, &volt_g[0], num_points, VAL_DOUBLE, |
||
298 | VAL_CONNECTED_POINTS, VAL_SIMPLE_DOT, VAL_SOLID, 1, VAL_WHITE); |
||
299 | ji = num_points; |
||
300 | } |
||
301 | |||
302 | FlushInQ (pCOM1); |
||
303 | FlushOutQ (pCOM1); |
||
304 | FlushInQ (pCOM2); |
||
305 | FlushOutQ (pCOM2); |
||
306 | run = 0; |
||
307 | |||
308 | return 0; |
||
309 | } |
||
310 | |||
311 | |||
312 | static int CVICALLBACK voltage_run(void *functionData) |
||
313 | { |
||
356 | f9daq | 314 | double voltA; |
354 | f9daq | 315 | |
316 | initialize_voltage(); |
||
317 | |||
356 | f9daq | 318 | chart = NewCtrl (panel, CTRL_STRIP_CHART_LS, "RUN VOLTAGE", 100, 15); |
354 | f9daq | 319 | SetCtrlAttribute(panel, chart, ATTR_HEIGHT, 600); |
320 | SetCtrlAttribute(panel, chart, ATTR_WIDTH , 1300); |
||
356 | f9daq | 321 | SetAxisRange(panel, chart, VAL_NO_CHANGE, 0, 0, VAL_AUTOSCALE, 0, 0); |
354 | f9daq | 322 | |
323 | while(1) |
||
324 | { |
||
325 | voltA = read_voltage(); |
||
356 | f9daq | 326 | PlotStripChartPoint (panel, chart, voltA); |
354 | f9daq | 327 | |
328 | SetCtrlVal(panel, p1_VOLTAGE, voltA); |
||
329 | |||
330 | Delay(0.05); |
||
331 | |||
332 | FlushInQ (pCOM2); |
||
333 | |||
334 | if (vi == 0) break; |
||
335 | } |
||
336 | |||
337 | DiscardCtrl (panel, chart); |
||
356 | f9daq | 338 | chart = 0; |
354 | f9daq | 339 | |
340 | return 0; |
||
341 | } |
||
342 | |||
343 | |||
344 | static int CVICALLBACK free_run(void *functionData) |
||
345 | { |
||
346 | if (run == 1) return 0; |
||
347 | run1 = 1; |
||
348 | |||
349 | if (left_1) movement(-10000); |
||
350 | if (right_1) movement(10000); |
||
351 | |||
352 | left_1 = 0; |
||
353 | right_1 = 0; |
||
354 | |||
355 | while (1) |
||
356 | { |
||
357 | if (run1 == 0) |
||
358 | { |
||
359 | ComWrt (pCOM1, "!\n", 2); //stops grbl |
||
360 | |||
361 | Delay(0.01); |
||
362 | |||
363 | ComWrt (pCOM1, "~\n", 2); //releases grbl |
||
364 | |||
365 | Delay(0.01); |
||
366 | |||
367 | int strLen = GetInQLen (pCOM1); |
||
368 | bytes_read0 = ComRd (pCOM1, read_data0, strLen); |
||
369 | //printf(" : %s", read_data0); |
||
370 | |||
356 | f9daq | 371 | //printf("Stop run1\n"); |
354 | f9daq | 372 | |
373 | break; |
||
374 | } |
||
375 | } |
||
376 | |||
377 | return 0; |
||
378 | } |
||
379 | |||
380 | |||
381 | int main() |
||
382 | { |
||
383 | |||
384 | SetStdioPort(HOST_SYSTEM_STDIO); |
||
385 | SetStdioWindowOptions(1000000, 0, 0); |
||
386 | SetStdioWindowVisibility(1); |
||
387 | |||
388 | initialize(); |
||
389 | |||
390 | CmtNewThreadPool (MAX_THREADS, &poolHandle); |
||
391 | |||
392 | DisplayPanel (panel = LoadPanel (0, "motor.uir", p1)); |
||
393 | RunUserInterface (); |
||
394 | |||
395 | CmtDiscardThreadPool (poolHandle); |
||
396 | |||
397 | return 0; |
||
398 | } |
||
399 | |||
400 | |||
401 | int CVICALLBACK StartCB (int panel, int control, int event, |
||
402 | void *callbackData, int eventData1, int eventData2) |
||
403 | { |
||
404 | int dummy; |
||
405 | |||
406 | switch (event) |
||
407 | { |
||
408 | case EVENT_COMMIT: |
||
409 | |||
410 | if (run == 1) break; |
||
411 | |||
412 | run = 1; |
||
413 | |||
414 | CmtScheduleThreadPoolFunction (poolHandle, daq_run, (void *)&dummy, &tfID); |
||
415 | |||
416 | } |
||
417 | |||
418 | return 0; |
||
419 | } |
||
420 | |||
421 | |||
422 | int CVICALLBACK StopCB (int panel, int control, int event, |
||
423 | void *callbackData, int eventData1, int eventData2) |
||
424 | { |
||
425 | switch (event) |
||
426 | { |
||
427 | case EVENT_COMMIT: |
||
428 | |||
429 | if (run == 0) break; |
||
430 | |||
431 | run = 0; |
||
432 | |||
433 | CmtWaitForThreadPoolFunctionCompletion (poolHandle, tfID, |
||
434 | OPT_TP_PROCESS_EVENTS_WHILE_WAITING); |
||
435 | CmtReleaseThreadPoolFunctionID (poolHandle, tfID); |
||
436 | |||
437 | printf("Stop\n"); |
||
438 | |||
439 | break; |
||
440 | |||
441 | } |
||
442 | |||
443 | return 0; |
||
444 | } |
||
445 | |||
446 | |||
447 | int CVICALLBACK ExitCB (int panel, int control, int event, |
||
448 | void *callbackData, int eventData1, int eventData2) |
||
449 | { |
||
450 | switch (event) |
||
451 | { |
||
452 | case EVENT_COMMIT: |
||
453 | QuitUserInterface (0); |
||
454 | CloseCom(pCOM1); |
||
455 | CloseCom(pCOM2); |
||
456 | break; |
||
457 | } |
||
458 | return 0; |
||
459 | } |
||
460 | |||
461 | |||
462 | int CVICALLBACK Fourier_TransCB (int panel, int control, int event, |
||
463 | void *callbackData, int eventData1, int eventData2) |
||
464 | { |
||
465 | if (!ji) return 0; |
||
466 | const int ii = ji; |
||
467 | double fourier_t[ii], fourier_ti[ii]; |
||
468 | char window_fun; |
||
356 | f9daq | 469 | double voltmax=0, voltmin=5, voltavg; |
354 | f9daq | 470 | |
471 | switch (event) |
||
472 | { |
||
473 | case EVENT_COMMIT: |
||
474 | |||
475 | for(int i=0; i<ii; i++) |
||
476 | { |
||
477 | fourier_t[i] = volt_g[i]; |
||
478 | fourier_ti[i] = 0; |
||
356 | f9daq | 479 | |
480 | if (fourier_t[i]>voltmax) voltmax = fourier_t[i]; |
||
481 | if (fourier_t[i]<voltmin) voltmin = fourier_t[i]; |
||
354 | f9daq | 482 | } |
483 | |||
356 | f9daq | 484 | voltavg = (voltmin+voltmax)/2.0; |
354 | f9daq | 485 | GetCtrlVal(panel, p1_WIN_FUN, &window_fun); |
486 | //printf("window_fun = %d\n", window_fun); |
||
356 | f9daq | 487 | //printf("max %f, min %f, avg %f\n", voltmax, voltmin, voltavg); |
354 | f9daq | 488 | if (window_fun!=0) |
489 | { |
||
356 | f9daq | 490 | //do something with window functions (N=ii) |
491 | 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 |
||
492 | 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 |
||
493 | 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 |
||
494 | YGraphPopup ("HOW voltage LOOKS NOW", fourier_t, ii, VAL_DOUBLE); |
||
354 | f9daq | 495 | } |
356 | f9daq | 496 | |
354 | f9daq | 497 | FFT (fourier_t, fourier_ti, ii); |
498 | for (int i=0; i<ii; i++) fourier_t[i] = fabs(fourier_t[i]); |
||
499 | fourier_t[0]=0; |
||
500 | |||
501 | YGraphPopup ("FOURIER", fourier_t, ii/2+1, VAL_DOUBLE); |
||
356 | f9daq | 502 | |
354 | f9daq | 503 | |
356 | f9daq | 504 | FILE *fp,*fp1; |
354 | f9daq | 505 | |
356 | f9daq | 506 | //file name has to have file extension |
507 | char fname[50]; |
||
508 | GetCtrlVal(panel, p1_F_NAME, fname); |
||
509 | |||
510 | char fname0[100] = "C:/Users/"; |
||
511 | char *name; |
||
512 | name = getenv("USERNAME"); |
||
513 | strcat(fname0,name); |
||
514 | strcat(fname0,"/Desktop/"); |
||
515 | //char fname0[100] = "C:/Users/Student/Desktop/"; //Alternative way |
||
516 | strcat(fname0,fname); |
||
517 | fp = fopen(fname0, "w"); |
||
354 | f9daq | 518 | |
356 | f9daq | 519 | char fname1[50]; |
520 | GetCtrlVal(panel, p1_F_NAME1, fname1); |
||
521 | char fname10[100] = "C:/Users/"; |
||
522 | strcat(fname10,name); |
||
523 | strcat(fname10,"/Desktop/"); |
||
524 | strcat(fname10,fname1); |
||
525 | fp1 = fopen(fname10, "w"); |
||
354 | f9daq | 526 | |
356 | f9daq | 527 | fprintf(fp, "step\tvoltage\n"); |
528 | for(int i=0; i<ii; i++) |
||
529 | { |
||
530 | fprintf(fp, "%d\t%lf\n", i+1, volt_g[i]); |
||
531 | } |
||
354 | f9daq | 532 | |
356 | f9daq | 533 | fprintf(fp1, "step\tfourier\n"); |
534 | for(int i=0; i<ii/2+1; i++) |
||
535 | { |
||
536 | fprintf(fp1, "%d\t%lf\n", i, fourier_t[i]); //0th component is from DC - background? YES |
||
354 | f9daq | 537 | } |
538 | |||
356 | f9daq | 539 | fclose(fp); |
540 | fclose(fp1); |
||
541 | printf("Files %s and %s created.\n", fname, fname1); |
||
542 | |||
354 | f9daq | 543 | break; |
544 | |||
545 | } |
||
546 | |||
547 | return 0; |
||
548 | } |
||
549 | |||
550 | |||
551 | int CVICALLBACK ExamplesCB (int panel, int control, int event, |
||
552 | void *callbackData, int eventData1, int eventData2) |
||
553 | { |
||
554 | const int ki=1024; |
||
555 | double ret[ki], ret1[ki]; |
||
556 | double val = 3.14159265/180; |
||
557 | int x1, x2, a1, a2; |
||
356 | f9daq | 558 | char window_fun; |
354 | f9daq | 559 | |
560 | switch (event) |
||
561 | { |
||
562 | case EVENT_COMMIT: |
||
563 | |||
564 | GetCtrlVal(panel, p1_FREQ1, &x1); |
||
565 | GetCtrlVal(panel, p1_FREQ2, &x2); |
||
566 | GetCtrlVal(panel, p1_AMPL1, &a1); |
||
567 | GetCtrlVal(panel, p1_AMPL2, &a2); |
||
568 | |||
356 | f9daq | 569 | GetCtrlVal(panel, p1_WIN_FUN, &window_fun); |
570 | //printf("window_fun = %d\n", window_fun); |
||
571 | |||
354 | f9daq | 572 | for (int i=0; i<ki; i++) |
573 | { |
||
574 | ret[i] = a1*sin(x1*i*val)+a2*sin(x2*i*val); |
||
575 | ret1[i] = 0; |
||
576 | } |
||
356 | f9daq | 577 | if (window_fun!=0) |
578 | { |
||
579 | //do something with window functions (N=ki) |
||
580 | if(window_fun==1) for(int i=0; i<ki; i++) ret[i] = ret[i]*(1 - fabs((i-(ki/2.0))/(ki/2))); //Bartlett |
||
581 | 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 |
||
582 | 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 |
||
583 | } |
||
354 | f9daq | 584 | YGraphPopup ("example graph", ret, ki, VAL_DOUBLE); |
585 | FFT (ret, ret1, ki); |
||
586 | for (int i=0; i<ki; i++) ret[i] = fabs(ret[i]); |
||
587 | //for (int i=0;i<ki;i++) printf("basic: %lf, fourier: %lf\n", ret1[i], ret[i]); |
||
588 | YGraphPopup ("example fourier", ret, ki/2+1, VAL_DOUBLE); |
||
589 | |||
590 | break; |
||
591 | } |
||
592 | return 0; |
||
593 | } |
||
594 | |||
595 | |||
596 | int CVICALLBACK Only_voltageCB (int panel, int control, int event, |
||
597 | void *callbackData, int eventData1, int eventData2) |
||
598 | { |
||
599 | int dummy, volt_run; |
||
600 | |||
601 | switch (event) |
||
602 | { |
||
603 | case EVENT_COMMIT: |
||
604 | |||
605 | GetCtrlVal (panel, p1_VOLT_GRAPH, &volt_run); |
||
606 | |||
607 | if(volt_run) |
||
608 | { |
||
609 | vi = 1; |
||
610 | CmtScheduleThreadPoolFunction (poolHandle, voltage_run, (void *)&dummy, &tfID); |
||
611 | } |
||
612 | |||
613 | else |
||
614 | { |
||
615 | vi = 0; |
||
616 | CmtWaitForThreadPoolFunctionCompletion (poolHandle, tfID, |
||
617 | OPT_TP_PROCESS_EVENTS_WHILE_WAITING); |
||
618 | CmtReleaseThreadPoolFunctionID (poolHandle, tfID); |
||
619 | } |
||
620 | |||
621 | break; |
||
622 | } |
||
623 | return 0; |
||
624 | } |
||
625 | |||
626 | |||
627 | int CVICALLBACK RangeCB (int panel, int control, int event, |
||
628 | void *callbackData, int eventData1, int eventData2) |
||
629 | { |
||
630 | int fix_range; |
||
631 | double ymin,ymax,xmin,xmax; |
||
632 | |||
633 | switch (event) |
||
634 | { |
||
635 | case EVENT_COMMIT: |
||
636 | |||
637 | GetCtrlVal(panel, p1_RANGE, &fix_range); |
||
356 | f9daq | 638 | |
639 | if(chart) |
||
640 | { |
||
641 | if(fix_range) |
||
642 | { |
||
643 | GetAxisRange (panel, chart, VAL_MANUAL, &xmin, &xmax, VAL_MANUAL, &ymin, &ymax); |
||
644 | if (ymax<=2) SetAxisRange (panel, chart, VAL_NO_CHANGE, 0, 0, VAL_MANUAL, 0, 2); |
||
645 | else SetAxisRange (panel, chart, VAL_NO_CHANGE, 0, 0, VAL_MANUAL, 0, 5); |
||
646 | } |
||
647 | else SetAxisRange(panel, chart, VAL_NO_CHANGE, 0, 0, VAL_AUTOSCALE, 0, 0); |
||
648 | } |
||
649 | |||
354 | f9daq | 650 | if(fix_range) |
651 | { |
||
652 | GetAxisRange (panel, p1_VOLTAGE_GRAPH, VAL_MANUAL, &xmin, &xmax, VAL_MANUAL, &ymin, &ymax); |
||
653 | if (ymax<=2) SetAxisRange (panel, p1_VOLTAGE_GRAPH, VAL_NO_CHANGE, 0, 0, VAL_MANUAL, 0, 2); |
||
654 | else SetAxisRange (panel, p1_VOLTAGE_GRAPH, VAL_NO_CHANGE, 0, 0, VAL_MANUAL, 0, 5); |
||
655 | } |
||
656 | else SetAxisRange (panel, p1_VOLTAGE_GRAPH, VAL_NO_CHANGE, 0, 0, VAL_AUTOSCALE, 0, 0); |
||
657 | |||
658 | break; |
||
659 | |||
660 | } |
||
661 | |||
662 | return 0; |
||
663 | } |
||
664 | |||
665 | |||
666 | int CVICALLBACK FreeMoveCB (int panel, int control, int event, |
||
667 | void *callbackData, int eventData1, int eventData2) |
||
668 | { |
||
669 | int dummy; |
||
670 | |||
671 | switch (event) |
||
672 | { |
||
673 | case EVENT_COMMIT: |
||
674 | |||
675 | if (run == 1) break; |
||
676 | if (run1 == 1) break; |
||
677 | right_1 = 1; |
||
678 | |||
679 | CmtScheduleThreadPoolFunction (poolHandle, free_run, (void *)&dummy, &tfID1); |
||
680 | |||
681 | break; |
||
682 | |||
683 | } |
||
684 | |||
685 | return 0; |
||
686 | } |
||
687 | |||
688 | int CVICALLBACK FreeMove1CB (int panel, int control, int event, |
||
689 | void *callbackData, int eventData1, int eventData2) |
||
690 | { |
||
691 | int dummy; |
||
692 | |||
693 | switch (event) |
||
694 | { |
||
695 | case EVENT_COMMIT: |
||
696 | |||
697 | if (run == 1) break; |
||
698 | if (run1 == 1) break; |
||
699 | left_1 = 1; |
||
700 | |||
701 | CmtScheduleThreadPoolFunction (poolHandle, free_run, (void *)&dummy, &tfID1); |
||
702 | |||
703 | break; |
||
704 | |||
705 | } |
||
706 | |||
707 | return 0; |
||
708 | } |
||
709 | |||
710 | int CVICALLBACK FreeMoveStopCB (int panel, int control, int event, |
||
711 | void *callbackData, int eventData1, int eventData2) |
||
712 | { |
||
713 | switch (event) |
||
714 | { |
||
715 | case EVENT_COMMIT: |
||
716 | |||
717 | if (run == 1) break; |
||
718 | if (run1 == 0) break; |
||
719 | run1 = 0; |
||
356 | f9daq | 720 | |
721 | if (run1 == 0) //Additionally here just to be sure |
||
722 | { |
||
723 | ComWrt (pCOM1, "!\n", 2); //stops grbl |
||
354 | f9daq | 724 | |
356 | f9daq | 725 | Delay(0.01); |
726 | |||
727 | ComWrt (pCOM1, "~\n", 2); //releases grbl |
||
728 | |||
729 | Delay(0.01); |
||
730 | |||
731 | int strLen = GetInQLen (pCOM1); |
||
732 | bytes_read0 = ComRd (pCOM1, read_data0, strLen); |
||
733 | //printf(" : %s", read_data0); |
||
734 | |||
735 | printf("Stop run1\n"); |
||
736 | } |
||
737 | |||
354 | f9daq | 738 | CmtWaitForThreadPoolFunctionCompletion (poolHandle, tfID1, |
739 | OPT_TP_PROCESS_EVENTS_WHILE_WAITING); |
||
740 | CmtReleaseThreadPoolFunctionID (poolHandle, tfID1); |
||
741 | |||
742 | break; |
||
743 | |||
744 | } |
||
745 | |||
746 | return 0; |
||
747 | } |
||
748 | |||
749 | |||
750 | int CVICALLBACK Pos0CB (int panel, int control, int event, |
||
751 | void *callbackData, int eventData1, int eventData2) |
||
752 | { |
||
753 | switch (event) |
||
754 | { |
||
755 | case EVENT_COMMIT: |
||
756 | |||
757 | if (run == 1) break; |
||
758 | if (run1 == 1) break; |
||
759 | FlushInQ (pCOM1); |
||
760 | FlushOutQ (pCOM1); |
||
761 | CloseCom(pCOM1); |
||
762 | initialize(); |
||
763 | |||
764 | break; |
||
765 | |||
766 | } |
||
767 | |||
768 | return 0; |
||
769 | } |