예제 #1
0
    def __init__(self, num_samples, start_freq, end_freq, set_voltage, Father,
                 RE_VAL):
        Thread.__init__(self)
        # Initialize Relay
        self.REL = relay.Relay_module()
        self.REL.reset()

        if APP_IMP_PRESENT:
            self.IMP = Impedanz_4192A_Eth.Impedance_Analyser(IMP_IP, IMP_PORT)
            self.IMP.startComm()

        # create file and title
        self.CF = data_storage.create_file()
        self.test_mode = "Auto"
        self.equipment_Info = "TTC-4006 + IMP-4192A"
        Driver_root = detect_file.File(folder)[1]
        start_time = str(datetime.datetime.now())
        filename = start_time.replace(" ", "_").replace(".",
                                                        "-").replace(":", "-")
        self.CF.folder(Driver_root, filename)

        self.Oven = getCurrentOven()

        self.CurrentStep = -1

        self.NEXTION_NUM_SAMPLES = num_samples
        self.NEXTION_START_FREQ = start_freq
        self.NEXTION_END_FREQ = end_freq
        self.NEXTION_SET_VOLTAGE = set_voltage

        self.Father = Father
        self.RE_VAL = RE_VAL
예제 #2
0
    def main(self, num, interval, Father):
        RE_VAL = set()
        t_start = time.time()
        self.num = num
        self.interval = interval
        REL = relay.Relay_module()
        REL.reset()
        while True:
            for i in range(num):
                t_now = time.time()
                t_run = t_now - t_start
                REL.RelaySelect(i)
                print(i)
                sleep(interval)
                REL.RelayDeSelect(i)

                Father.updateRelay([str(t_run), i, ])

                DE = str(DIS.read())
                # print (DE)
                RE_VAL.add(DE)
                # print (RE_VAL)

                if "['e\\x10\\x02\\x01\\xff\\xff\\xff']" in RE_VAL:
                    print("Exiting")
                    RE_VAL.clear()
                    DIS.write("page Device Select\xff\xff\xff")
                    return

                elif "['e\\x10\\x01\\x01\\xff\\xff\\xff']" in RE_VAL:
                    # DIS.write("rest\xff\xff\xff")
                    RE_VAL.clear()
                    DIS.write("page restart\xff\xff\xff")
                    os.system("sudo reboot")

                print('07 - Updating Display...')
    def main(self, num_samples, set_temperature, start_freq, end_freq,
             set_voltage, Father):
        RE_VAL = set()

        # Initialize Relay
        REL = relay.Relay_module()
        REL.reset()

        # set PID parameters
        P = 15
        I = 0
        D = 0

        pid = PID.PID(P, I, D)
        pid.setSampleTime(1)

        self.NEXTION_NUM_SAMPLES = num_samples
        self.NEXTION_SET_TEMP = float(
            format(float(set_temperature) / 0.84, ".2f"))
        self.NEXTION_START_FREQ = start_freq
        self.NEXTION_END_FREQ = end_freq
        self.NEXTION_SET_VOLTAGE = set_voltage

        # import oven and multimeter
        if APP_OVEN_PRESENT:
            OVEN = VT4002_SM.VT4002(VT4002_IP)
            OVEN.startComm()

        if APP_IMP_PRESENT:
            IMP = Impedanz_4192A_Eth.Impedance_Analyser(IMP_IP, IMP_PORT)
            IMP.startComm()

        # create file and title
        CF = data_storage.create_file()
        test_mode = "Auto"
        equipment_Info = "TTC-4006 + IMP-4192A"
        Driver_root = detect_file.File(folder)[1]
        start_time = str(datetime.datetime.now())
        filename = start_time.replace(" ", "_").replace(".",
                                                        "-").replace(":", "-")
        CF.folder(Driver_root, filename)

        t_start = time.time()

        t1 = datetime.datetime.now()
        t2 = datetime.datetime.now() + datetime.timedelta(seconds=20 * 60)

        while True:
            if APP_OVEN_PRESENT:
                # run oven)
                print("01 - Reading data from Oven...")
                temp = OVEN.read_temp()
                temp_set = temp[0]
                temp_real = temp[1]
            else:
                temp_set = format(1.00, "0.2f")
                temp_real = format(1.00, "0.2f")

            pid.SetPoint = self.NEXTION_SET_TEMP
            pid.setKp(float(P))
            pid.setKi(float(I))
            pid.setKd(float(D))

            # read temperature sensor
            # Humidity Sensor
            # If is not OK => apply non valid temp and humidity
            print("02 - Reading data from Humidity Sensor...")
            if APP_BME_280_PRESENT:
                try:
                    temperature, pressure, humidity = bme280.readBME280All()

                    # Medicine
                    if ((humidity == None) or (temperature == None)):
                        humidity = BME_280_INVALID_HUMI
                        temperature = BME_280_INVALID_TEMP
                        print(
                            "02 - Reading data from Humidity Sensor (NONE! - ERROR)..."
                        )
                    elif ((type(humidity) == str)
                          or (type(temperature) == str)):
                        humidity = BME_280_INVALID_HUMI
                        temperature = BME_280_INVALID_TEMP
                        print(
                            "02 - Reading data from Humidity Sensor (INVALID STRING! - ERROR)..."
                        )

                except:
                    humidity = BME_280_INVALID_HUMI
                    temperature = BME_280_INVALID_TEMP
                    print(
                        "02 - Reading data from Humidity Sensor (INVALID STRING! - ERROR)..."
                    )

            else:
                print("02 - Reading data from Humidity Sensor (DISABLED)...")
                humidity = BME_280_INVALID_HUMI
                temperature = BME_280_INVALID_TEMP

            HUMI_sensor = format(humidity, "0.2f")
            TEMP_sensor = format(temperature, "0.2f")
            print("02 - Reading data from Humidity Sensor: Temp(oC): ",
                  TEMP_sensor)
            print("02 - Reading data from Humidity Sensor: Humi(%): ",
                  HUMI_sensor)

            print("Sensor Temperature : ", str(TEMP_sensor))

            pid.update(float(TEMP_sensor))

            target_temperature = pid.output

            if target_temperature > 130:
                target_temperature = 130
            elif target_temperature < -40:
                target_temperature = -40
            else:
                target_temperature = target_temperature

            print("PID set Temperature : ", str(target_temperature))
            print("Chamber real Temperature : ", temp_real)

            OVEN.set_temp(target_temperature)

            t_step = time.time()
            while (t1 > t2):
                for i in range(self.NEXTION_NUM_SAMPLES):
                    # run time
                    t_run = format(t_step - t_start, "0.2f")

                    # relay selection
                    print("03 - Swtich Relay: %d" % i)
                    REL.RelaySelect(i)
                    sleep(RELAY_HOLDING_TIME)

                    # create folder for each sample
                    current_time = str(datetime.datetime.now())
                    self.time_str = current_time.replace(" ", "_").replace(
                        ".", "-").replace(":", "-")

                    Father.updateIMPSweep([
                        temp_set, temp_real, TEMP_sensor, HUMI_sensor, t_run,
                        "Measureing", " ", i
                    ])

                    DE = str(DIS.read())
                    # print (DE)
                    RE_VAL.add(DE)
                    # print (RE_VAL)

                    if "['e\\x11\\x04\\x01\\xff\\xff\\xff']" in RE_VAL:
                        print("Exiting")
                        OVEN.close()
                        RE_VAL.clear()
                        DIS.write("page Device Select\xff\xff\xff")
                        return

                    elif "['e\\x11\\x05\\x01\\xff\\xff\\xff']" in RE_VAL:
                        # DIS.write("rest\xff\xff\xff")
                        RE_VAL.clear()
                        DIS.write("page restart\xff\xff\xff")
                        os.system("sudo reboot")

                    print("07 - Updating Display...")

                    # creat file
                    sample_time = str(datetime.datetime.now()).replace(
                        " ", "_").replace(".", "-").replace(":", "-")
                    name = "Sample" + str(i)
                    locals()['v' + str(i)] = i
                    PA = CF.sample_folder(name)
                    CF.header_imp(PA, self.time_str, self.time_str,
                                  equipment_Info, test_mode,
                                  self.NEXTION_START_FREQ,
                                  self.NEXTION_END_FREQ,
                                  self.NEXTION_SET_VOLTAGE)

                    # run multimeter
                    print("04- Multimeter DMM196 Reading...")
                    if APP_IMP_PRESENT:
                        data = IMP.sweep_measure(self.NEXTION_START_FREQ,
                                                 self.NEXTION_END_FREQ,
                                                 self.NEXTION_SET_VOLTAGE)
                        CF.content(PA, self.time_str, data)

                    # relay reset
                    print("06 - Swtich Relay Unselection: %d" % i)
                    REL.RelayDeSelect(i)

            else:
                # run time
                t_run = format(t_step - t_start, "0.2f")

                Father.updateIMPSweep([
                    temp_set, temp_real, TEMP_sensor, HUMI_sensor, t_run,
                    "Waiting", " ", 0
                ])

                DE = str(DIS.read())
                # print (DE)
                RE_VAL.add(DE)
                # print (RE_VAL)

                if "['e\\x0f\\x1b\\x01\\xff\\xff\\xff']" in RE_VAL:
                    print("Exiting")
                    OVEN.close()
                    RE_VAL.clear()
                    DIS.write("page Device Select\xff\xff\xff")
                    return

                elif "['e\\x0f\\x1c\\x01\\xff\\xff\\xff']" in RE_VAL:
                    # DIS.write("rest\xff\xff\xff")
                    RE_VAL.clear()
                    DIS.write("page restart\xff\xff\xff")
                    os.system("sudo reboot")

                print("07 - Updating Display...")

                t1 = datetime.datetime.now()
    def main(self, num_samples, set_temperature, Father):
        RE_VAL = set()

        # Initialize Relay
        REL = relay.Relay_module()
        REL.reset()

        # set PID parameters
        P = 15
        I = 0
        D = 0

        pid = PID.PID(P, I, D)
        pid.setSampleTime(1)

        if APP_NEXTION_PRESENT:
            self.NEXTION_NUM_SAMPLES = num_samples
            self.NEXTION_SET_TEMP = float(format(float(set_temperature) / 0.94, ".2f"))
        else:
            self.NEXTION_NUM_SAMPLES = 8
            self.NEXTION_SET_TEMP = 25

        # import oven and multimeter
        if APP_OVEN_PRESENT:
            OVEN = VT4002_SM.VT4002(VT4002_IP)
            OVEN.startComm()

        if APP_DMM_196_PRESENT:
            Multimeter = DMM_196.DMM_196(DMM_IP, DMM_PORT)
            Multimeter.startComm()

        # create file and title
        CF = data_storage.create_file()
        test_mode = "Manual"
        equipment_Info = "VT-4002 + DMM-196"
        Driver_root = detect_file.File(folder)[1]
        start_time = str(datetime.datetime.now())
        filename = start_time.replace(" ", "_").replace(".", "-").replace(":", "-")
        PATH = CF.folder(Driver_root, filename)
        CF.header(PATH, filename, start_time, equipment_Info, test_mode, self.NEXTION_NUM_SAMPLES)
        CF.content(PATH, filename,
                   ("Time(s)\tSetTemp.(oC)\tActual Temp.(oC)\tHumidity(%)\tTemp.Sensor(oC)\tSample number\tResistence(ohm)\r\n"))

        t_start = time.time()

        while True:
            # Relay reset
            REL.reset()

            for i in range(self.NEXTION_NUM_SAMPLES):
                if APP_OVEN_PRESENT:
                    # run oven)
                    print("01 - Reading data from Oven...")
                    temp = OVEN.read_temp()
                    temp_set = temp[0]
                    temp_real = temp[1]
                else:
                    temp_set = format(1.00, "0.2f")
                    temp_real = format(1.00, "0.2f")

                pid.SetPoint = self.NEXTION_SET_TEMP
                pid.setKp(float(P))
                pid.setKi(float(I))
                pid.setKd(float(D))

                # read temperature sensor
                # Humidity Sensor
                # If is not OK => apply non valid temp and humidity
                print("02 - Reading data from Humidity Sensor...")
                if APP_BME_280_PRESENT:
                    try:
                        temperature, pressure, humidity = bme280.readBME280All()

                        # Medicine
                        if ((humidity == None) or (temperature == None)):
                            humidity = BME_280_INVALID_HUMI
                            temperature = BME_280_INVALID_TEMP
                            print("02 - Reading data from Humidity Sensor (NONE! - ERROR)...")
                        elif ((type(humidity) == str) or (type(temperature) == str)):
                            humidity = BME_280_INVALID_HUMI
                            temperature = BME_280_INVALID_TEMP
                            print("02 - Reading data from Humidity Sensor (INVALID STRING! - ERROR)...")

                    except:
                        humidity = BME_280_INVALID_HUMI
                        temperature = BME_280_INVALID_TEMP
                        print("02 - Reading data from Humidity Sensor (INVALID STRING! - ERROR)...")

                else:
                    print("02 - Reading data from Humidity Sensor (DISABLED)...")
                    humidity = BME_280_INVALID_HUMI
                    temperature = BME_280_INVALID_TEMP

                HUMI_sensor = format(humidity, "0.2f")
                TEMP_sensor = format(temperature, "0.2f")
                print("02 - Reading data from Humidity Sensor: Temp(oC): ", TEMP_sensor)
                print("02 - Reading data from Humidity Sensor: Humi(%): ", HUMI_sensor)

                print("Sensor Temperature : ", str(TEMP_sensor))

                pid.update(TEMP_sensor)

                target_temperature = pid.output

                if target_temperature > 130:
                    target_temperature = 130
                elif target_temperature < -40:
                    target_temperature = -40
                else:
                    target_temperature = target_temperature

                print("PID set Temperature : ", str(target_temperature))
                print("Chamber real Temperature : ", temp_real)

                OVEN.set_temp(target_temperature)

                # run time
                t_step = time.time()
                t_run = format(t_step - t_start, "0.2f")

                # relay selection
                print("03 - Swtich Relay: %d" % i)
                REL.RelaySelect(i)
                sleep(RELAY_HOLDING_TIME)

                # run multimeter
                print("04- Multimeter DMM196 Reading...")
                if APP_DMM_196_PRESENT:
                    mval = Multimeter.read_resiatance()
                else:
                    mval = (i * float(t_run))

                R_value = str(mval)

                # relay reset
                print("05 - Swtich Relay Unselection: %d" % i)
                REL.RelayDeSelect(i)

                # Persistency
                print("06 - Saving data...")
                result1 = []
                result2 = []
                result1.append([str(t_run), str(temp_set), str(temp_real), str(HUMI_sensor), str(TEMP_sensor), str(i), str(R_value)])
                CF.content(PATH, filename, result1)

                # create file for each sample
                result2.append([str(t_run), str(temp_set), str(temp_real), str(HUMI_sensor), str(TEMP_sensor), str(i), str(R_value)])
                CF.content(PATH, 'Sample' + str(i), result2)

                Father.updateMultimeter([temp_set, temp_real, TEMP_sensor, HUMI_sensor, t_run, R_value, i])

                DE = str(DIS.read())
                # print (DE)
                RE_VAL.add(DE)
                # print (RE_VAL)

                if "['e\\x0e\\x13\\x01\\xff\\xff\\xff']" in RE_VAL:
                    print("Exiting")
                    OVEN.close()
                    RE_VAL.clear()
                    DIS.write("page Device Select\xff\xff\xff")
                    return

                elif "['e\\x0e\\x14\\x01\\xff\\xff\\xff']" in RE_VAL:
                    # DIS.write("rest\xff\xff\xff")
                    RE_VAL.clear()
                    DIS.write("page restart\xff\xff\xff")
                    os.system("sudo reboot")

                print("07 - Updating Display...")
    def main(self, num_samples, voltage, frequency, Father):
        RE_VAL = set()

        # Initialize Relay
        REL = relay.Relay_module()
        REL.reset()

        # detect file
        if APP_PEN_DRIVE:
            FILE_NAME = detect_file.File(folder)[0]
        else:
            FILE_NAME = "/home/pi/Desktop/Temperature_profile.txt"

        # Load Profile
        TP = readTempProfile(FILE_NAME)[0]
        print(TP)

        self.NEXTION_NUM_SAMPLES = num_samples

        if APP_OVEN_PRESENT:
            OVEN = VT4002_SM.VT4002(VT4002_IP)
            OVEN.startComm()

        P = 5
        I = 0
        D = 0

        pid = PID.PID(P, I, D)
        pid.setSampleTime(1)

        if APP_LCR_PRESENT:
            LCR = LCR_Eth.LCR_Meter(LCR__IP, LCR_Port)
            LCR.startComm()

        # create file and title
        CF = data_storage.create_file()
        test_mode = "Auto"
        equipment_Info = "VT-4002 + LCR-4284A"

        if APP_PEN_DRIVE:
            Driver_root = detect_file.File(folder)[1]
        else:
            Driver_root = "/home/pi/Desktop/TTC_LCR/"

        start_time = str(datetime.datetime.now())
        filename = start_time.replace(" ", "_").replace(".",
                                                        "-").replace(":", "-")
        PATH = CF.folder(Driver_root, filename)
        CF.header(PATH, filename, start_time, equipment_Info, test_mode,
                  self.NEXTION_NUM_SAMPLES)
        CF.content(
            PATH, filename,
            "Time(s)\tSetTemp.(oC)\tActual Temp.(oC)\tHumidity(%)\tTemp.Sensor(oC)\tSample number\tR\tX\tCP\tRP\r\n"
        )

        t_start = time.time()

        for step in TP:
            print(step)
            # setting the oven
            step_time = step[0] * 60  # step_time in seconds
            step_temp = float(format(float(step[1]) / 0.94, ".2f"))

            t1 = datetime.datetime.now()
            t2 = datetime.datetime.now() + datetime.timedelta(
                seconds=step_time)

            while (t1 < t2):
                for i in range(self.NEXTION_NUM_SAMPLES):
                    if APP_OVEN_PRESENT:
                        # run oven)
                        print("01 - Reading data from Oven...")
                        temp = OVEN.read_temp()
                        temp_set = temp[0]
                        temp_real = temp[1]
                    else:
                        temp_set = format(1.00, "0.2f")
                        temp_real = format(1.00, "0.2f")

                    pid.SetPoint = step_temp
                    pid.setKp(float(P))
                    pid.setKi(float(I))
                    pid.setKd(float(D))

                    print("02 - Reading data from Humidity Sensor...")
                    if APP_BME_280_PRESENT:
                        try:
                            temperature, pressure, humidity = bme280.readBME280All(
                            )

                            # Medicine
                            if ((humidity == None) or (temperature == None)):
                                humidity = BME_280_INVALID_HUMI
                                temperature = BME_280_INVALID_TEMP
                                print(
                                    "02 - Reading data from Humidity Sensor (NONE! - ERROR)..."
                                )
                            elif ((type(humidity) == str)
                                  or (type(temperature) == str)):
                                humidity = BME_280_INVALID_HUMI
                                temperature = BME_280_INVALID_TEMP
                                print(
                                    "02 - Reading data from Humidity Sensor (INVALID STRING! - ERROR)..."
                                )

                        except:
                            humidity = BME_280_INVALID_HUMI
                            temperature = BME_280_INVALID_TEMP
                            print(
                                "02 - Reading data from Humidity Sensor (INVALID STRING! - ERROR)..."
                            )

                    else:
                        print(
                            "02 - Reading data from Humidity Sensor (DISABLED)..."
                        )
                        humidity = BME_280_INVALID_HUMI
                        temperature = BME_280_INVALID_TEMP

                    HUMI_sensor = format(humidity, "0.2f")
                    TEMP_sensor = format(temperature, "0.2f")
                    print('02 - Reading data from Humidity Sensor: Temp(oC): ',
                          TEMP_sensor)
                    print('02 - Reading data from Humidity Sensor: Humi(%): ',
                          HUMI_sensor)

                    print("Sensor Temperature : ", str(TEMP_sensor))

                    pid.update(float(TEMP_sensor))

                    target_temperature = pid.output

                    if target_temperature > 180:
                        target_temperature = 180
                    elif target_temperature < -40:
                        target_temperature = -40
                    else:
                        target_temperature = target_temperature

                    OVEN.set_temp(target_temperature)

                    print("PID set Temperature : ", str(target_temperature))
                    print("Chamber real Temperature : ", temp_real)

                    # run time
                    t_step = time.time()
                    t_r = t_step - t_start
                    t_run = format(t_r, "0.2f")

                    REL.RelaySelect(i)

                    if APP_LCR_PRESENT:
                        FinalD = LCR.measure(voltage, frequency)
                        print("LCR Reading: ", FinalD)
                        CP = str(FinalD[0])
                        RP = str(FinalD[1])
                        R_value = str(FinalD[2])
                        X_value = str(FinalD[3])
                    else:
                        CP = 0
                        RP = 0
                        R_value = 0
                        X_value = 0

                    REL.RelayDeSelect(i)

                    # Persistency
                    print("06 - Saving data...")
                    print("")
                    result1 = []
                    result2 = []
                    result1.append([
                        str(t_run),
                        str(step[1]),
                        str(temp_real),
                        str(HUMI_sensor),
                        str(TEMP_sensor),
                        str(i),
                        str(R_value),
                        str(X_value),
                        str(CP),
                        str(RP)
                    ])
                    CF.content(PATH, filename, result1)

                    # create file for each sample
                    result2.append([
                        str(t_run),
                        str(step[1]),
                        str(temp_real),
                        str(HUMI_sensor),
                        str(TEMP_sensor),
                        str(i),
                        str(R_value),
                        str(X_value),
                        str(CP),
                        str(RP)
                    ])
                    CF.content(PATH, 'Sample' + str(i), result2)

                    Father.Update([
                        temp_set, temp_real, TEMP_sensor, humidity, t_run,
                        R_value, X_value, i
                    ])

                    DE = str(DIS.read())
                    # print (DE)
                    RE_VAL.add(DE)
                    # print (RE_VAL)

                    if "['e\\x0f\\x1b\\x01\\xff\\xff\\xff']" in RE_VAL:
                        print("Exiting")
                        OVEN.close()
                        RE_VAL.clear()
                        DIS.write("page Device Select\xff\xff\xff")
                        return

                    elif "['e\\x0f\\x1c\\x01\\xff\\xff\\xff']" in RE_VAL:
                        # DIS.write("rest\xff\xff\xff")
                        RE_VAL.clear()
                        DIS.write("page restart\xff\xff\xff")
                        os.system("sudo reboot")

                    print("07 - Updating Display...")

                t1 = datetime.datetime.now()

        if APP_OVEN_PRESENT:
            OVEN.close()
