2021-05-01 09:29:10 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2021-04-30 21:23:11 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import csv
|
|
|
|
import time
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
sys.dont_write_bytecode = True
|
|
|
|
import config
|
|
|
|
sys.dont_write_bytecode = False
|
|
|
|
|
|
|
|
except:
|
|
|
|
print("Could not import config file.")
|
|
|
|
print("Copy config.py.EXAMPLE to config.py and adapt it for your setup.")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
script_dir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
sys.path.insert(0, script_dir + '/lib/')
|
|
|
|
profile_path = os.path.join(script_dir, "storage", "profiles")
|
|
|
|
|
|
|
|
from oven import RealOven, SimulatedOven
|
|
|
|
|
|
|
|
|
|
|
|
def tune(csvfile, targettemp):
|
|
|
|
# open the file to log data to
|
|
|
|
f = open(csvfile, 'w')
|
|
|
|
csvout = csv.writer(f)
|
2021-05-01 09:38:16 +00:00
|
|
|
csvout.writerow(['time', 'temperature'])
|
2021-04-30 21:23:11 +00:00
|
|
|
|
|
|
|
# construct the oven
|
|
|
|
if config.simulate:
|
|
|
|
oven = SimulatedOven()
|
|
|
|
else:
|
|
|
|
oven = RealOven()
|
|
|
|
|
|
|
|
# Main loop:
|
|
|
|
#
|
|
|
|
# * heat the oven to the target temperature at maximum burn.
|
|
|
|
# * when we reach it turn the heating off completely.
|
|
|
|
# * wait for it to decay back to the target again.
|
|
|
|
# * quit
|
|
|
|
#
|
|
|
|
# We record the temperature every config.sensor_time_wait
|
|
|
|
try:
|
|
|
|
stage = 'heating'
|
|
|
|
if not config.simulate:
|
|
|
|
oven.output.heat(1, tuning=True)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
temp = oven.board.temp_sensor.temperature + \
|
|
|
|
config.thermocouple_offset
|
|
|
|
|
|
|
|
csvout.writerow([time.time(), temp])
|
2021-05-01 08:44:55 +00:00
|
|
|
f.flush()
|
2021-04-30 21:23:11 +00:00
|
|
|
|
|
|
|
if stage == 'heating':
|
|
|
|
if temp > targettemp:
|
|
|
|
if not config.simulate:
|
|
|
|
oven.output.heat(0)
|
2021-05-01 09:35:14 +00:00
|
|
|
stage = 'cooling'
|
2021-04-30 21:23:11 +00:00
|
|
|
|
2021-05-01 09:35:14 +00:00
|
|
|
elif stage == 'cooling':
|
2021-04-30 21:23:11 +00:00
|
|
|
if temp < targettemp:
|
|
|
|
break
|
|
|
|
|
2021-05-01 09:38:16 +00:00
|
|
|
sys.stdout.write(f"\r{stage} {temp:.2f}/{targettemp} ")
|
2021-05-01 08:40:17 +00:00
|
|
|
sys.stdout.flush()
|
2021-04-30 21:23:11 +00:00
|
|
|
time.sleep(config.sensor_time_wait)
|
|
|
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
finally:
|
|
|
|
# ensure we always shut the oven down!
|
|
|
|
if not config.simulate:
|
|
|
|
oven.output.heat(0)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser(description='Record data for kiln tuning')
|
|
|
|
parser.add_argument('csvfile', type=str, help="The CSV file to write to.")
|
2021-05-01 08:44:55 +00:00
|
|
|
parser.add_argument('--targettemp', type=int, default=400, help="The target temperature to drive the kiln to (default 400).")
|
2021-04-30 21:23:11 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
tune(args.csvfile, args.targettemp)
|