#!/usr/bin/python
import hal, time
import subprocess as sp
h = hal.component("z_emulator")
h.newpin("Z_axis_pos", hal.HAL_FLOAT, hal.HAL_IN)
h.newpin("Z_axis_homed", hal.HAL_BIT, hal.HAL_IN)
h.newpin("Z_axis_dir", hal.HAL_BIT, hal.HAL_IN)
h.newpin("Torch_enable_status", hal.HAL_BIT, hal.HAL_IN)
h.newpin("cylinder_out", hal.HAL_BIT, hal.HAL_OUT)
h.newpin("probing_pulse", hal.HAL_BIT, hal.HAL_OUT)
h.newpin("ARC_plasma_voltage", hal.HAL_FLOAT, hal.HAL_OUT)
h.ready()
sp.Popen(['halcmd net emulator:arc-voltage-in z_emulator.ARC_plasma_voltage plasmac.arc-voltage-in'], shell=True)
sp.Popen(['halcmd net emulator:Z_axis_pos joint.2.pos-cmd z_emulator.Z_axis_pos'], shell=True)
sp.Popen(['halcmd net emulator:Z_axis_homed joint.2.homed z_emulator.Z_axis_homed'], shell=True)
sp.Popen(['halcmd net emulator:Z_axis_dir stepgen.2.dir z_emulator.Z_axis_dir'], shell=True)
sp.Popen(['halcmd net emulator:cylinder_out  parport.0.pin-07-out z_emulator.cylinder_out'], shell=True)
sp.Popen(['halcmd net emulator:probing_pulse debounce.0.0.in z_emulator.probing_pulse'], shell=True)
h['ARC_plasma_voltage'] = 100 


try:
    while 1:
        if h['Z_axis_pos'] < 30 : h['probing_pulse']=1 
        else: h['probing_pulse']= 0 
       
        time.sleep(0.1)
 
        if h['Z_axis_pos'] <40 and h['Z_axis_homed']==1: h['cylinder_out']=1
        else: h['cylinder_out']=0   

except KeyboardInterrupt:
    raise SystemExit
