Beispiel #1
0
    def test_dcgain_2(self):
        """Test function dcgain with different systems"""
        #Create different forms of a SISO system
        A, B, C, D = self.make_SISO_mats()
        num, den = scipy.signal.ss2tf(A, B, C, D)
        # numerator is only a constant here; pick it out to avoid numpy warning
        Z, P, k = scipy.signal.tf2zpk(num[0][-1], den)
        sys_ss = ss(A, B, C, D)

        #Compute the gain with ``dcgain``
        gain_abcd = dcgain(A, B, C, D)
        gain_zpk = dcgain(Z, P, k)
        gain_numden = dcgain(np.squeeze(num), den)
        gain_sys_ss = dcgain(sys_ss)
        # print('gain_abcd:', gain_abcd, 'gain_zpk:', gain_zpk)
        # print('gain_numden:', gain_numden, 'gain_sys_ss:', gain_sys_ss)

        #Compute the gain with a long simulation
        t = linspace(0, 1000, 1000)
        y, _t = step(sys_ss, t)
        gain_sim = y[-1]
        # print('gain_sim:', gain_sim)

        #All gain values must be approximately equal to the known gain
        assert_array_almost_equal(
            [gain_abcd, gain_zpk, gain_numden, gain_sys_ss, gain_sim],
            [0.026948, 0.026948, 0.026948, 0.026948, 0.026948],
            decimal=6)
    def testDcgain(self, siso):
        """Test dcgain() for SISO system"""
        # Create different forms of a SISO system using scipy.signal
        A, B, C, D = siso.ss1.A, siso.ss1.B, siso.ss1.C, siso.ss1.D
        Z, P, k = sp.signal.ss2zpk(A, B, C, D)
        num, den = sp.signal.ss2tf(A, B, C, D)
        sys_ss = siso.ss1

        # Compute the gain with ``dcgain``
        gain_abcd = dcgain(A, B, C, D)
        gain_zpk = dcgain(Z, P, k)
        gain_numden = dcgain(np.squeeze(num), den)
        gain_sys_ss = dcgain(sys_ss)
        # print('\ngain_abcd:', gain_abcd, 'gain_zpk:', gain_zpk)
        # print('gain_numden:', gain_numden, 'gain_sys_ss:', gain_sys_ss)

        # Compute the gain with a long simulation
        t = linspace(0, 1000, 1000)
        y, _t = step(sys_ss, t)
        gain_sim = y[-1]
        # print('gain_sim:', gain_sim)

        # All gain values must be approximately equal to the known gain
        np.testing.assert_array_almost_equal(
            [gain_abcd, gain_zpk, gain_numden, gain_sys_ss, gain_sim],
            [59, 59, 59, 59, 59])
    def test_dcgain_2(self):
        """Test function dcgain with different systems"""
        #Create different forms of a SISO system
        A, B, C, D = self.make_SISO_mats()
        Z, P, k = scipy.signal.ss2zpk(A, B, C, D)
        num, den = scipy.signal.ss2tf(A, B, C, D)
        sys_ss = ss(A, B, C, D)

        #Compute the gain with ``dcgain``
        gain_abcd = dcgain(A, B, C, D)
        gain_zpk = dcgain(Z, P, k)
        gain_numden = dcgain(np.squeeze(num), den)
        gain_sys_ss = dcgain(sys_ss)
        print('gain_abcd:', gain_abcd, 'gain_zpk:', gain_zpk)
        print('gain_numden:', gain_numden, 'gain_sys_ss:', gain_sys_ss)

        #Compute the gain with a long simulation
        t = linspace(0, 1000, 1000)
        y, _t = step(sys_ss, t)
        gain_sim = y[-1]
        print('gain_sim:', gain_sim)

        #All gain values must be approximately equal to the known gain
        assert_array_almost_equal([gain_abcd[0,0],   gain_zpk[0,0],
                                   gain_numden[0,0], gain_sys_ss[0,0], gain_sim],
                                  [0.026948, 0.026948, 0.026948, 0.026948,
                                   0.026948],
                                  decimal=6)

        #Test with MIMO system
        A, B, C, D = self.make_MIMO_mats()
        gain_mimo = dcgain(A, B, C, D)
        print('gain_mimo: \n', gain_mimo)
        assert_array_almost_equal(gain_mimo, [[0.026948, 0       ],
                                              [0,        0.026948]], decimal=6)
