Saturday 21 March 2009

Using Joystick buttons to run complex CNC commands

Earlier I described how to run a single line of G code with a single button press. The problem with this is that it is limited to a single line. :-)

Earlier I wrote a short multi-line program for setting tool length, and now I'd like to run it at the touch of a button.

There is already a method for doing this with some classic ladder HAL logic, but one look at classic ladder turned my stomach. Fair enough for people who already think that way and don't want to change, but as far as I can see, it's like running an emulation of an abacus on your PC so that you can use it do your accounts: a deliberate step back into a far more awkward past. So I went looking for an easier and more powerful way. :-)

One piece of the puzzle is a user-defined M-code. This allows us to run any executable with a single M1xx command. Unfortunately for these purposes, this is an executable program, not a G-code program.

The next piece of the puzzle is emcrsh. This allows fairly complete control over emc via telnet.

To glue them together I needed a program to connect an emcrsh session, run the touch off program, wait for completion, and close the session. I used this as an excuse to learn a bit more Python, but my unsuccessful efforts to get telnetlib to work took too much time from the main task, so I did something a bit kludgy instead: I piped the stuff I wanted to send into telnet and hoped for the best.


#!/bin/bash

telnet localhost 5007 < ~/emc2/nc_files/util/InvokeProbeAndSetToolLength.commands


Note that this file must be executable.

And here are the commands:


hello EMC client 1.0
set echo off
set verbose on
set enable EMCTOO
set task_plan_init
set mode auto
set echo off
set set_wait done
set open util/ProbeAndSetToolLength.ngc
set run
get mode
quit


Note that ProbeAndSetToolLength.ngc must be in ~/emc2/nc_files/util for this to work.

This works well enough. If the probing program does not complete for some reason, AXIS is left in auto mode as if it were still running a program. This locks out the manual controls. To get back to manual mode, I click on the MDI tab and back to manual.

Now I can set Z=0 to correspond to something useful at the touch of a button.

No comments: