This commit is contained in:
107
script/devices/SmartMagnet.py
Normal file
107
script/devices/SmartMagnet.py
Normal file
@@ -0,0 +1,107 @@
|
||||
class SmartMagnet(DeviceBase):
|
||||
def __init__(self, name):
|
||||
#DeviceBase.__init__(self, name, get_context().pluginManager.getDynamicClass("SmartMagnetConfig")())
|
||||
DeviceBase.__init__(self, name, DeviceConfig({
|
||||
"holdingCurrent":0.0,
|
||||
"restingCurrent":0.0,
|
||||
"mountCurrent":0.0,
|
||||
"unmountCurrent":0.0,
|
||||
"remanenceCurrent":0.0,
|
||||
}))
|
||||
|
||||
def doInitialize(self):
|
||||
DeviceBase.doInitialize(self)
|
||||
self.get_current()
|
||||
|
||||
def set_current(self, current):
|
||||
self.setCache(current, None)
|
||||
smc_current.write(current)
|
||||
|
||||
def get_current(self):
|
||||
cur = smc_current.read()
|
||||
self.setCache(cur, None)
|
||||
return cur
|
||||
|
||||
def get_current_rb(self):
|
||||
self.assert_status()
|
||||
return smc_current_rb.read()
|
||||
|
||||
def get_status(self):
|
||||
return smc_magnet_status.read()
|
||||
|
||||
def assert_status(self):
|
||||
if self.get_status() == False:
|
||||
raise Exception("Smart Magnet is in faulty status.")
|
||||
|
||||
def is_mounted(self):
|
||||
self.assert_status()
|
||||
m1 = smc_mounted_1.read()
|
||||
m2 = smc_mounted_2.read()
|
||||
if m2==m1:
|
||||
raise Exception("Smart Magnet has invalid detection.")
|
||||
return m2
|
||||
|
||||
def set_supress(self, value):
|
||||
smc_sup_det.write(value)
|
||||
|
||||
def get_supress(self):
|
||||
return smc_sup_det.read()
|
||||
|
||||
def check_mounted(self, idle_time =1.0, timeout = -1):
|
||||
self.assert_status()
|
||||
start = time.time()
|
||||
last = None
|
||||
while True:
|
||||
try:
|
||||
det = self.is_mounted()
|
||||
except:
|
||||
det = None
|
||||
if det != last:
|
||||
settling_timestamp = time.time()
|
||||
last = det
|
||||
else:
|
||||
if det is not None:
|
||||
if (time.time()-settling_timestamp > idle_time):
|
||||
return det
|
||||
if timeout >= 0:
|
||||
if (time.time() - start) > timeout:
|
||||
raise Exception("Timeout waiting for Smart Magnet detection.")
|
||||
time.sleep(0.01)
|
||||
|
||||
|
||||
def doUpdate(self):
|
||||
try:
|
||||
if self.is_mounted():
|
||||
self.setState(State.Busy)
|
||||
else:
|
||||
self.setState(State.Ready)
|
||||
except:
|
||||
self.setState(State.Fault)
|
||||
|
||||
def set_holding_current(self):
|
||||
self.set_current(self.config.getFieldValue("holdingCurrent"))
|
||||
|
||||
def set_resting_current(self):
|
||||
self.set_current(self.config.getFieldValue("restingCurrent"))
|
||||
|
||||
def set_mount_current(self):
|
||||
self.set_current(self.config.getFieldValue("mountCurrent"))
|
||||
|
||||
def set_unmount_current(self):
|
||||
self.set_current(self.config.getFieldValue("unmountCurrent"))
|
||||
|
||||
def set_remanence_current(self):
|
||||
self.set_current(self.config.getFieldValue("remanenceCurrent"))
|
||||
|
||||
def set_default_current():
|
||||
if self.is_mounted():
|
||||
self.set_holding_current()
|
||||
else:
|
||||
self.set_resting_current()
|
||||
|
||||
|
||||
add_device(SmartMagnet("smart_magnet"), force = True)
|
||||
|
||||
smart_magnet.polling = 1000
|
||||
|
||||
smart_magnet.set_default_current()
|
||||
Reference in New Issue
Block a user