Skip to content

Commit

Permalink
switched to abstract instrument class
Browse files Browse the repository at this point in the history
  • Loading branch information
CatherineH committed Sep 17, 2016
1 parent 0f7eff4 commit 84da64c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 42 deletions.
6 changes: 3 additions & 3 deletions doc/examples/minghe/ex_minghe_mhs5200.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
print(mhs.serial_number)
mhs.channel[0].frequency = 3000000*pq.Hz
print(mhs.channel[0].frequency)
mhs.channel[0].wave_type = MHS5200.WaveType.sawtooth_down
mhs.channel[0].function = MHS5200.Function.sawtooth_down
print(mhs.channel[0].wave_type)
mhs.channel[0].amplitude = 9.0*pq.V
mhs.channel[0].amplitude[0] = 9.0*pq.V
print(mhs.channel[0].amplitude)
mhs.channel[0].offset = -0.5
print(mhs.channel[0].offset)
mhs.channel[0].phase = 90
print(mhs.channel[0].phase)

mhs.channel[1].wave_type = MHS5200.WaveType.sawtooth_up
mhs.channel[1].wave_type = MHS5200.Function.sawtooth_up
63 changes: 34 additions & 29 deletions instruments/minghe/mhs5200a.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
from enum import Enum

import quantities as pq

from instruments.abstract_instruments import Instrument
from instruments.abstract_instruments import Instrument, FunctionGenerator
from instruments.util_fns import ProxyList, assume_units

