Snippets

Espec North America Example of creating a program and running it on a WatlowF4S.

Created by Myles Metzler last modified
import copy 
import json
import time
from chamberconnectlibrary.watlowf4 import WatlowF4

#connect to our controller, update 'interface_params' as needed.
interface_params = {'interface':"RTU", 'baudrate':19200, 'serialport':'//./COM6'}
controller = WatlowF4(profiles=True, loops=1, limits=[1], **interface_params)

def deep_update(target, updates):
    """Update the step_template with our data.
    This is targeted specifically for what we are doing here;
    don't reuse with significant testing.
    """
    for k, v in updates.items():
        if isinstance(v, dict):
            target[k] = deep_update(target[k], v)
        elif isinstance(v, list):
            for i, u in enumerate(v):
                if isinstance(u, dict):
                    target[k][i] = deep_update(target[k][i], u)
                else:
                    target[k][i] = u
        else:
            target[k] = v
    return target


# Program data to use to update our template.
program_slot = 2 # This is the where we will save the program.
program_name = 'WDTEST' # It is best practice to give the program a name
steps = [
    {'type':'ramptime', 'duration':{'seconds':1}, 'loops':[{'target':20.0}]}, # Initialize to 20C
    {'type':'soak', 'duration':{'seconds':1}, 'loops':[{'gsoak':True}]}, # Wait for the chamber to get to temperature
    # duration[minutes] is calculated as |start_time-end_time|/change_rate
    {'type':'ramptime', 'duration':{'minutes':abs(30.0-20.0)/2.0}, 'loops':[{'target':30.0}]}, #ramptime at 2.0C/min
    #{'type':'ramprate', 'loops':[{'target':30.0, 'rate':2.0}]}, # ramprate at 2.0C/min
    {'type':'end'} # All programs must have a end step.
]

print 'Get template from the controller'
template = controller.get_prgm(0)
step_template = template['steps'][0]

# Start to modify our template into the final program.
template['steps'] = [] # remove step template
template['name'] = program_name # It is best practice to give the program a name
for step in steps:
    mystep = copy.deepcopy(step_template) # copy the template (python objects are by reference)
    deep_update(mystep, step) # update the copied step
    template['steps'].append(mystep) # insert the step into our template
    
# save the program to a file so we may review it.
# with open('test.json', 'w') as f:
#     json.dump(template, f, indent=4, sort_keys=True)

print 'Save program to controller program slot #%d' % program_slot
controller.set_prgm(program_slot, template)

# Remove the exit(0) to start the program and monitor it.
# I don't want to accidentally cause any problems with your chambers.
exit(0)

print 'Ensure that the F4 chamber is on!'
print 'The run switch on F4 based BT chambers is a physical switch!'
print 'If the chamber is not on the behaviour will be undefined.'
print 'Start program, F4 is slow; sleep 3s'
controller.set_operation('program', program=program_slot)
time.sleep(3)

print 'Monitor the program'
while True: # emulate a do while loop
    status = controller.get_operation(template)
    setpoint = controller.get_loop(1, 'loop', 'setpoint')['setpoint']
    if status['status'] != 'Program Running': # emulate a do while loop
        break
    print 'temp setpoint: %sC' % setpoint['current']
    print '%s, Time Remaining: %s\n' % (status['status'], status['program']['time_remaining'])
    time.sleep(3)



Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.