예제 #6
0
    def main(self,num_samples, set_voltage, Father):
        RE_VAL = set()

        # Initialize Relay
        REL = relay.Relay_module()
        REL.reset()

        # detect file
        if APP_PEN_DRIVE:
            FILE_NAME = detect_file.File(folder)[0]
        else:
            FILE_NAME = "/home/pi/Desktop/Temperature_profile.txt"

        # Load Profile
        TP = readTempProfile(FILE_NAME)[0]
        print(TP)

        self.NEXTION_NUM_SAMPLES = num_samples
        self.NEXTION_SET_VOLTAGE = set_voltage

        # import oven and multimeter
        if APP_OVEN_PRESENT:
            OVEN = VT4002_SM.VT4002(VT4002_IP)
            OVEN.startComm()

        P = 5
        I = 0
        D = 0

        pid = PID.PID(P, I, D)
        pid.setSampleTime(1)

        if APP_Sourcemeter_PRESENT:
            Sourcemeter = Keithley_2602_Eth.SourceMeter(SM_IP, SM_PORT)
            Sourcemeter.startComm()

        # create file and title
        CF = data_storage.create_file()
        test_mode = "Auto"
        equipment_Info = "VT-4002 + Sourcemeter 2602"
        Driver_root = detect_file.File(folder)[1]
        start_time = str(datetime.datetime.now())
        filename = start_time.replace(" ", "_").replace(".", "-").replace(":", "-")
        PATH = CF.folder(Driver_root, filename)
        CF.header(PATH, filename, start_time, equipment_Info, test_mode, self.NEXTION_NUM_SAMPLES)
        CF.content(PATH, filename,
                   ("Time(s)\tSetTemp.(oC)\tActual Temp.(oC)\tHumidity(%)\tTemp.Sensor(oC)\tSample number\tCurrent(A)\tResistence(ohm)\r\n"))

        t_start = time.time()

        for step in TP:
            print(step)
            # setting the oven
            step_time = step[0] * 60  # step_time in seconds
            step_temp = float(format(float(step[1]) / 0.94, ".2f"))

            t1 = datetime.datetime.now()
            t2 = datetime.datetime.now() + datetime.timedelta(seconds=step_time)

            while( t1 < t2 ):
                # Relay switching
                REL.reset()

                for i in range(self.NEXTION_NUM_SAMPLES):
                    if APP_OVEN_PRESENT:
                        # run oven)
                        print("01 - Reading data from Oven...")
                        temp = OVEN.read_temp()
                        temp_set = temp[0]
                        temp_real = temp[1]
                    else:
                        temp_set = format(1.00, "0.2f")
                        temp_real = format(1.00, "0.2f")

                    pid.SetPoint = step_temp
                    pid.setKp(float(P))
                    pid.setKi(float(I))
                    pid.setKd(float(D))

                    print("02 - Reading data from Humidity Sensor...")
                    if APP_BME_280_PRESENT:
                        try:
                            temperature, pressure, humidity = bme280.readBME280All()

                            # Medicine
                            if ((humidity == None) or (temperature == None)):
                                humidity = BME_280_INVALID_HUMI
                                temperature = BME_280_INVALID_TEMP
                                print("02 - Reading data from Humidity Sensor (NONE! - ERROR)...")
                            elif ((type(humidity) == str) or (type(temperature) == str)):
                                humidity = BME_280_INVALID_HUMI
                                temperature = BME_280_INVALID_TEMP
                                print("02 - Reading data from Humidity Sensor (INVALID STRING! - ERROR)...")

                        except:
                            humidity = BME_280_INVALID_HUMI
                            temperature = BME_280_INVALID_TEMP
                            print("02 - Reading data from Humidity Sensor (INVALID STRING! - ERROR)...")

                    else:
                        print("02 - Reading data from Humidity Sensor (DISABLED)...")
                        humidity = BME_280_INVALID_HUMI
                        temperature = BME_280_INVALID_TEMP

                    HUMI_sensor = format(humidity, "0.2f")
                    TEMP_sensor = format(temperature, "0.2f")
                    print("02 - Reading data from Humidity Sensor: Temp(oC): ", TEMP_sensor)
                    print("02 - Reading data from Humidity Sensor: Humi(%): ", HUMI_sensor)

                    print("Sensor Temperature : ", str(TEMP_sensor))

                    pid.update(float(TEMP_sensor))

                    target_temperature = pid.output

                    if target_temperature > 180:
                        target_temperature = 180
                    elif target_temperature < -40:
                        target_temperature = -40
                    else:
                        target_temperature = target_temperature

                    OVEN.set_temp(target_temperature)

                    print("PID set Temperature : ", str(target_temperature))
                    print("Chamber real Temperature : ", temp_real)

                    # run time
                    t_step = time.time()
                    t_run = format(t_step - t_start, "0.2f")

                    REL.RelaySelect(i)
                    sleep(RELAY_HOLDING_TIME)

                    if APP_Sourcemeter_PRESENT:
                        fcurrent = Sourcemeter.Measure("a", self.NEXTION_SET_VOLTAGE)
                        print("Reading Sourcemeter:", i)
                    else:
                        fcurrent = (i*float(t_run))

                    I_Value = format(fcurrent, ".3E")

                    R_Value = format(self.NEXTION_SET_VOLTAGE / float(I_Value), ".3E")

                    print("Resistance:", R_Value)
                    print("")

                    REL.RelayDeSelect(i)

                    # Persistency
                    print("06 - Saving data...")
                    result1 = []
                    result2 = []
                    result1.append([
                        str(t_run), str(step[1]), str(temp_real), str(HUMI_sensor), str(TEMP_sensor), str(i),
                        str(I_Value),
                        str(R_Value)])
                    CF.content(PATH, filename, result1)

                    # create file for each sample
                    result2.append([
                        str(t_run), str(step[1]), str(temp_real), str(HUMI_sensor), str(TEMP_sensor), str(i),
                        str(I_Value),
                        str(R_Value)])
                    CF.content(PATH, "Sample" + str(i), result2)

                    Father.updateMultimeter([temp_set, temp_real, TEMP_sensor, HUMI_sensor, t_run, R_Value, i])

                    DE = str(DIS.read())
                    # print (DE)
                    RE_VAL.add(DE)
                    # print (RE_VAL)

                    if "['e\\x0e\\x13\\x01\\xff\\xff\\xff']" in RE_VAL:
                        print("Exiting")
                        OVEN.close()
                        RE_VAL.clear()
                        DIS.write("page Device Select\xff\xff\xff")
                        return

                    elif "['e\\x0e\\x14\\x01\\xff\\xff\\xff']" in RE_VAL:
                        # DIS.write("rest\xff\xff\xff")
                        RE_VAL.clear()
                        DIS.write("page restart\xff\xff\xff")
                        os.system("sudo reboot")

                    print("07 - Updating Display...")

                t1 = datetime.datetime.now()

        if APP_OVEN_PRESENT:
            OVEN.close()
