How to create “Spindle Delay” script file in PlanetCNC TNG software

With PlanetCNC TNG software user can create or customise g-code procedures (e.g. for M3/M5 g-codes, M6 g-code) using script files.

Script files are located in PlanetCNC TNG installation folder in scripts folder.

If your settings file is named e.g.”MyCNCRouter.settings”, then your corresponding scripts folder should be named MyCNCRouter.scripts. This way you can link multiple script files to corresponding setting files.

This tutorial will describe how create script file for short delay after spindle is turned ON.

 

To create and modify M3 g-code script please follow steps below:

1. Open PlanetCNC TNG software installation folder. Locate scripts folder and open it.

2. Create new text file and name it: Machine.UserDefined_M3.gcode

3. Open script file with text editor.

4. Add M3 and G04 g-codes.

Scipt file includes g-code lines:

M3
G04 P5

 

Now after each time we turn spindle ON, software waits 5 seconds before it continues with program execution.

 

You can read more on M3 and G04 g-codes here:
https://planet-cnc.com/controlling-spindle-coolant-planetcnc-tng-software/
https://planet-cnc.com/using-g04-g-code-planetcnc-tng-software/

How to control PlanetCNC TNG from your own software

Command line arguments

PlanetCNC TNG accepts commands from command line. Only one software instance can run at same time. When you run second instance it will simply pass its arguments to running instance. Running instance will process these arguments and execute command.

Here are some examples (Windows, 64bit):

1. Open command prompt.
2. Go to PlanetCNC TNG installation folder. Usually “C:Program Files (x86)PlanetCNC”.
3. Type PlanetCNC64.exe. Software will start.
4. Type PlanetCNC64.exe gcode “G01 X10G01 Y10”. You will notice that two g-code lines are now loaded.
5. Type PlanetCNC64.exe start. Machine will start and g-code will be executed.

Named pipes

More advanced option is to use named pipes. Named pipes is simple cross-platform interprocess communication mechanism. It is possible to use named pipes in almost all programming languages.

PlanetCNC TNG must be running otherwise it will not receive command. If you do not want to show main window you can start software with “hide” command.

Here is source code of an example (Windows, C++):
PipeExample

 

List of supported commands

Both options described above use same commands. Here is a list and brief explanation.

show Shows main window
hide Hides main window
exit Exits program
estop Toggles EStop
start Performs Start
stop Performs Stop
pause Performs Pause
isestop Returns 1 if EStop is active, 0 otherwise
ispause Returns 1 if Pause is active, 0 otherwise
open "filename" Opens GCode from file
gcode "program" Opens GCode from string
close Closes opened GCode
mdi "command" Executes MDI command
param name=value Sets parameter value
param name Returns parameter value
posabs Returns absolute position for all axes
posabsx Returns absolute position for X axis
posabsy Returns absolute position for Y axis
posabsz Returns absolute position for Z axis
posabsa Returns absolute position for A axis
posabsb Returns absolute position for B axis
posabsc Returns absolute position for C axis
posabsu Returns absolute position for U axis
posabsv Returns absolute position for V axis
posabsw Returns absolute position for W axis
pos Returns position for all axes
posx Returns position for X axis
posy Returns position for Y axis
posz Returns position for Z axis
posa Returns position for A axis
posb Returns position for B axis
posc Returns position for C axis
posu Returns position for U axis
posv Returns position for V axis
posw Returns position for W axis
speed Returns current speed
accel Returns current acceleration
spindle Returns current spindle RPM
input Returns value of INPUT 
input1 Returns value of INPUT 1 pin
input2 Returns value of INPUT 2 pin
input3 Returns value of INPUT 3 pin
input4 Returns value of INPUT 4 pin
input5 Returns value of INPUT 5 pin
input6 Returns value of INPUT 6 pin
input7 Returns value of INPUT 7 pin
input8 Returns value of INPUT 8 pin
jog Returns value of JOG
jog1 Returns value of JOG 1 pin
jog2 Returns value of JOG 2 pin
jog3 Returns value of JOG 3 pin
jog4 Returns value of JOG 4 pin
jog5 Returns value of JOG 5 pin
jog6 Returns value of JOG 6 pin
jog7 Returns value of JOG 7 pin
jog8 Returns value of JOG 8 pin
joga1 Returns value of JOG A1 pin
joga2 Returns value of JOG A2 pin
jogs Returns value of JOG S pin
jogpot Returns value of JOG POT
limit Returns value of LIMIT
limit1 Returns value of LIMIT 1 pin
limit2 Returns value of LIMIT 2 pin
limit3 Returns value of LIMIT 3 pin
limit4 Returns value of LIMIT 4 pin
limit5 Returns value of LIMIT 5 pin
limit6 Returns value of LIMIT 6 pin
limit7 Returns value of LIMIT 7 pin
limit8 Returns value of LIMIT 8 pin
limit9 Returns value of LIMIT 9 pin
output Returns value of OUTPUT
output1 Returns value of OUTPUT 1 pin
output2 Returns value of OUTPUT 2 pin
output3 Returns value of OUTPUT 3 pin
output4 Returns value of OUTPUT 4 pin
output5 Returns value of OUTPUT 5 pin
output6 Returns value of OUTPUT 6 pin
output7 Returns value of OUTPUT 7 pin
output8 Returns value of OUTPUT 8 pin
aux1 Returns value of AUX 1 pin
aux2 Returns value of AUX 2 pin
aux3 Returns value of AUX 3 pin
aux4 Returns value of AUX 4 pin
buffer Returns amount of free buffer 
bufferpercent Returns percent of buffer utilization
linecount Returns number of lines in GCode program
line Returns line number currently executing

 

