仿真环境对于机器人研究来讲太太重要,一个好的仿真环境可以逼真的模拟真实的场景、拥有高保真的物理引擎,可以比较真实的反映算法的性能。可是有时,咱们有一个Idea,想立刻实现,从而快速的验证这个idea的可行性,那么一款小巧、简洁的仿真环境可能更加合适。在获得验证有那么点意思后,再利用较真实的仿真环境验证,进一步,再放在实际环境中进行测试。通常来讲,简单仿真环境——高保真仿真环境——真实环境,同一算法所需的代码量是递增的(由于要考虑的外围因素会增多,再加上接口代码)。python
import numpy as np import matplotlib import matplotlib.pyplot as plt %matplotlib inline # set up matplotlib is_ipython = 'inline' in matplotlib.get_backend() if is_ipython: from IPython import display plt.ion() plt.figure(figsize=(18, 3)) class UGV_model: def __init__(self, x0, y0, theta0, L, v0, T): # L:wheel base self.x = x0 # X self.y = y0 # Y self.theta = theta0 # headding self.l = L # wheel base self.v = v0 # speed self.dt = T # decision time periodic def update(self, vt, deltat): # update ugv's state dx = self.v*np.cos(self.theta) dy = self.v*np.sin(self.theta) dtheta = self.v*np.tan(deltat)/self.l self.x += dx*self.dt self.y += dy*self.dt self.theta += dtheta*self.dt def plot_duration(self): plt.scatter(self.x, self.y, color='r') plt.axis([0, 18, -3, 3]) if is_ipython: display.clear_output(wait=True) display.display(plt.gcf()) # set reference trajectory refer_path = np.zeros((100, 2)) refer_path[:,0] = np.linspace(0, 18, 100) plt.plot(refer_path[:,0], refer_path[:,1], '-.b', linewidth=5.0) ugv = UGV_model(0, 0, 0, 2.86, 2.0, 0.1) for i in range(1000): ugv.update(2.0, np.cos(i/5.0)) ugv.plot_duration()
简单测试(方向盘转角以余弦周期性变化):
web
简单的仿真环境的模型最简单,预测偏差也最大,每每与真实环境中跑的结果相差很大。仿真环境越好,运行效果与真实环境越接近。算法