header image

Future Improvements

Future improvements can be made with this project. Implementation of such things as: better heating insulation, active cooling, network integration, notifications over textmessage or email, better board gripping, analog input heat elements, and much much more. This project has so much more potential implementable extras that there would not be an end to this list.

References

Distribution of work

  • Hardware Hacking - Brendon, Joe
  • Software Hacking - Joe, Brendon
  • Website structure and Formatting - Brendon
  • Content on website- Joe, Brendon

Code

Download the code

Main Reflow Control Script

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#
# REFLOW OVEN MAIN CONTROL SCRIPT
#
# Final Project for ECE 5990
#
# Brendon Jackson - btj28
# Joseph Featherston - jgf82
#

import pygame
import os, sys
import RPi.GPIO as GPIO
import time
import datetime
import collections
from pygame.locals import *


size = (width,height) = (320,240)
black = (0,0,0)
white = (255,255,255)
color_1 = (200,50,0)
color_2 = (50,100,150)
label_color = white
value_color = color_2
positive_color = (0,100,30)
negative_color = color_1
curr_color = (200,10,100)
des_color = (100,100,20)



os.putenv('SDL_VIDEODRIVER', 'fbcon')
os.putenv('SDL_FBDEV', '/dev/fb1')
os.putenv('SDL_MOUSEDRV', 'TSLIB')
os.putenv('SDL_MOUSEDEV', '/dev/input/touchscreen')

pygame.init()

screen = pygame.display.set_mode(size)
screen.fill(black)
pygame.display.flip()

sm_font = pygame.font.Font(None, 20)
md_font = pygame.font.Font(None, 40)
lg_font = pygame.font.Font(None, 80)

graph_surface = pygame.Surface((320,100))
graph = pygame.PixelArray(graph_surface)
graph_surface.lock()
graph[:,:] = black
graph_surface.unlock()

def drawStart():
  start_text  = [
    ('Preheat Temp',(60,20),label_color,sm_font),
    (getValue("preheat_temp"),(60,50),value_color,sm_font),
    ('Preheat Time',(60,80),label_color,sm_font),
    (getValue("preheat_time"),(60,110),value_color,sm_font),
    ('Preheat Rate',(60,140),label_color,sm_font),
    (getValue("preheat_rate"),(60,170),value_color,sm_font),
    ('Peak Temp',(180,20),label_color,sm_font),
    (getValue("peak_temp"),(180,50),value_color,sm_font),
    ('Peak Rate',(180,80),label_color,sm_font),
    (getValue("peak_rate"),(180,110),value_color,sm_font),
    ('Cool Rate',(180,140),label_color,sm_font),
    (getValue("cool_rate"),(180,170),value_color,sm_font),
    ('+',(90,45),positive_color,lg_font),
    ('-',(30,45),negative_color,lg_font),
    ('+',(90,105),positive_color,lg_font),
    ('-',(30,105),negative_color,lg_font),
    ('+',(90,165),positive_color,lg_font),
    ('-',(30,165),negative_color,lg_font),
    ('+',(210,45),positive_color,lg_font),
    ('-',(150,45),negative_color,lg_font),
    ('+',(210,105),positive_color,lg_font),
    ('-',(150,105),negative_color,lg_font),
    ('+',(210,165),positive_color,lg_font),
    ('-',(150,165),negative_color,lg_font),
    ]
  screen.fill(black)
  for text,pos,color,font in start_text:
    text = font.render(text, True, color)
    text_rect = text.get_rect(center=pos)
    screen.blit(text,text_rect)
  text = lg_font.render('GO', True, white)
  text_rect = text.get_rect(center=(275,40))
  screen.fill(positive_color,text_rect)
  screen.blit(text,text_rect)
  pygame.display.flip()


def handleStart():
  drawStart()
  events = pygame.event.get()
  for event in events:
    if event.type == pygame.KEYDOWN:
      if event.key == pygame.K_ESCAPE:
        done()
    if event.type ==  pygame.MOUSEBUTTONDOWN:
      x, y = pygame.mouse.get_pos()
      if isPressed(x,y,275,40,20):
        return False
      elif isPressed(x,y,90,45,20):
        incValue("preheat_temp")
      elif isPressed(x,y,30,45,20):
        decValue("preheat_temp")
      elif isPressed(x,y,90,105,20):
        incValue("preheat_time")
      elif isPressed(x,y,30,105,20):
        decValue("preheat_time")
      elif isPressed(x,y,90,165,20):
        incValue("preheat_rate")
      elif isPressed(x,y,30,165,20):
        decValue("preheat_rate")
      elif isPressed(x,y,210,45,20):
        incValue("peak_temp")
      elif isPressed(x,y,150,45,20):
        decValue("peak_temp")
      elif isPressed(x,y,210,105,20):
        incValue("peak_rate")
      elif isPressed(x,y,150,105,20):
        decValue("peak_rate")
      elif isPressed(x,y,210,165,20):
        incValue("cool_rate")
      elif isPressed(x,y,150,165,20):
        decValue("cool_rate")

  return True


def isPressed(x,y,cen_x,cen_y,size):
  if x<cen_x+size and x>cen_x-size:
    if y<cen_y+size and y>cen_y-size:
      return True
  return False

def drawRun():
  run_text  = [
    ('Current Temp',(70,20),label_color,sm_font),
    (getState("temp"),(70,50),value_color,md_font),
    ('Elapsed Time',(70,80),label_color,sm_font),
    (getState("time"),(70,110),value_color,md_font),
    ('Desired Temp',(250,20),label_color,sm_font),
    (getState("desired_temp"),(250,50),value_color,md_font),
    ('Current Stage',(250,80),label_color,sm_font),
    (getState("state"),(250,110),value_color,md_font),
    ]
  screen.fill(black)
  for text,pos,color,font in run_text:
    text = font.render(text, True, color)
    text_rect = text.get_rect(center=pos)
    screen.blit(text,text_rect)
  text = lg_font.render('STOP', True, white)
  text_rect = text.get_rect(center=(160,180))
  screen.fill(negative_color,text_rect)
  screen.blit(text,text_rect)
  #graph_surface.unlock()
  #screen.blit(graph_surface,(0,140))
  pygame.display.flip()


def updateGraph(currT,desT,t):
  graph_surface.lock()
  graph[t, round(currT/3)] = curr_color
  graph[t, round(desT/3)] = des_color
  graph.surface.unlock()

def done():
  open_Door()
  relay_OFF()
  door.stop()
  quit()



def handleRun():
  drawRun()
  events = pygame.event.get()
  for event in events:
    if event.type == pygame.KEYDOWN:
      if event.key == pygame.K_ESCAPE:
        done()
    if event.type ==  pygame.MOUSEBUTTONDOWN:
      x, y = pygame.mouse.get_pos()
      if isPressed(x,y,160,180,20):
        return False
  return True


#config = {
#  "preheat_temp" : 140,
#  "preheat_time" : 80,
#  "preheat_rate" : 2,
#  "peak_temp" : 235,
#  "peak_rate" :  3,
#  "cool_rate" :  6,
#}
#quick config for demos
config = {
  "preheat_temp" : 60,
  "preheat_time" : 20,
  "preheat_rate" : 2,
  "peak_temp" : 80,
  "peak_rate" :  3,
  "cool_rate" :  6,
}

state = {
  "temp" : 204,
  "desired_temp" : 200,
  "time" : 300,
  "state" : "Boot"
}

def getState(key):
  return str(state[key])

def incValue(key):
  config[key] = config[key]+1;


def decValue(key):
  config[key] = config[key]-1

def getValue(key):
  return str(config[key])

def pushConfig():
  global PREHEAT_TEMP
  global PREHEAT_TIME
  global PREHEAT_RATE
  global PEAK_TEMP
  global PEAK_RATE
  global COOLDOWN_TEMP
  PREHEAT_TEMP = config["preheat_temp"]
  PREHEAT_TIME = config["preheat_time"]
  PREHEAT_RATE = config["preheat_rate"]
  PEAK_TEMP = config["peak_temp"]
  PEAK_RATE = config["peak_rate"]
  COOLDOWN_TEMP = config["cool_rate"]



def updateValues():
  global state
  state["temp"] = temperature_C
  state["desired_temp"] = round(expected_temp)
  state["time"] = round(t-start_time)
  if s == 17: 
    state["state"] = "Preheat"
  elif s == 16: 
    state["state"] = "Startup"
  elif s == 18: 
    state["state"] = "Reflow"
  elif s == 19: 
    state["state"] = "Peak"
  elif s == 20: 
    state["state"] = "Cooldown"
  elif s == 21: 
    state["state"] = "Done"
  else:
    state["state"] = "error!"




# HardWare SPI
#import Adafruit_GPIO.SPI as SPI

import Adafruit_MAX31855.MAX31855 as MAX31855

## CONSTANTS SHOULD NOT CHANGE ##

SERVO_CH = 13
RELAY_CH = 26

DOOR_CL  = 10.0
DOOR_OP  = 5.0

## STATE MACHINE CONSTANTS FOR MENU ##
INIT     = 1 #should display current heating profile
             #and simple buttons to setup the profile
             #and start the process



# RUNNING STATES
WARM    = 16
PREHEAT  = 17
REFLOW   = 18
PEAK     = 19
COOLDOWN = 20
DONE = 21


## CONTROL VARIABLES ##
s = WARM #current state

start_time   = 0
expected_temp= 0
t  = 0


PREHEAT_TEMP = 0 #Celsius
PREHEAT_TIME = 0  #Seconds
PREHEAT_RATE = 0 #C/s

PEAK_TEMP  = 0 # Celsius
PEAK_TIME = 10  #seconds
PEAK_RATE  = 0  #C/s

DONE_TEMP = 30
WARM_TEMP = 50

COOLDOWN_RATE= 8  #C/s

## TEMPERATURE SENSOR INITIALIZATION ##

# SOFTWARE SPI SENSOR
CLK = 25
CS  = 24
D0  = 18


# HARDWARE SPI SENSOR
#SPI_PORT   = 0
#SPI_DEVICE = 0

#sensor = MAX31855.MAX31855(spi =  SPI.SpiDev(SPI_PORT, SPI_DEVICE))
last_temp = 0;
temperature_C = 0;
temperature_F = 0;

## GPIO SETUPS ##

GPIO.setmode(GPIO.BCM)

GPIO.setup(SERVO_CH, GPIO.OUT)
GPIO.setup(RELAY_CH, GPIO.OUT)

## SERVO INITIALIZATION ##
door = GPIO.PWM(SERVO_CH, 50)
door.start(DOOR_CL)



## FUNCTION DEFINITIONS TO ABSTRACT AWAY COMPLEXITY ##
def open_Door():
  door.ChangeDutyCycle(DOOR_OP)

def close_Door():
  door.ChangeDutyCycle(DOOR_CL)

def relay_ON():
  GPIO.output(RELAY_CH, GPIO.HIGH)

def relay_OFF():
  GPIO.output(RELAY_CH, GPIO.LOW)
  
def updateTemperature():
  global temperature_C
  global last_temp
  sensor = MAX31855.MAX31855(CLK, CS, D0)
  last_temp = temperature_C
  temperature_C = sensor.readTempC()
  temperature_F = (temperature_C * 9.0 / 5.0) + 32.0

reflow_time = 0

def updateControl():
  global s
  global t
  global expected_temp
  global start_peak
  global reflow_time
  t = time.time() #microSeconds -> seconds
  if s==WARM:
    expected_temp = (t-start_time)*PREHEAT_RATE
    if expected_temp>WARM_TEMP:
      expected_temp = WARM_TEMP
    if temperature_C > WARM_TEMP-10:
      s=PREHEAT
      reflow_time = t
  elif  s==PREHEAT:
    expected_temp = (t-start_time)*PREHEAT_RATE
    #expected_temp = last_temp+PREHEAT_RATE
    if expected_temp>PREHEAT_TEMP:
      expected_temp = PREHEAT_TEMP
    if t-reflow_time>PREHEAT_TIME:
      s=REFLOW
  elif s==REFLOW:
    expected_temp = (t-PREHEAT_TIME-reflow_time)*PEAK_RATE+PREHEAT_TEMP
    #expected_temp = last_temp + PREHEAT_TEMP
    if expected_temp > PEAK_TEMP:
      expected_temp = PEAK_TEMP
    if last_temp > PEAK_TEMP-10:
      start_peak = t
      s = PEAK
  elif s==PEAK:
    expected_temp = PEAK_TEMP
    if t-start_peak>PEAK_TIME:
      s= COOLDOWN
  elif s==COOLDOWN:
    expected_temp = (t-start_peak-PEAK_TIME)*(-COOLDOWN_RATE)+PEAK_TEMP
    if temperature_C < DONE_TEMP:
      s=DONE
  else:
    expected_temp = 0
  pid()
    # do nothing to output


errors = collections.deque(maxlen=10)

preverror = 0
error = 0
def pid():
  global error 
  preverror  = error
  error      = temperature_C - expected_temp
  errors.append(error)
  p = error
  i = sum(errors)/len(errors)
  d = (errors[len(errors)-1]-errors[0])/len(errors)
  correction = 2*p+4*i+10*d
  if correction < 0:
    close_Door()
    if correction < -10:
      relay_ON()
    else:
      relay_OFF()
  elif correction > 0:
    relay_OFF()
    if correction > 20:
      open_Door()
    else:
      close_Door()







## MAIN LOOP ##


while True:
  while handleStart():
    time.sleep(0.1)
  start_time = time.time()
  pushConfig()
  while handleRun():
    updateTemperature()
    updateControl()
    updateValues()
    time.sleep(1)
open_Door()
relay_OFF()
door.stop()
GPIO.cleanup()