00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "rs274ngc.hh"
00017 #include <stdio.h>
00018 #include <stdlib.h>
00019
00020
00021 #define DRIVER_ERROR(message,item) if(1) \
00022 {fprintf(stderr, message, item); \
00023 fprintf(stderr, "\n"); \
00024 return RS274NGC_ERROR; } \
00025 else
00026
00027 extern setup _interpreter_settings;
00028 extern FILE* _interpreter_fp;
00029 extern char _interpreter_linetext[];
00030 extern char _interpreter_blocktext[];
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 static char * strip_terminal_newline(char *string)
00050 {
00051 int index = strlen(string) - 1;
00052
00053 while (index >= 0) {
00054 if (string[index] == '\n' ||
00055 string[index] == '\r') {
00056 string[index] = 0;
00057 }
00058 index--;
00059 }
00060
00061 return string;
00062 }
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 int close_and_down(
00107 char * line)
00108 {
00109 int m;
00110 int n;
00111 int comment;
00112 char item;
00113 comment SET_TO 0;
00114 for (n SET_TO 0, m SET_TO 0; (item SET_TO line[m]) ISNT (char) NULL; m++)
00115 {
00116 if (comment)
00117 {
00118 line[n++] SET_TO item;
00119 if (item IS ')')
00120 {
00121 comment SET_TO 0;
00122 }
00123 else if (item IS '(')
00124 DRIVER_ERROR("Nested comment found%s", "");
00125 }
00126 else if ((item IS ' ') OR (item IS '\t') OR (item IS '\r'));
00127
00128 else if (item IS '\n')
00129 {
00130 if (line[m+1] ISNT 0)
00131 DRIVER_ERROR("Null missing after newline%s", "");
00132 }
00133 else if ((64 < item) AND (item < 91))
00134 {
00135 line[n++] SET_TO (32 + item);
00136 }
00137 else if (item IS '(')
00138 {
00139 comment SET_TO 1;
00140 line[n++] SET_TO item;
00141 }
00142 else
00143 {
00144 line[n++] SET_TO item;
00145 }
00146 }
00147 if (m IS (INTERP_TEXT_SIZE - 1))
00148 DRIVER_ERROR("Command too long%s", "");
00149 else if (comment)
00150 DRIVER_ERROR("Unclosed comment found%s", "");
00151 line[n] SET_TO 0;
00152 return RS274NGC_OK;
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 int read_keyboard_line(
00177 char * raw_line,
00178 char * line,
00179 int * length)
00180 {
00181 char * returned_value;
00182
00183 returned_value SET_TO fgets(raw_line, INTERP_TEXT_SIZE, stdin);
00184 strip_terminal_newline(raw_line);
00185 if (returned_value IS NULL)
00186 DRIVER_ERROR("fgets failed%s", "");
00187 strcpy(line, raw_line);
00188 if (close_and_down(line) IS RS274NGC_ERROR)
00189 return RS274NGC_ERROR;
00190 *length SET_TO strlen(line);
00191 return RS274NGC_OK;
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 int read_setup_file(
00237 char * setup_file,
00238 setup_pointer settings)
00239 {
00240 static char name[] SET_TO "read_setup_file";
00241 FILE * setup_file_port;
00242 char buffer[1000];
00243 char attribute[100];
00244 char value[100];
00245
00246 setup_file_port SET_TO fopen(setup_file, "r");
00247 if (setup_file_port IS NULL)
00248 DRIVER_ERROR("Cannot open %s", setup_file);
00249 for(;;)
00250 {
00251 if (fgets(buffer, 1000, setup_file_port) IS NULL)
00252 DRIVER_ERROR("Bad %s file format", "setup");
00253 else if (buffer[0] IS '\n')
00254 break;
00255 }
00256
00257 for (;;)
00258 {
00259 if (fgets(buffer, 1000, setup_file_port) IS NULL)
00260 break;
00261 if (sscanf(buffer, "%s%s", attribute, value) IS 0)
00262 DRIVER_ERROR("Bad input line \"%s\" in setup file", buffer);
00263 if (strcmp(attribute, "block_delete") IS 0)
00264 {
00265 if (strcmp(value, "ON") IS 0)
00266 settings->block_delete SET_TO ON;
00267 else if (strcmp(value, "OFF") IS 0)
00268 settings->block_delete SET_TO OFF;
00269 else
00270 DRIVER_ERROR("Bad value %s for block_delete in setup file", value);
00271 }
00272 else if (strcmp(attribute, "current_x") IS 0)
00273 settings->current_x SET_TO atof(value);
00274 else if (strcmp(attribute, "current_y") IS 0)
00275 settings->current_y SET_TO atof(value);
00276 else if (strcmp(attribute, "current_z") IS 0)
00277 settings->current_z SET_TO atof(value);
00278 else if (strcmp(attribute, "cutter_radius_comp") IS 0)
00279 {
00280 if (strcmp(value, "OFF") IS 0)
00281 settings->cutter_radius_compensation SET_TO OFF;
00282 else if (strcmp(value, "LEFT") IS 0)
00283 settings->cutter_radius_compensation SET_TO LEFT;
00284 else if (strcmp(value, "RIGHT") IS 0)
00285 settings->cutter_radius_compensation SET_TO RIGHT;
00286 else
00287 DRIVER_ERROR("Bad value %s for cutter_radius_comp in setup file",
00288 value);
00289 }
00290 else if (strcmp(attribute, "cycle_i") IS 0)
00291 settings->cycle_i SET_TO atof(value);
00292 else if (strcmp(attribute, "cycle_j") IS 0)
00293 settings->cycle_j SET_TO atof(value);
00294 else if (strcmp(attribute, "cycle_k") IS 0)
00295 settings->cycle_k SET_TO atof(value);
00296 else if (strcmp(attribute, "cycle_l") IS 0)
00297 settings->cycle_l SET_TO atoi(value);
00298 else if (strcmp(attribute, "cycle_p") IS 0)
00299 settings->cycle_p SET_TO atof(value);
00300 else if (strcmp(attribute, "cycle_q") IS 0)
00301 settings->cycle_q SET_TO atof(value);
00302 else if (strcmp(attribute, "cycle_r") IS 0)
00303 settings->cycle_r SET_TO atof(value);
00304 else if (strcmp(attribute, "cycle_z") IS 0)
00305 settings->cycle_z SET_TO atof(value);
00306 else if (strcmp(attribute, "distance_mode") IS 0)
00307 {
00308 if (strcmp(value, "ABSOLUTE") IS 0)
00309 settings->distance_mode SET_TO MODE_ABSOLUTE;
00310 else if (strcmp(value, "INCREMENTAL") IS 0)
00311 settings->distance_mode SET_TO MODE_INCREMENTAL;
00312 else
00313 DRIVER_ERROR("Bad value %s for distance_mode in setup file",
00314 value);
00315 }
00316 else if (strcmp(attribute, "feed_mode") IS 0)
00317 {
00318 if (strcmp(value, "PER_MINUTE") IS 0)
00319 settings->feed_mode SET_TO UNITS_PER_MINUTE;
00320 else if (strcmp(value, "INVERSE_TIME") IS 0)
00321 settings->feed_mode SET_TO INVERSE_TIME;
00322 else
00323 DRIVER_ERROR("Bad value %s for feed_mode in setup file", value);
00324 }
00325 else if (strcmp(attribute, "feed_rate") IS 0)
00326 settings->feed_rate SET_TO atof(value);
00327 else if (strcmp(attribute, "flood") IS 0)
00328 {
00329 if (strcmp(value, "OFF") IS 0)
00330 settings->flood SET_TO OFF;
00331 else if (strcmp(value, "ON") IS 0)
00332 settings->flood SET_TO ON;
00333 else
00334 DRIVER_ERROR("Bad value %s for flood in setup file", value);
00335 }
00336 else if (strcmp(attribute, "length_units") IS 0)
00337 {
00338 if (strcmp(value, "MILLIMETERS") IS 0)
00339 settings->length_units SET_TO CANON_UNITS_MM;
00340 else if (strcmp(value, "INCHES") IS 0)
00341 settings->length_units SET_TO CANON_UNITS_INCHES;
00342 else
00343 DRIVER_ERROR("Bad value %s for length_units in setup file", value);
00344 }
00345 else if (strcmp(attribute, "mist") IS 0)
00346 {
00347 if (strcmp(value, "OFF") IS 0)
00348 settings->mist SET_TO OFF;
00349 else if (strcmp(value, "ON") IS 0)
00350 settings->mist SET_TO ON;
00351 else
00352 DRIVER_ERROR("Bad value %s for mist in setup file", value);
00353 }
00354 else if (strcmp(attribute, "motion_mode") IS 0)
00355 settings->motion_mode SET_TO atoi(value);
00356 else if (strcmp(attribute, "plane") IS 0)
00357 {
00358 if (strcmp(value, "XY") IS 0)
00359 settings->plane SET_TO CANON_PLANE_XY;
00360 else if (strcmp(value, "YZ") IS 0)
00361 settings->plane SET_TO CANON_PLANE_YZ;
00362 else if (strcmp(value, "XZ") IS 0)
00363 settings->plane SET_TO CANON_PLANE_XZ;
00364 else
00365 DRIVER_ERROR("Bad value %s for plane in setup file", value);
00366 }
00367 else if (strcmp(attribute, "axis_offset_x") IS 0)
00368 settings->axis_offset_x SET_TO atof(value);
00369 else if (strcmp(attribute, "axis_offset_y") IS 0)
00370 settings->axis_offset_y SET_TO atof(value);
00371 else if (strcmp(attribute, "axis_offset_z") IS 0)
00372 settings->axis_offset_z SET_TO atof(value);
00373 else if (strcmp(attribute, "origin_offset_x") IS 0)
00374 settings->origin_offset_x SET_TO atof(value);
00375 else if (strcmp(attribute, "origin_offset_y") IS 0)
00376 settings->origin_offset_y SET_TO atof(value);
00377 else if (strcmp(attribute, "origin_offset_z") IS 0)
00378 settings->origin_offset_z SET_TO atof(value);
00379 else if (strcmp(attribute, "slot_for_length_offset") IS 0)
00380 settings->length_offset_index SET_TO atoi(value);
00381 else if (strcmp(attribute, "slot_for_radius_comp") IS 0)
00382 settings->tool_table_index SET_TO atoi(value);
00383 else if (strcmp(attribute, "slot_in_use") IS 0)
00384 settings->current_slot SET_TO atoi(value);
00385 else if (strcmp(attribute, "slot_selected") IS 0)
00386 settings->selected_tool_slot SET_TO atoi(value);
00387 else if (strcmp(attribute, "spindle_speed") IS 0)
00388 settings->speed SET_TO atof(value);
00389 else if (strcmp(attribute, "speed_feed_mode") IS 0)
00390 {
00391 if (strcmp(value, "INDEPENDENT") IS 0)
00392 settings->speed_feed_mode SET_TO CANON_INDEPENDENT;
00393 else if (strcmp(value, "SYNCHED") IS 0)
00394 settings->speed_feed_mode SET_TO CANON_SYNCHED;
00395 else
00396 DRIVER_ERROR("Bad value %s for speed_feed_mode in setup file",
00397 value);
00398 }
00399 else if (strcmp(attribute, "spindle_turning") IS 0)
00400 {
00401 if (strcmp(value, "STOPPED") IS 0)
00402 settings->spindle_turning SET_TO CANON_STOPPED;
00403 else if (strcmp(value, "CLOCKWISE") IS 0)
00404 settings->spindle_turning SET_TO CANON_CLOCKWISE;
00405 else if (strcmp(value, "COUNTERCLOCKWISE") IS 0)
00406 settings->spindle_turning SET_TO CANON_COUNTERCLOCKWISE;
00407 else
00408 DRIVER_ERROR("Bad value %s for spindle_turning in setup file",
00409 value);
00410 }
00411 else if (strcmp(attribute, "tool_length_offset") IS 0)
00412 settings->tool_length_offset SET_TO atof(value);
00413 else if (strcmp(attribute, "traverse_rate") IS 0)
00414 settings->traverse_rate SET_TO atof(value);
00415 else
00416 DRIVER_ERROR("Unknown attribute %s in setup file", attribute);
00417 }
00418 fclose(setup_file_port);
00419 return RS274NGC_OK;
00420 }
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459 int read_tool_file(
00460 char * tool_file,
00461 setup_pointer settings)
00462 {
00463 FILE * tool_file_port;
00464 int slot;
00465 int tool_id;
00466 double offset;
00467 double diameter;
00468 char buffer[1000];
00469
00470 tool_file_port SET_TO fopen(tool_file, "r");
00471 if (tool_file_port IS NULL)
00472 DRIVER_ERROR("Cannot open %s", tool_file);
00473 for(;;)
00474 {
00475 if (fgets(buffer, 1000, tool_file_port) IS NULL)
00476 DRIVER_ERROR("Bad %s file format", "tool");
00477 else if (buffer[0] IS '\n')
00478 break;
00479 }
00480
00481 for (;;)
00482 {
00483 if (fgets(buffer, 1000, tool_file_port) IS NULL)
00484 break;
00485 if (sscanf(buffer, "%d %d %lf %lf", &slot,
00486 &tool_id, &offset, &diameter) IS 0)
00487 DRIVER_ERROR("Bad input line \"%s\" in tool file", buffer);
00488 settings->tool_table[slot].id SET_TO tool_id;
00489 settings->tool_table[slot].length SET_TO offset;
00490 settings->tool_table[slot].diameter SET_TO diameter;
00491 }
00492 fclose(tool_file_port);
00493 return RS274NGC_OK;
00494 }
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538 int interpret_from_file(
00539 char * filename,
00540 char * tool_file,
00541 char * setup_file,
00542 int no_stop)
00543 {
00544 int status;
00545 int reads;
00546 int k;
00547 char trash[INTERP_TEXT_SIZE];
00548 char * read_ok;
00549 int program_status;
00550
00551 program_status SET_TO RS274NGC_OK;
00552 if (rs274ngc_init() IS RS274NGC_ERROR)
00553 return RS274NGC_ERROR;
00554 if (tool_file[0] ISNT 0)
00555 if (read_tool_file(tool_file, &_interpreter_settings) IS RS274NGC_ERROR)
00556 return RS274NGC_ERROR;
00557 if (setup_file[0] ISNT 0)
00558 if (read_setup_file(setup_file, &_interpreter_settings) IS RS274NGC_ERROR)
00559 return RS274NGC_ERROR;
00560 if (rs274ngc_open(filename) ISNT RS274NGC_OK)
00561 return RS274NGC_ERROR;
00562 for(reads SET_TO 0; ; reads++)
00563 {
00564 status SET_TO rs274ngc_read();
00565 if (status IS RS274NGC_ENDFILE)
00566 return RS274NGC_ENDFILE;
00567 if (status ISNT RS274NGC_OK)
00568 {
00569 fprintf(stderr, "%s\n", _interpreter_linetext);
00570 if (no_stop IS OFF)
00571 return RS274NGC_ERROR;
00572 else
00573 {
00574 program_status SET_TO RS274NGC_ERROR;
00575 rs274ngc_open(filename);
00576 for(k SET_TO -1; k < reads; k++)
00577 {
00578 read_ok SET_TO
00579 fgets(trash, INTERP_TEXT_SIZE, _interpreter_fp);
00580 if (read_ok IS NULL)
00581 return RS274NGC_ERROR;
00582 }
00583 continue;
00584 }
00585 }
00586 status SET_TO rs274ngc_execute(NULL);
00587 if (status IS RS274NGC_ERROR)
00588 {
00589 fprintf(stderr, "%s\n", _interpreter_linetext);
00590 if (no_stop IS OFF)
00591 return RS274NGC_ERROR;
00592 else
00593 {
00594 program_status SET_TO RS274NGC_ERROR;
00595 rs274ngc_open(filename);
00596 for(k SET_TO -1; k < reads; k++)
00597 {
00598 read_ok SET_TO
00599 fgets(trash, INTERP_TEXT_SIZE, _interpreter_fp);
00600 if (read_ok IS NULL)
00601 return RS274NGC_ERROR;
00602 }
00603 continue;
00604 }
00605 }
00606 else if (status IS RS274NGC_EXIT)
00607 return program_status;
00608 }
00609 }
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651 int interpret_from_keyboard(
00652 char * tool_file,
00653 char * setup_file)
00654 {
00655 char confirm[INTERP_TEXT_SIZE];
00656 int length;
00657 int command_ready;
00658
00659 if (rs274ngc_init() IS RS274NGC_ERROR)
00660 return RS274NGC_ERROR;
00661 if (tool_file[0] ISNT 0)
00662 if (read_tool_file(tool_file, &_interpreter_settings) IS RS274NGC_ERROR)
00663 return RS274NGC_ERROR;
00664 if (setup_file[0] ISNT 0)
00665 if (read_setup_file(setup_file, &_interpreter_settings) IS RS274NGC_ERROR)
00666 return RS274NGC_ERROR;
00667 for(command_ready SET_TO FALSE; ; )
00668 {
00669 if (command_ready)
00670 {
00671 command_ready SET_TO FALSE;
00672 printf("EXEC <-");
00673 fgets(confirm, INTERP_TEXT_SIZE, stdin);
00674 if (confirm[0] IS ';')
00675 {
00676 rs274ngc_execute(_interpreter_blocktext);
00677 confirm[0] SET_TO 0;
00678 }
00679 else {}
00680 }
00681 else
00682 {
00683 printf("READ => ");
00684 if (read_keyboard_line(_interpreter_linetext,
00685 _interpreter_blocktext, &length)
00686 IS RS274NGC_ERROR);
00687 else if (strcmp (_interpreter_blocktext, "quit") IS 0)
00688 return RS274NGC_OK;
00689 else if (length > 0)
00690 {
00691 command_ready SET_TO TRUE;
00692 }
00693 }
00694 }
00695 }
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760 int main(int argc, char ** argv)
00761 {
00762 char tool_file[200];
00763 char setup_file[200];
00764
00765 if ((argc > 3) OR ((argc IS 3) AND (strcmp (argv[2], "continue") ISNT 0)))
00766 {
00767 fprintf(stderr, "Usage \"rs274ngc\"\n");
00768 fprintf(stderr, " or \"rs274ngc filename\"\n");
00769 fprintf(stderr, " or \"rs274ngc filename continue\"\n");
00770 exit(1);
00771 }
00772 fprintf(stderr, "name of tool file => ");
00773 fflush(stderr);
00774 fgets(tool_file, 200, stdin);
00775 strip_terminal_newline(tool_file);
00776 if (tool_file[0] IS 0)
00777 fprintf(stderr, "using default tool table\n");
00778
00779 fprintf(stderr, "name of setup file => ");
00780 fflush(stderr);
00781 fgets(setup_file, 200, stdin);
00782 strip_terminal_newline(setup_file);
00783 if (setup_file[0] IS 0)
00784 fprintf(stderr, "using default machine setup\n");
00785
00786 if (argc IS 1)
00787 exit (interpret_from_keyboard(tool_file, setup_file));
00788 else if (argc IS 2)
00789 exit (interpret_from_file( argv[1], tool_file, setup_file, OFF));
00790 else
00791 exit (interpret_from_file( argv[1], tool_file, setup_file, ON));
00792 }
00793
00794