refactoring

This commit is contained in:
2024-02-22 10:00:00 +01:00
parent 5538c683c3
commit a3b100b419
30 changed files with 1001 additions and 654 deletions

73
examples/dc_signal.py Normal file → Executable file
View File

@@ -1,28 +1,57 @@
from fn_gen import DG2052
from fn_gen.enums import OutputChannel
import fn_gen.errors as fg_err
import logging
import time
#!/usr/bin/env python3
import argparse
from typing import Literal
if __name__ == "__main__":
import fn_gen.errors as fg_err
from fn_gen import DG2052
from common import close_output, get_postamplified, get_preamplified
from examples.discrete_sweep import AMPLIFICATION
def closeOutput(fg: DG2052, channel: Literal[1, 2]):
fg.set_output(channel, False)
print(f"Output{channel} State: {fg.get_output_state(channel)}")
def generateDCSignal(v_dc):
fg = DG2052("TCPIP::192.168.1.11::INSTR")
v_dc = get_preamplified(13.0, v_dc)
channel = 2
try:
# logging.basicConfig(filename="dg2052.log", encoding="utf-8", level=logging.DEBUG)
fn_gen = DG2052("TCPIP::192.168.1.11::INSTR")
channel = OutputChannel.TWO
print(fn_gen.whoami())
print( f"\nOutput{channel.value} Impedance: {fn_gen.get_output_impedance(channel)} Ohm" )
print( f"Output{channel.value} Load: {fn_gen.get_output_load(channel)} Ohm" )
print( f"Output{channel.value} Voltage Limits: {fn_gen.get_output_volt_limits(channel)} V" )
fn_gen.set_dc(channel, 10)
print( f"Output{channel.value}: {fn_gen.get_output_signal(channel)} | {fn_gen.get_output_state(channel)}" )
fn_gen.set_output(channel, True)
print( f"Output{channel.value} State: {fn_gen.get_output_state(channel)}" )
time.sleep(5)
fn_gen.set_output(channel, False)
print( f"Output{channel.value} State: {fn_gen.get_output_state(channel)}" )
except fg_err.UndefinedCommunicationMethodError as err:
print(err)
print(fg.whoami())
print(f"\nOutput{channel} Impedance: {fg.get_output_impedance(channel)} Ohm")
print(f"Output{channel} Load: {fg.get_output_load(channel)} Ohm")
print(f"Output{channel} Voltage Limits: {fg.get_output_volt_limits(channel)} V")
fg.set_dc(channel, v_dc)
print(
f"Output{channel}: {fg.get_output_signal(channel)} | {fg.get_output_state(channel)}"
)
fg.set_output(channel, True)
print(f"Voltage: {get_postamplified(AMPLIFICATION, v_dc):.2f} V")
print(f"Output{channel} State: {fg.get_output_state(channel)}")
while True:
pass
except fg_err.ValueOutOfBoundsError as err:
print(err)
except fg_err.UndefinedValueError as err:
print(err)
except KeyboardInterrupt:
close_output(fg, channel)
finally:
close_output(fg, channel)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="This program is for testing the DG2052 function generator library. It generates a DC signal based on the voltage value supplied."
)
parser.add_argument(
"-v",
"--voltage",
type=float,
required=True,
help="The DC voltage supplied",
)
args = parser.parse_args()
generateDCSignal(args.voltage)

View File

