Example #1
0
    def test_import_export(self):

        sys = ctrl.tf([1], [100, 1])
        sim_duration = 2000
        time = np.zeros(sim_duration)
        i = 0
        while (i < sim_duration):
            time[i] = i
            i += 1
        _, output = ctrl.step_response(sys, time)
        m = ps.FsrModel(output, t=time)
        # testing our model with a test signal
        test_sgnl_len = int(2500)
        u = np.zeros(test_sgnl_len)
        for i in range(test_sgnl_len):
            u[i] = 1
        time = np.zeros(test_sgnl_len)
        for i in range(test_sgnl_len):
            time[i] = i
        _, comp, _ = ctrl.forced_response(sys, time, u)
        # 'rb' and 'wb' to avoid problems under Windows!
        with open("temp", "w") as fh:
            ps.export_csv(m, fh)
        with open("temp", "r") as fh:
            m = ps.import_csv(fh)
        os.remove("temp")
        _, y = ps.forced_response(m, time, u)
        self.assertTrue(np.allclose(y, comp, rtol=1e-2), "import/export broke")
Example #2
0
def creation_example():
    """Example for creation of a FSR model with python-control."""

    # creating a step response for our model
    sys = ctrl.tf([1], [100, 1])
    sim_duration = 2000
    time = np.arange(sim_duration)
    _, output = ctrl.step_response(sys, time)
    # creating the model
    model = ps.FsrModel(output, t=time)
    # creating a test input signal
    u = np.ones(sim_duration)
    for i in range(len(u)):
        if i < 500:
            u[i] = 0.001 * i
    # getting response from our model
    t1, y1 = ps.forced_response(model, time, u)
    # simulating a reference with control
    t2, y2, _ = ctrl.forced_response(sys, time, u)
    # show everything
    ax = plt.subplot(111)
    plt.plot(t1, y1, label="Model response")
    plt.plot(t2, y2, label="Reference response")
    plt.plot(t1, u, label="Input signal")
    ax.legend()
    plt.title("pystrem example")
    plt.show()
Example #3
0
    def test_IT2_forced_response(self):

        sys = ctrl.tf([100], [50, 1000, 150, 0])  # IT2
        _, o = ctrl.step_response(sys, self.time)
        m = ps.FsrModel(o, t=self.time, optimize=False)
        _, y = ps.forced_response(m, self.t, self.u)
        _, o, _ = ctrl.forced_response(sys, self.t, self.u)
        self.assertTrue(np.allclose(y, o, rtol=1e-2), "it2 response broke")
Example #4
0
    def test_PT2_forced_response(self):

        sys = ctrl.tf([1], [1000, 10, 1])  # PT2 with overshooting
        _, o = ctrl.step_response(sys, self.time)
        m = ps.FsrModel(o, t=self.time)
        _, y = ps.forced_response(m, self.t, self.u)
        _, o, _ = ctrl.forced_response(sys, self.t, self.u)
        self.assertTrue(np.allclose(y, o, rtol=1e-2), "pt2 response broke")
Example #5
0
    def setUpClass(cls):

        # See https://stackoverflow.com/questions/14044474/python-unittest-setupclass-is-giving-me-trouble-why-cant-i-inherit-like-t
        super(OperandTest, cls).setUpClass()
        # Creating our systems is slow, so we do it once at startup.
        cls.sys1 = ctrl.tf([1], [10, 1])
        cls.sys2 = ctrl.tf([2], [10, 10, 1])
        sim_duration = 100
        cls.time = np.zeros(sim_duration)
        i = 0
        while (i < sim_duration):
            cls.time[i] = i
            i += 1
        _, o1 = ctrl.step_response(cls.sys1, cls.time)
        _, o2 = ctrl.step_response(cls.sys2, cls.time)
        cls.m1 = ps.FsrModel(o1, t=cls.time)
        cls.m2 = ps.FsrModel(o2, t=cls.time)
Example #6
0
    def test_all_pass_forced_response(self):

        sys = ctrl.tf([-50, 1], [1000, 10, 1])  # all-pass
        _, o = ctrl.step_response(sys, self.time)
        m = ps.FsrModel(o, t=self.time)
        _, y = ps.forced_response(m, self.t, self.u)
        _, o, _ = ctrl.forced_response(sys, self.t, self.u)
        self.assertTrue(np.allclose(y, o, rtol=1e-1),
                        "all-pass response broke")
Example #7
0
    def test_u_step_find(self):

        sys = ctrl.tf([1], [100, 1])  # PT1
        _, o = ctrl.step_response(sys, self.time)
        u = np.zeros(self.sim_duration)
        for i in range(60, len(u)):
            u[i] = 1
        m = ps.FsrModel(o, t=self.time, u=u)
        u = np.ones(len(m._u))
        self.assertTrue(np.array_equal(u, m._u), "u step finding broke")
Example #8
0
 def test_simulate_step(self):
     sys = ctrl.tf([1], [10, 1])
     time = np.arange(0, 10, 0.1)
     _, o = ctrl.step_response(sys)
     m = ps.FsrModel(o, time)
     time = np.arange(0, 10, 0.1)
     u = np.ones(len(time))
     y1 = []
     for i in range(len(time)):
         y1.append(m.simulate_step(u[i]))
     _, y2 = ps.step_response(m, time)
     self.assertTrue(np.array_equal(y1, y2), "simulate step broke")
Example #9
0
 def test_crazy_system_forced_response(self):
     # numerator: (s+50)(s-2)((s+8)²+45)(s²+100))
     # denominator: (s+2)(s+3)(s+3)(s+4)((s+1.5)²+5)((s+3)²+2)((s+0.5)²+3)
     #              ((s+4)²+30)
     sys = ctrl.tf([1, 64, 877, 10032, 66800, 363200, -1090000], [
         1, 30, 443.5, 4140, 26677.6, 124311, 430648, 1.12577e6, 2.22475e6,
         3.28564e6, 3.49963e6, 2.45298e6, 858429
     ])
     time = np.arange(0, 60, 0.01)
     u = np.ones(len(time))
     for i in range(len(u)):
         if i < 25 / 0.01:
             u[i] = 0.5
     _, o = ctrl.step_response(sys, time)
     m = ps.FsrModel(o, time)
     _, y = ps.forced_response(m, time, u)
     _, o, _ = ctrl.forced_response(sys, time, u)
     self.assertTrue(np.allclose(y, o, rtol=1e-2), "crazy response broke")