class MeltingPointCalibrator(Loggable, ExecuteMixin):
    graph = Instance(StreamStackedGraph)
    setpoint = Float(enter_set=True, auto_set=False)
    record_data_manager = None
    detector = 'L2(CDD)'

    def setup(self):
        self.record_data_manager = CSVDataManager()
        self.record_data_manager.new_frame(directory=paths.device_scan_dir)
        self.graph = StreamStackedGraph()
        self.graph.new_plot()
        self.graph.new_series()
        self.graph.new_plot()
        self.graph.new_series(plotid=1)

        dl = 1.8*600
        self.graph.set_data_limits(dl)
        self.graph.set_scan_widths(600)

    def _do_execute(self):
        self.setup()
        self.laser.enable_laser()
        period = 1
        st = time.time()
        while self.executing:
            self._iter(time.time() - st)
            time.sleep(period)

        self.laser.disable_laser()
        self.record_data_manager.close_file()

    # private
    def _setpoint_changed(self, new):
        self.laser.extract(new)

    def _iter(self, t):
        intensity = self.spectrometer_manager.get_intensity(self.detector)
        temperature = self.laser.get_pyrometer_temperature()

        self._record(t, intensity, temperature)
        self._graph(t, intensity, temperature)

    def _graph(self, t, intensity, temperature):
        self.graph.record(intensity, x=t, plotid=0, track_x=True, track_y=True)
        self.graph.record(temperature, x=t, plotid=1, track_x=True, track_y=True)

    def _record(self, t, intensity, temperature):
        self.record_data_manager.write_to_frame((t, intensity, temperature))

    def traits_view(self):
        tgrp = HGroup(UItem('execute', editor=ButtonEditor(label_value='execute_label')), UItem('setpoint'))
        ggrp = VGroup(UItem('graph', style='custom'))

        v = View(VGroup(tgrp, ggrp), resizable=True, title='Melting Point Calibration')
        return v
class ResponseRecorder(HasTraits):
    period = 2
    response_data = Array
    output_data = Array

    _alive = False

    output_device = Any
    response_device = Any
    response_device_secondary = Any
    data_manager = Instance(CSVDataManager)

    _start_time = 0

    def start(self):
        t = time.time()
        self._start_time = t
        self.response_data = array([(t, 0)])
        self.output_data = array([(t, 0)])

        self.data_manager = CSVDataManager()
        self.data_manager.new_frame(base_frame_name='diode_response_tc_control')
        self.data_manager.write_to_frame(('#time', self.output_device.name,
                                          self.response_device.name,
                                          self.response_device_secondary.name))
        t = Thread(target=self.run)
        t.start()

    def run(self):
        self._alive = True
        st = self._start_time
        p = self.period
        rd = self.response_device
        rds = self.response_device_secondary
        od = self.output_device
        dm = self.data_manager

        odata = self.output_data
        rdata = self.response_data
        while self._alive:
            to = time.time()
            t = to - st
            out = od.get_output()
            odata = vstack((odata, (t, out)))

            r = rd.get_response(force=True)
            rdata = vstack((rdata, (t, r)))

            r2 = rds.get_response(force=True)

            datum = (t, out, r, r2)
            datum = map(lambda x: floatfmt(x, n=3), datum)
            dm.write_to_frame(datum)
            et = time.time() - to
            slt = p - et - 0.001
            if slt > 0:
                time.sleep(slt)

        self.output_data = odata
        self.response_data = rdata

    def stop(self):
        self._alive = False
        if self.data_manager:
            self.data_manager.close_file()

    def get_response_blob(self):
        if len(self.response_data):
            return ''.join([struct.pack('<ff', x, y) for x, y in self.response_data])

    def get_output_blob(self):
        if len(self.output_data):
            return ''.join([struct.pack('<ff', x, y) for x, y in self.output_data])