@@ -1,29 +1,58 @@
from fn_gen import DG2052
from fn_gen.enums import OutputChannel
import fn_gen.errors as fg_err
import logging
import time
import argparse
if __name__ == "__main__":
import fn_gen.errors as fg_err
from fn_gen import DG2052
from common import close_output, get_postamplified, get_preamplified, AMPLIFICATION
def generate_ramp_wave(v, freq, phase):
fg = DG2052("TCPIP::192.168.1.11::INSTR")
v = get_preamplified(AMPLIFICATION, v)
channel = 2
try:
# logging.basicConfig(filename="dg2052.log", encoding="utf-8", level=logging.DEBUG)
fn_gen = DG2052("TCPIP::192.168.1.11::INSTR")
channel = OutputChannel.TWO
print(fn_gen.whoami())
print( f"\nOutput{channel.value} Impedance: {fn_gen.get_output_impedance(channel)} Ohm" )
print( f"Output{channel.value} Load: {fn_gen.get_output_load(channel)} Ohm" )
print( f"Output{channel.value} Voltage Limits: {fn_gen.get_output_volt_limits(channel)} V" )
fn_gen.set_ramp(channel, 500, 2.5, 0, 0)
print( f"Output{channel.value}: {fn_gen.get_output_signal(channel)} | {fn_gen.get_output_state(channel)}" )
fn_gen.set_output(channel, True)
print( f"Output{channel.value} State: {fn_gen.get_output_state(channel)}" )
time.sleep(5)
fn_gen.set_output(channel, False)
print( f"Output{channel.value} State: {fn_gen.get_output_state(channel)}" )
except fg_err.UndefinedCommunicationMethodError as err:
print(err)
print(fg.whoami())
print(f"\nOutput{channel} Impedance: {fg.get_output_impedance(channel)} Ohm")
print(f"Output{channel} Load: {fg.get_output_load(channel)} Ohm")
print(f"Output{channel} Voltage Limits: {fg.get_output_volt_limits(channel)} V")
fg.set_ramp(channel, freq, v, 0, phase)
print(
f"Output{channel}: {fg.get_output_signal(channel)} | {fg.get_output_state(channel)}"
)
fg.set_output(channel, True)
print(f"Voltage: {get_postamplified(AMPLIFICATION, v):.2f} V")
print(f"Frequency: {freq} Hz")
print(f"Output{channel} State: {fg.get_output_state(channel)}")
while True:
pass
except fg_err.ValueOutOfBoundsError as err:
print(err)
except fg_err.UndefinedValueError as err:
print(err)
except KeyboardInterrupt:
close_output(fg, channel)
finally:
close_output(fg, channel)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="This program is for testing the DG2052 function genrator library. It generates a ramp wave with the supplied parameters."
)
parser.add_argument(
"-v", "--voltage", type=float, required=True, help="The voltage supplied"
)
parser.add_argument(
"--freq", type=int, default=200, help="The frequency of the sine wave generated"
)
parser.add_argument(
"--phase",
type=int,
default=0,
help="The phase shift of the signal generated",
)
args = parser.parse_args()
if args.phase not in range(0, 360):
parser.print_help()
exit(1)
generate_ramp_wave(args.v, args.freq, args.phase)

View File

@@ -1,28 +1,58 @@
from fn_gen import DG2052
from fn_gen.enums import OutputChannel
import fn_gen.errors as fg_err
import logging
import time
import argparse
if __name__ == "__main__":
import fn_gen.errors as fg_err
from common import close_output, get_preamplified, get_postamplified, AMPLIFICATION
from fn_gen import DG2052
def generate_sine_wave(v: float, freq: int, phase: int):
fg = DG2052("TCPIP::192.168.1.11::INSTR")
channel = 2
v = get_preamplified(AMPLIFICATION, v)
try:
# logging.basicConfig(filename="dg2052.log", encoding="utf-8", level=logging.DEBUG)
fn_gen = DG2052("TCPIP::192.168.1.11::INSTR")
channel = OutputChannel.TWO
print(fn_gen.whoami())
print( f"\nOutput{channel.value} Impedance: {fn_gen.get_output_impedance(channel)} Ohm" )
print( f"Output{channel.value} Load: {fn_gen.get_output_load(channel)} Ohm" )
print( f"Output{channel.value} Voltage Limits: {fn_gen.get_output_volt_limits(channel)} V" )
fn_gen.set_sine_wave(channel, 500, 2.5, 0, 0)
print( f"Output{channel.value}: {fn_gen.get_output_signal(channel)} | {fn_gen.get_output_state(channel)}" )
fn_gen.set_output(channel, True)
print( f"Output{channel.value} State: {fn_gen.get_output_state(channel)}" )
time.sleep(5)
fn_gen.set_output(channel, False)
print( f"Output{channel.value} State: {fn_gen.get_output_state(channel)}" )
except fg_err.UndefinedCommunicationMethodError as err:
print(err)
print(fg.whoami())
print("")
# input("Press Enter to start...")
fg.set_sine_wave(channel, freq, v, 0, phase)
print(
f"Output{channel}: {fg.get_output_signal(channel)} | {fg.get_output_state(channel)}"
)
fg.set_output(channel, True)
print(f"Voltage: {get_postamplified(AMPLIFICATION, v):.2f} V")
print(f"Frequency: {freq} Hz")
print(f"Output{channel} State: {fg.get_output_state(channel)}")
while True:
pass
except fg_err.ValueOutOfBoundsError as err:
print(err)
except fg_err.UndefinedValueError as err:
print(err)
except KeyboardInterrupt:
close_output(fg, channel)
finally:
close_output(fg, channel)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="This program is for testing the DG2052 function genrator library. It generates a sine wave with the supplied parameters."
)
parser.add_argument(
"-v", "--voltage", type=float, required=True, help="The voltage supplied"
)
parser.add_argument(
"--freq", type=int, default=200, help="The frequency of the sine wave generated"
)
parser.add_argument(
"--phase",
type=int,
default=0,
help="The phase shift of the signal generated (must be between 0 and 360)",
)
args = parser.parse_args()
if args.phase not in range(0, 360):
parser.print_help()
exit(1)
generate_sine_wave(
v=args.voltage, freq=args.freq, phase=args.phase
)

