def setupTemp(self): #Setup for temperature mode. Very similar to the setup for the heater mode except that PID is not used. self.dataLog = log.dataLog("Temperature_Measurement_Test") self.thm1 = thm.setup1() self.thm2 = thm.setup2() self.t_center = thm.read(self.thm1) self.t_edge = thm.read(self.thm2) # These variables store the temperature averaged over the last two values. self.t_center_avg = self.t_center self.t_edge_avg = self.t_edge self.start_t = time.time() self.curr_t = time.time() self.times = [] self.cent_temps = [] self.edge_temps = []
def setup(self): #Setup for heating mode #Setup datalog self.dataLog = log.dataLog("PID_cartridge_test") #Setup thermocouple self.thm1 = thm.setup1() self.thm2 = thm.setup2() #Setup heater self.pwm_1 = heater.setup1() self.pwm_2 = heater.setup2() #Setup PID self.pid_edge = pid_setup.pid_setup_edge(self.temp, self.P_edge, self.I_edge, self.D_edge) self.pid_center = pid_setup.pid_setup_center(self.temp, self.P_center, self.I_center, self.D_center) self.pid_edge_val = self.pid_edge.getPID() self.pid_center_val = self.pid_center.getPID() # These variables will have current temp values written to them. self.t_center = thm.read(self.thm1) self.t_edge = thm.read(self.thm2) # These variables store the temperature averaged over the last two values. self.t_center_avg = self.t_center self.t_edge_avg = self.t_edge # Variables used for timekeeping and data plotting. self.start_t = time.time() self.curr_t = time.time() #Lists used for graph plotting self.times = [] self.cent_temps = [] self.edge_temps = []
def handleRun(self): ''' statusTemp = self.checkTemp() if(statusTemp == "enter a temperature"): self.generateMessageBox("Enter Temperature", "Please enter a temperature value") elif(statusTemp == "temperature too high"): self.generateMessageBox("Temperature too high", "The temperature value entered is too high. Please enter a value below 200.") elif(statusTemp == "invalid temperature"): self.generateMessageBox("Invalid temperature", "Please enter a valid temperature") elif(statusTemp == "valid temperature"): statusP_center = self.checkP("center") statusI_center = self.checkI("center") statusD_center = self.checkD("center") statusP_edge = self.checkP("edge") statusI_edge = self.checkI("edge") statusD_edge = self.checkD("edge") if(statusP_center == "invalid P"): self.generateMessageBox("Invalid center P", "The center P value entered is invalid. Please enter a valid value") elif(statusP_center == "enter a P"): self.generateMessageBox("Enter center P value", "Please enter a center P value.") if(statusI_center == "invalid I"): self.generateMessageBox("Invalid center I", "The center I value entered is invalid. Please enter a valid value") elif(statusI_center == "enter a I"): self.generateMessageBox("Enter center I value", "Please enter a center I value.") if(statusD_center == "invalid D"): self.generateMessageBox("Invalid D", "The center D value entered is invalid. Please enter a valid value") elif(statusD_center == "enter a D"): self.generateMessageBox("Enter center D value", "Please enter a center D value.") if(statusP_edge == "invalid P"): self.generateMessageBox("Invalid edge P", "The edge P value entered is invalid. Please enter a valid value") elif(statusP_edge == "enter a P"): self.generateMessageBox("Enter edge P value", "Please enter an edge P value.") if(statusI_edge == "invalid I"): self.generateMessageBox("Invalid edge I", "The edge I value entered is invalid. Please enter a valid value") elif(statusI_edge == "enter a I"): self.generateMessageBox("Enter edge I value", "Please enter an edge I value.") if(statusD_edge == "invalid D"): self.generateMessageBox("Invalid D", "The edge D value entered is invalid. Please enter a valid value") elif(statusD_edge == "enter a D"): self.generateMessageBox("Enter edge D value", "Please enter an edge D value.") if(statusP_center == "valid P" and statusI_center == "valid I" and statusD_center == "valid D"): center_Valid = True else: center_Valid = False if (statusP_edge == "valid P" and statusI_edge == "valid I" and statusD_edge == "valid D"): edge_Valid = True else: edge_Valid = False if(center_Valid == True and edge_Valid == True): self.temp = float(self.tempEdit.text()) self.P_center = float(self.pEdit_center.text()) self.I_center = float(self.iEdit_center.text()) self.D_center = float(self.dEdit_center.text()) self.P_edge = float(self.pEdit_edge.text()) self.I_edge = float(self.iEdit_edge.text()) self.D_edge = float(self.dEdit_edge.text()) ''' inputStatus = self.checkInput() if (inputStatus == True): self.data_log_freq = 1 # The PWM duty used for the initial heating. Generally around 50% for the # center and 100% for the edge seems to work. Should be adjusted based on # changes to the thermal volume based on empirical results. self.pwm_center = 40 self.pwm_edge = 100 # Used to suppress Kp as it approaches the setpoint. self.limited_kp = 1 # heater.calc_kp(work_temp) self.limited_kd = -0.2 # Time to wait after initial heating for temp to settle self.wait_time = 30 ################################################################################ ''' DON'T EDIT THESE UNLESS YOU KNOW WHAT YOU'RE DOING ''' self.log.setup("PID_cartridge_test") self.thm1 = thm.setup1() self.thm2 = thm.setup2() self.pwm_1 = heater.setup1() self.pwm_2 = heater.setup2() self.pid_edge = pid_setup.pid_setup_edge(self.temp, self.P_edge, self.I_edge, self.D_edge) self.pid_center = pid_setup.pid_setup_center( self.temp, self.P_center, self.I_center, self.D_center) self.pid_edge_val = self.pid_edge.getPID() self.pid_center_val = self.pid_center.getPID() # These variables will have current temp values written to them. self.t_center = thm.read(self.thm1) self.t_edge = thm.read(self.thm2) # These variables store the temperature averaged over the last two values. self.t_center_avg = self.t_center self.t_edge_avg = self.t_edge # Variables used for timekeeping and data plotting. self.start_t = time.time() self.curr_t = time.time() self.times = [] self.cent_temps = [] self.edge_temps = [] self.outputMessage.append("Setup completed, initial heating ... ") self.runButton.setEnabled(False) # change try: # Function stored in heater.py. Algorithm based on empirical results. self.heat_time = heater.initial_heating_time( self.t_center, self.t_edge, self.temp, self.thm1, self.thm2) heater.change_duty(self.pwm_center, self.pwm_edge, self.pwm_1, self.pwm_2) # This is the initial heating. while ((time.time() - self.start_t) < self.heat_time): if ((time.time() - self.curr_t) >= self.data_log_freq): self.t_center = thm.read(self.thm1) self.t_edge = thm.read(self.thm2) self.curr_t = time.time() log.write_line_to_log(self.t_center, self.t_edge, self.pwm_center, self.pwm_edge, self.curr_t, self.start_t, self.cent_temps, self.edge_temps, self.times, self) # Update PWM values to zero. self.pwm_center = 0 self.pwm_edge = 0 heater.change_duty(self.pwm_center, self.pwm_edge, self.pwm_1, self.pwm_2) print('Initial heating finished...') log.write('LINE', round((time.time() - self.start_t), 2), 'Initial heating finished at: ') self.pid_start_t = time.time() # Wait for the temperature to settle. while ((time.time() - self.pid_start_t) < self.wait_time): if ((time.time() - self.curr_t) >= self.data_log_freq): self.t_center = thm.read(self.thm1) self.t_edge = thm.read(self.thm2) self.curr_t = time.time() log.write_line_to_log(self.t_center, self.t_edge, self.pwm_center, self.pwm_edge, self.curr_t, self.start_t, self.cent_temps, self.edge_temps, self.times, self) print('PID controller started ...') log.write('LINE', round((time.time() - self.start_t), 2), 'PID Controller started at: ') working = True limited = [[False, False], [False, False]] while working: self.t_center_last = self.t_center self.t_edge_last = self.t_edge self.t_center = thm.read(self.thm1) self.t_edge = thm.read(self.thm2) if ((time.time() - self.curr_t) >= self.data_log_freq): self.curr_t = time.time() log.write_line_to_log(self.t_center, self.t_edge, self.pwm_center, self.pwm_edge, self.curr_t, self.start_t, self.cent_temps, self.edge_temps, self.times, self) self.t_center_avg = (self.t_center + self.t_center_last) / 2.0 self.t_edge_avg = (self.t_edge + self.t_edge_last) / 2.0 self.pid_center.update(self.t_center_avg) self.pwm_center = self.pid_center.output self.pwm_center = heater.clamp(self.pwm_center, 0, 100) self.pid_edge.update(self.t_edge_avg) self.pwm_edge = self.pid_edge.output self.pwm_edge = heater.clamp(self.pwm_edge, 0, 100) heater.change_duty(self.pwm_center, self.pwm_edge, self.pwm_1, self.pwm_2) # Suppress Kp once the current temp nears the working temp. if ((limited[0][0] == False) and ((self.temp - self.t_center_avg) < 15)): print("Kp center suppressed ... ") log.write('LINE', 0, 'Kp center suppressed') self.pid_center.setKp(self.limited_kp) limited[0][0] = True if ((limited[1][0] == False) and ((self.temp - self.t_edge_avg) < 15)): print("Kp edge suppressed ... ") log.write('LINE', 0, 'Kp edge suppressed') self.pid_edge.setKp(self.limited_kp) limited[1][0] = True if ((limited[0][1] == False) and ((self.temp - self.t_center_avg) < 1)): print("Kd center suppressed ... ") log.write('LINE', 0, 'Kd center suppressed') self.pid_center.setKd(self.limited_kd) limited[0][1] = True if ((limited[1][1] == False) and ((self.temp - self.t_edge_avg) < 1)): print("Kd edge suppressed ... ") log.write('LINE', 0, 'Kd edge suppressed') self.pid_edge.setKd(self.limited_kd) limited[1][1] = True except KeyboardInterrupt: log.close() thm.close(self.thm1) thm.close(self.thm2) heater.close(self.pwm_1) heater.close(self.pwm_2) coefficients_center = self.pid_center.getPID() coefficients_edge = self.pid_edge.getPID() log.createPlot(self.times, self.cent_temps, self.edge_temps, self.heat_time, coefficients_center, coefficients_edge, self.pid_center_val, self.pid_edge_val, self) sys.exit() except: traceback.print_exc() log.close() thm.close(self.thm1) thm.close(self.thm2) heater.close(self.pwm_1) heater.close(self.pwm_2) sys.exit()