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