Source code for cmd_tk_afg3000

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# 
# Copyright 2012-2017 Frédéric Magniette, Miguel Rubio-Roy
# This file is part of Pyrame.
# 
# Pyrame is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# Pyrame is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrame.  If not, see <http://www.gnu.org/licenses/>

import time
import pools,conf_strings,scpi

tk_afg3000_pool=pools.pool()

# Functions

class tk_afg3000_class(scpi.scpi):
    channels=["1","2"]

    def __init__(self):
        super(tk_afg3000_class,self).__init__("tk_afg3000")

    def set_function(self,tk_afg3000_id,function):
        if function not in ["sine","square","ramp","pulse"]:
            return 0,"invalid function"
        value={"sine":"SIN","square":"SQU","ramp":"RAMP","pulse":"PULS"}.get(function)
        command="SOUR{channel}:FUNC %s"%(value)
        return self.simple_command(tk_afg3000_id,command)

    def set_frequency(self,tk_afg3000_id,frequency):
        if frequency=="undef":
            return 1,"ok"
        command="SOUR{channel}:FREQ %.6f"%(float(frequency))
        return self.simple_command(tk_afg3000_id,command)

    def get_frequency(self,tk_afg3000_id):
        command="SOUR{channel}:FREQ?"
        return self.simple_query(tk_afg3000_id,command)

    def set_high_level(self,tk_afg3000_id,high_level):
        if high_level=="undef":
            return 1,"ok"
        command="SOUR{channel}:VOLT:HIGH %.4f"%(float(high_level))
        return self.simple_command(tk_afg3000_id,command)

    def set_low_level(self,tk_afg3000_id,low_level):
        if low_level=="undef":
            return 1,"ok"
        command="SOUR{channel}:VOLT:LOW %.4f"%(float(low_level))
        return self.simple_command(tk_afg3000_id,command)

    def set_pulse_width(self,tk_afg3000_id,pulse_width):
        if pulse_width=="undef":
            return 1,"ok"
        command=r"SOUR{channel}:PULS:WIDT %.4e\n"%(float(pulse_width))
        command+="SOUR{channel}:PULS:HOLD WIDT"
        return self.simple_command(tk_afg3000_id,command)

    def set_edges(self,tk_afg3000_id,rising_edge,falling_edge):
        # Verify edges
        if rising_edge.lower() not in ["min","max","undef"]:
            value_r="%.9e"%(float(rising_edge))
        else:
            value_r=rising_edge
        if falling_edge.lower() not in ["min","max","undef"]:
            value_f="%.9e"%(float(falling_edge))
        else:
            value_f=falling_edge
        # Send the command
        command=""
        if value_r!="undef":
            command+="SOUR{channel}:PULS:TRAN:LEAD %s"%(value_r)
        if "undef" not in [value_r,value_f]:
            command+=r"\n"
        if value_f!="undef":
            command+="SOUR{channel}:PULS:TRAN:TRA %s"%(value_f)
        return self.simple_command(tk_afg3000_id,command)

    def set_symmetry(self,tk_afg3000_id,symmetry):
        if symmetry=="undef":
            return 1,"ok"
        command="SOUR{channel}:FUNC:RAMP:SYMM %.4f"%(float(symmetry))
        return self.simple_command(tk_afg3000_id,command)

    def set_phase(self,tk_afg3000_id,phase):
        if phase=="undef":
            return 1,"ok"
        command="SOUR{channel}:PHAS %.3fDEG"%(float(phase))
        return self.simple_command(tk_afg3000_id,command)

    def set_delay(self,tk_afg3000_id,delay):
        if delay=="undef":
            return 1,"ok"
        if delay.lower() not in ["max","min"]:
            d=float(delay)
        else:
            d=delay
        command="SOUR{channel}:PULS:DEL %.3es"%(d)
        return self.simple_command(tk_afg3000_id,command)

    def power_on(self,tk_afg3000_id):
        command="OUTP{channel} ON"
        return self.simple_command(tk_afg3000_id,command)

    def power_off(self,tk_afg3000_id):
        command="OUTP{channel} OFF"
        return self.simple_command(tk_afg3000_id,command)

me=tk_afg3000_class()

