我试图用python类对车辆纵向运动的动力学进行建模,我的模型应该经过的方程在下面的图像链接中得到了澄清:
https://drive.google.com/file/d/1CK75Q5JzkHM3YRQkWGpI6JojXwhrplD3/view?usp=sharing
我根据以下逻辑构建了我的模型:
要使用该模型,应遵循以下步骤:
下面的代码部分简要介绍了该代码:
import sys
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
class Vehicle():
def __init__(self):
# ==================================
# Parameters are defined below but i deleted them to shorten the code
# ==================================
#Throttle to engine torque
# Gear ratio, effective radius, mass + inertia
# Aerodynamic and friction coefficients
# Tire force
# State variables
self.x = 0
self.v = 5
self.a = 0
self.w_e = 100
self.w_e_dot = 0
self.sample_time = 0.01
def reset(self):
# reset state variables
self.x = 0
self.v = 5
self.a = 0
self.w_e = 100
self.w_e_dot = 0
def step(self, throttle, alpha):
# calculate F_x, F_load, and T_e respictively
# F_x calculations
w_w = self.GR * self.w_e
slip = ((w_w * self.r_e) - self.v) / self.v
if (slip < 1 and slip > -1):
f_x = self.c * slip
else:
f_x = self.F_max
# F_load calculations
f_aero = self.c_a * (self.v * self.v)
r_x = self.c_r1 * self.v
f_g = self.m * self.g * np.sin(alpha)
f_load = f_aero + r_x + f_g
# T_e calculations
t_e = throttle * (self.a_0 + (self.a_1 * self.w_e) + (self.a_2 * self.w_e**2))
# now update vehicle and engine acceleration rates
self.a = (1 / self.m) * (f_x - f_load)
self.w_e_dot = (1 / self.J_e) * (t_e - (self.GR * self.r_e * f_load))
# now update vehicle position, speed and engine speed according to the updated vehicle and engine acceleration rates
# using newton's formulas of motion (assuming constant acceleration during sample time )
self.x = (self.v * self.sample_time) + (0.5 * self.a * self.sample_time**2) + self.x
self.v = (self.a * self.sample_time) + self.v
self.w_e = (self.w_e_dot * self.sample_time) + self.w_e我测试了我的模型的恒定和变化的油门和倾角输入,它的行为与预期。例如,当节气门逐渐增加为零倾斜角时,加速度、速度、车轮角速度和加速度也会增加,当倾斜角不为零时,其行为也会随倾角的变化而变化(也就是说,如果油门太大,由于负载力过大而导致负加速度,汽车将无法行驶)。
下面是一个示例,其中一个恒定的零倾斜节流阀被传递给模型100秒的时间周期,采样时间为0.01秒,然后绘制速度与时间的关系图:
sample_time = 0.01
time_end = 100
model = Vehicle()
t_data = np.arange(0,time_end,sample_time)
v_data = np.zeros_like(t_data)
# throttle percentage between 0 and 1
throttle = 0.2
# incline angle (in radians)
alpha = 0
for i in range(t_data.shape[0]):
v_data[i] = model.v
model.step(throttle, alpha)
plt.plot(t_data, v_data)
plt.show()结果如下图所示:
https://drive.google.com/open?id=1ldPozpuJI24MPdOb9tnyQI03oHKF3W5f
这是合理的,因为汽车加速,直到力平衡,速度变得恒定。
coursera上的平地机提供了一个期望的轨迹(特定的、节气门和倾斜角度在给定的时间内),并要求我将上面提到的模型传递给我,然后将位置状态变量(x)的数据保存到一个文件中,并将其提交给评分系统。但是,每当我尝试这样做时,它总是输出以下信息:
评估失败!!你的轨道偏离太多,或者你的模型是不正确的!
它也没有提供任何关于正确结果的信息。我真的找不出错误的来源,也不知道如何修复它。我知道这是一个很长的问题,但我真的需要帮助,谁能帮上忙吗?
发布于 2019-07-28 01:13:22
我可以成功地找出误差在哪里,结果证明模型的构建是正确的,但当我将轨迹数据(节流阀和倾角α)传递到我的模型时,我犯了一个大错误,我根据时间样本将倾斜角传递给模型,而根据距离给出斜角(即前60m的倾斜角为0.05rad,下90m的斜角为0.1rad),但根据时间将这些数据输入到模型中(即前5秒通过.05的一个倾斜角,10秒pas的斜角为0.1),造成了路径上的轻微偏差,导致了颗粒的拒绝。希望这有一天能帮到某人,谢谢大家。
下面是我首先编写的代码,这是错误的:
time_end = 20
t_data = np.arange(0,time_end,sample_time)
x_data = np.zeros_like(t_data)
throttle_data = np.zeros_like(t_data)
alpha_data = np.zeros_like(t_data)
# reset the states
model.reset()
# ==================================
# Learner solution begins here
# ==================================
# throttle profile
for i in range(499):
throttle_data[i] = (0.06*sample_time*i) + 0.2
throttle_data[500:1499] = 0.5
for i in range(1500,1999):
throttle_data[i] = 2 - (0.1*sample_time*i)
# incline angle profile (in radians)
alpha_data[0:499] = np.arctan(3/60)
alpha_data[500:1499] = np.arctan(9/90)
alpha_data[1500:1999] = 0
for i in range(t_data.shape[0]):
x_data[i] = model.x
model.step(throttle_data[i], alpha_data[i])
# ==================================
# Learner solution ends here
# ==================================
# Plot x vs t for visualization
plt.plot(t_data, x_data)
plt.show()这是正确的代码:
time_end = 20
t_data = np.arange(0,time_end,sample_time)
x_data = np.zeros_like(t_data)
# reset the states
model.reset()
# ==================================
# Learner solution begins here
# ==================================
for i in range(t_data.shape[0]):
x_data[i] = model.x
if t_data[i] < 5:
throttle = (0.3/5)*t_data[i] + 0.2
else:
if t_data[i] < 15:
throttle = 0.5
else:
throttle = (-0.5/5)*t_data[i] + 2
if x_data[i] < 60:
alpha = 0.0499583957
else:
if x_data[i] < 150:
alpha = 0.0996686525
else:
alpha = 0
model.step(throttle, alpha)
# ==================================
# Learner solution ends here
# ==================================
# Plot x vs t for visualization
plt.plot(t_data , x_data)
plt.show()https://stackoverflow.com/questions/57227484
复制相似问题