Starting program from g-code

You can also start your own program from gcode. Optionally you can wait for program to finish before g-code continues or you can pass parameters.

Start myprogram.exe:
(exec,myprogram.exe) 

Start myprogram.exe and send current X and Y coordinates:
(exec,myprogram.exe #<_x> #<_y>) 

Start myprogram.exe, send current X and Y coordinates and wait for program to finish:
(execwait,myprogram.exe #<_x> #<_y>) 

Make sure that myprogram.exe is added to whitelist in settings otherwise TNG will refuse to execute it.

Hint: This external program can use named pipes to set some g-code parameters. TNG will use these parameters when g-code execution will continue.

How to create custom tabs in PlanetCNC TNG software

With PlanetCNC TNG user can create his own custom tabs.

Tabs are displayed on main user interface and can include any parameter used by software whether its tool offset value, number of g-code line or absolute position of machine. By default, software will display tabs “Speed” and “IO”.

User can create maximum of  5 state files, meaning, you can display up to 5 custom tabs. File with extension .state1 creates first tab file, .state2 creates second tab file all the way up to .state5 file.

To create new custom tab go to your PlanetCNC TNG installation folder and create new text file and change its name to PlanetCNC64 or PlanetCNC32 and its extension to “state1“: PlanetCNC64.state1 or PlanetCNC32.state1

It is worth mentioning that the text before .state must be the same as name of your settings file. So, if your settings file is named: MyCNCRouter.settings, then your corresponding .state file should be named MyCNCRouter.state1. This way you can link multiple state files to corresponding setting files.

Now open this file and copy/paste bottom example text for an easy start:

If we look parameter “_abs_x 3” you can see that at the end we add number 3. Since this parameter is X axis absolute position value, this number means number of decimal places.

If parameter uses Boolean logic you add instead of number a letter B.

Now that we created  state file and we configured it, “My new tab” tab should appear after we restart PlanetCNC TNG software:

If we click “My new tab” tab we can see the parameters displayed:

Parameter 1 displays X axis absolute position
Parameter 2 displays Total run time of g-code file
Parameter 3 displays SPU value of X axis

We can change names of parameters to make them more understandable:

Now parameter names are much easier to understand:

You can download state file with all parameters here: All parameter .state file

Using the G04 g-code with PlanetCNC TNG software

In machining it is sometimes necessary to stop the motion of all axes for a given amount of time (for example to let the spindle come up to speed or wait for the coolant to start flowing). This is called dwell. It’s activated with the G04 command. The time is specified using a P-word and a number seconds (the number is a float so fractions of a second can be used).
For example: G04 P3.7

A common place to use the G04 dwell command is when starting a spindle of a machine. Because it can’t accelerate instantly to the desired speed we need to allow it some time to speed up before starting to cut the material. An example of code that waits 5 seconds after starting the spindle at 10000 RPM is here:
M03 S10000
G04 P5

Using G02 and G03 g-codes with PlanetCNC TNG software

Another two g-codes used to specify machine movements are G02 and G03. They move the machine in an arc, in contrast to G00 and G01 which move the machine in a straight line.

G02 moves the machine in a clockwise arc and G03 moves the machine in a counterclockwise arc.
To use these codes we need to specify the coordinates to which the machine will travel, the offsets to the center of the arc or the arc radius and optionally the number of additional turns the machine will make while performing the move. The speed of these movements is also defined by the F-word (feed rate).

For example: Starting from X0 Y0 Z0, we would like to round a 90 degree corner using a counterclockwise arc to X50 Y50 Z0. To do this we will use the command G03 X50 Y50. But this command isn’t complete yet and the program would signal an error if we tried to use it like that. We still need to specify the center of the arc. This is done by specifying offsets from the starting position or with the deprecated method of specifying the radius.

The offsets are simply the distance from the starting position to the center of the arc. The offsets are separated into components for each axis. The center also needs to be equidistant from both the starting and the destination point. Otherwise the arc won’t be part of a circle and the controller will signal an error accordingly.

As we said, the offsets are the distance the machine would need to move along each axis separately to get to the center. In our case (starting from the origin) this would be 50 units along the Y axis. This also means that the radius of the circle will be 50 units (the radius equals the distance from the center to the origin or destination). Now we need to specify the offsets to the machine. To do this we use I, J and K-words for the X, Y and Z axes accordingly. When we put all this together we get the final code that looks like this: G03 X50 Y50 J50.

G03

Controlling the Spindle and Coolant with PlanetCNC TNG Software

A fundamental operation in CNC machining is controlling the spindle. It is controlled in g-code using M3, M4 and M5 commands.
M03 will start the spindle turning clockwise.
M04 will start the spindle turning counterclockwise.
Both commands turn on the spindle at the speed determined separately by the S-word. It is used to set the RPM at which the spindle will turn when turned on.
M05 will stop the spindle regardless of direction.

Other than controlling the spindle, the M03 command can also turn on a plasma cutter, laser, water jet or even a hot wire for cutting polystyrene foam. Their power is likewise set using the S-word.

When machining some materials, most often metals, it is sometimes required to use coolant. It can be supplied to the tool and on the work in different ways. There are three commands for coolant control.
M07 will turn on the mist coolant supply.
M08 will turn on the flood coolant supply.
M09 will turn both coolant supplies off.

As with M03, these commands can be used to control something else, as they just turn an output on the CNC USB controller on or off. For example one of the codes could be used to control the dust collection vacuum for a woodworking machine.

The M07, M08 and M03 commands can also be activated manually using the buttons from top toolbar:
CoolantFlood CoolantMist SpindleM3

 

Currently active M-codes will also show up in the machine state display.

M03M07M08M05M09

How to measure work position with movable sensor in PlanetCNC TNG software

1. Reference your machine using Homing procedure(Machine/Home):
How to set Homing procedure in PlanetCNC TNG software

2. Connect movable sensor (further in text referenced as sensor) to one of the inputs (Input connector) of controller.

InputWiringMk34

InputWiringMk3

3. In settings select either Sensor 1 or Sensor 2 (File/Settings/Input/Sensors). It doesn’t matter which one you choose.

From drop down menu select input to which you connected your sensor.

SensorInput2

4. Activate sensor by hand and under IO tab of main display check if designated input gets properly triggered.

SensorIO

For safety reasons jog your machine (it can be in any direction) and see if machine stops when you activate sensor.

5. In settings set:

Probe speed(File/Settings/Program Options/Probe speed): This is the speed at which Z axis will descend to measure Z axis work position.

Sensor Thickness(File/Settings/Program Options/Probe speed): If you use copper board as a movable sensor, then value of this parameter is the thickness of the copper board( usually 1.6mm).

Thickness

 6. Start “Work Position Measure” procedure(Machine/Work Position/Measure). You can also use the “Work Position Measure” button on the left toolbar:

Button

7. When “Work Position Measure” procedure is executed, default sequence will be:

Machine will at current machine position descend towards sensor at probe speed(File/Settings/Program Options/Probe Speed). When sensor is activated, machine will stop and ascend to Z height from which we started “Work Position Measure” procedure.

Z axis work position value on surface of material should now be 0.

You have configured “Work Position Measure” procedure and you should be able to use it with your movable sensor.

If you would like to edit “Work Position Measure” procedure to fit your needs or to have more in depth look at the procedure itself, you can do that by opening the “Work Position Measure” procedure script file.

 

 

8. You can create and access “Work Position Measure” script file  in two ways:

a.) In settings (File/Settings/Program Options/Scripts/Work position) you can click buttons Generate and Edit. With Generate button you create default file and with Edit button you can open it and make any modifications of the script g-code

GenerateEdit

 

b.) When you start “Work Position Measure” procedure for the first time(for example via button), Machine.Work_Position.Measure.gcode default file will be created. Open your PlanetCNC TNG software installation folder and locate folder: “.Scripts”