예제 #7
0
    def main(self, num_samples, set_temperature, setV, setF, Father):
        RE_VAL = set()

        # Initialize Relay
        REL = relay.Relay_module()
        REL.reset()

        # set PID parameters
        P = 5
        I = 0
        D = 0

        pid = PID.PID(P, I, D)
        pid.setSampleTime(1)

        if APP_NEXTION_PRESENT:
            self.NEXTION_NUM_SAMPLES = num_samples
            self.NEXTION_SET_TEMP = float(
                format(float(set_temperature) / 0.84, ".2f"))
        else:
            self.NEXTION_NUM_SAMPLES = 8
            self.NEXTION_SET_TEMP = 25

        # import oven and multimeter
        if APP_OVEN_PRESENT:
            OVEN = TTC4006_Tira.TTC4006(TTC_SERIAL_PORT)
            OVEN.TTC_ON()
            OVEN.TTC_ENABLE_TEMP()

        if APP_DMM_196_PRESENT:
            LCR = LCR_Eth.LCR_Meter(LCR_IP, LCR_PORT)
            LCR.startComm()

        # create file and title
        CF = data_storage.create_file()
        test_mode = "Manual"
        equipment_Info = "VT-4002 + LCR-4192A"
        Driver_root = detect_file.File(folder)[1]
        start_time = str(datetime.datetime.now())
        filename = start_time.replace(" ", "_").replace(".",
                                                        "-").replace(":", "-")
        PATH = CF.folder(Driver_root, filename)
        CF.header(PATH, filename, start_time, equipment_Info, test_mode,
                  self.NEXTION_NUM_SAMPLES)
        CF.content(
            PATH, filename,
            "Time(s)\tSetTemp.(oC)\tActual Temp.(oC)\tHumidity(%)\tTemp.Sensor(oC)\tSample number\tR\tX\tCP\tRP\r\n"
        )

        t_start = time.time()

        while True:
            for i in range(self.NEXTION_NUM_SAMPLES):
                if APP_OVEN_PRESENT:
                    # run oven)
                    print("01 - Reading data from Oven...")
                    temp_real = OVEN.TTC_Read_PV_Temp()
                    temp_set = OVEN.TTC_Read_SP_Temp()
                else:
                    temp_set = format(1.00, "0.2f")
                    temp_real = format(1.00, "0.2f")

                pid.SetPoint = self.NEXTION_SET_TEMP
                pid.setKp(float(P))
                pid.setKi(float(I))
                pid.setKd(float(D))

                # read temperature sensor
                # Humidity Sensor
                # If is not OK => apply non valid temp and humidity
                print("02 - Reading data from Humidity Sensor...")
                if APP_BME_280_PRESENT:
                    try:
                        temperature, pressure, humidity = bme280.readBME280All(
                        )

                        # Medicine
                        if ((humidity == None) or (temperature == None)):
                            humidity = BME_280_INVALID_HUMI
                            temperature = BME_280_INVALID_TEMP
                            print(
                                "02 - Reading data from Humidity Sensor (NONE! - ERROR)..."
                            )
                        elif ((type(humidity) == str)
                              or (type(temperature) == str)):
                            humidity = BME_280_INVALID_HUMI
                            temperature = BME_280_INVALID_TEMP
                            print(
                                "02 - Reading data from Humidity Sensor (INVALID STRING! - ERROR)..."
                            )

                    except:
                        humidity = BME_280_INVALID_HUMI
                        temperature = BME_280_INVALID_TEMP
                        print(
                            "02 - Reading data from Humidity Sensor (INVALID STRING! - ERROR)..."
                        )

                else:
                    print(
                        "02 - Reading data from Humidity Sensor (DISABLED)...")
                    humidity = BME_280_INVALID_HUMI
                    temperature = BME_280_INVALID_TEMP

                HUMI_sensor = format(humidity, "0.2f")
                TEMP_sensor = format(temperature, "0.2f")
                print("02 - Reading data from Humidity Sensor: Temp(oC): ",
                      TEMP_sensor)
                print("02 - Reading data from Humidity Sensor: Humi(%): ",
                      HUMI_sensor)

                print("Sensor Temperature : ", str(TEMP_sensor))

                pid.update(TEMP_sensor)

                target_temperature = pid.output

                if target_temperature > 130:
                    target_temperature = 130
                elif target_temperature < -40:
                    target_temperature = -40
                else:
                    target_temperature = target_temperature

                print("PID set Temperature : ", str(target_temperature))
                print("Chamber real Temperature : ", temp_real)

                OVEN.TTC_Set_Temp(target_temperature)

                # run time
                t_step = time.time()
                t_run = format(t_step - t_start, "0.2f")

                # relay selection
                print("03 - Swtich Relay: %d" % i)
                REL.RelaySelect(i)
                sleep(RELAY_HOLDING_TIME)

                # run multimeter
                print("04- Multimeter DMM196 Reading...")
                if APP_DMM_196_PRESENT:
                    FinalD = LCR.measure(setV, setF)
                    print("LCR Reading: ", FinalD)
                    CP = str(FinalD[0])
                    RP = str(FinalD[1])
                    R_value = str(FinalD[2])
                    X_value = str(FinalD[3])
                else:
                    CP = 0
                    RP = 0
                    R_value = 0
                    X_value = 0

                REL.RelayDeSelect(i)

                # Persistency
                print("06 - Saving data...")
                print("")
                result1 = []
                result2 = []
                result1.append([
                    str(t_run),
                    str(temp_real),
                    str(HUMI_sensor),
                    str(TEMP_sensor),
                    str(i),
                    str(R_value),
                    str(X_value),
                    str(CP),
                    str(RP)
                ])
                CF.content(PATH, filename, result1)

                # create file for each sample
                result2.append([
                    str(t_run),
                    str(temp_real),
                    str(HUMI_sensor),
                    str(TEMP_sensor),
                    str(i),
                    str(R_value),
                    str(X_value),
                    str(CP),
                    str(RP)
                ])
                CF.content(PATH, 'Sample' + str(i), result2)

                Father.Update([
                    temp_set, temp_real, TEMP_sensor, humidity, t_run, R_value,
                    X_value, i
                ])

                DE = str(DIS.read())
                # print (DE)
                RE_VAL.add(DE)
                # print (RE_VAL)

                if "['e\\x0f\\x1b\\x01\\xff\\xff\\xff']" in RE_VAL:
                    print("Exiting")
                    RE_VAL.clear()
                    OVEN.TTC_OFF()
                    DIS.write("page Device Select\xff\xff\xff")
                    return

                elif "['e\\x0f\\x1c\\x01\\xff\\xff\\xff']" in RE_VAL:
                    # DIS.write("rest\xff\xff\xff")
                    RE_VAL.clear()
                    DIS.write("page restart\xff\xff\xff")
                    os.system("sudo reboot")

                print("07 - Updating Display...")
