示例#1
0
def control(alg, name, acqtime=0, target=318):
    last = 0

    integral = 0
    t0 = time.time()
    tque.append(time.time())
    kque.append(heat.get())

    while acqtime==0 or t0 > tque[-1] - acqtime:
        tque.append(time.time())
        kque.append(heat.get())
        plot_t.append(tque[-1])
        plot_k.append(kque[-1])

        if tque[-1] > last + 1:
            cplt.add(tque[-1], kque[-1])
            last = tque[-1]

        mtemp = kque[-1] # sum(list(kque)) / len(kque)
        dt = tque[-1] - tque[-2]
        dque.append((kque[-1] - kque[-2]) / dt)
        derivative = sum(list(dque)) / len(dque)
        integral += (kque[-1] - target)  * dt
        integral = max(min(integral, 60), -60)
        set_duty(alg(mtemp - target, integral, derivative))

        while tque[0] + BOXT < tque[-1]:
            tque.popleft()
            kque.popleft()
    np.savetxt(name, np.vstack((plot_t, plot_k))) 
示例#2
0
def test_duty(duty, deltat=30):
    """ Finds max oscillations for a given duty cycle """
    print("Testing {}".format(duty))
    set_duty(1)
    while get_temp() < setpoint:
        pass
    set_duty(0)
    while get_temp() > setpoint:
        pass
    print("Starting oscillation.")
    oscillations = 0
    temperatures = []
    tee = time.time()
    set_duty(duty)
    while tee + deltat > time.time():
        temp = get_temp()
        temperatures.append(temp)
        if temp < setpoint:
            set_duty(duty)
        else:
            set_duty(0)
    t = np.array(temperatures)
    t -= setpoint
    max_ = np.max(np.abs(t))
    print("Tested {} with osc {}".format(duty, max_))
    plt.scatter(*zip(*enumerate(temperatures)))
    plt.show()
    return max_