Find “Machine.Work_Position.Measure.gcode” file and open it with text editor:

ScriptG-code

9. Description of parameters and g-codes(in order of appearance in script g-code):

M73:
This g-code saves states of of all modal g-codes currently used by software. When Machine.Work_Position.Measure.gcode procedure is executed in its entirety, all states will be restored to their previous values.

#3 = #<_abs_z>: Stores value of current Z axis position as parameter #3.

G90:
With G90 g-code we set absolute mode of motion.

 

F#<_speed_probe>:
Speed at which machine descends to measure tool. Set with Probe Speed in settings(File/Settings/Program Options/Probe Speed). This value can be set directly with new F value. In such case you should delete the “#<_speed_probe>” line of code.

G38.2 Z-100000:
This g-code moves selected axis in selected direction. G38.2 g-code uses parameter X(-/+)Value,Y(-/+)Value or Z(-/+)Value which define to which coordinate value machine should move and in what direction. When probe activation occurs over the time of machine travel, machine will stop.

G92 Z#<_sensor_thick>:

When movable sensor is activated, we set current work position as thickness of sensor. Here comes in great help g-code G92.

G53 G0 Z#3:

Machine moves in absolute coordinates to previously saved Z axis position.

 

 

 

 

 

How to measure tool offset using fixed tool sensor in PlanetCNC TNG software