예제 #8
0
    def main(self, num_samples, set_temperature, start_voltage, end_voltage,
             step_voltage, Father):
        RE_VAL = set()

        # Initialize Relay
        REL = relay.Relay_module()
        REL.reset()

        # set PID parameters
        P = 5
        I = 0
        D = 0

        pid = PID.PID(P, I, D)
        pid.setSampleTime(1)

        if APP_NEXTION_PRESENT:
            self.NEXTION_NUM_SAMPLES = num_samples
            self.NEXTION_SET_TEMP = float(
                format(float(set_temperature) / 0.84, ".2f"))
            self.NEXTION_START_VOLTAGE = start_voltage
            self.NEXTION_END_VOLTAGE = end_voltage
            self.NEXTION_STEP_VOLTAGE = step_voltage
        else:
            self.NEXTION_NUM_SAMPLES = 8
            self.NEXTION_SET_TEMP = 25
            self.NEXTION_NUM_SAMPLES = num_samples
            self.NEXTION_START_VOLTAGE = 2
            self.NEXTION_END_VOLTAGE = -2
            self.NEXTION_STEP_VOLTAGE = 0.1

        # import oven and multimeter
        if APP_OVEN_PRESENT:
            OVEN = TTC4006_Tira.TTC4006(TTC_SERIAL_PORT)
            OVEN.TTC_ON()
            OVEN.TTC_ENABLE_TEMP()

        if APP_Sourcemeter_PRESENT:
            Sourcemeter = Sourcemeter_sweep.SourceMeter(SM_IP, SM_PORT)
            Sourcemeter.startComm()

        # create file and title
        CF = data_storage.create_file()
        test_mode = "Manual"
        equipment_Info = "TTC-4006 + Sourcemeter-2602"
        Driver_root = detect_file.File(folder)[1]
        start_time = str(datetime.datetime.now())
        filename = start_time.replace(" ", "_").replace(".",
                                                        "-").replace(":", "-")
        PATH = CF.folder(Driver_root, filename)

        t_start = time.time()

        t1 = datetime.datetime.now()
        t2 = datetime.datetime.now() + datetime.timedelta(seconds=20 * 60)

        while True:
            if APP_OVEN_PRESENT:
                # run oven)
                print("01 - Reading data from Oven...")
                temp_real = OVEN.TTC_Read_PV_Temp()
                temp_set = OVEN.TTC_Read_SP_Temp()
            else:
                temp_set = format(1.00, "0.2f")
                temp_real = format(1.00, "0.2f")

            pid.SetPoint = self.NEXTION_SET_TEMP
            pid.setKp(float(P))
            pid.setKi(float(I))
            pid.setKd(float(D))

            # read temperature sensor
            # Humidity Sensor
            # If is not OK => apply non valid temp and humidity
            print("02 - Reading data from Humidity Sensor...")
            if APP_BME_280_PRESENT:
                try:
                    temperature, pressure, humidity = bme280.readBME280All()

                    # Medicine
                    if ((humidity == None) or (temperature == None)):
                        humidity = BME_280_INVALID_HUMI
                        temperature = BME_280_INVALID_TEMP
                        print(
                            "02 - Reading data from Humidity Sensor (NONE! - ERROR)..."
                        )
                    elif ((type(humidity) == str)
                          or (type(temperature) == str)):
                        humidity = BME_280_INVALID_HUMI
                        temperature = BME_280_INVALID_TEMP
                        print(
                            "02 - Reading data from Humidity Sensor (INVALID STRING! - ERROR)..."
                        )

                except:
                    humidity = BME_280_INVALID_HUMI
                    temperature = BME_280_INVALID_TEMP
                    print(
                        "02 - Reading data from Humidity Sensor (INVALID STRING! - ERROR)..."
                    )

            else:
                print("02 - Reading data from Humidity Sensor (DISABLED)...")
                humidity = BME_280_INVALID_HUMI
                temperature = BME_280_INVALID_TEMP

            HUMI_sensor = format(humidity, "0.2f")
            TEMP_sensor = format(temperature, "0.2f")
            print("02 - Reading data from Humidity Sensor: Temp(oC): ",
                  TEMP_sensor)
            print("02 - Reading data from Humidity Sensor: Humi(%): ",
                  HUMI_sensor)

            print("Sensor Temperature : ", str(TEMP_sensor))

            pid.update(float(TEMP_sensor))

            target_temperature = pid.output

            if target_temperature > 130:
                target_temperature = 130
            elif target_temperature < -40:
                target_temperature = -40
            else:
                target_temperature = target_temperature

            print("PID set Temperature : ", str(target_temperature))
            print("Chamber real Temperature : ", temp_real)

            OVEN.TTC_Set_Temp(target_temperature)

            t_step = time.time()
            if t1 > t2:
                for i in range(self.NEXTION_NUM_SAMPLES):
                    # run time
                    t_run = format(t_step - t_start, "0.2f")

                    # relay selection
                    print("03 - Swtich Relay: %d" % i)
                    REL.RelaySelect(i)
                    sleep(RELAY_HOLDING_TIME)

                    Father.updateSweep([
                        temp_set, temp_real, TEMP_sensor, HUMI_sensor, t_run,
                        "Measureing", " ", i
                    ])

                    DE = str(DIS.read())
                    # print (DE)
                    RE_VAL.add(DE)
                    # print (RE_VAL)

                    if "['e\\x0e\\x13\\x01\\xff\\xff\\xff']" in RE_VAL:
                        print("Exiting")
                        RE_VAL.clear()
                        OVEN.TTC_OFF()
                        DIS.write("page Device Select\xff\xff\xff")
                        return

                    elif "['e\\x0e\\x14\\x01\\xff\\xff\\xff']" in RE_VAL:
                        # DIS.write("rest\xff\xff\xff")
                        RE_VAL.clear()
                        DIS.write("page restart\xff\xff\xff")
                        os.system("sudo reboot")

                    print("07 - Updating Display...")

                    # creat file
                    sample_time = str(datetime.datetime.now()).replace(
                        " ", "_").replace(".", "-").replace(":", "-")
                    name = 'Sample' + str(i)
                    locals()['v' + str(i)] = i
                    CF.header_sm(PATH, name + "_" + sample_time, sample_time,
                                 equipment_Info, test_mode,
                                 self.NEXTION_NUM_SAMPLES,
                                 self.NEXTION_START_VOLTAGE,
                                 self.NEXTION_END_VOLTAGE,
                                 self.NEXTION_STEP_VOLTAGE, str(TEMP_sensor))

                    # run multimeter
                    print("04- Multimeter DMM196 Reading...")
                    if APP_Sourcemeter_PRESENT:
                        data = Sourcemeter.Measure("a",
                                                   self.NEXTION_START_VOLTAGE,
                                                   self.NEXTION_END_VOLTAGE,
                                                   self.NEXTION_STEP_VOLTAGE)
                        CF.content(PATH, name + "_" + sample_time, data)

                    # relay reset
                    print("06 - Swtich Relay Unselection: %d" % i)
                    REL.RelayDeSelect(i)

            else:
                # run time
                t_run = format(t_step - t_start, "0.2f")

                Father.updateSweep([
                    temp_set, temp_real, TEMP_sensor, HUMI_sensor, t_run,
                    "Waiting", " ", 0
                ])

                DE = str(DIS.read())
                # print (DE)
                RE_VAL.add(DE)
                # print (RE_VAL)

                if "['e\\x0e\\x13\\x01\\xff\\xff\\xff']" in RE_VAL:
                    print("Exiting")
                    OVEN.TTC_OFF()
                    RE_VAL.clear()
                    DIS.write("page Device Select\xff\xff\xff")
                    return

                elif "['e\\x0e\\x14\\x01\\xff\\xff\\xff']" in RE_VAL:
                    # DIS.write("rest\xff\xff\xff")
                    RE_VAL.clear()
                    DIS.write("page restart\xff\xff\xff")
                    os.system("sudo reboot")

                print("07 - Updating Display...")

                t1 = datetime.datetime.now()
    def main(self, num_samples, Father):
        RE_VAL = set()

        # Initialize Relay
        REL = relay.Relay_module()
        REL.reset()

        # detect file
        if APP_PEN_DRIVE:
            FILE_NAME = detect_file.File(folder)[0]

        # Load Profile
        TP = readTempProfile(FILE_NAME)[0]
        print(TP)

        # set PID parameters
        P = 5
        I = 0
        D = 0

        pid = PID.PID(P, I, D)
        pid.setSampleTime(1)

        # get sample number
        if APP_NEXTION_PRESENT:
            self.NEXTION_NUM_SAMPLES = num_samples
        else:
            self.NEXTION_NUM_SAMPLES = 8

        # import oven and multimeter
        if APP_OVEN_PRESENT:
            OVEN = TTC4006_Tira.TTC4006(TTC_SERIAL_PORT)
            OVEN.TTC_ON()
            OVEN.TTC_ENABLE_TEMP()

        if APP_DMM_196_PRESENT:
            Multimeter = DMM_196.DMM_196(DMM_IP, DMM_PORT)
            Multimeter.startComm()

        # create file and title
        CF = data_storage.create_file()
        test_mode = "Auto"
        equipment_Info = "TTC-4006 + DMM-196"
        Driver_root = detect_file.File(folder)[1]
        start_time = str(datetime.datetime.now())
        filename = start_time.replace(" ", "_").replace(".",
                                                        "-").replace(":", "-")
        PATH = CF.folder(Driver_root, filename)
        CF.header(PATH, filename, start_time, equipment_Info, test_mode,
                  self.NEXTION_NUM_SAMPLES)
        CF.content(PATH, filename, (
            "Time(s)\tSetTemp.(oC)\tActual Temp.(oC)\tHumidity(%)\tTemp.Sensor(oC)\tSample number\tResistence(ohm)\r\n"
        ))

        t_start = time.time()

        # temperature loop
        for step in TP:
            # setting the oven
            step_time = step[0] * 60  # step_time in seconds
            step_temp = float(format(float(step[1]) / 0.84, ".2f"))

            print(step)

            t1 = datetime.datetime.now()
            t2 = datetime.datetime.now() + datetime.timedelta(
                seconds=step_time)

            while (t1 < t2):
                # Relay switching
                REL.reset()

                for i in range(self.NEXTION_NUM_SAMPLES):
                    # run oven
                    if APP_OVEN_PRESENT:
                        temp_set1 = OVEN.TTC_Read_SP_Temp()
                        temp_real1 = OVEN.TTC_Read_PV_Temp()
                        temp_set = str(format(temp_set1, "0,.2f"))
                        temp_real = str(format(temp_real1, "0,.2f"))

                    else:
                        temp_set = format(1.00, "0.2f")
                        temp_real = format(1.00, "0.2f")

                    pid.SetPoint = step_temp
                    pid.setKp(float(P))
                    pid.setKi(float(I))
                    pid.setKd(float(D))

                    # read temperature sensor
                    # Humidity Sensor
                    # If is not OK => apply non valid temp and humidity
                    print("02 - Reading data from Humidity Sensor...")
                    if APP_BME_280_PRESENT:
                        try:
                            temperature, pressure, humidity = bme280.readBME280All(
                            )

                            # Medicine
                            if ((humidity == None) or (temperature == None)):
                                humidity = BME_280_INVALID_HUMI
                                temperature = BME_280_INVALID_TEMP
                                print(
                                    "02 - Reading data from Humidity Sensor (NONE! - ERROR)..."
                                )
                            elif ((type(humidity) == str)
                                  or (type(temperature) == str)):
                                humidity = BME_280_INVALID_HUMI
                                temperature = BME_280_INVALID_TEMP
                                print(
                                    "02 - Reading data from Humidity Sensor (INVALID STRING! - ERROR)..."
                                )

                        except:
                            humidity = BME_280_INVALID_HUMI
                            temperature = BME_280_INVALID_TEMP
                            print(
                                "02 - Reading data from Humidity Sensor (INVALID STRING! - ERROR)..."
                            )

                    else:
                        print(
                            "02 - Reading data from Humidity Sensor (DISABLED)..."
                        )
                        humidity = BME_280_INVALID_HUMI
                        temperature = BME_280_INVALID_TEMP

                    HUMI_sensor = format(humidity, "0.2f")
                    TEMP_sensor = format(temperature, "0.2f")
                    print("02 - Reading data from Humidity Sensor: Temp(oC): ",
                          TEMP_sensor)
                    print("02 - Reading data from Humidity Sensor: Humi(%): ",
                          HUMI_sensor)

                    print("Sensor Temperature : ", str(TEMP_sensor))

                    pid.update(float(TEMP_sensor))

                    target_temperature = pid.output

                    if target_temperature > 130:
                        target_temperature = 130
                    elif target_temperature < -40:
                        target_temperature = -40
                    else:
                        target_temperature = target_temperature

                    print("PID set Temperature : ", str(target_temperature))
                    print("Chamber real Temperature : ", temp_real)

                    if APP_OVEN_PRESENT:
                        OVEN.TTC_Set_Temp(target_temperature)

                    # run time
                    t_step = time.time()
                    t_r = t_step - t_start
                    t_run = format(t_r, "0.2f")

                    REL.RelaySelect(i)
                    sleep(RELAY_HOLDING_TIME)

                    # run multimeter
                    print('04- Multimeter DMM196 Reading...')
                    if APP_DMM_196_PRESENT:
                        mval = Multimeter.read_resiatance()
                    else:
                        mval = (i * float(t_run))

                    R_value = str(mval)

                    # relay reset
                    REL.RelayDeSelect(i)

                    # Persistency
                    print('06 - Saving data...')
                    result1 = []
                    result2 = []
                    result1.append([
                        str(t_run),
                        str(temp_set),
                        str(temp_real),
                        str(HUMI_sensor),
                        str(TEMP_sensor),
                        str(i),
                        str(R_value)
                    ])
                    CF.content(PATH, filename, result1)

                    # create file for each sample
                    result2.append([
                        str(t_run),
                        str(temp_set),
                        str(temp_real),
                        str(HUMI_sensor),
                        str(TEMP_sensor),
                        str(i),
                        str(R_value)
                    ])
                    CF.content(PATH, 'Sample' + str(i), result2)

                    Father.updateMultimeter([
                        temp_set, temp_real, TEMP_sensor, HUMI_sensor, t_run,
                        R_value, i
                    ])

                    DE = str(DIS.read())
                    # print (DE)
                    RE_VAL.add(DE)
                    # print (RE_VAL)

                    if "['e\\x0e\\x13\\x01\\xff\\xff\\xff']" in RE_VAL:
                        print("Exiting")
                        OVEN.TTC_OFF()
                        RE_VAL.clear()
                        DIS.write("page Device Select\xff\xff\xff")
                        return

                    elif "['e\\x0e\\x14\\x01\\xff\\xff\\xff']" in RE_VAL:
                        # DIS.write("rest\xff\xff\xff")
                        RE_VAL.clear()
                        DIS.write("page restart\xff\xff\xff")
                        os.system("sudo reboot")

                    print("07 - Updating Display...")

                t1 = datetime.datetime.now()

        if APP_OVEN_PRESENT:
            OVEN.TTC_OFF()
    def main(self,num_samples, start_voltage, end_voltage, step_voltage, Father):
        # Initialize Relay
        self.REL = relay.Relay_module()
        self.REL.reset()

        # detect file
        if APP_PEN_DRIVE:
            FILE_NAME = detect_file.File(folder)[0]
        else:
            FILE_NAME = "/home/pi/Desktop/Temperature_profile.txt"

        # Load Profile
        TP = readTempProfile(FILE_NAME)
        print(TP)
        
        #sleep(5)

        P = 5
        I = 0
        D = 0

        pid = PID.PID(P, I, D)
        pid.setSampleTime(1)

        # import oven and multimeter
        if APP_OVEN_PRESENT:
            OVEN = VT4002_SM.VT4002(VT4002_IP)
            OVEN.startComm()

        if APP_Sourcemeter_PRESENT:
            self.Sourcemeter = Sourcemeter_sweep.SourceMeter(IP_addr, PORT)
            self.Sourcemeter.startComm()

        self.NEXTION_NUM_SAMPLES = num_samples
        self.NEXTION_START_VOLTAGE = start_voltage
        self.NEXTION_END_VOLTAGE = end_voltage
        self.NEXTION_STEP_VOLTAGE = step_voltage

        # create file and title
        self.CF = data_storage.create_file()
        self.test_mode = "Auto"
        self.equipment_Info = "VT-4002 + Sourcemeter-2602"
       # Driver_root = detect_file.File(folder)[1]
        Driver_root = "/home/pi/Desktop/"
        start_time = str(datetime.datetime.now())
        self.folder_name = start_time.replace(" ", "_").replace(".", "-").replace(":", "-")
        self.CF.folder(Driver_root, self.folder_name)

        t_start = time.time()

        for step in TP:
            #create folder for each step
            self.CF.folder_SM(step)

            # setting the oven
            step_time = step[1] * 60  # step_time in seconds
            step_temp = float(format(float(step[0]) / 0.94, ".2f"))

            print(step)
            #sleep(5)

            t1 = datetime.datetime.now()
            t2 = datetime.datetime.now() + datetime.timedelta(seconds=step_time)
            t3 = datetime.datetime.now() + datetime.timedelta(seconds=10)

            counter = 0
            while (t1 < t2):
                # run oven
                print('01 - Reading data from Oven...')
                if APP_OVEN_PRESENT:
                    temp = OVEN.read_temp()
                    temp_set = temp[0]
                    temp_real = temp[1]
                else:
                    temp_set = format(1.00, "0.2f")
                    temp_real = format(1.00, "0.2f")

                # Humidity Sensor
                # If is not OK => apply non valid temp and humidity
                print('02 - Reading data from Humidity Sensor...')
                if APP_BME_280_PRESENT:
                    try:
                        temperature, pressure, humidity = bme280.readBME280All()

                        # Medicine
                        if ((humidity == None) or (temperature == None)):
                            humidity = BME_280_INVALID_HUMI
                            temperature = BME_280_INVALID_TEMP
                            print('02 - Reading data from Humidity Sensor (NONE! - ERROR)...')
                        elif ((type(humidity) == str) or (type(temperature) == str)):
                            humidity = BME_280_INVALID_HUMI
                            temperature = BME_280_INVALID_TEMP
                            print('02 - Reading data from Humidity Sensor (INVALID STRING! - ERROR)...')

                    except:
                        humidity = BME_280_INVALID_HUMI
                        temperature = BME_280_INVALID_TEMP
                        print('02 - Reading data from Humidity Sensor (INVALID STRING! - ERROR)...')

                else:
                    print('02 - Reading data from Humidity Sensor (DISABLED)...')
                    humidity = BME_280_INVALID_HUMI
                    temperature = BME_280_INVALID_TEMP

                HUMI_sensor = format(humidity, "0.2f")
                TEMP_sensor = format(temperature, "0.2f")
                print('02 - Reading data from Humidity Sensor: Temp(oC): ', TEMP_sensor)
                print('02 - Reading data from Humidity Sensor: Humi(%): ', HUMI_sensor)

                # run oven
                if APP_OVEN_PRESENT:
                    pid.SetPoint = step_temp
                    pid.setKp(float(P))
                    pid.setKi(float(I))
                    pid.setKd(float(D))

                    temp = OVEN.read_temp()
                    temp_real = str(temp[1])

                    self.actual_temperature = temperature

                    print("Sensor Temperature : " + str(self.actual_temperature))

                    pid.update(self.actual_temperature)

                    target_temperature = pid.output

                    if target_temperature > 130:
                        target_temperature = 130
                    elif target_temperature < -40:
                        target_temperature = -40
                    else:
                        target_temperature = target_temperature

                    print("PID set Temperature : " + str(target_temperature))
                    print("Chamber real Temperature : " + temp_real)

                    OVEN.set_temp(target_temperature)

                if t1 > t3 and t1 < t2 and counter < 2:
                    for i in range(self.NEXTION_NUM_SAMPLES):
                        print('03 - Swtich Relay: %d' % i)
                        self.REL.RelaySelect(i)
                        sleep(RELAY_HOLDING_TIME)

                        # create folder for each sample
                        current_time = str(datetime.datetime.now())
                        self.time_str = current_time.replace(" ", "_").replace(".", "-").replace(":", "-")

                        name = 'Sample' + str(i)
                        locals()['v' + str(i)] = i
                        PA = self.CF.sample_folder(name)
                        self.CF.header_sm(PA, self.time_str, self.time_str, self.equipment_Info, self.test_mode, self.NEXTION_NUM_SAMPLES, self.NEXTION_START_VOLTAGE,
                                    self.NEXTION_END_VOLTAGE, self.NEXTION_STEP_VOLTAGE, 10)

                        print('04 - Sourcemeter Reading...')
                        # measurement of sourcemeter
                        if APP_Sourcemeter_PRESENT:
                            data = self.Sourcemeter.Measure('a', self.NEXTION_START_VOLTAGE, self.NEXTION_END_VOLTAGE,
                                                       self.NEXTION_STEP_VOLTAGE, self)
                            self.CF.content(PA, self.time_str, data)

                        print('05 - Swtich Relay Unselection: %d' % i)
                        self.REL.RelayDeSelect(i)

                        if APP_NEXTION_PRESENT:
                            print('07 - Reading Display...')
                            RE_VAL = set()
                            DE = str(DIS.read())
                            # print (DE)
                            RE_VAL.add(DE)
                            # print (RE_VAL)

                            if "['e\\x05\\x15\\x01\\xff\\xff\\xff']" in RE_VAL:
                                DIS.write('t0.txt="Stop"')
                                OVEN.close()
                                sys.exit()

                            elif "['e\\x05\\x14\\x01\\xff\\xff\\xff']" in RE_VAL:
                                # DIS.write("rest\xff\xff\xff")
                                os.system("sudo reboot")

                            print('07 - Updating Display...')

                            Father.Update(self.updateList)

                counter += 1

                t1 = datetime.datetime.now()

        if APP_OVEN_PRESENT:
            OVEN.close()