Machine Tending strategy

So here’s the version which seems to work, combined with a physical trigger.
I added a couple counters to help track the program flow, and made the timers all variables.

Key to solving this was to use an output from the controller to trigger the footswitch input on the gage, which will release the serialRead() function once it hangs when no more data is received. There might be a way to do that within that URCap function which would make this kinda clunky approach unnecessary, so I’ll keep pushing our programmer to look into that.

I’m still playing with the timeout & trigger pulse times - looks like they can be pretty short.

Hopefully this will continue to work correctly once I put it inside the thread which will activate the rest of the gage process. (advance & retract an air cylinder a few times & average a few readings).

# Flush gage buffer and pull current gage value

def read_gage_test():

global flush_counter = 0
global gage_write_count = 0
global flush_loop_ctr = 0
global gage_timeout = 0.2
global trigger_pulse = 0.1
global write_wait = 0.25
global flush_max = 3


global flush_prev_counter = flush_counter
global flush_gage_buffer_th = run flush_gage_buffer()
sleep(gage_timeout)

/# loop flush_max times if gage is not empty on first try

while(flush_prev_counter != flush_counter):  
    sleep(gage_timeout)
    flush_loop_ctr = flush_loop_ctr+1
    if flush_loop_ctr > flush_max:
	flush_counter = 0
    end		# end if
end		#end while 

/# Stop thread
kill flush_gage_buffer_th

/# Trigger gage (footswitch) - needs 2X for some reason
sleep(trigger_pulse)
set_standard_digital_out(6,True)
sleep(trigger_pulse)
set_standard_digital_out(6,False)
sleep(trigger_pulse)

serialWrite("sog", "M1?")		         #trigger gage
sleep(write_wait)

gage_write_count = gage_write_count + 1    	#track progress here

global gagestring = serialRead("sog") 		# read the desired output string

end # end read_gage function

# Thread to flush gage buffer

thread flush_gage_buffer():
while True: #start loop to read/flush unwanted strings
serialRead(“sog”) #read unwanted string from gage
flush_counter = flush_counter + 1
end
sync()
end #end thread