Skip to content

Gcode

This module provides utilities for G-code based devices.

ATTRIBUTE DESCRIPTION
MOVEMENT_BUFFER

buffer time after movement

TYPE: int

MOVEMENT_TIMEOUT

timeout for movement

TYPE: int

CLASS DESCRIPTION
GCodeDevice

Protocol for G-code devices

GCode

Interface to control a G-code based device.

Documentation last updated: 2025-02-22

GCode

GCode(
    port: str,
    *,
    device_type_name: str = "GRBL",
    baudrate: int = 115200,
    movement_buffer: int | None = None,
    movement_timeout: int | None = None,
    verbose: bool = False,
    **kwargs,
)

Bases: Mover

GCode provides an interface to control a G-code based device. Refer to https://reprap.org/wiki/G-code for more information on G-code commands.

ATTRIBUTE DESCRIPTION
movement_buffer

buffer time after movement

TYPE: int

movement_timeout

timeout for movement

TYPE: int

connection_details

connection details for the device

TYPE: dict

device

device object that communicates with physical tool

TYPE: Device

flags

flags for the class

TYPE: SimpleNamespace[str, bool]

is_busy

whether the device is busy

TYPE: bool

is_connected

whether the device is connected

TYPE: bool

verbose

verbosity of class

TYPE: bool

deck

Deck object for workspace

TYPE: Deck

workspace

workspace bounding box

TYPE: BoundingBox

safe_height

safe height in terms of robot coordinate system

TYPE: float

saved_positions

dictionary of saved positions

TYPE: dict

speed

travel speed of robot

TYPE: float

speed_factor

fraction of maximum travel speed of robot

TYPE: float

speed_max

maximum speed of robot in mm/min

TYPE: float

robot_position

current position of the robot

TYPE: Position

home_position

home position of the robot in terms of robot coordinate system

TYPE: Position

tool_offset

tool offset from robot to end effector

TYPE: Position

calibrated_offset

calibrated offset from robot to work position

TYPE: Position

scale

factor to scale the basis vectors by

TYPE: float

tool_position

robot position of the tool end effector

TYPE: Position

work_position

work position of the robot

TYPE: Position

worktool_position

work position of the tool end effector

TYPE: Position

position

work position of the tool end effector; alias for worktool_position

TYPE: Position

max_accels

maximum accelerations of the robot

TYPE: ndarray

max_speeds

maximum speeds of the robot

TYPE: ndarray

METHOD DESCRIPTION
query

query the device

toggleCoolantValve

toggle the coolant valve

connect

connect to the device

disconnect

disconnect from the device

resetFlags

reset all flags to class attribute _default_flags

shutdown

shutdown procedure for tool

enterZone

enter a zone on the deck

exitZone

exit the current zone on the deck

halt

halt robot movement

home

make the robot go home

isFeasible

checks and returns whether the target coordinates is feasible

loadDeck

load Deck layout object to mover

loadDeckFromDict

load Deck layout object from dictionary

loadDeckFromFile

load Deck layout object from file

move

move the robot in a specific axis by a specific value

moveBy

move the robot by target direction

moveTo

move the robot to target position

moveToSafeHeight

move the robot to safe height

moveRobotTo

move the robot to target position

moveToolTo

move the tool end effector to target position

reset

reset the robot

rotate

rotate the robot in a specific axis by a specific value

rotateBy

rotate the robot by target direction

rotateTo

rotate the robot to target orientation

rotateRobotTo

rotate the robot to target orientation

rotateToolTo

rotate the tool end effector to target orientation

safeMoveTo

safe version of moveTo by moving to safe height first

setSafeHeight

set safe height for robot

setSpeedFactor

set the speed factor of the robot

setToolOffset

set the tool offset of the robot

showWorkspace

show the workspace of the robot

updateRobotPosition

update the robot position

transformRobotToTool

transform robot coordinates to tool coordinates

transformRobotToWork

transform robot coordinates to work coordinates

transformToolToRobot

transform tool coordinates to robot coordinates

transformWorkToRobot

transform work coordinates to robot coordinates

calibrate

calibrate the internal and external coordinate systems

Initialize GCode class

PARAMETER DESCRIPTION
port

serial port address

TYPE: str

device_type_name

name of the device type. Defaults to 'GRBL'.

TYPE: str DEFAULT: 'GRBL'

