def __init__(self, datasocket=None):
     threading.Thread.__init__(self)
     if datasocket is not None:
         self.datasocket = datasocket
     else:
         self.datasocket = None    
     self.measured_voltage = 0
     self.filament = {}
     self.filament['device'] = CPX.CPX400DPDriver(1, usbchannel=0)
     self.filament['voltage'] = 0
     self.filament['current'] = 0
     self.filament['idle_voltage'] = 3
     self.filament['device'].set_current_limit(5)
     self.filament['device'].output_status(True)
     self.bias = {}
     self.bias['device'] = CPX.CPX400DPDriver(2, usbchannel=0)
     self.bias['grid_voltage'] = 0
     self.bias['grid_current'] = 0
     self.bias['device'].output_status(True)
     self.looptime = 0
     self.update_setpoint(0.2)
     self.adc = DeltaSigma(0x68, 0x69, 18)
     self.adc.setPGA(8)  # Adjust this if resistor value is changed
     self.running = True
     self.wanted_voltage = 0
     self.emission_current = 999
     self.pid = pid.PID(2, 0.07, 0, 9)
     self.pid.UpdateSetpoint(self.setpoint)
 def __init__(self, datasocket=None):
     threading.Thread.__init__(self)
     if datasocket is not None:
         self.datasocket = datasocket
     else:
         self.datasocket = None    
     self.measured_voltage = 0
     self.filament = {}
     port = '/dev/serial/by-id/usb-TTI_CPX400_Series_PSU_C2F952E5-if00'
     self.filament['device'] = CPX.CPX400DPDriver(1, port)
     self.filament['voltage'] = 0
     self.filament['current'] = 0
     self.filament['idle_voltage'] = 3
     self.filament['device'].set_current_limit(5)
     self.filament['device'].output_status(True)
     self.bias = {}
     self.bias['device'] = CPX.CPX400DPDriver(2, port)
     self.bias['grid_voltage'] = 0
     self.bias['grid_current'] = 0
     self.bias['device'].output_status(True)
     self.looptime = 0
     self.update_setpoint(0.7)
     self.adc = DeltaSigma(0x68, 0x69, 18)
     self.adc.setPGA(8)  # Adjust this if resistor value is changed
     self.running = True
     self.wanted_voltage = 0
     self.emission_current = 999
     self.pid = pid.PID(2, 0.03, 0, 9)
     self.pid.UpdateSetpoint(self.setpoint)
class EmissionControl(threading.Thread):
    """ Control the emission of a filament. """
    def __init__(self, datasocket=None):
        threading.Thread.__init__(self)
        if datasocket is not None:
            self.datasocket = datasocket
        else:
            self.datasocket = None    
        self.measured_voltage = 0
        self.filament = {}
        port = '/dev/serial/by-id/usb-TTI_CPX400_Series_PSU_C2F952E5-if00'
        self.filament['device'] = CPX.CPX400DPDriver(1, port)
        self.filament['voltage'] = 0
        self.filament['current'] = 0
        self.filament['idle_voltage'] = 3
        self.filament['device'].set_current_limit(5)
        self.filament['device'].output_status(True)
        self.bias = {}
        self.bias['device'] = CPX.CPX400DPDriver(2, port)
        self.bias['grid_voltage'] = 0
        self.bias['grid_current'] = 0
        self.bias['device'].output_status(True)
        self.looptime = 0
        self.update_setpoint(0.7)
        self.adc = DeltaSigma(0x68, 0x69, 18)
        self.adc.setPGA(8)  # Adjust this if resistor value is changed
        self.running = True
        self.wanted_voltage = 0
        self.emission_current = 999
        self.pid = pid.PID(2, 0.03, 0, 9)
        self.pid.UpdateSetpoint(self.setpoint)

    def set_bias(self, bias):
        """ Set the bias-voltage """
        if self.datasocket is not None:
            self.datasocket.set_point_now('ionenergy', bias)
        if bias > -1:
            self.bias['device'].set_voltage(bias)
        if bias < 5:
            pass  # TODO: Implement check to make sure not to melt the filament

    def update_setpoint(self, setpoint):
        """ Update the setpoint """
        self.setpoint = setpoint
        if self.datasocket is not None:
            self.datasocket.set_point_now('setpoint', setpoint)

    def set_filament_voltage(self, U):
        """ Set the filament voltage """
        return(self.filament['device'].set_voltage(U))

    def read_filament_voltage(self):
        """ Read the filament voltage """
        return(self.filament['device'].read_actual_voltage())

    def read_filament_current(self):
        """ Read the filament current """
        return(self.filament['device'].read_actual_current())

    def read_grid_voltage(self):
        """Read the actual grid voltage """
        return(self.bias['device'].read_actual_voltage())

    def read_grid_current(self):
        """ Read the grid current as measured by power supply """
        return(self.bias['device'].read_actual_current())

    def read_emission_current(self):
        """ Read the actual emission current """
        value = self.adc.readVoltage(1)
        self.measured_voltage = value
        current = 1000.0 * value / 3.4  # Resistance value read off component label
        return(current)

    def run(self):
        while self.running:
            #time.sleep(0.1)
            t = time.time()
            self.emission_current = self.read_emission_current()
            self.wanted_voltage = self.pid.WantedPower(self.emission_current) + self.filament['idle_voltage']
            self.pid.UpdateSetpoint(self.setpoint)
            self.set_filament_voltage(self.wanted_voltage)
            self.filament['voltage'] = self.read_filament_voltage()
            self.filament['current'] = self.read_filament_current()
            self.bias['grid_voltage'] = self.read_grid_voltage()
            self.bias['grid_current'] = self.read_grid_current()
            if self.datasocket is not None:
                self.datasocket.set_point_now('emission', self.emission_current)
            self.looptime = time.time() - t
        self.setpoint = 0
        self.set_filament_voltage(0)
        self.set_bias(0)
