3

当我使用 python-control 包创建系统时:

import control
H = control.tf([1], [1])

然后想迭代地模拟那个系统,我该怎么做呢?

我知道我可以这样做:

T = np.arange(0, 10, 0.01)
u = np.sin(T)
y, t, x = control.lsim(H, u, T)

但我想做的是:

Tstart = get_current_time()   # returns a scalar
T = get_current_time()
x = None
while T - Tstart < 100:
    u = get_next_input()      # returns a scalar
    T = get_current_time()
    y, x = control.step_system(H, u, T, x)
    do_something_with_output(y)

有什么办法可以做到这一点吗?您还应该如何使用使用控制包开发的系统来控制某些东西?

4

1 回答 1

1

这是一个很好的问题。我自己对此很感兴趣,不久前在 Mathworks 论坛上问了一个类似的问题,目前在 MATLAB 中是不可能的。

好消息是,您现在可以使用iosys模块和input_output_response函数在 Python Control 中执行此操作。

对于您的示例中的线性系统,您使用LinearIOSystem该类

这是我的模拟示例:

import time
import numpy as np
import matplotlib.pyplot as plt
import control
from control import input_output_response
from control.iosys import LinearIOSystem

# Define system
# Continuous-time transfer function
G = control.tf([1], [2, 1])

# Convert to state-space representation
Gss = control.ss(G)

# Construct IO system
sys = LinearIOSystem(Gss, inputs='u', outputs='y')

def get_next_input(u, avg_time=0.5):
    """Function to simulate data acquisition"""
    t0 = time.time()
    wait_time = avg_time*(0.5 + np.random.rand())
    while time.time() - t0 < wait_time:
        pass
    if np.random.rand() > 0.8:
        u = u + np.random.randn()
    return u

# Simulate system in response to irregular inputs
t0 = time.time()
t = 0
y0 = 0
u = 0
x = np.zeros(sys.nstates)
np.random.seed(1)
sim_results = [[0, u, y0]]
print(sim_results[-1])
while t < 10:
    u_new, t_new  = get_next_input(u), time.time() - t0
    # Simulation of system up to current time
    T_sim = [t, t_new]
    T_sim, Y_sim, X_sim = input_output_response(sys, T_sim, u, X0=x,
                                                return_x=True)
    sim_results.append([T_sim[-1], u_new, Y_sim[-1]])
    print(sim_results[-1])
    # Set current state and outputs to end of simulation period
    x = X_sim[0, -1]
    u = u_new
    t = t_new

sim_results = np.array(sim_results)
t = sim_results[:, 0]
u = sim_results[:, 1]
y = sim_results[:, 2]

# Plot inputs and outputs
plt.subplot(2, 1, 1)
plt.plot(t, y, 'o-')
plt.xlabel('t')
plt.ylabel('y(t)')
plt.grid()
plt.subplot(2, 1, 2)
plt.step(t, u, where='post')
plt.xlabel('t')
plt.ylabel('u(t)')
plt.grid()
plt.show()

模拟系统的输入输出时间响应

回答你的最后一个问题:

你还应该如何使用使用控制包开发的系统来控制某些东西?”

我认为 MATLAB 控制模块和 python-control 等工具旨在用于控制系统的分析、设计和仿真,而不一定用于它们的实现。根据您的应用程序,通常最终的控制系统实现是在专门的硬件和/或软件上完成的,或者可能是用 C 等低级语言手动编码的。可以说 MATLAB 和 Python 等高级语言太不可靠且难以维护/升级,因此它们无法成为任何严肃的过程控制或现实世界机器人应用程序中的有吸引力的解决方案。但是对于业余爱好者和实验室实验来说,它们是理想的,所以我同意这种功能很有用。

于 2021-08-07T15:09:08.137 回答