📄 driver.cc
字号:
/********************************************************************* Description: driver.cc* Drives the interpreter from a menu based user interface.** 13-Oct-2000 WPS changed gets to fgets and moved external canon variable* definitions to canon.hh. (This may be temporary.)* Early March 2007 MGS adapted this to emc2** Derived from a work by Tom Kramer** Author:* License: GPL Version 2* System: Linux** Copyright (c) 2007 All rights reserved.** Last change:* $Revision: 1.9 $* $Author: jepler $* $Date: 2007/06/12 00:34:18 $*********************************************************************/#include "rs274ngc.hh"#include "rs274ngc_return.hh"#include "inifile.hh" // INIFILE#include "canon.hh" // _parameter_file_name#include "config.h" // LINELEN#include <stdio.h> /* gets, etc. */#include <stdlib.h> /* exit */#include <string.h> /* strcpy */#include <getopt.h>Interp interp_new;//Inifile inifile;#define active_settings interp_new.active_settings#define active_g_codes interp_new.active_g_codes#define active_m_codes interp_new.active_m_codes#define error_text interp_new.error_text#define interp_execute interp_new.execute#define file_name interp_new.file_name#define interp_init interp_new.init#define stack_name interp_new.stack_name#define line_text interp_new.line_text#define line_length interp_new.line_length#define sequence_number interp_new.sequence_number#define interp_close interp_new.close#define interp_exit interp_new.exit#define interp_open interp_new.open#define interp_read interp_new.read/*This file contains the source code for an emulation of using the six-axisrs274 interpreter from the EMC system.*//*********************************************************************//* report_errorReturned Value: noneSide effects: an error message is printed on stderrCalled by: interpret_from_file interpret_from_keyboard mainThis1. calls error_text to get the text of the error message whosecode is error_code and prints the message,2. calls line_text to get the text of the line on which theerror occurred and prints the text, and3. if print_stack is on, repeatedly calls stack_name to getthe names of the functions on the function call stack and prints thenames. The first function named is the one that sent the errormessage.*/void report_error( /* ARGUMENTS */ int error_code, /* the code number of the error message */ int print_stack) /* print stack if ON, otherwise not */{ char interp_error_text_buf[LINELEN]; int k; error_text(error_code, interp_error_text_buf, 5); /* for coverage of code */ error_text(error_code, interp_error_text_buf, LINELEN); fprintf(stderr, "%s\n", ((interp_error_text_buf[0] == 0) ? "Unknown error, bad error code" : interp_error_text_buf)); line_text(interp_error_text_buf, LINELEN); fprintf(stderr, "%s\n", interp_error_text_buf); if (print_stack == ON) { for (k = 0; ; k++) { stack_name(k, interp_error_text_buf, LINELEN); if (interp_error_text_buf[0] != 0) fprintf(stderr, "%s\n", interp_error_text_buf); else break; } }}/***********************************************************************//* interpret_from_keyboardReturned Value: int (0)Side effects: Lines of NC code entered by the user are interpreted.Called by: interpret_from_file mainThis prompts the user to enter a line of rs274 code. When the userhits <enter> at the end of the line, the line is executed.Then the user is prompted to enter another line.Any canonical commands resulting from executing the line are printedon the monitor (stdout). If there is an error in reading or executingthe line, an error message is printed on the monitor (stderr).To exit, the user must enter "quit" (followed by a carriage return).*/int interpret_from_keyboard( /* ARGUMENTS */ int block_delete, /* switch which is ON or OFF */ int print_stack) /* option which is ON or OFF */{ char line[LINELEN]; int status; for(; ;) { printf("READ => "); fgets(line, LINELEN, stdin); if (strcmp (line, "quit\n") == 0) return 0; status = interp_read(line); if ((status == INTERP_EXECUTE_FINISH) && (block_delete == ON)); else if (status == INTERP_ENDFILE); else if ((status != INTERP_EXECUTE_FINISH) && (status != INTERP_OK)) report_error(status, print_stack); else { status = interp_execute(); if ((status == INTERP_EXIT) || (status == INTERP_EXECUTE_FINISH)); else if (status != INTERP_OK) report_error(status, print_stack); } }}/*********************************************************************//* interpret_from_fileReturned Value: int (0 or 1) If any of the following errors occur, this returns 1. Otherwise, it returns 0. 1. interp_read returns something other than INTERP_OK or INTERP_EXECUTE_FINISH, no_stop is off, and the user elects not to continue. 2. interp_execute returns something other than INTERP_OK, EXIT, or INTERP_EXECUTE_FINISH, no_stop is off, and the user elects not to continue.Side Effects: An open NC-program file is interpreted.Called By: mainThis emulates the way the EMC system uses the interpreter.If the do_next argument is 1, this goes into MDI mode if an error isfound. In that mode, the user may (1) enter code or (2) enter "quit" toget out of MDI. Once out of MDI, this asks the user whether to continueinterpreting the file.If the do_next argument is 0, an error does not stop interpretation.If the do_next argument is 2, an error stops interpretation.*/int interpret_from_file( /* ARGUMENTS */ int do_next, /* what to do if error */ int block_delete, /* switch which is ON or OFF */ int print_stack) /* option which is ON or OFF */{ int status=0; char line[LINELEN]; for(; ;) { status = interp_read(NULL); if ((status == INTERP_EXECUTE_FINISH) && (block_delete == ON)) continue; else if (status == INTERP_ENDFILE) break; if ((status != INTERP_OK) && // should not be EXIT (status != INTERP_EXECUTE_FINISH)) { report_error(status, print_stack); if ((status == NCE_FILE_ENDED_WITH_NO_PERCENT_SIGN) || (do_next == 2)) /* 2 means stop */ { status = 1; break; } else if (do_next == 1) /* 1 means MDI */ { fprintf(stderr, "starting MDI\n"); interpret_from_keyboard(block_delete, print_stack); fprintf(stderr, "continue program? y/n =>"); fgets(line, LINELEN, stdin); if (line[0] != 'y') { status = 1; break; } else continue; } else /* if do_next == 0 -- 0 means continue */ continue; } status = interp_execute(); if ((status != INTERP_OK) && (status != INTERP_EXIT) && (status != INTERP_EXECUTE_FINISH)) { report_error(status, print_stack); status = 1; if (do_next == 1) /* 1 means MDI */ { fprintf(stderr, "starting MDI\n"); interpret_from_keyboard(block_delete, print_stack); fprintf(stderr, "continue program? y/n =>"); fgets(line, LINELEN, stdin); if (line[0] != 'y') break; } else if (do_next == 2) /* 2 means stop */ break; } else if (status == INTERP_EXIT) return 0; } return ((status == 1) ? 1 : 0);}/************************************************************************//* read_tool_fileReturned Value: int If any of the following errors occur, this returns 1. Otherwise, it returns 0. 1. The file named by the user cannot be opened. 2. No blank line is found. 3. A line of data cannot be read. 4. A tool slot number is less than 1 or >= _tool_maxSide Effects: Values in the tool table of the machine setup are changed, as specified in the file.Called By: mainTool File Format-----------------Everything above the first blank line is read and ignored, so any sortof header material may be used.Everything after the first blank line should be data. Each line ofdata should have four or more items separated by white space. The fourrequired items are slot, tool id, tool length offset, and tool diameter.Other items might be the holder id and tool description, but these areoptional and will not be read. Here is a sample line:20 1419 4.299 1.0 1 inch carbide end millThe tool_table is indexed by slot number.*/int read_tool_file( /* ARGUMENTS */ char * tool_file_name) /* name of tool file */{ FILE * tool_file_port; char buffer[1000]; int slot; int tool_id; double zoffset, xoffset, frontangle, backangle; double diameter; int orientation; if (tool_file_name[0] == 0) /* ask for name if given name is empty string */ { fprintf(stderr, "name of tool file => "); fgets(buffer, 1000, stdin); buffer[strlen(buffer) - 1] = 0; tool_file_port = fopen(buffer, "r"); } else tool_file_port = fopen(tool_file_name, "r"); if (tool_file_port == NULL) { fprintf(stderr, "Cannot open %s\n", ((tool_file_name[0] == 0) ? buffer : tool_file_name)); return 1; } for(;;) /* read and discard header, checking for blank line */ { if (fgets(buffer, 1000, tool_file_port) == NULL) { fprintf(stderr, "Bad tool file format\n"); return 1; } else if (buffer[0] == '\n') break; } for (slot = 0; slot < _tool_max; slot++) /* initialize */ { _tools[slot].id = -1; _tools[slot].zoffset = 0.; _tools[slot].diameter = 0.; _tools[slot].xoffset = 0.; _tools[slot].frontangle = 0.; _tools[slot].backangle = 0.; _tools[slot].orientation = 0; } for (; (fgets(buffer, 1000, tool_file_port) != NULL); ) { if (sscanf(buffer, "%d %d %lf %lf %lf %lf %lf %d", &slot, &tool_id, &zoffset, &xoffset, &diameter, &frontangle, &backangle, &orientation) == 8 && slot >= 0 && slot < _tool_max) { _tools[slot].id = tool_id;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -