gnea\grbl-Mega  1.0f
Source Code Documentation ( Internal Workings )
gcode.c
Go to the documentation of this file.
1 /*
2  gcode.c - rs274/ngc parser.
3  Part of Grbl
4 
5  Copyright (c) 2011-2016 Sungeun K. Jeon for Gnea Research LLC
6  Copyright (c) 2009-2011 Simen Svale Skogsrud
7 
8  Grbl is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  Grbl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Grbl. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "grbl.h"
25 // arbitrary value, and some GUIs may require more. So we increased it based on a max safe
26 // value when converting a float (7.2 digit precision)s to an integer.
27 #define MAX_LINE_NUMBER 10000000
28 #define MAX_TOOL_NUMBER 255
29 
30 #define AXIS_COMMAND_NONE 0
31 #define AXIS_COMMAND_NON_MODAL 1
32 #define AXIS_COMMAND_MOTION_MODE 2
33 #define AXIS_COMMAND_TOOL_LENGTH_OFFSET 3
34 parser_state_t gc_state;
37 
38 #define FAIL(status) return(status);
39 
40 
41 void gc_init()
42 {
43  memset(&gc_state, 0, sizeof(parser_state_t));
44 
45 // Load default G54 coordinate system.
48  }
49 }
50 
52 // limit pull-off routines.
54 {
56 }
57 
59 // characters and signed floating point values (no whitespace). Comments and block delete
60 // characters have been removed. In this function, all units and positions are converted and
61 // exported to grbl's internal functions in terms of (mm, mm/min) and absolute machine
62 // coordinates, respectively.
63 uint8_t gc_execute_line(char *line)
64 {
72  memset(&gc_block, 0, sizeof(parser_block_t));
73  memcpy(&gc_block.modal,&gc_state.modal,sizeof(gc_modal_t));
74 
75  uint8_t axis_command = AXIS_COMMAND_NONE;
76  uint8_t axis_0, axis_1, axis_linear;
77  uint8_t coord_select = 0;
78 
79 // Initialize bitflag tracking variables for axis indices compatible operations.
80  uint8_t axis_words = 0;
81  uint8_t ijk_words = 0;
82 
83 // Initialize command and value words and parser flags variables.
84  uint16_t command_words = 0;
85  uint16_t value_words = 0;
86  uint8_t gc_parser_flags = GC_PARSER_NONE;
87 
88 // Determine if the line is a jogging motion or a normal g-code block.
89  if (line[0] == '$') {
90 // Set G1 and G94 enforced modes to ensure accurate error checks.
92  gc_parser_flags |= GC_PARSER_JOG_MOTION;
93  gc_block.modal.motion = MOTION_MODE_LINEAR;
95  gc_block.values.n = JOG_LINE_NUMBER;
96  }
97 
104  uint8_t word_bit;
105  uint8_t char_counter;
106  char letter;
107  float value;
108  uint8_t int_value = 0;
109  uint16_t mantissa = 0;
110  if (gc_parser_flags & GC_PARSER_JOG_MOTION) { char_counter = 3; }
111  else { char_counter = 0; }
112 
113  while (line[char_counter] != 0) {
114 
115 // Import the next g-code word, expecting a letter followed by a value. Otherwise, error out.
116  letter = line[char_counter];
117  if((letter < 'A') || (letter > 'Z')) { FAIL(STATUS_EXPECTED_COMMAND_LETTER); }
118  char_counter++;
119  if (!read_float(line, &char_counter, &value)) { FAIL(STATUS_BAD_NUMBER_FORMAT); }
120 
121 // Convert values to smaller uint8 significand and mantissa values for parsing this word.
122 //
124 // accurate than the NIST gcode requirement of x10 when used for commands, but not quite
125 // accurate enough for value words that require integers to within 0.0001. This should be
126 // a good enough comprimise and catch most all non-integer errors. To make it compliant,
127 // we would simply need to change the mantissa to int16, but this add compiled flash space.
128 // Maybe update this later.
129  int_value = trunc(value);
130  mantissa = round(100*(value - int_value));
131 //
133 
134 // Check if the g-code word is supported or errors due to modal group violations or has
135 // been repeated in the g-code block. If ok, update the command or record its value.
136  switch(letter) {
137 
141 
206 
213 
227 
241 
280 
286 
291 
306 
321 
325 
352 
354 
363 
378 
382 
400 
407 
416 
432 
437 
441 
449 
487 
493 
503 
522 
547 
589 
598 
620 
639 
643 
648 
660 
693 
756 
782 
829 
857 
865 
871 
894 
958 
968 
989 
993 
1016 
1060 
1076 
1139 
#define GC_PARSER_JOG_MOTION
Definition: gcode.h:151
void system_convert_array_steps_to_mpos(float *position, int32_t *steps)
Updates a machine 'position' array based on the 'step' array sent.
Definition: system.c:295
uint8_t read_float(char *line, uint8_t *char_counter, float *float_ptr)
Extracts a floating point value from a string. The following code is based loosely on...
Definition: nuts_bolts.c:35
void gc_sync_position()
Sets g-code parser position in mm. Input in steps. Called by the system abort and hard...
Definition: gcode.c:53
uint8_t gc_execute_line(char *line)
Executes one line of 0-terminated G-Code. The line is assumed to contain only uppercase.
Definition: gcode.c:63
void report_status_message(uint8_t status_code)
Handles the primary confirmation protocol response for streaming interfaces and human-feedback.
Definition: report.c:110
uint8_t settings_read_coord_data(uint8_t coord_select, float *coord_data)
Read selected coordinate data from EEPROM. Updates pointed coord_data value.
Definition: settings.c:160
gc_modal_t modal
Definition: gcode.h:196
#define STATUS_BAD_NUMBER_FORMAT
Definition: report.h:25
#define STATUS_EXPECTED_COMMAND_LETTER
Definition: report.h:24
int32_t n
Line number.
Definition: gcode.h:185
#define MOTION_MODE_LINEAR
G1 (Do not alter value)
Definition: gcode.h:70
gc_values_t values
Definition: gcode.h:217
NOTE: When this struct is zeroed, the above defines set the defaults for the system.
Definition: gcode.h:162
#define JOG_LINE_NUMBER
System motion line numbers must be zero.
Definition: jog.h:26
uint8_t feed_rate
{G93,G94}
Definition: gcode.h:164
gc_modal_t modal
Definition: gcode.h:216
void gc_init()
Initialize the parser.
Definition: gcode.c:41
uint8_t coord_select
{G54,G55,G56,G57,G58,G59}
Definition: gcode.h:173
#define FEED_RATE_MODE_UNITS_PER_MIN
Modal Group G5: Feed rate mode.
Definition: gcode.h:95
#define FAIL(status)
Definition: gcode.c:38
int32_t sys_position[N_AXIS]
NOTE: These position variables may need to be declared as volatiles, if problems arise.
Definition: system.h:130
memset(pl_data, 0, sizeof(plan_line_data_t))
Zero pl_data struct.
memcpy(block_coord_system, gc_state.coord_system, sizeof(gc_state.coord_system))
#define GC_PARSER_NONE
Define gcode parser flags for handling special cases.
Definition: gcode.h:150
parser_block_t gc_block
Definition: gcode.c:36
float coord_system[N_AXIS]
Current work coordinate system (G54+). Stores offset from absolute machine.
Definition: gcode.h:205
float position[N_AXIS]
Where the interpreter considers the tool to be at this point in the code.
Definition: gcode.h:203
uint8_t motion
{G0,G1,G2,G3,G38.2,G80}
Definition: gcode.h:163
#define STATUS_SETTING_READ_FAIL
Definition: report.h:30
parser_state_t gc_state
Declare gc extern struct.
Definition: gcode.c:35
#define AXIS_COMMAND_NONE
Definition: gcode.c:30