aboutsummaryrefslogtreecommitdiff
path: root/examples/lock_eit.py
blob: 0cdd86fd8ca9037aad7a1b750a6ba4ba43788f05 (plain)
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
import pyvisa
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import time

from qolab.hardware.scope import SDS1104X
from qolab.hardware.rf_generator import AgilentE8257D
from qolab.feedback import PID
from qolab.data import TraceSetSameX, TraceXY, Trace

def adjustRF(pid, scope, rfgen):
    pid.reset()

    errorTrace = Trace("error")
    errorTrace.config['unit']='V'
    timeTrace = Trace("time")
    timeTrace.config['unit']='S'
    errorLog = TraceXY("error")
    errorLog.x = timeTrace
    errorLog.y = errorTrace

    freqTrace = Trace("frequency")
    freqTrace.config['unit']='Hz'
    freqLog = TraceXY("frequency")
    freqLog.x = timeTrace
    freqLog.y = freqTrace

    feedbackTrace = Trace("feedback")
    feedbackTrace.config['unit']='Hz'
    feedbackLog = TraceXY("feedback")
    feedbackLog.x = timeTrace
    feedbackLog.y = feedbackTrace


    log = TraceSetSameX("timelog")
    log.addTrace(errorLog)
    log.addTrace(freqLog)
    log.addTrace(feedbackLog)


    # while True:
    for i in range(0,10000):
        timenow = time.time()

        ch1=scope.getTrace(1, decimate=False)
        err = np.mean(ch1.y.values)
        fdbck = pid.feedback(err)

        freq0 = rfgen.getFreqFixed()
        freq = freq0 + fdbck
        rfgen.setFreqFixed(freq)
        print(f'error = {err}, feedback = {fdbck}, request freq = {freq}')

        log.addPointToTrace(timenow)
        log.addPointToTrace(err, "error")
        log.addPointToTrace(freq0, "frequency")
        log.addPointToTrace(fdbck, "feedback")
        # time.sleep(.01)

    plt.clf()
    log.plot()
    return log



if __name__ == '__main__': 
    print("testing")
    rm = pyvisa.ResourceManager()
    instr_scope=rm.open_resource('TCPIP::192.168.0.61::INSTR')
    scope = SDS1104X(instr_scope)
    instr_rfgen=rm.open_resource('TCPIP::192.168.0.114::INSTR')
    rfgen = AgilentE8257D(instr_rfgen)
    print('------ Header start -------------')
    print(str.join('\n', scope.getHeader()))
    print(str.join('\n', rfgen.getHeader()))
    print('------ Header ends  -------------')
    ch1  = scope.getTrace(1)
    traces = scope.getAllTraces()

    freqZero = 6834687190
    freqDeltaM2 = 6835387164
    SweepSpan = 10000

    rfgen.stopFrequencySweep()
    rfgen.setFreqFixed(freqDeltaM2+100)

    pid = PID(100,400,0, sign=-1)
    log=adjustRF(pid, scope, rfgen)

    log.save('lock.dat', item_format='.15e')