예제 #1
0
    def __init__(self, sysIn, Name, Tab=None, systemInput=None):
        self.sys = sysIn
        self.Name = Name
        self.Tab = Tab
        self.systemInput = systemInput
        # Generate LaTeX of tf:
        Ys, Xs = control.tfdata(self.sys)
        Ys, Xs = Ys[0][0], Xs[0][0]
        Gs = "Eq(G(s),("
        YStr = []
        i = len(Ys) - 1
        while i >= 0:
            if Ys[len(Ys) - i - 1] != 0:
                if i == 0:
                    s = "{}".format(Ys[len(Ys) - i - 1])
                else:
                    s = "{}*s**({})".format(Ys[len(Ys) - i - 1], i)
                YStr.append(s)
            i -= 1
        Gs += "+".join(YStr)
        Gs += ")/("
        XStr = []
        i = len(Xs) - 1
        while i >= 0:
            if Xs[len(Xs) - i - 1] != 0:
                if i == 0:
                    s = "{}".format(Xs[len(Xs) - i - 1])
                else:
                    s = "{}*s**({})".format(Xs[len(Xs) - i - 1], i)
                XStr.append(s)
            i -= 1
        Gs += "+".join(XStr)
        Gs += "))"
        Gs = AF.number_shaver(Gs)
        Sys_Gs = parse_expr(Gs, evaluate=False)
        Sys_Gs_LaTeX = sympy.latex(Sys_Gs)
        self.Sys_Gs_LaTeX_L = r"$\displaystyle "
        self.Sys_Gs_LaTeX_N = "$"
        self.Sys_Gs_LaTeX_L += Sys_Gs_LaTeX
        self.Sys_Gs_LaTeX_N += Sys_Gs_LaTeX
        self.Sys_Gs_LaTeX_L += "$"
        self.Sys_Gs_LaTeX_N += "$"

        # Generate LaTeX of ss:
        try:
            A, B, C, D = control.ssdata(self.sys)
            self.Order = A.shape[0]
            x_vec = []
            x_vec_diff = []
            i = 1
            while i <= self.Order:
                x_vec.append("x_{}(t)".format(i))
                x_vec_diff.append("diff(x_{}(t),t)".format(i))
                i += 1
            x_vec = str(sympy.Matrix(x_vec))
            x_vec_diff = str(sympy.Matrix(x_vec_diff))
            A, B = AF.number_shaver(str(sympy.Matrix(A))), AF.number_shaver(
                str(sympy.Matrix(B)))
            C, D = AF.number_shaver(str(sympy.Matrix(C))), AF.number_shaver(
                str(sympy.Matrix(D)))
            self.SSx_LaTeX = AF.LaTeX("Eq(" + x_vec_diff + "," + A + "*" +
                                      x_vec + "+" + B + "*u(t))")
            self.SSy_LaTeX = AF.LaTeX("Eq(y(t)," + C + "*" + x_vec + "+" + D +
                                      "*u(t))")
            self.Sys_SS_LaTeX_L = r"$\displaystyle " + self.SSx_LaTeX + "$\n" + r"$\displaystyle " + self.SSy_LaTeX + "$"
            self.Sys_SS_LaTeX_N = "$" + self.SSx_LaTeX + "$\n$" + self.SSy_LaTeX + "$"
        except common_exceptions:
            NC(lvl=2,
               msg="Could not create LaTeX for state space",
               exc=sys.exc_info(),
               input=self.sys,
               func="SystemClass.__init__",
               win="System Control Window")
            self.SSx_LaTeX = "ERROR"
            self.SSy_LaTeX = "ERROR"
            self.Sys_SS_LaTeX_L = "ERROR"
            self.Sys_SS_LaTeX_N = "ERROR"

        # Combine LaTeX of ss and tf:
        self.CheckStability()
        try:
            self.Sys_LaTeX_L = "System: ${}$ \nBIBO-Stable: ${}$\nTransfer Function:\n".format(
                AF.LaTeX(AF.AstusParse(self.Name)), self.BIBOStabel
            ) + self.Sys_Gs_LaTeX_L + "\nState Space:\n" + self.Sys_SS_LaTeX_L
            self.Sys_LaTeX_N = "System: ${}$ \nBIBO-Stable: ${}$\nTransfer Function:\n".format(
                AF.LaTeX(AF.AstusParse(self.Name)), self.BIBOStabel
            ) + self.Sys_Gs_LaTeX_N + "\nState Space:\n" + self.Sys_SS_LaTeX_N
        except common_exceptions:
            NC(1,
               "Invalid Name (Could not convert name to LaTeX)",
               exc=sys.exc_info(),
               func="SystemClass.__init__",
               input=self.Name,
               win="System Control Window")
            raise Exception("Invalid Name (Could not convert name to LaTeX)")
