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

Show parent comments

2

u/naught-me Oct 24 '24 edited Oct 24 '24

>> more dynamic control based on real-time information

This is hard. The rest is easy enough, but, generally, on the other side of g-code is black box. You tell it what to do, but not when, and even though you tell it "exactly", it has algorithms that have to approximate things on some level (or runs fancy acceleration algorithms). And, it gives you back, like, 20 or 50 updates per second about where it is, with no data about what lines it's actually executed, because those lines get destructively ingested (when they go from parse->plan, the machine no longer remembers anything about the command that was parsed, and only has the plan to work with, on all GRBL-based systems, at least).

LinuxCNC is the only easy way I've found.

1

u/anfroholic Oct 24 '24

It is hard, I'll give you that, but there's a lot of fun things I've done and want to make it easier for others to do the same.

I have never really cared for LinuxCNC for whatever reason. Currently I have FluidNC as the motion core. As long as you keep the planner topped off, things are just peachy. Bart Dring, the guy who manages and writes most for it is one of my friends.

2

u/naught-me Oct 24 '24

FluidNC has the limitations I mentioned regarding realtime stuff. I spent a fair amount of time trying to get realtime reactivity out of FluidNC specifically. There's some things it's very bad at, like realtime direction changes (you have to keep the queue full, and if you're moving fast, that's a fair amount of motion before you can even begin to change directions). And, it's got the black-box thing going on, which you can mostly get your way around, if 20hz or 50 hz updates are good enough for your application, and if you aren't re-crossing the same points in quick succession (like, you only get position out, so, if one position does not map exactly to one point in time, you know where the machine is but not when it is).

Would be happy to see what you come up with, though. I think there's a lot of use-cases for a Python wrapper over FluidNC - its limitations probably won't matter for most projects.

1

u/anfroholic Oct 24 '24

I am aware of some of those limitations with FluidNC, some of the problems come from it not being happy if the planner isn't full, which by it's very nature means that your python/gcode is running in the future, with no good way to dump the buffer if you want to change course. (I think we're saying the same thing?) And no that's not something I'm going for at the moment and most of what I have planned, this limitation is probably just fine.

Thanks for the input and I'll keep making some updates if you wall will have me.

2

u/naught-me Oct 24 '24

Yeah, I'd at least try whatever. I sometimes think about going back to work on my FluidNC Python wrapper. I ended up using generators for mine, too. I'll think about publishing my work. I've probably got something that would be useful to you.

→ More replies (0)