from ABElectronics_DeltaSigmaPi import DeltaSigma
import time
import os

# ================================================
# ABElectronics Delta-Sigma Pi 8-Channel ADC demo
# Version 1.0 Created 09/05/2014
#
# Requires python smbus to be installed
# run with: python demo-readvoltage.py
# ================================================


# Initialise the ADC device using the default addresses and sample rate, change this value if you have changed the address selection jumpers
# Sample rate can be 12,14, 16 or 18
adc = DeltaSigma(0x68, 0x69, 18)

while (True):

  # clear the console
  os.system('clear')

  # read from adc channels and print to screen
  print ("Channel 1: %02f" % adc.readVoltage(1))
  print ("Channel 2: %02f" % adc.readVoltage(2))
  print ("Channel 3: %02f" % adc.readVoltage(3))
  print ("Channel 4: %02f" % adc.readVoltage(4))
  print ("Channel 5: %02f" % adc.readVoltage(5))
  print ("Channel 6: %02f" % adc.readVoltage(6))
  print ("Channel 7: %02f" % adc.readVoltage(7))
  print ("Channel 8: %02f" % adc.readVoltage(8))
class EmissionControl(threading.Thread):
    """ Control the emission of a filament. """
    def __init__(self, datasocket=None):
        threading.Thread.__init__(self)
        if datasocket is not None:
            self.datasocket = datasocket
        else:
            self.datasocket = None    
        self.measured_voltage = 0
        self.filament = {}
        self.filament['device'] = CPX.CPX400DPDriver(1, usbchannel=0)
        self.filament['voltage'] = 0
        self.filament['current'] = 0
        self.filament['idle_voltage'] = 3
        self.filament['device'].set_current_limit(5)
        self.filament['device'].output_status(True)
        self.bias = {}
        self.bias['device'] = CPX.CPX400DPDriver(2, usbchannel=0)
        self.bias['grid_voltage'] = 0
        self.bias['grid_current'] = 0
        self.bias['device'].output_status(True)
        self.looptime = 0
        self.update_setpoint(0.2)
        self.adc = DeltaSigma(0x68, 0x69, 18)
        self.adc.setPGA(8)  # Adjust this if resistor value is changed
        self.running = True
        self.wanted_voltage = 0
        self.emission_current = 999
        self.pid = pid.PID(2, 0.07, 0, 9)
        self.pid.UpdateSetpoint(self.setpoint)

    def set_bias(self, bias):
        """ Set the bias-voltage """
        if bias > -1:
            self.bias['device'].set_voltage(bias)
        if bias < 5:
            pass  # TODO: Implement check to make sure not to melt the filament

    def update_setpoint(self, setpoint):
        """ Update the setpoint """
        self.setpoint = setpoint
        if self.datasocket is not None:
            self.datasocket.set_point_now('setpoint', setpoint)

    def set_filament_voltage(self, U):
        """ Set the filament voltage """
        return(self.filament['device'].set_voltage(U))

    def read_filament_voltage(self):
        """ Read the filament voltage """
        return(self.filament['device'].read_actual_voltage())

    def read_filament_current(self):
        """ Read the filament current """
        return(self.filament['device'].read_actual_current())

    def read_grid_voltage(self):
        """Read the actual grid voltage """
        return(self.bias['device'].read_actual_voltage())

    def read_grid_current(self):
        """ Read the grid current as measured by power supply """
        return(self.bias['device'].read_actual_current())

    def read_emission_current(self):
        """ Read the actual emission current """
        value = self.adc.readVoltage(1)
        self.measured_voltage = value
        current = 1000.0 * value / 3.4  # Resistance value read off component label
        return(current)

    def run(self):
        while self.running:
            #time.sleep(0.1)
            t = time.time()
            self.emission_current = self.read_emission_current()
            self.wanted_voltage = self.pid.WantedPower(self.emission_current) + self.filament['idle_voltage']
            self.pid.UpdateSetpoint(self.setpoint)
            self.set_filament_voltage(self.wanted_voltage)
            self.filament['voltage'] = self.read_filament_voltage()
            self.filament['current'] = self.read_filament_current()
            self.bias['grid_voltage'] = self.read_grid_voltage()
            self.bias['grid_current'] = self.read_grid_current()
            if self.datasocket is not None:
                self.datasocket.set_point_now('emission', self.emission_current)
            self.looptime = time.time() - t
        self.setpoint = 0
        self.set_filament_voltage(0)
        self.set_bias(0)