G# macro-programming

NightRoad
Posts: 8
Joined: Fri Jun 03, 2016 12:43 am
Location: Australia

G# macro-programming

Post by NightRoad » Sat Jun 04, 2016 8:27 am

I was surprised that GRBL controllers do not support standard macro-programming language similar to LinuxCNC. I heard that bCNC introduced Python-like programming, but because it is rather non-standard this limits application area and user acceptance.

I absolutely understand that it is impossible to add this functionality to GRBL itself. GRBL code is ingenious enough to fit G-code interpreter, route planner and CNC controller into 32KB of Arduino memory. But I cannot see why it cannot be done in the external software controllers.

So to fix this discrepancy I wrote a software library which converts macro-programming commands (highly compatible with LinuxCNC, differences are minimal) into a stream of corresponding plain G-code lines. I called it G# language for simplicity, but in effect it has the same syntax, described in G Code Overview and O Codes. Variables, conditions, loops, subroutines, active comments - all included.

The library has quite a simple interface to be incorporated into existing controllers. The code is in github and is under permissive BSD license. You can get it from here: https://github.com/NRSoft/GSharp. I'm happy to support and enhance the library if there is any interest. Currently it's been tested under Ubuntu Linux only, but it's generic enough to be used in other OS platforms.

In addition to the library there is an example of how to use it. After simple compilation it can work as a stand-alone converter from G# program into plain G-code file. (It is command-line application, doesn't have any GUI).

I would be happy to get any comments, bug reports and suggestions on how to improve the library.

WillAdams
Posts: 8489
Joined: Mon Apr 09, 2012 6:11 pm
Location: Pennsylvania --- south of the Turnpike, East of US-15
Contact:

Re: G# macro-programming

Post by WillAdams » Sat Jun 04, 2016 10:26 am

You have just made a bunch of people very, very happy.

For those who aren’t familiar w/ it, G-code programming makes possible a number of designs / styles which would be very difficult to model / draw in 3D — esp. if one has a lathe / 4th axis:

http://www.sawmillcreek.org/showthread. ... e-CNC-work

Looking forward to this being bundled w/ pretty much every G-code sender by default. It moves my making a 4th axis way up the priority stack.
Shapeoko 3XL #0006 w/Makita RT0701 Router w/0.125″ and ¼″ Elaire precision collets
Nomad 883 Pro #596 (bamboo)

NightRoad
Posts: 8
Joined: Fri Jun 03, 2016 12:43 am
Location: Australia

Re: G# macro-programming

Post by NightRoad » Sun Jun 05, 2016 6:55 am

You are right, I haven't thought about the 4th axis. It would definitely be beneficial there. I have a simple 3-axis milling machine controlled from Ubuntu, and it doesn't seem that there is much of sophisticated software for Linux. Hopefully G-code senders could add this new functionality.

But even now you can use the provided command-line converter to generate plain G-code program and then load it into any grbl controller. It's just an extra manual step before execution.

Would be happy to help if you have any questions!

Will Winder
Posts: 435
Joined: Fri Apr 20, 2012 2:40 pm
Location: Hudson, MA
Contact:

Re: G# macro-programming

Post by Will Winder » Thu Jun 09, 2016 2:46 pm

This looks really cool! One feature that would be useful for sender integration is if you had a mode to attach some metadata (such as line number) to each generated line of gcode.

This would allow a gcode editor to highlight a block of G# script then in a visualizer highlight which lines of generated code correspond to the highlighted lines. This is something I've been planning for the "Platform" version of UGS for some time. Most of the pieces are in place to support it, but so far I haven't been ready to take on a macro expansion project.

It would be great if you setup a CI server to build and test the different platforms. I've used Jenkins for portable C++ applications in the past and am happy to lend a hand setting that up if you're interested. cloudbees.com offers free Jenkins hosting for open source programs which is what I use for UGS.
ShapeOko #367: Dual-Y drive, Belt on outside, 1000mm Y-Axis, DW660 Spindle, Nema-23 X/Y motors.

Primary developer on Universal Gcode Sender.

NightRoad
Posts: 8
Joined: Fri Jun 03, 2016 12:43 am
Location: Australia

Re: G# macro-programming

Post by NightRoad » Thu Jun 09, 2016 11:57 pm

Thanks Will, great to know that the library may find its way into the real CNC control software!

