BNFreqSignUpdate.py 1.66 KB
Newer Older
1
import time
controls's avatar
controls committed
2
from epics import caget, caput
3
4
5
6
7
8
9
10
11
12
13
14
15
import ConfigParser
import argparse
import numpy as np

'''
Author: Anchal Gupta (agupta) - anchal@caltech.edu
Created:  Dec 25, 2018

This code just checks BN Frequency all the time and sets the EPICS channel
for its sign in case a flip has occured
'''
BNFreqCH = "C3:PSL-PRECAV_BEATNOTE_FREQ"
BNFreqSignCH = "C3:PSL-PRECAV_BEATNOTE_FREQ_SIGN"
anchal.gupta's avatar
anchal.gupta committed
16
17
timestep = 1
lookBackTime = 5
Anchal's avatar
Anchal committed
18
# Keep this time 2 times the polling period of precav bn freq
anchal.gupta's avatar
anchal.gupta committed
19
investigationTime = 2
Anchal's avatar
Anchal committed
20
21
# Sign will be changed below cutoff only
freqCutOff = 5  # MHz
Anchal's avatar
Anchal committed
22
23
24
# Reset to looking for trigger after
# Long times will miss sudden twice changes
# But they are rare, so it is fine.
Anchal's avatar
Anchal committed
25
resetTime = 60 # s
Anchal's avatar
Anchal committed
26

Anchal's avatar
Anchal committed
27
28
29
recLen = int(lookBackTime/timestep)
BNFreqTS = np.zeros(recLen)

30
31

def checkFreqFlip(BNFreq):
32
    #Freq Counter has 1 Hz resolution from 1-40 MHz.]
Anchal's avatar
Anchal committed
33
    if not np.any(BNFreq-np.roll(BNFreq,1)):
34
        print("Investigating frequency flip...")
controls's avatar
controls committed
35
        val1 = caget(BNFreqCH)
Anchal's avatar
Anchal committed
36
        time.sleep(investigationTime)
controls's avatar
controls committed
37
        val2 = caget(BNFreqCH)
38
39
40
41
        if np.abs(val1 - val2)<0.000002:
            if BNFreq[0]<10:
                print("Frequency flip found.")
                return True
42
43
44
            else:
                print("False Alarm.")
                return False
Anchal's avatar
Anchal committed
45
46
47
    else:
        return False

48
49

while(1):
controls's avatar
controls committed
50
    Sign  = caget(BNFreqSignCH)
51
    BNFreqTS = np.roll(BNFreqTS,1)
controls's avatar
controls committed
52
    BNFreqTS[0] = caget(BNFreqCH)
53
54
    if checkFreqFlip(BNFreqTS):
        Sign = int(np.mod(Sign+1,2))
controls's avatar
controls committed
55
        caput(BNFreqSignCH,Sign)
56
        print("Sign changed")
Anchal's avatar
Anchal committed
57
58
59
        print("Going down for ",resetTime, " seconds...")
        time.sleep(resetTime)
        print("Trigger ready again.")
60
    time.sleep(timestep)