Beispiel #4
0
    def test_dcgain(self):
        """Test function dcgain with different systems"""
        if slycot_check():
            #Test MIMO systems
            A, B, C, D = self.make_MIMO_mats()

            gain1 = dcgain(ss(A, B, C, D))
            gain2 = dcgain(A, B, C, D)
            sys_tf = ss2tf(A, B, C, D)
            gain3 = dcgain(sys_tf)
            gain4 = dcgain(sys_tf.num, sys_tf.den)
            #print("gain1:", gain1)

            assert_array_almost_equal(gain1,
                                      array([[0.0269, 0.], [0., 0.0269]]),
                                      decimal=4)
            assert_array_almost_equal(gain1, gain2)
            assert_array_almost_equal(gain3, gain4)
            assert_array_almost_equal(gain1, gain4)

        #Test SISO systems
        A, B, C, D = self.make_SISO_mats()

        gain1 = dcgain(ss(A, B, C, D))
        assert_array_almost_equal(gain1, array([[0.0269]]), decimal=4)
def test_dcgain_2():
    """Test function dcgain with different systems"""
    #Create different forms of a SISO system
    A, B, C, D = make_SISO_mats()
    Z, P, k = scipy.signal.ss2zpk(A, B, C, D)
    num, den = scipy.signal.ss2tf(A, B, C, D)
    sys_ss = ss(A, B, C, D)

    #Compute the gain with ``dcgain``
    gain_abcd = dcgain(A, B, C, D)
    gain_zpk = dcgain(Z, P, k)
    gain_numden = dcgain(np.squeeze(num), den)
    gain_sys_ss = dcgain(sys_ss)
    print 'gain_abcd:', gain_abcd, 'gain_zpk:', gain_zpk
    print 'gain_numden:', gain_numden, 'gain_sys_ss:', gain_sys_ss

    #Compute the gain with a long simulation
    t = linspace(0, 1000, 1000)
    _t, y = step(sys_ss, t)
    gain_sim = y[-1]
    print 'gain_sim:', gain_sim

    #All gain values must be approximately equal to the known gain
    assert_array_almost_equal([
        gain_abcd[0, 0], gain_zpk[0, 0], gain_numden[0, 0], gain_sys_ss[0, 0],
        gain_sim
    ], [0.026948, 0.026948, 0.026948, 0.026948, 0.026948],
                              decimal=6)

    #Test with MIMO system
    A, B, C, D = make_MIMO_mats()
    gain_mimo = dcgain(A, B, C, D)
    print 'gain_mimo: \n', gain_mimo
    assert_array_almost_equal(gain_mimo, [[0.026948, 0], [0, 0.026948]],
                              decimal=6)
    def test_dcgain(self):
        """Test function dcgain with different systems"""
        #Test MIMO systems
        A, B, C, D = self.make_MIMO_mats()

        gain1 = dcgain(ss(A, B, C, D))
        gain2 = dcgain(A, B, C, D)
        sys_tf = ss2tf(A, B, C, D)
        gain3 = dcgain(sys_tf)
        gain4 = dcgain(sys_tf.num, sys_tf.den)
        #print("gain1:", gain1)

        assert_array_almost_equal(gain1,
                                  array([[0.0269, 0.    ],
                                         [0.    , 0.0269]]),
                                  decimal=4)
        assert_array_almost_equal(gain1, gain2)
        assert_array_almost_equal(gain3, gain4)
        assert_array_almost_equal(gain1, gain4)

        #Test SISO systems
        A, B, C, D = self.make_SISO_mats()

        gain1 = dcgain(ss(A, B, C, D))
        assert_array_almost_equal(gain1,
                                  array([[0.0269]]),
                                  decimal=4)
    def test_dcgain_2(self):
        """Test function dcgain with different systems"""
        #Create different forms of a SISO system
        A, B, C, D = self.make_SISO_mats()
        num, den = scipy.signal.ss2tf(A, B, C, D)
        # numerator is only a constant here; pick it out to avoid numpy warning
        Z, P, k = scipy.signal.tf2zpk(num[0][-1], den)
        sys_ss = ss(A, B, C, D)

        #Compute the gain with ``dcgain``
        gain_abcd = dcgain(A, B, C, D)
        gain_zpk = dcgain(Z, P, k)
        gain_numden = dcgain(np.squeeze(num), den)
        gain_sys_ss = dcgain(sys_ss)
        # print('gain_abcd:', gain_abcd, 'gain_zpk:', gain_zpk)
        # print('gain_numden:', gain_numden, 'gain_sys_ss:', gain_sys_ss)

        #Compute the gain with a long simulation
        t = linspace(0, 1000, 1000)
        y, _t = step(sys_ss, t)
        gain_sim = y[-1]
        # print('gain_sim:', gain_sim)

        #All gain values must be approximately equal to the known gain
        assert_array_almost_equal([gain_abcd[0,0],   gain_zpk[0,0],
                                   gain_numden[0,0], gain_sys_ss[0,0], gain_sim],
                                  [0.026948, 0.026948, 0.026948, 0.026948,
                                   0.026948],
                                  decimal=6)
    def test_matlab_wrapper_exceptions(self):
        """Test out exceptions in matlab/wrappers.py"""
        sys = tf([1], [1, 2, 1])

        # Extra arguments in bode
        with pytest.raises(ControlArgument, match="not all arguments"):
            bode(sys, 'r-', [1e-2, 1e2], 5.0)

        # Multiple plot styles
        with pytest.warns(UserWarning, match="plot styles not implemented"):
            bode(sys, 'r-', sys, 'b--', [1e-2, 1e2])

        # Incorrect number of arguments to dcgain
        with pytest.raises(ValueError, match="needs either 1, 2, 3 or 4"):
            dcgain(1, 2, 3, 4, 5)