Exemple #3
0
class ResponseRecorder(Loggable):
    period = Float(2)
    response_data = Array
    output_data = Array
    setpoint_data = Array

    _alive = False

    output_device = Any
    response_device = Any
    response_device_secondary = Any
    data_manager = Instance(CSVDataManager)

    _start_time = 0
    _write_data = False

    def start(self, base_frame_name=None):
        if self._alive:
            self.debug('response recorder already alive')
            return

        t = time.time()
        self._start_time = t
        self.response_data = array([(t, 0)])
        self.output_data = array([(t, 0)])
        self.setpoint_data = array([(t, 0)])
        self._write_data = False

        if base_frame_name:
            self._write_data = True
            self.data_manager = CSVDataManager()
            self.data_manager.new_frame(base_frame_name=base_frame_name)
            self.data_manager.write_to_frame(('#time', self.output_device.name,
                                              self.response_device.name,
                                              self.response_device_secondary.name))

        t = Thread(target=self.run)
        t.setDaemon(True)
        t.start()

    def run(self):
        self.debug('start response recorder')
        self._alive = True
        st = self._start_time
        p = self.period
        rd = self.response_device
        rds = self.response_device_secondary
        od = self.output_device
        dm = self.data_manager

        wd = self._write_data

        odata = self.output_data
        rdata = self.response_data
        sdata = self.setpoint_data
        r2 = None
        while self._alive:
            to = time.time()
            t = to - st
            out = od.get_output()
            odata = vstack((odata, (t, out)))

            sp = od.get_setpoint()
            sdata = vstack((sdata, (t, sp)))

            r = rd.get_response(force=True)
            rdata = vstack((rdata, (t, r)))

            self.debug('response t={}, out={}, setpoint={}, response={}'.format(t, out, sp, r))
            if rds:
                r2 = rds.get_response(force=True)

            if wd:
                if r2:
                    datum = (t, out, sp, r, r2)
                else:
                    datum = (t, out, sp, r)

                datum = map(lambda x: floatfmt(x, n=3), datum)

                dm.write_to_frame(datum)

            et = time.time() - to
            slt = p - et - 0.001
            if slt > 0:
                time.sleep(slt)

            self.output_data = odata
            self.response_data = rdata
            self.setpoint_data = sdata

    def check_reached_setpoint(self, v, n, tol, std=None):
        """
        return True if response is OK, i.e. average of last n points is within tol of v.
        if std is not None then standard dev must be less than std
        :param v:
        :param n:
        :param tol:
        :param std:
        :return:
        """
        pts = self.response_data[-n:, 1]

        std_bit = True
        if std:
            std_bit = pts.std() < std

        error_bit = abs(pts.mean() - v) < tol

        return std_bit and error_bit

    def stop(self):
        self.debug('stop response recorder')
        self._alive = False
        if self.data_manager:
            self.data_manager.close_file()

    def get_response_blob(self):
        if len(self.response_data):
            return ''.join([struct.pack('<ff', x, y) for x, y in self.response_data])

    def get_output_blob(self):
        if len(self.output_data):
            return ''.join([struct.pack('<ff', x, y) for x, y in self.output_data])

    def get_setpoint_blob(self):
        if len(self.setpoint_data):
            return ''.join([struct.pack('<ff', x, y) for x, y in self.setpoint_data])

    @property
    def max_response(self):
        if len(self.response_data):
            return self.response_data.max()
