I have determined that it is possible to add #5400 to the .var file, and the tool-in-spindle is then automatically recorded in there.
Unfortunately as soon as LinuxCNC starts that value is over-written again with a zero.
It should be possible to read the file directly, store the value and act on it.
Here is an attempt at Python script that can be loaded very early in the HAL sequence (loadusr tool_loader)
It doesn't work, it had got too late to finish it here:
Code:
#! /usr/bin/env python
import sys
import linuxcnc
import re
s = linuxcnc.stat()
c = linuxcnc.command()
s.poll()
#inifile = linuxcnc.ini(sys.argv[1])
varfile = "sim_mm.var" #inifile.find("RS274NGC", "PARAMETER_FILE")
old_tool = 0
for line in open(varfile, "r"):
M = re.search("^5400\s([\.\d]*)", line)
if M: old_tool = re.search("^5400\s([\.\d]*)", line).group(1)
def ok_for_mdi():
s.poll()
return (not s.estop) and s.enabled and s.homed and (s.interp_state == linuxcnc.INTERP_IDLE)
while not ok_for_mdi:
print ok_for_mdi
pause(1)
print "trying to set tool to %s" % old_tool
c.mode(linuxcnc.MODE_MDI)
c.wait_complete()
c.mdi("M61 Q%s" % old_tool)
The issues are:
(1) Not finding the INI file so not being able to read the PARAMETER_FILE
(2) It doesn't wait for "ok_for_mdi" and tries to run the MDI command at an invalid time.
But other than that, it looks like it might work. It does at least try to set the correct tool number.