baudrate

baudrate of the device. Defaults to 115200.

TYPE: int DEFAULT: 115200

movement_buffer

buffer time after movement. Defaults to None.

TYPE: int DEFAULT: None

movement_timeout

timeout for movement. Defaults to None.

TYPE: int DEFAULT: None

verbose

verbosity of class. Defaults to False.

TYPE: bool DEFAULT: False

max_accels property

max_accels: ndarray

Maximum accelerations of the robot

max_speeds property

max_speeds: ndarray

Maximum speeds of the robot

connect

connect()

Connect to the device

halt

halt() -> Position

Halt robot movement

home

home(
    axis: str | None = None, *, timeout: int | None = None
) -> bool

Make the robot go home

PARAMETER DESCRIPTION
axis

axis to home. Defaults to None.

TYPE: str DEFAULT: None

timeout

timeout for movement. Defaults to None.

TYPE: int DEFAULT: None

RETURNS DESCRIPTION
bool

whether the robot successfully homed

TYPE: bool

moveBy

moveBy(
    by: Sequence[float] | Position | ndarray,
    speed_factor: float | None = None,
    *,
    jog: bool = False,
    rapid: bool = False,
    robot: bool = False,
) -> Position

Move the robot by target direction

PARAMETER DESCRIPTION
by

target direction

TYPE: Sequence[float] | Position | ndarray

speed_factor

speed factor. Defaults to None.

TYPE: float DEFAULT: None

jog

jog movement. Defaults to False.

TYPE: bool DEFAULT: False

rapid

rapid movement. Defaults to False.

TYPE: bool DEFAULT: False

robot

robot coordinates. Defaults to False.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
Position

new tool/robot position

TYPE: Position

moveTo

moveTo(
    to: Sequence[float] | Position | ndarray,
    speed_factor: float | None = None,
    *,
    jog: bool = False,
    rapid: bool = False,
    robot: bool = False,
) -> Position

Move the robot to target position

PARAMETER DESCRIPTION
to

target position

TYPE: Sequence[float] | Position | ndarray

speed_factor

speed factor. Defaults to None.

TYPE: float DEFAULT: None

jog

jog movement. Defaults to False.

TYPE: bool DEFAULT: False

rapid

rapid movement. Defaults to False.

TYPE: bool DEFAULT: False

robot

robot coordinates. Defaults to False.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
Position

new tool/robot position

TYPE: Position

query

query(
    data: Any,
    multi_out: bool = True,
    *,
    timeout: int | float = 1,
    jog: bool = False,
    wait: bool = False,
) -> Any

Query the device

PARAMETER DESCRIPTION
data

data to query

TYPE: Any

multi_out

lines of data. Defaults to True.

TYPE: bool DEFAULT: True

timeout

timeout for movement. Defaults to None.

TYPE: int DEFAULT: 1

jog

jog movement. Defaults to False.

TYPE: bool DEFAULT: False

wait

wait for movement. Defaults to False.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
Any

response from device

TYPE: Any

reset

reset()

Reset the robot

setSpeedFactor

setSpeedFactor(
    speed_factor: float | None = None,
    *,
    persist: bool = True,
)

Set the speed factor of the robot

PARAMETER DESCRIPTION
speed_factor

speed factor. Defaults to None.

TYPE: float DEFAULT: None

persist

persist speed factor. Defaults to True.

TYPE: bool DEFAULT: True

toggleCoolantValve

toggleCoolantValve(on: bool)

Toggle the coolant valve

PARAMETER DESCRIPTION
on

whether to turn the coolant valve on

TYPE: bool

GCodeDevice

Bases: Protocol

Protocol for G-code devices

clear

clear()

Clear the input and output buffers

connect

connect()

Connect to the device

disconnect

disconnect()

Disconnect from the device

getSettings

getSettings() -> dict[str, int | float | str]

Check the settings of the device

halt

halt() -> Position

Halt the device

home

home(axis: str | None = None, **kwargs) -> bool

Home the device

query

query(
    data: Any, multi_out: bool = True, **kwargs
) -> list[str] | None

Query the device

read

read() -> str

Read data from the device

readAll

readAll() -> list[str]

Read all data from the device

setSpeedFactor

setSpeedFactor(speed_factor: float | int, **kwargs)

Set the speed factor of the device

write

write(data: str) -> bool

Write data to the device