pid_only.py 9.4 KB
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
##################################################
# GNU Radio Python Flow Graph
# Title: Pid Only
# Generated: Fri Mar 31 15:40:54 2017
##################################################

if __name__ == '__main__':
    import ctypes
    import sys
    if sys.platform.startswith('linux'):
        try:
            x11 = ctypes.cdll.LoadLibrary('libX11.so')
            x11.XInitThreads()
        except:
            print "Warning: failed to XInitThreads()"

from PyQt4 import Qt
from gnuradio import eng_notation
from gnuradio import gr
from gnuradio.eng_option import eng_option
from gnuradio.filter import firdes
from gnuradio.qtgui import Range, RangeWidget
from optparse import OptionParser
import redpitaya_gnuradio
import sys


class pid_only(gr.top_block, Qt.QWidget):

    def __init__(self):
        gr.top_block.__init__(self, "Pid Only")
        Qt.QWidget.__init__(self)
        self.setWindowTitle("Pid Only")
        try:
            self.setWindowIcon(Qt.QIcon.fromTheme('gnuradio-grc'))
        except:
            pass
        self.top_scroll_layout = Qt.QVBoxLayout()
        self.setLayout(self.top_scroll_layout)
        self.top_scroll = Qt.QScrollArea()
        self.top_scroll.setFrameStyle(Qt.QFrame.NoFrame)
        self.top_scroll_layout.addWidget(self.top_scroll)
        self.top_scroll.setWidgetResizable(True)
        self.top_widget = Qt.QWidget()
        self.top_scroll.setWidget(self.top_widget)
        self.top_layout = Qt.QVBoxLayout(self.top_widget)
        self.top_grid_layout = Qt.QGridLayout()
        self.top_layout.addLayout(self.top_grid_layout)

        self.settings = Qt.QSettings("GNU Radio", "pid_only")
        self.restoreGeometry(self.settings.value("geometry").toByteArray())

        ##################################################
        # Variables
        ##################################################
        self.port = port = 1001
        self.pid_sp = pid_sp = 0
        self.pid_sign = pid_sign = 0
        self.pid_kp = pid_kp = 0
        self.pid_ki = pid_ki = 0
        self.pid_kd = pid_kd = 0
        self.pid_int_rst = pid_int_rst = 0
        self.dac1_offset = dac1_offset = 0
        self.addr = addr = "192.168.0.202"
        self.adc1_offset = adc1_offset = 0

        ##################################################
        # Blocks
        ##################################################
        self._pid_sp_range = Range(-2**13, 2**13-1, 1, 0, 200)
        self._pid_sp_win = RangeWidget(self._pid_sp_range, self.set_pid_sp, 'pid_sp', "counter_slider", int)
        self.top_grid_layout.addWidget(self._pid_sp_win, 2, 2)
        _pid_sign_check_box = Qt.QCheckBox('pid_sign')
        self._pid_sign_choices = {True: 1, False: 0}
        self._pid_sign_choices_inv = dict((v,k) for k,v in self._pid_sign_choices.iteritems())
        self._pid_sign_callback = lambda i: Qt.QMetaObject.invokeMethod(_pid_sign_check_box, "setChecked", Qt.Q_ARG("bool", self._pid_sign_choices_inv[i]))
        self._pid_sign_callback(self.pid_sign)
        _pid_sign_check_box.stateChanged.connect(lambda i: self.set_pid_sign(self._pid_sign_choices[bool(i)]))
        self.top_grid_layout.addWidget(_pid_sign_check_box, 3, 2)
        self._pid_kp_range = Range(0, 2**13-1, 1, 0, 200)
        self._pid_kp_win = RangeWidget(self._pid_kp_range, self.set_pid_kp, 'pid_kp', "counter_slider", int)
        self.top_grid_layout.addWidget(self._pid_kp_win, 2, 1)
        self._pid_ki_range = Range(0, 2**13-1, 1, 0, 200)
        self._pid_ki_win = RangeWidget(self._pid_ki_range, self.set_pid_ki, 'pid_ki', "counter_slider", int)
        self.top_grid_layout.addWidget(self._pid_ki_win, 3, 1)
        self._pid_kd_range = Range(0, 2**13-1, 1, 0, 200)
        self._pid_kd_win = RangeWidget(self._pid_kd_range, self.set_pid_kd, 'pid_kd', "counter_slider", int)
        self.top_grid_layout.addWidget(self._pid_kd_win, 4, 1)
        _pid_int_rst_push_button = Qt.QPushButton('pid_int_rst')
        self._pid_int_rst_choices = {'Pressed': 1, 'Released': 0}
        _pid_int_rst_push_button.pressed.connect(lambda: self.set_pid_int_rst(self._pid_int_rst_choices['Pressed']))
        _pid_int_rst_push_button.released.connect(lambda: self.set_pid_int_rst(self._pid_int_rst_choices['Released']))
        self.top_grid_layout.addWidget(_pid_int_rst_push_button, 4, 2)
        self._dac1_offset_range = Range(-2**13, 2**13-1, 1, 0, 200)
        self._dac1_offset_win = RangeWidget(self._dac1_offset_range, self.set_dac1_offset, 'dac1_offset', "counter_slider", int)
        self.top_grid_layout.addWidget(self._dac1_offset_win, 1, 2)
        self._adc1_offset_range = Range(-2**13, 2**13-1, 1, 0, 200)
        self._adc1_offset_win = RangeWidget(self._adc1_offset_range, self.set_adc1_offset, 'adc1_offset', "counter_slider", int)
        self.top_grid_layout.addWidget(self._adc1_offset_win, 1, 1)
        self.add_const_set_offset_0_0_0_1_0_0_0_0_0 = redpitaya_gnuradio.add_const_set_offset(
                addr=addr,
                port=port,
                device='/dev/pid_int_rst',
                const=pid_int_rst,
        )
          
        self.add_const_set_offset_0_0_0_1_0_0_0_0 = redpitaya_gnuradio.add_const_set_offset(
                addr=addr,
                port=port,
                device='/dev/pid_sign',
                const=pid_sign,
        )
          
        self.add_const_set_offset_0_0_0_1_0_0_0 = redpitaya_gnuradio.add_const_set_offset(
                addr=addr,
                port=port,
                device='/dev/pid_kd',
                const=pid_kd,
        )
          
        self.add_const_set_offset_0_0_0_1_0_0 = redpitaya_gnuradio.add_const_set_offset(
                addr=addr,
                port=port,
                device='/dev/pid_ki',
                const=pid_ki,
        )
          
        self.add_const_set_offset_0_0_0_1_0 = redpitaya_gnuradio.add_const_set_offset(
                addr=addr,
                port=port,
                device='/dev/pid_kp',
                const=pid_kp,
        )
          
        self.add_const_set_offset_0_0_0_1 = redpitaya_gnuradio.add_const_set_offset(
                addr=addr,
                port=port,
                device='/dev/pid_sp',
                const=pid_sp,
        )
          
        self.add_const_set_offset_0_0_0_0 = redpitaya_gnuradio.add_const_set_offset(
                addr=addr,
                port=port,
                device='/dev/dac1_offset',
                const=dac1_offset,
        )
          
        self.add_const_set_offset_0_0_0 = redpitaya_gnuradio.add_const_set_offset(
                addr=addr,
                port=port,
                device='/dev/adc1_offset',
                const=adc1_offset,
        )
          

    def closeEvent(self, event):
        self.settings = Qt.QSettings("GNU Radio", "pid_only")
        self.settings.setValue("geometry", self.saveGeometry())
        event.accept()

    def get_port(self):
        return self.port

    def set_port(self, port):
        self.port = port

    def get_pid_sp(self):
        return self.pid_sp

    def set_pid_sp(self, pid_sp):
        self.pid_sp = pid_sp
        self.add_const_set_offset_0_0_0_1.set_const(device='/dev/pid_sp', const=self.pid_sp)

    def get_pid_sign(self):
        return self.pid_sign

    def set_pid_sign(self, pid_sign):
        self.pid_sign = pid_sign
        self._pid_sign_callback(self.pid_sign)
        self.add_const_set_offset_0_0_0_1_0_0_0_0.set_const(device='/dev/pid_sign', const=self.pid_sign)

    def get_pid_kp(self):
        return self.pid_kp

    def set_pid_kp(self, pid_kp):
        self.pid_kp = pid_kp
        self.add_const_set_offset_0_0_0_1_0.set_const(device='/dev/pid_kp', const=self.pid_kp)

    def get_pid_ki(self):
        return self.pid_ki

    def set_pid_ki(self, pid_ki):
        self.pid_ki = pid_ki
        self.add_const_set_offset_0_0_0_1_0_0.set_const(device='/dev/pid_ki', const=self.pid_ki)

    def get_pid_kd(self):
        return self.pid_kd

    def set_pid_kd(self, pid_kd):
        self.pid_kd = pid_kd
        self.add_const_set_offset_0_0_0_1_0_0_0.set_const(device='/dev/pid_kd', const=self.pid_kd)

    def get_pid_int_rst(self):
        return self.pid_int_rst

    def set_pid_int_rst(self, pid_int_rst):
        self.pid_int_rst = pid_int_rst
        self.add_const_set_offset_0_0_0_1_0_0_0_0_0.set_const(device='/dev/pid_int_rst', const=self.pid_int_rst)

    def get_dac1_offset(self):
        return self.dac1_offset

    def set_dac1_offset(self, dac1_offset):
        self.dac1_offset = dac1_offset
        self.add_const_set_offset_0_0_0_0.set_const(device='/dev/dac1_offset', const=self.dac1_offset)

    def get_addr(self):
        return self.addr

    def set_addr(self, addr):
        self.addr = addr

    def get_adc1_offset(self):
        return self.adc1_offset

    def set_adc1_offset(self, adc1_offset):
        self.adc1_offset = adc1_offset
        self.add_const_set_offset_0_0_0.set_const(device='/dev/adc1_offset', const=self.adc1_offset)


def main(top_block_cls=pid_only, options=None):

    from distutils.version import StrictVersion
    if StrictVersion(Qt.qVersion()) >= StrictVersion("4.5.0"):
        style = gr.prefs().get_string('qtgui', 'style', 'raster')
        Qt.QApplication.setGraphicsSystem(style)
    qapp = Qt.QApplication(sys.argv)

    tb = top_block_cls()
    tb.start()
    tb.show()

    def quitting():
        tb.stop()
        tb.wait()
    qapp.connect(qapp, Qt.SIGNAL("aboutToQuit()"), quitting)
    qapp.exec_()


if __name__ == '__main__':
    main()