#!/usr/bin/python
import linuxcnc, hal, time, comms
vfd1 = hal.component("spdvfd")
vfd1.newpin("drivestatus", hal.HAL_U32, hal.HAL_OUT)
vfd1.newpin("outtorque", hal.HAL_FLOAT, hal.HAL_OUT)
vfd1.newpin("outfrequency", hal.HAL_FLOAT, hal.HAL_OUT)
vfd1.newpin("outcurrent", hal.HAL_FLOAT, hal.HAL_OUT)
vfd1.newpin("outvoltage", hal.HAL_FLOAT, hal.HAL_OUT)
vfd1.newpin("power", hal.HAL_FLOAT, hal.HAL_OUT)
vfd1.newpin("runtime", hal.HAL_FLOAT, hal.HAL_OUT)
vfd1.newpin("powerontime", hal.HAL_FLOAT, hal.HAL_OUT)
vfd1.newpin("run", hal.HAL_BIT, hal.HAL_IN)
vfd1.newpin("forward", hal.HAL_BIT, hal.HAL_IN)
vfd1.newpin("freqset", hal.HAL_FLOAT, hal.HAL_IN)
vfd1.ready()
vfd2 = hal.component("cltvfd")
vfd2.newpin("drivestatus", hal.HAL_U32, hal.HAL_OUT)
vfd2.newpin("errorstatus", hal.HAL_U32, hal.HAL_OUT)
vfd2.newpin("commfrequency", hal.HAL_FLOAT, hal.HAL_OUT)
vfd2.newpin("outfrequency", hal.HAL_FLOAT, hal.HAL_OUT)
vfd2.newpin("outcurrent", hal.HAL_FLOAT, hal.HAL_OUT)
vfd2.newpin("outvoltage", hal.HAL_FLOAT, hal.HAL_OUT)
vfd2.newpin("dcbusvoltage", hal.HAL_FLOAT, hal.HAL_OUT)
vfd2.newpin("run", hal.HAL_BIT, hal.HAL_IN)
vfd2.newpin("forward", hal.HAL_BIT, hal.HAL_IN)
vfd2.newpin("freqset", hal.HAL_FLOAT, hal.HAL_IN)
vfd2.ready()
vfd3 = hal.component("hydvfd")
vfd3.newpin("drivestatus", hal.HAL_U32, hal.HAL_OUT)
vfd3.newpin("errorstatus", hal.HAL_U32, hal.HAL_OUT)
vfd3.newpin("commfrequency", hal.HAL_FLOAT, hal.HAL_OUT)
vfd3.newpin("outfrequency", hal.HAL_FLOAT, hal.HAL_OUT)
vfd3.newpin("outcurrent", hal.HAL_FLOAT, hal.HAL_OUT)
vfd3.newpin("outvoltage", hal.HAL_FLOAT, hal.HAL_OUT)
vfd3.newpin("dcbusvoltage", hal.HAL_FLOAT, hal.HAL_OUT)
vfd3.newpin("run", hal.HAL_BIT, hal.HAL_IN)
vfd3.newpin("forward", hal.HAL_BIT, hal.HAL_IN)
vfd3.newpin("freqset", hal.HAL_FLOAT, hal.HAL_IN)
vfd3.ready()
baudrate = 9600
bytesize = 8
parity = comms.serial.PARITY_NONE
stopbits = 2
timeout = 0.2
port = '/dev/ttyS0'
serialvfd1 = comms.Instrument(port, 1)
serialvfd1.serial.baudrate = baudrate
serialvfd1.serial.bytesize = bytesize
serialvfd1.serial.parity = parity
serialvfd1.serial.stopbits = stopbits
serialvfd1.serial.timeout = timeout
serialvfd1.mode = comms.MODE_CUSTOM
serialvfd2 = comms.Instrument(port, 2)
serialvfd2.serial.baudrate = baudrate
serialvfd2.serial.bytesize = bytesize
serialvfd2.serial.parity = parity
serialvfd2.serial.stopbits = stopbits
serialvfd2.serial.timeout = timeout
serialvfd2.mode = comms.MODE_ASCII
serialvfd3 = comms.Instrument(port, 3)
serialvfd3.serial.baudrate = baudrate
serialvfd3.serial.bytesize = bytesize
serialvfd3.serial.parity = parity
serialvfd3.serial.stopbits = stopbits
serialvfd3.serial.timeout = timeout
serialvfd3.mode = comms.MODE_ASCII
#initialize variables to safe settings
vfd1.run, vfd2.run, vfd3.run = 0, 0, 0
vfd1.forward, vfd2.forward, vfd3.forward = 1, 1, 1
vfd1.freqset, vfd2.freqset, vfd3.freqset = 7.49, 60, 60
cmd2, cmd3 = 17, 17
try:
while 1:
time.sleep(0.02)
#-----------------SPINDLE VFD--------------------
readparamA = []
responseA=serialvfd1._performCommand(3, "none")
for i in xrange(0,len(responseA)/8):
readparamA.append(responseA[i*8+2:8*i+8+2])
vfd1.outfrequency = float(readparamA[0])/10000
vfd1.outcurrent = float(readparamA[1])/1000
vfd1.drivestatus = int(readparamA[2])
vfd1.outtorque = int(readparamA[7])/100
vfd1.outvoltage = float(readparamA[8])/1000
vfd1.power = float(readparamA[9])/1000
vfd1.runtime = int(readparamA[11])
vfd1.powerontime = int(readparamA[12])
#start/stop/cw/ccw
if vfd1.run and vfd1.forward: #run CW
serialvfd1._performCommand(0, "1")
elif vfd1.run and not vfd1.forward: #run CCW
serialvfd1._performCommand(0, "2")
else: #do not run. keep resending this for safety.
serialvfd1._performCommand(0, "0")
#set the frequency
serialvfd1._performCommand(1, '%06d' % (vfd1.freqset*100))
#---------------COOLANT VFD-----------------
vfd2.errorstatus = int(serialvfd2.read_register(0x2100, 0))
vfd2.drivestatus = int(serialvfd2.read_register(0x2101, 0))
vfd2.commfrequency = float(serialvfd2.read_register(0x2102, 0))/100
vfd2.outfrequency = float(serialvfd2.read_register(0x2103, 0))/100
vfd2.outcurrent = float(serialvfd2.read_register(0x2104, 0))/10
vfd2.dcbusvoltage = float(serialvfd2.read_register(0x2105, 0))/10
vfd2.outvoltage = float(serialvfd2.read_register(0x2106, 0))
#start/stop/cw/ccw
if vfd2.run and vfd2.forward: #run CW
cmd2 = 18
elif vfd2.run and not vfd2.forward: #run CCW
cmd2 = 34
elif not vfd2.run and not vfd2.forward: #stop in CCW mode
cmd2 = 33
else: #stop in CW mode by default
cmd2 = 17
serialvfd2.write_register(0x2000,cmd2,0,6)
#set the frequency
serialvfd2.write_register(0x2001,vfd2.freqset*100,0,6)
#---------------HYDRAULICS VFD--------------------
vfd3.errorstatus = int(serialvfd3.read_register(0x2100, 0))
vfd3.drivestatus = int(serialvfd3.read_register(0x2101, 0))
vfd3.commfrequency = float(serialvfd3.read_register(0x2102, 0))/100
vfd3.outfrequency = float(serialvfd3.read_register(0x2103, 0))/100
vfd3.outcurrent = float(serialvfd3.read_register(0x2104, 0))/10
vfd3.dcbusvoltage = float(serialvfd3.read_register(0x2105, 0))/10
vfd3.outvoltage = float(serialvfd3.read_register(0x2106, 0))
#start/stop/cw/ccw
if vfd3.run and vfd3.forward: #run CW
cmd3 = 18
elif vfd3.run and not vfd3.forward: #run CCW
cmd3 = 34
elif not vfd3.run and not vfd3.forward: #stop in CCW mode
cmd3 = 33
else: #stop in CW mode by default
cmd3 = 17
serialvfd3.write_register(0x2000,cmd3,0,6)
#set the frequency
serialvfd3.write_register(0x2001,vfd3.freqset*100,0,6)
except KeyboardInterrupt:
raise SystemExit