CNC Plotter - Error in reading G-codes

Hello, trying to make an arduino code for a (virtual) CNC plotter which returns absolute X-Y coordinates based on G-code inputs. My linemoves in all directions and arcs all give the correct output by themselves, but having trouble with stringing multiple G-codes together.

This is my project developed on WOKWI

This is the g-code set which I would like the plotter output for

G01 X10.0 F1000 #
G03 X5.0 Y5.0 I5.0 J10.0 #
G01 Y5.0 #
G03 X-5.0 Y5.0 I-5.0 J0.0 #
G01 X-10.0 #
G01 X-15

It runs the first 3 lines just fine but stops at the 4th (the second arc)

Can anyone see what the issue is?

(ARCMOVE code still needs to have stepper instructions added but ignore for now)

1 Like

Well the way to analyse this is to add a lot of serial debug-output to make visible what is going in in your code.

I tested the second line
G03 X5.0 Y5.0 I5.0 J10.0 #

does not work too. So as soon as you combine more than one cooordinate the parser does not work

If you want to do this as an exercise to learn very good.
If you just need the functionality itself
use grbl or an existing g-code-parses

For your developping-process add one small thing at a time.

Go back to a much earlier code-version that is parsing a single coordinate
and then add the second coordinate including serial debug-output in the parsing section

Me personal I use these three macros for debugging
one that prints each time it is called
one that print only after a specified time-interval
one that prints once if an integer-variable has changed its value

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *

// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope

#define dbgi(myFixedText, variableName,timeInterval) \
  do { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  } while (false);
// usage: dbgi("2:my fixed text",myVariable,1000);
// myVariable can be any variable or expression that is defined in scope
// third parameter is the time in milliseconds that must pass by until the next time a
// Serial.print is executed
// end of macros dbg and dbgi
// print only once when value has changed
#define dbgc(myFixedText, variableName) \
  do { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  } while (false);
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *

best regards Stefan

1 Like

Apologies this G code is incorrect, should be:
G01 X10.0 F1000 #
G03 X5.0 Y5.0 I0 J5.0 #
G01 Y5.0 #
G03 X-5.0 Y5.0 I-5.0 J0.0 #
G01 X-10.0 #
G01 X-15

      case  '#':
      case '\n':   // return key has been hit. Output a move 

It's going to execute each line twice. Once when it hits the comment marker and again when it hit the newline.

void rapid (float x, float y)
{
  xabs = x;    //Updating absolute co-ordinates
  yabs = y;
}

It's doing drawing relative but rapid motion absolute?

The function rapid refers to the changing of absolute co-ordinates but without actually drawing anything, as if you lifted your pen off the paper.

And noted thanks, but still stalling after 3rd line, is there a variable that i need to reset to 0 somewhere but can't see it?

Updated, does work by entering all codes individually but still stopping if entering all together

I've never seen Gcode with a # at the end of a line but no actual comment - remove them.

CAMotics is a free Gcode simulator you could use to check the code. I tried it on your code and got:

ERROR:Expected number, reference, or bracked expression, found EOL_TOKEN
ERROR: At: C:/Users/John Haine/Desktop/example.txt:2:1
ERROR:Expected number, reference, or bracked expression, found EOL_TOKEN
ERROR: At: C:/Users/John Haine/Desktop/example.txt:3:1
ERROR:Expected number, reference, or bracked expression, found EOL_TOKEN
ERROR: At: C:/Users/John Haine/Desktop/example.txt:4:1
ERROR:Expected number, reference, or bracked expression, found EOL_TOKEN
ERROR: At: C:/Users/John Haine/Desktop/example.txt:5:1
ERROR:Expected number, reference, or bracked expression, found EOL_TOKEN
ERROR: At: C:/Users/John Haine/Desktop/example.txt:6:1
WARNING:C:/Users/John Haine/Desktop/example.txt:6:Cutting move but no current tool, selecting tool 1
ERROR:Cutting move with zero feed
ERROR:While executing GCode block:G1 X-15
ERROR: At: C:/Users/John Haine/Desktop/example.txt:6
ERROR:Caused by: Cutting move with zero feed

Right, I have tried your code in both Camotics and Mach3 now. Both give errors in your original but both work perfectly if you remove the # at the end of the line. That's your problem.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.