前几年无人机很火,这几年无人车很火。
一个叫UAV(Unmanned Aerial Vehicle),一个叫UGV(Unmanned Ground Vehicle)。
利用python可以写一个基于车辆运动学模型的无人车,具体代码如下,该部分代码参考了这篇博文:https://blog.csdn.net/u013468614/article/details/103489350。
import numpy as np
import matplotlib.pyplot as plt
# set figure size
plt.figure(figsize=(20, 3))
# define UGV class
class UGV_model:
def __init__(self, x0, y0, theta0, v0, L, T):
self.x = x0
self.y = y0
self.theta = theta0
self.v = v0
self.l = L
self.dt = T
def update(self,deltat):
dx_vs_dt = self.v*np.cos(self.theta)
dy_vs_dt = self.v*np.sin(self.theta)
dtheta_vs_dt = self.v*np.tan(deltat)/self.l
self.x += dx_vs_dt*self.dt
self.y += dy_vs_dt*self.dt
self.theta += dtheta_vs_dt*self.dt
def draw(self):
plt.scatter(self.x, self.y, color='r')
plt.axis([0, 20, -1, 2])
plt.grid(linestyle=":")
# set reference trajectory
refer_traj = np.ones((100,2))
refer_traj[:,0] = np.linspace(0,20,100)
# draw reference trajectory
plt.plot(refer_traj[:,0], refer_traj[:,1], '-.b', linewidth=2.0)
# an UGV instance
ugv = UGV_model(0, 0, 0, 2.0, 2.5, 0.1)
for i in range(1000):
if(i%2==0):
ugv.update(np.pi/6)
else:
ugv.update(-np.pi/6)
ugv.draw()
代码的执行结果如下图所示,蓝色线代表参考轨迹,红色线是跟踪轨迹(仿真时前轮转角随便给的值,并没有植入具体的路径跟踪算法)。