camrasdevices.py 2.9 KB
Newer Older
1
2
import astropy.units as u
from astropy.units import Quantity
3

4
5
import hpib
import serial.threaded
6

7
8
9

class CamrasHpibDevice(object):
    """Wrapper around HPIB commands"""
10

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
11
12
    command_thread = None

13
    def __init__(self, address, check_frequency=True):
14
        self.hpib_address = address
15
        self._check_frequency = check_frequency
16

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
17
        if not CamrasHpibDevice.command_thread:
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
18
            serial_port = serial.Serial("/dev/ttyUSB0")
19
20
            CamrasHpibDevice.command_thread = serial.threaded.ReaderThread(serial_port,
                                                                           hpib.GPIBProtocol)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
21
            CamrasHpibDevice.command_thread.start()
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
22
            CamrasHpibDevice.command_thread._connection_made.wait()
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
23
            CamrasHpibDevice.command_thread.protocol.init_hpib()
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
24

25
26
    @property
    def frequency(self):
27
28
29
30
31
32
33
34
35
36
37
38
39
        """Frequency of the device as an astropy Quantity.

        Returns:
            Quantity: frequency (in Hz) of the device

        Raises:
            RuntimeError: If the device does not respond
        """
        freq_str = CamrasHpibDevice.command_thread.protocol.command("freq?",
                                                                    address=self.hpib_address)
        if not freq_str or len(freq_str) == 0:
            raise RuntimeError("Camras device at address {} is not responding".format(
                self.hpib_address))
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
40
        return int(float(freq_str)) * u.Hz
41
42
43

    @frequency.setter
    def frequency(self, freq):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
44
45
46
47
        """Set the device to the specified frequency. Throws a RuntimeError if failed

        Args:
            freq (Union[float, Quantity]): new frequency. If no unit present, assume Hz
48
49
50
51

        Raises:
            RuntimeError: If the device sticks to a different frequency (e.g. if the specified
                frequency is outside the accepted range for the device)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
52
        """
53
54
        if isinstance(freq, Quantity):
            freq = freq.to(u.Hz).value
55
56
        CamrasHpibDevice.command_thread.protocol.command("freq {:d} Hz".format(int(freq)),
                                                         address=self.hpib_address)
57
        new_freq = self.frequency
58
        if self._check_frequency and new_freq.to(u.Hz).value != int(freq):
59
            raise RuntimeError("Setting frequency failed: tried to set to {}, it is now {}".format(
60
61
                freq, new_freq))

62
63
64
65

class Receiver(CamrasHpibDevice):
    """Wrapper around HPIB commands for the Rohde & Schwartz receiver"""

66
    def __init__(self, **kwargs):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
67
        super(Receiver, self).__init__(1)
68

69

70
class LocalOscillator(CamrasHpibDevice):
71
72
    """Wrapper around HPIB commands for the local oscillator"""

73
    def __init__(self, **kwargs):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
74
        super(LocalOscillator, self).__init__(28)
75

76

77
78
class ClockGenerator(CamrasHpibDevice):
    """Wrapper around HPIB commands for the clock generator (should be at 140MHz)"""
79

80
    def __init__(self, **kwargs):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
81
        super(ClockGenerator, self).__init__(14)