annotate graster/hacklab-engraver/M101 @ 15:8ad629298649

major refactoring
author Robert McIntyre <rlm@mit.edu>
date Sun, 29 Aug 2010 19:02:26 -0400
parents f952052e37b7
children
rev   line source
rlm@11 1 #!/usr/bin/python
rlm@11 2
rlm@11 3 # === Graster Streaming Script ===
rlm@11 4 #
rlm@11 5 # This script is invoked by g-code M101 at the
rlm@11 6 # beginning of every graster job. It figures out
rlm@11 7 # the name of the mask file and forks a process
rlm@11 8 # to stream it to the HAL while AXIS executes the
rlm@11 9 # motion code.
rlm@11 10 #
rlm@11 11 # This file must be symlinked to the ~/emc2/nc_files dir
rlm@11 12
rlm@11 13 import emc, os, sys, threading, gtk, time, popen2, signal, select
rlm@11 14
rlm@11 15 os.chdir("/home/hacklab/emc2/configs/hacklab-engraver3")
rlm@11 16 ini = emc.ini("hacklab-engraver3.ini")
rlm@11 17 emc.nmlfile = ini.find("EMC","NML_FILE")
rlm@11 18 s = emc.stat()
rlm@11 19 s.poll()
rlm@11 20 ngc = s.file
rlm@11 21 (base,ext) = os.path.splitext(ngc)
rlm@11 22 gmask = base+".gmask"
rlm@11 23 if not os.path.exists(gmask):
rlm@11 24 gmask = ngc+".gmask"
rlm@11 25 if not os.path.exists(gmask):
rlm@11 26 os.system('zenity --error --text "Mask file \''+base+'.gmask\' not found. Laser disabled for this job."')
rlm@11 27 sys.exit(os.EX_NOINPUT)
rlm@11 28
rlm@11 29
rlm@11 30 def make_pbar():
rlm@11 31 (path,fn) = os.path.split(gmask)
rlm@11 32 message = "Streaming '"+fn+"'\n\n" + \
rlm@11 33 "This dialog should complete and close itself some time before\n" + \
rlm@11 34 "your job is done. If you stop the job, wait for this dialog\n" + \
rlm@11 35 "to go away before starting another one. If it won't go away,\n" + \
rlm@11 36 "click 'Cancel' and kill any processes containing 'M101'.\n"
rlm@11 37 return popen2.Popen3('zenity --progress --auto-close --auto-kill --title "Graster Streamer" --text "'+message+'"')
rlm@11 38
rlm@11 39 pbar = make_pbar()
rlm@11 40
rlm@11 41 total_lines = 0
rlm@11 42 fd = open(gmask,'r')
rlm@11 43 for x in fd.readlines():
rlm@11 44 total_lines += 1
rlm@11 45 fd.close()
rlm@11 46
rlm@11 47 total_lines = float(total_lines)
rlm@11 48 count = 0
rlm@11 49
rlm@11 50 print "Streaming '"+gmask+"'..."
rlm@11 51 pid = os.fork()
rlm@11 52
rlm@11 53 if pid:
rlm@11 54 time.sleep(2)
rlm@11 55 sys.exit(os.EX_OK)
rlm@11 56 else:
rlm@11 57 os.setsid()
rlm@11 58 fin = open(gmask,'r')
rlm@11 59 fout = os.popen('halstreamer','w')
rlm@11 60 try:
rlm@11 61 for line in fin.readlines():
rlm@11 62 #r,w,e = select.select((),(fout,),(fout,),0.1)
rlm@11 63 if pbar.poll() != -1: raise
rlm@11 64
rlm@11 65 #if len(w) > 0:
rlm@11 66 fout.write(line)
rlm@11 67 count += 1
rlm@11 68 #elif len(e) > 0:
rlm@11 69 # os.system("zenity --error --text 'Error writing to halstreamer. Job may not finish.'")
rlm@11 70 # raise
rlm@11 71
rlm@11 72 pbar.tochild.write(str(int(100.0*count/total_lines))+"\n")
rlm@11 73
rlm@11 74 finally:
rlm@11 75 print "...cleaning up..."
rlm@11 76 #fin.close()
rlm@11 77 #fout.close()
rlm@11 78 pbar.tochild.close()
rlm@11 79 pbar.fromchild.close()
rlm@11 80 #os.kill(pbar.pid,signal.SIGINT)
rlm@11 81 #os.kill(pbar.pid,signal.SIGKILL)
rlm@11 82 print "...done!"
rlm@11 83 os._exit(os.EX_OK)
rlm@11 84 #sys.exit(os.EX_OK)
rlm@11 85