请设计一个无人机气动力学例程及源程序
时间: 2023-09-25 11:10:07 浏览: 54
以下是一个简单的无人机气动力学例程及源程序,使用Python编写:
## 无人机气动力学例程
import numpy as np
from scipy.integrate import odeint
## 无人机参数
mass = 1.0 # kg
Ixx = 0.05 # kg-m^2
Iyy = 0.05 # kg-m^2
Izz = 0.1 # kg-m^2
S = 0.25 # m^2
b = 1.0 # m
c = 0.25 # m
AR = b**2 / S
e = 0.9
CD0 = 0.05
k = 0.1
CLa = 2*np.pi*AR / (2 + np.sqrt(4 + (AR*0.01)**2 * (1 + np.tan(np.radians(5))**2)))
Cma = -0.05
alpha0 = np.radians(-2)
rho = 1.225 # kg/m^3
g = 9.81 # m/s^2
## 控制输入
def control_input(t):
if t < 10.0:
delta_e = np.radians(2)
delta_t = 0.5
else:
delta_e = np.radians(0)
delta_t = 0.5
return delta_e, delta_t
## 状态方程
def state_equations(state, t, delta_e, delta_t):
u, v, w, p, q, r, phi, theta, psi, x, y, z = state
alpha = np.arctan2(w, u)
beta = np.arcsin(v / np.sqrt(u**2 + v**2 + w**2))
qbar = 0.5 * rho * np.sqrt(u**2 + v**2 + w**2)**2
CL = CLa * (alpha - alpha0)
CD = CD0 + k*CL**2
L = CL*qbar*S
D = CD*qbar*S
T = delta_t*mass*g
fx = T*np.cos(alpha)*np.cos(beta) - D*np.sin(beta) + L*np.cos(beta)*np.sin(alpha)
fy = T*np.sin(beta) + D*np.cos(beta) + L*np.sin(beta)
fz = -T*np.sin(alpha)*np.cos(beta) - D*np.cos(beta)*np.sin(alpha) - L*np.cos(alpha)*np.cos(beta)
m = qbar*S*c*Cma + delta_e*qbar*S*c*(CLa*(alpha - alpha0) - Cma)
u_dot = r*v - q*w - g*np.sin(theta) + fx/mass
v_dot = p*w - r*u + g*np.sin(phi)*np.cos(theta) + fy/mass
w_dot = q*u - p*v + g*np.cos(phi)*np.cos(theta) + fz/mass
p_dot = (Iyy - Izz)*q*r/Ixx + m/Ixx
q_dot = (Izz - Ixx)*p*r/Iyy + (delta_e*qbar*S*c/Iyy)*CLa
r_dot = (Ixx - Iyy)*p*q/Izz + (delta_e*qbar*S*c/Izz)*Cma
phi_dot = p + np.tan(theta)*(q*np.sin(phi) + r*np.cos(phi))
theta_dot = q*np.cos(phi) - r*np.sin(phi)
psi_dot = (q*np.sin(phi) + r*np.cos(phi))/np.cos(theta)
x_dot = u*np.cos(theta)*np.cos(psi) + v*(np.cos(theta)*np.sin(psi))*w*np.sin(theta)
y_dot = u*np.sin(psi) - v*np.cos(psi)
z_dot = u*np.sin(theta)*np.cos(psi) + v*(np.sin(theta)*np.sin(psi))*w*np.cos(theta)
return u_dot, v_dot, w_dot, p_dot, q_dot, r_dot, phi_dot, theta_dot, psi_dot, x_dot, y_dot, z_dot
## 初始状态
state0 = [10.0, 0.0, 0.0, 0.0, 0.0, 0.0, np.radians(0), np.radians(5), np.radians(0), 0.0, 0.0, 100.0]
## 时间范围
t = np.linspace(0, 30, 1000)
## 解ODE方程
delta_e_array, delta_t_array = [], []
for i in range(len(t)):
delta_e, delta_t = control_input(t[i])
delta_e_array.append(delta_e)
delta_t_array.append(delta_t)
state = odeint(state_equations, state0, [t[i-1], t[i]], args=(delta_e, delta_t))
state0 = state[1]
## 绘制图形
import matplotlib.pyplot as plt
plt.subplot(3, 2, 1)
plt.plot(t, [np.degrees(alpha) for alpha in state[:, 7]])
plt.xlabel('Time (s)')
plt.ylabel('Angle of Attack (deg)')
plt.grid(True)
plt.subplot(3, 2, 2)
plt.plot(t, [np.degrees(beta) for beta in state[:, 8]])
plt.xlabel('Time (s)')
plt.ylabel('Sideslip Angle (deg)')
plt.grid(True)
plt.subplot(3, 2, 3)
plt.plot(t, [np.degrees(state[:, 6]) for psi in state[:, 6]])
plt.xlabel('Time (s)')
plt.ylabel('Roll Angle (deg)')
plt.grid(True)
plt.subplot(3, 2, 4)
plt.plot(t, [np.degrees(state[:, 7]) for theta in state[:, 7]])
plt.xlabel('Time (s)')
plt.ylabel('Pitch Angle (deg)')
plt.grid(True)
plt.subplot(3, 2, 5)
plt.plot(t, [np.degrees(state[:, 8]) for phi in state[:, 8]])
plt.xlabel('Time (s)')
plt.ylabel('Yaw Angle (deg)')
plt.grid(True)
plt.subplot(3, 2, 6)
plt.plot(state[:, 9], state[:, 11])
plt.xlabel('x (m)')
plt.ylabel('z (m)')
plt.grid(True)
plt.show()
这个例程模拟了一个无人机的气动力学行为,包括滚转、俯仰、偏航和位置控制。它使用了一些基本的飞行动力学公式,例如Lift和Drag系数、气动力等。在这个例程中,我们使用ODEINT函数求解微分方程,并绘制了无人机的各种状态随时间的变化。