G-Code and Mendrel

As described in the RAMPS post, the Mendrel is controlled by Marlin firmware. When the RAMPS receives g-code commands, these commands are sent to motors which in turn cut out our object. In this post, I will give a brief description of g-code and sending commands to the Mendrel.

What is G-Code?

G-code is a very basic, semi-standardized machine language. To demonstrate what the previous sentence actually means, if I want to have machine A move its cutting bit to the position X 30 mm, Y 50mm, I would send the following command:

G1 X30 Y50

If I had a completely different type of machine, machine B, and sent it the same command, it too should move to position X 30 mm, Y 50 mm. Hence, the same code should theoretically perform the same on different machines.

Marlin is designed around 3-D printing, so many of the g-code commands it uses are geared towards the use of a plastic extruder. Because the Mendrel cuts, the only g-code commands used are movement and coordinate commands. These are:

Command    Description Example
G0 Rapid Move G0 X30 Y30 and G0 Z10
G1 Controlled Move G0 X70 and G0 Z0
G28 Move to home position / zero position G28
G90 Set to absolute positioning (default) If current position is (30, 40) and G90 is active the command G1 X50 will move the machine to (50, 40)
G91 Set to relative positioning If current position is (30, 40) and G91 is active the command G1 X50 will move the machine to (80, 40)

There are more commands accepted by Marlin, but knowing these will serve 99.9% of your needs for the Marlin. For the full list see RepRap wiki on G-Code.

It’s Alive!

RAMPS receives input via serial. In order to communicate with RAMPS we need to know its port and baud rate. The RAMPS board runs at /dev/ttyACM0 (may not be 0 if something has taken that spot) with a 112500 baud rate.

To move the board, you can use Arduino’s serial monitor function, as described in the RAMPS post. To test some movements, I sent the following to Mendrel:

G28 ; Home (move to X:0 Y:0 Z:0)
F200.00000 ; Feed Rate (move at 200 mm / sec)
G1 X50 ; Move to position X:50 mm
G1 X80
G1 X80
G1 X50
G1 X50
G28 ; Home

The result:

That minor extra movement during homing is a result of how homing is implemented:

[G28] causes the RepRap machine to move back to its X, Y and Z zero endstops, a process known as “homing”. It does so accelerating, so as to get there fast. But when it arrives it backs off by 1 mm in each direction slowly, then moves back slowly to the stop. This ensures more accurate positioning. [source]

Mendel Controller

Marlin and Arduino Mega can buffer a reasonable number of g-code commands, which is why we can send a few of them through the serial monitor without issue. With actual models, the number of commands can run into the thousands, so all the commands cannot be buffered on the microcontroller.

Marlin works by a simple send-acknowledge process where if you send a buffered g-code command (movements and homing), the Marlin will reply with “ok” if it was read correctly; otherwise, it will reply with “rs” for resend. I implemented a simple python script using the multiprocessing and serial packages to facilitate communication with the Marlin and RAMPS. The code for it can be found at https://bitbucket.org/raw/cnc_mendel/src. I did not bother implementing checksum or dealing with “rs” as I found from experience I never really needed it. Future work will correct this.

Additionally, as discussed in the PyCam post, PyCAM generates cutting g-code from STL files — which is great. A slight problem with this g-code is that Marlin does not like some of the formatting of commands. As a result, g-code directly from PyCAM will not work. I wrote a simple Python script that converts the PyCAM g-code so that it works in Marlin. It also adds some extra homing commands for better automation. The details of what it fixes are pretty minor, but its existence should be noted.

  1. Mendel Background
  2. Hardware
  3. Electronics
  4. RAMPS
  5. PyCAM
  6. G-Code
  7. Cutting

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s