1. Reference your machine using Homing procedure(Machine/Home):
How to set Homing procedure in PlanetCNC TNG software

2. Mount your fixed tool sensor(further in text referenced as sensor) on machine table.

3. Connect sensor to one of the inputs (Input connector) of controller.
InputWiringMk34

InputWiringMk3

4. In settings select either Sensor 1 or Sensor 2(File/Settings/Input/Sensors). It doesn’t matter which one you choose.

From drop down menu select input to which you connected your sensor.

SensorInput1

5. Activate sensor by hand and under IO tab of main display check if designated input gets properly triggered.
SensorInput1_IO

For safety reasons jog your machine (it can be in any direction) and see if machine stops when you activate sensor.

6. Jog your machine to sensor position, so that tool is right above the centre of fixed tool sensor.
For Sensor Position(File/Settings/Program Options/Sensor Position)insert X,Y motor values that can be seen under Motors tab of coordinate display.
Z value of sensor position is usually sensors height. Sensor Position(File/Settings/Program Options/Sensor Position).

7. Start Measure Tool procedure(Machine/Tool Offset/Measure Tool).
You can also use the “Tool Offset Measure Tool” button on the left toolbar:
ToolbarButton

8. When “Measure Tool” procedure is executed, default tool measure sequence will be:

Machine will ascend to safe height and move from current position to sensors XY position(the one we set in settings).

Machine will descend towards sensor with probe speed(File/Settings/Program Options/Probe Speed) and when sensor gets activated, machine will stop and ascend to safe height.
Machine will return back to XY position from which we started the “Measure Tool” procedure.

You have configured your fixed tool sensor and you should be able to use “Measure Tool” procedure.

If you would like to edit “Measure Tool” procedure to fit your needs or to have more in depth look at the tool measure procedure itself, you can do that by opening the tool measure script file.

 

 

9. You can create and access “Measure Tool” procedure script file  in two ways:

a.) In settings (File/Settings/Program Options/Scripts/Tool Offset) you can click buttons Generate and Edit. With Generate button you create default file and with Edit button you can open it and make any modifications of the script g-code

GenerateEdit

