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()
def handleRun(self): if (self.process.mode == "Heating mode"): #Check the validity for the inputs for the PID values inputStatus = self.checkInput() if (inputStatus == True): self.outputMessage.clear() self.runButton.setEnabled(False) self.stopButton.setEnabled(True) self.displayMessage("Setup completed, initial heating ... ") # change try: self.process.setup() # Function stored in heater.py. Algorithm based on empirical results. self.process.heat_time = heater.initial_heating_time( self.process) heater.change_duty(self.process) # This is the initial heating. while ((time.time() - self.process.start_t) < self.process.heat_time): if ((time.time() - self.process.curr_t) >= self.process.data_log_freq): self.process.t_center = thm.read(self.process.thm1) self.process.t_edge = thm.read(self.process.thm2) self.process.curr_t = time.time() self.process.dataLog.write_line_to_log(self) # Update PWM values to zero. self.process.pwm_center = 0 self.process.pwm_edge = 0 heater.change_duty(self.process) self.displayMessage('Initial heating finished...') self.process.dataLog.write( 'LINE', round((time.time() - self.process.start_t), 2), 'Initial heating finished at: ') self.process.pid_start_t = time.time() # Wait for the temperature to settle. while ((time.time() - self.process.pid_start_t) < self.process.wait_time): if ((time.time() - self.process.curr_t) >= self.process.data_log_freq): self.process.t_center = thm.read(self.process.thm1) self.process.t_edge = thm.read(self.process.thm2) self.process.curr_t = time.time() self.process.dataLog.write_line_to_log(self) self.displayMessage('PID controller started ...') self.process.dataLog.write( 'LINE', round((time.time() - self.process.start_t), 2), 'PID Controller started at: ') working = True limited = [[False, False], [False, False]] while working: self.process.t_center_last = self.process.t_center self.process.t_edge_last = self.process.t_edge self.process.t_center = thm.read(self.process.thm1) self.process.t_edge = thm.read(self.process.thm2) if ((time.time() - self.process.curr_t) >= self.process.data_log_freq): self.process.curr_t = time.time() self.process.dataLog.write_line_to_log(self) self.process.t_center_avg = ( self.process.t_center + self.process.t_center_last) / 2.0 self.process.t_edge_avg = ( self.process.t_edge + self.process.t_edge_last) / 2.0 self.process.pid_center.update( self.process.t_center_avg) self.process.pwm_center = self.process.pid_center.output self.process.pwm_center = heater.clamp( self.process.pwm_center, 0, 100) self.process.pid_edge.update(self.process.t_edge_avg) self.process.pwm_edge = self.process.pid_edge.output self.process.pwm_edge = heater.clamp( self.process.pwm_edge, 0, 100) heater.change_duty(self.process) # Suppress Kp once the current temp nears the working temp. if ((limited[0][0] == False) and ((self.process.temp - self.process.t_center_avg) < 15)): self.displayMessage("Kp center suppressed ... ") self.process.dataLog.write('LINE', 0, 'Kp center suppressed') self.process.pid_center.setKp( self.process.limited_kp) limited[0][0] = True if ((limited[1][0] == False) and ((self.process.temp - self.process.t_edge_avg) < 15)): self.displayMessage("Kp edge suppressed ... ") self.process.dataLog.write('LINE', 0, 'Kp edge suppressed') self.process.pid_edge.setKp( self.process.limited_kp) limited[1][0] = True if ((limited[0][1] == False) and ((self.process.temp - self.process.t_center_avg) < 1)): self.displayMessage("Kd center suppressed ... ") self.process.dataLog.write('LINE', 0, 'Kd center suppressed') self.process.pid_center.setKd( self.process.limited_kd) limited[0][1] = True if ((limited[1][1] == False) and ((self.process.temp - self.process.t_edge_avg) < 1)): self.displayMessage(("Kd edge suppressed ... ")) self.process.dataLog.write('LINE', 0, 'Kd edge suppressed') self.process.pid_edge.setKd( self.process.limited_kd) limited[1][1] = True # change if (time.time() - self.process.start_t >= 1000): working = False except KeyboardInterrupt: self.process.dataLog.close() thm.close(self.process.thm1) thm.close(self.process.thm2) heater.close(self.process.pwm_1) heater.close(self.process.pwm_2) self.process.coefficients_center = self.process.pid_center.getPID( ) self.process.coefficients_edge = self.process.pid_edge.getPID( ) self.process.dataLog.createPlot(self) sys.exit() except: traceback.print_exc() self.process.dataLog.close() thm.close(self.process.thm1) thm.close(self.process.thm2) heater.close(self.process.pwm_1) heater.close(self.process.pwm_2) sys.exit() else: #This is for the temperature mode self.outputMessage.clear() self.process.measuring = True self.runTemperatureMeasurement()