# CLASSES #####################################################################
Expand All @@ -30,17 +29,17 @@ class MHS5200(Instrument):
communications protocol:
https://github.com/wd5gnr/mhs5200a/blob/master/MHS5200AProtocol.pdf
"""
# pylint: disable=unused-variable
def __init__(self, filelike):
super(MHS5200, self).__init__(filelike)
self._channel_count = 2

# INNER CLASSES #

class Channel(object):
class Channel(FunctionGenerator):
"""
Class representing a channel on the MHS52000.
"""
# pylint: disable=protected-access

__CHANNEL_NAMES = {
1: '1',
Expand All @@ -49,14 +48,14 @@ class Channel(object):

def __init__(self, mhs, idx):
self._mhs = mhs
super(MHS5200.Channel, self).__init__(self._mhs._file)
# Use zero-based indexing for the external API, but one-based
# for talking to the instrument.
self._idx = idx + 1
self._chan = self.__CHANNEL_NAMES[self._idx]
self._count = 0

@property
def amplitude(self):
def _get_amplitude_(self):
"""
Gets/Sets the amplitude of this channel.
Expand All @@ -66,13 +65,17 @@ def amplitude(self):
"""
query = ":r{0}a".format(self._chan)
response = self._mhs.query(query)
return float(response.replace(query, ""))/100.0*pq.V

@amplitude.setter
def amplitude(self, new_val):
new_val = 100*assume_units(new_val, pq.V).rescale(pq.V).magnitude
return float(response.replace(query, ""))/100.0, self.VoltageMode.rms

def _set_amplitude_(self, new_val, units):
if units == self.VoltageMode.peak_to_peak or \
units == self.VoltageMode.rms:
new_val = assume_units(new_val, "V").rescale(pq.V).magnitude
elif units == self.VoltageMode.dBm:
raise NotImplementedError("Decibel units are not supported.")
new_val *= 100
query = ":s{0}a{1}".format(self._chan, int(new_val))
response = self._mhs.query(query)
self._mhs.sendcmd(query)

@property
def duty_cycle(self):
Expand All @@ -92,14 +95,15 @@ def duty_cycle(self):
def duty_cycle(self, new_val):
new_val = assume_units(new_val, pq.s).rescale(pq.s).magnitude
query = ":s{0}d{1}".format(self._chan, int(new_val))
response = self._mhs.query(query)
self._mhs.sendcmd(query)

@property
def enable(self):
"""
Gets/Sets the enable state of this channel.
:param bool new_val: the enable state
:param new_val: the enable state
:type: `bool`
"""
query = ":r{0}b".format(self._chan)
return int(self._mhs.query(query).replace(query, "").
Expand All @@ -108,7 +112,7 @@ def enable(self):
@enable.setter
def enable(self, new_val):
query = ":s{0}b{1}".format(self._chan, int(new_val))
response = self._mhs.query(query)
self._mhs.sendcmd(query)

@property
def frequency(self):
Expand All @@ -129,16 +133,16 @@ def frequency(self, new_val):
new_val = assume_units(new_val, pq.Hz).rescale(pq.Hz).\
magnitude*100.0
query = ":s{0}f{1}".format(self._chan, int(new_val))
response = self._mhs.query(query)
self._mhs.sendcmd(query)

@property
def offset(self):
"""
Gets/Sets the offset of this channel.
:units: As specified (if a `~quantities.Quantity`) or assumed to be
of fraction.
:type: `~quantities.Quantity`
:param new_val: The fraction of the duty cycle to offset the
function by.
:type: `float`
"""
# need to convert
query = ":r{0}o".format(self._chan)
Expand All @@ -149,7 +153,7 @@ def offset(self):
def offset(self, new_val):
new_val = int(new_val*100)+120
query = ":s{0}o{1}".format(self._chan, new_val)
response = self._mhs.query(query)
self._mhs.sendcmd(query)

@property
def phase(self):
Expand All @@ -163,31 +167,32 @@ def phase(self):
# need to convert
query = ":r{0}p".format(self._chan)
response = self._mhs.query(query)
return int(response.replace(query, ""))
return int(response.replace(query, ""))*pq.deg

@phase.setter
def phase(self, new_val):
new_val = assume_units(new_val, pq.deg).rescale("deg").magnitude
query = ":s{0}p{1}".format(self._chan, int(new_val))
response = self._mhs.query(query)
self._mhs.sendcmd(query)

@property
def wave_type(self):
def function(self):
"""
Gets/Sets the wave type of this channel.
:type: `MHS5200.WaveType`
"""
query = ":r{0}w".format(self._chan)
response = self._mhs.query(query).replace(query, "")
return self._mhs.WaveType(int(response))
return self._mhs.Function(int(response))

@wave_type.setter
def wave_type(self, new_val):
@function.setter
def function(self, new_val):
query = ":s{0}w{1}".format(self._chan,
self._mhs.WaveType(new_val).value)
response = self._mhs.query(query)
self._mhs.Function(new_val).value)
self._mhs.sendcmd(query)

class WaveType(Enum):
class Function(Enum):
"""
Enum containing valid wave modes for
"""
Expand Down
35 changes: 25 additions & 10 deletions instruments/tests/test_minghe/test_minghe_mhs5200a.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,27 @@ def test_mhs_amplitude():
],
sep="\n"
) as mhs:
assert mhs.channel[0].amplitude == 3.3*pq.V
assert mhs.channel[1].amplitude == 5.0*pq.V
assert mhs.channel[0].amplitude[0] == 3.3*pq.V
assert mhs.channel[1].amplitude[0] == 5.0*pq.V
mhs.channel[0].amplitude = 6.6*pq.V
mhs.channel[1].amplitude = 8.0*pq.V


@raises(NotImplementedError)
def test_mhs_amplitude_dbm_notimplemented():
with expected_protocol(
ik.minghe.MHS5200,
[
":s1a660"
],
[
"ok"
],
sep="\n"
) as mhs:
mhs.channel[0].amplitude = 6.6*ik.units.dBm


def test_mhs_duty_cycle():
with expected_protocol(
ik.minghe.MHS5200,
Expand Down Expand Up @@ -94,13 +109,13 @@ def test_mhs_frequency():
[
":r1f",
":r2f",
":s1f6000",
":s2f8000"
":s1f600000",
":s2f800000"

],
[
":r1f33000",
":r2f500000",
":r1f3300000",
":r2f50000000",
"ok",
"ok"
],
Expand Down Expand Up @@ -178,10 +193,10 @@ def test_mhs_wave_type():
],
sep="\n"
) as mhs:
assert mhs.channel[0].wave_type == mhs.WaveType.sine
assert mhs.channel[1].wave_type == mhs.WaveType.square
mhs.channel[0].wave_type = mhs.WaveType.triangular
mhs.channel[1].wave_type = mhs.WaveType.sawtooth_up
assert mhs.channel[0].function == mhs.Function.sine
assert mhs.channel[1].function == mhs.Function.square
mhs.channel[0].function = mhs.Function.triangular
mhs.channel[1].function = mhs.Function.sawtooth_up


def test_mhs_serial_number():
Expand Down

0 comments on commit 84da64c

Please sign in to comment.