I wanted to show the estimated and running time for program in my gladevcp UI panel. Ignoring the fact that the estimated time may be off by quite a bit.
Given that there the "file->properties" dialogue in the Axis UI that shows the estimated time I wanted to expose the same calculated time to my gladevcp panel.
Since the gladevcp python file is in a seperate procesa and I could not find a way to access needed information through available python librares (linuxcnc, hal, halgui and so on) my slightly hacky workaround was to:
1. Use the USER_COMMAND_FILE that has access to Axis objects to interact with gcode-data and reuse the same code that populates the estimated time for "file-properties" panel
2. Monitor if a new file has been loaded through user_live_update() function. (I could not find anywhere to add an eventlistener...)
3. Write the estimated time for program to a temporary file when a new file has been loaded by Axis GUI
4. On the other side in my gladevcp python script - Listen to file changes through a gllib.timeout_add_seconds() function called every second
5. Load time estimate from temporary file in step 2
6. Display in my gladevcp
In my USER_COMMAND_FILE python file:
Code:
lastLoadedFile = ""
def user_live_update():
global lastLoadedFile
if loaded_file:
if loaded_file <> lastLoadedFile:
runTime = calculateRunTime(loaded_file)
print("New file loaded: " + loaded_file + " - " + str(runTime) + " seconds")
lastLoadedFile = loaded_file
f = open("/home/pi/ektefilewatcher", "w")
f.write (loaded_file + "\r\n")
f.write (str(runTime) + "\r\n")
f.close()
#logic copied from axis.py in linuxcnc source
def calculateRunTime(loaded_file):
runTime = ""
if not loaded_file:
return 0
else:
mf = vars.max_speed.get()
return (sum(dist(l[1][:3], l[2][:3])/min(mf, l[3]) for l in o.canon.feed) +
sum(dist(l[1][:3], l[2][:3])/min(mf, l[3]) for l in o.canon.arcfeed) +
sum(dist(l[1][:3], l[2][:3])/mf for l in o.canon.traverse) +
o.canon.dwell_time
)
In my gladevcp python file
Code:
def __init__(self, halcomp,builder,useropts):
self.builder = builder
self.isRunning = False
self.runTime = 0
self.estimatedTime = 0
self.lastFile = ""
glib.timeout_add_seconds(1, self.on_timer_tick)
def on_timer_tick(self, userdata=None):
s.poll()
currentFile = s.file
if(currentFile != self.lastFile):
f = open("/home/pi/ektefilewatcher", "r")
watchedFile = f.readline().strip()
fileTime = float(f.readline())
f.close()
if(watchedFile == currentFile ):
print("got new file time: " + currentFile + " - " + str(fileTime) )
self.lastFile = currentFile
self.estimatedTime = fileTime
self.builder.get_object("ekte_filetiming_display").set_label("" + self.getTime(self.estimatedTime))
return True
This is a very rounad about way of doing it, but atleast I got what I wanted. Does anyone know of a easier way to do this?