CAD CAM EDM DRO - Yahoo Group Archive

Re: [turbocnc] Re: High-level g-code language?

Posted by Codesuidae
on 2005-05-04 09:21:20 UTC
Howdy,

I liked the idea of using an existing scripting language, and it occurs
to me that it doesnt' really matter what it is, for this purpose they
are pretty much all equivalent. Since I'm a Delphi (pascal) programmer,
I've decided to do some testing with a Delphi based scripting language.
Not so much because I think its the best choice for the task, but
because it is the tool with which I am most proficent, and I want to get
a proof-of-principle project going to see if I think a full
implimentation will be worth persuing.

So far I have a program that supports most of the Delphi language
structures (for, while, repeat, if, try-finally, try-except, numerous
data types, record structures, classes, large chunks of the VCL library
functions, etc) with which I can write functions and procedures to
output gcode. It turns out that the work to set up a scripting engine
to do this is pretty trivial. All it really needs is a few functions
(i.e. RapidMove(), Move(), Arc(), etc) that translate their parameters
into gcode. This core set of functions can be written in pretty much
any scripting language, so it would be easy to translate to anyones
prefered engine.

My proof-of-principle system works as follows. The user runs the
program and is presented with a text input area and a gcode output
window. The user enters pascal/delphi code into the text input and uses
a number of functions such as Move(), RapidMove() and SetFeedRate() to
output gcode. When the script is run the gcode appears in the gcode
output window. This output can be saved to a file and processed on a
simulator or machine. If I continue with this I'll include some basic
simulation functions in the script processor as well. In the script
window the code is colorized and formatted, and the script processor
provides popup hints listing the procedures and functions that are
available, along with the required parameters, info about syntax errors
and such.

As a test I've written a short script to draw a sine curve. This is the
program I entered to do this:

// Main program
// Note that this is just a proof-of-principle and as such it
// doesn't do any translation from the coord system of the x-y
// axis used into the machine coord system. The calculated axis
// values are output directly.
begin
try
EmitGcode( '', 'This is a test program to demonstrate' );
EmitGcode( '', 'the capabilities of the script language' );

// Various constants, most affect what the output extents will be
const XMin : Float = 0.0; // Leftmost position on X
axis
const XMax : Float = 360.0; // Rightmost position on
X axis
const Step : Float = 0.5; // Resolution per step on X
const RapidHeight : Float = 0.1;
const CutHeight : Float = -0.1;

// Variables to hold the x, y and z positions
var x : Float;
var y : Float;
var z : Float;

SetFeedRate( 100.0, 'Set feed rate in mm' );

// Start by moving to the leftmost axis position
x := XMin;
y := 5 * Sin( DegToRad(x) );
z := RapidHeight;
RapidMove( X, Y, Z, 'Leftmost X axis position' );

// Loop and generate the output path
repeat
// Calculate the y axis position for the current X value
y := 5 * Sin( DegToRad(x) );

// Update the machine position.
// Instead of a real coord system mapping, this
// just compresses X by a factor of 10
Move( X/10, Y, CutHeight );

// incriment x to the next value
x := x + Step;
until x > XMax;

// Withdraw from the material
RapidMove( X/10, Y, RapidHeight, 'Finished' );
except
// Catch any errors and display the text of the error in a message box
on e: exception do begin
ShowMessage( 'There was an error while processing the
script.'#13#10+
'The following is the text of the error
message:'#13#10#13#10+
e.message );
end;
end;
end;

The procedures to output the gcode are as follows. These are included
as a seperate script module so that they can be modified and added to as
required by the user. The key bit is the 'EmitGCode' routine, which
takes a couple of text strings and puts them into the ouput file. This
can be pretty much anything, so additional gcode commands are trivial to
add. A clever programmer could even use this as a sort of meta-language
to output gcode subroutines.

procedure Move( X, Y, Z : Float; Comment: string = ''; Rapid: Boolean =
False );
begin
// todo: fix the FormatFloat routine
// todo: options to only affect the desired axis
try
var Code : string;
if Rapid then
Code := 'G00 '
else
Code := 'G01 ';

EmitGCode( Code +
'X'+ FloatToText( X ) + ' ' +
'Y'+ FloatToText( Y ) + ' ' +
'Z'+ FloatToText( Z ) + ' ',
Comment );
except
on e: exception do begin
// Pre-pend the name of the routine where the error occurs
e.Message := 'Error in Move: '#13#10 + e.message;
raise;
end;
end;
end;

