r/hobbycnc • • Oct 24 '24

Python > G-Code. Change my mind

Hello /r/hobbycnc ,

I'm creating a programming paradigm intended for running robots with Python; specifically non-cartesian CNC machines like robot arms with bespoke toolheads and functions.

I'm looking for what kind of syntactic sugar you all like (or would like to see).

a typical line of gcode has the structure of G1 | X25 | Y15 | F1200 G1 X25 Y15 F1200 function name {space} kwarg {space} kwarg basically a function followed by nothing or any number of kwargs typically denoted by a single letter

The trouble is that pyton does not execute code in a way needed for these machines. So let's build up from the ground. Fundamentally I think we want something similar.

G1 X25 F1200 means that we want the machine to move (in a linear fashion) to the position 25 units on the axis 'X' at a speed of 1200 units/time.

My proposed new version would be {"cmd": "move.linear", "x": 25, "feed": 1200} There are a lot of reasons to have something that's serializable, most notably flow control.

Question 1: @Machinists if you saw all these commands would you be able to read them?

line1: compiled code, think of this like assembly
line2: python function

{"cmd": "move.rapid", "x": 0}  ==> G00 X0
move.rapid(x=0) ==> G00 X0

{"cmd": "spindle", "arg": True} ==> M03
spindle.on() ==> M03

{"cmd": "spindle", "state": True, "direction": "cw", "speed": 5000} ==> M03 S5000
spindle.on(direction='cw', speed=5000) ==> M03 S5000

{"cmd": "sleep", "seconds": 1.5} => G04 P1500
sleep(1.5) => G04 P1500

Question 2: Machinists know what Gcode is, does this need a new name? Since Gcode is already so ill-defined, is it ok to call it Gcode?

{"cmd": "move.rapid", "x": 0}
{"cmd": "spindle", "arg": True}
{"cmd": "sleep", "seconds": 1.5}

Fundamentally we have bits of json separated by '\n' newline characters.


Advanced questions

In the above examples imagine we have a carefully crafted namespace and the engine running above code does type introspection and can eval() if desired

Referenced before was the need for flow control. ie. we can't just execute an entire program, sometimes our script needs to wait, like when a machine needs to move from one place to another, that takes time.

Question 3: Generators appear to be the best execution scheme. If there are other methods that I am unaware of please let me know.

Fundamentally at the top level this leaves us with robot.run(iterable)

Now we've seen what the compiled version looks like, how about some python code

Somewhat contrived Robot Bartender Example

def get_ice():
    yield move(**ice_location)
    yield ice_machine.vend()
    yield move(**after_ice)

def pour(ounces: float): 
    yield move(**pour_location)
    yield move(**bottle_pour)
    yield wait_until(scale, lambda val: val > ounces) 
    yield move(**bottle_up)

def bottle_handler(bottle: str, operation: str):
    if operation == 'pick':
        yield move(**bottles[bottle])
        yield gripper.close()
        yield move(**bottle_rack_clear)
    elif operation == 'place':
        yield move(**bottles[bottle])
        yield gripper.open()
        yield move(**bottle_rack_clear)

def make_drink(bottle: str, ice: bool):
    if ice:
        yield from get_ice()
    yield from bottle_handler(bottle, 'pick')
    yield from pour(5)
    yield from bottle_handler(bottle, 'place')

Play with a browser based version of this code here

https://floe.evezor.com/pyscript/671616e5fd297e7dcanv?core=mpy

Question 4: Do you understand yield and yield from? How does writing scripts as generator objects make you feel?

Thank you so much for your time!

4 Upvotes

66 comments sorted by

View all comments

3

u/alcaron Oct 24 '24

I keep reading your comments and still just. You don’t give any specific examples of what “novel” things you are trying to do that in any way are the “fault” of gcode. The command interpreter on the machine side and the physical nature of what you are doing is why gcode is the way it is and ultimately an entire language just to do some niche things feels like you could just write an interpreter that does what you want.

0

u/anfroholic Oct 24 '24

You don’t give any specific examples of what “novel” things you are trying to do that in any way are the “fault” of gcode.

I've tried pretty hard to knock gcode too hard. But in some of the ways people could talk about assembly in the same way. Does that mean that no one should ever write assembly? Not necessarily, and there is a benefit to knowing it. But i would say that the bartender example is fairly novel, we've got branching, we've got realtime controls waiting for sensors and macros. Nothing there is incredibly difficult, but expressing it in gcode would not be fun, if at all possible without writing your own firmware.

you could just write an interpreter that does what you want.

Or still not and just lean on python.

Thanks for taking the time to comment, I think I'm starting to get a feel for where people are and how they interpret different aspects.

2

u/alcaron Oct 25 '24

I feel like you think you are a bit more clever than you seem. Once again you haven't given any example of what gcode CANT do, or. why Python is better.

"Or still not and just lean on python." why create an ENTIRELY new command language? You have not once said "because you can't do X with gcode". You mention "programmatic' stuff and macros, but one of MANY examples why that is not true is klipper.

You can EASILY utilize an array of sensors, you can EASILY introduce logic for "programmatic" stuff.

I don't really know how to change your mind when the premise is kind of invalid and your ego seems to be in the way of you seeing that.

0

u/Straight-Subject-770 May 13 '26

Reading through some of these a year+ after it almost feels like the mill lathe paradigm easy to start hard to master harder to start easier to master. It feels he hit a limit wall on gcode and is trying to use another code to get past it but for what actual reason.