예제 #2
0
    def ControlSystems_4_Dirty_Display(self):
        if not QtWidgets.QApplication.instance().advanced_mode:
            NC(3,
               "This is the \"danger zone\"!\nPlease activate Advanced Mode to confirm that you know what you are doing!",
               func="AMaDiA_Control_Window.ControlSystems_4_Dirty_Display",
               win=str(self.windowTitle()),
               input="Advanced Mode: {}".format(
                   str(QtWidgets.QApplication.instance().advanced_mode)))
        else:
            self.ControlSystems_tabWidget.setCurrentIndex(1)
            input_text = "from External_Libraries.python_control_master.control import * \nglobal sys1\nglobal u\nu=\"\"\n" + self.ControlSystems_4_Dirty_Input.toPlainText(
            )
            #K_D,K_P,K_i = 0,1,0
            try:
                g, l = dict(), dict()
                exec(input_text, g, l)
                print(g["sys1"])
                self.ControlSystems_2_Display.Display(g["sys1"], Ufunc=g["u"])
                self.ControlSystems_tabWidget.setFocus()
                self.ControlSystems_3_SingleDisplay.clear()
                # Generate LaTeX of tf:
                sys1 = g["sys1"]
                Ys, Xs = control.tfdata(sys1)
                Ys, Xs = Ys[0][0], Xs[0][0]
                Gs = "Eq(G(s),("
                YStr = []
                i = len(Ys) - 1
                while i >= 0:
                    if Ys[len(Ys) - i - 1] != 0:
                        if i == 0:
                            s = "{}".format(Ys[len(Ys) - i - 1])
                        else:
                            s = "{}*s**({})".format(Ys[len(Ys) - i - 1], i)
                        YStr.append(s)
                    i -= 1
                Gs += "+".join(YStr)
                Gs += ")/("
                XStr = []
                i = len(Xs) - 1
                while i >= 0:
                    if Xs[len(Xs) - i - 1] != 0:
                        if i == 0:
                            s = "{}".format(Xs[len(Xs) - i - 1])
                        else:
                            s = "{}*s**({})".format(Xs[len(Xs) - i - 1], i)
                        XStr.append(s)
                    i -= 1
                Gs += "+".join(XStr)
                Gs += "))"
                Gs = AF.number_shaver(Gs)
                Sys_Gs = parse_expr(Gs, evaluate=False)
                Sys_Gs_LaTeX = sympy.latex(Sys_Gs)
                Sys_Gs_LaTeX_L = r"$\displaystyle "
                Sys_Gs_LaTeX_N = "$"
                Sys_Gs_LaTeX_L += Sys_Gs_LaTeX
                Sys_Gs_LaTeX_N += Sys_Gs_LaTeX
                Sys_Gs_LaTeX_L += "$"
                Sys_Gs_LaTeX_N += "$"

                # Generate LaTeX of ss:
                A, B, C, D = control.ssdata(sys1)
                Order = A.shape[0]
                x_vec = []
                x_vec_diff = []
                i = 1
                while i <= Order:
                    x_vec.append("x_{}(t)".format(i))
                    x_vec_diff.append("diff(x_{}(t),t)".format(i))
                    i += 1
                x_vec = str(sympy.Matrix(x_vec))
                x_vec_diff = str(sympy.Matrix(x_vec_diff))
                A, B = AF.number_shaver(str(
                    sympy.Matrix(A))), AF.number_shaver(str(sympy.Matrix(B)))
                C, D = AF.number_shaver(str(
                    sympy.Matrix(C))), AF.number_shaver(str(sympy.Matrix(D)))
                SSx_LaTeX = AF.LaTeX("Eq(" + x_vec_diff + "," + A + "*" +
                                     x_vec + "+" + B + "*u(t))")
                SSy_LaTeX = AF.LaTeX("Eq(y(t)," + C + "*" + x_vec + "+" + D +
                                     "*u(t))")
                Sys_SS_LaTeX_L = r"$\displaystyle " + SSx_LaTeX + "$\n" + r"$\displaystyle " + SSy_LaTeX + "$"
                Sys_SS_LaTeX_N = "$" + SSx_LaTeX + "$\n$" + SSy_LaTeX + "$"

                # Display LaTeX:
                Sys_LaTeX_L = "From Code Input:\nTransfer Function:\n" + Sys_Gs_LaTeX_L + "\nState Space:\n" + Sys_SS_LaTeX_L
                Sys_LaTeX_N = "From Code Input:\nTransfer Function:\n" + Sys_Gs_LaTeX_N + "\nState Space:\n" + Sys_SS_LaTeX_N
                self.ControlSystems_1_Output_2L_LaTeXDisplay.DisplayRaw(
                    Sys_LaTeX_L, Sys_LaTeX_N,
                    self.TopBar.Font_Size_spinBox.value(),
                    QtWidgets.QApplication.instance().MainWindow.
                    Menu_Options_action_Use_Pretty_LaTeX_Display.isChecked())
            except common_exceptions:
                NC(1,
                   "Could not execute code to generate the system",
                   exc=sys.exc_info(),
                   func="AMaDiA_Control_Window.ControlSystems_4_Dirty_Display",
                   win=self.windowTitle(),
                   input=input_text)
                self.ControlSystems_tabWidget.setCurrentIndex(3)