示例#3
0
def test_temp(target):
    t0 = time.time()
    box_temp = deque()
    box_mtemp = deque()
    box_rtd = deque()
    prev = 0
    while True:
        temp = heat.get()
        box_temp.append(temp)
        mtemp = sum(list(box_temp)) / len(box_temp)
        box_mtemp.append(mtemp)
        box_rtd.append(rtd.get())
        if time.time() - TINTERVAL > t0:
            box_mtemp.popleft()
            box_rtd.popleft()
            box_temp.popleft()

        ep = np.array(list(box_mtemp))
        ep -= target
        ep = np.abs(ep)
        if np.all(ep < EPSILON) and time.time() - TINTERVAL > t0:
            # only get second half bc of the lag
            box_rtd = list(box_rtd)[len(box_rtd)//2:]
            rtdtemp = sum(list(box_rtd)) / len(box_rtd)
            print("Read {} from RTD for {}".format(rtdtemp, target))
            return rtdtemp
        set_duty(duty(target) if mtemp < target else 0)
        print(mtemp)
示例#4
0
def get_temp():
    vin = adc.readADCDifferential01(4096, 128) * 0.001  # returns float
    vout = adc.readADCDifferential23(4096, 128) * 0.001  # returns float
    current = vout / 100e3
    resistance = (vin - vout) / current
    power = (vin - vout)**2 / resistance
    temp = (-a - np.sqrt(a**2 + 4 * b * np.log(resistance / R0))) / (2 *
                                                                     b) + T0
    if temp >= SAFETY:
        set_duty(0)
        print("DANGEROUS TEMPERATURE READING; SHUTTING OFF")
        sys.exit(0)
    return temp
示例#5
0
def main():
    if pwm.run_synth()==0:
        print "Error running synth."
        return

    try:
        window = Tkinter.Tk()
        window.title("libmapper PWM synth demo")

        name = [False, Tkinter.StringVar()]
        name[1].set("Waiting for device name...")
        label = Tkinter.Label(window, textvariable=name[1])
        label.pack()

        gain = Tkinter.Scale(window, from_=0, to=100, label='gain',
                             orient=Tkinter.HORIZONTAL, length=300,
                             command=lambda n: pwm.set_gain(float(n)/100.0))
        gain.pack()

        freq = Tkinter.Scale(window, from_=0, to=1000, label='freq',
                             orient=Tkinter.HORIZONTAL, length=300,
                             command=lambda n: pwm.set_freq(float(n)))
        freq.pack()

        duty = Tkinter.Scale(window, from_=0, to=100, label='duty',
                             orient=Tkinter.HORIZONTAL, length=300,
                             command=lambda n: pwm.set_duty(float(n)/100.0))
        duty.pack()

        dev = mapper.device("tk_pwm", 9000)

        dev.add_input_signal("/gain", 1, 'f', None, 0, 100, lambda s,i,n,t: gain.set(n))
        dev.add_input_signal("/freq", 1, 'f', "Hz", 0, 1000, lambda s,i,n,t: freq.set(n))
        dev.add_input_signal("/duty", 1, 'f', None, 0, 100, lambda s,i,n,t: duty.set(n))

        def do_poll():
            dev.poll(0)
            if not name[0] and dev.ready():
                name[0] = True
                name[1].set('Device name: %s, listening on port %d'
                            %(dev.name, dev.port))
            window.after(5, do_poll)

        do_poll()
        window.mainloop()

    finally:
        pwm.stop_synth()
示例#6
0
import sys
import time
import numpy as np
import matplotlib.pyplot as plt
from Adafruit import ADS1x15
from pwm import set_duty
adc = ADS1x15()
a = -0.048
b = 0.0001
T0 = 23.265 + 273.15
R0 =  107682.260

setpoint = 0
try:
    setpoint = float(sys.argv[1])
except:
    print("USAGE: python3 p4d_hw7.py SETPOINT\n where SETPOINT is desired temp in K")
    sys.exit()

try:
    while True:
       vin = adc.readADCDifferential01(4096, 128)*0.001  # returns float
       vout = adc.readADCDifferential23(4096, 128)*0.001  # returns float
       current = vout / 100e3
       resistance = (vin - vout) / current
       power = (vin - vout) ** 2 / resistance
       temp = (-a - np.sqrt(a ** 2 + 4 * b * np.log(resistance / R0))) / (2 * b) + T0
       set_duty(1 if temp < setpoint else 0)
示例#7
0
setpoint = 0
try:
    setpoint = float(sys.argv[1])
except:
    print("USAGE: python3 p2_hw8.py SETPOINT\n where SETPOINT is desired temp in K")
    sys.exit()

try:
    temperature = []
    rtd_temp = []
    t = []
    t0 = time.time()
    while True:
        tee0 = time.time()
        set_duty(0.5)
        while time.time() - 1 < tee0:
           resistance, power, temp = heat.get_rpt()
           if len(t) < 1 or time.time() > t[-1] + .1 + t0:
               rtd_temp.append(rtd.get())
               temperature.append(temp)
               t.append(time.time() - t0)
               print("temp: {}K".format(temp))
        set_duty(0)
        while heat.get() > setpoint:
           if len(t) < 1 or time.time() > t[-1] + .1 + t0:
               temp = rtd.get()
               rtd_temp.append(rtd.get())
               temperature.append(heat.get())
               t.append(time.time() - t0)
               print("temp: {}K".format(temp))
示例#8
0
def main():
    if pwm.run_synth() == 0:
        print "Error running synth."
        return

    try:
        window = Tkinter.Tk()
        window.title("libmapper PWM synth demo")

        name = [False, Tkinter.StringVar()]
        name[1].set("Waiting for device name...")
        label = Tkinter.Label(window, textvariable=name[1])
        label.pack()

        gain = Tkinter.Scale(window,
                             from_=0,
                             to=100,
                             label='gain',
                             orient=Tkinter.HORIZONTAL,
                             length=300,
                             command=lambda n: pwm.set_gain(float(n) / 100.0))
        gain.pack()

        freq = Tkinter.Scale(window,
                             from_=0,
                             to=1000,
                             label='freq',
                             orient=Tkinter.HORIZONTAL,
                             length=300,
                             command=lambda n: pwm.set_freq(float(n)))
        freq.pack()

        duty = Tkinter.Scale(window,
                             from_=0,
                             to=100,
                             label='duty',
                             orient=Tkinter.HORIZONTAL,
                             length=300,
                             command=lambda n: pwm.set_duty(float(n) / 100.0))
        duty.pack()

        dev = mapper.device("tk_pwm", 9000)

        dev.add_input("/gain", 1, 'f', None, 0, 100,
                      lambda s, i, n, t: gain.set(n))
        dev.add_input("/freq", 1, 'f', "Hz", 0, 1000,
                      lambda s, i, n, t: freq.set(n))
        dev.add_input("/duty", 1, 'f', None, 0, 100,
                      lambda s, i, n, t: duty.set(n))

        def do_poll():
            dev.poll(0)
            if not name[0] and dev.ready():
                name[0] = True
                name[1].set('Device name: %s, listening on port %d' %
                            (dev.name, dev.port))
            window.after(5, do_poll)

        do_poll()
        window.mainloop()

    finally:
        pwm.stop_synth()
示例#9
0
except:
    print(
        "USAGE: python3 p2_hw8.py SETPOINT\n where SETPOINT is desired temp in K"
    )
    sys.exit()

try:
    while True:
        resistance, power, temp = heat.get_rpt()

        box_temp.append(temp)
        if time.time() - TINTERVAL > t0:
            box_temp.popleft()

        mtemp = sum(list(box_temp)) / len(box_temp)
        set_duty(duty(setpoint) if mtemp < setpoint else 0)

        if len(t) < 1 or time.time() > t[-1] + 5 + t0:
            t.append(time.time() - t0)
            temperature.append(temp)
            print("Resistance: {} Ohms Power: {} J Temperature: {} K".format(
                resistance, power, temp))

except KeyboardInterrupt:
    set_duty(0)
    plt.plot(t, temperature)
    plt.xlabel("Time (s)")
    plt.ylabel("Temperature (K)")
    plt.show()
    np.savetxt(sys.argv[1], np.vstack((t, temperature)))
示例#10
0
    t -= setpoint
    max_ = np.max(np.abs(t))
    print("Tested {} with osc {}".format(duty, max_))
    plt.scatter(*zip(*enumerate(temperatures)))
    plt.show()
    return max_


points = [0.0078125, 0.01171875]
try:
    while True:
        # binary search for the best duty cycle
        midpoint = np.mean(points)
        lowtest = np.mean([points[0], midpoint])
        hightest = np.mean([points[1], midpoint])
        low_osc = test_duty(lowtest)
        high_osc = test_duty(hightest)
        osc = 0
        if low_osc <= high_osc:
            points = [points[0], midpoint]
            osc = low_osc
        else:
            points = [midpoint, points[1]]
            osc = high_osc
        print("Range {} with oscillation {} Celsius".format(points, osc))

except KeyboardInterrupt:
    set_duty(0)
    print("Duty set to 0. Exiting.")
    sys.exit()
示例#11
0
except:
    print(
        "USAGE: python3 p2_hw8.py SETPOINT\n where SETPOINT is desired temp in K"
    )
    sys.exit()

try:
    while True:
        resistance, power, temp = heat.get_rpt()

        box_temp.append(temp)
        if time.time() - TINTERVAL > t0:
            box_temp.popleft()

        mtemp = sum(list(box_temp)) / len(box_temp)
        set_duty(duty(setpoint, mtemp))

        if len(t) < 1 or time.time() > t[-1] + 5 + t0:
            t.append(time.time() - t0)
            temperature.append(temp)
            print("Resistance: {} Ohms Power: {} J Temperature: {} K".format(
                resistance, power, temp))

except KeyboardInterrupt:
    set_duty(0)
    plt.plot(t, temperature)
    plt.xlabel("Time (s)")
    plt.ylabel("Temperature (K)")
    plt.show()
    np.savetxt(sys.argv[2], np.vstack((t, temperature)))
示例#12
0
def safe_exit():
    pwm.set_duty(0)
    raise OnFireError
示例#13
0
    setpoint = float(sys.argv[1])
except:
    print("USAGE: python3 p2_hw8.py SETPOINT\n where SETPOINT is desired temp in K")
    sys.exit()

try:
    DUTY = float(sys.argv[2])
except:
    pass

try:
    while True:
       vin = adc.readADCDifferential01(4096, 128)*0.001  # returns float
       vout = adc.readADCDifferential23(4096, 128)*0.001  # returns float
       current = vout / 100e3
       resistance = (vin - vout) / current
       power = (vin - vout) ** 2 / resistance
       temp = (-a - np.sqrt(a ** 2 + 4 * b * np.log(resistance / R0))) / (2 * b) + T0
       set_duty(DUTY if temp < setpoint else 0)
       if len(t) < 1 or time.time() > t[-1] + 5 + t0:
           t.append(time.time() - t0)
           temperature.append(temp)
           print("Resistance: {} Ohms Power: {} J Temperature: {} K".format(resistance, power, temp))
except KeyboardInterrupt:
    set_duty(0)
    plt.plot(t, temperature)
    plt.xlabel("Time (s)")
    plt.ylabel("Temperature (K)")
    plt.show()
    np.savetxt(sys.argv[1], np.vstack((t, temperature)))
示例#14
0
def safe_exit():
    pwm.set_duty(0)
    sys.exit()