# 路径规划: PRM路径规划算法

PRM是一种基于图搜索的方法，它将连续空间转换成离散空间，再利用A*等搜索算法在路线图上寻找路径，以提高搜索效率。这种方法能用相对少的随机采样点来找到一个解，对多数问题而言，相对少的样本足以覆盖大部分可行的空间，并且找到路径的概率为1（随着采样数增加，P（找到一条路径）指数的趋向于1）。显然，当采样点太少，或者分布不合理时，PRM算法是不完备的，但是随着采用点的增加，也可以达到完备。所以PRM是概率完备且不最优的。

The PRM path planner constructs a roadmap in the free space of a given map using randomly sampled nodes in the free space and connecting them with each other. Once the roadmap has been constructed, you can query for a path from a given start location to a given end location on the map.

The probabilistic roadmap planner consists of two phases: a construction and a query phase. 学习阶段：在给定图的自由空间里随机撒点（自定义个数），构建一个路径网络图。查询阶段：查询从一个起点到一个终点的路径。

• Roadmap is a graph G(V, E)  （无向网络图G，其中V代表随机点集，E代表所有可能的两点之间的路径集）

• Robot configuration q→Q_free is a vertex （每个点都要确保机器人与障碍物无碰撞）

• Edge (q1, q2) implies collision-free path between these robot configurations

• A metric is needed for d(q1,q2) (e.g. Euclidean distance)  （Dist function计算Configuration Space中点与点之间的距离，判断是否是同一个点）

• Uses coarse sampling of the nodes, and fine sampling of the edges

• Result: a roadmap in Q_free

## Step 1, Learning the Map

• Initially empty Graph G

• A configuration q is randomly chosen

• If q →Q_free then added to G (collision detection needed here)

• Repeat until N vertices chosen

• For each q, select k closest neighbors，

• Local planner connects q to neighbor q’

• If connect successful (i.e. collision free local path), add edge (q, q’)

PRM.m文件如下，注意下面代码中并没有选取k个最近点进行连接（或是限定连接距离），而是连接了全部的节点。

其它M文件：

## MATLAB Robotics System Toolbox

MATLAB的robotics system toolbox中提供了PRM路径规划方法，可以很方便的创建一个probabilistic roadmap path planner来进行路径规划。使用时有下面几点需要注意：

• Tune the Number of Nodes（调整节点数目）

Increasing the number of nodes can increase the efficiency of the path by giving more feasible paths. However, the increased complexity increases computation time. To get good coverage of the map, you might need a large number of nodes. Due to the random placement of nodes, some areas of the map may not have enough nodes to connect to the rest of the map. In this example, you create a large and small number of nodes in a roadmap.

• Tune the Connection Distance（调整连接距离）

Use the ConnectionDistance property on the PRM object to tune the algorithm. ConnectionDistance is an upper threshold for points that are connected in the roadmap. Each node is connected to all nodes within this connection distance that do not have obstacles between them. By lowering the connection distance, you can limit the number of connections to reduce the computation time and simplify the map. However, a lowered distance limits the number of available paths from which to find a complete obstacle-free path. When working with simple maps, you can use a higher connection distance with a small number of nodes to increase efficiency. For complex maps with lots of obstacles, a higher number of nodes with a lowered connection distance increases the chance of finding a solution.

• Create or Update PRM

This roadmap changes only if you call update or change the properties in the PRM object. When properties change, any method (updatefindpath, or show) called on the object triggers the roadmap points and connections to be recalculated. Because recalculating the map can be computationally intensive, you can reuse the same roadmap by calling findpath with different starting and ending locations. 即当使用update函数进行更新或者改变PRM对象属性后，调用findpath、show等方法会引发重新计算。

The PRM algorithm recalculates the node placement and generates a new network of nodes

• Inflate the Map Based on Robot Dimension

PRM does not take into account the robot dimension while computing an obstacle free path on a map. Hence, you should inflate the map by the dimension of the robot, in order to allow computation of an obstacle free path that accounts for the robot's size and ensures collision avoidance for the actual robot. This inflation is used to add a factor of safety on obstacles and create buffer zones between the robot and obstacle in the environment. The inflate method of an occupancy grid object converts the specified radius to the number of cells rounded up from the resolution*radius value.

•  Find a Feasible Path on the Constructed PRM

Since you are planning a path on a large and complicated map, larger number of nodes may be required. However, often it is not clear how many nodes will be sufficient. Tune the number of nodes to make sure there is a feasible path between the start and end location.

下面在VREP仿真软件中搭建一个简单的场景（修改practicalPathPlanningDemo.ttt）：在地图正上方放置一个视觉传感器采集地图黑白图像，绿色方块代表起始位置，红色方块代表目标位置。然后使用MATLAB中的PRM进行路径规划。

MATLAB代码如下：

下图是MATLAB根据获取到的黑白图像创建的栅格图，并进行路径规划的结果：

下图可以看出方块沿着生成的路径移动到目标位置，只是路径还需要进一步优化。

Occupancy Grids

Path Planning in Environments of Different Complexity

Code for Robot Path Planning using Probabilistic Roadmap