8383prog = sys .argv [0 ] # this program
8484tools = dict () # tools[tno]: dict of text toollines
8585toollist = [] # list of tool numbers
86- restore_pocket = dict () # for nonran toolchanger
86+ nonran_from_pocket = dict ()# use to restore pocket in db
8787p0tool = - 1 # nonran:-1, ran:-1|0 ==> no tool in spindle
8888start_time = 0 # 0 ==> indeterminate
8989elapsed_time = 0 # tool time usage
@@ -193,7 +193,7 @@ def save_tools_to_file(fname,comment=""):
193193 history .append (comment )
194194 if len (history ) > history_max_ct : history = history [1 :]
195195 if len (history ):
196- f .write ("\n ;Recent (last %d) db history:\n " % history_max_ct )
196+ f .write ("\n ; Recent (last %d) db history:\n " % history_max_ct )
197197 for i in range (len (history )):
198198 f .write ("; %s\n " % history [i ])
199199 f .close ()
@@ -202,6 +202,15 @@ def nonran_pno(tno): # simulation rule:
202202 return tno + pocket_offset # make startup pocket number different than tool number
203203 # to avoid assumptions about tno and pno
204204
205+ def nonran_restore_pocket (tno ):
206+ D = toolline_to_dict (tools [tno ],all_letters )
207+ D ['P' ] = nonran_from_pocket [tno ]
208+ tools [tno ] = dict_to_toolline (D ,all_letters )
209+ if not tno in nonran_from_pocket :
210+ umsg ("nonran_restore_pocket tno=%d not in dict: %s" % (tno ,nonran_from_pocket ))
211+ #msg("Restore pocket tno:%d pno %s"%(tno,int(D['P'])))
212+ del nonran_from_pocket [tno ]
213+
205214def assign_pocket (tno ):
206215 global available_pockets
207216 if not random_toolchanger :
@@ -355,8 +364,10 @@ def handle_disconnect(fname):
355364 global disconnect_evt
356365 if p0tool > 0 :
357366 stop_tool_timer (p0tool )
358- save_tools_to_file (fname ,"tno:%3d in spindle at termination" % p0tool )
359367 msg ("!!!Stopped with loaded tool: %d" % p0tool )
368+ if not random_toolchanger :
369+ nonran_restore_pocket (p0tool )
370+ save_tools_to_file (fname ,"tno:%3d in spindle at termination" % p0tool )
360371 msg ("disconnected" )
361372 disconnect_evt .set ()
362373
@@ -389,14 +400,11 @@ def stop_tool_timer(tno):
389400
390401def update_tool (tno ,update_line ):
391402 D = toolline_to_dict (tools [tno ],all_letters )
392- savep = D ['P' ]
393403 for item in toolline_to_list (update_line ):
394404 for l in tletters :
395405 if l in item : D [l ]= item .lstrip (l ) #supersede by update_line
396- if not random_toolchanger :
397- if D ['P' ] != savep and D ['P' ] != 0 :
398- umsg ("update_tool(): reject pocket change %s (is:%s)" % (D ['P' ],savep ))
399- D ['P' ] = savep # nonran handle p0 case
406+ if not random_toolchanger and p0tool == tno :
407+ D ['P' ] = '0' # unload uses nonran_from_pocket[]
400408 tools [tno ] = dict_to_toolline (D ,all_letters )
401409
402410def apply_db_rules ():
@@ -481,15 +489,14 @@ def user_load_spindle_nonran_tc(tno,params):
481489 if TMP ['P' ] != "0" : umsg ("user_load_spindle_nonran_tc P=%s" % TMP ['P' ])
482490 if tno == 0 : umsg ("user_load_spindle_nonran_tc tno=%d" % tno )
483491
484- # save restore_pocket as pocket may have been altered by apply_db_rules()
485492 D = toolline_to_dict (tools [tno ],all_letters )
486- if tno != p0tool : restore_pocket [tno ] = D ['P' ] # avoid reset if redundant load
493+
494+ # save nonran_from_pocket as pocket may have been altered by apply_db_rules()
495+ if tno != p0tool : nonran_from_pocket [tno ] = D ['P' ] # avoid reset if redundant load
487496
488497 if p0tool != - 1 : # accrue time for prior tool:
489498 stop_tool_timer (p0tool )
490- RESTORE = toolline_to_dict (tools [p0tool ],all_letters )
491- RESTORE ['P' ] = restore_pocket [p0tool ]
492- tools [p0tool ] = dict_to_toolline (RESTORE ,all_letters )
499+ nonran_restore_pocket (p0tool )
493500
494501 p0tool = tno
495502 D ['T' ] = str (tno )
@@ -501,7 +508,7 @@ def user_load_spindle_nonran_tc(tno,params):
501508def user_unload_spindle_nonran_tc (tno ,params ):
502509 global p0tool
503510 #msg("user_unload_spindle_nonran_tc p0tool=%d restore=%s'u %s'"%
504- # (p0tool,restore_pocket ,params))
511+ # (p0tool,nonran_from_pocket ,params))
505512 check_params (tno ,params )
506513
507514 if p0tool == - 1 : return # ignore
@@ -510,17 +517,14 @@ def user_unload_spindle_nonran_tc(tno,params):
510517 if TMP ['P' ] != "0" : umsg ("user_unload_spindle_nonran_tc P=%s" % TMP ['P' ])
511518
512519 stop_tool_timer (p0tool )
513- D = toolline_to_dict (tools [p0tool ],all_letters )
514- D ['T' ] = str (p0tool )
515- D ['P' ] = restore_pocket [p0tool ]
516- tools [p0tool ] = dict_to_toolline (D ,all_letters )
517-
520+ nonran_restore_pocket (p0tool )
518521 p0tool = - 1
522+
519523 save_tools_to_file (db_savefile ,"tno:%3d (unload from spindle)(empty)" % tno )
520524
521525def user_load_spindle_ran_tc (tno ,params ):
522526 global p0tool
523- #msg("user_load_spindle_ran_tc 'l %s'"% params)
527+ #msg("user_load_spindle_ran_tc 'l %s' (p0tool=%d)"%( params,p0tool) )
524528 check_params (tno ,params )
525529
526530 D = toolline_to_dict (tools [tno ],all_letters )
@@ -537,26 +541,24 @@ def user_load_spindle_ran_tc(tno,params):
537541 tools [tno ] = dict_to_toolline (D ,all_letters )
538542
539543 pno = int (D ['P' ])
540- msg = "tno:%3d --> pno:%3d ( load to spindle)" % (tno ,pno )
544+ txt = "tno:%3d --> pno:%3d ( load to spindle)" % (tno ,pno )
541545 if tno == 0 and pno == 0 :
542- msg = "tno:%3d --> pno:%3d ( load t0 spindle)(empty)" % (tno ,pno )
543- save_tools_to_file (db_savefile ,msg )
546+ txt = "tno:%3d --> pno:%3d ( load t0 spindle)(empty)" % (tno ,pno )
547+ save_tools_to_file (db_savefile ,txt )
544548
545549def user_unload_spindle_ran_tc (tno ,params ):
546550 global p0tool
547- #msg("user_unload_spindle_ran_tc 'u %s'"% params)
551+ #msg("user_unload_spindle_ran_tc 'u %s' (p0tool=%d)"%( params,p0tool) )
548552 check_params (tno ,params )
549553 TMP = toolline_to_dict (params ,['T' ,'P' ])
550554 if p0tool == - 1 :
555+ if tno != 0 :
556+ umsg ("user_unload_spindle_ran_tc p0tool=%d tno=%d" % (p0tool ,tno ))
551557 D = dict ()
552558 D ['T' ] = TMP ['T' ]
553559 D ['P' ] = TMP ['P' ]
554560 tools [0 ] = dict_to_toolline (D ,['T' ,'P' ])
555- #msg("user_unload_spindle_ran_tc: ignoring for no p0tool")
556- pass
557561 else :
558- if p0tool == 0 :
559- pass
560562 stop_tool_timer (p0tool )
561563 D = toolline_to_dict (tools [p0tool ],all_letters )
562564 D ['T' ] = TMP ['T' ]
@@ -565,10 +567,10 @@ def user_unload_spindle_ran_tc(tno,params):
565567 p0tool = - 1
566568
567569 pno = int (D ['P' ])
568- msg = "tno:%3d --> pno:%3d (unload from spindle)" % (tno ,pno )
570+ txt = "tno:%3d --> pno:%3d (unload from spindle)" % (tno ,pno )
569571 if tno == 0 :
570- msg = "tno:%3d --> pno:%3d (unload from spindle)(notool)" % (tno ,pno )
571- save_tools_to_file (db_savefile ,msg )
572+ txt = "tno:%3d --> pno:%3d (unload from spindle)(notool)" % (tno ,pno )
573+ save_tools_to_file (db_savefile ,txt )
572574
573575#-----------------------------------------------------------
574576# begin interface to LinuxCNC
0 commit comments