在生成栅格地图之前,首先需要构造一个用于车辆运动的环境地图(这个地图是用于仿真的真值,对于车辆来说是未知的环境)。我们用0和1值来构造M*N的环境地图,0表示可行驶区域,1表示占用区域。
M = 50
N = 60
true_map = np.zeros((M, N))
true_map[0:10, 0:10] = 1
true_map[30:35, 40:45] = 1
true_map[3:6,40:60] = 1;
true_map[20:30,25:29] = 1;
true_map[40:50,5:25] = 1;
然后构建车辆的运动模型。这里实现了一个简单的运动模型:车辆遇到障碍物或者到达地图边界之前,沿一个方向一直行驶;遇到障碍物或者到达地图边界之后,调整方向继续行驶。
# Initializing the robot's location.
x_0 = [30, 30, 0]
# The sequence of robot motions.
u = np.array([[3, 0, -3, 0], [0, 3, 0, -3]])
u_i = 1
# Initialize the vector of states for our simulation.
x = np.zeros((3, len(time_steps)))
x[:, 0] = x_0
while(Some Conditon...) :
# Perform robot motion.
move = np.add(x[0:2, t-1], u[:, u_i])
# If we hit the map boundaries, or a collision would occur, remain still.
if (move[0] >= M - 1) or (move[1] >= N - 1) or (move[0] <= 0) or (move[1] <= 0) or true_map[int(round(move[0])), int(round(move[1]))] == 1:
x[:, t] = x[:, t-1]
u_i = (u_i + 1) % 4
else:
x[0:2, t] = move
车辆的运动效果如下所示:
最后要构建激光雷达(Lidar)的旋转模型。这里假设在车辆运动过程中,激光雷达(lidar)以0.3/Step的速度持续旋转,对周围的环境进行扫描。
x[2, t] = (x[2, t-1] + w[t]) % (2 * math.pi)
有了地图和车辆运动模型,我们看看如何生成运动车辆上的激光雷达(lidar)扫描数据。
首先,我们需要搞清楚激光雷达的外参和内参,并以此推导出激光雷达(lidar)在Map坐标系下的姿态(x, y,
)和激光雷达(lidar)的激光束的水平和垂直角度分布(激光束的水平和垂直角度分布跟激光雷达自身的硬件属性相关,一般可以从Lidar产品说明书中获取)。
其次,我们需要知道激光雷达(Lidar)的最大扫描范围,超出该范围的区域不能被当前位置的Lidar扫描到,因而是定义为未知区域。最大扫描范围其实也是跟激光雷达自身属性相关的参数
# Parameters for the sensor model.
meas_phi = np.arange(-0.4, 0.4, 0.05)
rmax = 30 # Max beam range.
alpha = 1 # Width of an obstacle (distance about measurement to fill in).
beta = 0.05 # Angular width of a beam.
基于已知环境地图、车辆位置、Lidar激光束分布和Lidar最大扫描范围获取Lidar扫描数据的详细的代码如下:
def get_ranges(true_map, X, meas_phi, rmax):
(M, N) = np.shape(true_map)
x = X[0]
y = X[1]
theta = X[2]
meas_r = rmax * np.ones(meas_phi.shape)
# Iterate for each measurement bearing.
for i in range(len(meas_phi)):
# Iterate over each unit step up to and including rmax.
for r in range(1, rmax+1):
# Determine the coordinates of the cell.
xi = int(round(x + r * math.cos(theta + meas_phi[i])))
yi = int(round(y + r * math.sin(theta + meas_phi[i])))
# If not in the map, set measurement there and stop going further.
if (xi <= 0 or xi >= M-1 or yi <= 0 or yi >= N-1):
meas_r[i] = r
break
# If in the map, but hitting an obstacle, set the measurement range
# and stop ray tracing.
elif true_map[int(round(xi)), int(round(yi))] == 1:
meas_r[i] = r
break
return meas_r
获取激光雷达(Lidar)的测量数据之后,下一步就是将其关联匹配到地图的Map Cell上。主要流程是:
1)将 Lidar bearing与Map Cell相对于传感器的方位进行最小误差匹配,得到影响当前Map Cell的激光束;
匹配的代码如下:
r = math.sqrt((i - x)**2 + (j - y)**2)
phi = (math.atan2(j - y, i - x) - theta + math.pi) % (2 * math.pi) - math.pi
# Find the range measurement associated with the relative bearing.
k = np.argmin(np.abs(np.subtract(phi, meas_phi)))
2) 计算每个Cell被占用的概率。计算完成之后,得到三种不同类型的区域:未探测区域、障碍物区域和非障碍物区域,并赋给它们不同的占用概率。这里将未探测区域的占用概率设为0.5,表示不确定是否占用;障碍物区域占用概率等于0.7,表示大概率被占用;可行驶区域占用概率0.3,表示小概率被占用。
完整的Inverse Scanner Model的实现代码如下:
def inverse_scanner(num_rows, num_cols, x, y, theta, meas_phi, meas_r, rmax, alpha, beta):
m = np.zeros((M, N))
for i in range(num_rows):
for j in range(num_cols):
# Find range and bearing relative to the input state (x, y, theta).
r = math.sqrt((i - x)**2 + (j - y)**2)
phi = (math.atan2(j - y, i - x) - theta + math.pi) % (2 * math.pi) - math.pi
# Find the range measurement associated with the relative bearing.
k = np.argmin(np.abs(np.subtract(phi, meas_phi)))
# If the range is greater than the maximum sensor range, or behind our range
# measurement, or is outside of the field of view of the sensor, then no
# new information is available.
if (r > min(rmax, meas_r[k] + alpha / 2.0)) or (abs(phi - meas_phi[k]) > beta / 2.0):
m[i, j] = 0.5
# If the range measurement lied within this cell, it is likely to be an object.
elif (meas_r[k] < rmax) and (abs(r - meas_r[k]) < alpha / 2.0):
m[i, j] = 0.7
# If the cell is in front of the range measurement, it is likely to be empty.
elif r < meas_r[k]:
m[i, j] = 0.3
return m
Inverse Scanner Model的测量结果如下图所示:
生成概率占位地图的过程就是循环对激光雷达(lidar)的测量结果应用Inverse Scanner Model,然后更新各个Map Cell的Log Odds的过程(详细推导过程参见:自动驾驶Mapping-占位栅格图(Occupancy Grid Map)):
其中:
是Inverse Measurement Model,
是网格i在t-1时刻的置信度(belif),
是Initial belief。
最后,将log odds还原为真实概率,得到每个网格的占位概率值。
生成概率占位地图的代码如下:
meas_rs = []
meas_r = get_ranges(true_map, x[:, 0], meas_phi, rmax)
meas_rs.append(meas_r)
invmods = []
invmod = inverse_scanner(M, N, x[0, 0], x[1, 0], x[2, 0], meas_phi, meas_r, rmax, alpha, beta)
invmods.append(invmod)
ms = []
ms.append(m)
# Main simulation loop.
for t in range(1, len(time_steps)):
# Perform robot motion.
move = np.add(x[0:2, t-1], u[:, u_i])
# If we hit the map boundaries, or a collision would occur, remain still.
if (move[0] >= M - 1) or (move[1] >= N - 1) or (move[0] <= 0) or (move[1] <= 0) or true_map[int(round(move[0])), int(round(move[1]))] == 1:
x[:, t] = x[:, t-1]
u_i = (u_i + 1) % 4
else:
x[0:2, t] = move
x[2, t] = (x[2, t-1] + w[t]) % (2 * math.pi)
# Gather the measurement range data, which we will convert to occupancy probabilities
meas_r = get_ranges(true_map, x[:, t], meas_phi, rmax)
meas_rs.append(meas_r)
# Given our range measurements and our robot location, apply inverse scanner model
invmod = inverse_scanner(M, N, x[0, t], x[1, t], x[2, t], meas_phi, meas_r, rmax, alpha, beta)
invmods.append(invmod)
# Calculate and update the log odds of our occupancy grid, given our measured occupancy probabilities from the inverse model.
L = np.log(np.divide(invmod, np.subtract(1, invmod))) + L - L0
# Calculate a grid of probabilities from the log odds.
m = np.divide(np.exp(L), np.add(1, np.exp(L)))
ms.append(m)
生成概率占用地图的过程如下:
最终生成的概率占用栅格地图如下图所示。可以看看它基本反应了真实的实际车辆运行环境。