Currently the library provides the line number which has been returned in the previous step. A single execution step runs through all control lines and stops after interpreting the first valid G-code line (or the line containing an active comment). You have to call GetCurrentLineNumber() method after each step to get the corresponding line number if you want to highlight it. I'm not quite sure what you mean by "block" of lines. I can probably return a list of all line numbers that where interpreted during the last step, but it may become quite messy in displaying (especially with sub calls). But let me know if this is what you need and I can add this method.

If you need more metadata, I'd suggest you create an issue in github - it'd be good to be opened for other people to add their suggestions.

I had a patchy experience with Jenkins before - I know it's very useful for bigger projects or where several developers have to collaborate. To be honest I'm not sure if I need it at this stage. Hopefully the project may get bigger and then I'd be happy to get some of your knowledge on how to set it up. Currently I'm more concerned about completeness of my unit tests, especially for detection of incorrect syntax. But thanks for the offer of help anyway!

GrblGru
Posts: 228
Joined: Sat Nov 01, 2014 5:44 pm

Re: G# macro-programming

Post by GrblGru » Thu Jun 16, 2016 6:23 pm

Congratulations on your project. Great work.
Your description sounds very, very interesting to me. I've written a free 3D simulation program for windows os, that also provides controller functionality for GRBL and TinyG.
You can see it in this forum: viewtopic.php?f=6&t=4710&start=110
It would be a great improvement for my program, if I could use your g#2g program to handle standard macros.
Unfortunately I'm not familiar with the type of license. So my question to you is following:
Is it allowed that I use your g#2g.exe and also distribute it together with my installation ?
My workflow: Only a 2D-editor and GrblGru, that's it
Free download from https://GrblGru.com
Youtube: https://www.youtube.com/channel/UC4_AHk ... 61ZW4qBHWA

CastIrony
Posts: 650
Joined: Mon Jul 21, 2014 10:21 pm
Location: Portland, OR

Re: G# macro-programming

Post by CastIrony » Thu Jun 16, 2016 7:47 pm

GrblGru wrote:Congratulations on your project. Great work.
Your description sounds very, very interesting to me. I've written a free 3D simulation program for windows os, that also provides controller functionality for GRBL and TinyG.
You can see it in this forum: viewtopic.php?f=6&t=4710&start=110
It would be a great improvement for my program, if I could use your g#2g program to handle standard macros.
Unfortunately I'm not familiar with the type of license. So my question to you is following:
Is it allowed that I use your g#2g.exe and also distribute it together with my installation ?
Yes you can distribute this application with yours. You can also incorporate its source code into the code of yours. The only requirement is that you mention where it came from in a way that does not imply endorsement.

More details in the LICENSE file.
Shapeoko 3 #192 - Complete! Needs limit switches.

NightRoad
Posts: 8
Joined: Fri Jun 03, 2016 12:43 am
Location: Australia

Re: G# macro-programming

Post by NightRoad » Fri Jun 17, 2016 9:11 am

Thanks CastIrony, I couldn't have said it better myself.

Indeed the license is quite permissive. GrblGru, you are even allowed to change the code and keep the changes to yourself. But obviously it would be better for the community if critical bug fixes and useful features find their way back to the repository.

Good luck with your project! If you need any technical support about G# library, please let me know.

NightRoad
Posts: 8
Joined: Fri Jun 03, 2016 12:43 am
Location: Australia

Re: G# macro-programming

Post by NightRoad » Fri Jun 17, 2016 9:21 am

Also I just added support for building the library under Windows (using free edition of Visual Studio 2015). It's under "win" directory of the main repository.

As a bonus there is a GUI version of G# to Gcode converter with both 32 and 64 bit executables. Please let me know if window controls are mispositioned in any way - these things happen under certain Windows configurations.
NR

GrblGru
Posts: 228
Joined: Sat Nov 01, 2014 5:44 pm

Re: G# macro-programming

Post by GrblGru » Fri Jun 17, 2016 6:18 pm

Thanks CastIrony, thanks NightRoad for the clear answers. This is exactly, what I was hoping for. :)

My program is developed with c#, but I've successfully created the g#2g project with gcc V5.1
So my first idea is to to use only your exe. Thank you for offering your help. I will report, if something goes wrong.
My workflow: Only a 2D-editor and GrblGru, that's it
Free download from https://GrblGru.com
Youtube: https://www.youtube.com/channel/UC4_AHk ... 61ZW4qBHWA

Post Reply