class ResponseRecorder(Loggable):
    period = Float(2)
    response_data = Array
    output_data = Array
    setpoint_data = Array

    _alive = False

    output_device = Any
    response_device = Any
    response_device_secondary = Any
    data_manager = Instance(CSVDataManager)

    _start_time = 0
    _write_data = False

    def start(self, base_frame_name=None):
        if self._alive:
            self.debug('response recorder already alive')
            return

        t = time.time()
        self._start_time = t
        self.response_data = array([(t, 0)])
        self.output_data = array([(t, 0)])
        self.setpoint_data = array([(t, 0)])
        self._write_data = False

        if base_frame_name:
            self._write_data = True
            self.data_manager = CSVDataManager()
            self.data_manager.new_frame(base_frame_name=base_frame_name)
            self.data_manager.write_to_frame(
                ('#time', self.output_device.name, self.response_device.name,
                 self.response_device_secondary.name))

        t = Thread(target=self.run)
        t.setDaemon(True)
        t.start()

    def run(self):
        self.debug('start response recorder')
        self._alive = True
        st = self._start_time
        p = self.period
        rd = self.response_device
        rds = self.response_device_secondary
        od = self.output_device
        dm = self.data_manager

        wd = self._write_data

        odata = self.output_data
        rdata = self.response_data
        sdata = self.setpoint_data
        r2 = None
        while self._alive:
            to = time.time()
            t = to - st
            out = od.get_output()
            odata = vstack((odata, (t, out)))

            sp = od.get_setpoint()
            sdata = vstack((sdata, (t, sp)))

            r = rd.get_response(force=True)
            rdata = vstack((rdata, (t, r)))

            self.debug(
                'response t={}, out={}, setpoint={}, response={}'.format(
                    t, out, sp, r))
            if rds:
                r2 = rds.get_response(force=True)

            if wd:
                if r2:
                    datum = (t, out, sp, r, r2)
                else:
                    datum = (t, out, sp, r)

                datum = [floatfmt(x, n=3) for x in datum]

                dm.write_to_frame(datum)

            et = time.time() - to
            slt = p - et - 0.001
            if slt > 0:
                time.sleep(slt)

            self.output_data = odata
            self.response_data = rdata
            self.setpoint_data = sdata

    def check_reached_setpoint(self, v, n, tol, std=None):
        """
        return True if response is OK, i.e. average of last n points is within tol of v.
        if std is not None then standard dev must be less than std
        :param v:
        :param n:
        :param tol:
        :param std:
        :return:
        """
        pts = self.response_data[-n:, 1]

        std_bit = True
        if std:
            std_bit = pts.std() < std

        error_bit = abs(pts.mean() - v) < tol

        return std_bit and error_bit

    def stop(self):
        self.debug('stop response recorder')
        self._alive = False
        if self.data_manager:
            self.data_manager.close_file()

    def get_response_blob(self):
        if len(self.response_data):
            # return ''.join([struct.pack('<ff', x, y) for x, y in self.response_data])
            return pack('<ff', self.response_data)

    def get_output_blob(self):
        if len(self.output_data):
            return pack('<ff', self.output_data)
            # return ''.join([struct.pack('<ff', x, y) for x, y in self.output_data])

    def get_setpoint_blob(self):
        if len(self.setpoint_data):
            return pack('<ff', self.setpoint_data)
            # return ''.join([struct.pack('<ff', x, y) for x, y in self.setpoint_data])

    @property
    def max_response(self):
        if len(self.response_data):
            return self.response_data.max()
class ResponseRecorder(HasTraits):
    period = 2
    response_data = Array
    output_data = Array

    _alive = False

    output_device = Any
    response_device = Any
    response_device_secondary = Any
    data_manager = Instance(CSVDataManager)

    _start_time = 0

    def start(self):
        t = time.time()
        self._start_time = t
        self.response_data = array([(t, 0)])
        self.output_data = array([(t, 0)])

        self.data_manager = CSVDataManager()
        self.data_manager.new_frame(
            base_frame_name='diode_response_tc_control')
        self.data_manager.write_to_frame(
            ('#time', self.output_device.name, self.response_device.name,
             self.response_device_secondary.name))
        t = Thread(target=self.run)
        t.start()

    def run(self):
        self._alive = True
        st = self._start_time
        p = self.period
        rd = self.response_device
        rds = self.response_device_secondary
        od = self.output_device
        dm = self.data_manager

        odata = self.output_data
        rdata = self.response_data
        while self._alive:
            to = time.time()
            t = to - st
            out = od.get_output()
            odata = vstack((odata, (t, out)))

            r = rd.get_response(force=True)
            rdata = vstack((rdata, (t, r)))

            r2 = rds.get_response(force=True)

            datum = (t, out, r, r2)
            datum = map(lambda x: floatfmt(x, n=3), datum)
            dm.write_to_frame(datum)
            et = time.time() - to
            slt = p - et - 0.001
            if slt > 0:
                time.sleep(slt)

        self.output_data = odata
        self.response_data = rdata

    def stop(self):
        self._alive = False
        if self.data_manager:
            self.data_manager.close_file()

    def get_response_blob(self):
        if len(self.response_data):
            return ''.join(
                [struct.pack('<ff', x, y) for x, y in self.response_data])

    def get_output_blob(self):
        if len(self.output_data):
            return ''.join(
                [struct.pack('<ff', x, y) for x, y in self.output_data])