b.) When you start “Measure Tool” procedure for the first time, Machine.Tool_Offset.Measure_Tool.gcode default file will be created. Open your PlanetCNC TNG software installation folder and locate folder: “.Scripts”

Find “Machine.Tool_Offset.Measure_Tool.gcode” file and open it with text editor:

MeasureScript

 

10. Description of parameters and g-codes(in order of appearance in script g-code):

M73:
This g-code saves states of of all modal g-codes currently used by software. When Machine.Tool_Offset.Measure_Tool.gcode procedure is executed in its entirety, all states will be restored to their previous values.

#1 = # (store current x position): Stores value of current X axis position as parameter #1.
#2 = # (store current y position): Stores value of current Y axis position as parameter #2.
#3 = # (store current z position): Stores value of current Z axis position as parameter #3.

G90:
With G90 g-code we set absolute mode of motion. Prior to “Measure Tool” procedure , motion mode could be different so we used M73 to save current modal states.

G53 G0 Z#<_motorlimit_zp>:
Machine ascends to safe height.

G53 G0 X#<_sensor_x> Y#<_sensor_y>:
Machine moves to sensor XY position.

F#<_speed_probe>:
Speed at which machine descends to measure tool. Set with Probe Speed in settings(File/Settings/Program Options/Probe Speed). This value can be set directly with new F value. In such case you should delete the “#<_speed_probe>” line of code.

G38.2 Z-100000:
This g-code moves selected axis in selected direction. G38.2 g-code uses parameter X(-/+)Value,Y(-/+)Value or Z(-/+)Value which define to which coordinate value machine should move and in what direction. When probe activation occurs over the time of machine travel, machine will stop.