View File

@@ -1,28 +1,55 @@
import argparse
from fn_gen import DG2052
from fn_gen.enums import OutputChannel
import fn_gen.errors as fg_err
import logging
import time
from common import close_output, get_preamplified, get_postamplified, AMPLIFICATION
if __name__ == "__main__":
def generate_square_wave(v: float, freq: int, phase: int):
"""
This program is for testing the DG2052 function genrator library. It generates a square wave with the supplied parameters.
"""
fg = DG2052("TCPIP::192.168.1.11::INSTR")
v = get_preamplified(AMPLIFICATION, v)
channel = 2
try:
# logging.basicConfig(filename="dg2052.log", encoding="utf-8", level=logging.DEBUG)
fn_gen = DG2052("TCPIP::192.168.1.11::INSTR")
channel = OutputChannel.TWO
print(fn_gen.whoami())
print( f"\nOutput{channel.value} Impedance: {fn_gen.get_output_impedance(channel)} Ohm" )
print( f"Output{channel.value} Load: {fn_gen.get_output_load(channel)} Ohm" )
print( f"Output{channel.value} Voltage Limits: {fn_gen.get_output_volt_limits(channel)} V" )
fn_gen.set_square_wave(channel, 500, 2.5, 0, 0)
print( f"Output{channel.value}: {fn_gen.get_output_signal(channel)} | {fn_gen.get_output_state(channel)}" )
fn_gen.set_output(channel, True)
print( f"Output{channel.value} State: {fn_gen.get_output_state(channel)}" )
time.sleep(5)
fn_gen.set_output(channel, False)
print( f"Output{channel.value} State: {fn_gen.get_output_state(channel)}" )
except fg_err.UndefinedCommunicationMethodError as err:
print(err)
print(fg.whoami())
print(f"\nOutput{channel} Impedance: {fg.get_output_impedance(channel)} Ohm")
print(f"Output{channel} Load: {fg.get_output_load(channel)} Ohm")
print(f"Output{channel} Voltage Limits: {fg.get_output_volt_limits(channel)} V")
fg.set_square_wave(channel, freq, v, 0, phase)
print(
f"Output{channel}: {fg.get_output_signal(channel)} | {fg.get_output_state(channel)}"
)
fg.set_output(channel, True)
print(f"Voltage: {get_postamplified(AMPLIFICATION,v)} V")
print(f"Frequency: {freq} Hz")
print(f"Output{channel} State: {fg.get_output_state(channel)}")
while True:
pass
except fg_err.ValueOutOfBoundsError as err:
print(err)
except fg_err.UndefinedValueError as err:
print(err)
except KeyboardInterrupt:
close_output(fg, channel)
finally:
close_output(fg, channel)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="")
parser.add_argument("-v", "--voltage", type=float, required=True, help="The voltage supplied")
parser.add_argument(
"--freq", type=int, default=200, help="The frequency of the square wave generated"
)
parser.add_argument(
"--phase",
type=int,
default=0,
help="The phase shift of the signal generated (must be between 0 and 360)",
)
args = parser.parse_args()
if args.phase not in range(0,360):
parser.print_help()
exit(1)
generate_square_wave(args.voltage, args.freq, args.phase)

View File