procedure RapidMove( X, Y, Z : Float; Comment: string ='' );
begin
try
Move( X, Y, Z, Comment, True );
except
on e: exception do begin
// Pre-pend the name of the routine where the error occurs
e.Message := 'Error in RapidMove: '#13#10 + e.message;
raise;
end;
end;
end;

procedure SetFeedRate( Rate: Float; Comment: string = '' );
begin
EmitGcode( 'F'+FloatToStr( Rate ), Comment );
end;

The output from the program is just a series of movement commands:

; This is a test program to
demonstrate
; the capabilities of the
script language
F100 ; Set feed rate in mm
G00 X0.000 Y0.000 Z0.100 ; Leftmost X axis position
G01 X0.000 Y0.000 Z-0.100 ;
G01 X0.050 Y0.044 Z-0.100 ;
G01 X0.100 Y0.087 Z-0.100 ;
G01 X0.150 Y0.131 Z-0.100 ;
G01 X0.200 Y0.174 Z-0.100 ;
G01 X0.250 Y0.218 Z-0.100 ;
[snip]

Loaded into a CNC simulator they draw one full cycle of a sine wave
about 36mm long and 5mm tall.

So, anyway, thats the idea, anybody think its worth persuing? As above
I'd want to include some basic simulation capabilities so you can see
what paths the code is actually generating. There are classes available
for loading graphics files, so a tool that generates gcode for milling a
picture based on pixel color would be very easy. Tracing of fonts and
such would be possible as well, although I haven't tried that. It has
the ability to load external gcode, so code produced by other programs
could easily be incorporated. Output to SVG format or possibly DXF
would not be difficult. Lots of possibilities.

Comments, ideas, suggestions?
cs

Discussion Thread

Codesuidae 2005-05-04 09:21:20 UTC Re: [turbocnc] Re: High-level g-code language? Alex Holden 2005-05-04 10:55:54 UTC Re: [CAD_CAM_EDM_DRO] Re: [turbocnc] Re: High-level g-code language? Codesuidae 2005-05-04 11:22:39 UTC Re: [CAD_CAM_EDM_DRO] Re: [turbocnc] Re: High-level g-code language? Dhiren Shah 2005-05-04 11:38:32 UTC RE: [CAD_CAM_EDM_DRO] Re: [turbocnc] Re: High-level g-code language? Alan Marconett 2005-05-04 12:54:26 UTC RE: [CAD_CAM_EDM_DRO] Re: [turbocnc] Re: High-level g-code language? Alex Holden 2005-05-04 13:17:25 UTC Re: [CAD_CAM_EDM_DRO] Re: [turbocnc] Re: High-level g-code language? andyolney 2005-05-04 17:21:12 UTC [turbocnc] Re: High-level g-code language? jymmm 2005-05-05 12:46:59 UTC [turbocnc] Re: High-level g-code language? Dhiren Shah 2005-05-06 13:15:49 UTC RE: [CAD_CAM_EDM_DRO] [turbocnc] Re: High-level g-code language? alipavsky@i... 2005-05-06 13:33:28 UTC RE: [CAD_CAM_EDM_DRO] [turbocnc] Re: High-level g-code language? Bob Muse 2005-05-06 15:02:59 UTC Re: [CAD_CAM_EDM_DRO] [turbocnc] Re: High-level g-code language? Codesuidae 2005-05-08 22:24:08 UTC Re: High-level g-code language? washcomp 2005-05-09 06:05:13 UTC Re: High-level g-code language? Dhiren Shah 2005-05-09 06:51:37 UTC RE: [CAD_CAM_EDM_DRO] [turbocnc] Re: High-level g-code language? Dhiren Shah 2005-05-09 06:52:02 UTC RE: [CAD_CAM_EDM_DRO] [turbocnc] Re: High-level g-code language? Codesuidae 2005-05-09 06:56:16 UTC Re: [CAD_CAM_EDM_DRO] [turbocnc] Re: High-level g-code language? Codesuidae 2005-05-09 07:37:41 UTC Re: [CAD_CAM_EDM_DRO] Re: High-level g-code language? Dhiren Shah 2005-05-09 08:07:17 UTC RE: [CAD_CAM_EDM_DRO] [turbocnc] Re: High-level g-code language?