From 1ef5a4547900c94f89d5958f403094340b154cfd Mon Sep 17 00:00:00 2001 From: Sonny Jeon Date: Wed, 2 Jul 2014 08:39:19 -0600 Subject: [PATCH] Minor bug fixes and updates. Line number tracking. - Line number tracking was getting truncated at 255, since it was using wrong variable type. Fixed it with a trunc(). - Increased the max number line allowed by Grbl to 9999999 from the g-code standard 99999. The latter seems to be an arbitrary number, so we are allowing larger ones for at least one known use case scenario. - Created a new test directory to contain some testing g-code to proof the firmware. Only got started with one test case so far. More will be inserted as needed. - Some other commenting updates to clarify certain aspects of the code. --- gcode.c | 29 ++++++++++++++++++----------- nuts_bolts.c | 1 + protocol.c | 16 ++++++++++++---- test/test.py | 25 +++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 15 deletions(-) create mode 100644 test/test.py diff --git a/gcode.c b/gcode.c index 5214fc4..a4a8e70 100644 --- a/gcode.c +++ b/gcode.c @@ -29,7 +29,10 @@ #include "probe.h" #include "report.h" -#define MAX_LINE_NUMBER 99999 +// NOTE: Max line number is defined by the g-code standard to be 99999. It seems to be an +// arbitrary value, and some GUIs may require more. So we increased it based on a max safe +// value when converting a float (7.2 digit precision)s to an integer. +#define MAX_LINE_NUMBER 9999999 #define AXIS_COMMAND_NONE 0 #define AXIS_COMMAND_NON_MODAL 1 @@ -91,8 +94,8 @@ uint8_t gc_execute_line(char *line) memcpy(&gc_block.modal,&gc_state.modal,sizeof(gc_modal_t)); // Copy current modes uint8_t axis_explicit = AXIS_COMMAND_NONE; uint8_t axis_0, axis_1, axis_linear; - float coordinate_data[N_AXIS]; - float parameter_data[N_AXIS]; + float coordinate_data[N_AXIS]; // Multi-use variable to store coordinate data for execution + float parameter_data[N_AXIS]; // Multi-use variable to store parameter data for execution // Initialize bitflag tracking variables for axis indices compatible operations. uint8_t axis_words = 0; // XYZ tracking @@ -305,7 +308,7 @@ uint8_t gc_execute_line(char *line) case 'J': word_bit = WORD_J; gc_block.values.ijk[Y_AXIS] = value; ijk_words |= (1< 0.005) { - if (target_r > 0.5) { FAIL(STATUS_GCODE_INVALID_TARGET); } // [Arc definition error] - if (target_r > 0.001*gc_block.values.r) { FAIL(STATUS_GCODE_INVALID_TARGET); } // [Arc definition error] + // Compute difference between current location and target radii for final error-checks. + float delta_r = fabs(target_r-gc_block.values.r); + if (delta_r > 0.005) { + if (delta_r > 0.5) { FAIL(STATUS_GCODE_INVALID_TARGET); } // [Arc definition error] > 0.5mm + if (delta_r > (0.001*gc_block.values.r)) { FAIL(STATUS_GCODE_INVALID_TARGET); } // [Arc definition error] > 0.005mm AND 0.1% radius } } break; diff --git a/nuts_bolts.c b/nuts_bolts.c index dff34d4..a8a159c 100644 --- a/nuts_bolts.c +++ b/nuts_bolts.c @@ -151,6 +151,7 @@ uint8_t get_direction_mask(uint8_t axis_idx) return(axis_mask); } + float hypot_f(float x, float y) { return(sqrt(x*x + y*y)); diff --git a/protocol.c b/protocol.c index c97829b..788115b 100644 --- a/protocol.c +++ b/protocol.c @@ -61,7 +61,7 @@ static void protocol_execute_line(char *line) /* - GRBL MAIN LOOP: + GRBL PRIMARY LOOP: */ void protocol_main_loop() { @@ -81,9 +81,9 @@ void protocol_main_loop() system_execute_startup(line); // Execute startup script. } - // ------------------------------------------------------------------------------ - // Main loop! Upon a system abort, this exits back to main() to reset the system. - // ------------------------------------------------------------------------------ + // --------------------------------------------------------------------------------- + // Primary loop! Upon a system abort, this exits back to main() to reset the system. + // --------------------------------------------------------------------------------- uint8_t iscomment = false; uint8_t char_counter = 0; @@ -92,6 +92,14 @@ void protocol_main_loop() // Process one line of incoming serial data, as the data becomes available. Performs an // initial filtering by removing spaces and comments and capitalizing all letters. + + // NOTE: While comment, spaces, and block delete(if supported) handling should technically + // be done in the g-code parser, doing it here helps compress the incoming data into Grbl's + // line buffer, which is limited in size. The g-code standard actually states a line can't + // exceed 256 characters, but the Arduino Uno does not have the memory space for this. + // With a better processor, it would be very easy to pull this initial parsing out as a + // seperate task to be shared by the g-code parser and Grbl's system commands. + while((c = serial_read()) != SERIAL_NO_DATA) { if ((c == '\n') || (c == '\r')) { // End of line reached line[char_counter] = 0; // Set string termination character. diff --git a/test/test.py b/test/test.py new file mode 100644 index 0000000..41c845c --- /dev/null +++ b/test/test.py @@ -0,0 +1,25 @@ +import random +import serial +import time +ser = serial.Serial('/dev/tty.usbmodem24111', 115200, timeout=0.001) +time.sleep(1) +outstanding = 0 +data = '' +while True: + time.sleep(0.1) + data += ser.read() + pos = data.find('\n') + if pos == -1: + line = '' + else: + line = data[0:pos + 1] + data = data[pos + 1:] + if line == '' and outstanding < 3: + while outstanding < 3: + ser.write("G0 Z%0.3f\n" % (0.01 * (random.random() - 0.5))) + #ser.write("M3\n") + outstanding += 1 + continue + if line == 'ok\r\n': + outstanding -= 1 + print outstanding, repr(line.rstrip()) \ No newline at end of file