Beispiel #9
0
    def assert_systems_behave_equal(self, sys1, sys2):
        '''
        Test if the behavior of two LTI systems is equal. Raises ``AssertionError``
        if the systems are not equal.

        Works only for SISO systems.

        Currently computes dcgain, and computes step response.
        '''
        #gain of both systems must be the same
        assert_array_almost_equal(dcgain(sys1), dcgain(sys2))

        #Results of ``step`` simulation must be the same too
        y1, t1 = step(sys1)
        y2, t2 = step(sys2, t1)
        assert_array_almost_equal(y1, y2)
    def assert_systems_behave_equal(self, sys1, sys2):
        '''
        Test if the behavior of two Lti systems is equal. Raises ``AssertionError``
        if the systems are not equal.

        Works only for SISO systems.

        Currently computes dcgain, and computes step response.
        '''
        #gain of both systems must be the same
        assert_array_almost_equal(dcgain(sys1), dcgain(sys2))

        #Results of ``step`` simulation must be the same too
        t, y1 = step(sys1)
        _t, y2 = step(sys2, t)
        assert_array_almost_equal(y1, y2)
Beispiel #11
0
def pidplot(num, den, Kp, Ki, Kd, desired_settle=1.):
    '''
    Plot system step response when open loop system is subjected to feedback PID compensation.
    Also plots 2% settling lines, and a vertical line at a desired settling time.
    y, t =pidplot(num,den,Kp,Ki,Kd,desired_settle=1.)
    Parameters:
    :param num: [array-like], coefficients of numerator of open loop transfer function
    :param den: [array-like], coefficients of denominator of open loop transfer function
    :param Kp: [float] Proportional gain
    :param Ki: [float] Integral gain
    :param Kd: [float] Derivative gain
    :param desired_settle: [float] Desired settling time, for tuning PID controllers, default = 1.0
    Returns:
    :return: y: [array-like] time step response
    :return: t: [array-like] time vector
    '''
    numc = [Kd, Kp, Ki]
    denc = [0, 1, 0]

    numcg = convolve(numc, num)
    dencg = convolve(denc, den)

    Gfb = feedback(tf(numcg, dencg), 1)
    y, t = step(Gfb)
    yss = dcgain(Gfb)
    plot(t, y, 'r')
    plot(t, 1.02 * yss * ones(len(t)), 'k--')
    plot(t, 0.98 * yss * ones(len(t)), 'k--')
    plot(desired_settle * ones(15), linspace(0, yss + 0.25, 15), 'b-.')
    xlim(0)
    xlabel('Time [s]')
    ylabel('Magnitude')
    grid()
    show()
    return y, t
Beispiel #12
0
    def test_dcgain_siso(self, SISO_mats):
        """Test function dcgain with SISO systems"""
        A, B, C, D = SISO_mats

        gain1 = dcgain(ss(A, B, C, D))
        assert_array_almost_equal(gain1,
                                  array([[0.0269]]),
                                  decimal=4)
Beispiel #13
0
    def test_dcgain_mimo(self, MIMO_mats):
        """Test function dcgain with MIMO systems"""
        #Test MIMO systems
        A, B, C, D = MIMO_mats

        gain1 = dcgain(ss(A, B, C, D))
        gain2 = dcgain(A, B, C, D)
        sys_tf = ss2tf(A, B, C, D)
        gain3 = dcgain(sys_tf)
        gain4 = dcgain(sys_tf.num, sys_tf.den)
        #print("gain1:", gain1)

        assert_array_almost_equal(gain1,
                                  array([[0.0269, 0.], [0., 0.0269]]),
                                  decimal=4)
        assert_array_almost_equal(gain1, gain2)
        assert_array_almost_equal(gain3, gain4)
        assert_array_almost_equal(gain1, gain4)
 def testDcgain_mimo(self, mimo):
     """Test dcgain() for MIMO system"""
     gain_mimo = dcgain(mimo.ss1)
     # print('gain_mimo: \n', gain_mimo)
     np.testing.assert_array_almost_equal(gain_mimo, [[59., 0],
                                                      [0, 59.]])