"""Simulatorauthor: Atsushi Sakai"""import numpy as npimport matplotlib.pyplot as pltimport mathimport randomfrom scipy.spatial.transform import Rotation as Rotclass VehicleSimulator:def __init__(self, i_x, i_y, i_yaw, i_v, max_v, w, L):self.x = i_xself.y = i_yself.yaw = i_yawself.v = i_vself.max_v = max_vself.W = wself.L = Lself._calc_vehicle_contour()def update(self, dt, a, omega):self.x += self.v * np.cos(self.yaw) * dtself.y += self.v * np.sin(self.yaw) * dtself.yaw += omega * dtself.v += a * dtif self.v >= self.max_v:self.v = self.max_vdef plot(self):plt.plot(self.x, self.y, ".b")# convert global coordinategx, gy = self.calc_global_contour()plt.plot(gx, gy, "--b")def calc_global_contour(self):rot = Rot.from_euler('z', self.yaw).as_matrix()[0:2, 0:2]gxy = np.stack([self.vc_x, self.vc_y]).T @ rotgx = gxy[:, 0] + self.xgy = gxy[:, 1] + self.yreturn gx, gydef _calc_vehicle_contour(self):self.vc_x = []self.vc_y = []self.vc_x.append(self.L / 2.0)self.vc_y.append(self.W / 2.0)self.vc_x.append(self.L / 2.0)self.vc_y.append(-self.W / 2.0)self.vc_x.append(-self.L / 2.0)self.vc_y.append(-self.W / 2.0)self.vc_x.append(-self.L / 2.0)self.vc_y.append(self.W / 2.0)self.vc_x.append(self.L / 2.0)self.vc_y.append(self.W / 2.0)self.vc_x, self.vc_y = self._interpolate(self.vc_x, self.vc_y)@staticmethoddef _interpolate(x, y):rx, ry = [], []d_theta = 0.05for i in range(len(x) - 1):rx.extend([(1.0 - theta) * x[i] + theta * x[i + 1]for theta in np.arange(0.0, 1.0, d_theta)])ry.extend([(1.0 - theta) * y[i] + theta * y[i + 1]for theta in np.arange(0.0, 1.0, d_theta)])rx.extend([(1.0 - theta) * x[len(x) - 1] + theta * x[1]for theta in np.arange(0.0, 1.0, d_theta)])ry.extend([(1.0 - theta) * y[len(y) - 1] + theta * y[1]for theta in np.arange(0.0, 1.0, d_theta)])return rx, ryclass LidarSimulator:def __init__(self):self.range_noise = 0.01def get_observation_points(self, v_list, angle_resolution):x, y, angle, r = [], [], [], []# store all pointsfor v in v_list:gx, gy = v.calc_global_contour()for vx, vy in zip(gx, gy):v_angle = math.atan2(vy, vx)vr = np.hypot(vx, vy) * random.uniform(1.0 - self.range_noise,1.0 + self.range_noise)x.append(vx)y.append(vy)angle.append(v_angle)r.append(vr)# ray casting filterrx, ry = self.ray_casting_filter(angle, r, angle_resolution)return rx, ry@staticmethoddef ray_casting_filter(theta_l, range_l, angle_resolution):rx, ry = [], []range_db = [float("inf") for _ in range(int(np.floor((np.pi * 2.0) / angle_resolution)) + 1)]for i in range(len(theta_l)):angle_id = int(round(theta_l[i] / angle_resolution))if range_db[angle_id] > range_l[i]:range_db[angle_id] = range_l[i]for i in range(len(range_db)):t = i * angle_resolutionif range_db[i] != float("inf"):rx.append(range_db[i] * np.cos(t))ry.append(range_db[i] * np.sin(t))return rx, rydef main():print("start!!")print("done!!")if __name__ == '__main__':main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。