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

26

u/madbobmcjim Oct 24 '24

You probably need more preamble here, what problem are you trying to solve? A lot of us here probably don't interact with gcode much unless something goes wrong, I usually just take the output of Fusion and pass it to my machine. So I'm not sure the problem you're working on.

Gcode isn't exactly pretty or easy to understand without a lookup table, but it works. Is your goal to make it more readable?

Also, a lot of your questions are coding questions, and so you're probably going to have a limited number of people here who can help.

16

u/metisdesigns Oct 24 '24

I'm not sure the OP understands what Gcode is used for.

It's simply the instructions for the machine to follow, in as simple a fashion as possible to make it as quick to process as possible.

It sounds like they want to be able to read it as a human.

0

u/anfroholic Oct 24 '24

I am a journeyman machinist. I've also built quite a few machines and run others. I would consider my native programming language to be gcode.

Quick to process is not really an issue as very powerful computers are so cheap now. (there's more power in my watch than my first machines)

Functionally gcode and my proposed codes are both serialized commands that can then be interpreted to do any number of things. Gcode, works great for what it's meant for but falls a little flat when you try to do more programmatic things.

And readability is important. Prusa of 3d printers just came out with a new binary gcode format, aimed more specifically at file size, but will just state again, that looking at code that comes out of CAM tools or slicers, is mildly out of scope for what I'm referring to here and is what most machinists are used to (and for good reason!)

Thank you for taking the time to give an insightful comment as I get better at describing this!

-1

u/anfroholic Oct 24 '24

Thanks for your insight.

You probably need more preamble here,

I didn't want this post to get too long, so probably missed a few of the most key points.

A lot of us here probably don't interact with gcode much unless something goes wrong, I usually just take the output of Fusion and pass it to my machine. So I'm not sure the problem you're working on.

This is very true as well. My first career was as a machinist and I'd also consider my first programming language to be gcode. Having given some public talks lately, I do realize there is a kind of disconnect at some kind of fundamental level. At be beginning of my talk, I lamented the fact that I learned gcode in college without any need for it in my professional life, then towards the end of the talk, I was referring to code that is very much like gcode and actually writing it.

To go from something like Fusion to a machine, in all actuality there is little reason for a person to get involved and even look at it unless there is a problem.

Gcode isn't exactly pretty or easy to understand without a lookup table, but it works. Is your goal to make it more readable?

I also gave a talk about gcode some years ago and made a joke that one day someone suggested putting the punch cards into a computer and they decided to call it gcode... And no one changed it since.

For robust toolchains, what we have is fine, but completely falls on its face when you try to do something novel.

Having taught coding to a few people about coding, I emphasize readability is what good coding is. Furthermore, gcode is limiting in it's forms of expression, even for something simple like making a toolchange macro from my other comment on this thread. Not to mention there is no conceivable way to write something like "Look up some things on the internet, then write me a post-it with the current time and the weather outside"

Also, a lot of your questions are coding questions, and so you're probably going to have a limited number of people here who can help.

Yeah, I do know that. You'd actually be pretty surprised how wide the gulf is on both sides (programmers, machinists) as gcode and languages like python inherently function is such wildly different ways.

Hope I was able to give you a little more understanding of the problem and thank you so much for your comment. As I keep talking to people I think I'm finally starting to wrap my head around ways to explain it better. Thank you

6

u/pr06lefs Oct 24 '24

Yeah gcode is deterministic, while you're looking for something that is more of a robotics language. A gcode program will do the same thing every time, barring crashes, while you're looking for something dynamic that will move based on sensor input, like 'find where a glass is on the table and then pick it up'.

That's a legit need, but also in some ways a downside for machinists. Having gcode do the same thing on any well calibrated machine is a feature, not a bug. Machinists want consistency, not dynamic control.

You may even want to go as far as having control logic at a finer level than gcode allows - instead of simple movements, altering servo acceleration and velocity during a move, or not having 'moves' at all but responding frame by frame to inputs with motor setting changes.

2

u/anfroholic Oct 24 '24

I pretty much agree with you on all points. And am for sure looking for things that are more of a robotics language.

As things move forward, I think it's going to become increasingly valuable to be able to run/ program/operate machines of all types. Machinists have a special place in my heart, as it was my first career. I would like to see something that is palatable enough that machinists could and would cross that divide, as in actuality they're perfectly positioned to make a jump like that.

I don't think I answered your question exactly, but hope it's close enough.

Thank you for your time and comments!

2

u/Onphone_irl Oct 24 '24

so I was planning on doing a project with an arduino and a cnc shield. I've yet to research how a cnc shield works, but it seems like maybe this is an application. Python runs on raspberry pi, seems like it would be better suited. many things can be done using x,y and z motion and there are cnc shields available...if you're making it seemless to pass code to cnc you're onto something.

2

u/anfroholic Oct 24 '24

Different hardware, but effectively yes. Very close to that.

2

u/start3ch Oct 24 '24

Why not just add a more readable interface to Gcode?

Ultimately you need to send commands in a way the machine understands, and currently that’s just commanding positions, or predetermined routines that the machine understands like curved paths.

You could also just add your own Gcodes to the machine firmware, to do more complicated types of movements.

2

u/anfroholic Oct 24 '24

Some things are more nuanced and is more than just making gcode more readable. Some of it is to allow people to be more expressive.

With a robot arm, you could have a move like move.linear(x=10) but the planner will see a ton of little small moves that interpolate a straight line because robots have no concept of a straight line, to say nothing of general IO. There are many gcode firmwares out there, but when you don't know a whole lot they can seem very daunting, and it's not clear right away how to easily add functionality without unintentionally breaking something else.

Thank you for your insight!

3

u/madbobmcjim Oct 24 '24

OK, I think I'm getting a little closer to what you're talking about. For my machine the macros I've written are a mix of gcode and C#, as that's what the system is built in and there's no standardized way of doing it.

I'd love to integrate my CNC with MQTT so it can appear in HomeAssistant...

But I'm still not sure what changing gcode does here, when I've written the C# macros it literally calls Gcode for machine movements, so why is that a problem? Or I guess, why is that enough of a problem that it needs a while new way of programatically expressing machine motion?

1

u/anfroholic Oct 24 '24

Wow! Yes! I love all those ideas!

I'm still trying to find ways to describe this effectively, so I hope I get this right.

The changing of gcode has a little more to do with what I'm going to refer to as the 'lifetime' of your code/program/macro.

Gcode doesn't really have any notion of return and branching. (I do know there are counter examples, but none of them are very user friendly).

To channel some of the other comments in this post, it would be nice to have a single place where you can write programmatic expressions, and motion like commands without context switching. And to state some things a little more clearly because you seem more well versed, any of the calls that just look like json are what a serialized/compiled line of gcode would look like, but in a handwritten script, you'd likely only see function calls.

I love your ideas and thank you again!!