diff graster/hacklab-engraver/M101 @ 11:f952052e37b7

trying a fix.
author Robert McIntyre <rlm@mit.edu>
date Tue, 24 Aug 2010 19:06:45 -0400
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/graster/hacklab-engraver/M101	Tue Aug 24 19:06:45 2010 -0400
     1.3 @@ -0,0 +1,85 @@
     1.4 +#!/usr/bin/python
     1.5 +
     1.6 +# === Graster Streaming Script ===
     1.7 +#
     1.8 +# This script is invoked by g-code M101 at the
     1.9 +# beginning of every graster job. It figures out
    1.10 +# the name of the mask file and forks a process
    1.11 +# to stream it to the HAL while AXIS executes the
    1.12 +# motion code.
    1.13 +#
    1.14 +# This file must be symlinked to the ~/emc2/nc_files dir
    1.15 +
    1.16 +import emc, os, sys, threading, gtk, time, popen2, signal, select
    1.17 +
    1.18 +os.chdir("/home/hacklab/emc2/configs/hacklab-engraver3")
    1.19 +ini = emc.ini("hacklab-engraver3.ini")
    1.20 +emc.nmlfile = ini.find("EMC","NML_FILE")
    1.21 +s = emc.stat()
    1.22 +s.poll()
    1.23 +ngc = s.file
    1.24 +(base,ext) = os.path.splitext(ngc)
    1.25 +gmask = base+".gmask"
    1.26 +if not os.path.exists(gmask):
    1.27 +  gmask = ngc+".gmask"
    1.28 +  if not os.path.exists(gmask):
    1.29 +    os.system('zenity --error --text "Mask file \''+base+'.gmask\' not found. Laser disabled for this job."')
    1.30 +    sys.exit(os.EX_NOINPUT)
    1.31 +
    1.32 +
    1.33 +def make_pbar():
    1.34 +  (path,fn) = os.path.split(gmask)
    1.35 +  message = "Streaming '"+fn+"'\n\n" + \
    1.36 +            "This dialog should complete and close itself some time before\n" + \
    1.37 +            "your job is done. If you stop the job, wait for this dialog\n" + \
    1.38 +            "to go away before starting another one. If it won't go away,\n" + \
    1.39 +            "click 'Cancel' and kill any processes containing 'M101'.\n"
    1.40 +  return popen2.Popen3('zenity --progress --auto-close --auto-kill --title "Graster Streamer" --text "'+message+'"')
    1.41 +
    1.42 +pbar = make_pbar()
    1.43 +
    1.44 +total_lines = 0
    1.45 +fd = open(gmask,'r')
    1.46 +for x in fd.readlines():
    1.47 +  total_lines += 1
    1.48 +fd.close()
    1.49 +
    1.50 +total_lines = float(total_lines)
    1.51 +count = 0
    1.52 +
    1.53 +print "Streaming '"+gmask+"'..."
    1.54 +pid = os.fork()
    1.55 +
    1.56 +if pid:
    1.57 +  time.sleep(2)
    1.58 +  sys.exit(os.EX_OK)
    1.59 +else:
    1.60 +  os.setsid()
    1.61 +  fin = open(gmask,'r')
    1.62 +  fout = os.popen('halstreamer','w')
    1.63 +  try:
    1.64 +    for line in fin.readlines():
    1.65 +      #r,w,e = select.select((),(fout,),(fout,),0.1)
    1.66 +      if pbar.poll() != -1: raise
    1.67 +
    1.68 +      #if len(w) > 0:
    1.69 +      fout.write(line)
    1.70 +      count += 1
    1.71 +      #elif len(e) > 0:
    1.72 +      #  os.system("zenity --error --text 'Error writing to halstreamer. Job may not finish.'")
    1.73 +      #  raise
    1.74 +
    1.75 +      pbar.tochild.write(str(int(100.0*count/total_lines))+"\n")
    1.76 + 
    1.77 +  finally:
    1.78 +    print "...cleaning up..."
    1.79 +    #fin.close()
    1.80 +    #fout.close()
    1.81 +    pbar.tochild.close()
    1.82 +    pbar.fromchild.close()
    1.83 +    #os.kill(pbar.pid,signal.SIGINT)
    1.84 +    #os.kill(pbar.pid,signal.SIGKILL)
    1.85 +    print "...done!"
    1.86 +    os._exit(os.EX_OK)
    1.87 +    #sys.exit(os.EX_OK)
    1.88 +