@@ -44,7 +44,7 @@ def print_state():
4444c .home (2 )
4545l .wait_for_home ([1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 ])
4646c .mode (linuxcnc .MODE_AUTO )
47-
47+
4848
4949#
5050# run the .ngc test file, starting from the special line
@@ -56,28 +56,63 @@ def print_state():
5656def mod_5_is_0 (x ):
5757 return abs ((x + epsilon ) % 5 ) < 2 * epsilon
5858
59- # Take first step
59+
60+ def wait_complete_step ():
61+
62+ '''The normal `linuxcnc.command.wait_complete()` function does not
63+ understand single-stepping. It waits for the `state` to return to
64+ `RCS_DONE` but this does not happen when single-stepping, `state`
65+ stays at `RCS_EXEC` while waiting for the next step.
66+
67+ This function instead waits for status.task.execState to tell us
68+ that Task is no longer waiting for Motion in any way.'''
69+
70+ timeout = 5.0
71+ start = time .time ()
72+
73+ # Wait for the command to be acknowledged by Task (FIXME or is it motion?).
74+ while time .time () - start < timeout :
75+ s .poll ()
76+ if s .echo_serial_number >= c .serial :
77+ break
78+ time .sleep (0.1 )
79+
80+ # Wait for Task to be done waiting for Motion.
81+ while time .time () - start < timeout :
82+ s .poll ()
83+ if s .exec_state not in [ linuxcnc .EXEC_WAITING_FOR_MOTION , linuxcnc .EXEC_WAITING_FOR_MOTION_AND_IO , linuxcnc .EXEC_WAITING_FOR_MOTION_QUEUE ]:
84+ return
85+ time .sleep (0.1 )
86+
87+ raise SystemExit ('timeout in wait_complete_step()' )
88+
89+
90+ # Take first three steps (these cause no motion).
91+ c .auto (linuxcnc .AUTO_STEP )
92+ c .auto (linuxcnc .AUTO_STEP )
6093c .auto (linuxcnc .AUTO_STEP )
94+ wait_complete_step ()
6195
6296count = 0
6397while True :
6498 s .poll ()
99+ if s .interp_state == linuxcnc .INTERP_IDLE :
100+ sys .stderr .write ("Finished: Detected program finish\n " )
101+ break
102+
65103 (x , y ) = (h ["Xpos" ], h ["Ypos" ])
66104 if mod_5_is_0 (x ) and mod_5_is_0 (y ):
67- # Both axes on goal; make next step and let motion start
105+ # Both axes on goal; command the next step and wait for it to finish.
68106 sys .stderr .write ("Taking step from X%.2f Y%.2f\n " % (x , y ))
69107 c .auto (linuxcnc .AUTO_STEP )
70- time . sleep ( 0.1 )
108+ wait_complete_step ( )
71109
72110 print_state ()
73111
74112 count += 1
75113 if count >= 1000 : # Shouldn't happen, but prevent runaways
76114 sys .stderr .write ("Finished: Exceeded max cycles\n " )
77115 sys .exit (1 )
78- if s .interp_state == linuxcnc .INTERP_IDLE :
79- sys .stderr .write ("Finished: Detected program finish\n " )
80- break
81116
82117 time .sleep (0.1 )
83118
0 commit comments