@@ -1,28 +1,134 @@
from fn_gen import DG2052
from fn_gen.enums import OutputChannel, SweepSpacing
import fn_gen.errors as fg_err
import logging
import time
import argparse
if __name__ == "__main__":
import fn_gen.errors as fg_err
from common import close_output, get_preamplified
from fn_gen import DG2052
from fn_gen.enums import SweepSignalType, SweepSpacing, SweepTriggerSource
def sweep_over_signal(
signal: str,
v_min: float,
v_max: float,
delay: int,
duration: int,
freq_start: int,
freq_stop: int,
phase: int,
spacing: str,
):
fg = DG2052("TCPIP::192.168.1.11::INSTR")
v_min = get_preamplified(13.0, v_min)
v_max = get_preamplified(13.0, v_max)
channel = 2
signal_type = SweepSignalType.SINE
match signal:
case "sine":
signal_type = SweepSignalType.SINE
case "square":
signal_type = SweepSignalType.SQUARE
case "ramp":
signal_type = SweepSignalType.RAMP
spacing_type = SweepSpacing.LOG
match spacing:
case "lin":
spacing_type = SweepSpacing.LIN
case "log":
spacing_type = SweepSpacing.LOG
try:
# logging.basicConfig(filename="dg2052.log", encoding="utf-8", level=logging.DEBUG)
fn_gen = DG2052("TCPIP::192.168.1.11::INSTR")
channel = OutputChannel.TWO
print(fn_gen.whoami())
print( f"\nOutput{channel.value} Impedance: {fn_gen.get_output_impedance(channel)} Ohm" )
print( f"Output{channel.value} Load: {fn_gen.get_output_load(channel)} Ohm" )
print( f"Output{channel.value} Voltage Limits: {fn_gen.get_output_volt_limits(channel)} V" )
fn_gen.set_sweep(channel, htime_start=1, htime_stop=1, time=10, spacing=SweepSpacing.LOG)
print( f"Output{channel.value}: {fn_gen.get_output_signal(channel)} | {fn_gen.get_output_state(channel)}" )
fn_gen.set_output(channel, True)
print( f"Output{channel.value} State: {fn_gen.get_output_state(channel)}" )
time.sleep(20)
fn_gen.set_output(channel, False)
print( f"Output{channel.value} State: {fn_gen.get_output_state(channel)}" )
except fg_err.UndefinedCommunicationMethodError as err:
print(err)
print(fg.whoami())
print(f"\nOutput{channel} Impedance: {fg.get_output_impedance(channel)} Ohm")
print(f"Output{channel} Load: {fg.get_output_load(channel)} Ohm")
print(f"Output{channel} Voltage Limits: {fg.get_output_volt_limits(channel)} V")
fg.set_sweep(
channel,
amp=(v_max + v_min)/2,
offset=0,
phase=phase,
signal_type=signal_type,
freq_start=freq_start,
freq_stop=freq_stop,
spacing=spacing_type,
trigger_source=SweepTriggerSource.MANUAL,
time=duration,
)
print(
f"Output{channel}: {fg.get_output_signal(channel)} | {fg.get_output_state(channel)}"
)
fg.set_output(channel, True)
time.sleep(delay)
fg.trigger_sweep(channel)
t0 = time.time()
t1 = time.time()
while (t1 - t0) < duration:
print(f"Current Frequency: {fg.get_output_signal(channel)}")
t1 = time.time()
print(f"Output{channel} State: {fg.get_output_state(channel)}")
except fg_err.ValueOutOfBoundsError as err:
print(err)
except fg_err.UndefinedValueError as err:
print(err)
except KeyboardInterrupt:
pass
finally:
close_output(fg, channel)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="This program is for testing the DG2052 function genrator library. It sweeps over a signal with the supplied parameters."
)
parser.add_argument(
"-s",
"--signal",
type=str,
choices=["sine", "square", "ramp"],
default="sine",
help='The type of signal being sweeped',
)
parser.add_argument(
"--vmin", type=float, default=0, help="The minimum voltage supplied"
)
parser.add_argument(
"--vmax", type=float, default=1, help="The maximum voltage supplied"
)
parser.add_argument(
"-d",
"--delay",
type=int,
default=0,
help="The buffer time before the sweep starts",
)
parser.add_argument(
"--duration", type=int, default=5 * 60, help="The duration of the sweep"
)
parser.add_argument(
"--freq-start", type=int, default=10, help="The start frequency of the sweep"
)
parser.add_argument(
"--freq-stop", type=int, default=1000, help="The stop frequency of the sweep"
)
parser.add_argument(
"-p",
"--phase",
type=int,
default=0,
help="The phase shift of the signal generated (must be between 0 and 360)",
)
parser.add_argument(
"--spacing",
type=str,
choices=["lin", "log"],
default="log",
help='The spacing of the sweep',
)
args = parser.parse_args()
if (
args.phase not in range(0, 360)
or args.spacing not in ["lin", "log"]
or args.signal not in ["sine", "square", "ramp"]
):
parser.print_help()
exit(1)
sweep_over_signal(args.signal, args.vmin, args.vmax, args.delay, args.duration, args.freq_start, args.freq_stop, args.phase, args.spacing)

View File

@@ -1,15 +1,13 @@
import pyvisa
import sys
import click
from fn_gen import DG2052
@click.command()
def print_whoami():
"""
This program is for testing the DG2052 function generator library. It prints the conncted device identification.
"""
fg = DG2052("TCPIP::192.168.1.11::INSTR")
click.echo(f"{fg.whoami()}")
if __name__ == "__main__":
rm = pyvisa.ResourceManager()
resources = rm.list_resources() # list available resources
if len(resources) == 0:
print("No Resources Found!")
sys.exit(1)
else:
print(f"Available Resources: {resources}")
port = resources[0] # Select the first resource
inst = DG2052(port)
print(inst.whoami())
print_whoami()