G43.1 Z[#<_probe_z> – #<_sensor_z>]:
When G38.2 g-code senses that sensor is activated and stops the machine, this g-code sets tool offset.

G53 G0 Z#<_motorlimit_zp>:
Machine moves in absolute coordinates to safe height.

G0 X#1 Y#2:
Rapid move to position from which we started the “Measure Tool” procedure .

o<100> if[[TOABSZ[#3] LT #<_motorlimit_zp>] AND [TOABSZ[#3] GT #<_motorlimit_zm>]]
G0 Z#3 (move to last z position)
o<100> endif

With this sub program we make sure that we are safely within Z axis motors limits when machine moves back to position from which we started the “Measure Tool” procedure .

How to configure limit switch inputs of controller in PlanetCNC TNG software

Limit switches are used to reference your machine(homing procedure) as also for safety reasons.

If you use only one limit switch for axis X motor (positive direction), connected to controllers limit input 1:

XAxis+

If you have two limit switches for axis X motor (negative and positive direction), connected to same limit input of controller:

XAxis+-

If you have two limit switches on X axis(negative and positive direction), connected to separate limit inputs of controller:

XAxisSeparatePin

Same principles that were shown above for X axis can be used for all other axes.

Limit switch test:

When your limit switches are connected to limit inputs of controller, you can test if PlanetCNC TNG software senses the limit switch activation. On main screen click the “IO” tab and observe controllers Limit status bar behaviour:

Limit1_IO

Limit1_IO_Active

Invert option:
If you use normally closed type of limit switches then you can invert controller limit input in settings: File/Settings/Limit

LimitInvert

Click round button next to limit pin that you wish to invert:

LimitInvertEnabled

How to set Homing procedure in PlanetCNC TNG software

Homing procedure will reference your machine and set machine work area.

1. First you need to configure these settings:
Motor limits(File/Settings/Motors/Limits):
Set limitation of movement for each motor.
Motor limits tutorial can be found on this link: Motor Limits tutorial

Limit switch input configuration(File/Settings/Limit/Limit Switches):
Configure limit switch inputs of controller. Limit switch input configuration tutorial can be found on this link: Limit switch input configuration tutorial

Home speed(File/Settings/Program Options/Home Speed):
Set homing speed.

2. To execute homing procedure click: Machine/Home or click the “Home” button located on vertical toolbar: HomingButton

Homing procedure(File/Settings/Program Options/Homing) can be configured to fit your needs.

To set homing Order of axes you select order number from drop down menu for each axis:

HomingSettings

To set homing Direction of axes you click the round button of axis direction option:

HomingDirection

You can set home Position for each axis:

HomePosition

Switch hysteresis:
This is the distance from the moment when limit switch is activated to when limit switch is released. Usually around 2-5mm. Test this on your machine.

When Homing is executed, homing sequence(as set above) will be:

Z axis will homed first. Axis will move in positive direction until Z axis limit switch is activated. When switch is activated machine stops and moves to position set with Z “Position”.

X axis will homed second. Axis will move in positive direction until X axis limit switch is activated. When switch is activated machine stops and moves to position set with X “Position”.

Y axis will homed third. Axis will move in positive direction until Y axis limit switch is activated. When switch is activated machine stops and moves to position set with Y “Position”.

These are all necessary steps to configure Homing procedure of your machine. 

To take more in-depth look at the code behind Homing procedure see next chapter.

 

3. When you click home button for the first time, Machine.Home.gcode file will be created. Open your PlanetCNC TNG software installation folder and locate folder: “.Scripts”

Find “Machine.Home.gcode” file and open it with text editor(bottom image describes homing g-code only for three axes):

Script_g-code

Description of important script parameters and g-codes:

M73:
This g-code saves states of of all modal g-codes currently used by software. When Machine.Tool_Offset.Measure_Tool.gcode procedure is executed in its entirety, all states will be restored to their previous values.

G90:
With G90 g-code we set absolute mode of motion. Prior to measure tool procedure, motion mode could be different so we used M73 to save current modal states.

F#<_speed_home>:

Speed at which machine will home each axis. Set with Home Speed in settings(File/Settings/Program Options/Home Speed). This value can be set directly with new F value. In such case you should delete the “#<_speed_home>” line of code.

 

G38.1 X+100000:
This g-code moves selected axis in selected direction and senses the limit switch activation.
G38.1 g-code uses parameter X(-/+)Value,Y(-/+)Value or Z(-/+)Value which define to which coordinate value machine should move and in what direction.
When limit swith activation occures over the time of machine travel, machine will stop.

G10 L9 X[#<_motorlimit_xm> + #<_homeswitch_hysteresis>]:
When G38.1 g-code senses and stops the machine, this g-code sets current absolute position for selected axis .
Example:
G10 L9 X[#<_motorlimit_xp> + #<_homeswitch_hysteresis>]: Absolute position Z will now be added value of X+ motor limit  and  X+ switch hysteresis value.

G53 G0 X[#<_home_x>]: Machine moves in absolute coordinates to x axis home postion.
Example: If new machine absolute position is the one saved with G10 L9 g-code(X+ motor limit  and  X+ switch hysteresis value), we move machine back to X axis home position.

Homing procedure can be configured to fit your needs. You can set homing speed, axes homing order, home position etc…

How to set Motion Limits in PlanetCNC TNG software

To set motion limits of your machine click: File/Settings/Motion-> “Motion Limits”

Motion Limits tab

Limit- value sets limitation of machine/effector (tool) movement in negative direction, Limit+ value sets limitation of machine/effector (tool) movement in positive direction for specific axis:

MotionLimitsValues

With Motion limits values inserted, we set motion limits of machine. If we want machine to stop when limits are reached we must enable them.
To enable Motion limits for specified axis click the round button:

MotionLimitsValuesEnabled

When you set your machines motion limits you will notice that 3D representation of machine limits will be displayed accordingly on the main screen:

3DDisplayValues

If you use normal XYZ axis machine then we recommend that you set your motion limits at same values as motor limits.

You are probably wondering what is the difference between Motor limits and Motion limits?
We try to think that motor limits are limiting actual motor movement and Motion limits are limiting machine or effector (tool) movement.

Eleboration:
With use of combined motors and special mechanics you can achieve movement which doesn’t follow the same kinematic rules as linear motion CNC’s.
Examples of such machines are H-bot, Delta printer or multiple axis machines. In such cases values for Motor limits and Motion limits would not be the same.

How to set Motor limits in PlanetCNC TNG software

To set your axis motor limits click: File/Settings/Motors -> “Motor Limits”.
MotorLimitsTab

Limit- value sets limitation of motor movement in negative direction, Limit+ value sets limitation of motor movement in positive direction:

MotorLimitsTabValues

With Motor limits values inserted, we set limits of motors. If we want motors to stop when limits are reached, we must enable them.
To enable Motor limits for specified axis click the round button:

MotorLimitsEnabled