[docs]def init_tk_afg3000(tk_afg3000_id,conf_string): """Initialize tk_afg3000 pattern generator. *conf_string*: must include: - channel: channel on which the tk_afg3000_id will act - bus: conf_string of the underlying link module (gpib, tcp, serial, ...)""" return me.init(tk_afg3000_id,conf_string)
[docs]def deinit_tk_afg3000(tk_afg3000_id): "Deregister an tk_afg3000 from the pool" return me.deinit(tk_afg3000_id)
[docs]def config_tk_afg3000(tk_afg3000_id): "Configure an tk_afg3000" return me.config(tk_afg3000_id)
[docs]def inval_tk_afg3000(tk_afg3000_id): "Invalidate configuration of an tk_afg3000" return me.inval(tk_afg3000_id)
[docs]def reset_tk_afg3000(tk_afg3000_id): "Send RST signal to PG" return me.reset(tk_afg3000_id)
def configure_base(tk_afg3000_id,frequency,sync,high_level,low_level): retcode,res=me.set_frequency(tk_afg3000_id,frequency) if retcode==0: return 0,res if sync!="undef": return 0,"unable to set sync. must be undef" retcode,res=me.set_high_level(tk_afg3000_id,high_level) if retcode==0: return 0,res retcode,res=me.set_low_level(tk_afg3000_id,low_level) if retcode==0: return 0,res return 1,"ok"
[docs]def configure_sine_tk_afg3000(tk_afg3000_id,frequency,sync,high_level,low_level,phase): "Configure tk_afg3000 to a sine wave with the specified parameters" retcode,res=me.set_function(tk_afg3000_id,"sine") if retcode==0: return 0,res retcode,res=configure_base(tk_afg3000_id,frequency,sync,high_level,low_level) if retcode==0: return 0,res retcode,res=me.set_phase(tk_afg3000_id,phase) if retcode==0: return 0,res return 1,"ok"
[docs]def configure_square_tk_afg3000(tk_afg3000_id,frequency,sync,high_level,low_level,phase,duty_cycle): "Configure tk_afg3000 to a square wave with the specified parameters" retcode,res=me.set_function(tk_afg3000_id,"square") if retcode==0: return 0,res retcode,res=configure_base(tk_afg3000_id,frequency,sync,high_level,low_level) if retcode==0: return 0,res retcode,res=me.set_phase(tk_afg3000_id,phase) if retcode==0: return 0,res if duty_cycle!="undef": return 0,"unable to set duty cycle. must be undef" return 1,"ok"
[docs]def configure_ramp_tk_afg3000(tk_afg3000_id,frequency,sync,high_level,low_level,phase,symmetry): "Configure tk_afg3000 to a ramp wave with the specified parameters" retcode,res=me.set_function(tk_afg3000_id,"ramp") if retcode==0: return 0,res retcode,res=configure_base(tk_afg3000_id,frequency,sync,high_level,low_level) if retcode==0: return 0,res retcode,res=me.set_phase(tk_afg3000_id,phase) if retcode==0: return 0,res retcode,res=me.set_symmetry(tk_afg3000_id,symmetry) if retcode==0: return 0,res return 1,"ok"
[docs]def configure_pulse_tk_afg3000(tk_afg3000_id,frequency,sync,high_level,low_level,delay,pulse_width,rising_edge,falling_edge): "Configure tk_afg3000 to a pulse train with the specified parameters" retcode,res=me.set_function(tk_afg3000_id,"pulse") if retcode==0: return 0,res retcode,res=configure_base(tk_afg3000_id,frequency,sync,high_level,low_level) if retcode==0: return 0,res retcode,res=me.set_delay(tk_afg3000_id,delay) if retcode==0: return 0,res retcode,res=me.set_pulse_width(tk_afg3000_id,pulse_width) if retcode==0: return 0,res retcode,res=me.set_edges(tk_afg3000_id,rising_edge,falling_edge) if retcode==0: return 0,res return 1,"ok"
[docs]def power_on_tk_afg3000(tk_afg3000_id): "Turn on" return me.power_on(tk_afg3000_id)
[docs]def power_off_tk_afg3000(tk_afg3000_id): "Turn off" return me.power_off(tk_afg3000_id)